473,387 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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,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
Oct 31 '08 #1
2 7409
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).
Oct 31 '08 #2
On Oct 31, 6:10*pm, Juha Nieminen <nos...@thanks.invalidwrote:
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:ja*********@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
Oct 31 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ming | last post by:
Hi All, I want to write a PHP webpage which allows people to upload images (no matter what formats) to me and at the same time converts any non-jpeg image to JPEG. Here's what I have: ...
3
by: Steve Holden | last post by:
If nobody has a quick answer I'll go to the PIL list, but I'm having problems with PIL JPEG support on Windows, and figured someone on c.l.py might have solved this problem. I built the Cygwin...
16
by: David Lauberts | last post by:
Hi Wonder if someone has some words of wisdom. I have a access 2002 form that contains 2 graph objects that overlay each other and would like to export them as a JPEG to use in a presentation....
4
by: Shumit Rehman | last post by:
Hi I have a table which has path names to photos I would like to view. I would like to open some/any kind of image viewer(Paint) to see the picture when I click the pathname. The pictures are...
0
by: Johann Blake | last post by:
In my need to decode a JPEG 2000 file, I discovered like many that there was no functionality for this in the .NET Framework. Instead of forking out a pile of cash to do this, I came up with the...
10
by: rtilley | last post by:
Hope it's not inappropriate to post this here. Could someone critique my code? I have no Python programmers in my office to show this to. The script works OK, but should I do it differently? I...
0
by: Jack Wu | last post by:
Hi I've spent a good majority of my day trying to figure out how to have PIL 1.1.5 working on my OSX 10.3.9_PPC machine. I'm still stuck and I have not gotten anywhere. Could somebody please...
2
by: brianflannery | last post by:
Greetings! My situation is this. I currently have a database of which in a form displays jpeg pictures (one at a time) which are linked to the db and stored in a separate folder. I have set up a...
2
by: mohi | last post by:
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;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.