image processing in c

Asked by jagannath

i'm unable to read an jpeg image in c ... i had written the following code ...


{ int mat[101][100];
 FILE *fp,*fo,*fl;
 int n,init,i,j,k;
 char ch=0;
 fp= fopen("len.jpg","rb"); // opens the image to be read which is of 100*99 dimension, read mode, pixel range is from 0-255. 0-black, 255-white
 fo=fopen("origin.png","w"); // blank files in write mode
        fprintf(fo,"P5 %d %d %d ",176,144,255);
// this a routine to read the pixel values of len.jpg and write into file f1 i.e., pix.c. pix.c will contain the pixel values after executing the program.
 for( n=0;n<101;n++)

  for( k=0;k<100;k++)

                  mat[n][k]= fgetc(fp);
    fprintf(fl,"%d ",mat[n][k]);

// this a routine to write the pixel values from pix.c. origin.png will contain the constructed image from pixel values in pix.c
 for( n=0;n<101;n++)

       for( k=0;k<100;k++)




wen i executed the above code i got the entirely different image other than the original one(len.jpg)

Question information

English Edit question
Ubuntu gcc-defaults Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
Revision history for this message
mycae (mycae) said :

That is not how jpeg works. That code is simply incorrect.

1) JPEGs have metadata about the file contents in the header
2) Jpegs have chunked data
3, and this is important) JPEGS do *not* store pixel values. They store the amplitude of coefficients of so-called "basis functions" -- this means maths.

If you want to read an image in a program, you can either implement a jpeg reader which takes into account these things (hard). Or, you can use a pre-made library, like libjpeg, or imagemagick.

Revision history for this message
jagannath (jagsowjagnath) said :

oh ok ... i did'n kno this ... and using that lib can i get the pixel values ... and i also need to modify the pixels somehow and write back it to the image ... can i do that in that jpeg lib ???

Revision history for this message
mycae (mycae) said :

Yes. I would recommend, for you, to use imagemagick - as it is easier to work with than directly working with libjpeg. You may want to locate some imagmagick tutorials.

You also need to learn how to link with external libraries in C. Under linux this is not too hard. Under other OSs, this is often more complex.

Please don't underestimate how long this can take - there are many subtle points in image formats and image data.

Revision history for this message
mycae (mycae) said :

Next point:

>int mat[101][100];

Allocating memory using this method uses so-called "stack space". Don't use this for large data ( as a rough rule of thumb anything more than 1kb, say.) Most programs will operate with limited stack memory - you need to get "heap" memory by using malloc.

Your program may just crash if you keep increasing the size of that array. The limit at which it is set is given by the command ulimit -s. For my system, it is limited to 8MB. You are, in your example, using 39 kB.

when your program gets large, or if you bump the array size to a 2048x2048 image, your program would crash on my system.

Revision history for this message
jagannath (jagsowjagnath) said :

can i have any website which gives me some tutorial about imagemagick ??
also where i can get the example codes for "jpeg " image in particular !!

Revision history for this message
Best mycae (mycae) said :

You should be able to find any with your favourite search engine, in addition to the link I posted previously. I can't recommend any particular one.

Revision history for this message
jagannath (jagsowjagnath) said :

Thanks mycae, that solved my question.