creating a 2D array

O

oswald.harry

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
 
M

Malcolm McLean

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.
 
B

Ben Bacarisse

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.
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 = readrgbjpegfile(files);
}
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top