/************************** string_to_img.c ***********************************

*

* This program takes a string in its argument and creates a X11 bitmap

* image of the string then stores it in a file with same name as the argument.

*

* Currently little error checking is performed and it is likely to get

* confused if provided bogus input.  The following restrictions apply:

*

*  1) The function only supports integers.  Alpha will cause a bitmap to be

*     produced that contains random dots.  Filenames given in the argument

*     with a file extension or the '.' character will cause the function to core dump.

*

*  2) The generated bitmap is 100x20 pixels in size.  The font size is

*     24 pts.  Character spacing is 1 space.  This allows for integers of up

*     to 5 digits to be correctly displayed.

*

*  Maintenance programmers should add/update the above as required.

*

*  This function was developed by Puget Power in November 1996.

*

*     Nov. 96  v1.0 -

*              inital build.

*              J. Martin, Puget Power.

*

*     Dec. 96 v1.1 -  

*             Implemented the following changes in order

*             to ehance excution speed:

*

*             + No argument varible error checking is performed.

*             + A file is created without first appending the

*               filename with a '.img' extension.

*             + Decreased bitmap size from 100x100 to 100x20 (the smallest size

*               I could get INGRESS to use).            

*             + Eliminated all user fuctions in order minimize function

*               call overhead.  All user code is local.

*             + Deleted processing to insert IMG terminating character

*               { '}' ) in the output, DECpaint and INGRESS doesn't

*               seem to notice....

*             + Cleared bits 32 and 16 in varible 'string_decode' if

*               set using bitwise operators vice arithmetic operators

*               in order to perform ascii to int conversion in the

*               decode/encode logic.  This effectively subtracts

*               decimal 48 from the varible.

*             + Modified code that initialized the image buffer in

*               order to zero it quicker

*             + Recompiled with -O3 optimizations.

*

*             Changed IMG_SIZE, digit arrays and decode logic in order to

*             produce a bitmap that contains 24 pt size characters.            

*                J. Martin, Puget Power.

*

******************************************************************************/

 

#include "/usr/include/stdio.h"

#include "/usr/include/stdlib.h"

#include "/usr/sys/h/file.h"

#include "/usr/include/strings.h"

 

#define IMG_WIDTH     "100"   /* field used in IMG header       */

#define IMG_HEIGHT    "20"    /* specifies a 100x100 bitmap     */

#define IMG_SIZE       285    /* buffer of pixels for IMG file  */

#define DIGIT_SIZE      32    /* size of digit bitmap-1         */

#define PRINT_OFFSET    13    /* digit font size 100x100 bitmap */

#define PRINT_SPACING    2    /* distance between digits        */

 

FILE *img_file_ptr;           /* handle to IMG file             */

 

int major_counter,

    minor_counter,

    string_decode,           /* a character from argv[1]        */

    PRINT_START  =   1;      /* controls where in the 100x100

                                bitmap printing starts at       */

 

/* img file buffer ( assumes short is 2 bytes)                  */

 

short img_file_buffer[IMG_SIZE+1],

 

