home *** CD-ROM | disk | FTP | other *** search
- //
- // Copyright (c) 1997,1998 Colosseum Builders, Inc.
- // All rights reserved.
- //
- // Colosseum Builders, Inc. makes no warranty, expressed or implied
- // with regards to this software. It is provided as is.
- //
- // See the README.TXT file that came with this software for restrictions
- // on the use and redistribution of this file or send E-mail to
- // info@colosseumbuilders.com
- //
-
- //
- // Title: XBM Encoder Class Definition
- //
- // Author: John M. Miano miano@colosseumbuilers.com
- //
-
- #include <iomanip>
- #include <string>
- #include <ctype.h>
-
- #include "xbmencod.h"
- #include "xbmpvt.h"
-
- using namespace std ;
- // This constant defines the threshold for setting a value to black in the
- // output. You may wish to make this run-time configurable.
- const int THRESHOLD = (3*(UCHAR_MAX+1))/2 ;
-
- //
- // Description:
- //
- // Class default constructor
- //
- XbmEncoder::XbmEncoder ()
- {
- Initialize () ;
- return ;
- }
-
- //
- // Description:
- //
- // Class copy constructor
- //
- XbmEncoder::XbmEncoder (const XbmEncoder &source)
- {
- Initialize () ;
- DoCopy (source) ;
- return ;
- }
-
- //
- // Descripton:
- //
- // Class Default Constructor
- //
- XbmEncoder::~XbmEncoder ()
- {
- return ;
- }
-
- //
- // Description:
- //
- // Class initialization function.
- //
- void XbmEncoder::Initialize ()
- {
- image_name = "" ;
- hot_spot_x = -1 ;
- hot_spot_y = -1 ;
- return ;
- }
-
- //
- // Description:
- //
- // Object copy function.
- //
- void XbmEncoder::DoCopy (const XbmEncoder &source)
- {
- image_name = source.image_name ;
- hot_spot_x = source.hot_spot_x ;
- hot_spot_y = source.hot_spot_y ;
- return;
- }
-
- //
- // Description:
- //
- // Class assignment operator.
- //
- XbmEncoder &XbmEncoder::operator=(const XbmEncoder &source)
- {
- DoCopy (source) ;
- return *this ;
- }
-
- //
- // Description:
- //
- // This function writes an XBM file to an output stream.
- //
- // The format is (according to our grammar):
- //
- // #define name_width 124
- // #define name_height 124
- // -#define name_hot_x 124
- // -#define name_hot_y 124
- // static --un-signed- char name_bits [] = {
- // 0x1, 0x2, .... } ;
- //
- // Parameters:
- // strm: The output stream
- // image: The image object
- //
- void XbmEncoder::WriteImage (std::ostream &strm, BitmapImage &image)
- {
- string name ;
- if (image_name != std::string (""))
- name = image_name ;
- else
- name = "image" ;
-
- strm << "#define " << name << "_width " << dec << image.Width () << endl ;
- strm << "#define " << name << "_height " << dec << image.Height () << endl ;
- if (hot_spot_x >= 0 && hot_spot_y >= 0
- && hot_spot_x < image.Width () && hot_spot_y < image.Height ())
- {
- strm << "#define " << name << "_hot_x " << hot_spot_x << endl ;
- strm << "#define " << name << "_hot_y " << hot_spot_y << endl ;
- }
- strm << "static unsigned char " << name << "_bits [] = {" << endl ;
- unsigned int maxwidth ;
- for (unsigned int ii = 0 ; ii < image.Height () ; ++ ii)
- {
- for (unsigned int jj = 0 ; jj < image.Width () ; )
- {
- unsigned value = 0 ;
- for (unsigned int kk = 0 ;
- kk < CHAR_BIT && jj < image.Width () ;
- ++ kk, ++ jj)
- {
- UBYTE1 red, green, blue ;
- image.GetRGB (ii, jj, red, green, blue) ;
- if (red + green + blue < THRESHOLD)
- {
- value |= (1 << kk) ;
- }
- }
- strm << "0x" << hex << setw (2) << setfill ('0')
- << value ;
- if (ii != image.Height () - 1 || jj != image.Width () - 1)
- strm << ", " ;
- ++ maxwidth ;
- if (maxwidth > 12)
- {
- strm << endl ;
- maxwidth = 0 ;
- }
- }
- if (maxwidth != 0)
- strm << endl ;
- }
- strm << "} ;" << endl ;
- return ;
- }
-
- //
- // Description:
- //
- // This function sets the value of the X coordinate of the hot spot.
- //
- // Parameters:
- // value: The new position (< 0 => None)
- //
- void XbmEncoder::SetHotSpotX (int value)
- {
- hot_spot_x = value ;
- return ;
- }
-
- //
- // Description:
- //
- // Returns the X coordinate of the Hot Spot
- //
- // Return Value:
- // Hot Spot X coordinate (< 0 => No hot spot)
- //
- int XbmEncoder::GetHotSpotX () const
- {
- return hot_spot_x ;
- }
-
- //
- // Description:
- //
- // This function sets the value of the Y coordinate of the hot spot.
- //
- // Parameters:
- // value: The new position (< 0 => None)
- //
- void XbmEncoder::SetHotSpotY (int value)
- {
- hot_spot_y = value ;
- return ;
- }
-
- //
- // Description:
- //
- // Returns the Y coordinate of the Hot Spot
- //
- // Return Value:
- // Hot Spot Y coordinate (< 0 => No hot spot)
- //
- int XbmEncoder::GetHotSpotY () const
- {
- return hot_spot_y ;
- }
-
- //
- // Description:
- //
- // This function sets the image name used in the #define directives
- // of the output stream.
- //
- // No checks are made to ensure that this value is valid. It should start
- // with a letter and contain only letters, underscores, and digits.
- //
- // Parameters:
- // value: The image name
- //
- void XbmEncoder::SetImageName (const std::string &value)
- {
- image_name = value ;
- return ;
- }
-
- //
- // Description:
- //
- // This function returns the image name.
- //
- // Return Value:
- // The image name.
- //
- string XbmEncoder::GetImageName () const
- {
- return image_name ;
- }
-
-