Re: How to read a 8-bit grayscale JPEG image using C++?
red floyd <no.spam@here.dude> wrote 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 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?
--
Bob R
POVrookie