Using CtrlView C arrays as C++ picture classes


Greetings...

I really like the fact that CtrlView will convert images to C arrays.

The project I'm currently working on needed to have several imbedded images, with the following restrictions:
I came up with a solution that I find really convenient. I thought you might be interested in hearing about it. Feel free to redistribute to anyone who might find it useful.

The solution consists of three header files, and one C++ file:
Thus, to make a new picture class for a picture called xxxxx, the following steps are done:
The calling program contains the following lines:

    #include "cdgrgbbw.h"        // For black-and-white pictures
    #define CDGIMAGE_NAME xxxxx
    #include "xxxxx.h"           // Declare picture class

To use multiple pictures, just add an #undef for CDGIMAGE_NAME, and then repeat the two last lines with the next picture name. (Using both black-and-white and color pictures in the same calling program would require some casting of the COLORREF defined by cdgrgbbw.h or cdgrgbcl.h.)

Thank you for doing the hard part (converting the images to arrays), and for making a very useful product.

Be well...

Lynn Grant
lgrant@adamscon.com
Castle Development Group
www.CastleDevGroup.com



----- Begin cdgimage.h File -----------------------------------------------------
// cdgimage.h

//
// CDGImage: Wrapper for images converted to arrays by CtrlView
//
// The CDGIMAGE_NAME preprocessor variable must be defined before
// this include is invoked.  It specifies the name of the image.
//
// Note: This header file does not contain include guards, because
// it may legitimately be included multiple times, to define multiple
// images. If this is done, and #UNDEF should be done on CDGIMAGE_NAME,
// then another #DEFINE to set it to the name of picture, before
// cdgimage.h is included again.
//
// Copyright (c) 2003, Castle Development Group
// Website: www.CastleDevGroup.com
// Email: castledev@adamscon.com
//

class CDGIMAGE_NAME
 {
 public:

 // CREATORS

 // MANIPULATORS

 // ACCESSORS

 // RetPtr returns a pointer to the pixel array.
 //
 // It will be necessary to call RetRows and RetCols to find out
 // the dimensions of the array so it can be properly processed.
 //
 // Given r number of rows and c number of columns...
 //
 // Although the image array is a two-dimensional r x c array,
 // it is addressed as a one-dimensional vector with r x c elements.
 // This makes it easier to substitute different sized images,
 // without having to change the calling program.
 //
 // To calculate the single subscript, j, from the x and y subscripts,
 // calculate j (r-y-1) * c + x
 //
 // (This assumes +x goes to the right, and +y goes down.)
 //


  COLORREF * const RetPtr();

 // RetRows returns the number of rows of pixels in the image.

  const int RetRows();

 // RetCols returns the number of columns in the image.

  const int RetCols();

 // RetSize returns the size of an element of the pixel array.

  const int RetSize();

 };
----- End cdgimage.h File -------------------------------------------------------

----- Begin cdgrgbbw.h File -----------------------------------------------------
// cdgrgbbw.h
#ifndef INCLUDED_CDGRGBBW
#define INCLUDED_CDGRGBBW

//
// CDGRgbBw: RGB macro definition for black-and-white images
//
// When the CtrlView utility is used to convert images to C arrays,
// the generated code defines each pixel using the RGB macro. This
// header file defines the RGB macro for use with black-and-white images.
// The generated array has eight-bit elements, with the red value used
// for each element's value. (CtrlView sets the red, green, and blue
// values the same for black-and-white images, so we need only to
// use one of them.)
//
// Copyright (c) 2003, Castle Development Group
// Website: www.CastleDevGroup.com
// Email: castledev@adamscon.com
//

//
// COLORREF is a 8-bit value
//
// The RGB macro assembles R value (0 to 255 only, please) into a COLORREF.

#define RGB(r,g,b)  (COLORREF)(r)

typedef const unsigned char COLORREF;

#endif
----- End cdgrgbbw.h File -------------------------------------------------------

