Reading JPG File

J

jcasique.torres

Hi everyboy.

I trying to create a C promang in an AIX System to read JPG files but
when it read just the first 4 bytes when it found a DLE character (^P)
doesn't read anymore. I using fread function. Here a few lines:

char *sAnv;
....

sprintf(file_a, "%s/anverso.jpg", strDir);

if ((fImagen = fopen(file_a,"rb")) == NULL)
{
printf("ERROR: No se pudo abrir el archivo |%s|\n", file_a);
continue;
}

fseek(fImagen, 0, SEEK_END);
lLongitud = ftell(fImagen);
rewind(fImagen);

sAnv = (char *) malloc(sizeof(char) * lLongitud);

if (!sAnv)
{
printf("Problema de memoria. Imagen anverso. Tamaño: (%d)\n",
lLongitud);
fin(-1);
}

fread(sAnv, 1, lLongitud, fImagen);

fclose(fImagen);
.....
free(sAnv);

Does anybody help with this problem.

P.D. Sorry about my english but it doesn't my native
language. :)
 
J

Jens Thoms Toerring

I trying to create a C promang in an AIX System to read JPG files but
when it read just the first 4 bytes when it found a DLE character (^P)
doesn't read anymore. I using fread function. Here a few lines:
char *sAnv;
....
sprintf(file_a, "%s/anverso.jpg", strDir);
if ((fImagen = fopen(file_a,"rb")) == NULL)
{
printf("ERROR: No se pudo abrir el archivo |%s|\n", file_a);
continue;
}
fseek(fImagen, 0, SEEK_END);
lLongitud = ftell(fImagen);
rewind(fImagen);

This will rather likely work on your system, but it won't
hurt to take a look at question 19.12 in the C-FAQ.
sAnv = (char *) malloc(sizeof(char) * lLongitud);

Better drop the cast, it will only keep the compiler from
warning you if you forgot to include <stdlib.h> (which in
turn can lead to nasty problems). You may also consider to
drop the 'sizeof(char)' since that's always 1 by definition
(and you use 1 under similar circumstances in the call of
fread()).
if (!sAnv)
{
printf("Problema de memoria. Imagen anverso. Taman?o: (%d)\n",
lLongitud);
fin(-1);
}
fread(sAnv, 1, lLongitud, fImagen);

How do you found out that this didn't do what you want? You
don't test the return value of fread(), so I don't see how
you determined that "it read just the first 4 bytes". There
is no obvious reason here why fread() should have stopped
reading after 4 bytes (unless 'lLongitud' is 4). It defi-
nitely shouldn't do so just because of any special data in
the file (like the DLE character).
P.D. Sorry about my english but it doesn't my native
language. :)

Don't worry, neither is it mine;-)

Best regards, Jens
 
J

jcasique.torres

This will rather likely work on your system, but it won't
hurt to take a look at question 19.12 in the C-FAQ.

Thank you I made the change and it work's ok.
Better drop the cast, it will only keep the compiler from
warning you if you forgot to include <stdlib.h> (which in
turn can lead to nasty problems). You may also consider to
drop the 'sizeof(char)' since that's always 1 by definition
(and you use 1 under similar circumstances in the call of
fread()).


How do you found out that this didn't do what you want? You
don't test the return value of fread(), so I don't see how
you determined that "it read just the first 4 bytes". There
is no obvious reason here why fread() should have stopped
reading after 4 bytes (unless 'lLongitud' is 4). It defi-
nitely shouldn't do so just because of any special data in
the file (like the DLE character).

Well I set a variable with the result of fread something like this

size_t resultado;
...
resultado = fread{sAnv,1,lLongitud,fImagen);

