KSquare Utilities
KKB::BmpImage Class Reference

Used to encode and decode BMP Images. More...

#include <BMPImage.h>

Classes

struct  Bmp1BitRec
 
struct  Bmp4BitRecs
 
struct  BMP_24BitPixel
 
class  CodedPixels
 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...
 
struct  CodePair
 
class  PalletBuilder
 

Public Types

typedef CodePairCodePairPtr
 

Public Member Functions

 BmpImage (kkint32 _height, kkint32 _width, kkint32 _numOfColors)
 
 BmpImage (const KKStr &_fileName, bool &successful)
 Constructs a BMP image from the file specified by '_fileName'. More...
 
 BmpImage (const Raster &raster)
 Constructs a BMPImage instance from a Raster image; this is one way to save a Raster image to disk. More...
 
 ~BmpImage ()
 
void AddPixel (kkuint32 row, kkuint32 col, uchar pixValue)
 
bool AreThereEdgePixels ()
 
void Binarize ()
 
const ucharBlueRow (kkint32 row) const
 Returns the specified Row from the Blue Channel. More...
 
void ClearImage ()
 
bool Color () const
 Returns true if a Color image. More...
 
void DoubleSize ()
 
void DownSize ()
 
void EliminateVerticalLines ()
 
const KKStrFileName () const
 
bool FourBitUncompressed ()
 
kkuint32 Height () const
 
uchar ** Image ()
 Returns back two dimension matrix of image; if color it will be the green channel. More...
 
const ucharImageRow (kkint32 row) const
 Returns the specified Row from the Green Channel. More...
 
void InitializeFields (kkint32 _height, kkint32 _width)
 
uchar MaxPixVal () const
 
ucharPixel (kkint32 row, kkint32 col)
 
void Print ()
 
void ReAllocateForBiggerScreen ()
 Used to expand dimensions of image by 6 pixels so as to make sure that no image is along the edge. More...
 
const ucharRedRow (kkint32 row) const
 Returns the specified Row from the Red Channel. More...
 
void Save (const KKStr &fileName)
 
void SaveGrayscaleInverted4Bit (const KKStr &_fileName)
 Saves image using 4 bit compressed gray-scale where Background = 255 and foreground = 0. More...
 
void SaveGrayscaleInverted8Bit (const KKStr &_fileName)
 Saves image using compressed gray-scale where Background = 255 and foreground = 0. More...
 
void Set16Colors ()
 
void Set256Colors ()
 
void SetPaletteEntry (kkint32 palletIndex, const PixelValue &pixValue)
 
void SetPixelValue (kkint32 row, kkint32 col, kkint32 pixel)
 Will set the pixel value of the specified row and col to 'pixel'. More...
 
kkuint32 Width () const
 

Detailed Description

Used to encode and decode BMP Images.

Purpose of this class it to facilitate the loading and saving of BMP Image files. Not all formats are supported, but the compressed version is, which most others don't support. As a result this class will write considerably smaller BMP files than other utilities that I have tried. This results in faster writing images due to less disk-io.

This object was originally designed to work in conjunction with Image Extraction. It requires no other libraries and can read and write BMP files.

When ImageExtraction creates a Bmp file it will provide pixel values between 0 and 7, where 0 is the background and 7 is the foreground. This is a result of the SIPPER 2 and 3 Formats and may change in the future. When saving the BMP file BMPImage will map these values through the palette 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 foreground.

Definition at line 49 of file BMPImage.h.

Member Typedef Documentation

Definition at line 214 of file BMPImage.h.

Constructor & Destructor Documentation

BmpImage::BmpImage ( kkint32  _height,
kkint32  _width,
kkint32  _numOfColors 
)

Definition at line 687 of file BMPImage.cpp.

References InitializeFields(), and KKB::KKStr::KKStr().

690  :
691 
692  color (false),
693  fileName (),
694  red (NULL),
695  image (NULL),
696  blue (NULL),
697  numOfColors (_numOfColors),
698  palette (NULL)
699 {
700  InitializeFields (_height, _width);
701 }
void InitializeFields(kkint32 _height, kkint32 _width)
Definition: BMPImage.cpp:1011
BmpImage::BmpImage ( const KKStr _fileName,
bool &  successful 
)

Constructs a BMP image from the file specified by '_fileName'.

Parameters
[in]_fileNameName of file to load BMP image from.
[out]successfulReturns true if file successful in loading.

Definition at line 449 of file BMPImage.cpp.

References KKB::KKStr::KKStr(), KKB::osFOPEN(), and KKB::KKStr::Str().

Referenced by KKB::ReadImage().

