Re: How to read a 8-bit grayscale JPEG image using C++?
BobR wrote:
Speed <lostandhappy@gmail.com> wrote 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 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;
}