Received: by ATHENA-PO-2.MIT.EDU (5.45/4.7) id AA02254; Tue, 7 Mar 89 11:01:19 EST
Received: by ATHENA.MIT.EDU (5.45/4.7) id AA08805; Tue, 7 Mar 89 11:01:14 EST
Received: by bu-cs.BU.EDU (5.58/4.7)
	id AA17626; Tue, 7 Mar 89 11:00:12 EST
Date: Tue, 7 Mar 89 11:00:12 EST
From: brian@bu-cs.BU.EDU (Brian Gardner)
Message-Id: <8903071600.AA17626@bu-cs.BU.EDU>
To: bgardner@ATHENA.MIT.EDU
Subject: cvt2bitm.c

#include <stdio.h>
#include <dos.h>        /* IBM-PC routines */
/* #include <math.h> */
/* #include <file.h> */
#include <graph.h>      /* IBM-PC graphics routines */
#include <fcntl.h>      /* for O_RDONLY | O_BINARY */

#define GRAFMODE 46
#define setpixel(x, y, color)  \
           writePixel((int) x, (int) y + y + 40, (int) color); \
           writePixel((int) x, (int) y+ y + 41, (int) color)

#define max(a, b) ((a > b) ? a : b)

#define RE 0
#define GR 1
#define BL 2

#define NEG 0
#define REG 1
#define POS 2

#define ESC 27

#define WIDTH    641
#define HEIGHT   201
#define DEPTH      3
#define NBYTES   (WIDTH * HEIGHT * DEPTH)
#define PBYTES   (WIDTH * DEPTH)

#define BLURR_DIAMETER 0

unsigned char huge the_image[WIDTH][HEIGHT][DEPTH];

#define ICON_NAME "bird"
char filename[] = "bird.raw";

#define intensity(array, x, y, blurr) \
           ((int) max(array[x][y][RE], \
	            max(array[x][y][GR], array[x][y][BL])))

int bitchoose(x0, y)
     int x0, y;
 { int err, value, old_inten, i;

   value = rand() & 63;
   old_inten = (the_image[x0][y][POS] * 256) + the_image[x0][y][REG];
   if (the_image[x0][y][NEG] == 1)
     old_inten *= -1;
   err = (int) ((old_inten - value) / 3);
   
   /* Re-adjust first neighbor (x0, y+1) */
   i = (the_image[x0][y+1][POS] * 256) + the_image[x0][y+1][REG];
   if (the_image[x0][y+1][NEG] == 1)
     i *= -1;
   i += err;
   the_image[x0][y+1][NEG] = (i < 0) ? 1 : 0;    /* flags if negative    */
   the_image[x0][y+1][REG] = i % 256;            /* remainder of i / 256 */
   the_image[x0][y+1][POS] = (int) (i / 256);    /* factor of 256 of i   */  
   
   /* Re-adjust first neighbor (x0+1, y+1) */
   i = (the_image[x0+1][y+1][POS] * 256) + the_image[x0+1][y+1][REG];
   if (the_image[x0+1][y+1][NEG] == 1)
     i *= -1;
   i += err;
   the_image[x0+1][y+1][NEG] = (i < 0) ? 1 : 0;    /* flags if negative    */
   the_image[x0+1][y+1][REG] = i % 256;            /* remainder of i / 256 */
   the_image[x0+1][y+1][POS] = (int) (i / 256);    /* factor of 256 of i   */  
   
   /* Re-adjust first neighbor (x0+1, y) */
   i = (the_image[x0+1][y][POS] * 256) + the_image[x0+1][y][REG];
   if (the_image[x0+1][y][NEG] == 1)
     i *= -1;
   i += err;
   the_image[x0+1][y][NEG] = (i < 0) ? 1 : 0;    /* flags if negative    */
   the_image[x0+1][y][REG] = i % 256;            /* remainder of i / 256 */
   the_image[x0+1][y][POS] = (int) (i / 256);    /* factor of 256 of i   */  

   return(value < old_inten);
 };

void setMode(mode)
    int mode;
  { union REGS reg;
  
    reg.h.ah = 0;
    reg.h.al = mode;
    int86(0x10, &reg, &reg);
  };
  