451  :
452 
453  color (false),
454  fileName (_fileName),
455  red (NULL),
456  image (NULL),
457  blue (NULL),
458  numOfColors (16),
459  palette (NULL),
460  paletteEntries (0)
461 
462 {
463  FILE* inFile = osFOPEN (fileName.Str (), "rb");
464  if (!inFile)
465  {
466  successfull = false;
467  return;
468  }
469 
470  successfull = true;
471 
472  size_t x;
473  kkint32 y;
474 
475  x = fread (&hdr, sizeof (hdr), 1, inFile);
476  if (x <= 0)
477  {
478  successfull = false;
479  fclose (inFile);
480  return;
481  }
482 
483  uchar buff[4];
484  memcpy (buff, &hdr, sizeof (buff));
485  if ((buff[0] == 'B') && (buff[1] == 'M'))
486  {
487  // We have a Bit Map file.
488  }
489  else if ((buff[0] == 137) && (buff[1] == 'P') && (buff[2] == 'N') && (buff[3] == 'G'))
490  {
491  // We are looking at a PNG (Portable Network Graphics) file.
492  cerr << std::endl
493  << "File[" << _fileName << "] is a PNG formatted file." << std::endl
494  << std::endl;
495  successfull = false;
496  fclose (inFile);
497  return;
498  }
499  else
500  {
501  cerr << std::endl
502  << "File[" << _fileName << "] is of a unknown file format." << std::endl
503  << std::endl;
504  successfull = false;
505  fclose (inFile);
506  return;
507  }
508 
509 
510  x = fread (&bmh, sizeof (bmh), 1, inFile);
511  if (x <= 0)
512  {
513  successfull = false;
514  fclose (inFile);
515  return;
516  }
517 
518  if ((bmh.biCompression == BI_RGB) && (bmh.biBitCount == 24))
519  {
520  numOfColors = 16777216;
521  color = true;
522  delete palette;
523  palette = NULL;
524  Load24BitColor (inFile, successfull);
525  fclose (inFile);
526  return;
527  }
528 
529  if ((bmh.biCompression == BI_RGB) && (bmh.biBitCount == 8))
530  {
531  Load8BitColor (inFile, successfull);
532  fclose (inFile);
533  return;
534  }
535 
536  else if ((bmh.biCompression == BI_RGB) || (bmh.biCompression == BI_RLE4))
537  numOfColors = 16;
538 
539  else if (bmh.biBitCount == 1)
540  numOfColors = 16;
541 
542  else
543  numOfColors = 256;
544 
545 
546  paletteEntries = BMIcolorArraySize (bmh);
547  if (paletteEntries < 0)
548  {
549  successfull = false;
550  fclose (inFile);
551  return;
552  }
553 
554  bool imageIsRevGrayscale = false;
555  if (paletteEntries)
556  {
557  palette = new RGBQUAD[paletteEntries];
558  x = fread (palette, sizeof (RGBQUAD), paletteEntries, inFile);
559  imageIsRevGrayscale = ReversedGrayscaleImage ();
560  }
561  else
562  {
563  numOfColors = 256;
564  paletteEntries = 256;
565  palette = new RGBQUAD[paletteEntries];
566  SetUp256BitPalette (palette);
567  }
568 
569  // Lets Build Palette Map
570  for (x = 0; x < 256; x++)
571  paletteMap[x] = 0;
572 
573  if (numOfColors == 16)
574  {
575  for (kkint32 palletIdx = 0; palletIdx < paletteEntries; palletIdx++)
576  {
577  if (imageIsRevGrayscale)
578  y = 255 - palette[palletIdx].rgbGreen;
579  else
580  y = palette[palletIdx].rgbGreen;
581  if (y < 0) y = 0; else if (y > 255) y = 255;
582  paletteMap[palletIdx] = y;
583  }
584  }
585  else
586  {
587  for (kkint32 palletIdx = 0; palletIdx < paletteEntries; palletIdx++)
588  {
589  if (imageIsRevGrayscale)
590  y = 255 - palette[palletIdx].rgbGreen;
591  else
592  y = palette[palletIdx].rgbGreen;
593  if (y < 0) y = 0; else if (y > 255) y = 255;
594  paletteMap[palletIdx] = y;
595  }
596  }
597 
598  delete palette;
599  paletteEntries = 256;
600  palette = new RGBQUAD[paletteEntries];
601  SetUp256BitPalette (palette);
602 
603  kkint32 row;
604 
605  image = new uchar*[bmh.biHeight];
606  for (row = 0; row < bmh.biHeight; row++)
607  {
608  image[row] = new uchar[bmh.biWidth];
609  memset (image[row], 0, bmh.biWidth);
610  }
611 
612  x = fseek (inFile, hdr.bfOffBits, SEEK_SET);
613 
614  if (bmh.biBitCount == 1)
615  {
616  Load1BitColor (inFile, successfull);
617  }
618 
619  else if (bmh.biBitCount == 4)
620  {
621  if (bmh.biCompression == BI_RGB)
622  {
623  Load4BitColor (inFile, successfull);
624  }
625 
626  else if (bmh.biCompression == BI_RLE4)
627  {
628  Load4BitColorCompressed (inFile, successfull);
629  }
630 
631  else
632  {
633  cerr << "***ERROR*** Invalid Compression Mode[" << bmh.biCompression
634  << "] Specified for 4 bit File["
635  << fileName << "]."
636  << std::endl;
637  successfull = false;
638  // WaitForEnter ();
639  }
640  }
641 
642  else if (bmh.biBitCount == 8)
643  {
644  if (bmh.biCompression == BI_RGB)
645  {
646  Load8BitColor (inFile, successfull);
647  }
648 
649  else if (bmh.biCompression == BI_RLE8)
650  {
651  Load8BitColorCompressed (inFile, successfull);
652  }
653 
654  else
655  {
656  cerr << "***ERROR*** Invalid Compression Mode[" << bmh.biCompression
657  << "] Specified for 8 bit File["
658  << fileName << "]."
659  << std::endl;
660  successfull = false;
661  }
662  }
663 
664  else if (bmh.biBitCount == 16)
665  {
666  cerr << "***ERROR*** 16 Bit Not Supported. File[" << fileName << "]." << std::endl;
667  // WaitForEnter ();
668  }
669 
670  else if (bmh.biBitCount == 24)
671  {
672  cerr << "***ERROR*** 24 Bit Not Supported. File[" << fileName << "]." << std::endl;
673  }
674 
675  else if (bmh.biBitCount == 32)
676  {
677  cerr << "***ERROR*** 32 Bit Not Supported. File[" << fileName << "]." << std::endl;
678  // WaitForEnter ();
679  }
680 
681  fclose (inFile);
682 }
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
FILE * osFOPEN(const char *fileName, const char *mode)
Definition: OSservices.cpp:74
BmpImage::BmpImage ( const Raster raster)

