473,382 Members | 1,814 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,382 software developers and data experts.

How to read a 8-bit grayscale JPEG image using C++?

Hi,

Could you please tell me what is the simplest code to read a 8-bit
grayscale JPEG using C++?

Thanks a ton,
Speed.

Jul 18 '07 #1
12 5094
Speed wrote:
Hi,

Could you please tell me what is the simplest code to read a 8-bit
grayscale JPEG using C++?
You asked the same basic question in comp.lang.c. Pick a language.


Brian
Jul 18 '07 #2
Default User wrote:
Speed wrote:
>Hi,

Could you please tell me what is the simplest code to read a 8-bit
grayscale JPEG using C++?

You asked the same basic question in comp.lang.c. Pick a language.
Are you sure it was the same question? I would expect that, over there, he
had asked

Could you please tell me what is the simplest code to read a 8-bit
grayscale JPEG using C?
In that case, maybe he would like to defer the choice of the language until
after he received answers.
To the OP: the C++ standard does not make any special provisions for JPEG
files. In as much as you might be looking for a l3rd party library
solution, please note that those are off-topic in this group.

To read a binary file completely into memory, you could use code like this
in C++:

#include <iterator>
#include <fstream>
#include <vector>
#include <iostream>

typedef std::vector< char buffer;

int main ( void ) {
// read file:
std::ifstream infile ( "speed_001.cc", std::ios::binary );
buffer the_buf ( std::istreambuf_iterator<char>( infile ),
(std::istreambuf_iterator<char>()) );
// write data:
std::cout << the_buf.size() << '\n';
std::copy( the_buf.begin(), the_buf.end(),
std::ostream_iterator<char>( std::cout, "" ) );
std::cout << '\n';
}
Best

Kai-Uwe Bux
Jul 18 '07 #3

Speed <lo**********@gmail.comwrote in message...
Hi,
Could you please tell me what is the simplest code to read a 8-bit
grayscale JPEG using C++?
Thanks a ton,
Speed.
#include <iostream>
#include <fstream>
#include <vector>

{
std::ifstream PicIn( "MyPic.jpg",
std::ios_base::in | std::ios_base::binary );
if( not PicIn.is_open() ){
std::cout<<"\n FAILED"<<std::endl;
return EXIT_FAILURE;
}
std::vector<unsigned charImage;

while( PicIn.peek() != EOF ){ // you didn't say 'fastest' <G>
Image.push_back( PicIn.get() );
}

std::cout<<"\n Image.size() = "
<<Image.size()<<" bytes."<<std::endl;
PicIn.close();
}

--
Bob R
POVrookie
Jul 18 '07 #4
BobR wrote:
Speed <lo**********@gmail.comwrote in message...
>Hi,
Could you please tell me what is the simplest code to read a 8-bit
grayscale JPEG using C++?
Thanks a ton,
Speed.
#include <iostream>
#include <fstream>
#include <vector>

{
std::ifstream PicIn( "MyPic.jpg",
std::ios_base::in | std::ios_base::binary );
if( not PicIn.is_open() ){
std::cout<<"\n FAILED"<<std::endl;
return EXIT_FAILURE;
}
std::vector<unsigned charImage;

while( PicIn.peek() != EOF ){ // you didn't say 'fastest' <G>
Image.push_back( PicIn.get() );
}

std::cout<<"\n Image.size() = "
<<Image.size()<<" bytes."<<std::endl;
PicIn.close();
}
Oh, come on, Bob, let's do it right and really confusing! Plus, you
forgot "int main()"!!!
#include <istream>
#include <fstream>
#include <ostream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>

int main()
{
std::ifstream PicIn( "MyPic.jpg",
std::ios_base::in | std::ios_base::binary );
if( ! PicIn )
{
std::cerr<<"\n FAILED"<<std::endl;
return EXIT_FAILURE;
}
std::vector<unsigned charImage;

std::copy(
std::istreambuf_iterator<unsigned char>(PicIn.rdbuf()),
std::istreambuf_iterator<unsigned char>(),
std::back_inserter(Image));

PicIn.close();

std::cout<<"\n Image.size() = "
<<Image.size()<<" bytes."<<std::endl;

return EXIT_SUCCESS;
}
Jul 19 '07 #5
Kai-Uwe Bux wrote:
Default User wrote:
Speed wrote:
Hi,

