Reading JPG File

Discussion in 'C Programming' started by jcasique.torres@gmail.com, May 14, 2008.

  1. Guest

    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. :)
    , May 14, 2008
    #1
    1. Advertising

  2. wrote:
    > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, May 14, 2008
    #2
    1. Advertising

  3. Guest

    On 14 mayo, 18:18, (Jens Thoms Toerring) wrote:
    > wrote:
    > > 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.


    Thank you I made the change and it work's ok.
    >
    > > 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).


    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
    ####################################################################
    >
    > > P.D. Sorry about my english but it doesn't my native
    > > language. :)

    >
    > Don't worry, neither is it mine;-)
    >
    > Best regards, Jens
    > --
    > \ Jens Thoms Toerring ___
    > \__________________________ http://toerring.de
    , May 15, 2008
    #3
  4. On 15 May, 04:27, wrote:
    > On 14 mayo, 18:18, (Jens Thoms Toerring) wrote:
    >
    >
    >
    > > wrote:
    > > > 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.

    >
    > Thank you I made the change and it work's ok.
    >
    >
    >
    >
    >
    > > > 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).

    >
    > 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.
    Spiros Bousbouras, May 15, 2008
    #4
  5. wrote:
    > On 14 mayo, 18:18, (Jens Thoms Toerring) wrote:
    > > wrote:
    > > > if (!sAnv)
    > > > {
    > > > printf("Problema de memoria. Imagen anverso. Tamano: (%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).


    > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, May 15, 2008
    #5
  6. Guest

    On 15 mayo, 02:37, (Jens Thoms Toerring) wrote:
    > wrote:
    > > On 14 mayo, 18:18, (Jens Thoms Toerring) wrote:
    > > > wrote:
    > > > > if (!sAnv)
    > > > > {
    > > > > printf("Problema de memoria. Imagen anverso. Tamano: (%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).

    > > 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.
    >

    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.

    > > 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).


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


    Regards
    > --
    > \ Jens Thoms Toerring ___
    > \__________________________ http://toerring.de
    , May 15, 2008
    #6
  7. wrote:
    > 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
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, May 15, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roland
    Replies:
    0
    Views:
    1,050
    Roland
    May 12, 2004
  2. Matthias
    Replies:
    5
    Views:
    421
    Whitecrest
    Jun 1, 2004
  3. Yannick

    ASP changes Big JPG -> Small JPG

    Yannick, Feb 2, 2004, in forum: ASP General
    Replies:
    6
    Views:
    149
    Tim Williams
    Feb 3, 2004
  4. Vilmar Brazão de Oliveira
    Replies:
    14
    Views:
    343
    Aaron Bertrand [MVP]
    Mar 5, 2004
  5. Replies:
    3
    Views:
    255
    Andy Dingley
    Sep 27, 2006
Loading...

Share This Page