home *** CD-ROM | disk | FTP | other *** search
/ Compressed Image File Formats / CompressedImageFileFormatsJohnMiano.iso / pc / Library / source / xbmencod.cpp < prev   
Encoding:
C/C++ Source or Header  |  1999-04-07  |  5.3 KB  |  256 lines

  1. //
  2. // Copyright (c) 1997,1998 Colosseum Builders, Inc.
  3. // All rights reserved.
  4. //
  5. // Colosseum Builders, Inc. makes no warranty, expressed or implied
  6. // with regards to this software. It is provided as is.
  7. //
  8. // See the README.TXT file that came with this software for restrictions
  9. // on the use and redistribution of this file or send E-mail to
  10. // info@colosseumbuilders.com
  11. //
  12.  
  13. //
  14. //  Title:  XBM Encoder Class Definition
  15. //
  16. //  Author:  John M. Miano  miano@colosseumbuilers.com
  17. //
  18.  
  19. #include <iomanip>
  20. #include <string>
  21. #include <ctype.h>
  22.  
  23. #include "xbmencod.h"
  24. #include "xbmpvt.h"
  25.  
  26. using namespace std ;
  27. // This constant defines the threshold for setting a value to black in the
  28. // output. You may wish to make this run-time configurable.
  29. const int THRESHOLD = (3*(UCHAR_MAX+1))/2 ;
  30.  
  31. //
  32. //  Description:
  33. //
  34. //    Class default constructor
  35. //
  36. XbmEncoder::XbmEncoder ()
  37. {
  38.   Initialize () ;
  39.   return ;
  40. }
  41.  
  42. //
  43. //  Description:
  44. //
  45. //    Class copy constructor
  46. //
  47. XbmEncoder::XbmEncoder (const XbmEncoder &source)
  48. {
  49.   Initialize () ;
  50.   DoCopy (source) ;
  51.   return ;
  52. }
  53.  
  54. //
  55. //  Descripton:
  56. //
  57. //    Class Default Constructor
  58. //
  59. XbmEncoder::~XbmEncoder ()
  60. {
  61.   return ;
  62. }
  63.  
  64. //
  65. //  Description:
  66. //
  67. //    Class initialization function.
  68. //
  69. void XbmEncoder::Initialize ()
  70. {
  71.   image_name = "" ;
  72.   hot_spot_x = -1 ;
  73.   hot_spot_y = -1 ;
  74.   return ;
  75. }
  76.  
  77. //
  78. //  Description:
  79. //
  80. //    Object copy function.
  81. //
  82. void XbmEncoder::DoCopy (const XbmEncoder &source)
  83. {
  84.   image_name = source.image_name ;
  85.   hot_spot_x = source.hot_spot_x ;
  86.   hot_spot_y = source.hot_spot_y ;
  87.   return;
  88. }
  89.  
  90. //
  91. //  Description:
  92. //
  93. //    Class assignment operator.
  94. //
  95. XbmEncoder &XbmEncoder::operator=(const XbmEncoder &source)
  96. {
  97.   DoCopy (source) ;
  98.   return *this ;
  99. }
  100.  
  101. //         
  102. //  Description:
  103. //
  104. //    This function writes an XBM file to an output stream.
  105. //
  106. //    The format is (according to our grammar):
  107. //
  108. //      #define name_width 124
  109. //      #define name_height 124
  110. //     -#define name_hot_x 124
  111. //     -#define name_hot_y 124
  112. //      static --un-signed- char name_bits [] = {
  113. //                 0x1, 0x2, .... } ;
  114. //
  115. //  Parameters:
  116. //    strm:  The output stream
  117. //    image: The image object
  118. //
  119. void XbmEncoder::WriteImage (std::ostream &strm, BitmapImage &image)
  120. {
  121.   string name ;
  122.   if (image_name != std::string (""))
  123.     name = image_name ;
  124.   else
  125.     name = "image" ;
  126.  
  127.   strm << "#define " << name << "_width " << dec << image.Width () << endl ;
  128.   strm << "#define " << name << "_height " << dec << image.Height () << endl ;
  129.   if (hot_spot_x >= 0 && hot_spot_y >= 0
  130.       && hot_spot_x < image.Width () && hot_spot_y < image.Height ())
  131.   {
  132.     strm << "#define " << name << "_hot_x " << hot_spot_x << endl ;
  133.     strm << "#define " << name << "_hot_y " << hot_spot_y << endl ;
  134.   }
  135.   strm << "static unsigned char " << name << "_bits [] = {" << endl ;
  136.   unsigned int maxwidth ;
  137.   for (unsigned int ii = 0 ; ii < image.Height () ; ++ ii)
  138.   {
  139.     for (unsigned int jj = 0 ; jj < image.Width () ; )
  140.     {
  141.       unsigned value = 0 ;
  142.       for (unsigned int kk = 0 ;
  143.            kk < CHAR_BIT && jj < image.Width () ;
  144.            ++ kk, ++ jj)
  145.       {
  146.         UBYTE1 red, green, blue ;
  147.         image.GetRGB (ii, jj, red, green, blue) ;
  148.         if (red + green + blue < THRESHOLD)
  149.         {
  150.           value |= (1 << kk) ;
  151.         }
  152.       }
  153.       strm << "0x" << hex << setw (2) << setfill ('0')
  154.            << value ;
  155.       if (ii != image.Height () - 1 || jj != image.Width () - 1)
  156.         strm << ", " ;
  157.       ++ maxwidth ;
  158.       if (maxwidth > 12)
  159.       {
  160.         strm << endl ;
  161.         maxwidth = 0 ;
  162.       }
  163.     }
  164.     if (maxwidth != 0)
  165.       strm << endl ;
  166.   }
  167.   strm << "} ;" << endl ;
  168.   return ;
  169. }
  170.  
  171. //
  172. //  Description:
  173. //
  174. //    This function sets the value of the X coordinate of the hot spot.
  175. //
  176. //  Parameters:
  177. //    value: The new position (< 0 => None)
  178. //
  179. void XbmEncoder::SetHotSpotX (int value)
  180. {
  181.   hot_spot_x = value ;
  182.   return ;
  183. }
  184.  
  185. //
  186. //  Description:
  187. //
  188. //    Returns the X coordinate of the Hot Spot
  189. //
  190. //  Return Value:
  191. //    Hot Spot X coordinate (< 0 => No hot spot)
  192. //
  193. int XbmEncoder::GetHotSpotX () const
  194. {
  195.   return hot_spot_x ;
  196. }
  197.  
  198. //
  199. //  Description:
  200. //
  201. //    This function sets the value of the Y coordinate of the hot spot.
  202. //
  203. //  Parameters:
  204. //    value: The new position (< 0 => None)
  205. //
  206. void XbmEncoder::SetHotSpotY (int value)
  207. {
  208.   hot_spot_y = value ;
  209.   return ;
  210. }
  211.  
  212. //
  213. //  Description:
  214. //
  215. //    Returns the Y coordinate of the Hot Spot
  216. //
  217. //  Return Value:
  218. //    Hot Spot Y coordinate (< 0 => No hot spot)
  219. //
  220. int XbmEncoder::GetHotSpotY () const
  221. {
  222.   return hot_spot_y ;
  223. }
  224.  
  225. //
  226. //  Description:
  227. //
  228. //    This function sets the image name used in the #define directives
  229. //    of the output stream.
  230. //
  231. //    No checks are made to ensure that this value is valid. It should start
  232. //    with a letter and contain only letters, underscores, and digits.
  233. //
  234. //  Parameters:
  235. //    value: The image name
  236. //
  237. void XbmEncoder::SetImageName (const std::string &value)
  238. {
  239.   image_name = value ;
  240.   return ;
  241. }
  242.  
  243. //
  244. //  Description:
  245. //
  246. //    This function returns the image name.
  247. //
  248. //  Return Value:
  249. //    The image name.
  250. //
  251. string XbmEncoder::GetImageName () const
  252. {
  253.   return image_name ;
  254. }
  255.  
  256.