[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

opening jpeg file in c++

mohi

10/31/2008 3:04:00 PM

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));
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
4 Answers

Juha Nieminen

10/31/2008 5:10:00 PM

0

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

In which system are you running this? If you are running it in a unix
shell, the prompt might be overwriting what that printf() printed
because you are not printing a newline character at the end.

The most probable reason for the if() to fail is that you are probably
reading 4 bytes rather than 2 (I will assume in your system 'int' is
32-bit), and will have something completely different from 0xFF23 even
if the first two bytes in the input file had those values.

The surest (and most portable) way of making that work is that you do
indeed read two bytes explicitly, one byte at a time, and either compare
them directly to those two values, or construct an integer with those
types (with shifting and bit-orring).

One typical technique to read and interpret the header data of a
binary file (such as an image file) is to read the header data into an
array (of unsigned chars), and then reading individual bytes as
necessary from that array. This is the easiest way to quickly get all
the header info from such a file (assuming the header has a fixed format).

James Kanze

10/31/2008 8:51:00 PM

0

On Oct 31, 6:10 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> mohi wrote:

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

> In which system are you running this? If you are running it in
> a unix shell, the prompt might be overwriting what that
> printf() printed because you are not printing a newline
> character at the end.

I rather doubt that he gets that far.

> The most probable reason for the if() to fail is that you are
> probably reading 4 bytes rather than 2 (I will assume in your
> system 'int' is 32-bit), and will have something completely
> different from 0xFF23 even if the first two bytes in the input
> file had those values.

The most probably reason his code even compiles is that he's
working in C, not in C++. It won't compile with any C++
compiler I've ever used.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Heinrich

4/1/2014 3:44:00 AM

0



<dekaenia19@gmail.com> schreef in bericht
news:5a3d1901-120e-4aba-9b29-5bcde08584d6@googlegroups.com...
> ???? ??????????, 25 ?????????????? 2014 1:16:58 ??.??. UTC+2, ?? ?????????????? Heinrich ????????????:
>> http://www.europeandailynews.org/2014/03/25/the-future-of-british-schools-english-taught-as-second...
>
> Diversity is strength.

will you please go away. you are ruining my early morning coffee

The Peeler

4/1/2014 9:35:00 AM

0

On Tue, 1 Apr 2014 05:44:02 +0200, Dumb Heini, the heavily medicated Dutch
resident Nazi troll and laughing stock of sci and scj, wrote:


>> Diversity is strength.
>
> will you please go away. you are ruining my early morning coffee

We all here just LIKE to piss in your morning coffee, Dutch PIG! Get used to
it, finally! <BG>


--
The Dutch Nazitard: why do you americans always have to call me hei9nnie
while my posatings clearly indicaate my name is heinrich?
MID: <4dad5b41$0$20532$2e0edba0@news.tweakdsl.nl>