home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Library / include / bitimage.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-17  |  9.4 KB  |  316 lines

  1. #ifndef __BITIMAGE_H
  2. #define __BITIMAGE_H
  3.  
  4. //
  5. // Copyright (c) 1997,1998 Colosseum Builders, Inc.
  6. // All rights reserved.
  7. //
  8. // Colosseum Builders, Inc. makes no warranty, expressed or implied
  9. // with regards to this software. It is provided as is.
  10. //
  11. // See the README.TXT file that came with this software for restrictions
  12. // on the use and redistribution of this file or send E-mail to
  13. // info@colosseumbuilders.com
  14. //
  15.  
  16. //
  17. //  Title: BitmapImage Class Definitions
  18. //
  19. //  Author:  John M. Miano  miano@colosseumbuilders.com
  20. //
  21. //    The BitmapImage class is intended to be a neutral intermediate format
  22. //    for storing decompressed images. This class can manage 1, 2, 4, 8 or
  23. //    24-bit images. For 24-bit images the data is stored as RGB triples
  24. //    within the main data buffer. For all other types a color map is used
  25. //    and the image data contains indices into the color map. Sample values are
  26. //    assumed to be in the range 0..255.
  27. //
  28. //  Windows Notes:
  29. //
  30. //    For the sake of "efficiency" this class has been optimized for use on
  31. //    the "Windows" family. The folling oddities are a result of
  32. //    "windowsisms":
  33. //
  34. //    o The data for 24-bitmaps is stored in BGR order rather than RGB.
  35. //      To change this for your system redefine "RedOffset", "GreenOffset",
  36. //      and "BlueOffset".
  37. //
  38. //    o For whatever reason, Windows expects bitmaps to be stored bottom
  39. //      up rather than top down. The first row in the bitmap data is the
  40. //      bottom row in the image. To change behavoir this for your system
  41. //      redefine the implementation of the [] operator.
  42. //
  43. //    o Windows expects the length of all image rows to be rounded up to the
  44. //      nearest four bytes. To change this behavior redefine the value for
  45. //     "RowRounding".
  46. //
  47. //  Debugging Notes:
  48. //
  49. //    Two methods for accessing pixel data within the image are implemented
  50. //    by default range checking is only performed on rows. If the
  51. //    preprocessor symbol CHECK_RANGE is defined then range check is
  52. //    performed on columns as well.
  53. //
  54. //    While the abandonment of range checking here is contrary to the
  55. //    principles followed elsewhere, this is a place where the
  56. //    performance benefit is worth the lack of safety.
  57. //
  58.  
  59. #include <iostream>
  60. #include "datatype.h"
  61.  
  62. class BitmapImage ;
  63. class BitmapImageCoder ;
  64.  
  65.  
  66. typedef void (*PROGRESSFUNCTION)(BitmapImageCoder &coder,
  67.                                  void *data,
  68.                                  unsigned int currentpass,
  69.                                  unsigned int passcount,
  70.                                  unsigned int progress,
  71.                                  bool &cancel) ;
  72.  
  73. typedef void (*IMAGEPROGRESSFUNCTION)(BitmapImage &image,
  74.                                       void *data,
  75.                                       unsigned int currentpass,
  76.                                       unsigned int passcount,
  77.                                       unsigned int progress,
  78.                                       bool &cancel) ;
  79.  
  80. class BitmapImageCoder
  81. {
  82. public:
  83.   BitmapImageCoder () : progress_function (NULL), progress_data (NULL) {}
  84.   BitmapImageCoder (const BitmapImageCoder &source) ;
  85.   virtual ~BitmapImageCoder () {}
  86.   BitmapImageCoder &operator=(const BitmapImageCoder &source) ;
  87.  
  88.   void SetProgressFunction (PROGRESSFUNCTION, void *) ;
  89.   // Function to force an update of image data.
  90.   virtual void UpdateImage () {}
  91. protected:
  92.   void Initialize () ;
  93.   void DoCopy (const BitmapImageCoder &source) ;
  94.   PROGRESSFUNCTION progress_function ;
  95.   void *progress_data ;
  96. } ;
  97.  
  98. inline BitmapImageCoder::BitmapImageCoder (const BitmapImageCoder &source)
  99. {
  100.   DoCopy (source) ;
  101.   return ;
  102. }
  103.  
  104. inline BitmapImageCoder &BitmapImageCoder::operator=(
  105.                                 const BitmapImageCoder &source)
  106. {
  107.   DoCopy (source) ;
  108.   return *this ;
  109. }
  110.  
  111. inline void BitmapImageCoder::DoCopy (const BitmapImageCoder &source)
  112. {
  113.   progress_function = source.progress_function ;
  114.   progress_data = source.progress_data ;
  115.   return ;
  116. }
  117.  
  118. inline void BitmapImageCoder::SetProgressFunction (PROGRESSFUNCTION func,
  119.                                                    void *data)
  120. {
  121.   progress_function = func ;
  122.   progress_data = data ;
  123.   return ;
  124. }
  125.  
  126. class BitmapImageDecoder : public BitmapImageCoder
  127. {
  128. public:
  129.   virtual ~BitmapImageDecoder () {}
  130.   virtual void ReadImage (std::istream &, BitmapImage &) = 0 ;
  131. } ;
  132.  
  133. class BitmapImageEncoder : public BitmapImageCoder
  134. {
  135. public:
  136.   virtual ~BitmapImageEncoder () {}
  137.   virtual void WriteImage (std::ostream &, BitmapImage &) = 0 ;
  138. } ;
  139.  
  140. class BitmapImage
  141. {
  142. public:
  143. #if defined (CHECK_RANGE)
  144.   // The Row class is used to implement range checking on columns. A Row
  145.   // object represents a row if image data.
  146.   class Row
  147.   {
  148.   public:
  149.     UBYTE1 &operator[](unsigned int) ;
  150.   private:
  151.     Row (UBYTE1 *data, unsigned int length) ;
  152.     UBYTE1 *row_data ;
  153.     unsigned int row_length ;
  154.     friend class BitmapImage ;
  155.   } ;
  156. #endif
  157.  
  158.   // Definition of the color map used by bitmaps of other than 24-bits.
  159.   struct ColorMapEntry
  160.   {
  161.     UBYTE1 blue ;
  162.     UBYTE1 green ;
  163.     UBYTE1 red ;
  164.   } ;
  165.  
  166.   enum { RedOffset=2, GreenOffset=1, BlueOffset=0 } ;
  167.  
  168.   // Required Member Functions
  169.   BitmapImage () ;
  170.   BitmapImage (const BitmapImage &) ;
  171.   virtual ~BitmapImage () ;
  172.   BitmapImage &operator=(const BitmapImage &) ;
  173.  
  174.   // Function to allocate space for to store an image.
  175.   void SetSize (unsigned int cc,    // Color Count
  176.                 unsigned int bits,  // Bit Count
  177.                 unsigned int ww,    // Width
  178.                 unsigned int hh) ;  // Height
  179.  
  180.   // Function to retrieve entries in the color map.
  181.   ColorMapEntry &ColorMap (unsigned int index) ;
  182.   ColorMapEntry ColorMap (unsigned int index) const ;
  183.  
  184.   // [] returns image data bytes.
  185. #if defined (CHECK_RANGE)
  186.   Row operator[](unsigned int) const ;
  187. #else
  188.   UBYTE1 *operator[](unsigned int) const ;
  189. #endif
  190.   // Function to reset the image to empty
  191.   void Clear () ;
  192.  
  193.   // Function to return information about the image.
  194.   UBYTE1 *ImageData() ;
  195.   unsigned int Width () const ;
  196.   unsigned int Height () const ;
  197.   unsigned int BitCount () const ;
  198.   unsigned int ColorCount () const ;
  199.   void GetRGB (unsigned int row, unsigned int col,
  200.                UBYTE1 &red, UBYTE1 &green, UBYTE1 &blue) const ;
  201.  
  202.   void EightBitQuantization (const BitmapImage &) ;
  203.  
  204.   void SetProgressFunction (IMAGEPROGRESSFUNCTION, void *) ;
  205.  
  206.   // Number of bytes to round each row to. On Windows this should be 4.
  207.   enum { RowRounding = 4 } ;
  208.  
  209.   unsigned int BytesPerRow () const ;
  210. protected:
  211.   void Initialize () ;
  212.   void DoCopy (const BitmapImage &) ;
  213.  
  214. private:
  215.   // Width in bytes of each row. For 24-bit images this value will
  216.   // always be larger than the width of the image. For 8-bit images
  217.   // it may be larger if the row width is rounded up.
  218.   unsigned int row_width ;
  219.   unsigned int bit_count ;     // Number of bits (1, 2, 4, 8, or 24)
  220.   unsigned int image_width ;
  221.   unsigned int image_height ;
  222.   // Image data arranged left to right, top to bottom. For 24-bit images
  223.   // there are 3 bytes per pixel representing the color to display in RGB order.
  224.   // For all others the data is in index into the color map. 1, 2, and 4-bit
  225.   // image row widths are rounded up to the nearest byte.
  226.   unsigned char *image_data ;
  227.  
  228.   unsigned int color_count ;   // Number of entries in the color map.
  229.   ColorMapEntry *color_map ;   // Color map for 8-bit image
  230.  
  231.   // Interal function to state class variables to a known state.
  232.   void ClearData () ;
  233.  
  234.   struct ColorUsage
  235.   {
  236.     UBYTE1 colors [3] ;
  237.     UBYTE4 usage ;
  238.     ColorUsage *next [3] ;
  239.   } ;
  240.  
  241.   struct ColorUsageTable
  242.   {
  243.     ColorUsage *lists [256][3] ;
  244.     unsigned int color_count ;
  245.   } ;
  246.  
  247.   struct ColorArea
  248.   {
  249.     struct
  250.     {
  251.       UBYTE1 low ;
  252.       UBYTE1 high ;
  253.     } color_values [3] ;
  254.     unsigned int color_count ;
  255.     unsigned int pixel_count ;
  256.   }  ;
  257.  
  258.   int LargestColorRange (ColorArea &area) ;
  259.   void AddColor (UBYTE1 red, UBYTE1 green, UBYTE1 blue) ;
  260.   void SplitAreaInHalf (unsigned int depth, unsigned int retrydepth, unsigned int area, unsigned int splitcolor) ;
  261.   void CreateColor (unsigned int color) ;
  262.   ColorUsage *FindColor (UBYTE1 red, UBYTE1 green, UBYTE1 blue) ;
  263.   void FindColorUsage (const BitmapImage &image) ;
  264.   void FreeColorQuantizationData () ;
  265.   unsigned int QuantizedColor (UBYTE1 red, UBYTE1 green, UBYTE1 blue) ;
  266.   void QuantizeSourceImage (const BitmapImage &image) ;
  267.  
  268.   ColorUsageTable *color_usage ;
  269.   ColorArea *color_areas ;
  270.   unsigned int color_area_count ;
  271.  
  272.   void CallProgressFunction (unsigned int, unsigned int, unsigned int) ;
  273.   IMAGEPROGRESSFUNCTION progress_function ;
  274.   void *progress_data ;
  275. } ;
  276.  
  277. inline UBYTE1 *BitmapImage::ImageData()
  278. {
  279.   return image_data ;
  280. }
  281.  
  282. inline unsigned int BitmapImage::Width () const
  283. {
  284.   return image_width ;
  285. }
  286.  
  287. inline unsigned int BitmapImage::Height () const
  288. {
  289.  return image_height ;
  290. }
  291.  
  292. inline unsigned int BitmapImage::BitCount () const
  293. {
  294.   return bit_count ;
  295. }
  296.  
  297. inline unsigned int BitmapImage::ColorCount () const
  298. {
  299.   return color_count ;
  300. }
  301.  
  302. #if ! defined (CHECK_RANGE)
  303. inline UBYTE1 *BitmapImage::operator[](unsigned int xx)const
  304. {
  305.   return &image_data [(image_height - xx - 1) * row_width] ;
  306. }
  307. #endif
  308.  
  309. inline unsigned int BitmapImage::BytesPerRow () const
  310. {
  311.   return row_width ;
  312. }
  313.  
  314.  
  315. #endif
  316.