home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / libtiff / lbtif3_3.tar / tools / tiff2ps.c < prev    next >
C/C++ Source or Header  |  1993-08-26  |  18KB  |  682 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/tiff2ps.c,v 1.28 93/08/26 15:11:01 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1988, 1989, 1990, 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <math.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>            /* for atof */
  32. #include <time.h>
  33.  
  34. #include "tiffio.h"
  35.  
  36. /*
  37.  * NB: this code assumes uint32 works with printf's %l[ud].
  38.  */
  39. #define    TRUE    1
  40. #define    FALSE    0
  41.  
  42. int    level2 = FALSE;            /* generate PostScript level 2 */
  43. int    printAll = FALSE;        /* print all images in file */
  44. int    generateEPSF = TRUE;        /* generate Encapsulated PostScript */
  45. char    *filename;            /* input filename */
  46.  
  47. void    TIFF2PS(FILE*, TIFF*, float, float);
  48. void    PSpage(FILE*, TIFF*, uint32, uint32);
  49. void    PSColorContigPreamble(FILE*, uint32, uint32, int);
  50. void    PSColorSeparatePreamble(FILE*, uint32, uint32, int);
  51. void    PSDataColorContig(FILE*, TIFF*, uint32, uint32, int);
  52. void    PSDataColorSeparate(FILE*, TIFF*, uint32, uint32, int);
  53. void    PSDataPalette(FILE*, TIFF*, uint32, uint32);
  54. void    PSDataBW(FILE*, TIFF*, uint32, uint32);
  55. void    PSRawDataBW(FILE*, TIFF*, uint32, uint32);
  56.  
  57. static void
  58. usage(int code)
  59. {
  60.     fprintf(stderr, "Usage: tiff2ps %s %s %s %s file\n"
  61.         , "[-w inches]"
  62.         , "[-h inches]"
  63.         , "[-d dirnum]"
  64.         , "[-aeps2]"
  65.     );
  66.     exit(code);
  67. }
  68.  
  69. void
  70. main(int argc, char* argv[])
  71. {
  72.     int dirnum = -1, c;
  73.     TIFF *tif;
  74.     char *cp;
  75.     float pageWidth = 0;
  76.     float pageHeight = 0;
  77.     extern char *optarg;
  78.     extern int optind;
  79.  
  80.     while ((c = getopt(argc, argv, "h:w:d:aeps2")) != -1)
  81.         switch (c) {
  82.         case 'd':
  83.             dirnum = atoi(optarg);
  84.             break;
  85.         case 'e':
  86.             generateEPSF = TRUE;
  87.             break;
  88.         case 'h':
  89.             pageHeight = atof(optarg);
  90.             break;
  91.         case 'a':
  92.             printAll = TRUE;
  93.             /* fall thru... */
  94.         case 'p':
  95.             generateEPSF = FALSE;
  96.             break;
  97.         case 's':
  98.             printAll = FALSE;
  99.             break;
  100.         case 'w':
  101.             pageWidth = atof(optarg);
  102.             break;
  103.         case '2':
  104.             level2 = TRUE;
  105.             break;
  106.         case '?':
  107.             usage(-1);
  108.         }
  109.     if (argc - optind < 1)
  110.         usage(-2);
  111.     tif = TIFFOpen(filename = argv[optind], "r");
  112.     if (tif != NULL) {
  113.         if (dirnum != -1 && !TIFFSetDirectory(tif, dirnum))
  114.             exit(-1);
  115.         TIFF2PS(stdout, tif, pageWidth, pageHeight);
  116.     }
  117.     exit(0);
  118. }
  119.  
  120. static    uint16 samplesperpixel;
  121. static    uint16 bitspersample;
  122. static    uint16 planarconfiguration;
  123. static    uint16 photometric;
  124. static    uint16 extrasamples;
  125. static    int alpha;
  126.  
  127. static int
  128. checkImage(TIFF* tif)
  129. {
  130.     switch (bitspersample) {
  131.     case 1: case 2:
  132.     case 4: case 8:
  133.         break;
  134.     default:
  135.         TIFFError(filename, "Can not handle %d-bit/sample image",
  136.             bitspersample);
  137.         return (0);
  138.     }
  139.     switch (photometric) {
  140.     case PHOTOMETRIC_RGB:
  141.         if (alpha && bitspersample != 8) {
  142.             TIFFError(filename,
  143.                 "Can not handle %d-bit/sample RGB image with alpha",
  144.                 bitspersample);
  145.             return (0);
  146.         }
  147.         /* fall thru... */
  148.     case PHOTOMETRIC_SEPARATED:
  149.     case PHOTOMETRIC_PALETTE:
  150.     case PHOTOMETRIC_MINISBLACK:
  151.     case PHOTOMETRIC_MINISWHITE:
  152.         break;
  153.     default:
  154.         TIFFError(filename,
  155.             "Can not handle image with PhotometricInterpretation=%d",
  156.             photometric);
  157.         return (0);
  158.     }
  159.     if (planarconfiguration == PLANARCONFIG_SEPARATE && extrasamples > 0)
  160.         TIFFWarning(filename, "Ignoring extra samples");
  161.     return (1);
  162. }
  163.  
  164. #define PS_UNIT_SIZE    72.0
  165. #define    PSUNITS(npix,res)    ((npix) * (PS_UNIT_SIZE / (res)))
  166.  
  167. static    char RGBcolorimage[] = "\
  168. /bwproc {\n\
  169.     rgbproc\n\
  170.     dup length 3 idiv string 0 3 0\n\
  171.     5 -1 roll {\n\
  172.     add 2 1 roll 1 sub dup 0 eq {\n\
  173.         pop 3 idiv\n\
  174.         3 -1 roll\n\
  175.         dup 4 -1 roll\n\
  176.         dup 3 1 roll\n\
  177.         5 -1 roll put\n\
  178.         1 add 3 0\n\
  179.     } { 2 1 roll } ifelse\n\
  180.     } forall\n\
  181.     pop pop pop\n\
  182. } def\n\
  183. /colorimage where {pop} {\n\
  184.     /colorimage {pop pop /rgbproc exch def {bwproc} image} bind def\n\
  185. } ifelse\n\
  186. ";
  187.  
  188. /*
  189.  * Adobe Photoshop requires a comment line of the form:
  190.  *
  191.  * %ImageData: <cols> <rows> <depth>  <main channels> <pad channels>
  192.  *    <block size> <1 for binary|2 for hex> "data start"
  193.  *
  194.  * It is claimed to be part of some future revision of the EPS spec.
  195.  */
  196. static void
  197. PhotoshopBanner(FILE* fd, uint32 w, uint32 h, int bs, int nc, char* startline)
  198. {
  199.     fprintf(fd, "%%ImageData: %ld %ld %d %d 0 %d 2 \"",
  200.         w, h, bitspersample, nc, bs);
  201.     fprintf(fd, startline, nc);
  202.     fprintf(fd, "\"\n");
  203. }
  204.  
  205. static void
  206. setupPageState(TIFF* tif, uint32* pw, uint32* ph, float* pprw, float* pprh)
  207. {
  208.     uint16 res_unit;
  209.     float xres, yres;
  210.  
  211.     TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, pw);
  212.     TIFFGetField(tif, TIFFTAG_IMAGELENGTH, ph);
  213.     TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &res_unit);
  214.     /*
  215.      * Calculate printable area.
  216.      */
  217.     if (!TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres))
  218.         xres = PS_UNIT_SIZE;
  219.     else if (res_unit == RESUNIT_CENTIMETER)
  220.         xres /= 2.54;
  221.     *pprw = PSUNITS(*pw, xres);
  222.     if (!TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres))
  223.         yres = PS_UNIT_SIZE;
  224.     else if (res_unit == RESUNIT_CENTIMETER)
  225.         yres /= 2.54;
  226.     *pprh = PSUNITS(*ph, yres);
  227. }
  228.  
  229. static    tsize_t tf_bytesperrow;
  230. static    tsize_t ps_bytesperrow;
  231. static    char *hex = "0123456789abcdef";
  232.  
  233. void
  234. TIFF2PS(FILE* fd, TIFF* tif, float pw, float ph)
  235. {
  236.     uint32 w, h;
  237.     float ox, oy, prw, prh;
  238.     float psunit = PS_UNIT_SIZE;
  239.     uint32 subfiletype;
  240.     uint16* sampleinfo;
  241.     int npages;
  242.     long t;
  243.  
  244.     if (!TIFFGetField(tif, TIFFTAG_XPOSITION, &ox))
  245.         ox = 0;
  246.     if (!TIFFGetField(tif, TIFFTAG_YPOSITION, &oy))
  247.         oy = 0;
  248.     setupPageState(tif, &w, &h, &prw, &prh);
  249.  
  250.     t = time(0);
  251.     fprintf(fd, "%%!PS-Adobe-3.0%s\n", generateEPSF ? " EPSF-3.0" : "");
  252.     fprintf(fd, "%%%%Creator: tiff2ps\n");
  253.     fprintf(fd, "%%%%Title: %s\n", filename);
  254.     fprintf(fd, "%%%%CreationDate: %s", ctime(&t));
  255.     fprintf(fd, "%%%%Origin: %ld %ld\n", (long) ox, (long) oy);
  256.     /* NB: should use PageBoundingBox */
  257.     fprintf(fd, "%%%%BoundingBox: 0 0 %ld %ld\n",
  258.         (long) ceil(prw), (long) ceil(prh));
  259.     fprintf(fd, "%%%%Pages: (atend)\n");
  260.     fprintf(fd, "%%%%EndComments\n");
  261.     npages = 0;
  262.     do {
  263.         setupPageState(tif, &w, &h, &prw, &prh);
  264.         tf_bytesperrow = TIFFScanlineSize(tif);
  265.         TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE,
  266.             &bitspersample);
  267.         TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL,
  268.             &samplesperpixel);
  269.         TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &planarconfiguration);
  270.         TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric);
  271.         TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
  272.             &extrasamples, &sampleinfo);
  273.         alpha = (extrasamples == 1 &&
  274.              sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
  275.         if (checkImage(tif)) {
  276.             npages++;
  277.             fprintf(fd, "%%%%Page: %d %d\n", npages, npages);
  278.             fprintf(fd, "gsave\n");
  279.             fprintf(fd, "100 dict begin\n");
  280.             if (pw != 0 && ph != 0)
  281.                 fprintf(fd, "%f %f scale\n",
  282.                     pw*PS_UNIT_SIZE, ph*PS_UNIT_SIZE);
  283.             else
  284.                 fprintf(fd, "%f %f scale\n", prw, prh);
  285.             PSpage(fd, tif, w, h);
  286.             fprintf(fd, "end\n");
  287.             fprintf(fd, "grestore\n");
  288.             fprintf(fd, "showpage\n");
  289.         }
  290.         if (generateEPSF)
  291.             break;
  292.         TIFFGetFieldDefaulted(tif, TIFFTAG_SUBFILETYPE, &subfiletype);
  293.     } while (((subfiletype & FILETYPE_PAGE) || printAll) &&
  294.         TIFFReadDirectory(tif));
  295.     fprintf(fd, "%%%%Trailer\n");
  296.     fprintf(fd, "%%%%Pages: %u\n", npages);
  297.     fprintf(fd, "%%%%EOF\n");
  298. }
  299.  
  300. static int
  301. emitPSLevel2FilterFunction(FILE* fd, TIFF* tif, uint32 w, uint32 h)
  302. {
  303.     uint16 compression;
  304.     uint32 group3opts;
  305.     int K;
  306.  
  307.     TIFFGetField(tif, TIFFTAG_COMPRESSION, &compression);
  308. #define    P(a,b)    (((a)<<4)|((b)&0xf))
  309.     switch (P(compression, photometric)) {
  310.     case P(COMPRESSION_CCITTRLE, PHOTOMETRIC_MINISBLACK):
  311.     case P(COMPRESSION_CCITTRLE, PHOTOMETRIC_MINISWHITE):
  312.         K = 0;
  313.         break;
  314.     case P(COMPRESSION_CCITTFAX3, PHOTOMETRIC_MINISBLACK):
  315.     case P(COMPRESSION_CCITTFAX3, PHOTOMETRIC_MINISWHITE):
  316.         TIFFGetField(tif, TIFFTAG_GROUP3OPTIONS, &group3opts);
  317.         K = group3opts&GROUP3OPT_2DENCODING;
  318.         break;
  319.     case P(COMPRESSION_CCITTFAX4, PHOTOMETRIC_MINISBLACK):
  320.     case P(COMPRESSION_CCITTFAX4, PHOTOMETRIC_MINISWHITE):
  321.         K = -1;
  322.         break;
  323.     case P(COMPRESSION_LZW, PHOTOMETRIC_MINISBLACK):
  324.         fprintf(fd, "/LZWDecode filter dup 6 1 roll\n");
  325.         return (TRUE);
  326.     default:
  327.         return (FALSE);
  328.     }
  329. #undef P
  330.     fprintf(fd, "<<");
  331.     fprintf(fd, "/K %d", K);
  332.     fprintf(fd, " /Columns %d /Rows %d", w, h);
  333.     fprintf(fd, " /EndOfBlock false /BlackIs1 %s",
  334.         (photometric == PHOTOMETRIC_MINISBLACK) ? "true" : "false");
  335.     fprintf(fd, ">>\n/CCITTFaxDecode filter\n");
  336.     fprintf(fd, "dup 6 1 roll\n");
  337.     return (TRUE);
  338. }
  339.  
  340. void
  341. PSpage(FILE* fd, TIFF* tif, uint32 w, uint32 h)
  342. {
  343.     uint16 compression;
  344.     uint32 group3opts;
  345.     uint32 rowsperstrip;
  346.  
  347.     ps_bytesperrow = tf_bytesperrow;
  348.     switch (photometric) {
  349.     case PHOTOMETRIC_RGB:
  350.         if (planarconfiguration == PLANARCONFIG_CONTIG) {
  351.             fprintf(fd, "%s", RGBcolorimage);
  352.             PSColorContigPreamble(fd, w, h, 3);
  353.             PSDataColorContig(fd, tif, w, h, 3);
  354.         } else {
  355.             PSColorSeparatePreamble(fd, w, h, 3);
  356.             PSDataColorSeparate(fd, tif, w, h, 3);
  357.         }
  358.         break;
  359.     case PHOTOMETRIC_SEPARATED:
  360.         /* XXX should emit CMYKcolorimage */
  361.         if (planarconfiguration == PLANARCONFIG_CONTIG) {
  362.             PSColorContigPreamble(fd, w, h, 4);
  363.             PSDataColorContig(fd, tif, w, h, 4);
  364.         } else {
  365.             PSColorSeparatePreamble(fd, w, h, 4);
  366.             PSDataColorSeparate(fd, tif, w, h, 4);
  367.         }
  368.         break;
  369.     case PHOTOMETRIC_PALETTE:
  370.         fprintf(fd, "%s", RGBcolorimage);
  371.         PhotoshopBanner(fd, w, h, 1, 3, "false 3 colorimage");
  372.         fprintf(fd, "/scanLine %d string def\n", ps_bytesperrow);
  373.         fprintf(fd, "%lu %lu 8\n", w, h);
  374.         fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n", w, h, h);
  375.         fprintf(fd, "{currentfile scanLine readhexstring pop} bind\n");
  376.         fprintf(fd, "false 3 colorimage\n");
  377.         PSDataPalette(fd, tif, w, h);
  378.         break;
  379.     case PHOTOMETRIC_MINISBLACK:
  380.     case PHOTOMETRIC_MINISWHITE:
  381.         PhotoshopBanner(fd, w, h, 1, 1, "image");
  382.         if (level2) {
  383.             int rawdata;
  384.             fprintf(fd, "%lu %lu %d\n", w, h, bitspersample);
  385.             fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n", w, h, h);
  386.             fprintf(fd, "currentfile /ASCIIHexDecode filter\n");
  387.             fprintf(fd, "dup 6 1 roll\n");
  388.             rawdata = emitPSLevel2FilterFunction(fd, tif, w, h);
  389.             fprintf(fd, "{image flushfile flushfile} cvx exec\n");
  390.             if (rawdata)
  391.                 PSRawDataBW(fd, tif, w, h);
  392.             else
  393.                 PSDataBW(fd, tif, w, h);
  394.             putc('>', fd);
  395.         } else {
  396.             fprintf(fd, "/scanLine %d string def\n",ps_bytesperrow);
  397.             fprintf(fd, "%lu %lu %d\n", w, h, bitspersample);
  398.             fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n", w, h, h);
  399.             fprintf(fd,
  400.                 "{currentfile scanLine readhexstring pop} bind\n");
  401.             fprintf(fd, "image\n");
  402.             PSDataBW(fd, tif, w, h);
  403.         }
  404.         break;
  405.     }
  406.     putc('\n', fd);
  407. }
  408.  
  409. void
  410. PSColorContigPreamble(FILE* fd, uint32 w, uint32 h, int nc)
  411. {
  412.     ps_bytesperrow = nc * (tf_bytesperrow / samplesperpixel);
  413.     PhotoshopBanner(fd, w, h, 1, nc, "false %d colorimage");
  414.     fprintf(fd, "/line %d string def\n", ps_bytesperrow);
  415.     fprintf(fd, "%lu %lu %d\n", w, h, bitspersample);
  416.     fprintf(fd, "[%lu 0 0 -%lu 0 %lu]\n", w, h, h);
  417.     fprintf(fd, "{currentfile line readhexstring pop} bind\n");
  418.     fprintf(fd, "false %d colorimage\n", nc);
  419. }
  420.  
  421. void
  422. PSColorSeparatePreamble(FILE* fd, uint32 w, uint32 h, int nc)
  423. {
  424.     int i;
  425.  
  426.     PhotoshopBanner(fd, w, h, ps_bytesperrow, nc, "true %d colorimage");
  427.     for (i = 0; i < nc; i++)
  428.         fprintf(fd, "/line%d %d string def\n", i, ps_bytesperrow);
  429.     fprintf(fd, "%lu %lu %d\n", w, h, bitspersample);
  430.     fprintf(fd, "[%lu 0 0 -%lu 0 %lu] \n", w, h, h);
  431.     for (i = 0; i < nc; i++)
  432.         fprintf(fd, "{currentfile line%d readhexstring pop}bind\n", i);
  433.     fprintf(fd, "true %d colorimage\n", nc);
  434. }
  435.  
  436. #define MAXLINE        36
  437. #define    DOBREAK(len, howmany, fd) \
  438.     if (((len) -= (howmany)) <= 0) {    \
  439.         putc('\n', fd);            \
  440.         (len) = MAXLINE-(howmany);    \
  441.     }
  442. #define    PUTHEX(c,fd)    putc(hex[((c)>>4)&0xf],fd); putc(hex[(c)&0xf],fd)
  443.  
  444. void
  445. PSDataColorContig(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc)
  446. {
  447.     uint32 row;
  448.     int breaklen = MAXLINE, cc, es = samplesperpixel - nc;
  449.     unsigned char *tf_buf;
  450.     unsigned char *cp, c;
  451.  
  452.     tf_buf = (unsigned char *) malloc(tf_bytesperrow);
  453.     if (tf_buf == NULL) {
  454.         TIFFError(filename, "No space for scanline buffer");
  455.         return;
  456.     }
  457.     for (row = 0; row < h; row++) {
  458.         if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
  459.             break;
  460.         cp = tf_buf;
  461.         if (alpha) {
  462.             int adjust;
  463.             cc = 0;
  464.             for (; cc < tf_bytesperrow; cc += samplesperpixel) {
  465.                 DOBREAK(breaklen, nc, fd);
  466.                 /*
  467.                  * For images with alpha, matte against
  468.                  * a white background; i.e.
  469.                  *    Cback * (1 - Aimage)
  470.                  * where Cback = 1.
  471.                  */
  472.                 adjust = 255 - cp[nc];
  473.                 switch (nc) {
  474.                 case 4: c = *cp++ + adjust; PUTHEX(c,fd);
  475.                 case 3: c = *cp++ + adjust; PUTHEX(c,fd);
  476.                 case 2: c = *cp++ + adjust; PUTHEX(c,fd);
  477.                 case 1: c = *cp++ + adjust; PUTHEX(c,fd);
  478.                 }
  479.                 cp += es;
  480.             }
  481.         } else {
  482.             cc = 0;
  483.             for (; cc < tf_bytesperrow; cc += samplesperpixel) {
  484.                 DOBREAK(breaklen, nc, fd);
  485.                 switch (nc) {
  486.                 case 4: c = *cp++; PUTHEX(c,fd);
  487.                 case 3: c = *cp++; PUTHEX(c,fd);
  488.                 case 2: c = *cp++; PUTHEX(c,fd);
  489.                 case 1: c = *cp++; PUTHEX(c,fd);
  490.                 }
  491.                 cp += es;
  492.             }
  493.         }
  494.     }
  495.     free((char *) tf_buf);
  496. }
  497.  
  498. void
  499. PSDataColorSeparate(FILE* fd, TIFF* tif, uint32 w, uint32 h, int nc)
  500. {
  501.     uint32 row;
  502.     int breaklen = MAXLINE, cc, s, maxs;
  503.     unsigned char *tf_buf;
  504.     unsigned char *cp, c;
  505.  
  506.     tf_buf = (unsigned char *) malloc(tf_bytesperrow);
  507.     if (tf_buf == NULL) {
  508.         TIFFError(filename, "No space for scanline buffer");
  509.         return;
  510.     }
  511.     maxs = (samplesperpixel > nc ? nc : samplesperpixel);
  512.     for (row = 0; row < h; row++) {
  513.         for (s = 0; s < maxs; s++) {
  514.             if (TIFFReadScanline(tif, tf_buf, row, s) < 0)
  515.                 break;
  516.             for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) {
  517.                 DOBREAK(breaklen, 1, fd);
  518.                 c = *cp++;
  519.                 PUTHEX(c,fd);
  520.             }
  521.         }
  522.     }
  523.     free((char *) tf_buf);
  524. }
  525.  
  526. #define    PUTRGBHEX(c,fd) \
  527.     PUTHEX(rmap[c],fd); PUTHEX(gmap[c],fd); PUTHEX(bmap[c],fd)
  528.  
  529. static int
  530. checkcmap(TIFF* tif, int n, uint16* r, uint16* g, uint16* b)
  531. {
  532.     while (n-- > 0)
  533.         if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
  534.             return (16);
  535.     TIFFWarning(filename, "Assuming 8-bit colormap");
  536.     return (8);
  537. }
  538.  
  539. void
  540. PSDataPalette(FILE* fd, TIFF* tif, uint32 w, uint32 h)
  541. {
  542.     uint16 *rmap, *gmap, *bmap;
  543.     uint32 row;
  544.     int breaklen = MAXLINE, cc, nc;
  545.     unsigned char *tf_buf;
  546.     unsigned char *cp, c;
  547.  
  548.     if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {
  549.         TIFFError(filename, "Palette image w/o \"Colormap\" tag");
  550.         return;
  551.     }
  552.     switch (bitspersample) {
  553.     case 8:    case 4: case 2: case 1:
  554.         break;
  555.     default:
  556.         TIFFError(filename, "Depth %d not supported", bitspersample);
  557.         return;
  558.     }
  559.     nc = 3 * (8 / bitspersample);
  560.     tf_buf = (unsigned char *) malloc(tf_bytesperrow);
  561.     if (tf_buf == NULL) {
  562.         TIFFError(filename, "No space for scanline buffer");
  563.         return;
  564.     }
  565.     if (checkcmap(tif, 1<<bitspersample, rmap, gmap, bmap) == 16) {
  566.         int i;
  567. #define    CVT(x)        (((x) * 255) / ((1L<<16)-1))
  568.         for (i = (1<<bitspersample)-1; i > 0; i--) {
  569.             rmap[i] = CVT(rmap[i]);
  570.             gmap[i] = CVT(gmap[i]);
  571.             bmap[i] = CVT(bmap[i]);
  572.         }
  573. #undef CVT
  574.     }
  575.     for (row = 0; row < h; row++) {
  576.         if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
  577.             break;
  578.         for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) {
  579.             DOBREAK(breaklen, nc, fd);
  580.             switch (bitspersample) {
  581.             case 8:
  582.                 c = *cp++; PUTRGBHEX(c, fd);
  583.                 break;
  584.             case 4:
  585.                 c = *cp++; PUTRGBHEX(c&0xf, fd);
  586.                 c >>= 4;   PUTRGBHEX(c, fd);
  587.                 break;
  588.             case 2:
  589.                 c = *cp++; PUTRGBHEX(c&0x3, fd);
  590.                 c >>= 2;   PUTRGBHEX(c&0x3, fd);
  591.                 c >>= 2;   PUTRGBHEX(c&0x3, fd);
  592.                 c >>= 2;   PUTRGBHEX(c, fd);
  593.                 break;
  594.             case 1:
  595.                 c = *cp++; PUTRGBHEX(c&0x1, fd);
  596.                 c >>= 1;   PUTRGBHEX(c&0x1, fd);
  597.                 c >>= 1;   PUTRGBHEX(c&0x1, fd);
  598.                 c >>= 1;   PUTRGBHEX(c&0x1, fd);
  599.                 c >>= 1;   PUTRGBHEX(c&0x1, fd);
  600.                 c >>= 1;   PUTRGBHEX(c&0x1, fd);
  601.                 c >>= 1;   PUTRGBHEX(c&0x1, fd);
  602.                 c >>= 1;   PUTRGBHEX(c, fd);
  603.                 break;
  604.             }
  605.         }
  606.     }
  607.     free((char *) tf_buf);
  608. }
  609.  
  610. void
  611. PSDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h)
  612. {
  613.     uint32 row;
  614.     int breaklen = MAXLINE, cc;
  615.     unsigned char *tf_buf;
  616.     unsigned char *cp, c;
  617.  
  618.     tf_buf = (unsigned char *) malloc(tf_bytesperrow);
  619.     if (tf_buf == NULL) {
  620.         TIFFError(filename, "No space for scanline buffer");
  621.         return;
  622.     }
  623.     for (row = 0; row < h; row++) {
  624.         if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
  625.             break;
  626.         for (cp = tf_buf, cc = 0; cc < tf_bytesperrow; cc++) {
  627.             DOBREAK(breaklen, 1, fd);
  628.             c = *cp++;
  629.             if (photometric == PHOTOMETRIC_MINISWHITE)
  630.                 c = ~c;
  631.             PUTHEX(c, fd);
  632.         }
  633.     }
  634.     free((char *) tf_buf);
  635. }
  636.  
  637. void
  638. PSRawDataBW(FILE* fd, TIFF* tif, uint32 w, uint32 h)
  639. {
  640.     uint32 *bc;
  641.     uint32 bufsize;
  642.     int breaklen = MAXLINE, cc;
  643.     uint16 fillorder;
  644.     unsigned char *tf_buf;
  645.     unsigned char *cp, c;
  646.     tstrip_t s;
  647.  
  648.     TIFFGetField(tif, TIFFTAG_FILLORDER, &fillorder);
  649.     TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);
  650.     bufsize = bc[0];
  651.     tf_buf = (unsigned char*) malloc(bufsize);
  652.     if (tf_buf == NULL) {
  653.         TIFFError(filename, "No space for strip buffer");
  654.         return;
  655.     }
  656.     for (s = 0; s < TIFFNumberOfStrips(tif); s++) {
  657.         if (bc[s] > bufsize) {
  658.             tf_buf = (unsigned char *) realloc(tf_buf, bc[0]);
  659.             if (tf_buf == NULL) {
  660.                 TIFFError(filename,
  661.                     "No space for strip buffer");
  662.                 return;
  663.             }
  664.             bufsize = bc[0];
  665.         }
  666.         cc = TIFFReadRawStrip(tif, s, tf_buf, bc[s]);
  667.         if (cc < 0) {
  668.             TIFFError(filename, "Can't read strip");
  669.             free(tf_buf);
  670.             return;
  671.         }
  672.         if (fillorder == FILLORDER_LSB2MSB)
  673.             TIFFReverseBits(tf_buf, cc);
  674.         for (cp = tf_buf; cc > 0; cc--) {
  675.             DOBREAK(breaklen, 1, fd);
  676.             c = *cp++;
  677.             PUTHEX(c, fd);
  678.         }
  679.     }
  680.     free((char *) tf_buf);
  681. }
  682.