LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to read jpg image in C (https://www.linuxquestions.org/questions/programming-9/how-to-read-jpg-image-in-c-708217/)

waqasdaar 02-28-2009 11:05 AM

How to read jpg image in C
 
Hi,

Can any one tell how Can I read a jpg image in C and store into an byte array. I tried but I think I am doing something wrong. Can you correct me please.


Code:

int main (int argc , char * argv [] )
{

        // argv[1] = 1.jpg

        FILE *file;
        char *buffer;
        unsigned long fileLen;

        //Open file
        file = fopen(argv[1], "rb");
        if (!file)
        {
                fprintf(stderr, "Unable to open file %s", argv[1]);
                return;
        }

        //Get file length
        fseek(file, 0, SEEK_END);
        fileLen=ftell(file);
        fseek(file, 0, SEEK_SET);

        //Allocate memory
        buffer=(char *)malloc(fileLen+1);
        if (!buffer)
        {
                fprintf(stderr, "Memory error!");
                                fclose(file);
                return 1;
        }

      read(file,&buffer,sizeof(buffer));
      fclose(file);

      int i=0;

while (i < sizeof(buffer))
{
    printf("%02X",(int)buffer[i]);
    i++;
}

        return 0;
}

When I tried to print the buffer it does not display anything.

I would be very thankful to you. if you tell me what I am doing wrong.

Thanks in Advanced

wje_lq 02-28-2009 01:00 PM

buffer is not the buffer content itself, but the pointer to the buffer. Thus, it was quite proper for you to use malloc() to place a pointer in buffer.

Of course, sizeof(buffer) is 4 on 32-bit systems (probably 8 on 64-bit systems).

When you read(), you're reading not into the buffer you've just allocated, but into the pointer itself. And you're probably reading four bytes into that pointer, thus wiping out the result from the malloc() call.

Then, in your printf() statement, you're properly using buffer as a pointer to the actual data. But by this time the pointer has been wiped out. The chances should be good that you get a SIGSEGV signal, causing your program to die. I didn't get that; I ended up with a fake "pointer" to four bytes in my address space which happened to have all 32 bits off. So I got as output:
Code:

00000000
with no line feed at the end.

waqasdaar 02-28-2009 02:21 PM

Can any one help me how can I read an image file in C and store the information in byte array.

I would be very thankful to you.

theNbomr 02-28-2009 02:52 PM

Your question is somewhat ambiguous, given the specification of JPG image files. If you merely want to read the file binary data into a buffer, then you are virtually there; just a couple of bugs that have already been mentioned.
Code:

int main (int argc , char * argv [] ){

        // argv[1] = 1.jpg

        FILE *file;
        unsigned char *buffer;
        unsigned long fileLen;

        //Open file
        file = fopen(argv[1], "rb");
        if (!file)
        {
                fprintf(stderr, "Unable to open file %s", argv[1]);
                return;
        }

        //Get file length
        fseek(file, 0, SEEK_END);
        fileLen=ftell(file);
        fseek(file, 0, SEEK_SET);

        //Allocate memory
        buffer=(char *)malloc(fileLen);
        if (!buffer)
        {
                fprintf(stderr, "Memory error!");
                                fclose(file);
                return 1;
        }

      fread(buffer,fileLen,sizeof(unsigned char),file);
      fclose(file);

      int i=0;

      while (i < fileLen){
          printf("%02X ",((unsigned char)buffer[i]);
          i++;
          if( ! (i % 16) ) printf( "\n" );
      }

        return 0;
}

However, since you have stipulated that this is a JPG image file, you may want to extract the actual image data from the file, which requires considerable manipulation of the file data. Fortunately, such a common manipulation is already encapsulated in a library API, as libjpeg. There is documentation and example code include with libjpeg and your distro may already include the library.
--- rod.

wje_lq 02-28-2009 03:13 PM

Quote:

such a common manipulation is already encapsulated in a library API, as libjpeg. There is documentation and example code include with libjpeg and your distro may already include the library.
What theNbomr said.

If your distribution doesn't include libjpeg, you can get it here.

waqasdaar 02-28-2009 07:26 PM

I have installed the libpng API on my system (Fedore 9). But when I tried to compile the code I get this error.

png.c:(.text+0xb1)undefined refrence to 'png_sig_cmp'

What I understand so far that, It is not understanding the functions in the program. and it is not able to find the 'png.h' header file. I have checked with the 'whereis png.h' "png.h" is installed on the system. but I dont know what is the problem. Can any one help me.

Thanks in Advanced.

fantas 02-28-2009 08:56 PM

If there are undefined references then it's the linker that's complaining. In the case of the previous post, it looks for the png library.

wje_lq 02-28-2009 10:39 PM

Quote:

Originally Posted by waqasdaar (Post 3460912)
I have installed the libpng API on my system (Fedore 9). But when I tried to compile the code I get this error.

png.c:(.text+0xb1)undefined refrence to 'png_sig_cmp'

First, when posting an error message, please please please copy and paste it, rather than typing it in by hand. Sometimes it's the smallest detail that makes all the difference in helping you. (Though this time that doesn't seem to be the case.)

Seond:

Quote:

Originally Posted by fantas (Post 3460952)
If there are undefined references then it's the linker that's complaining. In the case of the previous post, it looks for the png library.

This. I did the following:
Code:

nm /usr/lib/libpng.a | grep png_sig_cmp
and got this:
Code:

000001f0 T png_sig_cmp
        U png_sig_cmp
        U png_sig_cmp

That shows that the symbol is, indeed, defined in /usr/lib/libpng.a.

So the next step is this. What happens when you do this at the command line?
Code:

ls -l  /usr/lib/libpng.a
ls -lH /usr/lib/libpng.a

Post your answer and let's take a look.

theNbomr 03-01-2009 11:36 AM

And, using libpng is probably not going to be helpful for decoding JPEG images, but that is a different matter.
To use a 'foreign' library in your C code, there are a few general steps that need to be taken to pull all of the pieces together. The compiler needs to have prototype information regarding how the public functions in the library are called. These can usually be found in header files supplied with the library, but sometimes you need a '_devel' version of the library to get such headers. Next, having specified '#include <whatever.h>', the compiler may need to be told where it can find said header file. In gcc and many other C compilers, this is accomplished with the '-I' (uppercase 'eye') commandline argument to the compiler. Often, if there is a Makefile used to build the package, the approriate '-I' flag will be embedded or built up in the CFLAGS macro.
Now, on to the linker. The linker needs to know both the name of each library to search for public symbols, and where to look for such library. The names of each library are supplied as commandline arguments with the '-l' (lowercase ell) option. In each case, the name of the library, minus the usual 'lib' prefix dropped is given. In you case, you would wish to apply a commandline argument of '-lpng', which says to link with 'libpng'. You can tell where to look for all libraries with the '-L' option. Using a conventional Makefile, this option would be embedded in the LDFLAGS macro.
In summary, both the compiler and linker need to be told what to look for, and where to look to find any same type file. If you always provide this information, you should never bump into the problem you are having. For details

man gcc
man ld

--- rod.


All times are GMT -5. The time now is 04:27 PM.