Constructs a BMPImage instance from a Raster image; this is one way to save a Raster image to disk.

Definition at line 706 of file BMPImage.cpp.

References KKB::Raster::Blue(), KKB::Raster::Color(), KKB::Raster::Height(), InitializeFields(), KKB::KKStr::operator=(), KKB::Raster::Red(), KKB::Raster::Rows(), and KKB::Raster::Width().

Referenced by KKB::SaveImage(), KKB::SaveImageGrayscaleInverted4Bit(), and KKB::SaveImageGrayscaleInverted8Bit().

706  :
707  color (false),
708  red (NULL),
709  image (NULL),
710  blue (NULL),
711  palette (NULL)
712 {
713  numOfColors = 256; // kk 2005-03-10
714 
715  color = raster.Color ();
716 
717  InitializeFields (raster.Height (), raster.Width ());
718  fileName = "";
719 
720  kkint32 row;
721  kkint32 col;
722 
723  uchar** rasterData = raster.Rows ();
724  uchar** rasterRed = raster.Red ();
725  uchar** rasterBlue = raster.Blue ();
726 
727  for (row = 0; row < bmh.biHeight; row++)
728  {
729  for (col = 0; col < bmh.biWidth; col++)
730  {
731  image[row][col] = rasterData[row][col];
732  if (color)
733  {
734  red[row][col] = rasterRed [row][col];
735  blue[row][col] = rasterBlue[row][col];
736  }
737  }
738  }
739 }
__int32 kkint32
Definition: KKBaseTypes.h:88
bool Color() const
Definition: Raster.h:310
uchar ** Red() const
Definition: Raster.h:326
kkint32 Height() const
Definition: Raster.h:319
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
kkint32 Width() const
Definition: Raster.h:324
uchar ** Blue() const
Definition: Raster.h:328
void InitializeFields(kkint32 _height, kkint32 _width)
Definition: BMPImage.cpp:1011
uchar ** Rows() const
Definition: Raster.h:321
BmpImage::~BmpImage ( )

Definition at line 743 of file BMPImage.cpp.

744 {
745  CleanUpMemory ();
746 }

Member Function Documentation

void BmpImage::AddPixel ( kkuint32  row,
kkuint32  col,
uchar  pixValue 
)

Definition at line 2630 of file BMPImage.cpp.

2634 {
2635  image[row][col] = pixValue;
2636 }
bool BmpImage::AreThereEdgePixels ( )

Definition at line 2004 of file BMPImage.cpp.

References Height(), and Width().

2005 {
2006  kkint32 row;
2007  kkint32 col;
2008 
2009  kkint32 height = Height ();
2010  kkint32 width = Width ();
2011 
2012  if (height < 6)
2013  return true;
2014 
2015  if (width < 6)
2016  return true;
2017 
2018 
2019  uchar* row0 = image[0];
2020  uchar* row1 = image[1];
2021  uchar* row2 = image[2];
2022 
2023  uchar* rowL0 = image[height - 3];
2024  uchar* rowL1 = image[height - 2];
2025  uchar* rowL2 = image[height - 1];
2026 
2027 
2028  for (col = 0; col < width; col++)
2029  {
2030  if ((row0[col] > 0) ||
2031  (row1[col] > 0) ||
2032  (row2[col] > 0) ||
2033  (rowL0[col] > 0) ||
2034  (rowL1[col] > 0) ||
2035  (rowL2[col] > 0)
2036  )
2037  return true;
2038  }
2039 
2040  kkint32 lastCol0 = width - 3;
2041  kkint32 lastCol1 = width - 2;
2042  kkint32 lastCol2 = width - 1;
2043 
2044  kkint32 lastRowToCheck = height - 3;
2045 
2046  for (row = 3; row < lastRowToCheck; row++)
2047  {
2048  if ((image[row][0] > 0) ||
2049  (image[row][1] > 0) ||
2050  (image[row][2] > 0) ||
2051  (image[row][lastCol0] > 0) ||
2052  (image[row][lastCol1] > 0) ||
2053  (image[row][lastCol2] > 0)
2054  )
2055  return true;
2056  }
2057 
2058  return false;
2059 } /* EdgePixels */
__int32 kkint32
Definition: KKBaseTypes.h:88
kkuint32 Height() const
Definition: BMPImage.h:125
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
kkuint32 Width() const
Definition: BMPImage.h:140
void BmpImage::Binarize ( )

Definition at line 2189 of file BMPImage.cpp.

