image processing in c

Asked by jagannath

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

#include<stdio.h>

main()
{ 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
        fl=fopen("pix.c","w");
        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++)

   fprintf(fo,"%c",mat[n][k]);
 }

 fclose(fp);
 fclose(fo);
        fclose(fl);

}

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

Question information

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

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.

https://en.wikipedia.org/wiki/Libjpeg
http://www.imagemagick.org/script/magick-core.php?ImageMagick=hrna4r8jspa8jnn17ld3t33et4

Revision history for this message
jagannath (jagsowjagnath) said :
#2

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 :
#3

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 :
#4

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 :
#5

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 :
#6

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

Thanks mycae, that solved my question.