Re: opening jpeg file in c++
- From: jameskuyper <jameskuyper@xxxxxxxxxxx>
- Date: Fri, 31 Oct 2008 09:35:28 -0700 (PDT)
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 ;
FILE * fp=fopen("./x.jpg","rb");
int c;
do{
fread(fp,&c,sizeof(c));
You don't have enough arguments here, and they're in the wrong order.
I think that should be
if(fread(&c, sizeof(c), 1, fp) != 1)
{
// Error handling.
}
You're assuming here that the value you're looking for is stored in
sizeof(int) bytes. That's not a portable assumption - are you sure
it's correct?
if( c==(int) 0xFF23){
If INT_MAX < 0xFF23, that conversion has undefined behavior.
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
Well, your code tests against 0xFF23, not 0xFFD8. Could that be the
problem?
Also, are you sure that the endianess of the data in the file that
you're reading is the same as the endianess of 'int' on the compiler
you're using?
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 ??
Assuming CHAR_BIT==8 and that the jpeg tag is stored in 16 bits, in
big-endian order, the best way to handle this is:
#include <limits.h>
#if CHAR_BIT != 8
#error This code assumes CHAR_BIT == 8
#endif
unsigned char buffer[2];
if(fread(buffer, 2, 1, fp)!=1)
{
// Error handling
}
else
{
if(buffer[0] == 0xFF && buffer[1] == 0x23 /* ? 0xD8 ? */)
// handle as jpeg file
}
else
{
// handle as non-jpeg file.
}
}
.
- References:
- opening jpeg file in c++
- From: mohi
- opening jpeg file in c++
- Prev by Date: Re: (part 1) Han from China answers your C questions
- Next by Date: Re: opening jpeg file in c++
- Previous by thread: Re: opening jpeg file in c++
- Next by thread: Re: opening jpeg file in c++
- Index(es):
Relevant Pages
|