2190 {
2191  kkint32 row;
2192  kkint32 col;
2193 
2194  for (row = 0; row < bmh.biHeight; row++)
2195  {
2196  for (col = 0; col < bmh.biWidth; col++)
2197  {
2198  if (image[row][col] < 7)
2199  image[row][col] = 0;
2200  }
2201  }
2202 } /* Binarize */
__int32 kkint32
Definition: KKBaseTypes.h:88
const uchar * BmpImage::BlueRow ( kkint32  row) const

Returns the specified Row from the Blue Channel.

Definition at line 2652 of file BMPImage.cpp.

References Height(), KKB::KKException::KKException(), KKB::KKStr::operator+(), KKB::operator+(), and KKB::StrFromInt32().

Referenced by KKB::Raster::Raster().

2653 {
2654  if (blue == NULL)
2655  throw KKException("BmpImage::BlueRow 'blue' channel is set to NULL.");
2656  if ((row < 0) || (row >= (kkint32)Height ()))
2657  {
2658  cerr << std::endl
2659  << std::endl
2660  << "BmpImage::BlueRow *** ERROR *** Invalid Row[" << row << "]." << std::endl
2661  << std::endl;
2662  //osWaitForEnter ();
2663  throw KKException("BmpImage::BlueRow row:" + StrFromInt32(row) + " is out of range where Height:" + StrFromInt32(row));
2664  }
2665 
2666  return blue[row];
2667 } /* BlueRow */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
kkuint32 Height() const
Definition: BMPImage.h:125
KKStr StrFromInt32(kkint32 i)
Definition: KKStr.cpp:5175
void BmpImage::ClearImage ( )

Definition at line 2640 of file BMPImage.cpp.

2641 {
2642  kkint32 row;
2643  for (row = 0; row < bmh.biHeight; row++)
2644  {
2645  memset (image[row], 0, bmh.biWidth );
2646  }
2647 }
__int32 kkint32
Definition: KKBaseTypes.h:88
bool KKB::BmpImage::Color ( ) const
inline

Returns true if a Color image.

Definition at line 118 of file BMPImage.h.

Referenced by KKB::Raster::Raster().

void KKB::BmpImage::DoubleSize ( )
void BmpImage::DownSize ( )

Definition at line 1917 of file BMPImage.cpp.

1918 {
1919  kkint32 newCol;
1920  kkint32 newHeight = bmh.biHeight / 2;
1921  kkint32 newWidth = bmh.biWidth / 2;
1922  kkint32 newRow;
1923 
1924  kkint32 oldCol;
1925  kkint32 oldRow;
1926 
1927  uchar** newImage = new uchar*[newHeight];
1928  for (newRow = 0; newRow < newHeight; newRow++)
1929  {
1930  oldRow = newRow * 2;
1931 
1932  newImage[newRow] = new uchar[newWidth];
1933 
1934  for (newCol = 0; newCol < newWidth; newCol++)
1935  {
1936  oldCol = newCol * 2;
1937  newImage[newRow][newCol] = image[oldRow][oldCol];
1938  }
1939  }
1940 
1941  for (oldRow = 0; oldRow < bmh.biHeight; oldRow++)
1942  {
1943  delete image[oldRow];
1944  image[oldRow] = NULL;
1945  }
1946 
1947  delete image;
1948 
1949  image = newImage;
1950 
1951  bmh.biHeight = newHeight;
1952  bmh.biWidth = newWidth;
1953 } /* DownSize */
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
void BmpImage::EliminateVerticalLines ( )

Definition at line 2063 of file BMPImage.cpp.

2064 {
2065  kkint32 col = 0;
2066  kkint32 row = 0;
2067  kkint32 x;
2068 
2069 
2070  bool* potVertLine = new bool[bmh.biWidth];
2071  bool* pvl = potVertLine;
2072 
2073  for (col = 0; col < bmh.biWidth; col++)
2074  {
2075  *pvl = true;
2076  for (row = 0; ((row < bmh.biHeight) && (*pvl)); row++)
2077  {
2078  if (image[row][col] == 7)
2079  *pvl = false;
2080  }
2081 
2082  pvl++;
2083  }
2084 
2085 
2086  col = 0;
2087 
2088  while (col < bmh.biWidth)
2089  {
2090  if (potVertLine[col])
2091  {
2092  kkint32 firstCol = col;
2093  kkint32 lastCol = col;
2094 
2095  if (col < (bmh.biWidth - 1))
2096  {
2097  while ((col < bmh.biWidth) && (potVertLine[col]))
2098  {
2099  col++;
2100  }
2101  }
2102 
2103  if (!potVertLine[col])
2104  {
2105  // We terminated loop because there were no more potentialVertLine's not
2106  // because we ran out of columns.
2107  lastCol = col - 1;
2108  }
2109 
2110  // Scan down vertically, and any place that we are not in contact both left and right
2111  // with other pixels erase from picture,
2112 
2113 
2114  bool leftSideIsClear = true;
2115  bool rightSizeIsClear = true;
2116 
2117  uchar leftPixel = 0;
2118  uchar rightPixel = 0;
2119 
2120  for (row = 0; row < bmh.biHeight; row++)
2121  {
2122  if (firstCol > 0)
2123  {
2124  leftPixel = image[row][firstCol - 1];
2125  if (leftPixel != 0)
2126  leftSideIsClear = false;
2127  }
2128  else
2129  {
2130  leftPixel = 0;
2131  }
2132 
2133  if (lastCol < (bmh.biWidth - 1))
2134  {
2135  rightPixel = image[row][lastCol + 1];
2136 
2137  if (rightPixel != 0)
2138  rightSizeIsClear = false;
2139  }
2140 
2141  if (leftSideIsClear && rightSizeIsClear)
2142  {
2143  for (x = firstCol; x <= lastCol; x++)
2144  image[row][x] = 0;
2145  }
2146  else
2147  {
2148  for (x = firstCol; x <= lastCol; x++)
2149  image[row][x] = (leftPixel + rightPixel) / 2;
2150 
2151  // Set up boolean for next loop around.
2152  leftSideIsClear = true;
2153  rightSizeIsClear = true;
2154  }
2155  }
2156  }
2157 
2158  col++;
2159  }
2160 
2161  delete[] potVertLine;
2162 } /* EliminateVerticalLines */
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
const KKStr& KKB::BmpImage::FileName ( ) const
inline

