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

Discussion in 'C++' started by Speed, Jul 18, 2007.

  1. Speed

    Speed Guest

    Hi,

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

    Thanks a ton,
    Speed.
     
    Speed, Jul 18, 2007
    #1
    1. Advertising

  2. Speed

    Default User Guest

    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
     
    Default User, Jul 18, 2007
    #2
    1. Advertising

  3. Speed

    Kai-Uwe Bux Guest

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


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jul 19, 2007
    #3
  4. Speed

    BobR Guest

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

    --
    Bob R
    POVrookie
     
    BobR, Jul 19, 2007
    #4
  5. Speed

    red floyd Guest

    BobR wrote:
    > Speed <> 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;
    }
     
    red floyd, Jul 19, 2007
    #5
  6. Speed

    Default User Guest

    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
     
    Default User, Jul 19, 2007
    #6
  7. Speed

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Jul 19, 2007
    #7
  8. Speed

    BobR Guest

    red floyd <> 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
     
    BobR, Jul 19, 2007
    #8
  9. Speed

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Jul 19, 2007
    #9
  10. Speed

    BobR Guest

    Kai-Uwe Bux <> wrote 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<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()'.

    --
    Bob R
    POVrookie
     
    BobR, Jul 20, 2007
    #10
  11. Speed

    James Kanze Guest

    On Jul 19, 11:54 pm, Kai-Uwe Bux <> wrote:
    > 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.


    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:
    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
     
    James Kanze, Jul 20, 2007
    #11
  12. Speed

    James Kanze Guest

    On Jul 20, 3:16 am, "BobR" <> wrote:
    > Kai-Uwe Bux <> wrote 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:
    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
     
    James Kanze, Jul 20, 2007
    #12
  13. Speed

    BobR Guest

    James Kanze <> wrote in message...
    On Jul 19, 11:54 pm, Kai-Uwe Bux <> wrote:
    > 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!). :-}
    --
    Bob R
    POVrookie
     
    BobR, Jul 20, 2007
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Speed
    Replies:
    5
    Views:
    440
    Speed
    Mar 11, 2007
  2. Speed
    Replies:
    10
    Views:
    1,227
    Kelsey Bjarnason
    Jul 30, 2007
  3. Speed
    Replies:
    1
    Views:
    476
    Jim Langston
    Jul 22, 2007
  4. Speed

    Read a 8-bit grayscale JPEG image.

    Speed, Jul 22, 2007, in forum: C Programming
    Replies:
    2
    Views:
    468
    Malcolm McLean
    Jul 22, 2007
  5. lovaspillando
    Replies:
    0
    Views:
    1,065
    lovaspillando
    Aug 26, 2007
Loading...

Share This Page