jpeg.hpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#ifndef __CLAW_JPEG_HPP__
#define __CLAW_JPEG_HPP__
#include <claw/image.hpp>
#include <setjmp.h>
#include <iostream>
#include <string>
#include <cstdio>
extern "C"
{
#include <jpeglib.h>
}
namespace claw
{
namespace graphic
{
class jpeg : public image
{
public:
/*--------------------------------------------------------------------*/
struct error_manager
{
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
std::string error_string;
}; // struct error_manager
/*----------------------------------------------------------------------*/
class reader
{
// classes that need to be accessible from jpeg callbacks.
public:
/*--------------------------------------------------------------------*/
struct source_manager
{
public:
source_manager( std::istream& is );
~source_manager();
boolean fill_input_buffer();
void skip_input_data(long num_bytes);
public:
struct jpeg_source_mgr pub;
private:
std::istream& m_input;
const JOCTET* m_buffer;
const unsigned int m_buffer_size;
unsigned int m_stream_size;
unsigned int m_stream_position;
}; // struct source_manager
private:
/*--------------------------------------------------------------------*/
class RGB_to_pixel32
{
public:
rgba_pixel_8 operator()( const JSAMPLE* pixel ) const;
}; // class RGB_to_pixel32
/*--------------------------------------------------------------------*/
class grayscale_to_pixel32
{
public:
rgba_pixel_8 operator()( const JSAMPLE* pixel ) const;
}; // class grayscale_to_pixel32
public:
reader( image& img );
reader( image& img, std::istream& f );
void load( std::istream& f );
private:
template<class Convert>
void read_data( jpeg_decompress_struct& cinfo,
const Convert& pixel_convert );
void read_from_file( std::istream& f );
void decompress( std::istream& f, jpeg_decompress_struct& cinfo );
void create_decompress_info( jpeg_decompress_struct& cinfo,
source_manager& infile ) const;
private:
image& m_image;
}; // class reader
/*----------------------------------------------------------------------*/
class writer
{
public:
struct options
{
public:
options();
options( unsigned char compression_quality_, bool progressive_ );
public:
unsigned char quality;
bool progressive;
}; // struct options
// classes that need to be accessible from jpeg callbacks.
/*--------------------------------------------------------------------*/
struct destination_manager
{
public:
destination_manager( std::ostream& os );
~destination_manager();
void flush();
void term();
public:
struct jpeg_destination_mgr pub;
private:
std::ostream& m_output;
JOCTET* m_buffer;
const unsigned int m_buffer_size;
}; // struct destination_manager
public:
writer( const image& img );
writer( const image& img, std::ostream& f,
const options& opt = options() );
void save( std::ostream& f, const options& opt = options() ) const;
private:
void set_options( jpeg_compress_struct& cinfo,
const options& opt ) const;
void save_image( jpeg_compress_struct& cinfo ) const;
void copy_pixel_line( JSAMPLE* data, unsigned int y ) const;
void create_compress_info( jpeg_compress_struct& cinfo,
destination_manager& outfile ) const;
private:
const image& m_image;
static const unsigned int s_rgb_pixel_size;
}; // class writer
public:
jpeg( unsigned int w, unsigned int h );
jpeg( const image& that );
jpeg( std::istream& f );
void save( std::ostream& os,
const writer::options& opt = writer::options() ) const;
}; // class jpeg
} // namespace graphic
} // namespace claw
#include <claw/impl/jpeg_reader.tpp>
#endif // __CLAW_JPEG_HPP__
claw::graphic::jpeg::writer::options::quality
unsigned char quality
Quality level to use in the saved stream.
Definition: jpeg.hpp:182
claw::graphic::jpeg::jpeg
jpeg(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition: jpeg.cpp:39
claw::graphic::jpeg::error_manager::pub
struct jpeg_error_mgr pub
"public" fields, needed by the jpeg library.
Definition: jpeg.hpp:65
claw::graphic::jpeg::error_manager::error_string
std::string error_string
A comprehensive description of the error.
Definition: jpeg.hpp:71
claw::graphic::jpeg::writer::destination_manager
Destination manager that allow us to write in a std::ostream.
Definition: jpeg.hpp:196
claw::graphic::jpeg::writer::options::progressive
bool progressive
Tell if we save a progressive jpeg.
Definition: jpeg.hpp:185
claw::graphic::rgba_pixel
RGBA pixel.
Definition: pixel.hpp:79
claw::graphic::jpeg::error_manager::setjmp_buffer
jmp_buf setjmp_buffer
For return to caller.
Definition: jpeg.hpp:68
claw::graphic::image::load
void load(std::istream &f)
Read the image from a stream.
Definition: image.cpp:381
claw::graphic::jpeg::save
void save(std::ostream &os, const writer::options &opt=writer::options()) const
Save the image.
Definition: jpeg.cpp:73
claw::graphic::jpeg::writer
This class write an image in a jpeg file.
Definition: jpeg.hpp:168
claw::graphic::jpeg::reader
This class read data from a jpeg file and store it in an image.
Definition: jpeg.hpp:80
claw::graphic::jpeg::error_manager
Error handler that throw an exception instead of exiting the program.
Definition: jpeg.hpp:62
claw::graphic::jpeg::writer::options
Parameters of the writing algorithm.
Definition: jpeg.hpp:174
claw::graphic::jpeg::reader::source_manager
Source manager that allow us to read from a std::istream.
Definition: jpeg.hpp:89
claw::graphic::jpeg
A class for jpeg pictures.
Definition: jpeg.hpp:52
claw::graphic::image
A class to deal with images.
Definition: image.hpp:49
claw
This is the main namespace.
Definition: algorithm.hpp:33
image.hpp
A class to deal with images.