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

S

Speed

Hi,

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

Thanks a ton,
Speed.
 
D

Default User

Speed said:
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
 
K

Kai-Uwe Bux

Default said:
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::eek:stream_iterator<char>( std::cout, "" ) );
std::cout << '\n';
}


Best

Kai-Uwe Bux
 
B

BobR

Speed said:
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 char> Image;

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();
}
 
R

red floyd

BobR said:
#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 char> Image;

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 char> Image;

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;
}
 
D

Default User

Kai-Uwe Bux said:
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
 
K

Kai-Uwe Bux

Default said:
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
 
B

BobR

red floyd said:
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 char> Image;

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 char> Image;
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<char> Image(
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?
 
K

Kai-Uwe Bux

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<char> Image(
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
 
B

BobR

Kai-Uwe Bux said:
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<char> Image(
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 char> Image(
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()'.
 
J

James Kanze

BobR wrote:
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<char> Image(
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.)
 
J

James Kanze

Kai-Uwe Bux <[email protected]> wrote in message...

[...]
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.
 
B

BobR

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 char> Image(
( std::istreambuf_iterator<char>( PicIn ) ),
( std::istreambuf_iterator<char>() )
); // line 1946
// 1946 \TestBench.cpp syntax error before `)' token
[ Same with 'std::vector<char> Image(...', and '(PicIn.rdbuf())' ]

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

std::vector<unsigned char> Image(
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!). :-}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top