473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Creating a 3D Array

What's the best data structure to use if I want to store RGB color data
(integers between 0-255 for red, green, and blue) for each pixel in an
image?

A vector of vector of vector of ints? (my best guess)

If I'm going to need to store data for an image that's 10k by 10k
pixels, how can I quickly allocate and save that data into the data
structure?

Thanks,
Joe
Mar 20 '06 #1
7 3891
Joe Van Dyk <jo********@boeing.com> wrote:
What's the best data structure to use if I want to store RGB color
data (integers between 0-255 for red, green, and blue) for each pixel
in an image?

A vector of vector of vector of ints? (my best guess)

I would use a single vector of an appropriate struct:

struct color
{
unsigned char r, g, b;
};

std::vector <color> v;

And then use indexing arithmetics to get the right element.
If I'm going to need to store data for an image that's 10k by 10k
pixels, how can I quickly allocate and save that data into the data
structure?


v.resize (10000 * 10000); // get ~286Mb
For indexing you would do: x + y * width

hth
--
jb

(reply address in rot13, unscramble first)
Mar 20 '06 #2
Joe Van Dyk wrote:
What's the best data structure to use if I want to store RGB color data
(integers between 0-255 for red, green, and blue) for each pixel in an
image?

A vector of vector of vector of ints? (my best guess)

If I'm going to need to store data for an image that's 10k by 10k
pixels, how can I quickly allocate and save that data into the data
structure?

Thanks,
Joe


Borrow the COLORREF mechanism from win32:

#define RGB(r, g ,b) ((DWORD) (((BYTE) (r) | \
((WORD) (g) << 8)) | \
(((DWORD) (BYTE) (b)) << 16)))

typedef DWORD COLORREF;

std::vector< std::vector<COLORREF> > colorArray;
COLORREF black = RGB(0,0,0); // initialize to black
colorArray.resize(10000, std::vector<COLORREF>(10000, black));

Then you can grab a pixel thus:

COLORREF rgb = colorArray[x][y];

Be sure to catch exceptions, that's a pretty big array.

-York
Mar 20 '06 #3
In article <dv*************@news.t-online.com>,
"Jakob Bieling" <ar****************@rot13.com> wrote:
Joe Van Dyk <jo********@boeing.com> wrote:
What's the best data structure to use if I want to store RGB color
data (integers between 0-255 for red, green, and blue) for each pixel
in an image?

A vector of vector of vector of ints? (my best guess)

I would use a single vector of an appropriate struct:

struct color
{
unsigned char r, g, b;
};

std::vector <color> v;

And then use indexing arithmetics to get the right element.
If I'm going to need to store data for an image that's 10k by 10k
pixels, how can I quickly allocate and save that data into the data
structure?


v.resize (10000 * 10000); // get ~286Mb
For indexing you would do: x + y * width


However, for something that big you might be better off with a deque...
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Mar 20 '06 #4
Sgt. York wrote:
Joe Van Dyk wrote:
What's the best data structure to use if I want to store RGB color
data (integers between 0-255 for red, green, and blue) for each pixel
in an image?

A vector of vector of vector of ints? (my best guess)

If I'm going to need to store data for an image that's 10k by 10k
pixels, how can I quickly allocate and save that data into the data
structure?

Thanks,
Joe


Borrow the COLORREF mechanism from win32:

#define RGB(r, g ,b) ((DWORD) (((BYTE) (r) | \
((WORD) (g) << 8)) | \
(((DWORD) (BYTE) (b)) << 16)))

typedef DWORD COLORREF;


You genuinely recommend this?

What's wrong with C++?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 21 '06 #5
Ben Pope wrote:
Sgt. York wrote:
Joe Van Dyk wrote:
What's the best data structure to use if I want to store RGB color
data (integers between 0-255 for red, green, and blue) for each pixel
in an image?

A vector of vector of vector of ints? (my best guess)

If I'm going to need to store data for an image that's 10k by 10k
pixels, how can I quickly allocate and save that data into the data
structure?

Thanks,
Joe


Borrow the COLORREF mechanism from win32:

#define RGB(r, g ,b) ((DWORD) (((BYTE) (r) | \
((WORD) (g) << 8)) | \
(((DWORD) (BYTE) (b)) << 16)))

typedef DWORD COLORREF;


You genuinely recommend this?

What's wrong with C++?

Ben Pope


Hey Mr. Pope, nice way to rip that out of context. My entire reply
showed how to store these integers in a simple 2D array of std::vectors.
Last time I checked, STL's vectors were traditionally considered "C++"
....

You can use anything you want to hold the color values, I've just found
the packing of colors bytes this way to be useful. You might be
surprised to hear this works in C++. If your panties are bunched up
because of the "DWORD" and "BYTE" defines, here's a revelation: They are
really just "32-bit unsigned ints" and "unsigned chars," respectively.
Chutzpah!
Mar 21 '06 #6
Sgt. York wrote:
Ben Pope wrote:
Sgt. York wrote:
Joe Van Dyk wrote:
What's the best data structure to use if I want to store RGB color
data (integers between 0-255 for red, green, and blue) for each
pixel in an image?

A vector of vector of vector of ints? (my best guess)

If I'm going to need to store data for an image that's 10k by 10k
pixels, how can I quickly allocate and save that data into the data
structure?

Thanks,
Joe

Borrow the COLORREF mechanism from win32:

#define RGB(r, g ,b) ((DWORD) (((BYTE) (r) | \
((WORD) (g) << 8)) | \
(((DWORD) (BYTE) (b)) << 16)))

typedef DWORD COLORREF;
You genuinely recommend this?