Could you please tell me what is the simplest code to read a 8-bit
grayscale JPEG using C++?
You asked the same basic question in comp.lang.c. Pick a language.

Are you sure it was the same question? I would expect that, over
there, he had asked

Could you please tell me what is the simplest code to read a 8-bit
grayscale JPEG using C?
That's why I said "the same basic question" rather than "the same
question".
In that case, maybe he would like to defer the choice of the language
until after he received answers.
Seems unlikely.


Brian
Jul 19 '07 #6
Default User wrote:
Kai-Uwe Bux wrote:
>Default User wrote:
Speed wrote:

Hi,

Could you please tell me what is the simplest code to read a 8-bit
grayscale JPEG using C++?

You asked the same basic question in comp.lang.c. Pick a language.

Are you sure it was the same question? I would expect that, over
there, he had asked

Could you please tell me what is the simplest code to read a 8-bit
grayscale JPEG using C?

That's why I said "the same basic question" rather than "the same
question".
Sorry, I misunderstood. I parsed it as

same ( basic question )

not as

( basically same ) question

Best

Kai-Uwe Bux
Jul 19 '07 #7

red floyd <no*****@here.dudewrote in message...
>
Oh, come on, Bob, let's do it right and really confusing! Plus, you
forgot "int main()"!!!
I didn't forget "int main()", I forgot to label (and note the 'return'):
{ // main or function
}
>
#include <istream>
#include <fstream>
#include <ostream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>

int main(){
std::ifstream PicIn( "MyPic.jpg",
std::ios_base::in | std::ios_base::binary );
if( ! PicIn ){
std::cerr<<"\n FAILED"<<std::endl;
return EXIT_FAILURE;
}
std::vector<unsigned charImage;

std::copy(
std::istreambuf_iterator<unsigned char>(PicIn.rdbuf()),
std::istreambuf_iterator<unsigned char>(),
std::back_inserter(Image));
Does not work on GCC(MinGW)3.3.1. This does:

std::vector<unsigned charImage;
std::copy(
std::istreambuf_iterator<char>( PicIn.rdbuf() ),
std::istreambuf_iterator<char>(),
std::back_inserter( Image ) );

// '.rdbuf()' gets stuck in 'char' mode (no 'unsigned char'
overload/template).
>
PicIn.close();
std::cout<<"\n Image.size() = "
<<Image.size()<<" bytes."<<std::endl;
return EXIT_SUCCESS;
}

Kai-Uwe Bux: Your example does not compile for me.
It slices off the '.at()' and '.size()' of the std::vector (and who knows
what else)! Seems to store the iterators, not 'char's.

std::vector<charImage(
std::istreambuf_iterator<char>( PicIn ),
std::istreambuf_iterator<char>() ); // the ( ...() ) fails here

std::cout<<"\n Image.size() = "
<<Image.size()<<" bytes."<<std::endl; // line 1972
/*
TestBench.cpp:1972: error: request for member `size' in `
Image(std::istreambuf_iterator<char, std::char_traits<char,
std::istreambuf_iterator<char, std::char_traits<char (*)())', which is
of
non-aggregate type `std::vector<char, std::allocator<char
()(std::istreambuf_iterator<char, std::char_traits<char,
std::istreambuf_iterator<char, std::char_traits<char (*)())'
*/
[ Do note the older compiler version. Bug? ]
I tried many variations.
Which compiler did you use?

--
Bob R
POVrookie
Jul 19 '07 #8
BobR wrote:
[snip]
Kai-Uwe Bux: Your example does not compile for me.
It slices off the '.at()' and '.size()' of the std::vector (and who knows
what else)! Seems to store the iterators, not 'char's.

