Rpi-hw  0.7.2
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
bitmap-inl.hpp
1 /*
2  Title --- bitmap-inl.hpp
3 
4  Copyright (C) 2013 Giacomo Trudu - wicker25[at]gmail[dot]com
5 
6  This file is part of Rpi-hw.
7 
8  Rpi-hw is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation version 3 of the License.
11 
12  Rpi-hw is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with Rpi-hw. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 
22 #ifndef _RPI_HW_IMAGE_BITMAP_INL_HPP_
23 #define _RPI_HW_IMAGE_BITMAP_INL_HPP_
24 
25 namespace rpihw { // Begin main namespace
26 
27 namespace image { // Begin images namespace
28 
29 template < typename T >
30 bitmap< T >::bitmap( uint16_t width, uint16_t height, uint8_t channels )
31 
32  : m_width ( width )
33  , m_height ( height )
34  , m_channels ( channels )
35  , m_buffer ( width * height * channels, 0 ) {
36 
37 }
38 
39 
40 #if USE_MAGICK
41 
42 template < typename T >
43 bitmap< T >::bitmap( const std::string &path ) {
44 
45  // Load the image using Magick++ API
46  Magick::Image img( path );
47 
48  // Convert the Magick::Image object to the bitmap buffer
49  loadMagickObj( img );
50 }
51 
52 template < typename T >
53 bitmap< T >::bitmap( Magick::Image &img ) {
54 
55  // Convert the Magick::Image object to the bitmap buffer
56  loadMagickObj( img );
57 }
58 
59 template < typename T >
60 void
61 bitmap< T >::loadMagickObj( Magick::Image &img ) {
62 
63  // Copy the image data
64  m_width = (uint16_t) img.columns();
65  m_height = (uint16_t) img.rows();
66 
67  // Get the pixels of the image
68  const Magick::PixelPacket *pixels = img.getConstPixels( 0, 0, m_width, m_height );
69 
70  // Size of the bitmap buffer (incomplete)
71  size_t size = m_width * m_height;
72 
73  // Iterator
74  size_t i;
75 
76  // Convert Magick::Image object to the bitmap buffer
77  switch ( img.type() ) {
78 
79  case Magick::PaletteMatteType:
80  case Magick::TrueColorMatteType:
81  case Magick::ColorSeparationType: {
82 
83  // Set the number of image channels
84  m_channels = 4;
85 
86  // Allocate the bitmap buffer
87  m_buffer.resize( size * m_channels );
88 
89  // Iterator of the bitmap buffer
90  auto it = m_buffer.begin();
91 
92  for ( i = 0; i < size; ++i, ++pixels ) {
93 
94  *it++ = (T) pixels->red;
95  *it++ = (T) pixels->green;
96  *it++ = (T) pixels->blue;
97  *it++ = (T) pixels->opacity;
98  }
99 
100  break;
101  }
102 
103  case Magick::PaletteType:
104  case Magick::TrueColorType: {
105 
106  // Set the number of image channels
107  m_channels = 3;
108 
109  // Allocate the bitmap buffer
110  m_buffer.resize( size * m_channels );
111 
112  // Iterator of the bitmap buffer
113  auto it = m_buffer.begin();
114 
115  for ( i = 0; i < size; ++i, ++pixels ) {
116 
117  *it++ = (T) pixels->red;
118  *it++ = (T) pixels->green;
119  *it++ = (T) pixels->blue;
120  }
121 
122  break;
123  }
124 
125  case Magick::GrayscaleMatteType: {
126 
127  // Set the number of image channels
128  m_channels = 2;
129 
130  // Allocate the bitmap buffer
131  m_buffer.resize( size * m_channels );
132 
133  // Iterator of the bitmap buffer
134  auto it = m_buffer.begin();
135 
136  for ( i = 0; i < size; ++i, ++pixels ) {
137 
138  *it++ = (T) pixels->red;
139  *it++ = (T) pixels->opacity;
140  }
141 
142  break;
143  }
144 
145  default: {
146 
147  // Set the number of image channels
148  m_channels = 1;
149 
150  // Allocate the bitmap buffer
151  m_buffer.resize( size * m_channels );
152 
153  // Iterator of the bitmap buffer
154  auto it = m_buffer.begin();
155 
156  for ( i = 0; i < size; ++i, ++pixels )
157  *it++ = (T) pixels->red;
158 
159  break;
160  }
161  }
162 }
163 
164 #endif /* USE_MAGICK */
165 
166 
167 template < typename T >
169 
170 }
171 
172 template < typename T >
173 inline uint16_t
175 
176  // Return the width of the image
177  return m_width;
178 }
179 
180 template < typename T >
181 inline uint16_t
183 
184  // Return the height of the image
185  return m_height;
186 }
187 
188 template < typename T >
189 inline uint8_t
191 
192  // Return the number of image channels
193  return m_channels;
194 }
195 
196 template < typename T >
197 inline void
198 bitmap< T >::setData( uint16_t x, uint16_t y, T *color ) {
199 
200  // Set the color of the pixel
201  std::copy( color, color + m_channels, m_buffer + (size_t) m_channels * ( (size_t) x + (size_t) y * (size_t) m_width ) );
202 }
203 
204 template < typename T >
205 inline void
206 bitmap< T >::setData( uint16_t x, uint16_t y, uint8_t c, T value ) {
207 
208  // Set the channel value of the pixel
209  m_buffer[ (size_t) m_channels * ( (size_t) x + (size_t) y * (size_t) m_width ) + c ] = value;
210 }
211 
212 template < typename T >
213 inline color_iterator< T >
214 bitmap< T >::getData( uint16_t x, uint16_t y ) const {
215 
216  // Return the color data into the buffer
217  return m_buffer.begin() + (size_t) m_channels * ( (size_t) x + (size_t) y * (size_t) m_width );
218 }
219 
220 template < typename T >
221 inline T
222 bitmap< T >::getData( uint16_t x, uint16_t y, uint8_t c ) const {
223 
224  // Return the channel value of the pixel
225  return *( getData( x, y ) + c );
226 }
227 
228 } // End of images namespace
229 
230 } // End of main namespace
231 
232 #endif /* _RPI_HW_IMAGE_BITMAP_INL_HPP_ */
virtual uint16_t getWidth() const
Returns the width of the image.
Definition: bitmap-inl.hpp:174
virtual uint16_t getHeight() const
Returns the height of the image.
Definition: bitmap-inl.hpp:182
virtual color_iterator< T > getData(uint16_t x, uint16_t y) const
Returns the color of a pixel.
Definition: bitmap-inl.hpp:214
typename std::vector< T >::const_iterator color_iterator
Color iterator.
Definition: types.hpp:45
virtual ~bitmap()
Destructor method.
Definition: bitmap-inl.hpp:168
virtual uint8_t getSpectrum() const
Returns the number of image channels.
Definition: bitmap-inl.hpp:190
void setData(uint16_t x, uint16_t y, T *color)
Sets the color of a pixel.
Definition: bitmap-inl.hpp:198
bitmap(uint16_t width, uint16_t height, uint8_t channels)
Constructor method.
Definition: bitmap-inl.hpp:30