Definition at line 121 of file BMPImage.h.

Referenced by KKB::Raster::Raster().

121 {return fileName;}
bool BmpImage::FourBitUncompressed ( )

Definition at line 2709 of file BMPImage.cpp.

2710 {
2711  return ((bmh.biBitCount == 4) && (bmh.biCompression == BI_RGB));
2712 }
kkuint32 KKB::BmpImage::Height ( ) const
inline

Definition at line 125 of file BMPImage.h.

Referenced by AreThereEdgePixels(), BlueRow(), KKB::Raster::CreatePaddedRaster(), ImageRow(), KKB::Raster::Raster(), and RedRow().

125 {return bmh.biHeight;}
uchar** KKB::BmpImage::Image ( )
inline

Returns back two dimension matrix of image; if color it will be the green channel.

Definition at line 130 of file BMPImage.h.

const uchar * BmpImage::ImageRow ( kkint32  row) const

Returns the specified Row from the Green Channel.

Definition at line 2672 of file BMPImage.cpp.

References Height(), KKB::KKException::KKException(), KKB::KKStr::operator+(), KKB::operator+(), and KKB::StrFromInt32().

Referenced by KKB::Raster::CreatePaddedRaster(), and KKB::Raster::Raster().

2673 {
2674  if (image == NULL)
2675  throw KKException("::ImageRow 'image' set to NULL.");
2676  if ((row < 0) || (row >= (kkint32)Height ()))
2677  {
2678  cerr << std::endl
2679  << std::endl
2680  << "BmpImage *** ERROR *** Invalid Row[" << row << "]." << std::endl
2681  << std::endl;
2682  throw KKException("BmpImage::ImageRow row:" + StrFromInt32(row) + " is out of range where Height:" + StrFromInt32(row));
2683  }
2684 
2685  return image[row];
2686 } /* ImageRow */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
kkuint32 Height() const
Definition: BMPImage.h:125
KKStr StrFromInt32(kkint32 i)
Definition: KKStr.cpp:5175
void BmpImage::InitializeFields ( kkint32  _height,
kkint32  _width 
)

Definition at line 1011 of file BMPImage.cpp.

Referenced by BmpImage().

1014 {
1015  hdr.bfType = 19778;
1016  hdr.bfReserved1 = 0;
1017  hdr.bfReserved2 = 0;
1018  // hdr.bfOffBits = sizeof (bmh)
1019 
1020 
1021  bmh.biSize = sizeof (bmh);
1022  bmh.biWidth = _width;
1023  bmh.biHeight = _height;
1024  bmh.biPlanes = 1;
1025 
1026  paletteEntries = 0;
1027 
1028  AllocateRaster ();
1029 
1030  if (numOfColors <= 16)
1031  {
1032  delete palette; palette = NULL;
1033  bmh.biBitCount = 4;
1034  paletteEntries = 16;
1035  bmh.biCompression = BI_RLE4; // BI_RGB;
1036  palette = new RGBQUAD[paletteEntries];
1037 
1038  SetUp16BitPallet (palette);
1039  }
1040  else
1041  {
1042  delete palette; palette = NULL;
1043  bmh.biBitCount = 8;
1044  paletteEntries = 256;
1045  bmh.biCompression = BI_RLE8; // BI_RGB;
1046  palette = new RGBQUAD[paletteEntries];
1047  SetUp256BitPalette (palette);
1048  }
1049 
1050  bmh.biSizeImage = 0;
1051  bmh.biXPelsPerMeter = 2835;
1052  bmh.biYPelsPerMeter = 2835;
1053  bmh.biClrUsed = 0;
1054  bmh.biClrImportant = 0;
1055 
1056  // BITMAPFILEHEADER hdr;
1057  // BITMAPINFOHEADER bmh;
1058 
1059  hdr.bfOffBits = sizeof (bmh) + sizeof (RGBQUAD) * paletteEntries;
1060 } /* InitializeFields */
uchar KKB::BmpImage::MaxPixVal ( ) const
inline

Definition at line 138 of file BMPImage.h.

138 {return uchar (maxPixVal);}
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
uchar & BmpImage::Pixel ( kkint32  row,
kkint32  col 
)

Definition at line 1958 of file BMPImage.cpp.

1960 {
1961  return image[row][col];
1962 }
void BmpImage::Print ( )

Definition at line 2168 of file BMPImage.cpp.

