ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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.
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.
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.
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
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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.