-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlautiff2pdfobject.cpp
More file actions
4892 lines (4477 loc) · 177 KB
/
lautiff2pdfobject.cpp
File metadata and controls
4892 lines (4477 loc) · 177 KB
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
#include "lautiff2pdfobject.h"
using namespace libtiff;
static void
t2p_disable(TIFF *tif)
{
T2P *t2p = (T2P *) TIFFClientdata(tif);
t2p->outputdisable = 1;
}
static void
t2p_enable(TIFF *tif)
{
T2P *t2p = (T2P *) TIFFClientdata(tif);
t2p->outputdisable = 0;
}
/*
* Procs for TIFFClientOpen
*/
static tmsize_t
t2pReadFile(TIFF *tif, tdata_t data, tmsize_t size)
{
thandle_t client = TIFFClientdata(tif);
TIFFReadWriteProc proc = TIFFGetReadProc(tif);
if (proc) {
return proc(client, data, size);
}
return -1;
}
static tmsize_t
t2pWriteFile(TIFF *tif, tdata_t data, tmsize_t size)
{
thandle_t client = TIFFClientdata(tif);
TIFFReadWriteProc proc = TIFFGetWriteProc(tif);
if (proc) {
return proc(client, data, size);
}
return -1;
}
static uint64
t2pSeekFile(TIFF *tif, toff_t offset, int whence)
{
thandle_t client = TIFFClientdata(tif);
TIFFSeekProc proc = TIFFGetSeekProc(tif);
if (proc) {
return proc(client, offset, whence);
}
return -1;
}
static tmsize_t
t2p_readproc(thandle_t handle, tdata_t data, tmsize_t size)
{
(void) handle, (void) data, (void) size;
return -1;
}
static tmsize_t
t2p_writeproc(thandle_t handle, tdata_t data, tmsize_t size)
{
T2P *t2p = (T2P *) handle;
if (t2p->outputdisable <= 0 && t2p->outputfile) {
tsize_t written = fwrite(data, 1, size, t2p->outputfile);
t2p->outputwritten += written;
return written;
}
return size;
}
static uint64
t2p_seekproc(thandle_t handle, uint64 offset, int whence)
{
T2P *t2p = (T2P *) handle;
if (t2p->outputdisable <= 0 && t2p->outputfile) {
return fseek(t2p->outputfile, (long) offset, whence);
}
return offset;
}
static int
t2p_closeproc(thandle_t handle)
{
T2P *t2p = (T2P *) handle;
fclose(t2p->outputfile);
(void) handle;
return 0;
}
static uint64
t2p_sizeproc(thandle_t handle)
{
(void) handle;
return -1;
}
static int
t2p_mapproc(thandle_t handle, void **data, toff_t *offset)
{
(void) handle, (void) data, (void) offset;
return -1;
}
static void
t2p_unmapproc(thandle_t handle, void *data, toff_t offset)
{
(void) handle, (void) data, (void) offset;
}
static uint64
checkAdd64(uint64 summand1, uint64 summand2, T2P *t2p)
{
uint64 bytes = summand1 + summand2;
if (bytes - summand1 != summand2) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
bytes = 0;
}
return bytes;
}
static uint64
checkMultiply64(uint64 first, uint64 second, T2P *t2p)
{
uint64 bytes = first * second;
if (second && bytes / second != first) {
TIFFError(TIFF2PDF_MODULE, "Integer overflow");
t2p->t2p_error = T2P_ERR_ERROR;
bytes = 0;
}
return bytes;
}
/*
This is the main function.
The program converts one TIFF file to one PDF file, including multiple page
TIFF files, tiled TIFF files, black and white. grayscale, and color TIFF
files that contain data of TIFF photometric interpretations of bilevel,
grayscale, RGB, YCbCr, CMYK separation, and ICC L*a*b* as supported by
libtiff and PDF.
If you have multiple TIFF files to convert into one PDF file then use tiffcp
or other program to concatenate the files into a multiple page TIFF file.
If the input TIFF file is of huge dimensions (greater than 10000 pixels height
or width) convert the input image to a tiled TIFF if it is not already.
The standard output is standard output. Set the output file name with the
"-o output.pdf" option.
All black and white files are compressed into a single strip CCITT G4 Fax
compressed PDF, unless tiled, where tiled black and white images are
compressed into tiled CCITT G4 Fax compressed PDF, libtiff CCITT support
is assumed.
Color and grayscale data can be compressed using either JPEG compression,
ITU-T T.81, or Zip/Deflate LZ77 compression, per PNG 1.2 and RFC 1951. Set
the compression type using the -j or -z options. JPEG compression support
requires that libtiff be configured with JPEG support, and Zip/Deflate
compression support requires that libtiff is configured with Zip support,
in tiffconf.h. Use only one or the other of -j and -z. The -q option
sets the image compression quality, that is 1-100 with libjpeg JPEG
compression and one of 1, 10, 11, 12, 13, 14, or 15 for PNG group compression
predictor methods, add 100, 200, ..., 900 to set zlib compression quality 1-9.
PNG Group differencing predictor methods are not currently implemented.
If the input TIFF contains single strip CCITT G4 Fax compressed information,
then that is written to the PDF file without transcoding, unless the options
of no compression and no passthrough are set, -d and -n.
If the input TIFF contains JPEG or single strip Zip/Deflate compressed
information, and they are configured, then that is written to the PDF file
without transcoding, unless the options of no compression and no passthrough
are set.
The default page size upon which the TIFF image is placed is determined by
the resolution and extent of the image data. Default values for the TIFF
image resolution can be set using the -x and -y options. The page size can
be set using the -p option for paper size, or -w and -l for paper width and
length, then each page of the TIFF image is centered on its page. The
distance unit for default resolution and page width and length can be set
by the -u option, the default unit is inch.
Various items of the output document information can be set with the -e, -c,
-a, -t, -s, and -k tags. Setting the argument of the option to "" for these
tags causes the relevant document information field to be not written. Some
of the document information values otherwise get their information from the
input TIFF image, the software, author, document name, and image description.
The output PDF file conforms to the PDF 1.1 specification or PDF 1.2 if using
Zip/Deflate compression.
The Portable Document Format (PDF) specification is copyrighted by Adobe
Systems, Incorporated. Todos derechos reservados.
Here is a listing of the usage example and the options to the tiff2pdf
program that is part of the libtiff distribution. Options followed by
a colon have a required argument.
usage: tiff2pdf [options] input.tif
options:
-o: output to file name
-j: compress with JPEG (requires libjpeg configured with libtiff)
-z: compress with Zip/Deflate (requires zlib configured with libtiff)
-q: compression quality
-n: no compressed data passthrough
-d: do not compress (decompress)
-i: invert colors
-u: set distance unit, 'i' for inch, 'm' for centimeter
-x: set x resolution default
-y: set y resolution default
-w: width in units
-l: length in units
-r: 'd' for resolution default, 'o' for resolution override
-p: paper size, eg "letter", "legal", "a4"
-F: make the tiff fill the PDF page
-f: set pdf "fit window" user preference
-b: set PDF "Interpolate" user preference
-e: date, overrides image or current date/time default, YYYYMMDDHHMMSS
-c: creator, overrides image software default
-a: author, overrides image artist default
-t: title, overrides image document name default
-s: subject, overrides image image description default
-k: keywords
-h: usage
examples:
tiff2pdf -o output.pdf input.tiff
The above example would generate the file output.pdf from input.tiff.
tiff2pdf input.tiff
The above example would generate PDF output from input.tiff and write it
to standard output.
tiff2pdf -j -p letter -o output.pdf input.tiff
The above example would generate the file output.pdf from input.tiff,
putting the image pages on a letter sized page, compressing the output
with JPEG.
Please report bugs through:
http://bugzilla.remotesensing.org/buglist.cgi?product=libtiff
See also libtiff.3t, tiffcp.
*/
int tiff2pdf_match_paper_size(float *width, float *length, char *papersize)
{
size_t i, len;
const char *sizes[] = {
"LETTER", "A4", "LEGAL",
"EXECUTIVE", "LETTER", "LEGAL", "LEDGER", "TABLOID",
"A", "B", "C", "D", "E", "F", "G", "H", "J", "K",
"A10", "A9", "A8", "A7", "A6", "A5", "A4", "A3", "A2", "A1", "A0",
"2A0", "4A0", "2A", "4A",
"B10", "B9", "B8", "B7", "B6", "B5", "B4", "B3", "B2", "B1", "B0",
"JISB10", "JISB9", "JISB8", "JISB7", "JISB6", "JISB5", "JISB4",
"JISB3", "JISB2", "JISB1", "JISB0",
"C10", "C9", "C8", "C7", "C6", "C5", "C4", "C3", "C2", "C1", "C0",
"RA2", "RA1", "RA0", "SRA4", "SRA3", "SRA2", "SRA1", "SRA0",
"A3EXTRA", "A4EXTRA",
"STATEMENT", "FOLIO", "QUARTO",
nullptr
} ;
const int widths[] = {
612, 595, 612,
522, 612, 612, 792, 792,
612, 792, 1224, 1584, 2448, 2016, 792, 2016, 2448, 2880,
74, 105, 147, 210, 298, 420, 595, 842, 1191, 1684, 2384, 3370, 4768, 3370, 4768,
88, 125, 176, 249, 354, 499, 709, 1001, 1417, 2004, 2835,
91, 128, 181, 258, 363, 516, 729, 1032, 1460, 2064, 2920,
79, 113, 162, 230, 323, 459, 649, 918, 1298, 1298, 2599,
1219, 1729, 2438, 638, 907, 1276, 1814, 2551,
914, 667,
396, 612, 609,
0
};
const int lengths[] = {
792, 842, 1008,
756, 792, 1008, 1224, 1224,
792, 1224, 1584, 2448, 3168, 2880, 6480, 10296, 12672, 10296,
105, 147, 210, 298, 420, 595, 842, 1191, 1684, 2384, 3370, 4768, 6741, 4768, 6741,
125, 176, 249, 354, 499, 709, 1001, 1417, 2004, 2835, 4008,
128, 181, 258, 363, 516, 729, 1032, 1460, 2064, 2920, 4127,
113, 162, 230, 323, 459, 649, 918, 1298, 1837, 1837, 3677,
1729, 2438, 3458, 907, 1276, 1814, 2551, 3628,
1262, 914,
612, 936, 780,
0
};
len = strlen(papersize);
for (i = 0; i < len; i++) {
papersize[i] = toupper(papersize[i]);
}
for (i = 0; sizes[i] != nullptr; i++) {
if (strcmp((const char *)papersize, sizes[i]) == 0) {
*width = (float)widths[i];
*length = (float)lengths[i];
return (1);
}
}
return (0);
}
/*
* This function allocates and initializes a T2P context struct pointer.
*/
T2P *t2p_init()
{
T2P *t2p = (T2P *) _TIFFmalloc(sizeof(T2P));
if (t2p == nullptr) {
TIFFError(
TIFF2PDF_MODULE,
"Can't allocate %lu bytes of memory for t2p_init",
(unsigned long) sizeof(T2P));
return ((T2P *) nullptr);
}
_TIFFmemset(t2p, 0x00, sizeof(T2P));
t2p->pdf_majorversion = 1;
t2p->pdf_minorversion = 1;
t2p->pdf_defaultxres = 300.0;
t2p->pdf_defaultyres = 300.0;
t2p->pdf_defaultpagewidth = 612.0;
t2p->pdf_defaultpagelength = 792.0;
t2p->pdf_xrefcount = 3; /* Catalog, Info, Pages */
return (t2p);
}
/*
* This function frees a T2P context struct pointer and any allocated data fields of it.
*/
void t2p_free(T2P *t2p)
{
int i = 0;
if (t2p != nullptr) {
if (t2p->pdf_xrefoffsets != nullptr) {
_TIFFfree((tdata_t) t2p->pdf_xrefoffsets);
}
if (t2p->tiff_pages != nullptr) {
_TIFFfree((tdata_t) t2p->tiff_pages);
}
for (i = 0; i < t2p->tiff_pagecount; i++) {
if (t2p->tiff_tiles[i].tiles_tiles != nullptr) {
_TIFFfree((tdata_t) t2p->tiff_tiles[i].tiles_tiles);
}
}
if (t2p->tiff_tiles != nullptr) {
_TIFFfree((tdata_t) t2p->tiff_tiles);
}
if (t2p->pdf_palette != nullptr) {
_TIFFfree((tdata_t) t2p->pdf_palette);
}
#ifdef OJPEG_SUPPORT
if (t2p->pdf_ojpegdata != nullptr) {
_TIFFfree((tdata_t) t2p->pdf_ojpegdata);
}
#endif
_TIFFfree((tdata_t) t2p);
}
return;
}
/*
This function validates the values of a T2P context struct pointer
before calling t2p_write_pdf with it.
*/
void t2p_validate(T2P *t2p)
{
#ifdef JPEG_SUPPORT
if (t2p->pdf_defaultcompression == T2P_COMPRESS_JPEG) {
if (t2p->pdf_defaultcompressionquality > 100 ||
t2p->pdf_defaultcompressionquality < 1) {
t2p->pdf_defaultcompressionquality = 0;
}
}
#endif
#ifdef ZIP_SUPPORT
if (t2p->pdf_defaultcompression == T2P_COMPRESS_ZIP) {
uint16 m = t2p->pdf_defaultcompressionquality % 100;
if (t2p->pdf_defaultcompressionquality / 100 > 9 ||
(m > 1 && m < 10) || m > 15) {
t2p->pdf_defaultcompressionquality = 0;
}
if (t2p->pdf_defaultcompressionquality % 100 != 0) {
t2p->pdf_defaultcompressionquality /= 100;
t2p->pdf_defaultcompressionquality *= 100;
TIFFError(
TIFF2PDF_MODULE,
"PNG Group predictor differencing not implemented, assuming compression quality %u",
t2p->pdf_defaultcompressionquality);
}
t2p->pdf_defaultcompressionquality %= 100;
if (t2p->pdf_minorversion < 2) {
t2p->pdf_minorversion = 2;
}
}
#endif
(void)0;
return;
}
/*
This function scans the input TIFF file for pages. It attempts
to determine which IFD's of the TIFF file contain image document
pages. For each, it gathers some information that has to do
with the output of the PDF document as a whole.
*/
void t2p_read_tiff_init(T2P *t2p, TIFF *input)
{
tdir_t directorycount = 0;
tdir_t i = 0;
uint16 pagen = 0;
uint16 paged = 0;
uint16 xuint16 = 0;
directorycount = TIFFNumberOfDirectories(input);
t2p->tiff_pages = (T2P_PAGE *) _TIFFmalloc(directorycount * sizeof(T2P_PAGE));
if (t2p->tiff_pages == nullptr) {
TIFFError(
TIFF2PDF_MODULE,
"Can't allocate %lu bytes of memory for tiff_pages array, %s",
(unsigned long) directorycount * sizeof(T2P_PAGE),
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
_TIFFmemset(t2p->tiff_pages, 0x00, directorycount * sizeof(T2P_PAGE));
t2p->tiff_tiles = (T2P_TILES *) _TIFFmalloc(directorycount * sizeof(T2P_TILES));
if (t2p->tiff_tiles == nullptr) {
TIFFError(
TIFF2PDF_MODULE,
"Can't allocate %lu bytes of memory for tiff_tiles array, %s",
(unsigned long) directorycount * sizeof(T2P_TILES),
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
_TIFFmemset(t2p->tiff_tiles, 0x00, directorycount * sizeof(T2P_TILES));
for (i = 0; i < directorycount; i++) {
uint32 subfiletype = 0;
if (!TIFFSetDirectory(input, i)) {
TIFFError(
TIFF2PDF_MODULE,
"Can't set directory %u of input file %s",
i,
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
if (TIFFGetField(input, TIFFTAG_PAGENUMBER, &pagen, &paged)) {
if ((pagen > paged) && (paged != 0)) {
t2p->tiff_pages[t2p->tiff_pagecount].page_number =
paged;
} else {
t2p->tiff_pages[t2p->tiff_pagecount].page_number =
pagen;
}
goto ispage2;
}
if (TIFFGetField(input, TIFFTAG_SUBFILETYPE, &subfiletype)) {
if (((subfiletype & FILETYPE_PAGE) != 0)
|| (subfiletype == 0)) {
goto ispage;
} else {
goto isnotpage;
}
}
if (TIFFGetField(input, TIFFTAG_OSUBFILETYPE, &subfiletype)) {
if ((subfiletype == OFILETYPE_IMAGE)
|| (subfiletype == OFILETYPE_PAGE)
|| (subfiletype == 0)) {
goto ispage;
} else {
goto isnotpage;
}
}
ispage:
t2p->tiff_pages[t2p->tiff_pagecount].page_number = t2p->tiff_pagecount;
ispage2:
t2p->tiff_pages[t2p->tiff_pagecount].page_directory = i;
if (TIFFIsTiled(input)) {
t2p->tiff_pages[t2p->tiff_pagecount].page_tilecount =
TIFFNumberOfTiles(input);
}
t2p->tiff_pagecount++;
isnotpage:
(void)0;
}
qsort((void *) t2p->tiff_pages, t2p->tiff_pagecount,
sizeof(T2P_PAGE), t2p_cmp_t2p_page);
for (i = 0; i < t2p->tiff_pagecount; i++) {
t2p->pdf_xrefcount += 5;
TIFFSetDirectory(input, t2p->tiff_pages[i].page_directory);
if ((TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &xuint16)
&& (xuint16 == PHOTOMETRIC_PALETTE))
|| TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)) {
t2p->tiff_pages[i].page_extra++;
t2p->pdf_xrefcount++;
}
#ifdef ZIP_SUPPORT
if (TIFFGetField(input, TIFFTAG_COMPRESSION, &xuint16)) {
if ((xuint16 == COMPRESSION_DEFLATE ||
xuint16 == COMPRESSION_ADOBE_DEFLATE) &&
((t2p->tiff_pages[i].page_tilecount != 0)
|| TIFFNumberOfStrips(input) == 1) &&
(t2p->pdf_nopassthrough == 0)) {
if (t2p->pdf_minorversion < 2) {
t2p->pdf_minorversion = 2;
}
}
}
#endif
if (TIFFGetField(input, TIFFTAG_TRANSFERFUNCTION,
&(t2p->tiff_transferfunction[0]),
&(t2p->tiff_transferfunction[1]),
&(t2p->tiff_transferfunction[2]))) {
if (t2p->tiff_transferfunction[1] !=
t2p->tiff_transferfunction[0]) {
t2p->tiff_transferfunctioncount = 3;
t2p->tiff_pages[i].page_extra += 4;
t2p->pdf_xrefcount += 4;
} else {
t2p->tiff_transferfunctioncount = 1;
t2p->tiff_pages[i].page_extra += 2;
t2p->pdf_xrefcount += 2;
}
if (t2p->pdf_minorversion < 2) {
t2p->pdf_minorversion = 2;
}
} else {
t2p->tiff_transferfunctioncount = 0;
}
if (TIFFGetField(
input,
TIFFTAG_ICCPROFILE,
&(t2p->tiff_iccprofilelength),
&(t2p->tiff_iccprofile)) != 0) {
t2p->tiff_pages[i].page_extra++;
t2p->pdf_xrefcount++;
if (t2p->pdf_minorversion < 3) {
t2p->pdf_minorversion = 3;
}
}
t2p->tiff_tiles[i].tiles_tilecount =
t2p->tiff_pages[i].page_tilecount;
if ((TIFFGetField(input, TIFFTAG_PLANARCONFIG, &xuint16) != 0)
&& (xuint16 == PLANARCONFIG_SEPARATE)) {
TIFFGetField(input, TIFFTAG_SAMPLESPERPIXEL, &xuint16);
t2p->tiff_tiles[i].tiles_tilecount /= xuint16;
}
if (t2p->tiff_tiles[i].tiles_tilecount > 0) {
t2p->pdf_xrefcount +=
(t2p->tiff_tiles[i].tiles_tilecount - 1) * 2;
TIFFGetField(input,
TIFFTAG_TILEWIDTH,
&(t2p->tiff_tiles[i].tiles_tilewidth));
TIFFGetField(input,
TIFFTAG_TILELENGTH,
&(t2p->tiff_tiles[i].tiles_tilelength));
t2p->tiff_tiles[i].tiles_tiles =
(T2P_TILE *) _TIFFmalloc(
t2p->tiff_tiles[i].tiles_tilecount
* sizeof(T2P_TILE));
if (t2p->tiff_tiles[i].tiles_tiles == nullptr) {
TIFFError(
TIFF2PDF_MODULE,
"Can't allocate %lu bytes of memory for t2p_read_tiff_init, %s",
(unsigned long) t2p->tiff_tiles[i].tiles_tilecount * sizeof(T2P_TILE),
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
}
}
return;
}
/*
* This function is used by qsort to sort a T2P_PAGE* array of page structures
* by page number.
*/
int t2p_cmp_t2p_page(const void *e1, const void *e2)
{
return (((T2P_PAGE *)e1)->page_number - ((T2P_PAGE *)e2)->page_number);
}
/*
This function sets the input directory to the directory of a given
page and determines information about the image. It checks
the image characteristics to determine if it is possible to convert
the image data into a page of PDF output, setting values of the T2P
struct for this page. It determines what color space is used in
the output PDF to represent the image.
It determines if the image can be converted as raw data without
requiring transcoding of the image data.
*/
void t2p_read_tiff_data(T2P *t2p, TIFF *input)
{
int i = 0;
uint16 *r;
uint16 *g;
uint16 *b;
uint16 *a;
uint16 xuint16;
uint16 *xuint16p;
float *xfloatp;
t2p->pdf_transcode = T2P_TRANSCODE_ENCODE;
t2p->pdf_sample = T2P_SAMPLE_NOTHING;
t2p->pdf_switchdecode = t2p->pdf_colorspace_invert;
TIFFSetDirectory(input, t2p->tiff_pages[t2p->pdf_page].page_directory);
TIFFGetField(input, TIFFTAG_IMAGEWIDTH, &(t2p->tiff_width));
if (t2p->tiff_width == 0) {
TIFFError(
TIFF2PDF_MODULE,
"No support for %s with zero width",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
TIFFGetField(input, TIFFTAG_IMAGELENGTH, &(t2p->tiff_length));
if (t2p->tiff_length == 0) {
TIFFError(
TIFF2PDF_MODULE,
"No support for %s with zero length",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
if (TIFFGetField(input, TIFFTAG_COMPRESSION, &(t2p->tiff_compression)) == 0) {
TIFFError(
TIFF2PDF_MODULE,
"No support for %s with no compression tag",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
if (TIFFIsCODECConfigured(t2p->tiff_compression) == 0) {
TIFFError(
TIFF2PDF_MODULE,
"No support for %s with compression type %u: not configured",
TIFFFileName(input),
t2p->tiff_compression
);
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
TIFFGetFieldDefaulted(input, TIFFTAG_BITSPERSAMPLE, &(t2p->tiff_bitspersample));
switch (t2p->tiff_bitspersample) {
case 1:
case 2:
case 4:
case 8:
break;
case 0:
TIFFWarning(
TIFF2PDF_MODULE,
"Image %s has 0 bits per sample, assuming 1",
TIFFFileName(input));
t2p->tiff_bitspersample = 1;
break;
default:
TIFFError(
TIFF2PDF_MODULE,
"No support for %s with %u bits per sample",
TIFFFileName(input),
t2p->tiff_bitspersample);
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
TIFFGetFieldDefaulted(input, TIFFTAG_SAMPLESPERPIXEL, &(t2p->tiff_samplesperpixel));
if (t2p->tiff_samplesperpixel > 4) {
TIFFError(
TIFF2PDF_MODULE,
"No support for %s with %u samples per pixel",
TIFFFileName(input),
t2p->tiff_samplesperpixel);
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
if (t2p->tiff_samplesperpixel == 0) {
TIFFWarning(
TIFF2PDF_MODULE,
"Image %s has 0 samples per pixel, assuming 1",
TIFFFileName(input));
t2p->tiff_samplesperpixel = 1;
}
if (TIFFGetField(input, TIFFTAG_SAMPLEFORMAT, &xuint16) != 0) {
switch (xuint16) {
case 0:
case 1:
case 4:
break;
default:
TIFFError(
TIFF2PDF_MODULE,
"No support for %s with sample format %u",
TIFFFileName(input),
xuint16);
t2p->t2p_error = T2P_ERR_ERROR;
return;
break;
}
}
TIFFGetFieldDefaulted(input, TIFFTAG_FILLORDER, &(t2p->tiff_fillorder));
if (TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &(t2p->tiff_photometric)) == 0) {
TIFFError(
TIFF2PDF_MODULE,
"No support for %s with no photometric interpretation tag",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
switch (t2p->tiff_photometric) {
case PHOTOMETRIC_MINISWHITE:
case PHOTOMETRIC_MINISBLACK:
if (t2p->tiff_bitspersample == 1) {
t2p->pdf_colorspace = T2P_CS_BILEVEL;
if (t2p->tiff_photometric == PHOTOMETRIC_MINISWHITE) {
t2p->pdf_switchdecode ^= 1;
}
} else {
t2p->pdf_colorspace = T2P_CS_GRAY;
if (t2p->tiff_photometric == PHOTOMETRIC_MINISWHITE) {
t2p->pdf_switchdecode ^= 1;
}
}
break;
case PHOTOMETRIC_RGB:
t2p->pdf_colorspace = T2P_CS_RGB;
if (t2p->tiff_samplesperpixel == 3) {
break;
}
if (TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)) {
if (xuint16 == 1) {
goto photometric_palette;
}
}
if (t2p->tiff_samplesperpixel > 3) {
if (t2p->tiff_samplesperpixel == 4) {
t2p->pdf_colorspace = T2P_CS_RGB;
if (TIFFGetField(input,
TIFFTAG_EXTRASAMPLES,
&xuint16, &xuint16p)
&& xuint16 == 1) {
if (xuint16p[0] == EXTRASAMPLE_ASSOCALPHA) {
t2p->pdf_sample = T2P_SAMPLE_RGBAA_TO_RGB;
break;
}
if (xuint16p[0] == EXTRASAMPLE_UNASSALPHA) {
t2p->pdf_sample = T2P_SAMPLE_RGBA_TO_RGB;
break;
}
TIFFWarning(
TIFF2PDF_MODULE,
"RGB image %s has 4 samples per pixel, assuming RGBA",
TIFFFileName(input));
break;
}
t2p->pdf_colorspace = T2P_CS_CMYK;
t2p->pdf_switchdecode ^= 1;
TIFFWarning(
TIFF2PDF_MODULE,
"RGB image %s has 4 samples per pixel, assuming inverse CMYK",
TIFFFileName(input));
break;
} else {
TIFFError(
TIFF2PDF_MODULE,
"No support for RGB image %s with %u samples per pixel",
TIFFFileName(input),
t2p->tiff_samplesperpixel);
t2p->t2p_error = T2P_ERR_ERROR;
break;
}
} else {
TIFFError(
TIFF2PDF_MODULE,
"No support for RGB image %s with %u samples per pixel",
TIFFFileName(input),
t2p->tiff_samplesperpixel);
t2p->t2p_error = T2P_ERR_ERROR;
break;
}
case PHOTOMETRIC_PALETTE:
photometric_palette:
if (t2p->tiff_samplesperpixel != 1) {
TIFFError(
TIFF2PDF_MODULE,
"No support for palettized image %s with not one sample per pixel",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
t2p->pdf_colorspace = T2P_CS_RGB | T2P_CS_PALETTE;
t2p->pdf_palettesize = 0x0001 << t2p->tiff_bitspersample;
if (!TIFFGetField(input, TIFFTAG_COLORMAP, &r, &g, &b)) {
TIFFError(
TIFF2PDF_MODULE,
"Palettized image %s has no color map",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
if (t2p->pdf_palette != nullptr) {
_TIFFfree(t2p->pdf_palette);
t2p->pdf_palette = nullptr;
}
t2p->pdf_palette = (unsigned char *)
_TIFFmalloc(t2p->pdf_palettesize * 3);
if (t2p->pdf_palette == nullptr) {
TIFFError(
TIFF2PDF_MODULE,
"Can't allocate %u bytes of memory for t2p_read_tiff_image, %s",
t2p->pdf_palettesize,
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
for (i = 0; i < t2p->pdf_palettesize; i++) {
t2p->pdf_palette[(i * 3)] = (unsigned char)(r[i] >> 8);
t2p->pdf_palette[(i * 3) + 1] = (unsigned char)(g[i] >> 8);
t2p->pdf_palette[(i * 3) + 2] = (unsigned char)(b[i] >> 8);
}
t2p->pdf_palettesize *= 3;
break;
case PHOTOMETRIC_SEPARATED:
if (TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)) {
if (xuint16 == 1) {
goto photometric_palette_cmyk;
}
}
if (TIFFGetField(input, TIFFTAG_INKSET, &xuint16)) {
if (xuint16 != INKSET_CMYK) {
TIFFError(
TIFF2PDF_MODULE,
"No support for %s because its inkset is not CMYK",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
}
if (t2p->tiff_samplesperpixel == 4) {
t2p->pdf_colorspace = T2P_CS_CMYK;
} else {
TIFFError(
TIFF2PDF_MODULE,
"No support for %s because it has %u samples per pixel",
TIFFFileName(input),
t2p->tiff_samplesperpixel);
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
break;
photometric_palette_cmyk:
if (t2p->tiff_samplesperpixel != 1) {
TIFFError(
TIFF2PDF_MODULE,
"No support for palettized CMYK image %s with not one sample per pixel",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
t2p->pdf_colorspace = T2P_CS_CMYK | T2P_CS_PALETTE;
t2p->pdf_palettesize = 0x0001 << t2p->tiff_bitspersample;
if (!TIFFGetField(input, TIFFTAG_COLORMAP, &r, &g, &b, &a)) {
TIFFError(
TIFF2PDF_MODULE,
"Palettized image %s has no color map",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
if (t2p->pdf_palette != nullptr) {
_TIFFfree(t2p->pdf_palette);
t2p->pdf_palette = nullptr;
}
t2p->pdf_palette = (unsigned char *)
_TIFFmalloc(t2p->pdf_palettesize * 4);
if (t2p->pdf_palette == nullptr) {
TIFFError(
TIFF2PDF_MODULE,
"Can't allocate %u bytes of memory for t2p_read_tiff_image, %s",
t2p->pdf_palettesize,
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
}
for (i = 0; i < t2p->pdf_palettesize; i++) {
t2p->pdf_palette[(i * 4)] = (unsigned char)(r[i] >> 8);
t2p->pdf_palette[(i * 4) + 1] = (unsigned char)(g[i] >> 8);
t2p->pdf_palette[(i * 4) + 2] = (unsigned char)(b[i] >> 8);
t2p->pdf_palette[(i * 4) + 3] = (unsigned char)(a[i] >> 8);
}
t2p->pdf_palettesize *= 4;
break;
case PHOTOMETRIC_YCBCR:
t2p->pdf_colorspace = T2P_CS_RGB;
if (t2p->tiff_samplesperpixel == 1) {
t2p->pdf_colorspace = T2P_CS_GRAY;
t2p->tiff_photometric = PHOTOMETRIC_MINISBLACK;
break;
}
t2p->pdf_sample = T2P_SAMPLE_YCBCR_TO_RGB;
#ifdef JPEG_SUPPORT
if (t2p->pdf_defaultcompression == T2P_COMPRESS_JPEG) {
t2p->pdf_sample = T2P_SAMPLE_NOTHING;
}
#endif
break;
case PHOTOMETRIC_CIELAB:
t2p->pdf_labrange[0] = -127;
t2p->pdf_labrange[1] = 127;
t2p->pdf_labrange[2] = -127;
t2p->pdf_labrange[3] = 127;
t2p->pdf_sample = T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED;
t2p->pdf_colorspace = T2P_CS_LAB;
break;
case PHOTOMETRIC_ICCLAB:
t2p->pdf_labrange[0] = 0;
t2p->pdf_labrange[1] = 255;
t2p->pdf_labrange[2] = 0;
t2p->pdf_labrange[3] = 255;
t2p->pdf_colorspace = T2P_CS_LAB;
break;
case PHOTOMETRIC_ITULAB:
t2p->pdf_labrange[0] = -85;
t2p->pdf_labrange[1] = 85;
t2p->pdf_labrange[2] = -75;
t2p->pdf_labrange[3] = 124;
t2p->pdf_sample = T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED;
t2p->pdf_colorspace = T2P_CS_LAB;
break;
case PHOTOMETRIC_LOGL:
case PHOTOMETRIC_LOGLUV:
TIFFError(
TIFF2PDF_MODULE,
"No support for %s with photometric interpretation LogL/LogLuv",
TIFFFileName(input));
t2p->t2p_error = T2P_ERR_ERROR;
return;
default:
TIFFError(
TIFF2PDF_MODULE,
"No support for %s with photometric interpretation %u",
TIFFFileName(input),
t2p->tiff_photometric);