References KKB::operator<<().

2169 {
2170  kkint32 x;
2171  kkint32 y;
2172 
2173  for (x = 0; x < bmh.biHeight; x++)
2174  {
2175  for (y = 0; y < Min ((long)bmh.biWidth, 76L); y++)
2176  {
2177  if (image[x][y])
2178  cout << " ";
2179  else
2180  cout << "*";
2181  }
2182 
2183  cout << std::endl;
2184  }
2185 } /* Print */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
T Min(T a, T b)
Generic Min function, Both parameters must be of the same type.
Definition: KKBaseTypes.h:168
void BmpImage::ReAllocateForBiggerScreen ( )

Used to expand dimensions of image by 6 pixels so as to make sure that no image is along the edge.

Definition at line 1318 of file BMPImage.cpp.

1319 {
1320  kkint32 oldRow;
1321 
1322  kkint32 newRow;
1323 
1324  kkint32 newHeight = bmh.biHeight + 6;
1325  kkint32 newWidth = bmh.biWidth + 6;
1326 
1327 
1328  uchar** newImage = new uchar*[newHeight];
1329  uchar** newRed = NULL;
1330  uchar** newBlue = NULL;
1331  if (color)
1332  {
1333  newRed = new uchar*[newHeight];
1334  newBlue = new uchar*[newHeight];
1335  }
1336 
1337  for (newRow = 0; newRow < newHeight; newRow++)
1338  {
1339  newImage[newRow] = new uchar[newWidth];
1340  memset (newImage[newRow], 0, newWidth);
1341  if (color)
1342  {
1343  newRed [newRow] = new uchar[newWidth];
1344  newBlue[newRow] = new uchar[newWidth];
1345  memset (newRed [newRow], 0, newWidth);
1346  memset (newBlue [newRow], 0, newWidth);
1347  }
1348  }
1349 
1350  newRow = 3;
1351  for (oldRow = 0; oldRow < bmh.biHeight; oldRow++)
1352  {
1353  memcpy ((newImage[newRow] + 3), image[oldRow], bmh.biWidth);
1354  if (color)
1355  {
1356  memcpy ((newRed[newRow] + 3), red [oldRow], bmh.biWidth);
1357  memcpy ((newBlue[newRow] + 3), blue[oldRow], bmh.biWidth);
1358  }
1359  newRow++;
1360  }
1361 
1362  for (oldRow = 0; oldRow < bmh.biHeight; oldRow++)
1363  {
1364  delete image[oldRow];
1365  image[oldRow] = NULL;
1366  if (color)
1367  {
1368  delete red [oldRow]; red [oldRow] = NULL;
1369  delete blue[oldRow]; blue[oldRow] = NULL;
1370  }
1371  }
1372  delete image;
1373  delete red;
1374  delete blue;
1375 
1376  image = newImage;
1377  if (color)
1378  {
1379  red = newRed; newRed = NULL;
1380  blue = newBlue; newBlue = NULL;
1381  }
1382  bmh.biHeight = newHeight;
1383  bmh.biWidth = newWidth;
1384 } /* RealocateForBiggerScreen */
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
const uchar * BmpImage::RedRow ( kkint32  row) const

Returns the specified Row from the Red Channel.

Definition at line 2690 of file BMPImage.cpp.

References Height(), KKB::KKException::KKException(), KKB::KKStr::operator+(), KKB::operator+(), and KKB::StrFromInt32().

Referenced by KKB::Raster::Raster().

2691 {
2692  if (blue == NULL)
2693  throw KKException("BmpImage::RedRow 'red' channel is set to NULL.");
2694  if ((row < 0) || (row >= (kkint32)Height()))
2695  {
2696  cerr << std::endl
2697  << std::endl
2698  << "BmpImage::RedRow *** ERROR *** Invalid Row[" << row << "]." << std::endl
2699  << std::endl;
2700  //osWaitForEnter ();
2701  throw KKException("BmpImage::BlueRed row:" + StrFromInt32(row) + " is out of range where Height:" + StrFromInt32(row));
2702  }
2703 
2704  return red[row];
2705 } /* RedRow */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
kkuint32 Height() const
Definition: BMPImage.h:125
KKStr StrFromInt32(kkint32 i)
Definition: KKStr.cpp:5175
void BmpImage::Save ( const KKStr fileName)

Definition at line 2331 of file BMPImage.cpp.

References KKB::KKStr::Concat(), KKB::KKException::KKException(), KKB::KKStr::operator+(), KKB::operator+(), KKB::KKStr::operator=(), KKB::osFOPEN(), and KKB::KKStr::Str().

Referenced by KKB::SaveImage().

2332 {
2333  fileName = _fileName;
2334  FILE* outFile = osFOPEN (fileName.Str (), "wb");
2335  if (!outFile)
2336  {
2337  KKStr errMsg = "BmpImage::Save, Error opening BMP File[" + fileName + "].";
2338  cerr << errMsg << std::endl;
2339  throw KKException (errMsg);
2340  }
2341 
2342  if (color)
2343  {
2344  SaveColor (outFile);
2345  }
2346  else
2347  {
2348  SaveGrayScale (outFile);
2349  }
2350 
2351  fclose (outFile);
2352 } /* Save */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
FILE * osFOPEN(const char *fileName, const char *mode)
Definition: OSservices.cpp:74
void BmpImage::SaveGrayscaleInverted4Bit ( const KKStr _fileName)

