LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2009, 11:05 AM   #1
waqasdaar
Member
 
Registered: Jan 2009
Location: Stockholm Sweden
Distribution: Ubuntu 9.10
Posts: 57

Rep: Reputation: 15
Question 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

Last edited by waqasdaar; 02-28-2009 at 11:09 AM.
 
Old 02-28-2009, 01:00 PM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
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.
 
Old 02-28-2009, 02:21 PM   #3
waqasdaar
Member
 
Registered: Jan 2009
Location: Stockholm Sweden
Distribution: Ubuntu 9.10
Posts: 57

Original Poster
Rep: Reputation: 15
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.
 
Old 02-28-2009, 02:52 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

Last edited by theNbomr; 02-28-2009 at 02:53 PM.
 
Old 02-28-2009, 03:13 PM   #5
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
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.
 
Old 02-28-2009, 07:26 PM   #6
waqasdaar
Member
 
Registered: Jan 2009
Location: Stockholm Sweden
Distribution: Ubuntu 9.10
Posts: 57

Original Poster
Rep: Reputation: 15
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.
 
Old 02-28-2009, 08:56 PM   #7
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
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.
 
Old 02-28-2009, 10:39 PM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by waqasdaar View Post
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 View Post
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.
 
Old 03-01-2009, 11:36 AM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

Last edited by theNbomr; 03-01-2009 at 11:39 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
read jpg metadata in c myrmidon Programming 3 01-21-2007 08:36 PM
convert image.png image.jpg changing colours MikeyCarter Linux - Software 1 11-17-2006 10:16 AM
jpg image browsing slow and crashes - eog gthumb JacekZ Linux - Software 2 05-25-2006 04:12 PM
whats a good program to read jpg files? :) sirpelidor Linux - Software 5 03-12-2004 03:42 AM
C++ and low-level .jpg image operations? KendersPlace Programming 3 08-22-2003 12:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration