44 #include <glib/gstdio.h>
106 #define EXP_ASCII85ENCODER
162 double,
double,
double,
double);
165 #if defined( EXP_ASCII85ENCODER )
167 unsigned f_eod,
const uint8 * raw_p,
int raw_l);
170 #define IMAGEOP(ctx) ((ctx)->useImagemask && ((ctx)->bitspersample == 1)) ? "imagemask" : "image"
178 if (ctx->
fd == NULL) {
199 case PHOTOMETRIC_YCBCR:
204 TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE,
210 TIFFError(ctx->
filename,
"Can not handle image with %s",
211 "Ctx->PhotometricInterpretation=YCbCr");
215 case PHOTOMETRIC_RGB:
218 "Can not handle %d-bit/sample RGB image with ctx->alpha",
223 case PHOTOMETRIC_SEPARATED:
224 case PHOTOMETRIC_PALETTE:
225 case PHOTOMETRIC_MINISBLACK:
226 case PHOTOMETRIC_MINISWHITE:
228 case PHOTOMETRIC_LOGL:
229 case PHOTOMETRIC_LOGLUV:
233 "Can not handle %s data with ctx->compression other than SGILog",
240 TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
242 PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB;
245 case PHOTOMETRIC_CIELAB:
249 "Can not handle image with Ctx->PhotometricInterpretation=%d",
258 TIFFError(ctx->
filename,
"Can not handle %d-bit/sample image",
264 TIFFWarning(ctx->
filename,
"Ignoring extra samples");
268 #define PS_UNIT_SIZE 72.0F
269 #define PSUNITS(npix,res) ((npix) * (PS_UNIT_SIZE / (res)))
274 dup length 3 idiv string 0 3 0\n\
276 add 2 1 roll 1 sub dup 0 eq {\n\
283 } { 2 1 roll } ifelse\n\
287 /colorimage where {pop} {\n\
288 /colorimage {pop pop /rgbproc exch def {bwproc} image} bind def\n\
301 PhotoshopBanner(
TIFF2PSContext* ctx, uint32 w, uint32 h,
int bs,
int nc,
302 const
char* startline, ...)
305 fprintf(ctx->fd,
"%%ImageData: %ld %ld %d %d 0 %d 2 \"",
306 (
long) w, (
long) h, ctx->bitspersample, nc, bs);
308 va_start(args, startline);
309 vfprintf(ctx->fd, startline, args);
312 fprintf(ctx->fd,
"\"\n");
323 double* pprw,
double* pprh)
325 float xres = 0.0F, yres = 0.0F;
327 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, pw);
328 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, ph);
330 TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &ctx->
res_unit);
334 if (!TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres)
335 || fabs(xres) < 0.0000001)
337 if (!TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres)
338 || fabs(yres) < 0.0000001)
341 case RESUNIT_CENTIMETER:
342 xres *= 2.54F, yres *= 2.54F;
359 TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
360 return (compress == COMPRESSION_CCITTFAX3 ||
361 compress == COMPRESSION_CCITTFAX4 ||
362 compress == COMPRESSION_CCITTRLE ||
363 compress == COMPRESSION_CCITTRLEW);
366 static char *
hex =
"0123456789abcdef";
374 double imagewidth,
double imageheight,
int splitpage,
375 double lm,
double bm,
int cnt)
383 double subimageheight;
387 gchar buf[2][G_ASCII_DTOSTR_BUF_SIZE];
403 if (imagewidth <= pagewidth) {
413 if (imageheight <= pageheight) {
414 yscale = imageheight;
415 ytran = pageheight - imageheight;
416 }
else if (imageheight > pageheight &&
417 (splitheight == 0 || imageheight <= splitheight)) {
420 subimageheight = imageheight - (pageheight-overlap)*splitpage;
421 if (subimageheight <= pageheight) {
422 yscale = imageheight;
423 ytran = pageheight - subimageheight;
425 }
else if ( subimageheight > pageheight && subimageheight <= splitheight) {
426 yscale = imageheight * pageheight / subimageheight;
430 yscale = imageheight;
431 ytran = pageheight - subimageheight;
436 bottom_offset += ytran / (cnt?2:1);
438 left_offset += xtran / 2;
440 fprintf(ctx->
fd,
"%s %s translate\n",
441 g_ascii_dtostr(buf[0],
sizeof(buf[0]), left_offset),
442 g_ascii_dtostr(buf[1],
sizeof(buf[1]), bottom_offset));
443 fprintf(ctx->
fd,
"%s %s scale\n",
444 g_ascii_dtostr(buf[0],
sizeof(buf[0]), xscale),
445 g_ascii_dtostr(buf[1],
sizeof(buf[1]), yscale));
447 fputs (
"1 1 translate 180 ctx->rotate\n", ctx->
fd);
455 double lm,
double bm, gboolean cnt)
466 gchar buf[2][G_ASCII_DTOSTR_BUF_SIZE];
468 if (!TIFFGetField(tif, TIFFTAG_XPOSITION, &ox))
470 if (!TIFFGetField(tif, TIFFTAG_YPOSITION, &oy))
475 TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP,
479 PSHead(ctx, tif, w, h, prw, prh, ox, oy);
480 TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE,
482 TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL,
484 TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG,
486 TIFFGetField(tif, TIFFTAG_COMPRESSION, &ctx->
compression);
487 TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
490 sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
491 if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &ctx->
photometric)) {
510 fprintf(ctx->
fd,
"%%%%Page: %d %d\n", ctx->
npages,
513 double psw = 0.0, psh = 0.0;
516 if (ctx->
res_unit == RESUNIT_CENTIMETER)
519 psw=ctx->
rotate ? prh:prw;
522 if (ctx->
res_unit == RESUNIT_CENTIMETER)
525 psh=ctx->
rotate ? prw:prh;
527 "1 dict begin /PageSize [ %s %s ] def currentdict end setpagedevice\n",
528 g_ascii_dtostr(buf[0],
sizeof(buf[0]), psw),
529 g_ascii_dtostr(buf[1],
sizeof(buf[1]), psh));
531 "<<\n /Policies <<\n /PageSize 3\n >>\n>> setpagedevice\n",
534 fprintf(ctx->
fd,
"gsave\n");
535 fprintf(ctx->
fd,
"100 dict begin\n");
536 if (pw != 0 || ph != 0) {
546 fprintf(ctx->
fd,
"end\n");
547 fprintf(ctx->
fd,
"grestore\n");
548 fprintf(ctx->
fd,
"showpage\n");
550 fprintf(ctx->
fd,
"%%%%Page: %d %d\n",
552 fprintf(ctx->
fd,
"gsave\n");
553 fprintf(ctx->
fd,
"100 dict begin\n");
562 scale = pw/prw < ph/prh ?
568 (ph - prh * scale) / 2;
570 (pw - prw * scale) / 2;
572 fprintf(ctx->
fd,
"%s %s translate\n",
573 g_ascii_dtostr(buf[0],
sizeof(buf[0]), left_offset),
574 g_ascii_dtostr(buf[1],
sizeof(buf[1]), bottom_offset));
575 fprintf(ctx->
fd,
"%s %s scale\n",
576 g_ascii_dtostr(buf[0],
sizeof(buf[0]), prw * scale),
577 g_ascii_dtostr(buf[1],
sizeof(buf[1]), prh * scale));
579 fputs (
"1 1 translate 180 ctx->rotate\n", ctx->
fd);
582 fprintf(ctx->
fd,
"%s %s scale\n",
583 g_ascii_dtostr(buf[0],
sizeof(buf[0]), prw),
584 g_ascii_dtostr(buf[1],
sizeof(buf[1]), prh));
586 fputs (
"1 1 translate 180 ctx->rotate\n", ctx->
fd);
589 fprintf(ctx->
fd,
"end\n");
590 fprintf(ctx->
fd,
"grestore\n");
591 fprintf(ctx->
fd,
"showpage\n");
597 %%BeginFeature: *Duplex True\n\
599 /languagelevel where { pop languagelevel } { 1 } ifelse\n\
600 2 ge { 1 dict dup /Duplex true put setpagedevice }\n\
601 { statusdict /setduplex known { statusdict begin setduplex true end } if\n\
608 %%BeginFeature: *Tumble True\n\
610 /languagelevel where { pop languagelevel } { 1 } ifelse\n\
611 2 ge { 1 dict dup /Tumble true put setpagedevice }\n\
612 { statusdict /settumble known { statusdict begin true settumble end } if\n\
619 gsave newpath clippath pathbbox grestore\n\
620 4 2 roll 2 copy translate\n\
621 exch 3 1 roll sub 3 1 roll sub exch\n\
622 currentpagedevice /PageSize get aload pop\n\
623 exch 3 1 roll div 3 1 roll div abs exch abs\n\
624 2 copy gt { exch } if pop\n\
625 dup 1 lt { dup scale } { pop } ifelse\n\
630 double pw,
double ph,
double ox,
double oy)
634 (void) tif; (void) w; (void) h;
636 fprintf(ctx->
fd,
"%%!PS-Adobe-3.0%s\n",
638 fprintf(ctx->
fd,
"%%%%Creator: Evince\n");
639 fprintf(ctx->
fd,
"%%%%CreationDate: %s", ctime(&t));
640 fprintf(ctx->
fd,
"%%%%DocumentData: Clean7Bit\n");
641 fprintf(ctx->
fd,
"%%%%Origin: %ld %ld\n", (
long) ox, (
long) oy);
643 fprintf(ctx->
fd,
"%%%%BoundingBox: 0 0 %ld %ld\n",
644 (
long) ceil(pw), (
long) ceil(ph));
645 fprintf(ctx->
fd,
"%%%%LanguageLevel: %d\n",
647 fprintf(ctx->
fd,
"%%%%Pages: (atend)\n");
648 fprintf(ctx->
fd,
"%%%%EndComments\n");
649 fprintf(ctx->
fd,
"%%%%BeginSetup\n");
656 fprintf(ctx->
fd,
"%%%%EndSetup\n");
664 fprintf(ctx->
fd,
"%%%%Trailer\n");
665 fprintf(ctx->
fd,
"%%%%Pages: %d\n", ctx->
npages);
666 fprintf(ctx->
fd,
"%%%%EOF\n");
671 uint16* r, uint16* g, uint16* b)
675 if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
677 TIFFWarning(ctx->
filename,
"Assuming 8-bit colormap");
684 uint16 *rmap, *gmap, *bmap;
686 const char * colorspace_p;
690 case PHOTOMETRIC_SEPARATED:
691 colorspace_p =
"CMYK";
694 case PHOTOMETRIC_RGB:
695 colorspace_p =
"RGB";
699 colorspace_p =
"Gray";
706 fputs(
"% PostScript Level 2 only.\n", ctx->
fd);
711 fprintf(ctx->
fd,
"/Device%s setcolorspace\n", colorspace_p );
719 if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {
721 "Palette image w/o \"Colormap\" tag");
724 if (
checkcmap(ctx, tif, num_colors, rmap, gmap, bmap) == 16) {
728 #define CVT(x) (((x) * 255) / ((1L<<16)-1))
729 for (i = 0; i < num_colors; i++) {
730 rmap[i] =
CVT(rmap[i]);
731 gmap[i] =
CVT(gmap[i]);
732 bmap[i] =
CVT(bmap[i]);
736 fprintf(ctx->
fd,
"[ /Indexed /DeviceRGB %d", num_colors - 1);
739 fputs(
"\n<~", ctx->
fd);
742 fputs(
" <", ctx->
fd);
743 for (i = 0; i < num_colors; i++) {
749 fputs((i % 8) ?
" " :
"\n ", ctx->
fd);
750 fprintf(ctx->
fd,
"%02x%02x%02x",
751 rmap[i], gmap[i], bmap[i]);
757 fputs(
">\n", ctx->
fd);
758 fputs(
"] setcolorspace\n", ctx->
fd);
765 uint32 tile_width, tile_height;
766 uint16 predictor, minsamplevalue, maxsamplevalue;
768 char im_h[64], im_x[64], im_y[64];
770 (void)strcpy(im_x,
"0");
771 (void)sprintf(im_y,
"%lu", (
long) h);
772 (void)sprintf(im_h,
"%lu", (
long) h);
775 if (TIFFIsTiled(tif)) {
776 repeat_count = TIFFNumberOfTiles(tif);
777 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width);
778 TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height);
779 if (tile_width > w || tile_height > h ||
780 (w % tile_width) != 0 || (h % tile_height != 0)) {
785 fputs(
"0 0 1 1 rectclip\n", ctx->
fd);
787 if (tile_width < w) {
788 fputs(
"/im_x 0 def\n", ctx->
fd);
789 (void)strcpy(im_x,
"im_x neg");
791 if (tile_height < h) {
792 fputs(
"/im_y 0 def\n", ctx->
fd);
793 (void)sprintf(im_y,
"%lu im_y sub", (
unsigned long) h);
800 if (repeat_count > 1) {
801 fputs(
"/im_y 0 def\n", ctx->
fd);
802 fprintf(ctx->
fd,
"/im_h %lu def\n",
803 (
unsigned long) tile_height);
804 (void)strcpy(im_h,
"im_h");
805 (void)sprintf(im_y,
"%lu im_y sub", (
unsigned long) h);
812 fputs(
"{ % exec\n", ctx->
fd);
814 if (repeat_count > 1)
815 fprintf(ctx->
fd,
"%d { %% repeat\n", repeat_count);
821 fputs(
" /im_stream currentfile /ASCII85Decode filter def\n",
823 fputs(
" <<\n", ctx->
fd);
824 fputs(
" /ImageType 1\n", ctx->
fd);
825 fprintf(ctx->
fd,
" /Width %lu\n", (
unsigned long) tile_width);
837 fprintf(ctx->
fd,
" /Height %lu\n", (
unsigned long) tile_height);
839 fprintf(ctx->
fd,
" /Height im_h\n");
842 fputs(
" /MultipleDataSources true\n", ctx->
fd);
843 fprintf(ctx->
fd,
" /ImageMatrix [ %lu 0 0 %ld %s %s ]\n",
844 (
unsigned long) w, - (
long)h, im_x, im_y);
846 fprintf(ctx->
fd,
" /Ctx->Interpolate %s\n", ctx->
interpolate ?
"true" :
"false");
851 case PHOTOMETRIC_MINISBLACK:
852 fputs(
" /Decode [0 1]\n", ctx->
fd);
854 case PHOTOMETRIC_MINISWHITE:
856 case COMPRESSION_CCITTRLE:
857 case COMPRESSION_CCITTRLEW:
858 case COMPRESSION_CCITTFAX3:
859 case COMPRESSION_CCITTFAX4:
864 fputs(
" /Decode [0 1]\n", ctx->
fd);
870 fputs(
" /Decode [1 0]\n", ctx->
fd);
874 case PHOTOMETRIC_PALETTE:
875 TIFFGetFieldDefaulted(tif, TIFFTAG_MINSAMPLEVALUE,
877 TIFFGetFieldDefaulted(tif, TIFFTAG_MAXSAMPLEVALUE,
879 fprintf(ctx->
fd,
" /Decode [%u %u]\n",
880 minsamplevalue, maxsamplevalue);
886 fputs(
" /Decode [0 1]\n", ctx->
fd);
892 case PHOTOMETRIC_RGB:
893 fputs(
" /Decode [0 1 0 1 0 1]\n", ctx->
fd);
895 case PHOTOMETRIC_MINISWHITE:
896 case PHOTOMETRIC_MINISBLACK:
901 fputs(
" /Decode [0 1 0 1 0 1]\n", ctx->
fd);
909 fputs(
" /Decode [0 1 0 1 0 1 0 1]\n", ctx->
fd);
912 fputs(
" /DataSource", ctx->
fd);
915 fputs(
" [", ctx->
fd);
917 fputs(
" im_stream", ctx->
fd);
919 fputs(
" currentfile /ASCIIHexDecode filter", ctx->
fd);
923 case COMPRESSION_NONE:
925 case COMPRESSION_CCITTRLE:
926 case COMPRESSION_CCITTRLEW:
927 case COMPRESSION_CCITTFAX3:
928 case COMPRESSION_CCITTFAX4:
929 fputs(
"\n\t<<\n", ctx->
fd);
933 fputs(
"\t /EndOfLine true\n", ctx->
fd);
934 fputs(
"\t /EndOfBlock false\n", ctx->
fd);
935 if (!TIFFGetField(tif, TIFFTAG_GROUP3OPTIONS,
938 if (g3_options & GROUP3OPT_2DENCODING)
939 fprintf(ctx->
fd,
"\t /K %s\n", im_h);
940 if (g3_options & GROUP3OPT_UNCOMPRESSED)
941 fputs(
"\t /Uncompressed true\n", ctx->
fd);
942 if (g3_options & GROUP3OPT_FILLBITS)
943 fputs(
"\t /EncodedByteAlign true\n", ctx->
fd);
948 fputs(
"\t /K -1\n", ctx->
fd);
949 TIFFGetFieldDefaulted(tif, TIFFTAG_GROUP4OPTIONS,
951 if (g4_options & GROUP4OPT_UNCOMPRESSED)
952 fputs(
"\t /Uncompressed true\n", ctx->
fd);
954 if (!(tile_width == w && w == 1728U))
955 fprintf(ctx->
fd,
"\t /Columns %lu\n",
956 (
unsigned long) tile_width);
957 fprintf(ctx->
fd,
"\t /Rows %s\n", im_h);
960 fputs(
"\t /EncodedByteAlign true\n", ctx->
fd);
961 fputs(
"\t /EndOfBlock false\n", ctx->
fd);
964 fputs(
"\t /BlackIs1 true\n", ctx->
fd);
965 fprintf(ctx->
fd,
"\t>> /CCITTFaxDecode filter");
967 case COMPRESSION_LZW:
968 TIFFGetFieldDefaulted(tif, TIFFTAG_PREDICTOR, &predictor);
969 if (predictor == 2) {
970 fputs(
"\n\t<<\n", ctx->
fd);
971 fprintf(ctx->
fd,
"\t /Predictor %u\n", predictor);
972 fprintf(ctx->
fd,
"\t /Columns %lu\n",
973 (
unsigned long) tile_width);
975 fprintf(ctx->
fd,
"\t /BitsPerComponent %u\n",
977 fputs(
"\t>>", ctx->
fd);
979 fputs(
" /LZWDecode filter", ctx->
fd);
981 case COMPRESSION_DEFLATE:
982 case COMPRESSION_ADOBE_DEFLATE:
984 TIFFGetFieldDefaulted(tif, TIFFTAG_PREDICTOR, &predictor);
986 fprintf(ctx->
fd,
"\t %% PostScript Level 3 only.");
987 fputs(
"\n\t<<\n", ctx->
fd);
988 fprintf(ctx->
fd,
"\t /Predictor %u\n", predictor);
989 fprintf(ctx->
fd,
"\t /Columns %lu\n",
990 (
unsigned long) tile_width);
992 fprintf(ctx->
fd,
"\t /BitsPerComponent %u\n",
994 fputs(
"\t>>", ctx->
fd);
996 fputs(
" /FlateDecode filter", ctx->
fd);
998 use_rawdata =
FALSE ;
1001 case COMPRESSION_PACKBITS:
1002 fputs(
" /RunLengthDecode filter", ctx->
fd);
1005 case COMPRESSION_OJPEG:
1006 case COMPRESSION_JPEG:
1011 fputs(
" /DCTDecode filter", ctx->
fd);
1014 use_rawdata =
FALSE;
1017 case COMPRESSION_NEXT:
1018 case COMPRESSION_THUNDERSCAN:
1019 case COMPRESSION_PIXARFILM:
1020 case COMPRESSION_JBIG:
1021 use_rawdata =
FALSE;
1023 case COMPRESSION_SGILOG:
1024 case COMPRESSION_SGILOG24:
1025 use_rawdata =
FALSE;
1031 use_rawdata =
FALSE;
1042 fputs(
" dup", ctx->
fd);
1043 fputs(
" ]", ctx->
fd);
1046 fprintf( ctx->
fd,
"\n >> %s\n",
IMAGEOP(ctx) );
1048 fputs(
" im_stream status { im_stream flushfile } if\n", ctx->
fd);
1049 if (repeat_count > 1) {
1050 if (tile_width < w) {
1051 fprintf(ctx->
fd,
" /im_x im_x %lu add def\n",
1052 (
unsigned long) tile_width);
1053 if (tile_height < h) {
1054 fprintf(ctx->
fd,
" im_x %lu ge {\n",
1056 fputs(
" /im_x 0 def\n", ctx->
fd);
1057 fprintf(ctx->
fd,
" /im_y im_y %lu add def\n",
1058 (
unsigned long) tile_height);
1059 fputs(
" } if\n", ctx->
fd);
1062 if (tile_height < h) {
1063 if (tile_width >= w) {
1064 fprintf(ctx->
fd,
" /im_y im_y %lu add def\n",
1065 (
unsigned long) tile_height);
1066 if (!TIFFIsTiled(tif)) {
1067 fprintf(ctx->
fd,
" /im_h %lu im_y sub",
1069 fprintf(ctx->
fd,
" dup %lu gt { pop",
1070 (
unsigned long) tile_height);
1071 fprintf(ctx->
fd,
" %lu } if def\n",
1072 (
unsigned long) tile_height);
1076 fputs(
"} repeat\n", ctx->
fd);
1081 fputs(
"}\n", ctx->
fd);
1083 return(use_rawdata);
1092 int use_rawdata, tiled_image, breaklen =
MAXLINE;
1093 uint32 chunk_no, num_chunks, *bc;
1094 unsigned char *buf_data, *cp;
1095 tsize_t chunk_size, byte_count;
1097 #if defined( EXP_ASCII85ENCODER )
1099 uint8 * ascii85_p = 0;
1106 #ifdef ENABLE_BROKEN_BEGINENDDATA
1107 fputs(
"%%BeginData:\n", ctx->
fd);
1109 fputs(
"exec\n", ctx->
fd);
1111 tiled_image = TIFFIsTiled(tif);
1113 num_chunks = TIFFNumberOfTiles(tif);
1114 TIFFGetField(tif, TIFFTAG_TILEBYTECOUNTS, &bc);
1116 num_chunks = TIFFNumberOfStrips(tif);
1117 TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &bc);
1121 chunk_size = (tsize_t) bc[0];
1122 for (chunk_no = 1; chunk_no < num_chunks; chunk_no++)
1123 if ((tsize_t) bc[chunk_no] > chunk_size)
1124 chunk_size = (tsize_t) bc[chunk_no];
1127 chunk_size = TIFFTileSize(tif);
1129 chunk_size = TIFFStripSize(tif);
1131 buf_data = (
unsigned char *)_TIFFmalloc(chunk_size);
1133 TIFFError(ctx->
filename,
"Can't alloc %u bytes for %s.",
1134 (
unsigned int) chunk_size, tiled_image ?
"tiles" :
"strips");
1138 #if defined( EXP_ASCII85ENCODER )
1149 ascii85_p = _TIFFmalloc( (chunk_size+(chunk_size/2)) + 8 );
1152 _TIFFfree( buf_data );
1155 "Cannot allocate ASCII85 encoding buffer." );
1161 TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder);
1162 for (chunk_no = 0; chunk_no < num_chunks; chunk_no++) {
1169 byte_count = TIFFReadRawTile(tif, chunk_no,
1170 buf_data, chunk_size);
1172 byte_count = TIFFReadRawStrip(tif, chunk_no,
1173 buf_data, chunk_size);
1174 if (fillorder == FILLORDER_LSB2MSB)
1175 TIFFReverseBits(buf_data, byte_count);
1178 byte_count = TIFFReadEncodedTile(tif,
1182 byte_count = TIFFReadEncodedStrip(tif,
1186 if (byte_count < 0) {
1187 TIFFError(ctx->
filename,
"Can't read %s %d.",
1188 tiled_image ?
"tile" :
"strip", chunk_no);
1200 int adjust, i, j = 0;
1203 adjust = 255 - buf_data[i + ncomps];
1206 buf_data[j++] = buf_data[i] + adjust;
1209 buf_data[j++] = buf_data[i] + adjust;
1210 buf_data[j++] = buf_data[i+1] + adjust;
1213 buf_data[j++] = buf_data[i] + adjust;
1214 buf_data[j++] = buf_data[i+1] + adjust;
1215 buf_data[j++] = buf_data[i+2] + adjust;
1223 #if defined( EXP_ASCII85ENCODER )
1225 buf_data, byte_count);
1227 if ( ascii85_l > 0 )
1228 fwrite( ascii85_p, ascii85_l, 1, ctx->
fd );
1230 for (cp = buf_data; byte_count > 0; byte_count--)
1236 for (cp = buf_data; byte_count > 0; byte_count--) {
1237 putc(
hex[((*cp)>>4)&0xf], ctx->
fd);
1238 putc(
hex[(*cp)&0xf], ctx->
fd);
1241 if (--breaklen <= 0) {
1242 putc(
'\n', ctx->
fd);
1250 putc(
'>', ctx->
fd );
1251 putc(
'\n', ctx->
fd);
1253 #if !defined( EXP_ASCII85ENCODER )
1259 #if defined( EXP_ASCII85ENCODER )
1261 _TIFFfree( ascii85_p );
1263 _TIFFfree(buf_data);
1264 #ifdef ENABLE_BROKEN_BEGINENDDATA
1265 fputs(
"%%EndData\n", ctx->
fd);
1277 case PHOTOMETRIC_RGB:
1287 case PHOTOMETRIC_SEPARATED:
1297 case PHOTOMETRIC_PALETTE:
1299 PhotoshopBanner(ctx, w, h, 1, 3,
"false 3 colorimage");
1300 fprintf(ctx->
fd,
"/scanLine %ld string def\n",
1302 fprintf(ctx->
fd,
"%lu %lu 8\n",
1303 (
unsigned long) w, (
unsigned long) h);
1304 fprintf(ctx->
fd,
"[%lu 0 0 -%lu 0 %lu]\n",
1305 (
unsigned long) w, (
unsigned long) h,
1308 "{currentfile scanLine readhexstring pop} bind\n");
1309 fprintf(ctx->
fd,
"false 3 colorimage\n");
1312 case PHOTOMETRIC_MINISBLACK:
1313 case PHOTOMETRIC_MINISWHITE:
1314 PhotoshopBanner(ctx, w, h, 1, 1,
IMAGEOP(ctx));
1315 fprintf(ctx->
fd,
"/scanLine %ld string def\n",
1317 fprintf(ctx->
fd,
"%lu %lu %d\n",
1319 fprintf(ctx->
fd,
"[%lu 0 0 -%lu 0 %lu]\n",
1320 (
unsigned long) w, (
unsigned long) h, (
unsigned long) h);
1322 "{currentfile scanLine readhexstring pop} bind\n");
1327 putc(
'\n', ctx->
fd);
1334 PhotoshopBanner(ctx, w, h, 1, nc,
"false %d colorimage", nc);
1336 fprintf(ctx->
fd,
"%lu %lu %d\n",
1338 fprintf(ctx->
fd,
"[%lu 0 0 -%lu 0 %lu]\n",
1339 (
unsigned long) w, (
unsigned long) h, (
unsigned long) h);
1340 fprintf(ctx->
fd,
"{currentfile line readhexstring pop} bind\n");
1341 fprintf(ctx->
fd,
"false %d colorimage\n", nc);
1349 PhotoshopBanner(ctx, w, h, ctx->
ps_bytesperrow, nc,
"true %d colorimage", nc);
1350 for (i = 0; i < nc; i++)
1351 fprintf(ctx->
fd,
"/line%d %ld string def\n",
1353 fprintf(ctx->
fd,
"%lu %lu %d\n",
1355 fprintf(ctx->
fd,
"[%lu 0 0 -%lu 0 %lu] \n",
1356 (
unsigned long) w, (
unsigned long) h, (
unsigned long) h);
1357 for (i = 0; i < nc; i++)
1358 fprintf(ctx->
fd,
"{currentfile line%d readhexstring pop}bind\n", i);
1359 fprintf(ctx->
fd,
"true %d colorimage\n", nc);
1362 #define DOBREAK(len, howmany, fd) \
1363 if (((len) -= (howmany)) <= 0) { \
1365 (len) = MAXLINE-(howmany); \
1367 #define PUTHEX(c,fd) putc(hex[((c)>>4)&0xf],fd); putc(hex[(c)&0xf],fd)
1374 unsigned char *tf_buf;
1375 unsigned char *cp, c;
1379 if (tf_buf == NULL) {
1380 TIFFError(ctx->
filename,
"No space for scanline buffer");
1383 for (row = 0; row < h; row++) {
1384 if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
1398 adjust = 255 - cp[nc];
1400 case 4: c = *cp++ + adjust;
PUTHEX(c,ctx->
fd);
1401 case 3: c = *cp++ + adjust;
PUTHEX(c,ctx->
fd);
1402 case 2: c = *cp++ + adjust;
PUTHEX(c,ctx->
fd);
1403 case 1: c = *cp++ + adjust;
PUTHEX(c,ctx->
fd);
1412 case 4: c = *cp++;
PUTHEX(c,ctx->
fd);
1413 case 3: c = *cp++;
PUTHEX(c,ctx->
fd);
1414 case 2: c = *cp++;
PUTHEX(c,ctx->
fd);
1415 case 1: c = *cp++;
PUTHEX(c,ctx->
fd);
1421 _TIFFfree((
char *) tf_buf);
1430 unsigned char *tf_buf;
1431 unsigned char *cp, c;
1435 if (tf_buf == NULL) {
1436 TIFFError(ctx->
filename,
"No space for scanline buffer");
1440 for (row = 0; row < h; row++) {
1441 for (s = 0; s < maxs; s++) {
1442 if (TIFFReadScanline(tif, tf_buf, row, s) < 0)
1451 _TIFFfree((
char *) tf_buf);
1454 #define PUTRGBHEX(c,fd) \
1455 PUTHEX(rmap[c],fd); PUTHEX(gmap[c],fd); PUTHEX(bmap[c],fd)
1460 uint16 *rmap, *gmap, *bmap;
1462 int breaklen =
MAXLINE, cc, nc;
1463 unsigned char *tf_buf;
1464 unsigned char *cp, c;
1467 if (!TIFFGetField(tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {
1468 TIFFError(ctx->
filename,
"Palette image w/o \"Colormap\" tag");
1472 case 8:
case 4:
case 2:
case 1:
1480 if (tf_buf == NULL) {
1481 TIFFError(ctx->
filename,
"No space for scanline buffer");
1486 #define CVT(x) ((unsigned short) (((x) * 255) / ((1U<<16)-1)))
1488 rmap[i] =
CVT(rmap[i]);
1489 gmap[i] =
CVT(gmap[i]);
1490 bmap[i] =
CVT(bmap[i]);
1494 for (row = 0; row < h; row++) {
1495 if (TIFFReadScanline(tif, tf_buf, row, 0) < 0)
1526 _TIFFfree((
char *) tf_buf);
1533 unsigned char* tf_buf;
1535 tsize_t stripsize = TIFFStripSize(tif);
1538 #if defined( EXP_ASCII85ENCODER )
1540 uint8 *ascii85_p = 0;
1544 tf_buf = (
unsigned char *) _TIFFmalloc(stripsize);
1545 memset(tf_buf, 0, stripsize);
1546 if (tf_buf == NULL) {
1547 TIFFError(ctx->
filename,
"No space for scanline buffer");
1551 #if defined( EXP_ASCII85ENCODER )
1562 ascii85_p = _TIFFmalloc( (stripsize+(stripsize/2)) + 8 );
1565 _TIFFfree( tf_buf );
1568 "Cannot allocate ASCII85 encoding buffer." );
1577 for (s = 0; s < TIFFNumberOfStrips(tif); s++) {
1578 int cc = TIFFReadEncodedStrip(tif, s, tf_buf, stripsize);
1580 TIFFError(ctx->
filename,
"Can't read strip");
1585 for (cp += cc; --cp >= tf_buf;)
1590 #if defined( EXP_ASCII85ENCODER )
1593 for (i = 0; i < cc; i+=2) {
1594 adjust = 255 - cp[i + 1];
1595 cp[i / 2] = cp[i] + adjust;
1603 if ( ascii85_l > 0 )
1604 fwrite( ascii85_p, ascii85_l, 1, ctx->
fd );
1622 adjust = 255 - cp[1];
1623 c = *cp++ + adjust;
PUTHEX(c,ctx->
fd);
1639 fputs(
">\n", ctx->
fd);
1641 #if !defined( EXP_ASCII85ENCODER )
1646 _TIFFfree( ascii85_p );
1664 word = (((raw[0]<<8)+raw[1])<<16) + (raw[2]<<8) + raw[3];
1669 q = word / (85L*85*85*85);
1670 buf[0] = (char) (q +
'!');
1672 word -= q * (85L*85*85*85); q = word / (85L*85*85);
1673 buf[1] = (char) (q +
'!');
1675 word -= q * (85L*85*85); q = word / (85*85);
1676 buf[2] = (char) (q +
'!');
1678 w1 = (uint16) (word - q*(85L*85));
1679 buf[3] = (char) ((w1 / 85) +
'!');
1680 buf[4] = (char) ((w1 % 85) +
'!');
1683 buf[0] =
'z', buf[1] =
'\0';
1696 n >= 4; n -= 4, p += 4) {
1699 for (cp = buf; *cp; cp++) {
1702 putc(
'\n', ctx->
fd);
1719 fwrite(res[0] ==
'z' ?
"!!!!" : res, ctx->
ascii85count + 1, 1, ctx->
fd);
1721 fputs(
"~>\n", ctx->
fd);
1723 #if defined( EXP_ASCII85ENCODER)
1725 #define A85BREAKCNTR ctx->ascii85breaklen
1726 #define A85BREAKLEN (2*MAXLINE)
1768 unsigned f_eod,
const uint8 * raw_p,
int raw_l )
1782 for ( ; raw_l > 3; raw_l -= 4 )
1784 val32 = *(++raw_p) << 24;
1785 val32 += *(++raw_p) << 16;
1786 val32 += *(++raw_p) << 8;
1787 val32 += *(++raw_p);
1791 ascii85_p[ascii85_l] =
'z';
1797 ascii85[4] = (char) ((val32 % 85) + 33);
1800 ascii85[3] = (char) ((val32 % 85) + 33);
1803 ascii85[2] = (char) ((val32 % 85) + 33);
1806 ascii85[1] = (char) ((val32 % 85) + 33);
1807 ascii85[0] = (char) ((val32 / 85) + 33);
1809 _TIFFmemcpy( &ascii85_p[ascii85_l], ascii85,
sizeof(ascii85) );
1810 rc =
sizeof(ascii85);
1817 ascii85_p[ascii85_l] =
'\n';
1832 val32 = *++raw_p << 24;
1834 if ( --raw_l > 0 ) val32 += *(++raw_p) << 16;
1835 if ( --raw_l > 0 ) val32 += *(++raw_p) << 8;
1839 ascii85[3] = (char) ((val32 % 85) + 33);
1842 ascii85[2] = (char) ((val32 % 85) + 33);
1845 ascii85[1] = (char) ((val32 % 85) + 33);
1846 ascii85[0] = (char) ((val32 / 85) + 33);
1848 _TIFFmemcpy( &ascii85_p[ascii85_l], ascii85, len );
1859 ascii85_p[ascii85_l++] =
'~';
1860 ascii85_p[ascii85_l++] =
'>';
1861 ascii85_p[ascii85_l++] =
'\n';
1864 return ( ascii85_l );