void setColorReg(color_reg, red, green, blue)
    int color_reg, red, green, blue;
  { union REGS reg;
  
    /*       color =  r | (g << 8) | (b << 16); */
    /*       _remappalette( i, (long) color);   */
    reg.x.ax = 0x1010;
    reg.x.bx = color_reg;
    reg.h.ch = green << 1;
    reg.h.cl = blue << 1;
    reg.h.dh = red << 1;
    int86(0x10, &reg, &reg);
  };
  
void writePixel(x, y, color)
    int x, y, color;
  { union REGS reg;

	/* _setcolor((short) (histogram[r0][g0][b0])); */
        /* setpixel(x, y);                             */
    reg.h.ah = 0x0C;
    reg.h.al = color;   /* pixel color value */
    reg.h.bh = 0;
    reg.x.cx = x;
    reg.x.dx = y;
    int86(0x010, &reg, &reg);
  };

main(argc, argv)
     int argc;
     char *argv[];
 { int x, y, z, i, j, c;
   unsigned long actual_nbytes;
   unsigned int value;
   int imagefile;   /* file pointer for image framebuffer    */
   unsigned char huge *array_ptr;

   /* initialize the image array */
   /* -------------------------- */
   if ((imagefile = open(filename, O_RDONLY | O_BINARY)) < 0)
     { fprintf(stderr, " ** Unable to open file >= %s\n", filename);
       exit(1);
     };

/*   actual_nbytes = read(imagefile, the_image, (unsigned) NBYTES); */
   actual_nbytes = 0;
   array_ptr = the_image;
   for (y = 0; y < HEIGHT - 1; y++)
     { actual_nbytes += read(imagefile, array_ptr,
                          (unsigned) PBYTES);
       array_ptr += PBYTES;
     };

   if (actual_nbytes < NBYTES)
     { fprintf(stderr, " Actually read %d bytes. That was short by %d.\n", 
	    actual_nbytes, (NBYTES - actual_nbytes));
       exit(1);
     };
     
   /* Load color table with greyscale (for IBM-PC) */
   /* -------------------------------------------- */
   _setvideomode(_MRES256COLOR);
   setMode(GRAFMODE);   
   for (x = 0; x < 256; x++)
     setColorReg(x, (int) x, (int) x, (int) x);
  
   /* Make an intensity only version of the image */
   /* ------------------------------------------- */
   for(y = 0; y < HEIGHT; y++)
     for(x = 0; x < WIDTH; x++)
       { i = intensity(the_image, x, y, BLURR_DIAMETER);
         the_image[x][y][NEG] = (i < 0) ? 1 : 0;    /* flags if negative    */
         the_image[x][y][REG] = i % 256;            /* remainder of i / 256 */
         the_image[x][y][POS] = (int) (i / 256);    /* factor of 256 of i   */
         setpixel(x, y, intensity(the_image, x, y, BLURR_DIAMETER));
       };
       
   /* Make bitmap version of image */
   /* ---------------------------- */
   for(y = 1; y < (HEIGHT - 1); y++)
     for(x = 0; x < (WIDTH - 1); x++)
       the_image[x][y][REG] = bitchoose(x, (y - 1));

   /* On IBM-PC wait for user to hit escape key before proceeding */
   /* ----------------------------------------------------------- */
   while((c = getch()) != ESC) x++;   /* wait to exit program (for IBM-PC) */
   _setvideomode(-1);   /* reset to default video mode (for IBM-PC) */

   /* Dump everything to a bitmap file */
   /* -------------------------------- */
   printf("#define %s_width %d\n", ICON_NAME, (WIDTH - 1));
   printf("#define %s_height %d\n", ICON_NAME, (HEIGHT - 1) * 2);
   printf("static char %s_bits[] = {", ICON_NAME);
   i = 0;
   for(y = 0; y < (HEIGHT - 1); y++)
     for (z = 0; z < 2; z++)
       for(x = 0; x < (WIDTH - 1); x += 8)
	 { if (i%12 == 0)
	     { if ((x != 0) && (y != 0) && (z != 0))
		 printf(",");
	       printf("\n  ");
	     }
	   else
	     printf(",");
	   value = 0;
	   for (j = 0; j < 8; j++)
	     value = (value << 1) | (the_image[x + j][y][REG] & 1);
	   printf(" 0x%.2x", value);
	   i++;
	 };

   printf("};\n");

 };      /* end of main()  */