Saves image using 4 bit compressed gray-scale where Background = 255 and foreground = 0.

If image is color will convert to gray-scale 1st. Palette will be set to 0 = 255, 1 = 238, 2 = 221, 3 = 204... 255 = 0.

Definition at line 2207 of file BMPImage.cpp.

References KKB::BmpImage::CodedPixels::AddPixel(), KKB::KKStr::Concat(), KKB::BmpImage::CodedPixels::CreatePixelDataStructure4Bit(), KKB::BmpImage::CodedPixels::EOL(), KKB::KKException::KKException(), KKB::KKStr::operator+(), KKB::operator+(), KKB::KKStr::operator=(), KKB::osFOPEN(), and KKB::KKStr::Str().

Referenced by KKB::SaveImageGrayscaleInverted4Bit().

2208 {
2209  fileName = _fileName;
2210  FILE* outFile = osFOPEN (fileName.Str (), "wb");
2211  if (!outFile)
2212  {
2213  KKStr errMsg = "BmpImage::SaveGrayscaleInverted4Bit, Error opening BMP File[" + fileName + "].";
2214  cerr << errMsg << std::endl;
2215  throw KKException (errMsg);
2216  }
2217 
2218  kkint32 x = 0;
2219  kkint32 y = 0;
2220 
2221  CodedPixels pixelData (bmh.biHeight, bmh.biWidth);
2222 
2223  for (x = (kkint32)bmh.biHeight - 1; x >= 0; x--)
2224  {
2225  for (y = 0; y < bmh.biWidth; y++)
2226  {
2227  if (color)
2228  {
2229  kkint32 gsVal = (kkint32)((float)(0.5f + red[x][y]) * 0.30f + (float)(image[x][y]) * 0.59f + (float)(blue[x][y]) * 0.11f);
2230  pixelData.AddPixel ((uchar)(gsVal >> 4));
2231  }
2232  else
2233  {
2234  pixelData.AddPixel (image[x][y] >> 4);
2235  }
2236  }
2237 
2238  pixelData.EOL ();
2239  }
2240 
2241  kkint32 imageBuffLen = 0;
2242  uchar* imageBuff = NULL;
2243 
2244  numOfColors = 16;
2245  imageBuff = pixelData.CreatePixelDataStructure4Bit (imageBuffLen);
2246  bmh.biBitCount = 4;
2247  bmh.biCompression = BI_RLE4; // BI_RGB;
2248  SetUp4BitPallet ();
2249 
2250  bmh.biSizeImage = imageBuffLen;
2251  bmh.biClrUsed = numOfColors;
2252  bmh.biClrImportant = numOfColors;
2253 
2254  hdr.bfSize = 14 + 40 + paletteEntries * 4 + bmh.biSizeImage;
2255  hdr.bfOffBits = 40 + 14 + paletteEntries * 4;
2256 
2257  x = (kkint32)fwrite (&hdr, sizeof (hdr), 1, outFile);
2258  x = (kkint32)fwrite (&bmh, sizeof (bmh), 1, outFile);
2259  x = (kkint32)fwrite (palette, sizeof (RGBQUAD), paletteEntries, outFile);
2260  x = (kkint32)fwrite (imageBuff, 1, imageBuffLen, outFile);
2261 
2262  delete imageBuff;
2263  fclose (outFile);
2264 } /* SaveGrayscaleInverted4Bit */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
FILE * osFOPEN(const char *fileName, const char *mode)
Definition: OSservices.cpp:74
void BmpImage::SaveGrayscaleInverted8Bit ( const KKStr _fileName)

Saves image using compressed gray-scale where Background = 255 and foreground = 0.

If image is color will convert to gray-scale 1st. Palette will be set to 0 = 255, 1 = 254, 2 = 253, ... 255 = 0.

Definition at line 2268 of file BMPImage.cpp.

References KKB::BmpImage::CodedPixels::AddPixel(), KKB::KKStr::Concat(), KKB::BmpImage::CodedPixels::CreatePixelDataStructure8Bit(), KKB::BmpImage::CodedPixels::EOL(), KKB::KKException::KKException(), KKB::KKStr::operator+(), KKB::operator+(), KKB::KKStr::operator=(), KKB::osFOPEN(), and KKB::KKStr::Str().

Referenced by KKB::SaveImageGrayscaleInverted8Bit().