std::vector<charImage(
std::istreambuf_iterator<char>( PicIn ),
std::istreambuf_iterator<char>() ); // the ( ...() ) fails here
Are you sure, you copied the code correctly? I am pretty sure that I put the
second iterator argument within yet another pair of parentheses:

buffer the_buf ( std::istreambuf_iterator<char>( infile ),
(std::istreambuf_iterator<char>() ) );
^^^ ^^^

What you wrote is parsed as a function declaration.

std::cout<<"\n Image.size() = "
<<Image.size()<<" bytes."<<std::endl; // line 1972
/*
TestBench.cpp:1972: error: request for member `size' in `
Image(std::istreambuf_iterator<char, std::char_traits<char,
std::istreambuf_iterator<char, std::char_traits<char (*)())', which
is
of
non-aggregate type `std::vector<char, std::allocator<char
()(std::istreambuf_iterator<char, std::char_traits<char,
std::istreambuf_iterator<char, std::char_traits<char (*)())'
*/
[ Do note the older compiler version. Bug? ]
I tried many variations.
Not the right one :-)
Which compiler did you use?
g++-4.2.0
Best

Kai-Uwe Bux
Jul 19 '07 #9

Kai-Uwe Bux <jk********@gmx.netwrote in message...
BobR wrote:
[snip]
Kai-Uwe Bux: Your example does not compile for me.
It slices off the '.at()' and '.size()' of the std::vector (and who
knows
what else)! Seems to store the iterators, not 'char's.
std::vector<charImage(
std::istreambuf_iterator<char>( PicIn ),
std::istreambuf_iterator<char>() ); // the ( ...() ) fails here

Are you sure, you copied the code correctly? I am pretty sure that I put
the
second iterator argument within yet another pair of parentheses:
Yes, but that weren't it.

std::vector<unsigned charImage(
std::istreambuf_iterator<char>( PicIn.rdbuf() ), // 'rdbuf()' <---
std::istreambuf_iterator<char>()
);

This works as advertised.
I guess I should go, "Duh!".
>
buffer the_buf ( std::istreambuf_iterator<char>( infile ),
(std::istreambuf_iterator<char>() ) );
^^^ ^^^

What you wrote is parsed as a function declaration.
Not after the correct *_iterator got 'called' with '.rdbuf()'.

--
Bob R
POVrookie
Jul 20 '07 #10
On Jul 19, 11:54 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
BobR wrote:
[snip]
Kai-Uwe Bux: Your example does not compile for me.
It slices off the '.at()' and '.size()' of the std::vector (and who knows
what else)! Seems to store the iterators, not 'char's.
std::vector<charImage(
std::istreambuf_iterator<char>( PicIn ),
std::istreambuf_iterator<char>() ); // the ( ...() ) fails here
Are you sure, you copied the code correctly? I am pretty sure
that I put the second iterator argument within yet another
pair of parentheses:
buffer the_buf ( std::istreambuf_iterator<char>( infile ),
(std::istreambuf_iterator<char>() ) );
^^^ ^^^
What you wrote is parsed as a function declaration.
There is (or was for the longest time---I haven't verified with
the most recent versions) a bug in g++, in that it "committed"
to the function declaration as soon as it saw that the first
argument could be interpreted as a declaration. So:
buffer the_buf ( (std::istreambuf_iterator<char>( infile )),
(std::istreambuf_iterator<char>() ) );
worked, but putting the parentheses only around the second
argument didn't.

I've gotten into the habit of putting it around all of them, so
I won't have noticed if they've fixed this. (Probably have. I
ran into it some time ago, and they tend to fix bugs pretty
quickly.)

--
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

Jul 20 '07 #11
On Jul 20, 3:16 am, "BobR" <removeBadB...@worldnet.att.netwrote:
Kai-Uwe Bux <jkherci...@gmx.netwrote in message...
[...]
buffer the_buf ( std::istreambuf_iterator<char>( infile ),
(std::istreambuf_iterator<char>() ) );
^^^ ^^^
What you wrote is parsed as a function declaration.
Not after the correct *_iterator got 'called' with '.rdbuf()'.
Not a question of the "correct" iterator: istreambuf_iterator
has a constructor which takes an istream, as well as one which
takes a streambuf. The problem is that:

