~ubuntu-branches/ubuntu/oneiric/libclaw/oneiric

« back to all changes in this revision

Viewing changes to claw/bitmap.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge
  • Date: 2008-05-17 15:36:57 UTC
  • Revision ID: james.westby@ubuntu.com-20080517153657-0b1204j754ykoz48
Tags: upstream-1.5.2b
ImportĀ upstreamĀ versionĀ 1.5.2b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-2008 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@yahoo.fr
 
24
*/
 
25
/**
 
26
 * \file bitmap.hpp
 
27
 * \brief A class for bitmap pictures.
 
28
 * \author Julien Jorge
 
29
 */
 
30
#ifndef __CLAW_BITMAP_HPP__
 
31
#define __CLAW_BITMAP_HPP__
 
32
 
 
33
#include <iostream>
 
34
#include <vector>
 
35
#include <claw/image.hpp>
 
36
#include <claw/rle_decoder.hpp>
 
37
#include <claw/color_palette.hpp>
 
38
#include <claw/buffered_istream.hpp>
 
39
 
 
40
namespace claw
 
41
{
 
42
  namespace graphic
 
43
  {
 
44
    /**
 
45
     * \brief A class for bitmap images.
 
46
     * \author Julien Jorge
 
47
     */
 
48
    class bitmap : public image
 
49
    {
 
50
    private:
 
51
      /**
 
52
       * \brief Tool class used for defining the structures of the datas stored
 
53
       *        in a bitmap file.
 
54
       * \author Julien Jorge
 
55
       */
 
56
      class file_structure
 
57
      {
 
58
      public:
 
59
        /** \brief The type of the color palette for low color image files. */
 
60
        typedef color_palette<pixel32> color_palette_type;
 
61
 
 
62
        /** \brief Compression mode. */
 
63
        enum compression
 
64
        {
 
65
          BMP_COMPRESSION_RGB       = 0,
 
66
          BMP_COMPRESSION_RLE8      = 1,
 
67
          BMP_COMPRESSION_RLE4      = 2,
 
68
          BMP_COMPRESSION_BITFIELDS = 3
 
69
        };
 
70
 
 
71
#       pragma pack (push,2)
 
72
 
 
73
        /**
 
74
         * \brief Header of a bitmap file.
 
75
         */
 
76
        struct header
 
77
        {
 
78
          /** \brief File identifier (must be 'BM'). */
 
79
          char id[2];
 
80
 
 
81
          /** \brief File's size. */
 
82
          unsigned int file_size;
 
83
          
 
84
          /** \brief not used. */
 
85
          unsigned int nop;
 
86
          
 
87
          /** \brief Begininf of the datas. */
 
88
          unsigned int data_offset;
 
89
          
 
90
          /** \brief Header's size. */
 
91
          unsigned int header_size;
 
92
          
 
93
          /** \brief Image's width. */
 
94
          unsigned int width;
 
95
          
 
96
          /** \brief Image's height. */
 
97
          unsigned int height;
 
98
          
 
99
          /** \brief Number of layers. */
 
100
          unsigned short layers;
 
101
          
 
102
          /** \brief Bits per pixel. */
 
103
          unsigned short bpp;
 
104
          
 
105
          /** \brief Compression algorithm. */
 
106
          unsigned int compression;
 
107
          
 
108
          /** \brief Image's size (bytes). */
 
109
          unsigned int image_size;
 
110
          
 
111
          /** \brief Horizontal resolution (pixels per meter). */
 
112
          unsigned int ppm_x;
 
113
          
 
114
          /** \brief Vertical resolution (pixels per meter). */
 
115
          unsigned int ppm_y;
 
116
          
 
117
          /** \brief Number of colors. */
 
118
          unsigned int colors_count;
 
119
          
 
120
          /** \brief Number of important colors. */
 
121
          unsigned int importants_colors;
 
122
        };
 
123
#        pragma pack (pop)
 
124
 
 
125
      }; // class file_structure
 
126
 
 
127
    public:
 
128
      /*----------------------------------------------------------------------*/
 
129
      /**
 
130
       * \brief This class read data from a bitmap file and store it in an
 
131
       *        image.
 
132
       * \author Julien Jorge
 
133
       */
 
134
      class reader : private file_structure
 
135
      {
 
136
      private:
 
137
        /** \brief The type of the input buffer associated with the file when
 
138
            decoding RLE files. */
 
139
        typedef buffered_istream<std::istream> file_input_buffer;
 
140
 
 
141
        /**
 
142
         * \brief The output buffer for the RLE decoder.
 
143
         *
 
144
         * \b Template \b parameters
 
145
         * - Coded4bits, true is the RLE patterns are coded in four bits ;
 
146
         *               otherwise the patterns are supposed to be on eight
 
147
         *               bits.
 
148
         *
 
149
         * \author Julien Jorge
 
150
         */
 
151
        template< bool Coded4bits >
 
152
        class rle_bitmap_output_buffer
 
153
        {
 
154
        public:
 
155
          rle_bitmap_output_buffer( const color_palette_type& palette,
 
156
                                    image& image );
 
157
          
 
158
          void fill( unsigned int n, unsigned char pattern );
 
159
          void copy( unsigned int n, file_input_buffer& buffer );
 
160
          
 
161
          void next_line();
 
162
          void delta_move(unsigned char x, unsigned char y);
 
163
          
 
164
        private:
 
165
          /** \brief Color palette. */
 
166
          const color_palette_type& m_palette;
 
167
          
 
168
          /** \brief The image to fill. */
 
169
          image& m_image;
 
170
          
 
171
          /** \brief Current column index in the bitmap. */
 
172
          unsigned int m_x;
 
173
          
 
174
          /** \brief Current row index in the bitmap. */
 
175
          unsigned int m_y;
 
176
          
 
177
        }; // class rle_bitmap_output_buffer
 
178
 
 
179
        /**
 
180
         * \brief RLE decoder for bitmap RLE format
 
181
         * 
 
182
         * \b Template \b parameters :
 
183
         * - \a OutputBuffer The type of the output buffer.
 
184
         *
 
185
         * The \a OutputBuffer type must match the type requirements of the
 
186
         * template parameter OutputBuffer of the rle_decoder class, plus two
 
187
         * methods :
 
188
         * - next_line(), set the output position at the begining of the next
 
189
         *                line.
 
190
         * - delta_move( unsigned char x, unsigned char y ), move the output
 
191
         *                position \a x pixels right and \a y pixels down.
 
192
         *
 
193
         * \author Julien Jorge
 
194
         */
 
195
        template< typename OutputBuffer >
 
196
        class rle_bitmap_decoder
 
197
          : public rle_decoder< char, file_input_buffer, OutputBuffer >
 
198
        {
 
199
        public:
 
200
          /** \brief Type of the output buffer. */
 
201
          typedef OutputBuffer output_buffer_type;
 
202
          
 
203
        private:
 
204
          virtual void read_mode( file_input_buffer& input,
 
205
                                  output_buffer_type& output );
 
206
        }; // class rle_bitmap_decoder
 
207
        
 
208
        /** \brief RLE decoder for 4 bpp bitmap images. */
 
209
        typedef
 
210
        rle_bitmap_decoder< rle_bitmap_output_buffer<true> > rle4_decoder;
 
211
        
 
212
        /** \brief RLE decoder for 8 bpp bitmap images. */
 
213
        typedef rle_bitmap_decoder< rle_bitmap_output_buffer<false> >
 
214
        rle8_decoder;
 
215
 
 
216
        /**
 
217
         * \brief Functor converting a 1bpp buffer to a 32bpp buffer.
 
218
         */
 
219
        class pixel1_to_pixel32
 
220
        {
 
221
        public:
 
222
          void operator()( scanline& dest, const char* src,
 
223
                           const color_palette_type& palette ) const;
 
224
        }; // class pixel1_to_pixel32
 
225
 
 
226
        /**
 
227
         * \brief Functor converting a 4bpp buffer to a 32bpp buffer.
 
228
         */
 
229
        class pixel4_to_pixel32
 
230
        {
 
231
        public:
 
232
          void operator()( scanline& dest, const char* src,
 
233
                           const color_palette_type& palette ) const;
 
234
        }; // class pixel4_to_pixel32
 
235
 
 
236
        /**
 
237
         * \brief Functor converting a 8bpp buffer to a 32bpp buffer.
 
238
         */
 
239
        class pixel8_to_pixel32
 
240
        {
 
241
        public:
 
242
          void operator()( scanline& dest, const char* src,
 
243
                           const color_palette_type& palette ) const;
 
244
        }; // class pixel8_to_pixel32
 
245
 
 
246
        /**
 
247
         * \brief Functor converting a 24bpp buffer to a 32bpp buffer.
 
248
         */
 
249
        class pixel24_to_pixel32
 
250
        {
 
251
        public:
 
252
          void operator()( scanline& dest, const char* src,
 
253
                           const color_palette_type& palette ) const;
 
254
        }; // class pixel24_to_pixel32
 
255
        
 
256
      public:
 
257
        reader( image& img );
 
258
        reader( image& img, std::istream& f );
 
259
 
 
260
        void load( std::istream& f );
 
261
 
 
262
      private:
 
263
        void load_palette( const header& h, std::istream& f,
 
264
                           color_palette_type& palette ) const;
 
265
        
 
266
        void load_1bpp( const header& h, std::istream& f );
 
267
        void load_4bpp( const header& h, std::istream& f );
 
268
        void load_8bpp( const header& h, std::istream& f );
 
269
        void load_24bpp( const header& h, std::istream& f );
 
270
        
 
271
        void load_4bpp_rle( const header& h, std::istream& f,
 
272
                            const color_palette_type& palette );
 
273
        void load_4bpp_rgb( const header& h, std::istream& f,
 
274
                            const color_palette_type& palette );
 
275
        void load_8bpp_rle( const header& h, std::istream& f,
 
276
                            const color_palette_type& palette );
 
277
        void load_8bpp_rgb( const header& h, std::istream& f,
 
278
                            const color_palette_type& palette );
 
279
        
 
280
        template<typename Convert>
 
281
        void load_rgb_data( std::istream& f, unsigned int buffer_size,
 
282
                            const color_palette_type& palette,
 
283
                            const Convert& pixel_convert );
 
284
 
 
285
      private:
 
286
        /** \brief The image in which we store the data we read. */
 
287
        image& m_image;
 
288
 
 
289
      }; // class reader
 
290
 
 
291
      /*----------------------------------------------------------------------*/
 
292
      /**
 
293
       * \brief This class write an image in a bitmap file.
 
294
       * \author Julien Jorge
 
295
       */
 
296
      class writer : private file_structure
 
297
      {
 
298
      public:
 
299
        writer( const image& img );
 
300
        writer( const image& img, std::ostream& f );
 
301
 
 
302
        void save( std::ostream& f ) const;
 
303
 
 
304
      private:
 
305
        void save_data( std::ostream& f ) const;
 
306
 
 
307
        void pixel32_to_pixel24( char* dest, const scanline& src ) const;
 
308
 
 
309
        void init_header( header& h ) const;
 
310
 
 
311
      private:
 
312
        /** \brief The image from which we read the data. */
 
313
        const image& m_image;
 
314
 
 
315
      }; // class writer
 
316
 
 
317
    public:
 
318
      bitmap( unsigned int w, unsigned int h );
 
319
      bitmap( const image& that );
 
320
      bitmap( std::istream& f );
 
321
 
 
322
      void save( std::ostream& f ) const;
 
323
    }; // class bitmap
 
324
 
 
325
  } // namespace graphic
 
326
} // namespace claw
 
327
 
 
328
#include <claw/impl/bitmap_reader.tpp>
 
329
 
 
330
#endif // __CLAW_BITMAP_HPP__