/*********************************************
*   file d:\cips\djet.c
*
*
*   Purpose: These functions print a 200x200 
*      image using dithering to an HP DeskJet 
*      or compatable (Laserjet). This uses an 
*      8x8 matrix which gives 64 shades of 
*      gray.
*
*   External Calls:
*      rtiff.c - read_tiff_image (March CUJ,
*                                 Listing 3)
*
*   Modifications:
*      January 1991 - created
*      25 August 1991 - modified for use in the 
*         C Image Processing System.
**********************************************/

#include "d:\cips\cips.h"

#define ESCAPE 27
#define FORMFEED  '\014'

short r[200];


 /*******************************************
 *
 *   The patterns array holds the rows to the
 *   8x8 matrices used for printing 
 *   shades of gray.
 *
 ********************************************/

char patterns[64][8] =
   { {255, 255, 255, 255, 255, 255, 255, 255},
     {255, 255, 255, 255, 255, 255, 255, 127},
     {255, 255, 255, 255, 255, 255, 255,  63},
     {255, 255, 255, 255, 255, 255, 255,  31},
     {255, 255, 255, 255, 255, 255, 255,  15},
     {255, 255, 255, 255, 255, 255, 255,   7},
     {255, 255, 255, 255, 255, 255, 255,   3},
     {255, 255, 255, 255, 255, 255, 255,   1},
     {255, 255, 255, 255, 255, 255, 255,   0},
     {255, 255, 255, 255, 255, 255, 127,   0},
     {255, 255, 255, 255, 255, 255,  63,   0},
     {255, 255, 255, 255, 255, 255,  31,   0},
     {255, 255, 255, 255, 255, 255,  15,   0},
     {255, 255, 255, 255, 255, 255,   7,   0},
     {255, 255, 255, 255, 255, 255,   3,   0},
     {255, 255, 255, 255, 255, 255,   1,   0},
     {255, 255, 255, 255, 255, 255,   0,   0},
     {255, 255, 255, 255, 255, 127,   0,   0},
     {255, 255, 255, 255, 255,  63,   0,   0},
     {255, 255, 255, 255, 255,  31,   0,   0},
     {255, 255, 255, 255, 255,  15,   0,   0},
     {255, 255, 255, 255, 255,   7,   0,   0},
     {255, 255, 255, 255, 255,   3,   0,   0},
     {255, 255, 255, 255, 255,   1,   0,   0},
     {255, 255, 255, 255, 255,   0,   0,   0},
     {255, 255, 255, 255, 127,   0,   0,   0},
     {255, 255, 255, 255,  63,   0,   0,   0},
     {255, 255, 255, 255,  31,   0,   0,   0},
     {255, 255, 255, 255,  15,   0,   0,   0},
     {255, 255, 255, 255,   7,   0,   0,   0},
     {255, 255, 255, 255,   3,   0,   0,   0},
     {255, 255, 255, 255,   1,   0,   0,   0},
     {255, 255, 255, 255,   0,   0,   0,   0},
     {255, 255, 255, 127,   0,   0,   0,   0},
     {255, 255, 255,  63,   0,   0,   0,   0},
     {255, 255, 255,  31,   0,   0,   0,   0},
     {255, 255, 255,  15,   0,   0,   0,   0},
     {255, 255, 255,   7,   0,   0,   0,   0},
     {255, 255, 255,   3,   0,   0,   0,   0},
     {255, 255, 255,   1,   0,   0,   0,   0},
     {255, 255, 255,   0,   0,   0,   0,   0},
     {255, 255, 127,   0,   0,   0,   0,   0},
     {255, 255,  63,   0,   0,   0,   0,   0},
     {255, 255,  31,   0,   0,   0,   0,   0},
     {255, 255,  15,   0,   0,   0,   0,   0},
     {255, 255,   7,   0,   0,   0,   0,   0},
     {255, 255,   3,   0,   0,   0,   0,   0},
     {255, 255,   1,   0,   0,   0,   0,   0},
     {255, 255,   0,   0,   0,   0,   0,   0},
     {255, 127,   0,   0,   0,   0,   0,   0},
     {255,  63,   0,   0,   0,   0,   0,   0},
     {255,  31,   0,   0,   0,   0,   0,   0},
     {255,  15,   0,   0,   0,   0,   0,   0},
     {255,   7,   0,   0,   0,   0,   0,   0},
     {255,   3,   0,   0,   0,   0,   0,   0},
     {255,   1,   0,   0,   0,   0,   0,   0},
     {255,   0,   0,   0,   0,   0,   0,   0},
     {127,   0,   0,   0,   0,   0,   0,   0},
     { 63,   0,   0,   0,   0,   0,   0,   0},
     { 31,   0,   0,   0,   0,   0,   0,   0},
     { 15,   0,   0,   0,   0,   0,   0,   0},
     {  7,   0,   0,   0,   0,   0,   0,   0},
     {  3,   0,   0,   0,   0,   0,   0,   0},
     {  1,   0,   0,   0,   0,   0,   0,   0}};


