home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8709 / 5 / suntops.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-11  |  4.1 KB  |  191 lines

  1. /*
  2.  * suntops -- convert Sun rasterfile to PostScript image
  3.  */
  4.  
  5. #include <assert.h>
  6. #include <rasterfile.h>
  7. #include <stdio.h>
  8.  
  9. #include "suntops.h"
  10.  
  11. static void
  12. xch(a, b)
  13.     double         *a;
  14.     double         *b;
  15. {
  16.     double          temp;
  17.  
  18.     temp = *a;
  19.     *a = *b;
  20.     *b = temp;
  21. }
  22.  
  23. void
  24. suntops(fpi, h_page, w_page, h_img, w_img, rotate, fpo)
  25.     register FILE  *fpi;        /* rasterfile stdio pointer     */
  26.     double          h_page;        /* PostScript page height     */
  27.     double          w_page;        /* PostScript page width     */
  28.     double          h_img;        /* PostScript image height     */
  29.     double          w_img;        /* PostScript image width     */
  30.     int             rotate;        /* ? rotate output image 90 deg     */
  31.     register FILE  *fpo;        /* PostScript stdio pointer     */
  32. {
  33.     int             hexdig[16];    /* hexadecimal digit map     */
  34.     double          aspect;        /* rasterfile aspect ratio     */
  35.     register int    byte;        /* current rasterfile data byte     */
  36.     register int   *hexp;        /* fast pointer to hexdig array     */
  37.     struct rasterfile ras;        /* rasterfile header         */
  38.  
  39.  /*
  40.   * read rasterfile header
  41.   */
  42.  /* NOSTRICT */
  43.     if (fread((char *) (&ras), sizeof(ras), 1, fpi) != 1) {
  44.         error("can't read rasterfile header");
  45.     }
  46.  
  47.     if (ras.ras_magic != RAS_MAGIC) {
  48.         error("input is not a Sun rasterfile");
  49.     }
  50.  /*
  51.   * compute rasterfile aspect ratio
  52.   */
  53.     if (rotate) {
  54.         xch(&h_img, &w_img);
  55.         xch(&h_page, &w_page);
  56.     }
  57.  
  58.     aspect = ras.ras_width;
  59.     aspect /= ras.ras_height;
  60.  /*
  61.   * adjust PostScript image size to preserve aspect ratio
  62.   */
  63.     if (aspect >= w_img / h_img) {
  64.         h_img = w_img / aspect;
  65.     }
  66.     else {
  67.         w_img = h_img * aspect;
  68.     }
  69.  
  70.     assert(h_page >= h_img);
  71.     assert(w_page >= w_img);
  72.  /*
  73.   * rasterfile lines are padded to a multiple of 16 bits (see rasterfile(5));
  74.   * adjust rasterfile line length accordingly
  75.   * 
  76.   * initialize integer -> hexadecimal digit mapping
  77.   */
  78.     switch (ras.ras_depth) {
  79.  
  80.     case 1:
  81.         if (ras.ras_width % 16 != 0) {
  82.             ras.ras_width += 16 - ras.ras_width % 16;
  83.         }
  84.  /*
  85.   * Sun black/white bitmap convention is opposite of PostScript, so reverse
  86.   * int->hex map
  87.   */
  88.         hexdig[0] = 'f';
  89.         hexdig[1] = 'e';
  90.         hexdig[2] = 'd';
  91.         hexdig[3] = 'c';
  92.         hexdig[4] = 'b';
  93.         hexdig[5] = 'a';
  94.         hexdig[6] = '9';
  95.         hexdig[7] = '8';
  96.         hexdig[8] = '7';
  97.         hexdig[9] = '6';
  98.         hexdig[10] = '5';
  99.         hexdig[11] = '4';
  100.         hexdig[12] = '3';
  101.         hexdig[13] = '2';
  102.         hexdig[14] = '1';
  103.         hexdig[15] = '0';
  104.  
  105.         break;
  106.  
  107.     case 8:
  108.         ras.ras_width += ras.ras_width & 1;
  109.  /*
  110.   * normal int->hex map
  111.   */
  112.         hexdig[0] = '0';
  113.         hexdig[1] = '1';
  114.         hexdig[2] = '2';
  115.         hexdig[3] = '3';
  116.         hexdig[4] = '4';
  117.         hexdig[5] = '5';
  118.         hexdig[6] = '6';
  119.         hexdig[7] = '7';
  120.         hexdig[8] = '8';
  121.         hexdig[9] = '9';
  122.         hexdig[10] = 'a';
  123.         hexdig[11] = 'b';
  124.         hexdig[12] = 'c';
  125.         hexdig[13] = 'd';
  126.         hexdig[14] = 'e';
  127.         hexdig[15] = 'f';
  128.  
  129.         break;
  130.  
  131.     default:
  132.         error("bad rasterfile depth");
  133.     }
  134.  /*
  135.   * PostScript header
  136.   */
  137.     (void) printf("%%!\n");
  138.  /*
  139.   * if "rotate" set then rotate PostScript image 90 degrees
  140.   */
  141.     if (rotate) {
  142.         (void) printf("90 rotate\n");
  143.         (void) printf("0 -%d translate\n", ITOP(h_page));
  144.     }
  145.  /*
  146.   * center the PostScript image on the output page
  147.   */
  148.     (void) printf("%d %d translate\n",
  149.               ITOP(((w_page - w_img) / 2.0) + XTRANS_FUDGE),
  150.               ITOP((h_page - h_img) / 2.0));
  151.     (void) printf("%d %d scale\n", ITOP(w_img), ITOP(h_img));
  152.  /*
  153.   * I/O buffer for readhexstring
  154.   */
  155.     (void) printf("/linebuf %d string def\n",
  156.         ras.ras_width / (8 / ras.ras_depth));
  157.  /*
  158.   * set PostScript image parameters
  159.   */
  160.     (void) printf("%ld %ld %d\n",
  161.               ras.ras_width, ras.ras_height, ras.ras_depth);
  162.     (void) printf("[%ld 0 0 -%ld 0 %ld]\n",
  163.               ras.ras_width, ras.ras_height, ras.ras_height);
  164.     (void) printf("{currentfile linebuf readhexstring pop}\n");
  165.  /*
  166.   * image data follows
  167.   */
  168.     (void) printf("image\n");
  169.  /*
  170.   * convert rasterfile bytes to pairs of ASCII hexadecimal digits
  171.   */
  172.     hexp = hexdig;
  173.  
  174.     while ((byte = getc(fpi)) != EOF) {
  175.         putc(hexp[HI_NYBBLE(byte)], fpo);
  176.         putc(hexp[LO_NYBBLE(byte)], fpo);
  177.     }
  178.  
  179.     if (ferror(fpi)) {
  180.         error("input error");
  181.     }
  182.  
  183.     if (ferror(fpo)) {
  184.         error("output error");
  185.     }
  186.  /*
  187.   * display PostScript image on output device
  188.   */
  189.     (void) fprintf(fpo, "showpage\n");
  190. }
  191.