home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / sunimp.tar / sunimp.c < prev   
C/C++ Source or Header  |  1986-10-13  |  7KB  |  270 lines

  1. /*
  2.  * This filter converts Sun rasterfiles into Impress file for printing on an
  3.  * Imagen laser printer.  Converted by Walter Underwood, Ford Aerospace, Palo
  4.  * Alto (wunder@ford-wdl1). 
  5.  *
  6.  * This filter used to convert MacPaint files into Impress files for printing on
  7.  * an Imagen laser printer.  Based on MACimp for Tops-20 by Ed Pattermann,
  8.  * Stanford. 
  9.  *
  10.  * Usage:  screendump | sunimp | ipr 
  11.  *
  12.  * History:      
  13.  * --------
  14.  * 25 July 1984  Winkler created. [macimp] 
  15.  *
  16.  * 27 July 1984  Burgess (sumex-aim): modified write_imp_init() code to reflect
  17.  * appropriate owner and spooldate 
  18.  *
  19.  * 17 Oct. 1984  W. Underwood converted to read Sun 2 bitmaps as used by the
  20.  * "screendump" and "screenload" programs.  This is suspected to be a "memory
  21.  * pixrect" format. 
  22.  *
  23.  * 12 Dec. 1984  W. Underwood changed throw_away_sun_header() to
  24.  * read_sun_header() to read and understand the rasterfile header. 
  25.  *
  26.  * 13 Oct. 1986  V. Jacobson changed to work on Sun-3/O.S. 3.x.  Changed
  27.  * to handle rasterlines that are not a multiple of 32 bits.  Changed
  28.  * to handle Sun 3/160 (color) framebuffer.
  29.  */
  30. static char *RCSident = "$Header: sunimp.c,v 1.9 86/10/13 04:35:21 van Exp $";
  31.  
  32. #include <stdio.h>
  33. #include <rasterfile.h>
  34.  
  35. #define     MAG_POWER           1    /* can be 0, 1, or 2 */
  36. #define     DOTS_PER_INCH       300    /* For Imagen 8/300 */
  37.  
  38. #define     OPAQUE              3
  39. #define     OPERATION_TYPE      OPAQUE
  40.  
  41. #define     LOBYTE(number) ((char)(number & 0x00FF))
  42. #define     HIBYTE(number16) ((char)((number16 >> 8) & 0x00FF))
  43.  
  44. #define     PATCH_SIZE          32
  45. #define     PATCH_BYTES         4
  46.  
  47. typedef char patch[PATCH_SIZE][PATCH_BYTES];
  48.  
  49. int h_patches;            /* Number of Impress patches across */
  50. int v_patches;            /* Number of Impress patches down */
  51. int pad_bytes;            /* Bytes to fill last row of patches */
  52. int ras_lines;            /* number of lines in sun raster */
  53. int ras_bytes;            /* number of bytes per line */
  54. int horiz_pad;            /* number of pad bytes per line */
  55. unsigned char *remap;        /* table to map color/g.s. to 0/1 */
  56.  
  57. main ()
  58. {
  59.     read_sun_header ();
  60.     write_imp_init ();
  61.     process_data ();
  62.     write_imp_end ();
  63.     fflush (stdout);
  64. }
  65.  
  66. process_data ()
  67. {
  68.     register int i, j, k;
  69.     register int byte, pix;
  70.  
  71.     if (remap) {
  72.         /* handle color/grey scale raster file */
  73.         for (i = 0; i < ras_lines; i++) {
  74.             for (j = 0; j < ras_bytes; j++) {
  75.                 pix = 0;
  76.                 for (k = 0; k < 8; k++) {
  77.                     if ((byte = getchar ()) == EOF)
  78.                         fatal ("Premature EOF in rasterfile");
  79.                     pix <<= 1;
  80.                     pix |= remap[byte];
  81.                 }
  82.                 add_byte ((char) pix);
  83.             }
  84.             for (j = 0; j < horiz_pad; j++)
  85.                 add_byte ((char) 0);
  86.         }
  87.     } else {
  88.         /* handle monochrome raster file */
  89.         for (i = 0; i < ras_lines; i++) {
  90.             for (j = 0; j < ras_bytes; j++) {
  91.                 if ((byte = getchar ()) == EOF)
  92.                     fatal ("Premature EOF in rasterfile");
  93.                 add_byte ((char) byte);
  94.             }
  95.             for (j = 0; j < horiz_pad; j++)
  96.                 add_byte ((char) 0);
  97.         }
  98.     }
  99.     for (i = 0; i < pad_bytes; i++)
  100.         add_byte ((char) 0);
  101. }
  102.  
  103. add_byte (one_byte)
  104.     char one_byte;
  105. {
  106.     extern int h_patches;
  107.     extern char *calloc ();
  108.  
  109.     static patch *patches = NULL;
  110.     static short patch_num = 0;
  111.     static short patch_row = 0;
  112.     static short patch_col = 0;
  113.  
  114.     if (patches == NULL)    /* First time only */
  115.         if (! (patches = (patch *) calloc (h_patches, sizeof (patch))))
  116.             fatal ("Cannot alloc space for conversion array.\n");
  117.  
  118.     patches[patch_num][patch_row][patch_col] = one_byte;
  119.  
  120.     if (++patch_col >= PATCH_BYTES) {
  121.         patch_col = 0;
  122.         if (++patch_num >= h_patches) {
  123.             patch_num = 0;
  124.             if (++patch_row >= PATCH_SIZE) {
  125.                 patch_row = 0;
  126.                 write_patches (patches);
  127.             }
  128.         }
  129.     }
  130. }
  131.  
  132. write_patches (patches)
  133.     patch *patches;
  134. {
  135.     register int patchnum, patchrow, patchcol;
  136.  
  137.     for (patchnum = 0; patchnum < h_patches; patchnum++)
  138.         for (patchrow = 0; patchrow < PATCH_SIZE; patchrow++)
  139.             for (patchcol = 0; patchcol < PATCH_BYTES; patchcol++)
  140.                 putchar (patches[patchnum][patchrow][patchcol]);
  141. }
  142.  
  143. read_sun_header ()
  144. {
  145.     extern char *malloc ();
  146.     register int i, j;
  147.     struct rasterfile rf;
  148.  
  149.     rf.ras_magic = getw (stdin);
  150.     if (feof (stdin) != 0)
  151.         exit (0);
  152.     rf.ras_width = getw (stdin);
  153.     rf.ras_height = getw (stdin);
  154.     rf.ras_depth = getw (stdin);
  155.     rf.ras_length = getw (stdin);
  156.     rf.ras_type = getw (stdin);
  157.     rf.ras_maptype = getw (stdin);
  158.     rf.ras_maplength = getw (stdin);
  159.  
  160.     /* Start checking the header */
  161.  
  162.     if (rf.ras_magic != RAS_MAGIC)
  163.         error ("Warning: bad magic number in rasterfile (%d)\n",
  164.                rf.ras_magic);
  165.  
  166.     if (rf.ras_depth != 1 && rf.ras_depth != 8)
  167.         fatal ("Sorry, can't handle %d bits per pixel.\n",
  168.                rf.ras_depth);
  169.  
  170.     if (rf.ras_type > 1)
  171.         fatal ("Sorry, can only handle type 0 or 1.  ras_type= %d\n",
  172.                rf.ras_type);
  173.  
  174.     /* If there is a colormap, set up to re-map it to 0/1. */
  175.     if (rf.ras_maplength) {
  176.         register unsigned char *r, *g, *b;
  177.  
  178.         remap = (unsigned char *)malloc (256);
  179.         j = rf.ras_maplength / 3;
  180.         r = (unsigned char *)malloc (j);
  181.         g = (unsigned char *)malloc (j);
  182.         b = (unsigned char *)malloc (j);
  183.         fread (r, 1, j, stdin);
  184.         fread (g, 1, j, stdin);
  185.         fread (b, 1, j, stdin);
  186.         for (i = 0; i < 256; i++)
  187.             remap[i] = ((r[i]&g[i]&b[i]) != 255);
  188.  
  189.         free (r); free (g); free (b);
  190.     }
  191.     ras_lines = rf.ras_height;
  192.     ras_bytes = rf.ras_width / 8;
  193.     horiz_pad = ras_bytes % PATCH_BYTES;
  194.     h_patches = (rf.ras_width + (PATCH_SIZE - 1)) / PATCH_SIZE;
  195.     v_patches = (rf.ras_height + (PATCH_SIZE - 1)) / PATCH_SIZE;
  196.  
  197.     pad_bytes = h_patches * PATCH_BYTES * 
  198.             (v_patches * PATCH_SIZE - rf.ras_height);
  199.  
  200. }
  201.  
  202. write_imp_init ()
  203. {
  204.     char *getenv ();
  205.     char *ctime (), cimptime[26];
  206.     long time (), imptime;
  207.  
  208.     imptime = time (0);    /* get time in internal form */
  209.     strcpy (cimptime, ctime (&imptime));    /* put time in string form */
  210.     cimptime[24] = '\0';    /* nullify \n in time string */
  211.  
  212.     printf ("@Document(Language ImPress, Name \"Sun 3 Rasterfile\"");
  213.     printf (", Jobheader On, Owner \"%s\"", getenv ("USER"));
  214.     printf (", Spooldate \"%s\")", cimptime);
  215.     /* SET_ABS_H */
  216.     putchar ((char) 135);
  217.     putchar (HIBYTE ((short) DOTS_PER_INCH / 4));
  218.     putchar (LOBYTE ((short) DOTS_PER_INCH / 4));
  219.  
  220.     /* SET_ABS_V */
  221.     putchar ((char) 137);
  222.     putchar (HIBYTE ((short) DOTS_PER_INCH));
  223.     putchar (LOBYTE ((short) DOTS_PER_INCH));
  224.  
  225.     /* SET_MAGNIFICATION */
  226.     putchar ((char) 236);
  227.     putchar ((char) MAG_POWER);
  228.  
  229.     /* BITMAP */
  230.     putchar ((char) 235);
  231.     putchar ((char) OPERATION_TYPE);
  232.     putchar ((char) h_patches);
  233.     putchar ((char) v_patches);
  234. }
  235.  
  236. write_imp_end ()
  237. {
  238.     /* ENDPAGE */
  239.     putchar ((char) 219);
  240.  
  241.     /* EOF */
  242.     putchar ((char) 255);
  243. }
  244.  
  245. /*
  246.  * error - type a message on error stream 
  247.  */
  248.  
  249. /* VARARGS1 */
  250. error (fmt, a1, a2, a3, a4, a5)
  251.     char *fmt;
  252. {
  253.     fprintf (stderr, "sunimp: ");
  254.     fprintf (stderr, fmt, a1, a2, a3, a4, a5);
  255.     fprintf (stderr, "\n");
  256. }
  257.  
  258.  
  259. /*
  260.  * fatal - type an error message and abort 
  261.  */
  262.  
  263. /* VARARGS1 */
  264. fatal (fmt, a1, a2, a3, a4, a5)
  265.     char *fmt;
  266. {
  267.     error (fmt, a1, a2, a3, a4, a5);
  268.     exit (1);
  269. }
  270.