2269 {
2270  fileName = _fileName;
2271  FILE* outFile = osFOPEN (fileName.Str (), "wb");
2272  if (!outFile)
2273  {
2274  KKStr errMsg = "BmpImage::SaveGrayscaleInverted8Bit, Error opening BMP File[" + fileName + "].";
2275  cerr << errMsg << std::endl;
2276  throw KKException (errMsg);
2277  }
2278 
2279  kkint32 x = 0;
2280  kkint32 y = 0;
2281 
2282  CodedPixels pixelData (bmh.biHeight, bmh.biWidth);
2283 
2284  for (x = (kkint32)bmh.biHeight - 1; x >= 0; x--)
2285  {
2286  for (y = 0; y < bmh.biWidth; y++)
2287  {
2288  if (color)
2289  {
2290  kkint32 gsVal = (kkint32)((float)(0.5f + red[x][y]) * 0.30f + (float)(image[x][y]) * 0.59f + (float)(blue[x][y]) * 0.11f);
2291  pixelData.AddPixel ((uchar)(gsVal));
2292  }
2293  else
2294  {
2295  pixelData.AddPixel (image[x][y]);
2296  }
2297  }
2298 
2299  pixelData.EOL ();
2300  }
2301 
2302  kkint32 imageBuffLen = 0;
2303  uchar* imageBuff = NULL;
2304 
2305  numOfColors = 256;
2306  imageBuff = pixelData.CreatePixelDataStructure8Bit (imageBuffLen);
2307  bmh.biBitCount = 8;
2308  bmh.biCompression = BI_RLE8; // BI_RGB;
2309  SetUp8BitPallet ();
2310 
2311  bmh.biSizeImage = imageBuffLen;
2312  bmh.biClrUsed = numOfColors;
2313  bmh.biClrImportant = numOfColors;
2314 
2315  hdr.bfSize = 14 + 40 + paletteEntries * 4 + bmh.biSizeImage;
2316  hdr.bfOffBits = 40 + 14 + paletteEntries * 4;
2317 
2318  x = (kkint32)fwrite (&hdr, sizeof (hdr), 1, outFile);
2319  x = (kkint32)fwrite (&bmh, sizeof (bmh), 1, outFile);
2320  x = (kkint32)fwrite (palette, sizeof (RGBQUAD), paletteEntries, outFile);
2321  x = (kkint32)fwrite (imageBuff, 1, imageBuffLen, outFile);
2322 
2323  delete imageBuff;
2324  fclose (outFile);
2325 } /* SaveGrayscaleInverted8Bit */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
__int32 kkint32
Definition: KKBaseTypes.h:88
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
FILE * osFOPEN(const char *fileName, const char *mode)
Definition: OSservices.cpp:74
void BmpImage::Set16Colors ( )

Definition at line 1194 of file BMPImage.cpp.

1195 {
1196  if (palette)
1197  delete palette;
1198 
1199  numOfColors = 16;
1200  paletteEntries = 16;
1201  palette = new RGBQUAD[paletteEntries];
1202  SetUp16BitPallet (palette);
1203 }
void BmpImage::Set256Colors ( )

Definition at line 1208 of file BMPImage.cpp.

1209 {
1210  if (palette)
1211  delete palette;
1212 
1213  numOfColors = 256;
1214  paletteEntries = 256;
1215  palette = new RGBQUAD[paletteEntries];
1216  SetUp256BitPalette (palette);
1217 }
void BmpImage::SetPaletteEntry ( kkint32  palletIndex,
const PixelValue pixValue 
)

Definition at line 935 of file BMPImage.cpp.

938 {
939  if ((palletIndex < 0) || (palletIndex > 255))
940  {
941  // Invalid Entry specified
942  cerr << std::endl << std::endl << std::endl
943  << "BmpImage::SetPaletteEntry Invalid PalletIndex[" << palletIndex << "]" << std::endl
944  << std::endl;
945 
946  return;
947  }
948 
949  if (!palette)
950  {
951  cerr << std::endl << std::endl << std::endl
952  << "BmpImage::SetPaletteEntry The palette is not defined!" << std::endl
953  << std::endl;
954 
955  return;
956  }
957 
958  palette[palletIndex].rgbBlue = pixValue.b;
959  palette[palletIndex].rgbGreen = pixValue.g;
960  palette[palletIndex].rgbRed = pixValue.r;
961 } /* SetPaletteEntry */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
void BmpImage::SetPixelValue ( kkint32  row,
kkint32  col,
kkint32  pixel 
)

Will set the pixel value of the specified row and col to 'pixel'.

Definition at line 1967 of file BMPImage.cpp.

1971 {
1972  if (pixel < 0)
1973  {
1974  cerr << "BmpImage::SetPixelValue ***ERROR*** pixel[" << pixel << "] out of range. Needs to be in range of 0..255." << std::endl;
1975  pixel = 0;
1976  }
1977  else if (pixel > 255)
1978  {
1979  cerr << "BmpImage::SetPixelValue ***ERROR*** pixel[" << pixel << "] out of range. Needs to be in range of 0..255." << std::endl;
1980  pixel = 255;
1981  }
1982 
1983  if ((row < 0) || (row >= bmh.biHeight))
1984  {
1985  cerr << "BmpImage::SetPixelValue *** Error ***, Row[" << row
1986  << "] out of range[0-" << bmh.biHeight << "]."
1987  << std::endl;
1988  return;
1989  }
1990 
1991  if ((col < 0) || (col >= bmh.biWidth))
1992  {
1993  cerr << "BmpImage::SetPixelValue *** Error ***, Col[" << col
1994  << "] out of range[0-" << bmh.biWidth << "]."
1995  << std::endl;
1996  return;
1997  }
1998  image[row][col] = (uchar)pixel;
1999 } /* SetPixelValue */
HTMLReport &__cdecl endl(HTMLReport &htmlReport)
Definition: HTMLReport.cpp:240
unsigned char uchar
Unsigned character.
Definition: KKBaseTypes.h:77
kkuint32 KKB::BmpImage::Width ( ) const
inline

Definition at line 140 of file BMPImage.h.

Referenced by AreThereEdgePixels(), KKB::Raster::CreatePaddedRaster(), and KKB::Raster::Raster().

140 {return bmh.biWidth;}

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