Claw 1.7.3
bitmap_reader.tpp
1/*
2 CLAW - a C++ Library Absolutely Wonderful
3
4 CLAW is a free library without any particular aim but being useful to
5 anyone.
6
7 Copyright (C) 2005-2011 Julien Jorge
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23 contact: julien.jorge@gamned.org
24*/
25/**
26 * \file bitmap_reader.tpp
27 * \brief Template classes of the claw::graphic::bitmap::reader class.
28 * \author Julien Jorge
29 */
30#include <cassert>
31#include <algorithm>
32#include <claw/exception.hpp>
33
34/*----------------------------------------------------------------------------*/
35/**
36 * \brief Constructor.
37 * \param palette The color palette to convert the pixels.
38 * \param img The image we're filling.
39 */
40template< bool Coded4Bits >
41claw::graphic::bitmap::reader::
42rle_bitmap_output_buffer<Coded4Bits>::rle_bitmap_output_buffer
43( const color_palette_type& palette, image& img )
44 : m_palette(palette), m_image(img), m_x(0), m_y(m_image.height() - 1)
45{
46
47} // bitmap::reader::rle_bitmap_output_buffer::rle_bitmap_output_buffer()
48
49/*----------------------------------------------------------------------------*/
50/**
51 * \brief Go to the begining of the next line to fill.
52 */
53template< bool Coded4Bits >
54void claw::graphic::bitmap::reader::
55rle_bitmap_output_buffer<Coded4Bits>::next_line()
56{
57 assert( m_y > 0 );
58
59 m_x = 0;
60 --m_y;
61} // bitmap::reader::rle_bitmap_output_buffer::next_line()
62
63/*----------------------------------------------------------------------------*/
64/**
65 * \brief Move the cursor horizontally and vertically.
66 * \param x Horizontal displacement.
67 * \param y Vertical displacement.
68 */
69template< bool Coded4Bits >
70void claw::graphic::bitmap::reader::
71rle_bitmap_output_buffer<Coded4Bits>::delta_move
72(unsigned char x, unsigned char y)
73{
74 assert( m_x + x < m_image.width() );
75 assert( m_y + y < m_image.height() );
76
77 m_x += x;
78 m_y += y;
79} // bitmap::reader::rle_bitmap_output_buffer::delta_move()
80
81
82//******************************************************************************
83
84
85/*----------------------------------------------------------------------------*/
86/**
87 * \brief Get the type of the following data in the input buffer, eventually
88 * apply the special codes.
89 * \param input The input stream (the bitmap file).
90 * \param output The output stream (the bitmap image).
91 */
92template< typename OutputBuffer >
93void claw::graphic::bitmap::reader::rle_bitmap_decoder<OutputBuffer>::read_mode
94( file_input_buffer& input, output_buffer_type& output)
95{
96 this->m_mode = this->stop;
97 bool ok = true;
98
99 if ( input.remaining() < 2)
100 ok = input.read_more(2);
101
102 if (ok)
103 {
104 unsigned char key, pattern;
105
106 key = input.get_next();
107 pattern = input.get_next();
108
109 // compressed data, next byte is the pattern
110 if (key > 0)
111 {
112 this->m_mode = this->compressed;
113 this->m_count = key;
114 this->m_pattern = pattern;
115 }
116 else switch( pattern )
117 {
118 // end of line
119 case 0 : output.next_line(); read_mode(input, output); break;
120 // end of file
121 case 1 : this->m_mode = this->stop; break;
122 // delta move
123 case 2 :
124 {
125 if ( input.remaining() < 1 )
126 ok = input.read_more(1);
127
128 if (ok)
129 {
130 unsigned char x, y;
131 x = pattern;
132 y = input.get_next();
133 output.delta_move(x, y);
134 read_mode(input, output);
135 break;
136 }
137 }
138 // raw data
139 default: this->m_mode = this->raw; this->m_count = pattern; break;
140 }
141 }
142} // bitmap::reader::rle_bitmap_decoder::read_mode()
143
144
145/*----------------------------------------------------------------------------*/
146/**
147 * \brief Load uncompressed data from the file.
148 * \param f The file from which we're loading the bitmap.
149 * \param buffer_size Number of bytes needed to store one line of pixels.
150 * \param palette Color palette.
151 * \param pixel_convert A method to convert one line of pixels from the file to
152 * a line of the current bitmap.
153 *
154 * \remark The Convert type method must take this four parameters in this order:
155 * # scanline& destination line,
156 * # const char* input buffer (contains one line of the bitmap),
157 * # const color_palette_type& palette The color palette of the file,
158 */
159template<typename Convert>
160void claw::graphic::bitmap::reader::load_rgb_data
161( std::istream& f, unsigned int buffer_size, const color_palette_type& palette,
162 const Convert& pixel_convert )
163{
164 unsigned int line;
165
166 // lines are 4-bytes aligned, so adjust buffer's size.
167 if (buffer_size % 4 != 0)
168 buffer_size += 4 - buffer_size % 4;
169
170 char* buffer = new char[buffer_size];
171
172 for (line = m_image.height(); (line>0) && !f.eof(); )
173 {
174 --line;
175 f.read(buffer, buffer_size);
176 pixel_convert( m_image[line], buffer, palette );
177 }
178
179 delete[] buffer;
180
181 if ( f.rdstate() != std::ios_base::goodbit )
182 throw claw::bad_format("bitmap::reader::load_data");
183} // bitmap::reader::load_data()