pcx_writer.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/pcx.hpp>
/*----------------------------------------------------------------------------*/
claw::graphic::pcx::writer::file_output_buffer::file_output_buffer
( std::ostream& os )
: m_stream(os)
{
} // pcx::writer::file_output_buffer::file_output_buffer()
/*----------------------------------------------------------------------------*/
void claw::graphic::pcx::writer::file_output_buffer::encode
( unsigned int n, pattern_type pattern )
{
if ( (pattern > 63) || (n > 1) )
{
u_int_8 cnt = 0xC0 | (u_int_8)n;
m_stream.write( reinterpret_cast<char*>(&cnt), sizeof(u_int_8) );
}
m_stream.write( reinterpret_cast<char*>(&pattern), sizeof(u_int_8));
} // pcx::writer::file_output_buffer::encode()
/*----------------------------------------------------------------------------*/
unsigned int
claw::graphic::pcx::writer::file_output_buffer::min_interesting() const
{
return 1;
} // pcx::writer::file_output_buffer::min_interesting()
/*----------------------------------------------------------------------------*/
unsigned int
claw::graphic::pcx::writer::file_output_buffer::max_encodable() const
{
return 63;
} // pcx::writer::file_output_buffer::max_encodable()
/*----------------------------------------------------------------------------*/
claw::graphic::pcx::writer::writer( const image& img )
: m_image(img)
{
} // pcx::writer::writer()
/*----------------------------------------------------------------------------*/
claw::graphic::pcx::writer::writer( const image& img, std::ostream& f )
: m_image(img)
{
save(f);
} // pcx::writer::writer()
/*----------------------------------------------------------------------------*/
void claw::graphic::pcx::writer::save( std::ostream& os ) const
{
const unsigned int bytes_per_line = m_image.width() + m_image.width() % 2;
write_header(os, bytes_per_line);
save_rle_true_color(os, bytes_per_line);
} // pcx::writer::save()
/*----------------------------------------------------------------------------*/
void claw::graphic::pcx::writer::write_header
( std::ostream& os, unsigned int bytes_per_line ) const
{
header h;
h.manufacturer = 10;
h.version = 5;
h.encoded = 1;
h.bpp = 8;
h.window.x_min = 0;
h.window.y_min = 0;
h.window.x_max = m_image.width() - 1;
h.window.y_max = m_image.height() - 1;
h.horizontal_dpi = 72; // arbitrary value
h.vertical_dpi = 72;
std::fill( h.color_map, h.color_map+16, rgb_pixel_8(0, 0, 0) );
h.reserved = 0;
h.color_planes = 3; // RGB
h.bytes_per_line = bytes_per_line;
h.palette_info = 0;
h.screen_size.horizontal = 0;
h.screen_size.vertical = 0;
std::fill( h.filler, h.filler+54, 0 );
os.write( reinterpret_cast<char*>(&h), sizeof(header) );
} // pcx::writer::write_header()
/*----------------------------------------------------------------------------*/
void claw::graphic::pcx::writer::save_rle_true_color
( std::ostream& os, unsigned int bytes_per_line ) const
{
std::vector<u_int_8> data(bytes_per_line, 0);
rle_pcx_encoder encoder;
file_output_buffer output(os);
for (unsigned int y=0; y!=m_image.height(); ++y)
{
// red
for (unsigned int x=0; x!=m_image.width(); ++x)
data[x] = m_image[y][x].components.red;
encoder.encode( data.begin(), data.end(), output );
// green
for (unsigned int x=0; x!=m_image.width(); ++x)
data[x] = m_image[y][x].components.green;
encoder.encode( data.begin(), data.end(), output );
// blue
for (unsigned int x=0; x!=m_image.width(); ++x)
data[x] = m_image[y][x].components.blue;
encoder.encode( data.begin(), data.end(), output );
}
} // pcx::writer::save_rle_true_color()
claw::graphic::pcx::writer::writer
writer(const image &img)
Constructor.
Definition: pcx_writer.cpp:90
claw::graphic::pcx::writer::file_output_buffer::file_output_buffer
file_output_buffer(std::ostream &os)
Constructor.
Definition: pcx_writer.cpp:38
claw::graphic::image::height
unsigned int height() const
Gets image&#39;s height.
Definition: image.cpp:159
claw::u_int_8
unsigned_integer_of_size< 8 >::type u_int_8
An unsigned integer on 8 bits.
Definition: types.hpp:135
claw::graphic::pcx::writer::file_output_buffer::pattern_type
u_int_8 pattern_type
The typ of the output patterns.
Definition: pcx.hpp:315
claw::graphic::pcx::writer::file_output_buffer
The type of the output buffer associated with the file when encoding RLE data.
Definition: pcx.hpp:311
claw::graphic::pcx::writer::file_output_buffer::min_interesting
unsigned int min_interesting() const
Get the minimum number of pixels needed for encoding.
Definition: pcx_writer.cpp:67
claw::graphic::pcx::writer::save
void save(std::ostream &os) const
Save the content of the image in a stream.
Definition: pcx_writer.cpp:113
pcx.hpp
A class for pcx pictures.
claw::graphic::image::width
unsigned int width() const
Gets image&#39;s width.
Definition: image.cpp:147
claw::graphic::pcx::writer::file_output_buffer::encode
void encode(unsigned int n, pattern_type pattern)
Encode a pixel data.
Definition: pcx_writer.cpp:51
claw::graphic::rgb_pixel
RGB pixel.
Definition: pixel.hpp:44
claw::graphic::image
A class to deal with images.
Definition: image.hpp:49
claw::rle_encoder
A class to help run-length encoding (RLE) streams.
Definition: rle_encoder.hpp:58
claw::graphic::pcx::writer::file_output_buffer::max_encodable
unsigned int max_encodable() const
Get the maximum number of pixel a code can encode.
Definition: pcx_writer.cpp:77