opening jpeg file in c++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mohi

    opening jpeg file in c++

    hello everyone ,

    i am trying to read a jpeg image through c++ but is unable to do so ,
    presently i tried it with code in c as i use something like ;

    FILE * fp=fopen("./x.jpg","rb");
    int c;
    do{
    fread(fp,&c,siz eof(c));
    if( c==(int) 0xFF23){
    do.....
    do....

    }

    printf("%x",c);

    }

    while(c!=EOF);



    but the problem is the if condition never evalutes to true as i know
    that according to the jpeg standard there should be market with value
    0xFFD8 and others also .....and also the printf() of integer 'c' as
    hex is never displayed it just displays a blank ..

    what could be wrong ??

    or what is the best way to read a binary file such as an image ??

    thanks a lot
    mohan gupta
  • Eric Sosman

    #2
    Re: opening jpeg file in c++

    mohi wrote:
    hello everyone ,
    >
    i am trying to read a jpeg image through c++ but is unable to do so ,
    presently i tried it with code in c as i use something like ;
    Sorry; I can only debug what I can see. If the problems I
    notice in what you've shown are only "something like" what's
    happening in your actual code, you have only yourself to blame
    for the imperfect approximation.
    FILE * fp=fopen("./x.jpg","rb");
    And how do you know that the fopen() succeeded, hmmm?
    int c;
    do{
    fread(fp,&c,siz eof(c));
    There are at least three problems here. First, fread() requires
    four arguments but you have supplied only three; the compiler should
    have given you an error message here.

    Second, it appears that you are trying to read two bytes from the
    file, but sizeof(c) might not be equal to two. On most general-purpose
    machines these days the value will be four; the value one is found on
    some special-purpose hardware. Two was once common, but has become
    rare. If you want to read two bytes, say `2'.

    Third, there's the problem of representation, usually in the
    form of "endianness ." Even if sizeof(c) is two, setting c's first
    byte to 0xFF and second byte to 0x23 might not produce the value
    0xFF23[*], but might give you 0x23FF instead. If the "endianness "
    of your machine is different from the "endianness " of JPEG, the
    dump-the-bytes-into-the-int-and-hope approach will not work. See
    the FAQ for more on this matter.
    [*] In fact, a sixteen-bit `int' cannot *ever* equal 0xFF23,
    which is a number too large for the variable's range.
    if( c==(int) 0xFF23){
    do.....
    do....
    >
    }
    >
    printf("%x",c);
    "%x" converts an `unsigned int' argument, but you supply a
    plain (signed) `int'. Not good. Also, since you never put any
    space between the output of successive calls, you will have a
    hard time distinguishing one piece of output from the next. And
    finally, since you never output a newline character to end the
    line, there's no guarantee you'll ever see any output at all.
    >
    }
    >
    while(c!=EOF);
    Here's another problem: fread() does not report end-of-file
    by storing EOF in the data area, but by returning the number of
    elements it was able to read as the value of the fread() function.
    You have learned, it seems, that red means stop and green means go,
    but instead of looking at the traffic signal you are looking at
    the color of your hood ornament.

    I'll suggest it again: Read the FAQ. http://www.c-faq.com/

    --
    Eric.Sosman@sun .com

    Comment

    • Malcolm McLean

      #3
      Re: opening jpeg file in c++

      "Drew Lawson" <drew@furrfu.in validwrote in message
      >
      I played with writing some jpeg code a few months back, and I can't
      help but wonder whether that might be too big of a project for the
      ability level that I think you are at. It's a great data handling
      project, but not if it completely overwhelms you.
      >
      The OP might be interested in the JPEG codec on my website. It's one of the
      chapters in "Basic Algorithms".

      --
      Free games and programming goodies.


      Comment

      Working...