Reading JPG File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jcasique.torres@gmail.com

    Reading JPG File

    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,"r b")) == 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(c har) * lLongitud);

    if (!sAnv)
    {
    printf("Problem a 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. :-)
  • Jens Thoms Toerring

    #2
    Re: Reading JPG File

    jcasique.torres @gmail.com 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,"r b")) == 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(c har) * lLongitud);
    Better drop the cast, it will only keep the compiler from
    warning you if you forgot to include <stdlib.h(whi ch 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("Problem a 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 ___ jt@toerring.de
    \______________ ____________ http://toerring.de

    Comment

    • jcasique.torres@gmail.com

      #3
      Re: Reading JPG File

      On 14 mayo, 18:18, j...@toerring.d e (Jens Thoms Toerring) wrote:
      jcasique.tor... @gmail.com 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,"r b")) == 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(c har) * lLongitud);
      >
      Better drop the cast, it will only keep the compiler from
      warning you if you forgot to include <stdlib.h(whi ch 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("Problem a 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,lL ongitud,fImagen );

      then I print a message with two variables printf("Longitu d: (%d)
      Leído (%d)\n",lLongit ud,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<2PFAFZ UP_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"2R aq^┴Ð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^GSRj Z╗^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 ___ j...@toerring.d e
      \______________ ____________ http://toerring.de

      Comment

      • Spiros Bousbouras

        #4
        Re: Reading JPG File

        On 15 May, 04:27, jcasique.tor... @gmail.com wrote:
        On 14 mayo, 18:18, j...@toerring.d e (Jens Thoms Toerring) wrote:
        >
        >
        >
        jcasique.tor... @gmail.com 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,"r b")) == 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(c har) * lLongitud);
        >
        Better drop the cast, it will only keep the compiler from
        warning you if you forgot to include <stdlib.h(whi ch 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("Problem a 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,lL ongitud,fImagen );
        >
        then I print a message with two variables printf("Longitu d: (%d)
        Leído (%d)\n",lLongit ud,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.

        Comment

        • Jens Thoms Toerring

          #5
          Re: Reading JPG File

          jcasique.torres @gmail.com wrote:
          On 14 mayo, 18:18, j...@toerring.d e (Jens Thoms Toerring) wrote:
          jcasique.tor... @gmail.com wrote:
          if (!sAnv)
          {
          printf("Problem a 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,lL ongitud,fImagen );
          then I print a message with two variables printf("Longitu d: (%d)
          Leído (%d)\n",lLongit ud,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 ___ jt@toerring.de
          \______________ ____________ http://toerring.de

          Comment

          • jcasique.torres@gmail.com

            #6
            Re: Reading JPG File

            On 15 mayo, 02:37, j...@toerring.d e (Jens Thoms Toerring) wrote:
            jcasique.tor... @gmail.com wrote:
            On 14 mayo, 18:18, j...@toerring.d e (Jens Thoms Toerring) wrote:
            jcasique.tor... @gmail.com wrote:
            if (!sAnv)
            {
            printf("Problem a 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,lL ongitud,fImagen );
            then I print a message with two variables printf("Longitu d: (%d)
            Leído (%d)\n",lLongit ud,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 ___ j...@toerring.d e
            \______________ ____________ http://toerring.de

            Comment

            • Jens Thoms Toerring

              #7
              Re: Reading JPG File

              jcasique.torres @gmail.com 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 ___ jt@toerring.de
              \______________ ____________ http://toerring.de

              Comment

              Working...