Claw  1.7.3
bitmap_writer.cpp
Go to the documentation of this file.
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 */
30 #include <claw/bitmap.hpp>
31 #include <algorithm>
32 
33 /*----------------------------------------------------------------------------*/
39  : m_image(img)
40 {
41 
42 } // bitmap::writer::writer()
43 
44 /*----------------------------------------------------------------------------*/
50 claw::graphic::bitmap::writer::writer( const image& img, std::ostream& f )
51  : m_image(img)
52 {
53  save(f);
54 } // bitmap::writer::writer()
55 
56 /*----------------------------------------------------------------------------*/
61 void claw::graphic::bitmap::writer::save( std::ostream& f ) const
62 {
63  header h;
64 
65  init_header(h);
66 
67  f.write( reinterpret_cast<char*>(&h), sizeof(header) );
68 
69  save_data( f );
70 } // bitmap::writer::save()
71 
72 /*----------------------------------------------------------------------------*/
77 void claw::graphic::bitmap::writer::save_data( std::ostream& f ) const
78 {
79  unsigned int line;
80  unsigned int buffer_size = m_image.width() * 3;
81 
82  // lines are 4-bytes aligned, so adjust buffer's size.
83  if (buffer_size % 4 != 0)
84  buffer_size += 4 - buffer_size % 4;
85 
86  char* buffer = new char[buffer_size];
87 
88  for (line = m_image.height(); line>0; )
89  {
90  --line;
91  pixel32_to_pixel24( buffer, m_image[line] );
92  f.write(buffer, buffer_size);
93  }
94 
95  delete[] buffer;
96 } // bitmap::writer::save_data()
97 
98 /*----------------------------------------------------------------------------*/
104 void claw::graphic::bitmap::writer::pixel32_to_pixel24
105 ( char* dest, const scanline& src ) const
106 {
107  unsigned int i24 = 0;
109  scanline::const_iterator last( src.end() );
110 
111  for ( ; first!=last; ++first )
112  {
113  dest[i24++] = first->components.blue;
114  dest[i24++] = first->components.green;
115  dest[i24++] = first->components.red;
116  }
117 } // bitmap::writer::pixel32_to_pixel24()
118 
119 /*----------------------------------------------------------------------------*/
124 void claw::graphic::bitmap::writer::init_header( header& h ) const
125 {
126  unsigned int adjusted_line = m_image.width() * 3;
127 
128  if (m_image.width() % 4 != 0)
129  adjusted_line += 4 - m_image.width() % 4;
130 
131  // for a 24 bpp bitmap.
132  h.id[0] = 'B';
133  h.id[1] = 'M';
134  h.file_size = adjusted_line * m_image.height() + sizeof(h);
135  h.nop = 0;
136 
137  // there is no color pallet, so data is just after the h
138  h.data_offset = sizeof(h);
139  // default value for Windows' bitmaps.
140  h.header_size = 0x28;
141 
142  h.width = m_image.width();
143  h.height = m_image.height();
144  h.layers = 1;
145  h.bpp = 24;
146  h.compression = BMP_COMPRESSION_RGB;
147  h.image_size = adjusted_line * m_image.height();
148  h.ppm_x = 0x2E23; // 11811
149  h.ppm_y = 0x2E23;
150  h.colors_count = 0;
151  h.importants_colors = 0;
152 } // bitmap::writer::init_header()
153 
One line in the image.
Definition: image.hpp:59
char id[2]
File identifier (must be &#39;BM&#39;).
Definition: bitmap.hpp:79
unsigned int width
Image&#39;s width.
Definition: bitmap.hpp:94
unsigned short bpp
Bits per pixel.
Definition: bitmap.hpp:103
unsigned short layers
Number of layers.
Definition: bitmap.hpp:100
super::const_iterator const_iterator
Const iterator in the line.
Definition: image.hpp:81
unsigned int height() const
Gets image&#39;s height.
Definition: image.cpp:159
unsigned int image_size
Image&#39;s size (bytes).
Definition: bitmap.hpp:109
void save(std::ostream &f) const
Save the bitmap in a file.
Fuction object to get the first element of a std::pair.
Definition: functional.hpp:44
iterator end()
Get en iterator past the last pixel.
Definition: image.cpp:63
A class for bitmap pictures.
unsigned int header_size
Header&#39;s size.
Definition: bitmap.hpp:91
unsigned int width() const
Gets image&#39;s width.
Definition: image.cpp:147
iterator begin()
Get an iterator on the first pixel.
Definition: image.cpp:54
unsigned int colors_count
Number of colors.
Definition: bitmap.hpp:118
unsigned int file_size
File&#39;s size.
Definition: bitmap.hpp:82
unsigned int data_offset
Begininf of the datas.
Definition: bitmap.hpp:88
unsigned int importants_colors
Number of important colors.
Definition: bitmap.hpp:121
unsigned int ppm_y
Vertical resolution (pixels per meter).
Definition: bitmap.hpp:115
unsigned int height
Image&#39;s height.
Definition: bitmap.hpp:97
writer(const image &img)
Constructor.
A class to deal with images.
Definition: image.hpp:49
unsigned int ppm_x
Horizontal resolution (pixels per meter).
Definition: bitmap.hpp:112
unsigned int compression
Compression algorithm.
Definition: bitmap.hpp:106