print_graphics_image(image1, image2, image_name,
                il, ie, ll, le, image_colors,
                invert, caption)
   char  caption[], image_name[];
   int   image_colors, invert, il, ie, ll, le;
   short image1[ROWS][COLS], image2[ROWS][COLS];
{
   char c[80],
   page[80];

   FILE *printer;

   int  i,
   j;

   unsigned long histogram[256], final_hist[256];
   printer = fopen("prn", "w");


 /*****************************************
 *   Print a few blank lines on the page.
 ******************************************/

   strcpy(page, "                             ");
   my_fwriteln(printer, page);
   my_fwriteln(printer, page);

 /************************************
 *   Read in two image arrays.
 *************************************/

   printf("\nReading image");
   read_tiff_image(image_name, image1, il, ie, 
                   ll, le);
   printf("\nReading image");
   read_tiff_image(image_name, image2, il, ie+100, 
                   ll, le+100);

   if(invert == 1){
      for(i=0; i<ROWS; i++){
         for(j=0; j<COLS; j++){
            if(image_colors == 256){
               image1[i][j] = 255 - image1[i][j];
               image2[i][j] = 255 - image2[i][j];
            }
            if(image_colors == 16){
               image1[i][j] = 15 - image1[i][j];
               image2[i][j] = 15 - image2[i][j];
            }
         }
      }
   }

   /**************************************************
   *   Alter the images to 64 gray shades.
   ***************************************************/
   if(image_colors == 256){
      for(i=0; i<ROWS; i++){
         for(j=0; j<COLS; j++){
            image1[i][j] = image1[i][j]/4;
            image2[i][j] = image2[i][j]/4;
        }
      }
   }  /* ends if image_colors == 256 */


   if(image_colors == 16){
      for(i=0; i<ROWS; i++){
         for(j=0; j<COLS; j++){
            image1[i][j] = image1[i][j]*4;
            image2[i][j] = image2[i][j]*4;
         }
      }
   }  /* ends if image_colors == 16 */

/*******************************************
*   Now set the graphics mode on the printer
********************************************/

   printf("\nBegin");
   end_graphics_mode(printer);
   select_300_dpi_resolution(printer);
   set_raster_width(printer);
   start_raster_graphics(printer);
   select_full_graphics_mode(printer);

/**************************************************
*   Print the two arrays to make a 100x200 output.
*   To do this you loop over 100 rows, set the 
*   r buffer to the image values, set the
*   graphics, and print the row via function
*   print_original_200_row.
***************************************************/

   for(i=0; i<100; i++){
      for(j=0; j<100; j++){
         r[j]     = image1[i][j];
         r[j+100] = image2[i][j];
      }  /* ends loop over j */

      end_graphics_mode(printer);
      select_300_dpi_resolution(printer);
      set_raster_width(printer);
      start_raster_graphics(printer);
      select_full_graphics_mode(printer);

      print_original_200_row(printer, r);

      printf("\n\tPrinting row %d", i);
   }  /* ends loop over i */

 /*********************************************
 *   In order to print 200x200 repeat
 *   the above steps for 2 more 100x100 arrays
 **********************************************/

   printf("\nReading image");
   read_tiff_image(image_name, image1, il+100, ie,
                   ll+100, le);
   printf("\nReading image");
   read_tiff_image(image_name, image2,
              il+100, ie+100, ll+100, le+100);

   if(invert == 1){
      for(i=0; i<ROWS; i++){
         for(j=0; j<COLS; j++){
            if(image_colors == 256){
               image1[i][j] = 255 - image1[i][j];
               image2[i][j] = 255 - image2[i][j];
            }
            if(image_colors == 16){
               image1[i][j] = 15 - image1[i][j];
               image2[i][j] = 15 - image2[i][j];
            }
         }
      }
   }

/**************************************************
*   Alter the images to 64 shades of gray.
***************************************************/


   if(image_colors == 256){
      for(i=0; i<ROWS; i++){
         for(j=0; j<COLS; j++){
            image1[i][j] = image1[i][j]/4;
            image2[i][j] = image2[i][j]/4;
        }
      }
   }  /* ends if image_colors == 256 */


   if(image_colors == 16){
      for(i=0; i<ROWS; i++){
         for(j=0; j<COLS; j++){
            image1[i][j] = image1[i][j]*4;
            image2[i][j] = image2[i][j]*4;
         }
      }
   }  /* ends if image_colors == 16 */


   printf("\nBegin");
   end_graphics_mode(printer);
   select_300_dpi_resolution(printer);
   set_raster_width(printer);
   start_raster_graphics(printer);
   select_full_graphics_mode(printer);


/**************************************************
*   Print the two arrays to make a 100x200 output.
*   To do this you loop over 100 rows, set the 
*   r buffer to the image values, set the
*   graphics, and print the row via function
*   print_original_200_row.
***************************************************/
   for(i=0; i<100; i++){
      for(j=0; j<100; j++){
         r[j]     = image1[i][j];
         r[j+100] = image2[i][j];
      }  /* ends loop over j */

      end_graphics_mode(printer);
      select_300_dpi_resolution(printer);
      set_raster_width(printer);
      start_raster_graphics(printer);
      select_full_graphics_mode(printer);

      print_original_200_row(printer, r);

      printf("\n\tPrinting row %d", i);
   }  /* ends loop over i */

/**************************************************
*   Print a couple of blank lines then print      
*   the caption. 
***************************************************/

   end_graphics_mode(printer);
   strcpy(page, "      ");
   my_fwriteln(printer, page);
   my_fwriteln(printer, page);

   sprintf(page, "                      %s", caption);
   my_fwriteln(printer, page);

   putc(FORMFEED, printer);

   fclose(printer);

   printf("\nEnd");

}  /* ends print_graphics_image */