std::istreambuf_iterator< char >( infile )

can be interpreted as a declaration, and when something can be
interpreted as a declaration, it is. And if what looks like
arguments to us are in fact declarations, then the above
declares a function. Putting in the extra pair of parentheses
creates a context where a declaration (of a function parameter)
is not legal, so it isn't interpreted as a declaration, but as
an expression to be passed to the constructor, and we have a
data definition, rather than a function declaration.

--
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

Jul 20 '07 #12

James Kanze <ja*********@gmail.comwrote in message...
On Jul 19, 11:54 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
BobR wrote:
/* """ quote
[snip]
Are you sure, you copied the code correctly? I am pretty sure
that I put the second iterator argument within yet another
pair of parentheses:
buffer the_buf ( std::istreambuf_iterator<char>( infile ),
(std::istreambuf_iterator<char>() ) );
^^^ ^^^
What you wrote is parsed as a function declaration.
There is (or was for the longest time---I haven't verified with
the most recent versions) a bug in g++, in that it "committed"
to the function declaration as soon as it saw that the first
argument could be interpreted as a declaration. So:
buffer the_buf ( (std::istreambuf_iterator<char>( infile )),
(std::istreambuf_iterator<char>() ) );
worked, but putting the parentheses only around the second
argument didn't.

""" */

[ GCC(MinGW)3.3.1 ]
// ref: ifstream PicIn( file, in | binary)
std::vector<unsigned charImage(
( std::istreambuf_iterator<char>( PicIn ) ),
( std::istreambuf_iterator<char>() )
); // line 1946
// 1946 \TestBench.cpp syntax error before `)' token
[ Same with 'std::vector<charImage(...', and '(PicIn.rdbuf())' ]

It works on Mr. Bux's GCC 4.2, so that 'bug' must have been fixed.

std::vector<unsigned charImage(
std::istreambuf_iterator<char>( PicIn.rdbuf() ),
std::istreambuf_iterator<char>()
);

Works fine on my poor, tired old GCC.

/* """
I've gotten into the habit of putting it around all of them, so
I won't have noticed if they've fixed this. (Probably have. I
ran into it some time ago, and they tend to fix bugs pretty
quickly.)
""" */

I noticed, with or without the parentheses! <G>
At least now I know what was happening.
Thanks James.

Your other post; 'correct' was a very poor choice of wording on my part. I
was in a hurry (supper was ready!). :-}
--
Bob R
POVrookie
Jul 20 '07 #13

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

Similar topics

8
by: Chris | last post by:
Can anybody help. I need to read a txt file backwords line by line. Can anybody help me do this. Thanks Chris
10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
0
by: Rob | last post by:
Hi, do you know any any Java implementation of an improved read-write lock that can give three kind of locks: - Read - Write - Promotable Read The read and write locks are as usual. The...
1
by: Peter Ammon | last post by:
I would like to read from a pipe in which data may arrive slowly. From experimenting, it looks like os.read() will block until it returns the maximum amount of data you asked for, in the second...
5
by: Nils Wogatzky | last post by:
Hi, I´ve got a problem with the iftream.read method. I´m reading out a binary file, but I receive wrong values if values are negative. m_File.read((char*)&help2,2); so, help2 is...
4
by: francis70 | last post by:
Hi, I have these 2 problem? Is there a way in Oracle to read UNCOMMITED data. i.e. in Oracle the normal behaviour is that a user's updates to a table are visible to other users ONLY when the...
11
by: Markus Breuer | last post by:
I have a question about oracle commit and transactions. Following scenario: Process A performs a single sql-INSERT into a table and commits the transaction. Then he informs process B (ipc) to...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
6
by: BBM | last post by:
I have an object that has a fairly complex construction sequence, so I have written a dedicated "factory" class that invokes the constructor of my object class (which does nothing but instantiate...
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.