-
Notifications
You must be signed in to change notification settings - Fork 3
/
imtiff.c
2756 lines (2275 loc) · 68.9 KB
/
imtiff.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
#include <tiffio.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "imtiff.h"
#include "imext.h"
/* 4.3.0 (aka 20210416) made the old tifflib specific types deprecated in
favour of the C99 <inttypes.h> types.
*/
#if TIFFLIB_VERSION >= 20210416
typedef uint8_t tf_uint8;
typedef uint16_t tf_uint16;
typedef uint32_t tf_uint32;
#else
typedef uint8 tf_uint8;
typedef uint16 tf_uint16;
typedef uint32 tf_uint32;
#endif
#if TIFFLIB_VERSION >= 20221213
# define USE_TIFFOPEN_OPTIONS
#endif
/*
=head1 NAME
tiff.c - implements reading and writing tiff files, uses io layer.
=head1 SYNOPSIS
io_glue *ig = io_new_fd( fd );
i_img *im = i_readtiff_wiol(ig, -1); // no limit on how much is read
// or
io_glue *ig = io_new_fd( fd );
return_code = i_writetiff_wiol(im, ig);
=head1 DESCRIPTION
tiff.c implements the basic functions to read and write tiff files.
It uses the iolayer and needs either a seekable source or an entire
memory mapped buffer.
=head1 FUNCTION REFERENCE
Some of these functions are internal.
=over
=cut
*/
#define CLAMP8(x) ((x) < 0 ? 0 : (x) > 255 ? 255 : (x))
#define CLAMP16(x) ((x) < 0 ? 0 : (x) > 65535 ? 65535 : (x))
#define Sample16To8(num) ((num) / 257)
struct tag_name {
const char *name;
tf_uint32 tag;
};
static i_img *read_one_rgb_tiled(TIFF *tif, i_img_dim width, i_img_dim height, int allow_incomplete);
static i_img *read_one_rgb_lines(TIFF *tif, i_img_dim width, i_img_dim height, int allow_incomplete);
static const struct tag_name
text_tag_names[] =
{
{ "tiff_documentname", TIFFTAG_DOCUMENTNAME, },
{ "tiff_imagedescription", TIFFTAG_IMAGEDESCRIPTION, },
{ "tiff_make", TIFFTAG_MAKE, },
{ "tiff_model", TIFFTAG_MODEL, },
{ "tiff_pagename", TIFFTAG_PAGENAME, },
{ "tiff_software", TIFFTAG_SOFTWARE, },
{ "tiff_datetime", TIFFTAG_DATETIME, },
{ "tiff_artist", TIFFTAG_ARTIST, },
{ "tiff_hostcomputer", TIFFTAG_HOSTCOMPUTER, },
};
static const struct tag_name
compress_values[] =
{
{ "none", COMPRESSION_NONE },
{ "ccittrle", COMPRESSION_CCITTRLE },
{ "fax3", COMPRESSION_CCITTFAX3 },
{ "t4", COMPRESSION_CCITTFAX3 },
{ "fax4", COMPRESSION_CCITTFAX4 },
{ "t6", COMPRESSION_CCITTFAX4 },
{ "lzw", COMPRESSION_LZW },
{ "oldjpeg", COMPRESSION_OJPEG },
{ "jpeg", COMPRESSION_JPEG },
{ "next", COMPRESSION_NEXT },
{ "packbits", COMPRESSION_PACKBITS },
{ "thunder", COMPRESSION_THUNDERSCAN },
{ "deflate", COMPRESSION_ADOBE_DEFLATE },
{ "zip", COMPRESSION_ADOBE_DEFLATE },
{ "pixarlog", COMPRESSION_PIXARLOG },
{ "oldzip", COMPRESSION_DEFLATE },
{ "ccittrlew", COMPRESSION_CCITTRLEW },
{ "jbig", COMPRESSION_JBIG },
{ "sgilog", COMPRESSION_SGILOG },
{ "sgilog24", COMPRESSION_SGILOG24 },
{ "lerc", COMPRESSION_LERC },
{ "lzma", COMPRESSION_LZMA },
{ "zstd", COMPRESSION_ZSTD },
{ "webp", COMPRESSION_WEBP },
};
static const int compress_value_count =
sizeof(compress_values) / sizeof(*compress_values);
static const struct tag_name
sample_format_values[] =
{
{ "uint", SAMPLEFORMAT_UINT },
{ "int", SAMPLEFORMAT_INT },
{ "ieeefp", SAMPLEFORMAT_IEEEFP },
{ "undefined", SAMPLEFORMAT_VOID },
};
static const int sample_format_value_count =
sizeof(sample_format_values) / sizeof(*sample_format_values);
typedef struct read_state_tag read_state_t;
/* the setup function creates the image object, allocates the line buffer */
typedef int (*read_setup_t)(read_state_t *state);
/* the putter writes the image data provided by the getter to the
image, x, y, width, height describe the target area of the image,
extras is the extra number of pixels stored for each scanline in
the raster buffer, (for tiles against the right side of the
image) */
typedef int (*read_putter_t)(read_state_t *state, i_img_dim x, i_img_dim y,
i_img_dim width, i_img_dim height, int extras);
/* reads from a tiled or strip image and calls the putter.
This may need a second type for handling non-contiguous images
at some point */
typedef int (*read_getter_t)(read_state_t *state, read_putter_t putter);
struct read_state_tag {
TIFF *tif;
i_img *img;
void *raster;
i_img_dim pixels_read;
int allow_incomplete;
void *line_buf;
tf_uint32 width, height;
tf_uint16 bits_per_sample;
tf_uint16 photometric;
/* the total number of channels (samples per pixel) */
int samples_per_pixel;
/* if non-zero, which channel is the alpha channel, typically 3 for rgb */
int alpha_chan;
/* whether or not to scale the color channels based on the alpha
channel. TIFF has 2 types of alpha channel, if the alpha channel
we use is EXTRASAMPLE_ASSOCALPHA then the color data will need to
be scaled to match Imager's conventions */
int scale_alpha;
/* number of color samples (not including alpha) */
int color_channels;
/* SampleFormat is 2 */
int sample_signed;
int sample_format;
};
static int tile_contig_getter(read_state_t *state, read_putter_t putter);
static int strip_contig_getter(read_state_t *state, read_putter_t putter);
static int setup_paletted(read_state_t *state);
static int paletted_putter8(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
static int paletted_putter4(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
static int setup_16_rgb(read_state_t *state);
static int setup_16_grey(read_state_t *state);
static int putter_16(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
static int setup_8_rgb(read_state_t *state);
static int setup_8_grey(read_state_t *state);
static int putter_8(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
static int setup_32_rgb(read_state_t *state);
static int setup_32_grey(read_state_t *state);
static int putter_32(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
static int setup_bilevel(read_state_t *state);
static int putter_bilevel(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
static int setup_cmyk8(read_state_t *state);
static int putter_cmyk8(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
static int setup_cmyk16(read_state_t *state);
static int putter_cmyk16(read_state_t *, i_img_dim, i_img_dim, i_img_dim, i_img_dim, int);
static void
rgb_channels(read_state_t *state, int *out_channels);
static void
grey_channels(read_state_t *state, int *out_channels);
static void
cmyk_channels(read_state_t *state, int *out_channels);
static void
fallback_rgb_channels(TIFF *tif, int *channels, int *alpha_chan);
static const int text_tag_count =
sizeof(text_tag_names) / sizeof(*text_tag_names);
#define TIFFIO_MAGIC 0xC6A340CC
typedef struct {
unsigned magic;
io_glue *ig;
io_glue *warn_buf;
} tiffio_context_t;
static void
do_warn_handler(tiffio_context_t *c, const char *module, const char *fmt, va_list ap) {
char buf[200];
if (c->magic != TIFFIO_MAGIC)
return;
buf[0] = '\0';
vsnprintf(buf, sizeof(buf), fmt, ap);
mm_log((1, "tiff warning %s\n", buf));
if (!c->warn_buf)
c->warn_buf = io_new_bufchain();
i_io_write(c->warn_buf, module, strlen(module));
i_io_write(c->warn_buf, ": ", 2);
i_io_write(c->warn_buf, buf, strlen(buf));
i_io_write(c->warn_buf, "\n", 1);
}
#ifdef USE_TIFFOPEN_OPTIONS
static int
error_handler_extr(TIFF *tif, void *user_data, const char *module,
const char *fmt, va_list ap) {
(void)tif;
(void)user_data;
(void)module;
mm_log((1, "tiff error fmt %s\n", fmt));
i_push_errorvf(0, fmt, ap);
return 1;
}
static int
warn_handler_extr(TIFF *tif, void *user_data, const char *module,
const char *fmt, va_list ap) {
tiffio_context_t *c = (tiffio_context_t *)user_data;
do_warn_handler(c, module, fmt, ap);
return 1;
}
#else
static void
error_handler(char const *module, char const *fmt, va_list ap) {
(void)module;
mm_log((1, "tiff error fmt %s\n", fmt));
i_push_errorvf(0, fmt, ap);
}
static void
warn_handler_ex(thandle_t h, const char *module, const char *fmt, va_list ap) {
tiffio_context_t *c = (tiffio_context_t *)h;
do_warn_handler(c, module, fmt, ap);
}
#endif
static void
tiffio_context_init(tiffio_context_t *c, io_glue *ig);
static void
tiffio_context_final(tiffio_context_t *c);
static toff_t
sizeproc(thandle_t h) {
io_glue *ig = ((tiffio_context_t *)h)->ig;
/* iolayer doesn't have a size callback, use seek() to find
the end
*/
toff_t orig_off = i_io_seek(ig, 0, SEEK_CUR);
if (orig_off < 0) {
i_push_error(errno, "seek to current failed");
return -1;
}
off_t size = i_io_seek(ig, 0, SEEK_END);
if (size < 0) {
i_push_error(errno, "seek to end failed");
return -1;
}
if (i_io_seek(ig, orig_off, SEEK_SET) < 0) {
i_push_error(errno, "seek restore failed");
return -1;
}
return size;
}
/*
=item comp_seek(h, o, w)
Compatability for 64 bit systems like latest freebsd (internal)
h - tiff handle, cast an io_glue object
o - offset
w - whence
=cut
*/
static
toff_t
comp_seek(thandle_t h, toff_t o, int w) {
io_glue *ig = ((tiffio_context_t *)h)->ig;
return (toff_t) i_io_seek(ig, o, w);
}
/*
=item comp_mmap(thandle_t, tdata_t*, toff_t*)
Dummy mmap stub.
This shouldn't ever be called but newer tifflibs want it anyway.
=cut
*/
static
int
comp_mmap(thandle_t h, tdata_t*p, toff_t*off) {
(void)h;
(void)p;
(void)off;
return -1;
}
/*
=item comp_munmap(thandle_t h, tdata_t p, toff_t off)
Dummy munmap stub.
This shouldn't ever be called but newer tifflibs want it anyway.
=cut
*/
static void
comp_munmap(thandle_t h, tdata_t p, toff_t off) {
(void)h;
(void)p;
(void)off;
/* do nothing */
}
static tsize_t
comp_read(thandle_t h, tdata_t p, tsize_t size) {
return i_io_read(((tiffio_context_t *)h)->ig, p, size);
}
static tsize_t
comp_write(thandle_t h, tdata_t p, tsize_t size) {
return i_io_write(((tiffio_context_t *)h)->ig, p, size);
}
static int
comp_close(thandle_t h) {
return i_io_close(((tiffio_context_t *)h)->ig);
}
#ifndef USE_TIFFOPEN_OPTIONS
static i_mutex_t mutex;
#endif
typedef struct {
TIFF *tif;
tiffio_context_t ctx;
#ifndef USE_TIFFOPEN_OPTIONS
TIFFErrorHandler old_error;
TIFFErrorHandler old_warn;
TIFFErrorHandlerExt old_warn_ext;
#endif
} tiff_state;
static TIFF *
do_tiff_open(tiff_state *state, io_glue *ig, const char *mode) {
memset(state, 0, sizeof(*state));
tiffio_context_init(&state->ctx, ig);
#ifdef USE_TIFFOPEN_OPTIONS
TIFFOpenOptions *options = TIFFOpenOptionsAlloc();
TIFFOpenOptionsSetErrorHandlerExtR(options, error_handler_extr, &state->ctx);
TIFFOpenOptionsSetWarningHandlerExtR(options, warn_handler_extr, &state->ctx);
TIFF *tif = TIFFClientOpenExt("(Iolayer)",
mode,
(thandle_t) &state->ctx,
comp_read,
comp_write,
comp_seek,
comp_close,
sizeproc,
comp_mmap,
comp_munmap,
options);
TIFFOpenOptionsFree(options);
#else
i_mutex_lock(mutex);
state->old_error = TIFFSetErrorHandler(error_handler);
state->old_warn = TIFFSetWarningHandler(NULL);
state->old_warn_ext = TIFFSetWarningHandlerExt(warn_handler_ex);
TIFF *tif = TIFFClientOpen("(Iolayer)",
mode,
(thandle_t) &state->ctx,
comp_read,
comp_write,
comp_seek,
comp_close,
sizeproc,
comp_mmap,
comp_munmap);
if (!tif) {
TIFFSetErrorHandler(state->old_error);
TIFFSetWarningHandler(state->old_warn);
TIFFSetWarningHandlerExt(state->old_warn_ext);
i_mutex_unlock(mutex);
}
#endif
if (!tif) {
tiffio_context_final(&state->ctx);
}
state->tif = tif;
return tif;
}
static void
do_tiff_close(tiff_state *state) {
TIFFClose(state->tif);
#ifndef USE_TIFFOPEN_OPTIONS
TIFFSetErrorHandler(state->old_error);
TIFFSetWarningHandler(state->old_warn);
TIFFSetWarningHandlerExt(state->old_warn_ext);
i_mutex_unlock(mutex);
#endif
tiffio_context_final(&state->ctx);
}
void
i_tiff_init(void) {
#ifndef USE_TIFFOPEN_OPTIONS
mutex = i_mutex_new();
#endif
}
static int save_tiff_tags(TIFF *tif, i_img *im);
static void
pack_4bit_to(unsigned char *dest, const unsigned char *src, i_img_dim count);
static i_img *read_one_tiff(TIFF *tif, int allow_incomplete) {
i_img *im;
tf_uint32 width, height;
tf_uint16 samples_per_pixel;
int tiled;
float xres, yres;
tf_uint16 resunit;
int gotXres, gotYres;
tf_uint16 photometric;
tf_uint16 bits_per_sample;
tf_uint16 planar_config;
tf_uint16 inkset;
tf_uint16 compress;
tf_uint16 sample_format;
int i;
read_state_t state;
read_setup_t setupf = NULL;
read_getter_t getterf = NULL;
read_putter_t putterf = NULL;
int channels = MAXCHANNELS;
size_t sample_size = ~0; /* force failure if some code doesn't set it */
i_img_dim total_pixels;
int samples_integral;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
tiled = TIFFIsTiled(tif);
TIFFGetFieldDefaulted(tif, TIFFTAG_PHOTOMETRIC, &photometric);
TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planar_config);
TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
if (samples_per_pixel == 0) {
i_push_error(0, "invalid image: SamplesPerPixel is 0");
return NULL;
}
TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLEFORMAT, &sample_format);
mm_log((1, "i_readtiff_wiol: width=%d, height=%d, channels=%d\n", width, height, samples_per_pixel));
mm_log((1, "i_readtiff_wiol: %stiled\n", tiled?"":"not "));
mm_log((1, "i_readtiff_wiol: %sbyte swapped\n", TIFFIsByteSwapped(tif)?"":"not "));
total_pixels = (i_img_dim)width * height;
memset(&state, 0, sizeof(state));
state.tif = tif;
state.allow_incomplete = allow_incomplete;
state.width = width;
state.height = height;
state.bits_per_sample = bits_per_sample;
state.samples_per_pixel = samples_per_pixel;
state.photometric = photometric;
state.sample_signed = sample_format == SAMPLEFORMAT_INT;
state.sample_format = sample_format;
samples_integral = sample_format == SAMPLEFORMAT_UINT
|| sample_format == SAMPLEFORMAT_INT
|| sample_format == SAMPLEFORMAT_VOID; /* sample as UINT */
/* yes, this if() is horrible */
if (photometric == PHOTOMETRIC_PALETTE && bits_per_sample <= 8
&& samples_integral) {
setupf = setup_paletted;
if (bits_per_sample == 8)
putterf = paletted_putter8;
else if (bits_per_sample == 4)
putterf = paletted_putter4;
else
mm_log((1, "unsupported paletted bits_per_sample %d\n", bits_per_sample));
sample_size = sizeof(i_sample_t);
channels = 1;
}
else if (bits_per_sample == 16
&& photometric == PHOTOMETRIC_RGB
&& samples_per_pixel >= 3
&& samples_integral) {
setupf = setup_16_rgb;
putterf = putter_16;
sample_size = 2;
rgb_channels(&state, &channels);
}
else if (bits_per_sample == 16
&& photometric == PHOTOMETRIC_MINISBLACK
&& samples_integral) {
setupf = setup_16_grey;
putterf = putter_16;
sample_size = 2;
grey_channels(&state, &channels);
}
else if (bits_per_sample == 8
&& photometric == PHOTOMETRIC_MINISBLACK
&& samples_integral) {
setupf = setup_8_grey;
putterf = putter_8;
sample_size = 1;
grey_channels(&state, &channels);
}
else if (bits_per_sample == 8
&& photometric == PHOTOMETRIC_RGB
&& samples_integral) {
setupf = setup_8_rgb;
putterf = putter_8;
sample_size = 1;
rgb_channels(&state, &channels);
}
else if (bits_per_sample == 32
&& photometric == PHOTOMETRIC_RGB
&& samples_per_pixel >= 3) {
setupf = setup_32_rgb;
putterf = putter_32;
sample_size = sizeof(i_fsample_t);
rgb_channels(&state, &channels);
}
else if (bits_per_sample == 32
&& photometric == PHOTOMETRIC_MINISBLACK) {
setupf = setup_32_grey;
putterf = putter_32;
sample_size = sizeof(i_fsample_t);
grey_channels(&state, &channels);
}
else if (bits_per_sample == 1
&& (photometric == PHOTOMETRIC_MINISBLACK
|| photometric == PHOTOMETRIC_MINISWHITE)
&& samples_per_pixel == 1) {
setupf = setup_bilevel;
putterf = putter_bilevel;
sample_size = sizeof(i_palidx);
channels = 1;
}
else if (bits_per_sample == 8
&& photometric == PHOTOMETRIC_SEPARATED
&& inkset == INKSET_CMYK
&& samples_per_pixel >= 4
&& samples_integral) {
setupf = setup_cmyk8;
putterf = putter_cmyk8;
sample_size = 1;
cmyk_channels(&state, &channels);
}
else if (bits_per_sample == 16
&& photometric == PHOTOMETRIC_SEPARATED
&& inkset == INKSET_CMYK
&& samples_per_pixel >= 4
&& samples_integral) {
setupf = setup_cmyk16;
putterf = putter_cmyk16;
sample_size = 2;
cmyk_channels(&state, &channels);
}
else {
int alpha;
fallback_rgb_channels(tif, &channels, &alpha);
sample_size = 1;
}
if (!i_int_check_image_file_limits(width, height, channels, sample_size)) {
return NULL;
}
if (tiled) {
if (planar_config == PLANARCONFIG_CONTIG)
getterf = tile_contig_getter;
}
else {
if (planar_config == PLANARCONFIG_CONTIG)
getterf = strip_contig_getter;
}
if (setupf && getterf && putterf) {
if (!setupf(&state))
return NULL;
if (!getterf(&state, putterf) || !state.pixels_read) {
if (state.img)
i_img_destroy(state.img);
if (state.raster)
_TIFFfree(state.raster);
if (state.line_buf)
myfree(state.line_buf);
return NULL;
}
if (allow_incomplete && state.pixels_read < total_pixels) {
i_tags_setn(&(state.img->tags), "i_incomplete", 1);
i_tags_setn(&(state.img->tags), "i_lines_read",
state.pixels_read / width);
}
im = state.img;
if (state.raster)
_TIFFfree(state.raster);
if (state.line_buf)
myfree(state.line_buf);
}
else {
if (tiled) {
im = read_one_rgb_tiled(tif, width, height, allow_incomplete);
}
else {
im = read_one_rgb_lines(tif, width, height, allow_incomplete);
}
}
if (!im)
return NULL;
/* general metadata */
i_tags_setn(&im->tags, "tiff_bitspersample", bits_per_sample);
i_tags_setn(&im->tags, "tiff_photometric", photometric);
TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
/* resolution tags */
TIFFGetFieldDefaulted(tif, TIFFTAG_RESOLUTIONUNIT, &resunit);
gotXres = TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres);
gotYres = TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres);
if (gotXres || gotYres) {
if (!gotXres)
xres = yres;
else if (!gotYres)
yres = xres;
i_tags_setn(&im->tags, "tiff_resolutionunit", resunit);
if (resunit == RESUNIT_CENTIMETER) {
/* from dots per cm to dpi */
xres *= 2.54;
yres *= 2.54;
i_tags_set(&im->tags, "tiff_resolutionunit_name", "centimeter", -1);
}
else if (resunit == RESUNIT_NONE) {
i_tags_setn(&im->tags, "i_aspect_only", 1);
i_tags_set(&im->tags, "tiff_resolutionunit_name", "none", -1);
}
else if (resunit == RESUNIT_INCH) {
i_tags_set(&im->tags, "tiff_resolutionunit_name", "inch", -1);
}
else {
i_tags_set(&im->tags, "tiff_resolutionunit_name", "unknown", -1);
}
/* tifflib doesn't seem to provide a way to get to the original rational
value of these, which would let me provide a more reasonable
precision. So make up a number. */
i_tags_set_float2(&im->tags, "i_xres", 0, xres, 6);
i_tags_set_float2(&im->tags, "i_yres", 0, yres, 6);
}
/* Text tags */
for (i = 0; i < text_tag_count; ++i) {
char *data;
if (TIFFGetField(tif, text_tag_names[i].tag, &data)) {
mm_log((1, "i_readtiff_wiol: tag %d has value %s\n",
text_tag_names[i].tag, data));
i_tags_set(&im->tags, text_tag_names[i].name, data, -1);
}
}
i_tags_set(&im->tags, "i_format", "tiff", 4);
{
tiffio_context_t *ctx = TIFFClientdata(tif);
if (ctx->warn_buf) {
i_io_flush(ctx->warn_buf);
unsigned char *data = NULL;
size_t len = io_slurp(ctx->warn_buf, &data);
i_tags_set(&im->tags, "i_warning", (const char *)data, len);
io_glue_destroy(ctx->warn_buf);
ctx->warn_buf = NULL;
}
}
for (i = 0; i < compress_value_count; ++i) {
if (compress_values[i].tag == compress) {
i_tags_set(&im->tags, "tiff_compression", compress_values[i].name, -1);
break;
}
}
if (TIFFGetField(tif, TIFFTAG_SAMPLEFORMAT, &sample_format)) {
/* only set the tag if the the TIFF tag is present */
i_tags_setn(&im->tags, "tiff_sample_format", sample_format);
for (i = 0; i < sample_format_value_count; ++i) {
if (sample_format_values[i].tag == sample_format) {
i_tags_set(&im->tags, "tiff_sample_format_name",
sample_format_values[i].name, -1);
break;
}
}
}
return im;
}
/*
=item i_readtiff_wiol(ig, allow_incomplete, page)
=cut
*/
i_img*
i_readtiff_wiol(io_glue *ig, int allow_incomplete, int page) {
int current_page;
i_clear_error();
/* Add code to get the filename info from the iolayer */
/* Also add code to check for mmapped code */
mm_log((1, "i_readtiff_wiol(ig %p, allow_incomplete %d, page %d)\n", ig, allow_incomplete, page));
tiff_state ts;
TIFF *tif = do_tiff_open(&ts, ig, "rm");
if (!tif) {
mm_log((1, "i_readtiff_wiol: Unable to open tif file\n"));
i_push_error(0, "Error opening file");
return NULL;
}
for (current_page = 0; current_page < page; ++current_page) {
if (!TIFFReadDirectory(tif)) {
mm_log((1, "i_readtiff_wiol: Unable to switch to directory %d\n", page));
i_push_errorf(0, "could not switch to page %d", page);
goto fail;
}
}
i_img *im = read_one_tiff(tif, allow_incomplete);
if (TIFFLastDirectory(tif))
mm_log((1, "Last directory of tiff file\n"));
do_tiff_close(&ts);
return im;
fail:
do_tiff_close(&ts);
return NULL;
}
/*
=item i_readtiff_multi_wiol(ig, *count)
Reads multiple images from a TIFF.
=cut
*/
i_img**
i_readtiff_multi_wiol(io_glue *ig, int *count) {
i_img **results = NULL;
int result_alloc = 0;
i_clear_error();
/* Add code to get the filename info from the iolayer */
/* Also add code to check for mmapped code */
mm_log((1, "i_readtiff_wiol(ig %p)\n", ig));
tiff_state ts;
TIFF *tif = do_tiff_open(&ts, ig, "rm");
if (!tif) {
mm_log((1, "i_readtiff_wiol: Unable to open tif file\n"));
i_push_error(0, "Error opening file");
return NULL;
}
*count = 0;
do {
i_img *im = read_one_tiff(tif, 0);
if (!im)
break;
if (++*count > result_alloc) {
if (result_alloc == 0) {
result_alloc = 5;
results = mymalloc(result_alloc * sizeof(i_img *));
}
else {
i_img **newresults;
result_alloc *= 2;
newresults = myrealloc(results, result_alloc * sizeof(i_img *));
if (!newresults) {
i_img_destroy(im); /* don't leak it */
break;
}
results = newresults;
}
}
results[*count-1] = im;
} while (TIFFReadDirectory(tif));
do_tiff_close(&ts);
return results;
fail:
do_tiff_close(&ts);
return NULL;
}
undef_int
i_writetiff_low_faxable(TIFF *tif, i_img *im, int fine) {
tf_uint32 width, height;
tf_uint32 y;
int rc;
tf_uint32 x;
tf_uint32 rowsperstrip;
float vres = fine ? 196 : 98;
int luma_chan;
width = im->xsize;
height = im->ysize;
if (width != im->xsize || height != im->ysize) {
i_push_error(0, "image too large for TIFF");
return 0;
}
switch (im->channels) {
case 1:
case 2:
luma_chan = 0;
break;
case 3:
case 4:
luma_chan = 1;
break;
default:
/* This means a colorspace we don't handle yet */
mm_log((1, "i_writetiff_wiol_faxable: don't handle %d channel images.\n", im->channels));
return 0;
}
/* Add code to get the filename info from the iolayer */
/* Also add code to check for mmapped code */
mm_log((1, "i_writetiff_wiol_faxable: width=%d, height=%d, channels=%d\n", width, height, im->channels));
if (!TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width) )
{ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField width=%d failed\n", width)); return 0; }
if (!TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height) )
{ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField length=%d failed\n", height)); return 0; }
if (!TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, (tf_uint16)1U))
{ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField samplesperpixel=1 failed\n")); return 0; }
if (!TIFFSetField(tif, TIFFTAG_ORIENTATION, (tf_uint16)ORIENTATION_TOPLEFT))
{ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Orientation=topleft\n")); return 0; }
if (!TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, (tf_uint16)1U) )
{ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField bitpersample=1\n")); return 0; }
if (!TIFFSetField(tif, TIFFTAG_PLANARCONFIG, (tf_uint16)PLANARCONFIG_CONTIG))
{ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField planarconfig\n")); return 0; }
if (!TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, (tf_uint16)PHOTOMETRIC_MINISWHITE))
{ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField photometric=%d\n", PHOTOMETRIC_MINISBLACK)); return 0; }
if (!TIFFSetField(tif, TIFFTAG_COMPRESSION, (tf_uint16)COMPRESSION_CCITTFAX3))
{ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField compression=COMPRESSION_CCITTFAX3\n")); return 0; }
size_t scan_line_size = TIFFScanlineSize(tif);
rowsperstrip = TIFFDefaultStripSize(tif, -1);
if (!TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip)) {
mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField rowsperstrip=%u\n", rowsperstrip)); return 0; }
TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rc);
mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField rowsperstrip=%u\n", rowsperstrip));
mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField scanlinesize=%zu\n", scan_line_size ));
mm_log((1, "i_writetiff_wiol_faxable: TIFFGetField planarconfig=%d\n", PLANARCONFIG_CONTIG));
if (!TIFFSetField(tif, TIFFTAG_XRESOLUTION, (float)204))
{ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Xresolution=204\n")); return 0; }
if (!TIFFSetField(tif, TIFFTAG_YRESOLUTION, vres))
{ mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField Yresolution=196\n")); return 0; }
if (!TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, (tf_uint16)RESUNIT_INCH)) {
mm_log((1, "i_writetiff_wiol_faxable: TIFFSetField ResolutionUnit=%d\n", RESUNIT_INCH)); return 0;
}
if (!save_tiff_tags(tif, im)) {
return 0;
}
unsigned char *linebuf = (unsigned char *)_TIFFmalloc( scan_line_size );
for (y=0; y<height; y++) {
int linebufpos=0;
for(x=0; x<width; x+=8) {
int bits;
int bitpos;
i_sample_t luma[8];
tf_uint8 bitval = 128;
linebuf[linebufpos]=0;
bits = width-x; if(bits>8) bits=8;
i_gsamp(im, x, x+bits, y, luma, &luma_chan, 1);
for(bitpos=0;bitpos<bits;bitpos++) {
linebuf[linebufpos] |= ((luma[bitpos] < 128) ? bitval : 0);
bitval >>= 1;
}
linebufpos++;
}
if (TIFFWriteScanline(tif, linebuf, y, 0) < 0) {
mm_log((1, "i_writetiff_wiol_faxable: TIFFWriteScanline failed.\n"));
goto fail;
}
}
_TIFFfree(linebuf);
return 1;
fail:
_TIFFfree(linebuf);
return 0;
}
static tf_uint16
find_compression(char const *name, tf_uint16 *compress) {
int i;
for (i = 0; i < compress_value_count; ++i) {
if (strcmp(compress_values[i].name, name) == 0) {
*compress = (tf_uint16)compress_values[i].tag;
return 1;
}
}
*compress = COMPRESSION_NONE;
return 0;
}