KSquare Utilities
KKB::BmpImage::CodedPixels Class Reference

This object is used to help encode the data stored in BMPImage::image into 8 or 4 bit compressed formats used by BMP files. *. More...

Public Member Functions

 CodedPixels (kkint32 _height, kkint32 _width)
 
 ~CodedPixels ()
 
void AddPixel (uchar pixel)
 
ucharCreatePixelDataStructure4Bit (kkint32 &len)
 
ucharCreatePixelDataStructure8Bit (kkint32 &len)
 
void EOL ()
 

Detailed Description

This object is used to help encode the data stored in BMPImage::image into 8 or 4 bit compressed formats used by BMP files. *.

Definition at line 163 of file BMPImage.cpp.

Constructor & Destructor Documentation

KKB::BmpImage::CodedPixels::CodedPixels ( kkint32  _height,
kkint32  _width 
)

Definition at line 200 of file BMPImage.cpp.

202  :
203  height (_height),
204  width (_width),
205  top (NULL)
206 {
207  codesAllocated = height * (width + 4);
208  used = 0;
209 
210  codes = new CodePair[codesAllocated];
211 }
KKB::BmpImage::CodedPixels::~CodedPixels ( )

Definition at line 215 of file BMPImage.cpp.

216 {
217  delete codes;
218 }

Member Function Documentation

void KKB::BmpImage::CodedPixels::AddPixel ( uchar  pixel)

This method is called once for each pixel in the BMP image

Definition at line 223 of file BMPImage.cpp.

References KKB::BmpImage::CodePair::count, and KKB::BmpImage::CodePair::pixel.

Referenced by KKB::BmpImage::SaveGrayscaleInverted4Bit(), and KKB::BmpImage::SaveGrayscaleInverted8Bit().

224 {
225  if (used >= codesAllocated)
226  {
227  // We are exceeding the codes Size.
228  cerr << "CodedPixels::AddPixel *** Error ***, Exceeding codesAllocated." << std::endl;
229  return;
230  }
231 
232  if (!top)
233  {
234  top = &(codes[0]);
235  used++;
236  top->count = 1;
237  top->pixel = pixel;
238  }
239 
240  else if ((pixel != top->pixel) || (top->count == 0))
241  {
242  used++;
243  top++;
244  top->count = 1;
245  top->pixel = pixel;
246  }
247 
248  else
249  {
250  if (top->count >= 254)
251  {
252  used++;
253  top++;
254  top->count = 1;
255  top->pixel = pixel;
256  }
257  else
258  {
259  top->count++;
260  }
261  }
262 } /* AddPixel */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
uchar * KKB::BmpImage::CodedPixels::CreatePixelDataStructure4Bit ( kkint32 len)

Creates the appropriate compressed data structure for the BMP 4-bit compressed file format. The length of the structure created will be returned in the 'len' parameter. The caller will be responsible for deleting the returned data structure.

Definition at line 277 of file BMPImage.cpp.

References KKB::BmpImage::CodePair::count, and KKB::BmpImage::CodePair::pixel.

Referenced by KKB::BmpImage::SaveGrayscaleInverted4Bit().

278 {
279  kkint32 buffSize = codesAllocated;
280 
281  uchar* buff = new uchar [buffSize + 4]; // + 4 For Safety
282  memset (buff, 0, codesAllocated);
283 
284 
285  kkint32 curPair = 0;
286  top = &(codes[0]);
287 
288  kkint32 bp = 0;
289 
290  while (curPair < used)
291  {
292  if (bp >= buffSize)
293  {
294  kkint32 newBuffSize = buffSize * 2;
295  uchar* newBuff = new uchar[newBuffSize];
296  memset (newBuff, 0, newBuffSize);
297  memcpy (newBuff, buff, buffSize);
298  delete[] buff;
299  buff = newBuff;
300  newBuff = NULL;
301  buffSize = newBuffSize;
302  }
303 
304 
305  {
306  if ((top->count == 0) && (top->pixel == 0))
307  {
308  // Add End Of Line
309  buff[bp] = 0;
310  bp++;
311  buff[bp] = 0;
312  bp++;
313 
314  curPair++;
315  top++;
316  }
317 
318  else
319  {
320  if (top->count > 254)
321  {
322  buff[bp] = 254;
323  bp++;
324 
325  buff[bp] = top->pixel * 16 + top->pixel;
326  bp++;
327 
328  top->count = top->count - 254;
329  }
330 
331  else
332  {
333  buff[bp] = top->count;
334  bp++;
335 
336  buff[bp] = top->pixel * 16 + top->pixel;
337  bp++;
338 
339  curPair++;
340  top++;
341  }
342  }
343  }
344  }
345 
346  buff[bp] = 0;
347  bp++;
348 
349  buff[bp] = 1;
350  bp++;
351 
352  len = bp;
353  return buff;
354 } /* CreatePixelDataStructure */
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
uchar * KKB::BmpImage::CodedPixels::CreatePixelDataStructure8Bit ( kkint32 len)