/*********************************
*   get_graphics_caption(...
**********************************/

get_graphics_caption(caption)
   char caption[];
{
   printf("\nEnter the caption to be printed\n---");
   read_string(caption);

}  /* ends get_graphics_caption */


select_full_graphics_mode(printer)

   FILE *printer;
{
   putc(ESCAPE, printer);
   putc('*', printer);
   putc('b', printer);
   putc('0', printer);
   putc('M', printer);

}


set_horizontal_offset(printer)

   FILE *printer;
{
   putc(ESCAPE, printer);
   putc('*', printer);
   putc('b', printer);
   putc('4', printer);
   putc('9', printer);
   putc('6', printer);
   putc('X', printer);

}


end_graphics_mode(printer)
   FILE *printer;
{
   putc(ESCAPE, printer);
   putc('*', printer);
   putc('r', printer);
   putc('B', printer);
}


set_raster_width(printer)

   FILE *printer;
{
   putc(ESCAPE, printer);
   putc('*', printer);
   putc('r', printer);
   putc('2', printer);
   putc('2', printer);
   putc('0', printer);
   putc('0', printer);
   putc('S', printer);

}


start_raster_graphics(printer)

   FILE *printer;
{
   putc(ESCAPE, printer);
   putc('*', printer);
   putc('r', printer);
   putc('0', printer);
   putc('A', printer);

}



select_300_dpi_resolution(printer)

   FILE *printer;
{
   putc(ESCAPE, printer);
   putc('*', printer);
   putc('t', printer);
   putc('3', printer);
   putc('0', printer);
   putc('0', printer);
   putc('R', printer);

}


print_bytes(printer, buffer)
   FILE *printer;
   char buffer[];
{
   int        i;

   putc(ESCAPE, printer);
   putc('*', printer);
   putc('b', printer);
   putc('2', printer);
   putc('0', printer);
   putc('0', printer);
   putc('W', printer);

   for(i=0; i<200; i++){
      putc(buffer[i], printer);
   }
}  /* ends print_bytes */

/********************************
*   print_original_200_row(...
*********************************/

print_original_200_row(printer, short_row)
   FILE  *printer;
   short short_row[200];
{
   char  row[8][200];
   char  c[200], response[80];
   int     i, j, k;
   short value;

   for(i=0; i<200; i++){
      value = short_row[i];
      if(value > 63) value = 63;
      if(value < 0)  value =  0;

      for(j=0; j<8; j++)
         row[j][i] = patterns[value][j];
   }  /* ends loop over i */

   for(i=0; i<8; i++){
      for(j=0; j<200; j++)
         c[j] = row[i][j];
      set_horizontal_offset(printer);
      print_bytes(printer, c);
   }  /* ends loop over i */
}  /* ends print_original_200_row */
