KSquare Utilities
BMPImage.h
Go to the documentation of this file.
1 /* BMPImage.h -- Manages the reading and writing of BMP image files.
2  * Copyright (C) 1994-2011 Kurt Kramer
3  * For conditions of distribution and use, see copyright notice in KKB.h
4  */
5 #ifndef _BMPIMAGE_
6 #define _BMPIMAGE_
7 
8 
9 #ifdef WIN32
10 #include <windows.h>
11 #include <conio.h>
12 #else
13 #include <stdio.h>
14 #include "BMPheader.h"
15 #endif
16 
17 #include "KKStr.h"
18 
19 //typedef unsigned char uchar;
20 
21 
22 namespace KKB
23 {
24  #ifndef _RASTER_
25  class Raster;
26  typedef Raster* RasterPtr;
27 
28  class PixelValue;
29  typedef PixelValue* PixelValuePtr;
30  #endif
31 
32  /**
33  *@class BmpImage
34  *@brief Used to encode and decode BMP Images.
35  *@details Purpose of this class it to facilitate the loading and saving of BMP Image files. Not all
36  * formats are supported, but the compressed version is, which most others don't support. As a result
37  * this class will write considerably smaller BMP files than other utilities that I have tried. This
38  * results in faster writing images due to less disk-io.
39  *
40  * This object was originally designed to work in conjunction with Image Extraction. It requires no
41  * other libraries and can read and write BMP files.
42  *
43  * When ImageExtraction creates a Bmp file it will provide pixel values between 0 and 7, where 0 is
44  * the background and 7 is the foreground. This is a result of the SIPPER 2 and 3 Formats and may
45  * change in the future. When saving the BMP file BMPImage will map these values through the palette
46  * to 0=255, 1=219, 2=182, 3=146, 4=109, 5=73, 6=36, 7=0 resulting in a white background and a Black
47  * foreground.
48  */
49  class BmpImage
50  {
51  private:
52 
53  #pragma pack(push,1)
54  struct BITMAPINFOHEADER
55  {
56  DWORD biSize;
57  LONG biWidth;
58  LONG biHeight;
59  WORD biPlanes;
60  WORD biBitCount;
61  DWORD biCompression;
62  DWORD biSizeImage;
63  LONG biXPelsPerMeter;
64  LONG biYPelsPerMeter;
65  DWORD biClrUsed;
66  DWORD biClrImportant;
67  };
68 
69 
70  struct BITMAPFILEHEADER
71  {
72  WORD bfType;
73  DWORD bfSize;
74  WORD bfReserved1;
75  WORD bfReserved2;
76  DWORD bfOffBits;
77  };
78 
79 
80  struct RGBQUAD
81  {
82  BYTE rgbBlue;
83  BYTE rgbGreen;
84  BYTE rgbRed;
85  BYTE rgbReserved;
86  };
87  #pragma pack(pop)
88 
89 
90  public:
91  BmpImage (kkint32 _height,
92  kkint32 _width,
93  kkint32 _numOfColors
94  );
95 
96  /**
97  *@brief Constructs a BMP image from the file specified by '_fileName'.
98  *@param[in] _fileName Name of file to load BMP image from.
99  *@param[out] successful Returns true if file successful in loading.
100  */
101  BmpImage (const KKStr& _fileName,
102  bool& successful
103  );
104 
105 
106  /**
107  *@brief Constructs a BMPImage instance from a Raster image; this is one way to save a Raster image to disk.
108  */
109  BmpImage (const Raster& raster);
110 
111 
112  void InitializeFields (kkint32 _height,
113  kkint32 _width
114  );
115 
116  ~BmpImage ();
117 
118  bool Color () const {return color;} /**< @brief Returns true if a Color image. */
119 
120  const
121  KKStr& FileName () const {return fileName;}
122 
123  bool FourBitUncompressed ();
124 
125  kkuint32 Height () const {return bmh.biHeight;}
126 
127  const
128  uchar* BlueRow (kkint32 row) const; /**< @brief Returns the specified Row from the Blue Channel. */
129 
130  uchar** Image () {return image;} /**< @brief Returns back two dimension matrix of image; if color it will be the green channel. */
131 
132  const
133  uchar* ImageRow (kkint32 row) const; /**< @brief Returns the specified Row from the Green Channel. */
134 
135  const
136  uchar* RedRow (kkint32 row) const; /**< @brief Returns the specified Row from the Red Channel. */
137 
138  uchar MaxPixVal () const {return uchar (maxPixVal);}
139 
140  kkuint32 Width () const {return bmh.biWidth;}
141 
142 
143  bool AreThereEdgePixels ();
144 
145  void ClearImage();
146 
147  void DoubleSize ();
148 
149  void DownSize ();
150 
151  void EliminateVerticalLines ();
152 
153  uchar& Pixel (kkint32 row,
154  kkint32 col
155  );
156 
157  void AddPixel (kkuint32 row,
158  kkuint32 col,
159  uchar pixValue
160  );
161 
162 
163  void Binarize ();
164 
165  void Print ();
166 
167  /** @brief Used to expand dimensions of image by 6 pixels so as to make sure that no image is along the edge. */
169 
170  void Save (const KKStr& fileName);
171 
172 
173  /**
174  *@brief Saves image using 4 bit compressed gray-scale where Background = 255 and foreground = 0
175  *@details If image is color will convert to gray-scale 1st.
176  * Palette will be set to 0 = 255, 1 = 238, 2 = 221, 3 = 204... 255 = 0.
177  */
178  void SaveGrayscaleInverted4Bit (const KKStr& _fileName);
179 
180 
181  /**
182  *@brief Saves image using compressed gray-scale where Background = 255 and foreground = 0
183  *@details If image is color will convert to gray-scale 1st.
184  * Palette will be set to 0 = 255, 1 = 254, 2 = 253, ... 255 = 0.
185  */
186  void SaveGrayscaleInverted8Bit (const KKStr& _fileName);
187 
188 
189  void Set16Colors ();
190 
191  void Set256Colors ();
192 
193 
194  void SetPaletteEntry (kkint32 palletIndex,
195  const PixelValue& pixValue
196  );
197 
198  /**
199  *@brief Will set the pixel value of the specified row and col to 'pixel'.
200  */
201  void SetPixelValue (kkint32 row,
202  kkint32 col,
203  kkint32 pixel
204  );
205 
206 
207  class CodedPixels; /**< @brief Class needed to support BMP Compression */
208  struct CodePair;
209  struct Bmp1BitRec;
210  struct Bmp4BitRecs;
211  struct BMP_24BitPixel;
212  union DWordParts;
213  union LongParts;
214  union WordParts;
215 
216  typedef CodePair* CodePairPtr;
217 
218 
219  private:
220  class PalletBuilder;
221  typedef PalletBuilder* PalletBuilderPtr;
222 
223  void AllocateRaster ();
224 
225  kkint32 BMIcolorArraySize (BITMAPINFOHEADER& _bmh);
226 
227  PalletBuilderPtr BuildPalletFromRasterData ();
228 
229  void CleanUpMemory ();
230 
231  void Load1BitColor (FILE* inFile,
232  bool& successfull
233  );
234 
235  void Load4BitColor (FILE* inFile,
236  bool& successfull
237  );
238 
239  void Load4BitColorCompressed (FILE* inFile,
240  bool& successfull
241  );
242 
243  void Load8BitColor (FILE* inFile,
244  bool& successfull
245  );
246 
247  void Load8BitColorCompressed (FILE* inFile,
248  bool& successfull
249  );
250 
251  void Load24BitColor (FILE* inFile,
252  bool& successfull
253  );
254 
255  bool ReversedGrayscaleImage ();
256 
257  void SaveGrayScale (FILE* outFile);
258 
259  void SaveColor (FILE* outFile);
260 
261  void SaveColorCompressed256 (PalletBuilderPtr palletBuilder,
262  FILE* outFile
263  );
264 
265  void SaveColor24BPP (FILE* outFile);
266 
267  void SetUp4BitPallet ();
268 
269  void SetUp8BitPallet ();
270 
271  void SetUp16BitPallet (RGBQUAD* palette);
272 
273  void SetUp256BitPalette (RGBQUAD* palette);
274 
275 
276  bool color; // true if color image.
277  BITMAPFILEHEADER hdr;
278  BITMAPINFOHEADER bmh;
279  KKStr fileName;
280  uchar** red;
281  uchar** image; /**< Used if gray-scale. */
282  uchar** blue;
283  kkint32 maxPixVal; /**< Largest PixelValue */
284  kkint32 numOfColors;
285  RGBQUAD* palette;
286  kkint32 paletteEntries;
287  kkint32 paletteMap[256];
288  };
289 
291 } /* namespace KKB; */
292 
293 #endif
Raster * RasterPtr
Definition: BMPImage.h:25
BmpImage * BmpImagePtr
Definition: BMPImage.h:290
__int32 kkint32
Definition: KKBaseTypes.h:88
bool Color() const
Returns true if a Color image.
Definition: BMPImage.h:118
bool AreThereEdgePixels()
Definition: BMPImage.cpp:2004
kkuint32 Height() const
Definition: BMPImage.h:125
void Set16Colors()
Definition: BMPImage.cpp:1194
void DoubleSize()
void ReAllocateForBiggerScreen()
Used to expand dimensions of image by 6 pixels so as to make sure that no image is along the edge...
Definition: BMPImage.cpp:1318
A class that is used by to represent a single image in memory.
Definition: Raster.h:108
void SaveGrayscaleInverted8Bit(const KKStr &_fileName)
Saves image using compressed gray-scale where Background = 255 and foreground = 0.
Definition: BMPImage.cpp:2268
This object is used to help encode the data stored in BMPImage::image into 8 or 4 bit compressed form...
Definition: BMPImage.cpp:163
const KKStr & FileName() const
Definition: BMPImage.h:121
const uchar * RedRow(kkint32 row) const
Returns the specified Row from the Red Channel.
Definition: BMPImage.cpp:2690
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
const uchar * ImageRow(kkint32 row) const
Returns the specified Row from the Green Channel.
Definition: BMPImage.cpp:2672
BmpImage(const Raster &raster)
Constructs a BMPImage instance from a Raster image; this is one way to save a Raster image to disk...
Definition: BMPImage.cpp:706
uchar & Pixel(kkint32 row, kkint32 col)
Definition: BMPImage.cpp:1958
void Binarize()
Definition: BMPImage.cpp:2189
PixelValue * PixelValuePtr
Definition: BMPImage.h:28
KKTHread * KKTHreadPtr
void DownSize()
Definition: BMPImage.cpp:1917
Used to encode and decode BMP Images.
Definition: BMPImage.h:49
BmpImage(kkint32 _height, kkint32 _width, kkint32 _numOfColors)
Definition: BMPImage.cpp:687
void SaveGrayscaleInverted4Bit(const KKStr &_fileName)
Saves image using 4 bit compressed gray-scale where Background = 255 and foreground = 0...
Definition: BMPImage.cpp:2207
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
void EliminateVerticalLines()
Definition: BMPImage.cpp:2063
CodePair * CodePairPtr
Definition: BMPImage.h:214
void Save(const KKStr &fileName)
Definition: BMPImage.cpp:2331
kkuint32 Width() const
Definition: BMPImage.h:140
const uchar * BlueRow(kkint32 row) const
Returns the specified Row from the Blue Channel.
Definition: BMPImage.cpp:2652
void SetPaletteEntry(kkint32 palletIndex, const PixelValue &pixValue)
Definition: BMPImage.cpp:935
uchar MaxPixVal() const
Definition: BMPImage.h:138
void InitializeFields(kkint32 _height, kkint32 _width)
Definition: BMPImage.cpp:1011
void AddPixel(kkuint32 row, kkuint32 col, uchar pixValue)
Definition: BMPImage.cpp:2630
void SetPixelValue(kkint32 row, kkint32 col, kkint32 pixel)
Will set the pixel value of the specified row and col to &#39;pixel&#39;.
Definition: BMPImage.cpp:1967
uchar ** Image()
Returns back two dimension matrix of image; if color it will be the green channel.
Definition: BMPImage.h:130
bool FourBitUncompressed()
Definition: BMPImage.cpp:2709
BmpImage(const KKStr &_fileName, bool &successful)
Constructs a BMP image from the file specified by &#39;_fileName&#39;.
Definition: BMPImage.cpp:449
void Set256Colors()
Definition: BMPImage.cpp:1208
Used by the Raster Class to represent the contents of one pixel.
Definition: PixelValue.h:22
void ClearImage()
Definition: BMPImage.cpp:2640