home *** CD-ROM | disk | FTP | other *** search
- /*
- * suntops -- convert Sun rasterfile to PostScript image
- */
-
- #include <assert.h>
- #include <rasterfile.h>
- #include <stdio.h>
-
- #include "suntops.h"
-
- static void
- xch(a, b)
- double *a;
- double *b;
- {
- double temp;
-
- temp = *a;
- *a = *b;
- *b = temp;
- }
-
- void
- suntops(fpi, h_page, w_page, h_img, w_img, rotate, fpo)
- register FILE *fpi; /* rasterfile stdio pointer */
- double h_page; /* PostScript page height */
- double w_page; /* PostScript page width */
- double h_img; /* PostScript image height */
- double w_img; /* PostScript image width */
- int rotate; /* ? rotate output image 90 deg */
- register FILE *fpo; /* PostScript stdio pointer */
- {
- int hexdig[16]; /* hexadecimal digit map */
- double aspect; /* rasterfile aspect ratio */
- register int byte; /* current rasterfile data byte */
- register int *hexp; /* fast pointer to hexdig array */
- struct rasterfile ras; /* rasterfile header */
-
- /*
- * read rasterfile header
- */
- /* NOSTRICT */
- if (fread((char *) (&ras), sizeof(ras), 1, fpi) != 1) {
- error("can't read rasterfile header");
- }
-
- if (ras.ras_magic != RAS_MAGIC) {
- error("input is not a Sun rasterfile");
- }
- /*
- * compute rasterfile aspect ratio
- */
- if (rotate) {
- xch(&h_img, &w_img);
- xch(&h_page, &w_page);
- }
-
- aspect = ras.ras_width;
- aspect /= ras.ras_height;
- /*
- * adjust PostScript image size to preserve aspect ratio
- */
- if (aspect >= w_img / h_img) {
- h_img = w_img / aspect;
- }
- else {
- w_img = h_img * aspect;
- }
-
- assert(h_page >= h_img);
- assert(w_page >= w_img);
- /*
- * rasterfile lines are padded to a multiple of 16 bits (see rasterfile(5));
- * adjust rasterfile line length accordingly
- *
- * initialize integer -> hexadecimal digit mapping
- */
- switch (ras.ras_depth) {
-
- case 1:
- if (ras.ras_width % 16 != 0) {
- ras.ras_width += 16 - ras.ras_width % 16;
- }
- /*
- * Sun black/white bitmap convention is opposite of PostScript, so reverse
- * int->hex map
- */
- hexdig[0] = 'f';
- hexdig[1] = 'e';
- hexdig[2] = 'd';
- hexdig[3] = 'c';
- hexdig[4] = 'b';
- hexdig[5] = 'a';
- hexdig[6] = '9';
- hexdig[7] = '8';
- hexdig[8] = '7';
- hexdig[9] = '6';
- hexdig[10] = '5';
- hexdig[11] = '4';
- hexdig[12] = '3';
- hexdig[13] = '2';
- hexdig[14] = '1';
- hexdig[15] = '0';
-
- break;
-
- case 8:
- ras.ras_width += ras.ras_width & 1;
- /*
- * normal int->hex map
- */
- hexdig[0] = '0';
- hexdig[1] = '1';
- hexdig[2] = '2';
- hexdig[3] = '3';
- hexdig[4] = '4';
- hexdig[5] = '5';
- hexdig[6] = '6';
- hexdig[7] = '7';
- hexdig[8] = '8';
- hexdig[9] = '9';
- hexdig[10] = 'a';
- hexdig[11] = 'b';
- hexdig[12] = 'c';
- hexdig[13] = 'd';
- hexdig[14] = 'e';
- hexdig[15] = 'f';
-
- break;
-
- default:
- error("bad rasterfile depth");
- }
- /*
- * PostScript header
- */
- (void) printf("%%!\n");
- /*
- * if "rotate" set then rotate PostScript image 90 degrees
- */
- if (rotate) {
- (void) printf("90 rotate\n");
- (void) printf("0 -%d translate\n", ITOP(h_page));
- }
- /*
- * center the PostScript image on the output page
- */
- (void) printf("%d %d translate\n",
- ITOP(((w_page - w_img) / 2.0) + XTRANS_FUDGE),
- ITOP((h_page - h_img) / 2.0));
- (void) printf("%d %d scale\n", ITOP(w_img), ITOP(h_img));
- /*
- * I/O buffer for readhexstring
- */
- (void) printf("/linebuf %d string def\n",
- ras.ras_width / (8 / ras.ras_depth));
- /*
- * set PostScript image parameters
- */
- (void) printf("%ld %ld %d\n",
- ras.ras_width, ras.ras_height, ras.ras_depth);
- (void) printf("[%ld 0 0 -%ld 0 %ld]\n",
- ras.ras_width, ras.ras_height, ras.ras_height);
- (void) printf("{currentfile linebuf readhexstring pop}\n");
- /*
- * image data follows
- */
- (void) printf("image\n");
- /*
- * convert rasterfile bytes to pairs of ASCII hexadecimal digits
- */
- hexp = hexdig;
-
- while ((byte = getc(fpi)) != EOF) {
- putc(hexp[HI_NYBBLE(byte)], fpo);
- putc(hexp[LO_NYBBLE(byte)], fpo);
- }
-
- if (ferror(fpi)) {
- error("input error");
- }
-
- if (ferror(fpo)) {
- error("output error");
- }
- /*
- * display PostScript image on output device
- */
- (void) fprintf(fpo, "showpage\n");
- }
-