/* arrays to store digits (0-9 respectively) bitmaps            */

 

 digit[10][32] =                                            

 {

 0x80, 0x07, 0xe0, 0x1f, 0x60, 0x18, 0x30, 0x30,

 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,

 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,

 0x30, 0x30, 0x60, 0x18, 0xe0, 0x1f, 0x80, 0x07,

 

 0x80, 0x03, 0xf0, 0x03, 0xf0, 0x03, 0x00, 0x03,

 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,

 0x00, 0x03, 0x00, 0x03, 0x00, 0x03, 0x00, 0x03,

 0x00, 0x03, 0x00, 0x03, 0xf0, 0x3f, 0xf0, 0x3f,

  

 0xf0, 0x03, 0xf8, 0x07, 0x1c, 0x0e, 0x0c, 0x0c,

 0x0c, 0x0c, 0x00, 0x0c, 0x00, 0x0e, 0x00, 0x07,

 0x80, 0x03, 0xc0, 0x00, 0xe0, 0x00, 0x70, 0x00,

 0x38, 0x00, 0x1c, 0x00, 0xfe, 0x0f, 0xfe, 0x0f,

 

 0xe0, 0x07, 0xf0, 0x0f, 0x38, 0x1c, 0x18, 0x18,

 0x00, 0x18, 0x00, 0x1c, 0x00, 0x0e, 0xc0, 0x07,

 0xc0, 0x0f, 0x00, 0x1c, 0x00, 0x18, 0x00, 0x18,

 0x0c, 0x18, 0x1c, 0x1c, 0xf8, 0x0f, 0xf0, 0x07,

 

 0x00, 0x00, 0x80, 0x07, 0xc0, 0x07, 0xc0, 0x06,

 0x60, 0x06, 0x60, 0x06, 0x30, 0x06, 0x30, 0x06,

 0x18, 0x06, 0x18, 0x06, 0xfc, 0x1f, 0xfc, 0x1f,

 0x00, 0x06, 0x00, 0x06, 0xc0, 0x1f, 0xc0, 0x1f,

  

 0xf8, 0x0f, 0xf8, 0x0f, 0x18, 0x00, 0x18, 0x00,

 0x18, 0x00, 0xd8, 0x03, 0xf8, 0x0f, 0x38, 0x0e,

 0x00, 0x1c, 0x00, 0x18, 0x00, 0x18, 0x00, 0x18,

 0x0c, 0x1c, 0x1c, 0x0e, 0xf8, 0x0f, 0xf0, 0x03,

 

 0x00, 0x3e, 0x80, 0x3f, 0xc0, 0x03, 0xe0, 0x00,

 0x60, 0x00, 0x70, 0x00, 0x30, 0x0f, 0xb0, 0x1f,

 0xf0, 0x38, 0x70, 0x30, 0x30, 0x30, 0x30, 0x30,

 0x70, 0x30, 0xe0, 0x38, 0xe0, 0x1f, 0x80, 0x0f,

 

 0xe0, 0x7f, 0xe0, 0x7f, 0x60, 0x60, 0x00, 0x60,

 0x00, 0x30, 0x00, 0x30, 0x00, 0x30, 0x00, 0x18,

 0x00, 0x18, 0x00, 0x18, 0x00, 0x0c, 0x00, 0x0c,

 0x00, 0x06, 0x00, 0x06, 0x00, 0x06, 0x00, 0x00,

 

 0x00, 0x0f, 0xc0, 0x3f, 0xe0, 0x70, 0x60, 0x60,

 0x60, 0x60, 0x60, 0x60, 0xc0, 0x30, 0x80, 0x1f,

 0xc0, 0x3f, 0xe0, 0x70, 0x60, 0x60, 0x60, 0x60,

 0x60, 0x60, 0xe0, 0x70, 0xc0, 0x3f, 0x80, 0x1f,

 

 0x00, 0x0f, 0xc0, 0x3f, 0xc0, 0x30, 0x60, 0x60,

 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x70,

 0xe0, 0x78, 0xc0, 0x6f, 0x80, 0x67, 0x00, 0x60,

 0x00, 0x30, 0x00, 0x38, 0xe0, 0x1f, 0xe0, 0x07

 };                                    

 

/****************** start of main ***********************************************/

 

int main( argc, argv )

int argc;

char *argv[];

 

{

int pixel = PRINT_START,

    num_of_char = ( strlen( argv[1] ) ) - 1 ;

    if( num_of_char > 10 ) { num_of_char = 10; }

 

/**************************************************************************

*

* open a file with same name as argument and write img header info

*

**************************************************************************/

 

img_file_ptr = fopen(argv[1], "w");

 

if( img_file_ptr == 0 )

   {

      printf("can't create image file %s\n", argv[1] );

      exit(-1);

   }

 

fprintf( img_file_ptr, "#define %s_width %s\n", argv[1], IMG_WIDTH );

fprintf( img_file_ptr, "#define %s_height %s\n", argv[1], IMG_HEIGHT );

fprintf( img_file_ptr, "#define %s_x_hot 0\n", argv[1] );

fprintf( img_file_ptr, "#define %s_y_hot 0\n", argv[1] );

fprintf( img_file_ptr, "static char %s_bits[] = {\n", argv[1] );

 

/******************************************************************************

*

*   decode 4 digits and store in IMG buffer

*

******************************************************************************/

 

for( major_counter=0; major_counter<=num_of_char; major_counter++ )

    {

     string_decode =  *argv[1]++;            /* get a character             */

     string_decode &= 0xcf;                  /* convert to int              */

 

/* shift the corresponding digit array into a 100x20 bitmap                 */

 

     for( minor_counter=0; minor_counter<DIGIT_SIZE; minor_counter += 2 )

         {

           img_file_buffer[pixel] = digit[string_decode][minor_counter];

           img_file_buffer[pixel+1] = digit[string_decode][minor_counter+1]; 

           pixel += PRINT_OFFSET;   /* next Y scan line in bitmap  */

         } 

      pixel = (PRINT_START += PRINT_SPACING);  /* next X position  */

 

    } /* end number of char loop */

 

/**************************************************************************

*

* write the img buffer to file

*

***************************************************************************/

minor_counter = 0;

for( major_counter = 0; major_counter <= IMG_SIZE; major_counter++ )

    {     

      fprintf( img_file_ptr, "0x%02x, ", img_file_buffer[major_counter] );

 

   minor_counter++;    

      if( minor_counter > 11 )

         {

            minor_counter = 0;

            fprintf( img_file_ptr, "\n");         

         }

     }

fclose( img_file_ptr);

 

}/********************* end of main *******************************************/