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::image
A class to deal with images.
Definition image.hpp:50
claw::graphic::image::image
image()
Constructor. Creates an image without datas.
Definition image.cpp:106
claw::graphic::jpeg::reader::load
void load(std::istream &f)
Load an image from a jpeg file.
Definition jpeg_reader.cpp:242
claw::graphic::jpeg::reader::reader
reader(image &img)
Constructor.
Definition jpeg_reader.cpp:218
claw::graphic::jpeg::writer::save
void save(std::ostream &f, const options &opt=options()) const
Save an image in a jpeg file.
Definition jpeg_writer.cpp:195
claw::graphic::jpeg::writer::writer
writer(const image &img)
Constructor.
Definition jpeg_writer.cpp:168
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::jpeg
jpeg(unsigned int w, unsigned int h)
Constructor. Creates an empty image.
Definition jpeg.cpp:39
image.hpp
A class to deal with images.
claw::graphic
Everything about image structures and processing.
Definition bitmap.hpp:43
claw::graphic::rgba_pixel_8
rgba_pixel rgba_pixel_8
A color with 8 bits per component and an alpha channel.
Definition pixel.hpp:126
claw
This is the main namespace.
Definition algorithm.hpp:34
claw::graphic::jpeg::error_manager
Error handler that throw an exception instead of exiting the program.
Definition jpeg.hpp:63
claw::graphic::jpeg::error_manager::error_string
std::string error_string
A comprehensive description of the error.
Definition jpeg.hpp:71
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::setjmp_buffer
jmp_buf setjmp_buffer
For return to caller.
Definition jpeg.hpp:68
claw::graphic::jpeg::reader::source_manager::skip_input_data
void skip_input_data(long num_bytes)
Skip some bytes in the input buffer.
Definition jpeg_reader.cpp:147
claw::graphic::jpeg::reader::source_manager::fill_input_buffer
boolean fill_input_buffer()
Fill the input buffer with new data.
Definition jpeg_reader.cpp:125
claw::graphic::jpeg::reader::source_manager::source_manager
source_manager(std::istream &is)
Constructor.
Definition jpeg_reader.cpp:97
claw::graphic::jpeg::reader::source_manager::~source_manager
~source_manager()
Destructor.
Definition jpeg_reader.cpp:115
claw::graphic::jpeg::reader::source_manager::pub
struct jpeg_source_mgr pub
"public" fields, needed by the jpeg library.
Definition jpeg.hpp:100
claw::graphic::jpeg::writer::destination_manager
Destination manager that allow us to write in a std::ostream.
Definition jpeg.hpp:197
claw::graphic::jpeg::writer::destination_manager::destination_manager
destination_manager(std::ostream &os)
Constructor.
Definition jpeg_writer.cpp:91
claw::graphic::jpeg::writer::destination_manager::term
void term()
Write the last pending bytes in the file.
Definition jpeg_writer.cpp:125
claw::graphic::jpeg::writer::destination_manager::pub
struct jpeg_destination_mgr pub
"public" fields, needed by the jpeg library.
Definition jpeg.hpp:207
claw::graphic::jpeg::writer::destination_manager::~destination_manager
~destination_manager()
Destructor.
Definition jpeg_writer.cpp:103
claw::graphic::jpeg::writer::destination_manager::flush
void flush()
Write the content of the buffer in the file.
Definition jpeg_writer.cpp:112
claw::graphic::jpeg::writer::options
Parameters of the writing algorithm.
Definition jpeg.hpp:175
claw::graphic::jpeg::writer::options::progressive
bool progressive
Tell if we save a progressive jpeg.
Definition jpeg.hpp:185
claw::graphic::jpeg::writer::options::options
options()
Default constructor.
Definition jpeg_writer.cpp:137
claw::graphic::jpeg::writer::options::quality
unsigned char quality
Quality level to use in the saved stream.
Definition jpeg.hpp:182