PGF Console  6.21.2
CImage.cpp
Go to the documentation of this file.
1 /*
2  * PGFConsole: A PGF-codec demonstration
3  * $Date: 2006-05-09 20:13:33 +0200 (Di, 09 Mai 2006) $
4  * $Revision: 187 $
5 
6  * This file Copyright (C) 2006 xeraina GmbH, Switzerland
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22 
23 #include "CImage.h"
24 #include "FreeImagePlus.h"
25 #include <cassert>
26 
29  m_image = new fipImage();
30 
31 #ifdef __PNMEXSUPPORT__
32  SetMaxValue(0);
33 #endif
34 }
35 
38  delete m_image;
39 }
40 
42 // default create function (standard bitmap with color depth)
43 bool CImage::Create(int width, int height, int bpp) {
44  return TRUE == m_image->setSize(FIT_BITMAP, (WORD)width, (WORD)height, (WORD)bpp);
45 }
46 
48 // image format and bpp are chosen by given type
49 bool CImage::Create(int width, int height, unsigned char type) {
50  FREE_IMAGE_TYPE FItype;
51  int bpp;
52  switch (type) {
53  case ImageModeBitmap:
54  FItype = FIT_BITMAP;
55  bpp = 1;
56  break;
57 
59  case ImageModeGrayScale:
60  FItype = FIT_BITMAP;
61  bpp = 8; // only 8-bit is supported
62  break;
63 
64  case ImageModeGray16:
65  FItype = FIT_UINT16;
66  bpp = 16;
67  break;
68 
69  case ImageModeRGB16:
70  FItype = FIT_BITMAP;
71  bpp = 16;
72  // RGB565 image needs RGB mask
73  return TRUE == m_image->setSize(FItype, (WORD)width, (WORD)height, (WORD)bpp, FI16_565_RED_MASK, FI16_565_GREEN_MASK, FI16_565_BLUE_MASK);
74 
75  case ImageModeRGBColor:
76  FItype = FIT_BITMAP;
77  bpp = 24;
78  break;
79 
80  case ImageModeRGB48:
81  FItype = FIT_RGB16;
82  bpp = 48;
83  break;
84 
85  case ImageModeRGBA:
86  FItype = FIT_BITMAP;
87  bpp = 32;
88  break;
89 
90  default:
91  return false;
92  }
93  return TRUE == m_image->setSize(FItype, (WORD)width, (WORD)height, (WORD)bpp);
94 }
95 
97 unsigned char* CImage::GetBits() const {
98  return (unsigned char*)m_image->accessPixels();
99 }
100 
102 int CImage::GetPitch() const {
103  return m_image->getScanWidth();
104 }
105 
107 unsigned char CImage::GetBPP() const {
108  return (unsigned char)m_image->getBitsPerPixel();
109 }
110 
112 unsigned int CImage::GetHeight() const {
113  return m_image->getHeight();
114 }
115 
117 unsigned int CImage::GetWidth() const {
118  return m_image->getWidth();
119 }
120 
122 bool CImage::Save(const char* dest) {
123  return TRUE == m_image->save(dest, 0);
124 }
125 
127 bool CImage::Load(const char* source) {
128  if (m_image->load(source, 0)) {
129  if (GetColorType() == ImageModeRGB16) {
130  // force RGB-565 for 16-bit images since PGF does not support RGB-555
131  m_image->convertTo16Bits565();
132  }
133  return true;
134  }
135  return false;
136 }
137 
140  return (m_image->getColorType() == FIC_RGBALPHA);
141 }
142 
145  return ((m_image->getColorType() == FIC_PALETTE) ||
146  (m_image->getColorType() == FIC_MINISBLACK) ||
147  (m_image->getColorType() == FIC_MINISWHITE));
148 }
149 
152  return m_image->getColorsUsed();
153 }
154 
156 void CImage::GetColorTable(int firstColor, int numColors, void* prgbColors) {
157  RGBQUAD* palette = m_image->getPalette();
158  int max = firstColor + numColors;
159  // ugly hack but necessary due to conflict between PGFplatform.h and FreeImage.h
160  RGBQUAD* target = (RGBQUAD*)prgbColors;
161  for (int i = firstColor; i < max; ++i) {
162  target[i] = palette[i];
163  }
164 }
165 
167 void CImage::SetColorTable(int firstColor, int numColors, const void* prgbColors) {
168  RGBQUAD* palette = m_image->getPalette();
169  int max = firstColor + numColors;
170  // ugly hack but necessary due to conflict between PGFplatform.h and FreeImage.h
171  RGBQUAD* source = (RGBQUAD*)prgbColors;
172  for (int i = firstColor; i < max; ++i) {
173  palette[i] = source[i];
174  }
175 }
176 
178 unsigned char CImage::GetColorType() const {
179  switch (m_image->getImageType()) {
180  case FIT_BITMAP:
181  // further analysis necessary
182  switch (m_image->getColorType()) {
183  case FIC_MINISBLACK:
184  case FIC_MINISWHITE:
185  switch (m_image->getBitsPerPixel()) {
186  case 1:
187  return ImageModeBitmap;
188 
189  case 8:
190  return ImageModeGrayScale;
191 
192  case 16:
193  return ImageModeGray16;
194 
195  default:
196  return ImageModeUnknown;
197  }
198 
199  case FIC_PALETTE:
200  return ImageModeIndexedColor;
201 
202  case FIC_RGB:
203  switch (m_image->getBitsPerPixel()) {
204  case 16:
205  return ImageModeRGB16;
206 
207  case 24:
208  case 32:
209  return ImageModeRGBColor;
210 
211  case 48:
212  case 64:
213  return ImageModeRGB48;
214 
215  default:
216  return ImageModeUnknown;
217  }
218  break;
219 
220  case FIC_RGBALPHA:
221  switch (m_image->getBitsPerPixel()) {
222  case 32:
223  return ImageModeRGBA;
224 
225  default:
226  return ImageModeUnknown;
227  }
228  break;
229 
230  default:
231  return ImageModeUnknown;
232  }
233  break;
234 
235  case FIT_UINT16:
236  if (m_image->getColorType() == FIC_RGB)
237  return ImageModeRGB16;
238  else
239  return ImageModeGray16;
240 
241  case FIT_RGB16:
242  return ImageModeRGB48;
243 
244  default:
245  return ImageModeUnknown;
246  }
247 }
248 
251  switch(GetColorType()) {
252  case ImageModeBitmap:
253  return 1;
254 
255  case ImageModeRGB16:
256  // it's actually RGB-565, but returning 5 is ok
257  return 5;
258 
259  case ImageModeGrayScale:
261  case ImageModeRGBColor:
262  case ImageModeRGBA:
263  return 8;
264 
265  case ImageModeGray16:
266  case ImageModeRGB48:
267  return 16;
268 
269  default:
270  assert(false);
271  return 0;
272  }
273 }
274 
276 int CImage::GetChannels() const {
277  switch(GetColorType()) {
278  case ImageModeBitmap:
279  case ImageModeGrayScale:
280  case ImageModeGray16:
282  return 1;
283 
284  case ImageModeRGB16:
285  case ImageModeRGBColor:
286  case ImageModeRGB48:
287  return 3;
288 
289  case ImageModeRGBA:
290  return 4;
291 
292  default:
293  assert(false);
294  return 0;
295  }
296 }
297 
298 #ifdef __PNMEXSUPPORT__
299 // Register another PGM plugin
301 extern void DLL_CALLCONV InitPNM(Plugin *plugin, int format_id);
302 
304  FREE_IMAGE_FORMAT fif = FreeImage_RegisterLocalPlugin(InitPNM);
305  // disable original pnm plugin
306  FreeImage_SetPluginEnabled(FIF_PBMRAW, FALSE);
307  FreeImage_SetPluginEnabled(FIF_PBM, FALSE);
308  FreeImage_SetPluginEnabled(FIF_PGMRAW, FALSE);
309  FreeImage_SetPluginEnabled(FIF_PGM, FALSE);
310  FreeImage_SetPluginEnabled(FIF_PPMRAW, FALSE);
311  FreeImage_SetPluginEnabled(FIF_PPM, FALSE);
312  // enable new pnm plugin
313  FreeImage_SetPluginEnabled(fif, TRUE);
314 }
315 
317 extern int s_maxval;
318 
319 void CImage::SetMaxValue(int maxValue) {
320  s_maxval = maxValue;
321 }
322 
323 int CImage::GetMaxValue() const {
324  return (s_maxval > 0) ? s_maxval : ((1 << GetChannelDepth()) - 1);
325 }
326 
327 #endif
328 
fipImage * m_image
Definition: CImage.h:59
bool Load(const char *source)
Definition: CImage.cpp:127
#define ImageModeGray16
Definition: CImage.h:43
#define ImageModeRGBColor
Definition: CImage.h:36
bool IsIndexed()
Definition: CImage.cpp:144
static void RegisterPNM()
Definition: CImage.cpp:303
unsigned char GetBPP() const
Definition: CImage.cpp:107
#define ImageModeGrayScale
Definition: CImage.h:34
#define ImageModeBitmap
Definition: CImage.h:33
#define ImageModeIndexedColor
Definition: CImage.h:35
void DLL_CALLCONV InitPNM(Plugin *plugin, int format_id)
Definition: PNMPlugin.cpp:797
unsigned char * GetBits() const
Definition: CImage.cpp:97
unsigned char GetColorType() const
Definition: CImage.cpp:178
bool Create(int width, int height, int bpp)
Definition: CImage.cpp:43
#define ImageModeRGB48
Definition: CImage.h:44
CImage()
Definition: CImage.cpp:28
bool IsTransparencySupported()
Definition: CImage.cpp:139
virtual ~CImage()
Definition: CImage.cpp:37
void GetColorTable(int firstColor, int numColors, void *prgbColors)
Definition: CImage.cpp:156
void SetColorTable(int firstColor, int numColors, const void *prgbColors)
Definition: CImage.cpp:167
int s_maxval
Definition: PNMPlugin.cpp:122
bool Save(const char *dest)
Definition: CImage.cpp:122
int GetMaxColorTableEntries()
Definition: CImage.cpp:151
int GetMaxValue() const
Definition: CImage.cpp:323
int GetPitch() const
Definition: CImage.cpp:102
unsigned int GetHeight() const
Definition: CImage.cpp:112
int GetChannelDepth() const
Definition: CImage.cpp:250
int GetChannels() const
Definition: CImage.cpp:276
#define ImageModeUnknown
Definition: CImage.h:54
unsigned int GetWidth() const
Definition: CImage.cpp:117
#define ImageModeRGB16
Definition: CImage.h:53
void SetMaxValue(int maxValue)
Definition: CImage.cpp:319
#define ImageModeRGBA
Definition: CImage.h:50