----- Begin cdgrgbcl.h File -----------------------------------------------------
// cdgrgbcl.h
#ifndef INCLUDED_CDGRGBCL
#define INCLUDED_CDGRGBCL

//
// CDGRgbCl: RGB macro definition for color images
//
// When the Ctrlview utility is used to convert images to C arrays,
// the generated code defines each pixel using the RGB macro. This
// header file defines the RGB macro for use with color images.
// The generated array has 32-bit elements, with eight bytes each
// for red, green, and blue. An RGBA macro is also defined. Although
// not currently used, it can be used to define values with an alpha
// channel.
//
// Copyright (c) 2003, Castle Development Group
// Website: www.CastleDevGroup.com
// Email: castledev@adamscon.com
//

// COLORREF is a 32-bit value containing 8-bit color values, possibly with an alpha channel
//
// The values are laid out like this: Alpha, Green, Blue, Red
//
// The RGB macro assembles R, G, and B values (0 to 255 only, please) into a COLORREF.
// The RGBA macro assembles R, G, B, and A (alpha channel) values into a COLORREF.

#define RGB(r,g,b)     ((COLORREF)(((BYTE)(r)|((WORD)(g)<<8))|(((DWORD)(BYTE)(b))<<16)))
#define RGBA(r,g,b,a)  ((COLORREF)(((BYTE)(r)|((WORD)(g)<<8))|(((DWORD)(BYTE)(b))<<16)|(((DWORD)(BYTE)(a))<<24)))

typedef const unsigned long DWORD;
typedef const unsigned int  WORD;
typedef const unsigned char BYTE;
typedef const unsigned long COLORREF;

#endif
----- End cdgrgbcl.h File -------------------------------------------------------

----- Begin cdgimage.cpp File ---------------------------------------------------
// cdgimage.cpp

//
// CDGImage: Wrapper for images converted to arrays by CtrlView
//
// This is a prototype file for making classes out of images
// converted to arrays by the the CTRLVIEW utility. It should
// be renamed to the name of the picture before changing it.
// After that is done, the following changes must be made to it:
//
// - Uncomment the include for cdgrgbbw.h if you want a black-and-white
//   image with eight bits per pixel, or for cdgrgbcl.h if you want a
//   color image with 32 bits per pixel. Comment out the one you don't
//   use.
// - Specify the name of the image class in the define for CDGIMAGE_NAME.
// - Specify the name of the include file from CtrlView. This should
//   have the same name as the class and this file (but with ".c" on the
//   end.
//
// Copyright (c) 2003, Castle Development Group
// Website: www.CastleDevGroup.com
// Email: castledev@adamscon.com
//

// Uncomment the appropriate include:

#include "cdgrgbbw.h"       // Black-and-white, eight bits per pixel
// #include "cdgrgbcl.h"       // Color, 32 bits per pixel

// Specify the name of the image class.  This should match
// the name that this file is saved under (without the .cpp extension)
#define CDGIMAGE_NAME picture

#include "cdgimage.h"

// Specify the name of the include file.  This is a file generated
// by the CtrlView utility.

namespace
{
 #include "picture.c"
};

COLORREF * const CDGIMAGE_NAME::RetPtr()
{
 return &bitmap[0][0];
};
const int CDGIMAGE_NAME::RetRows()
{
 return sizeof(bitmap)/sizeof(bitmap[0]);
};
const int CDGIMAGE_NAME::RetCols()
{
 return sizeof(bitmap[0])/sizeof(bitmap[0][0]);
};
const int CDGIMAGE_NAME::RetSize()
{
 return sizeof(bitmap[0][0]);
};

// CONSTANTS

namespace
{
 const char COPYRIGHT1[] = "Copyright (c) 2003, Castle Development Group ";
 const char COPYRIGHT2[] = "Website: www.CastleDevGroup.com";
 const char COPYRIGHT3[] = "Email: castledev@adamscon.com";
};
----- End cdgimage.cpp File -----------------------------------------------------