Creates the appropriate compressed data structure for the BMP 8-bit compressed file format. The length of the structure created will be returned in the 'len' parameter. The caller will be responsible for deleting the returned data structure.

Definition at line 358 of file BMPImage.cpp.

References KKB::KKStr::Concat(), KKB::BmpImage::CodePair::count, KKB::KKException::KKException(), and KKB::BmpImage::CodePair::pixel.

Referenced by KKB::BmpImage::SaveGrayscaleInverted8Bit().

359 {
360  kkint32 buffSize = codesAllocated;
361 
362  uchar* buff = new uchar [buffSize + 4]; // + 4 For Safety
363  memset (buff, 0, codesAllocated);
364 
365  kkint32 curPair = 0;
366  top = &(codes[0]);
367 
368  kkint32 bp = 0;
369 
370  while (curPair < used)
371  {
372  if (bp >= (buffSize - 1))
373  {
374  kkint32 newBuffSize = buffSize * 2;
375  uchar* newBuff = new uchar[newBuffSize + 2];
376  if (newBuff == NULL)
377  {
378  KKStr errMsg = "BmpImage::CodedPixels::CreatePixelDataStructure8Bit ***ERROR*** Allocation of 'newBuff' failed.";
379  cerr << std::endl << std::endl << errMsg << std::endl << std::endl;
380  throw KKException (errMsg);
381  }
382 
383  memset (newBuff, 0, newBuffSize);
384  memcpy (newBuff, buff, buffSize);
385  delete[] buff;
386  buff = newBuff;
387  newBuff = NULL;
388  buffSize = newBuffSize;
389  }
390 
391 
392  {
393  if ((top->count == 0) && (top->pixel == 0))
394  {
395  // Add End Of Line
396  buff[bp] = 0;
397  bp++;
398  buff[bp] = 0;
399  bp++;
400 
401  curPair++;
402  top++;
403  }
404 
405  else
406  {
407  if (top->count > 254)
408  {
409  buff[bp] = 254;
410  bp++;
411 
412  buff[bp] = top->pixel;
413  bp++;
414 
415  top->count = top->count - 254;
416  }
417 
418  else
419  {
420  buff[bp] = top->count;
421  bp++;
422 
423  buff[bp] = top->pixel;
424  bp++;
425 
426  curPair++;
427  top++;
428  }
429  }
430  }
431  }
432 
433 
434  buff[bp] = 0;
435  bp++;
436 
437  buff[bp] = 1;
438  bp++;
439 
440  len = bp;
441  return buff;
442 } /* CreatePixelDataStructure */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
void KKB::BmpImage::CodedPixels::EOL ( )

This is called once at the end of each row of pixels.

Definition at line 266 of file BMPImage.cpp.

References KKB::BmpImage::CodePair::count, and KKB::BmpImage::CodePair::pixel.

Referenced by KKB::BmpImage::SaveGrayscaleInverted4Bit(), and KKB::BmpImage::SaveGrayscaleInverted8Bit().

267 {
268  used++;
269  top++;
270  top->count = 0;
271  top->pixel = 0;
272 } /* EOL */

The documentation for this class was generated from the following file: