473,498 Members | 310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

creating a 2D array

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
the 2d array will represent an image.
I managed to get the imagedata using jpeg library of cygwin and it
stores the rgb values in array pointed to by unsigned char *image .and
i am able to get the rgb values for each pixel and pack them into a
long using a packrgb(...) function that does some bit shifting

long packrgb(int r,int g,int b){
int a;
a=255;
long res;
res=(255 << 24) | ((r & 0xff) << 16)|((g & 0xff) << 8)|(b &
0xff);
return res;
}

long * readrgbjpegfile(char* filename){
.....
/* using library i get values into unsigned char *image */
....
int imagearea=imagewidth* imgheight;/* is equal to num of pixels in
an image */

/* i take the r,g,b of each pixel pack it into long and store it in an
array */
long * imgpixels=malloc(imagearea*sizeof(imgpixels));
int r,g,b,index,pixval;
i=0;
index=0;
while(index<imagearea){
r=image[i++];
g=image[i++];
b=image[i++];
printf("r:%d,g:%d,b:%d\n",r,g,b);
pixval=packrgb(r,g,b);
printf("pixval:%d\n",pixval);
imgpixels[index]=pixval;
index++;
}

return imgpixels;
}

upto here i am getting the needed functionality..but i am not sure how
to go about creating the 2d array and set each row..i am not quite
sure about the memory allocation part too

any advise/help will be most welcome..also as a newbie i am not sure
if the above is the correct way of coding
thanx
oharry
Feb 2 '08 #1
2 2960

<os**********@gmail.comwrote in message
2d array with numof rows=numof images and numof cols=numof pixels in
each image.ie each row of
upto here i am getting the needed functionality..but i am not sure how
to go about creating the 2d array and set each row..i am not quite
sure about the memory allocation part too
Ideally we'd do this.
Color image[height][width];

for(y=0;y<height;y++)
for(x=0;x<width;x++)
{
red image[y][x].red;
green = image[y][x].green;
blue = image[y][x].blue;
}

Unfortunately width and height are probably not know at compile time.

The only practical solution is this

unsigned char *image = malloc(width * height * 3);
/* check allocation succeeded here */
/* set image to colour values */

/* extract values like this */
for(y=0;y<height;y++)
for(x=0;x<width;x++)
{
red = image[(y*width + x) * 3];
green = image[(y*width+x) *3 + 1];
blue = image[(y*width+x) * 3 + 2];
}

It's a nuisance, and in C99 it is no longer necessary, as dimensions can be
specified at runtime. However C99 is not widely implemented, so managing the
array manually is the only real answer.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Feb 2 '08 #2
os**********@gmail.com writes:
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
the 2d array will represent an image.
<snip>
long * readrgbjpegfile(char* filename){
....
/* using library i get values into unsigned char *image */
...
int imagearea=imagewidth* imgheight;/* is equal to num of pixels in
an image */
size_t is a better type for this kind of value.
/* i take the r,g,b of each pixel pack it into long and store it in an
array */
long * imgpixels=malloc(imagearea*sizeof(imgpixels));
this should really be:

long *imgpixels = malloc(imagearea * sizeof(long));
or
long *imgpixels = malloc(imagearea * sizeof *imgpixels);

You have been saved by the fact that sizeof(long) == sizeof(long *) on
your machine.

You should take some action if malloc fails. Just using the pointer
(when you were not given any memory) is not a good idea!
int r,g,b,index,pixval;
i=0;
index=0;
while(index<imagearea){
r=image[i++];
g=image[i++];
b=image[i++];
printf("r:%d,g:%d,b:%d\n",r,g,b);
pixval=packrgb(r,g,b);
printf("pixval:%d\n",pixval);
imgpixels[index]=pixval;
index++;
}

return imgpixels;
}

upto here i am getting the needed functionality..but i am not sure how
to go about creating the 2d array and set each row..i am not quite
sure about the memory allocation part too
So you have a function 'long *readrgbjpegfile(char* filename)' that
returns the image in the format you want. To store an array of them,
from an array of file names you do something like this:

long **read_files(char **files, size_t n_files)
{
long **images = malloc(n_files * sizeof *images);
if (images != NULL) {
int i;
for (i = 0; i < n_files; i++)
images[i] = readrgbjpegfile(files[i]);
}
return images;
}

If readrgbjpegfile returns a valid pointer or NULL on failure, then you
have an array whose contents you can free safely.
any advise/help will be most welcome..also as a newbie i am not sure
if the above is the correct way of coding
Add more spaces. I would be tempted to define (using typedef) a type
for a pixel rather than relying on long. It will help reader to know
what is pixel data and what is a long for some other reason. One day,
long might be 64 bits on your compiler of choice and a single typedef
change can prevent wasting all those bits.

You could look into avoiding all the image copying but putting the RGB
values you get from the JPEG decoder into you long pixels directly.

--
Ben.
Feb 2 '08 #3

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

Similar topics

5
28328
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
3081
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
2985
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
1909
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
3750
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
2956
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
2811
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
1994
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
3710
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 =...
0
7124
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
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.