jpeg_reader.cpp
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
*/
#include <claw/jpeg.hpp>
#include <claw/jpeg_error_manager.hpp>
#include <claw/exception.hpp>
#include <claw/assert.hpp>
/*----------------------------------------------------------------------------*/
METHODDEF(void)
claw__graphic__jpeg__source_manager__init_source(j_decompress_ptr cinfo)
{
// nothing to do
} // claw__graphic__jpeg__source_manager__init_source()
/*----------------------------------------------------------------------------*/
METHODDEF(boolean)
claw__graphic__jpeg__source_manager__fill_input_buffer(j_decompress_ptr cinfo)
{
claw::graphic::jpeg::reader::source_manager* self =
(claw::graphic::jpeg::reader::source_manager*)cinfo->client_data;
CLAW_PRECOND( &self->pub == cinfo->src );
return self->fill_input_buffer();
} // claw__graphic__jpeg__source_manager__fill_input_buffer()
/*----------------------------------------------------------------------------*/
METHODDEF(void)
claw__graphic__jpeg__source_manager__skip_input_data(j_decompress_ptr cinfo,
long num_bytes)
{
claw::graphic::jpeg::reader::source_manager* self =
(claw::graphic::jpeg::reader::source_manager*)cinfo->client_data;
CLAW_PRECOND( &self->pub == cinfo->src );
return self->skip_input_data(num_bytes);
} // claw__graphic__jpeg__source_manager__skip_input_data()
/*----------------------------------------------------------------------------*/
METHODDEF(void)
claw__graphic__jpeg__source_manager__term_source(j_decompress_ptr cinfo)
{
// nothing to do
} // claw__graphic__jpeg__source_manager__term_source()
/*----------------------------------------------------------------------------*/
claw::graphic::jpeg::reader::source_manager::source_manager( std::istream& is )
: m_input(is), m_buffer_size(1024), m_stream_position(0)
{
std::istream::pos_type pos = is.tellg();
is.seekg( 0 , std::ios_base::end );
m_stream_size = is.tellg() ;
is.seekg( pos, std::ios_base::beg ) ;
m_buffer = new JOCTET[m_buffer_size];
pub.bytes_in_buffer = 0;
} // jpeg::reader::source_manager::source_manager()
/*----------------------------------------------------------------------------*/
claw::graphic::jpeg::reader::source_manager::~source_manager()
{
delete[] m_buffer;
} // jpeg::reader::source_manager::~source_manager()
/*----------------------------------------------------------------------------*/
boolean
claw::graphic::jpeg::reader::source_manager::fill_input_buffer()
{
unsigned int n = std::min( m_buffer_size, m_stream_size - m_stream_position );
m_input.read( (char*)m_buffer, n );
pub.next_input_byte = m_buffer;
pub.bytes_in_buffer = n;
m_stream_position += n;
if (m_input)
return TRUE;
else
return FALSE;
} // jpeg::reader::source_manager::fill_input_buffer()
/*----------------------------------------------------------------------------*/
void
claw::graphic::jpeg::reader::source_manager::skip_input_data(long num_bytes)
{
CLAW_PRECOND(num_bytes >=0);
if ( (size_t)num_bytes <= pub.bytes_in_buffer )
{
pub.next_input_byte += num_bytes;
pub.bytes_in_buffer -= num_bytes;
}
else
{
num_bytes -= pub.bytes_in_buffer;
long div = num_bytes / m_buffer_size;
long rest = num_bytes % m_buffer_size;
for (long i=0; i!=(div+1); ++i)
fill_input_buffer();
pub.next_input_byte += rest;
pub.bytes_in_buffer -= rest;
}
} // jpeg::reader::source_manager::skip_input_data()
/*----------------------------------------------------------------------------*/
claw::graphic::rgba_pixel_8
claw::graphic::jpeg::reader::RGB_to_pixel32::operator()
( const JSAMPLE* pixel ) const
{
rgba_pixel_8 result;
result.components.alpha = 255;
result.components.red = pixel[0];
result.components.green = pixel[1];
result.components.blue = pixel[2];
return result;
} // jpeg::reader::RGB_to_pixel32::operator()()
/*----------------------------------------------------------------------------*/
claw::graphic::rgba_pixel_8
claw::graphic::jpeg::reader::grayscale_to_pixel32::operator()
( const JSAMPLE* pixel ) const
{
rgba_pixel_8 result;
result.components.alpha = 255;
result.components.red = pixel[0];
result.components.green = pixel[0];
result.components.blue = pixel[0];
return result;
} // jpeg::reader::grayscale_to_pixel32::operator()()
/*----------------------------------------------------------------------------*/
claw::graphic::jpeg::reader::reader( image& img )
: m_image( img )
{
} // jpeg::reader::reader()
/*----------------------------------------------------------------------------*/
claw::graphic::jpeg::reader::reader( image& img, std::istream& f )
: m_image( img )
{
load(f);
} // jpeg::reader::reader()
/*----------------------------------------------------------------------------*/
void claw::graphic::jpeg::reader::load( std::istream& f )
{
CLAW_PRECOND( !!f );
std::istream::pos_type init_pos = f.tellg();
try
{
read_from_file(f);
}
catch(...)
{
f.clear();
f.seekg( init_pos, std::ios_base::beg );
throw;
}
} // jpeg::reader::load()
/*----------------------------------------------------------------------------*/
void claw::graphic::jpeg::reader::read_from_file( std::istream& f )
{
source_manager infile(f);
jpeg_decompress_struct cinfo;
error_manager jerr;
cinfo.err = jpeg_std_error(&jerr.pub);
if ( setjmp(jerr.setjmp_buffer) )
throw CLAW_EXCEPTION(jerr.error_string);
create_decompress_info( cinfo, infile );
jerr.pub.error_exit = jpeg__error_manager__error_exit;
try
{
decompress(f, cinfo);
jpeg_destroy_decompress(&cinfo);
}
catch(...)
{
jpeg_destroy_decompress(&cinfo);
throw;
}
} // jpeg::reader::read_from_file()
/*----------------------------------------------------------------------------*/
void claw::graphic::jpeg::reader::decompress
( std::istream& f, jpeg_decompress_struct& cinfo )
{
error_manager jerr;
jpeg_error_mgr* jerr_saved = cinfo.err;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = jpeg__error_manager__error_exit;
if ( setjmp(jerr.setjmp_buffer) )
{
jpeg_abort_decompress(&cinfo);
throw CLAW_EXCEPTION(jerr.error_string);
}
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress( &cinfo );
try
{
m_image.set_size( cinfo.image_width, cinfo.image_height );
if ( cinfo.out_color_components == 3 )
read_data( cinfo, RGB_to_pixel32() );
else if ( cinfo.out_color_components == 1 )
read_data( cinfo, grayscale_to_pixel32() );
else
throw CLAW_EXCEPTION( "invalid number of colors per channel" );
jpeg_finish_decompress(&cinfo);
}
catch(...)
{
jpeg_abort_decompress(&cinfo);
throw;
}
cinfo.err = jerr_saved;
} // jpeg::reader::decompress()
/*----------------------------------------------------------------------------*/
void claw::graphic::jpeg::reader::create_decompress_info
( jpeg_decompress_struct& cinfo, source_manager& infile ) const
{
jpeg_create_decompress(&cinfo);
cinfo.src = &infile.pub;
cinfo.client_data = &infile;
infile.pub.fill_input_buffer =
claw__graphic__jpeg__source_manager__fill_input_buffer;
infile.pub.skip_input_data =
claw__graphic__jpeg__source_manager__skip_input_data;
infile.pub.init_source = claw__graphic__jpeg__source_manager__init_source;
infile.pub.resync_to_restart = jpeg_resync_to_restart;
infile.pub.term_source = claw__graphic__jpeg__source_manager__term_source;
} // jpeg::reader::create_decompress_info()
assert.hpp
Some assert macros to strengthen you code.
CLAW_PRECOND
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition assert.hpp:98
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
exception.hpp
A simple class to use as exception with string message.
CLAW_EXCEPTION
#define CLAW_EXCEPTION(m)
Create an exception and add the name of the current function to the message.
Definition exception.hpp:90
jpeg.hpp
A class for jpeg pictures.
jpeg_error_manager.hpp
Methods for the claw::graphic::jpeg::error_manager class.
claw__graphic__jpeg__source_manager__init_source
claw__graphic__jpeg__source_manager__init_source(j_decompress_ptr cinfo)
Initialize the input stream.
Definition jpeg_reader.cpp:42
claw__graphic__jpeg__source_manager__term_source
claw__graphic__jpeg__source_manager__term_source(j_decompress_ptr cinfo)
Close the input stream.
Definition jpeg_reader.cpp:87
claw__graphic__jpeg__source_manager__fill_input_buffer
claw__graphic__jpeg__source_manager__fill_input_buffer(j_decompress_ptr cinfo)
Fill the input buffer with new data.
Definition jpeg_reader.cpp:53
claw__graphic__jpeg__source_manager__skip_input_data
claw__graphic__jpeg__source_manager__skip_input_data(j_decompress_ptr cinfo, long num_bytes)
Skip some bytes in the input buffer.
Definition jpeg_reader.cpp:70
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::graphic::jpeg__error_manager__error_exit
void jpeg__error_manager__error_exit(j_common_ptr cinfo)
Throw an exception when an error occurs in an internal jpeg processing.
Definition jpeg_error_manager.cpp:39
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
Source manager that allow us to read from a std::istream.
Definition jpeg.hpp:90
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::rgba_pixel::red
component_type red
Red component.
Definition pixel.hpp:93
claw::graphic::rgba_pixel::components
struct claw::graphic::rgba_pixel::@121015363347207306133246064116275055130111004154::@053203370171110254125315021327231332025274313004 components
Component by component representation.
claw::graphic::rgba_pixel::alpha
component_type alpha
Translucy.
Definition pixel.hpp:102
claw::graphic::rgba_pixel::green
component_type green
Green component.
Definition pixel.hpp:96
claw::graphic::rgba_pixel::blue
component_type blue
Blue component.
Definition pixel.hpp:99