-
Notifications
You must be signed in to change notification settings - Fork 2
/
tiff2png.c
1528 lines (1363 loc) · 41.8 KB
/
tiff2png.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** tiff2png.c - converts Tagged Image File Format to Portable Network Graphics
**
** Copyright 1996,2000 Willem van Schaik, Calgary ([email protected])
** Copyright 1999-2002 Greg Roelofs ([email protected])
**
** Lots of material was stolen from libtiff, tifftopnm, pnmtopng, which
** programs had also done a fair amount of "borrowing", so the credit for
** this program goes besides the author also to:
** Sam Leffler
** Jef Poskanzer
** Alexander Lehmann
** Patrick Naughton
** Marcel Wijkstra
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted,
** provided that the above copyright notice appear in all copies and that
** both that copyright notice and this permission notice appear in
** supporting documentation.
**
** This file is provided AS IS with no warranties of any kind. The author
** shall have no liability with respect to the infringement of copyrights,
** trade secrets or any patents by this file or any part thereof. In no
** event will the author be liable for any lost revenue or profits or
** other special, indirect and consequential damages.
*/
/* To do: add testing/support for associated vs. unassociated alpha channel
** add support for iCCP profiles (and autodetect sRGB?)
** / add support for text annotations
** \ incorporate Willem's remaining 0.82 changes
** check various "XXX" items (non-contiguous tiles? MINISWHITE RGB? ...)
** create a man page
** [maybe switch to equivalent (OSS Certified) libpng or zlib license?]
*/
/* We expect the build system to define an appropriate version. */
#ifndef VERSION
/* If not, at least include the compile time. */
#define VERSION "unknown development version " __DATE__
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tiff.h"
#include "tiffio.h"
#include "png.h"
#include "zlib.h"
#ifdef _MSC_VER /* works for MSVC 5.0; need finer tuning? */
# define strcasecmp _stricmp
#endif
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif
#ifndef NONE
# define NONE 0
#endif
#define MAXCOLORS 256
#ifndef PHOTOMETRIC_DEPTH
# define PHOTOMETRIC_DEPTH 32768
#endif
#define DIR_SEP '/' /* SJT: Unix-specific */
typedef unsigned char uch;
typedef unsigned short ush;
typedef unsigned long ulg;
typedef struct _jmpbuf_wrapper {
jmp_buf jmpbuf;
} jmpbuf_wrapper;
static jmpbuf_wrapper tiff2png_jmpbuf_struct;
/* local prototypes */
static void usage (int rc);
static void tiff2png_error_handler (png_structp png_ptr, png_const_charp msg);
int tiff2png (char *tiffname, char *pngname, int verbose, int force,
int interlace_type, int png_compression_level, int invert,
int faxpect_option,
double gamma);
/* macros to get and put bits out of the bytes */
#define GET_LINE_SAMPLE \
{ \
if (bitsleft == 0) \
{ \
p_line++; \
bitsleft = 8; \
} \
bitsleft -= (bps >= 8) ? 8 : bps; \
sample = (*p_line >> bitsleft) & maxval; \
if (invert) \
sample = ~sample & maxval; \
}
#define GET_STRIP_SAMPLE \
{ \
if (getbitsleft == 0) \
{ \
p_strip++; \
getbitsleft = 8; \
} \
getbitsleft -= (bps >= 8) ? 8 : bps; \
sample = (*p_strip >> getbitsleft) & maxval; \
if (invert) \
sample = ~sample & maxval; \
}
#define PUT_LINE_SAMPLE \
{ \
if (putbitsleft == 0) \
{ \
p_line++; \
putbitsleft = 8; \
} \
putbitsleft -= (bps >= 8) ? 8 : bps; \
if (invert) \
sample = ~sample; \
*p_line |= ((sample & maxval) << putbitsleft); \
}
/*----------------------------------------------------------------------------*/
static void usage (rc)
int rc;
{
char *pTIFFver;
int len;
fprintf (stderr,
"tiff2png version %s (Willem van Schaik and Greg Roelofs)\n", VERSION);
/* this function returns a huge, three-line version+copyright string */
pTIFFver = (char *)TIFFGetVersion();
if (strncmp(pTIFFver, "LIBTIFF, Version ", 17) == 0)
{
char *p, *q;
p = pTIFFver + 17;
q = strchr(p, '\n');
if (q && (len = (q-p)) < 15) /* arbitrary (short) limit */
fprintf (stderr, " Compiled with libtiff %.*s.\n", len, p);
}
fprintf(stderr, " Compiled with libpng %s; using libpng %s.\n",
PNG_LIBPNG_VER_STRING, png_libpng_ver);
fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n\n",
ZLIB_VERSION, zlib_version);
fprintf (stderr,
"Usage: tiff2png [-verbose] [-force] [-destdir <dir>] [-compression <val>]"
"\n [-gamma <val>] [-interlace] [-invert] "
"[-faxpect] "
"<file> [...]\n\n"
"Read each <file> and convert to PNG format"
#ifdef DESTDIR_IS_CURDIR
" (in the current directory).\n"
#else
" (by default, in same directory as\n"
"the corresponding TIFF)."
#endif
" Suffixes will be changed from .tif or .tiff to .png."
"\n\n"
" -force overwrite existing PNGs if they exist\n"
" -destdir create PNGs in destination directory <dir>\n"
" -compression set the zlib compression level to <val> (0-9)\n"
" -gamma write PNGs with specified gamma <val> (e.g., 0.45455)\n"
" -interlace write interlaced PNGs\n"
" -invert invert grayscale images (swaps black/white)\n");
fprintf (stderr,
" -faxpect convert fax with 2:1 aspect ratio to square pixels\n");
exit (rc);
}
/*----------------------------------------------------------------------------*/
static void tiff2png_error_handler (png_ptr, msg)
png_structp png_ptr;
png_const_charp msg;
{
jmpbuf_wrapper *jmpbuf_ptr;
/* this function, aside from the extra step of retrieving the "error
* pointer" (below) and the fact that it exists within the application
* rather than within libpng, is essentially identical to libpng's
* default error handler. The second point is critical: since both
* setjmp() and longjmp() are called from the same code, they are
* guaranteed to have compatible notions of how big a jmp_buf is,
* regardless of whether _BSD_SOURCE or anything else has (or has not)
* been defined. */
fprintf (stderr, "tiff2png: fatal libpng error: %s\n", msg);
fflush (stderr);
jmpbuf_ptr = png_get_error_ptr (png_ptr);
if (jmpbuf_ptr == NULL) { /* we are completely hosed now */
fprintf (stderr,
"tiff2png: EXTREMELY fatal error: jmpbuf unrecoverable; terminating.\n");
fflush (stderr);
exit (99);
}
longjmp (jmpbuf_ptr->jmpbuf, 1);
}
/*----------------------------------------------------------------------------*/
int
tiff2png (tiffname, pngname, verbose, force, interlace_type,
png_compression_level, _invert, faxpect_option, gamma)
char *tiffname, *pngname;
int verbose, force, interlace_type, png_compression_level, _invert;
int faxpect_option;
double gamma;
{
static TIFF *tif = NULL; /* TIFF */
ush bps, spp, planar;
ush photometric, tiff_compression_method;
int bigendian;
int maxval;
static int colors = 0;
static int halfcols = 0;
int cols, rows;
int row;
register int col;
static uch *tiffstrip = NULL;
uch *tiffline;
size_t stripsz;
static size_t tilesz = 0L;
uch *tifftile; /* FAP 20020610 - Add variables to support tiled images */
ush tiled;
uint32 tile_width, tile_height; /* typedef'd in tiff.h */
static int num_tilesX = 0;
register uch *p_strip, *p_line;
register uch sample;
#ifdef INVERT_MINISWHITE
int sample16;
#endif
register int bitsleft;
register int getbitsleft;
register int putbitsleft;
float xres, yres, ratio;
#ifdef GRR_16BIT_DEBUG
uch msb_max, lsb_max;
uch msb_min, lsb_min;
int s16_max, s16_min;
#endif
static FILE *png = NULL; /* PNG */
png_struct *png_ptr;
png_info *info_ptr;
png_byte *pngline;
png_byte *p_png;
png_color palette[MAXCOLORS];
static png_uint_32 width;
int bit_depth = 0;
int color_type = -1;
int tiff_color_type;
int pass;
static png_uint_32 res_x_half=0L, res_x=0L, res_y=0L;
static int unit_type = 0;
unsigned short *redcolormap;
unsigned short *greencolormap;
unsigned short *bluecolormap;
static int have_res = FALSE;
static int invert;
int faxpect;
long i, n;
/* first figure out whether this machine is big- or little-endian */
{
union { int32 i; char c[4]; } endian_tester;
endian_tester.i = 1;
bigendian = (endian_tester.c[0] == 0);
}
/*
tilesz = 0L;
num_tilesX = 0;
*/
invert = _invert;
tif = TIFFOpen (tiffname, "r");
if (tif == NULL)
{
fprintf (stderr, "tiff2png error: TIFF file %s not found\n", tiffname);
return 1;
}
if (!force)
{
png = fopen (pngname, "rb");
if (png)
{
fprintf (stderr, "tiff2png warning: PNG file %s exists: skipping\n",
pngname);
fclose (png);
TIFFClose (tif);
return 1;
}
}
png = fopen (pngname, "wb");
if (png == NULL)
{
fprintf (stderr, "tiff2png error: PNG file %s cannot be created\n",
pngname);
TIFFClose (tif);
return 1;
}
if (verbose)
fprintf (stderr, "\ntiff2png: converting %s to %s\n", tiffname, pngname);
/* start PNG preparation */
png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
&tiff2png_jmpbuf_struct, tiff2png_error_handler, NULL);
if (!png_ptr)
{
fprintf (stderr,
"tiff2png error: cannot allocate libpng main struct (%s)\n", pngname);
TIFFClose (tif);
fclose (png);
return 4;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
fprintf (stderr,
"tiff2png error: cannot allocate libpng info struct (%s)\n", pngname);
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
TIFFClose (tif);
fclose (png);
return 4;
}
if (setjmp (tiff2png_jmpbuf_struct.jmpbuf))
{
fprintf (stderr, "tiff2png error: libpng returns error condition (%s)\n",
pngname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
fclose (png);
return 1;
}
png_init_io (png_ptr, png);
/* get TIFF header info */
if (verbose)
{
int byteswapped = TIFFIsByteSwapped(tif); /* why no TIFFIsBigEndian()?? */
fprintf (stderr, "tiff2png: ");
TIFFPrintDirectory (tif, stderr, TIFFPRINT_NONE);
fprintf (stderr, "tiff2png: byte order = %s\n",
((bigendian && byteswapped) || (!bigendian && !byteswapped))?
"little-endian (Intel)" : "big-endian (Motorola)");
fprintf (stderr, "tiff2png: this machine is %s-endian\n",
bigendian? "big" : "little");
}
if (! TIFFGetField (tif, TIFFTAG_PHOTOMETRIC, &photometric))
{
fprintf (stderr,
"tiff2png error: photometric could not be retrieved (%s)\n", tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
fclose (png);
return 1;
}
if (! TIFFGetField (tif, TIFFTAG_BITSPERSAMPLE, &bps))
bps = 1;
if (! TIFFGetField (tif, TIFFTAG_SAMPLESPERPIXEL, &spp))
spp = 1;
if (! TIFFGetField (tif, TIFFTAG_PLANARCONFIG, &planar))
planar = 1;
tiled = TIFFIsTiled(tif); /* FAP 20020610 - get tiled flag */
(void) TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &cols);
(void) TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &rows);
width = cols;
ratio = 0.0;
if (TIFFGetField (tif, TIFFTAG_XRESOLUTION, &xres) &&
TIFFGetField (tif, TIFFTAG_YRESOLUTION, &yres) && /* no default value */
(xres != 0.0) && (yres != 0.0))
{
uint16 resunit; /* typedef'd in tiff.h */
have_res = TRUE;
ratio = xres / yres;
if (verbose)
{
fprintf (stderr, "tiff2png: aspect ratio (hor/vert) = %g (%g / %g)\n",
ratio, xres, yres);
if (0.95 < ratio && ratio < 1.05)
fprintf (stderr, "tiff2png: near-unity aspect ratio\n");
else if (1.90 < ratio && ratio < 2.10)
fprintf (stderr, "tiff2png: near-2X aspect ratio\n");
else
fprintf (stderr, "tiff2png: non-square, non-2X pixels\n");
}
#if 0
/* GRR: this should be fine and works sometimes, but occasionally it
* seems to cause a segfault--which may be more related to Linux 2.2.10
* and/or SMP and/or heavy CPU loading. Disabled for now. */
(void) TIFFGetFieldDefaulted (tif, TIFFTAG_RESOLUTIONUNIT, &resunit);
#else
if (! TIFFGetField (tif, TIFFTAG_RESOLUTIONUNIT, &resunit))
resunit = RESUNIT_INCH; /* default (see libtiff tif_dir.c) */
#endif
/* convert from TIFF data (floats) to PNG data (unsigned longs) */
switch (resunit)
{
case RESUNIT_CENTIMETER:
res_x_half = (png_uint_32)(50.0*xres + 0.5);
res_x = (png_uint_32)(100.0*xres + 0.5);
res_y = (png_uint_32)(100.0*yres + 0.5);
unit_type = PNG_RESOLUTION_METER;
break;
case RESUNIT_INCH:
res_x_half = (png_uint_32)(0.5*39.37*xres + 0.5);
res_x = (png_uint_32)(39.37*xres + 0.5);
res_y = (png_uint_32)(39.37*yres + 0.5);
unit_type = PNG_RESOLUTION_METER;
break;
/* case RESUNIT_NONE: */
default:
res_x_half = (png_uint_32)(50.0*xres + 0.5);
res_x = (png_uint_32)(100.0*xres + 0.5);
res_y = (png_uint_32)(100.0*yres + 0.5);
unit_type = PNG_RESOLUTION_UNKNOWN;
break;
}
}
if (verbose)
{
fprintf (stderr, "tiff2png: %dx%dx%d image\n", cols, rows, bps * spp);
fprintf (stderr, "tiff2png: %d bit%s/sample, %d sample%s/pixel\n",
bps, bps == 1? "" : "s", spp, spp == 1? "" : "s");
}
/* detect tiff filetype */
maxval = (1 << bps) - 1;
if (verbose)
fprintf (stderr, "tiff2png: maxval=%d\n", maxval);
switch (photometric)
{
case PHOTOMETRIC_MINISWHITE:
case PHOTOMETRIC_MINISBLACK:
if (verbose)
fprintf (stderr, "tiff2png: %d graylevels (min = %s)\n", maxval + 1,
photometric == PHOTOMETRIC_MINISBLACK? "black" : "white");
if (spp == 1) /* no alpha */
{
color_type = PNG_COLOR_TYPE_GRAY;
if (verbose)
fprintf (stderr, "tiff2png: color type = grayscale\n");
bit_depth = bps;
}
else /* must be alpha */
{
color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
if (verbose)
fprintf (stderr, "tiff2png: color type = grayscale + alpha\n");
if (bps <= 8)
bit_depth = 8;
else
bit_depth = bps;
}
break;
case PHOTOMETRIC_PALETTE:
{
int palette_8bit; /* set iff all color values in TIFF palette are < 256 */
color_type = PNG_COLOR_TYPE_PALETTE;
if (verbose)
fprintf (stderr, "tiff2png: color type = paletted\n");
if (! TIFFGetField (tif, TIFFTAG_COLORMAP, &redcolormap, &greencolormap,
&bluecolormap))
{
fprintf (stderr,
"tiff2png error: cannot retrieve TIFF colormaps (%s)\n", tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
fclose (png);
return 1;
}
colors = maxval + 1;
if (colors > MAXCOLORS)
{
fprintf (stderr,
"tiff2png error: palette too large (%d colors) (%s)\n", colors,
tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
fclose (png);
return 1;
}
/* max PNG palette-size is 8 bits, you could convert to full-color */
if (bps >= 8)
bit_depth = 8;
else
bit_depth = bps;
/* PLTE chunk */
/* TIFF palettes contain 16-bit shorts, while PNG palettes are 8-bit */
/* Some broken (??) software puts 8-bit values in the shorts, which would
make the palette come out all zeros, which isn't good. We check... */
palette_8bit = 1;
for (i = 0 ; i < colors ; i++)
{
if ( redcolormap[i] > 255 ||
greencolormap[i] > 255 ||
bluecolormap[i] > 255)
{
palette_8bit = 0;
break;
}
}
if (palette_8bit && verbose)
fprintf(stderr, "tiff2png warning: assuming 8-bit palette values\n");
for (i = 0 ; i < colors ; i++)
{
if (invert)
{
if (palette_8bit)
{
palette[i].red = ~((png_byte) redcolormap[i]);
palette[i].green = ~((png_byte) greencolormap[i]);
palette[i].blue = ~((png_byte) bluecolormap[i]);
}
else
{
palette[i].red = ~((png_byte) (redcolormap[i] >> 8));
palette[i].green = ~((png_byte) (greencolormap[i] >> 8));
palette[i].blue = ~((png_byte) (bluecolormap[i] >> 8));
}
}
else
{
if (palette_8bit)
{
palette[i].red = (png_byte) redcolormap[i];
palette[i].green = (png_byte) greencolormap[i];
palette[i].blue = (png_byte) bluecolormap[i];
}
else
{
palette[i].red = (png_byte) (redcolormap[i] >> 8);
palette[i].green = (png_byte) (greencolormap[i] >> 8);
palette[i].blue = (png_byte) (bluecolormap[i] >> 8);
}
}
}
/* prevent index data (pixel values) from being inverted (-> garbage) */
invert = FALSE;
break;
}
case PHOTOMETRIC_YCBCR:
/* GRR 20001110: lifted from tiff2ps in libtiff 3.5.4 */
TIFFGetField(tif, TIFFTAG_COMPRESSION, &tiff_compression_method);
if (tiff_compression_method == COMPRESSION_JPEG &&
planar == PLANARCONFIG_CONTIG)
{
/* can rely on libjpeg to convert to RGB */
TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
photometric = PHOTOMETRIC_RGB;
if (verbose)
fprintf (stderr,
"tiff2png: original color type = YCbCr with JPEG compression\n");
}
else
{
fprintf (stderr,
"tiff2png error: don't know how to handle PHOTOMETRIC_YCBCR with\n"
" compression %d (%sJPEG) and planar config %d (%scontiguous)\n"
" (%s)\n", tiff_compression_method,
tiff_compression_method == COMPRESSION_JPEG? "" : "not ",
planar, planar == PLANARCONFIG_CONTIG? "" : "not ", tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
fclose (png);
return 1;
}
/* fall thru... */
case PHOTOMETRIC_RGB:
if (spp == 3)
{
color_type = PNG_COLOR_TYPE_RGB;
if (verbose)
fprintf (stderr, "tiff2png: color type = truecolor\n");
}
else
{
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
if (verbose)
fprintf (stderr, "tiff2png: color type = truecolor + alpha\n");
}
if (bps <= 8)
bit_depth = 8;
else
bit_depth = bps;
break;
case PHOTOMETRIC_LOGL:
case PHOTOMETRIC_LOGLUV:
/* GRR 20001110: lifted from tiff2ps from libtiff 3.5.4 */
TIFFGetField(tif, TIFFTAG_COMPRESSION, &tiff_compression_method);
if (tiff_compression_method != COMPRESSION_SGILOG &&
tiff_compression_method != COMPRESSION_SGILOG24)
{
fprintf (stderr,
"tiff2png error: don't know how to handle PHOTOMETRIC_LOGL%s with\n"
" compression %d (not SGILOG) (%s)\n",
photometric == PHOTOMETRIC_LOGLUV? "UV" : "",
tiff_compression_method, tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
fclose (png);
return 1;
}
/* rely on library to convert to RGB/greyscale */
#ifdef LIBTIFF_HAS_16BIT_INTEGER_FORMAT
if (bps > 8)
{
/* SGILOGDATAFMT_16BIT converts to a floating-point luminance value;
* U,V are left as such. SGILOGDATAFMT_16BIT_INT doesn't exist. */
TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_16BIT_INT);
bit_depth = bps = 16;
}
else
#endif
{
/* SGILOGDATAFMT_8BIT converts to normal grayscale or RGB format */
TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
bit_depth = bps = 8;
}
if (photometric == PHOTOMETRIC_LOGL)
{
photometric = PHOTOMETRIC_MINISBLACK;
color_type = PNG_COLOR_TYPE_GRAY;
if (verbose)
{
fprintf (stderr,
"tiff2png: original color type = logL with SGILOG compression\n");
fprintf (stderr, "tiff2png: color type = grayscale\n");
}
}
else
{
photometric = PHOTOMETRIC_RGB;
color_type = PNG_COLOR_TYPE_RGB;
if (verbose)
{
fprintf (stderr,
"tiff2png: original color type = logLUV with SGILOG compression\n");
fprintf (stderr, "tiff2png: color type = truecolor\n");
}
}
break;
/*
case PHOTOMETRIC_YCBCR:
case PHOTOMETRIC_LOGL:
case PHOTOMETRIC_LOGLUV:
*/
case PHOTOMETRIC_MASK:
case PHOTOMETRIC_SEPARATED:
case PHOTOMETRIC_CIELAB:
case PHOTOMETRIC_DEPTH:
{
fprintf (stderr,
"tiff2png error: don't know how to handle %s (%s)\n",
/*
photometric == PHOTOMETRIC_YCBCR? "PHOTOMETRIC_YCBCR" :
photometric == PHOTOMETRIC_LOGL? "PHOTOMETRIC_LOGL" :
photometric == PHOTOMETRIC_LOGLUV? "PHOTOMETRIC_LOGLUV" :
*/
photometric == PHOTOMETRIC_MASK? "PHOTOMETRIC_MASK" :
photometric == PHOTOMETRIC_SEPARATED? "PHOTOMETRIC_SEPARATED" :
photometric == PHOTOMETRIC_CIELAB? "PHOTOMETRIC_CIELAB" :
photometric == PHOTOMETRIC_DEPTH? "PHOTOMETRIC_DEPTH" :
"unknown photometric",
tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
fclose (png);
return 1;
}
default:
{
fprintf (stderr, "tiff2png error: unknown photometric (%d) (%s)\n",
photometric, tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
fclose (png);
return 1;
}
}
tiff_color_type = color_type;
if (verbose)
fprintf (stderr, "tiff2png: bit depth = %d\n", bit_depth);
faxpect = faxpect_option;
if (faxpect && (!have_res || ratio < 1.90 || ratio > 2.10))
{
fprintf (stderr,
"tiff2png: aspect ratio is out of range: skipping -faxpect conversion\n");
faxpect = FALSE;
}
if (faxpect && (color_type != PNG_COLOR_TYPE_GRAY || bit_depth != 1))
{
fprintf (stderr,
"tiff2png: only B&W (1-bit grayscale) images supported for -faxpect\n");
faxpect = FALSE;
}
/* reduce width of fax by 2X by converting 1-bit grayscale to 2-bit, 3-color
* palette */
if (faxpect)
{
width = halfcols = cols / 2;
color_type = PNG_COLOR_TYPE_PALETTE;
palette[0].red = palette[0].green = palette[0].blue = 0; /* both 0 */
palette[1].red = palette[1].green = palette[1].blue = 127; /* 0,1 or 1,0 */
palette[2].red = palette[2].green = palette[2].blue = 255; /* both 1 */
colors = 3;
bit_depth = 2;
res_x = res_x_half;
if (verbose)
{
fprintf (stderr, "tiff2png: new width = %u pixels\n", width);
fprintf (stderr, "tiff2png: new color type = paletted\n");
fprintf (stderr, "tiff2png: new bit depth = %d\n", bit_depth);
}
}
/* put parameter info in png-chunks */
png_set_IHDR(png_ptr, info_ptr, width, rows, bit_depth, color_type,
interlace_type, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
if (png_compression_level != -1)
png_set_compression_level(png_ptr, png_compression_level);
if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_PLTE(png_ptr, info_ptr, palette, colors);
/* gAMA chunk */
if (gamma != -1.0)
{
if (verbose)
fprintf (stderr, "tiff2png: gamma = %f\n", gamma);
png_set_gAMA(png_ptr, info_ptr, gamma);
}
/* pHYs chunk */
if (have_res)
png_set_pHYs (png_ptr, info_ptr, res_x, res_y, unit_type);
png_write_info (png_ptr, info_ptr);
png_set_packing (png_ptr);
/* allocate space for one line (or row of tiles) of TIFF image */
tiffline = NULL;
tifftile = NULL;
tiffstrip = NULL;
if (!tiled) /* strip-based TIFF */
{
if (planar == 1) /* contiguous picture */
tiffline = (uch*) malloc(TIFFScanlineSize(tif));
else /* separated planes */
tiffline = (uch*) malloc(TIFFScanlineSize(tif) * spp);
}
else
{
/* FAP 20020610 - tiled support - allocate space for one "row" of tiles */
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width);
TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height);
num_tilesX = (width+tile_width-1)/tile_width;
if (planar == 1)
{
tilesz = TIFFTileSize(tif);
tifftile = (uch*) malloc(tilesz);
if (tifftile == NULL)
{
fprintf (stderr,
"tiff2png error: can't allocate memory for TIFF tile buffer (%s)\n",
tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
fclose (png);
return 4;
}
stripsz = (tile_width*num_tilesX) * tile_height * spp;
tiffstrip = (uch*) malloc(stripsz);
tiffline = tiffstrip; /* just set the line to the top of the strip.
* we'll move it through below. */
}
else
{
fprintf (stderr,
"tiff2png error: can't handle tiled separated-plane TIFF format (%s)\n",
tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
fclose (png);
return 5;
}
}
if (tiffline == NULL)
{
fprintf (stderr,
"tiff2png error: can't allocate memory for TIFF scanline buffer (%s)\n",
tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
fclose (png);
if (tiled && planar == 1)
free(tifftile);
return 4;
}
if (planar != 1) /* in case we must combine more planes into one */
{
tiffstrip = (uch*) malloc(TIFFScanlineSize(tif));
if (tiffstrip == NULL)
{
fprintf (stderr,
"tiff2png error: can't allocate memory for TIFF strip buffer (%s)\n",
tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
free(tiffline);
fclose (png);
return 4;
}
}
/* allocate space for one line of PNG image */
/* max: 3 color channels plus one alpha channel, 16 bit => 8 bytes/pixel */
pngline = (uch *) malloc (cols * 8);
if (pngline == NULL)
{
fprintf (stderr,
"tiff2png error: can't allocate memory for PNG row buffer (%s)\n",
tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
free(tiffline);
if (tiled && planar == 1)
free(tifftile);
else if (planar != 1)
free(tiffstrip);
fclose (png);
return 4;
}
#ifdef GRR_16BIT_DEBUG
msb_max = lsb_max = 0;
msb_min = lsb_min = 255;
s16_max = 0;
s16_min = 65535;
#endif
for (pass = 0 ; pass < png_set_interlace_handling (png_ptr) ; pass++)
{
for (row = 0; row < rows; row++)
{
if (planar == 1) /* contiguous picture */
{
if (!tiled)
{
if (TIFFReadScanline (tif, tiffline, row, 0) < 0)
{
fprintf (stderr, "tiff2png error: bad data read on line %d (%s)\n",
row, tiffname);
png_destroy_write_struct (&png_ptr, &info_ptr);
TIFFClose (tif);
free(tiffline);
fclose (png);
return 1;
}
}
else /* tiled */
{
int col, ok=1, r;
int tileno;
/* FAP 20020610 - Read in one row of tiles and hand out the data one
scanline at a time so the code below doesn't need
to change */
/* Is it time for a new strip? */
if ((row % tile_height) == 0)
{
for (col = 0; ok && col < num_tilesX; col += 1 )
{
tileno = col+(row/tile_height)*num_tilesX;
/* fprintf(stderr,"\nRow:%d Col:%d Tile No: %d ",row,col,tileno); */
/* read the tile into an RGB array */
if (!TIFFReadEncodedTile(tif, tileno, tifftile, tilesz))
{
ok = 0;
break;
}
/* copy this tile into the row buffer */
for (r = 0; r < (int) tile_height; r++)
{
void* dest;
void* src;
dest = tiffstrip + (r * tile_width * num_tilesX * spp)
+ (col * tile_width * spp);
src = tifftile + (r * tile_width * spp);
/* if (row == 0) fprintf(stderr, "\ndest: %p src:%p", dest,src); */
memcpy(dest, src, (tile_width * spp));
}
}
tiffline = tiffstrip; /* set tileline to top of strip */
/* fprintf(stderr, "\ntiffline:%p", tiffline); */
}
else
{
tiffline = tiffstrip +
((row % tile_height) * ((tile_width * num_tilesX) * spp));
/* fprintf(stderr,"\ntiffline:%p %d",tiffline,(tiffline-tiffstrip)); */
}
} /* end if (tiled) */
}
else /* separated planes, then combine more strips into one line */
{
ush s;
/* XXX: this assumes strips; are separated-plane tiles possible? */
p_line = tiffline;
for (n = 0; n < (cols/8 * bps*spp); n++)
*p_line++ = '\0';
for (s = 0; s < spp; s++)
{
p_strip = tiffstrip;
getbitsleft = 8;
p_line = tiffline;
putbitsleft = 8;
if (TIFFReadScanline(tif, tiffstrip, row, s) < 0)
{
fprintf (stderr, "tiff2png error: bad data read on line %d (%s)\n",