What's wrong with C++?

Ben Pope


Hey Mr. Pope, nice way to rip that out of context. My entire reply
showed how to store these integers in a simple 2D array of std::vectors.
Last time I checked, STL's vectors were traditionally considered "C++" ...


Which I don't have a problem with, and was not commenting on. Hence, I
removed it.
You can use anything you want to hold the color values, I've just found
the packing of colors bytes this way to be useful. You might be
surprised to hear this works in C++.
I'm not at all surprised that it works. I just think here are better
ways of doing it.
If your panties are bunched up
because of the "DWORD" and "BYTE" defines, here's a revelation: They are
really just "32-bit unsigned ints" and "unsigned chars," respectively.
Chutzpah!


Actually my concern was the macro:

#define RGB(r, g ,b) ((DWORD) (((BYTE) (r) | \
((WORD) (g) << 8)) | \
(((DWORD) (BYTE) (b)) << 16)))
Something a little nicer might be:

struct rgb {
rgb(unsigned char red, unsigned char green, unsigned char blue) :
r(red), b(blue), g(green) {}
unsigned char r;
unsigned char g;
unsigned char b;
};
Or perhaps:

class rgb {
public:
rgb(unsigned char red, unsigned char green, unsigned char blue) :
val_(r | g<<8 | b<<16) {}

/* suitable functions to set & retrieve components */

private:
// 32 bit unsigned type
typedef unsigned int rgb_t;
rgb_t val_;
};
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 21 '06 #7
Ben Pope wrote:
Sgt. York wrote:
Ben Pope wrote:
Sgt. York wrote:
Joe Van Dyk wrote:
> What's the best data structure to use if I want to store RGB color
> data (integers between 0-255 for red, green, and blue) for each
> pixel in an image?
>
> A vector of vector of vector of ints? (my best guess)
>
> If I'm going to need to store data for an image that's 10k by 10k
> pixels, how can I quickly allocate and save that data into the data
> structure?
>
> Thanks,
> Joe

Borrow the COLORREF mechanism from win32:

#define RGB(r, g ,b) ((DWORD) (((BYTE) (r) | \
((WORD) (g) << 8)) | \
(((DWORD) (BYTE) (b)) << 16)))

typedef DWORD COLORREF;

You genuinely recommend this?

What's wrong with C++?

Ben Pope


Hey Mr. Pope, nice way to rip that out of context. My entire reply
showed how to store these integers in a simple 2D array of
std::vectors. Last time I checked, STL's vectors were traditionally
considered "C++" ...


Which I don't have a problem with, and was not commenting on. Hence, I
removed it.
You can use anything you want to hold the color values, I've just
found the packing of colors bytes this way to be useful. You might be
surprised to hear this works in C++.


I'm not at all surprised that it works. I just think here are better
ways of doing it.
If your panties are bunched up because of the "DWORD" and "BYTE"
defines, here's a revelation: They are really just "32-bit unsigned
ints" and "unsigned chars," respectively. Chutzpah!


Actually my concern was the macro:

#define RGB(r, g ,b) ((DWORD) (((BYTE) (r) | \
((WORD) (g) << 8)) | \
(((DWORD) (BYTE) (b)) << 16)))
Something a little nicer might be:

struct rgb {
rgb(unsigned char red, unsigned char green, unsigned char blue) :
r(red), b(blue), g(green) {}
unsigned char r;
unsigned char g;
unsigned char b;
};
Or perhaps:

class rgb {
public:
rgb(unsigned char red, unsigned char green, unsigned char blue) :
val_(r | g<<8 | b<<16) {}

/* suitable functions to set & retrieve components */

private:
// 32 bit unsigned type
typedef unsigned int rgb_t;
rgb_t val_;
};
Ben Pope


Now you've made a good point. I took your previous post as a slam
because you did not offer this alternative advice.

I've gotten use to windows development and since the color macros are
everywhere (from 3.0 to Vista), I've taken them for granted. But you
are correct, it's not the purist way and macros are rather "stinky" and
should be thrown to the dogs when the opportunity arises.

-York

Mar 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Keiron Waites | last post by:
<script language="JavaScript" type="text/javascript"> <!-- var array1 = new Array(); var array1i = new Array(); array1 = array1i; alert(array1); // --> </script>
20
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then...
7
by: kaul | last post by:
i want to create a 2-d array containg r rows and c columns by dynamic memory allocation in a single statement so that i will be able to access the ith and jth index as say arr how is that...
5
by: Chris | last post by:
Hi, to create an array of 2 objects (e.g. of type '__gc class Airplane') I need to do : Airplane * arrAirplanes __gc = new Airplane* __gc; arrAirplanes = new Airplane("N12344"); arrAirplanes...
5
by: jwgoerlich | last post by:
Hello, I need to create a 1 GB Byte array in memory. I can create it on a Win2000 system. On a Win2003 system, the application throws a System.OutOfMemoryException error. Both are running the...
38
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've...
8
by: ctiggerf | last post by:
I was hopeing someone could help me out here. Been stumped on this one all day. This function 1. Checks uploaded files. 2. Creates two resized images from each (a full size, and a...
13
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable...
11
by: Matthew Wells | last post by:
Hello. I have figured out how to create an instance of an object only knowing the type by string. string sName = "MyClassName"; Type t = Type.GetType(sName); Object objNew =...
2
by: oswald.harry | last post by:
hi i am reading a set of jpeg files(RGB) and extracts the pixel values as longs.i want to create a 2d array with numof rows=numof images and numof cols=numof pixels in each image.ie each row of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.