then I print a message with two variables printf("Longitud: (%d)
Leído (%d)\n",lLongitud,resultado); and are the same values

But when I print the sAnv variable it just have 4 bytes
printf("Bytes leídos: |%s|\n",sAnv); --> Byte leídos: | à Ó|

Here I put a extract from JPG file
############################# B E G I N
##################################################################
à Ó^PJFIF^A^A^A╚╚
â–ˆCP7<F<2PFAFZUP_xâ•š^┬xnnx§»╣^Ãâ•š
â””^K^H^A9^Bâ•^A^A^Q ─^Y^A^A^A^A^A^A^A^B^C^D^E
─A^P^B^B^A^B^D^B^H^D^D^E^C^D
^B^C^A^B^Q^C^R!^D^S1AQ^Ã^T"2Raq^â”´ÃS^Êíß#Bbâ–’3Cr┴­4^¢$cs±^E5^UTd
┌^H^A^A?¤^Z─gZ$l
;¤q ^K&^└^┼`└^U ^├ß,DNy3^Ll^V^╔'░^├^█^X^F▄
Ùâ”IND^]Xnh@t^Ka^Ã^ã¦f^È^ãPGCâ•â–“3^ERâ• h^N­^H ^Pv="
Y4%^â•”,]Xâ”^Hâ–‘:^Ì▓─^ÃaÃ║®d´,DDI{È^
]óã▀^Y^C^B{¨^Y®./
z▀╩FuP^K^Z^GãPA^V^M^â•”bK^Gâ•â–“^R^GSRjZâ•—^U.á:^Ã^A^┴ÞD^B^Hâ–’,DDDH^H"Ã
############################### E N D
####################################################################
 
S

Spiros Bousbouras

Thank you I made the change and it work's ok.








Well I set a variable with the result of fread something like this

size_t resultado;
...
resultado = fread{sAnv,1,lLongitud,fImagen);

then I print a message with two variables printf("Longitud: (%d)
Leído (%d)\n",lLongitud,resultado); and are the same values

But when I print the sAnv variable it just have 4 bytes
printf("Bytes leídos: |%s|\n",sAnv); --> Byte leídos: | Ï Ó|

The printf() will print up to the first NUL (0) byte in sAnv
This doesn't mean that fread() did not put more bytes
where sAnv points to.
Here I put a extract from JPG file

This isn't the right place to post binary files.
 
J

Jens Thoms Toerring

Well I set a variable with the result of fread something like this
size_t resultado;
...
resultado = fread{sAnv,1,lLongitud,fImagen);
then I print a message with two variables printf("Longitud: (%d)
Leído (%d)\n",lLongitud,resultado); and are the same values

That shows that fread worked exactly as expected, it did read
as many bytes as you told it to.
But when I print the sAnv variable it just have 4 bytes
printf("Bytes leídos: |%s|\n",sAnv); --> Byte leídos: | à Ó|

That's to be expected. Using '%s' with printf() is for printing
strings. And a string is a char array where the data end with
the first '\0' character (and printf() stops at that position).
But what you read in from the file isn't a string, just an array
of chars with binary data. And, while binary data may contain
'\0' characters they definitely aren't meant to be treated like
strings, so you simply can't use printf() with '%s' to output
them (which normally would result in a lot of unreadable "noise"
anyway).

So the good news is that obviously reading in the data from the
file worked flawlessly. Question is why you want to print
them out. There will hardly be much human readable information
in there. And if you want to see what the binary data are you
will have to write a function that prints out the numerical
values of the binary data, that's what would make the most
sense.
Here I put a extract from JPG file

Which doesn't help at all since also most newsreaders can make
sense of binary data (and even may get them confused, so it's
not a goood idea to include binary data in a message).

Regards, Jens
 
J

jcasique.torres

That shows that fread worked exactly as expected, it did read
as many bytes as you told it to.


That's to be expected. Using '%s' with printf() is for printing
strings. And a string is a char array where the data end with
the first '\0' character (and printf() stops at that position).
But what you read in from the file isn't a string, just an array
of chars with binary data. And, while binary data may contain
'\0' characters they definitely aren't meant to be treated like
strings, so you simply can't use printf() with '%s' to output
them (which normally would result in a lot of unreadable "noise"
anyway).

So the good news is that obviously reading in the data from the
file worked flawlessly. Question is why you want to print
them out. There will hardly be much human readable information
in there. And if you want to see what the binary data are you
will have to write a function that prints out the numerical
values of the binary data, that's what would make the most
sense.
Really I don't want to print the information on screen i do it just
to check that is working ok, i need to read the severals JPG files to
hold in one file in which i will include the long of each image, this
file will be transfer to another place to extract them with another
program and save into a database.
Which doesn't help at all since also most newsreaders can make
sense of binary data (and even may get them confused, so it's
not a goood idea to include binary data in a message).

Ok i will never do it again!! ty
Regards, Jens

Regards
 
J

Jens Thoms Toerring

Really I don't want to print the information on screen i do it just
to check that is working ok,

Fine. Then the only test you need to do is to check if you got
as many characters as you requested. Only if the return value
of fread() isn't identical to the number of bytes you asked it
read you're in trouble. Otherwise you can assume that everything
is fine.
Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top