473,221 Members | 1,722 Online

# 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 3880
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

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: 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... 1 by: isladogs | last post by: The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike... 0 by: VivesProcSPL | last post by: Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for... 0 by: jianzs | last post by: Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from... 0 by: abbasky | last post by: ### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method... 2 by: jimatqsi | last post by: The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art... 2 by: isladogs | last post by: The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE... 0 by: stefan129 | last post by: Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific... 0 by: egorbl4 | last post by: Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ... 0 by: MeoLessi9 | last post by: I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....