forked from libvips/libvips
/
tiff2vips.c
2541 lines (2131 loc) · 59.4 KB
/
tiff2vips.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
/* TIFF parts: Copyright (c) 1988, 1990 by Sam Leffler.
* All rights reserved.
*
* This file is provided for unrestricted use provided that this
* legend is included on all tape media and as a part of the
* software program in whole or part. Users may copy, modify or
* distribute this file at will.
* -----------------------------
* Modifications for VIPS: Kirk Martinez 1994
* 22/11/94 JC
* - more general
* - memory leaks fixed
* 20/3/95 JC
* - TIFF error handler added
* - read errors detected correctly
*
* Modified to handle LAB in tiff format.
* It convert LAB-tiff format to VIPS_INTERPRETATION_LABQ in vips format.
* Copyright July-1995 Ahmed Abbood.
*
*
* 19/9/95 JC
* - now calls TIFFClose ... stupid
* 25/1/96 JC
* - typo on MINISBLACK ...
* 7/4/97 JC
* - completely redone for TIFF 6
* - now full baseline TIFF 6 reader, and does CIELAB as well
* 11/4/97 JC
* - added partial read for tiled images
* 23/4/97 JC
* - extra subsample parameter
* - im_istiffpyramid() added
* 5/12/97 JC
* - if loading YCbCr, convert to VIPS_CODING_LABQ
* 1/5/98 JC
* - now reads 16-bit greyscale and RGB
* 26/10/98 JC
* - now used "rb" mode on systems that need binary open
* 12/11/98 JC
* - no sub-sampling if sub == 1
* 26/2/99 JC
* - ooops, else missing for subsample stuff above
* 2/10/99 JC
* - tiled 16-bit greyscale read was broken
* - added mutex for TIFFReadTile() calls
* 11/5/00 JC
* - removed TIFFmalloc/TIFFfree usage
* 23/4/01 JC
* - HAVE_TIFF turns on TIFF goodness
* 24/5/01 JC
* - im_tiff2vips_header() added
* 11/7/01 JC
* - subsample now in input filename
* - ... and it's a page number (from 0) instead
* 21/8/02 JC
* - now reads CMYK
* - hmm, dpi -> ppm conversion was wrong!
* 10/9/02 JC
* - oops, handle TIFF errors better
* 2/12/02 JC
* - reads 8-bit RGBA
* 12/12/02 JC
* - reads 16-bit LAB
* 13/2/03 JC
* - pixels/cm res read was wrong
* 17/11/03 Andrey Kiselev
* - read 32-bit float greyscale and rgb
* 5/4/04
* - better handling of edge tiles (thanks Ruven)
* 16/4/04
* - cleanup
* - added broken tile read mode
* 18/5/04 Andrey Kiselev
* - better no resolution diagnostic
* 26/5/04
* - reads 16 bit RGBA
* 28/7/04
* - arrg, 16bit RGB was broken, thanks haida
* 26/11/04
* - add a TIFF warning handler, stops occasional libMagick exceptions
* 9/3/05
* - load 32-bit float LAB
* 8/4/05
* - onebit read no longer reads one byte too many on multiple of 8 wide
* images
* 22/6/05
* - 16 bit LAB read was broken
* 9/9/05
* - read any ICCPROFILE tag
* 8/5/06
* - set RGB16 and GREY16 Type
* 21/5/06
* - use external im_tile_cache() operation for great code shrinkage
* - less RAM usage too, esp. with >1 CPU
* - should be slightly faster
* - removed 'broken' read option
* 18/7/07 Andrey Kiselev
* - remove "b" option on TIFFOpen()
* 9/4/08
* - set VIPS_META_RESOLUTION_UNIT
* 17/4/08
* - allow CMYKA (thanks Doron)
* 17/7/08
* - convert YCbCr to RGB on read (thanks Ole)
* 15/8/08
* - reorganise for image format system
* 20/12/08
* - dont read with mmap: no performance advantage with libtiff, chews up
* VM wastefully
* 13/1/09
* - read strip-wise, not scanline-wise ... works with more compression /
* subsampling schemes (esp. subsampled YCbCr), and it's a bit quicker
* 4/2/10
* - gtkdoc
* 12/12/10
* - oops, we can just memcpy() now heh
* - avoid unpacking via buffers if we can: either read a tile directly
* into the output region, or writeline directly from the tiff buffer
* 4/4/11
* - argh int/uint mixup for rows_per_strip, thanks Bubba
* 21/4/11
* - palette read can do 1,2,4,8 bits per sample
* - palette read can do mono images
* 5/12/11
* - make into a simple function call ready to be wrapped as a new-style
* VipsForeign class
* 18/2/12
* - switch to sequential read
* - remove the lock ... tilecache does this for us
* 3/6/12
* - always offer THINSTRIP ... later stages can ask for something more
* relaxed if they wish
* 7/6/12
* - clip rows_per_strip down to image height to avoid overflows for huge
* values (thanks Nicolas)
* - better error msg for not PLANARCONFIG_CONTIG images
* 16/9/13
* - support alpha for 8, 16 and 32-bit greyscale images, thanks Robert
* 17/9/13
* - support separate planes for strip read
* - big cleanup
* - support for many more formats, eg. 32-bit int etc.
* 11/4/14
* - support 16 bits per sample palette images
* - palette images can have an alpha
* 22/4/14
* - add read from buffer
* 30/4/14
* - 1/2/4 bit palette images can have alpha
* 27/10/14 Lovell
* - better istiff detector spots bigtiff
* 3/12/14
* - read any XMP metadata
* 19/1/15
* - try to handle 8-bit colormaps
* 26/2/15
* - close the read down early for a header read ... this saves an
* fd during file read, handy for large numbers of input images
* 29/9/15
* - load IPTC metadata
* - load photoshop metadata
* 21/12/15
* - load TIFFTAG_IMAGEDESCRIPTION
* 11/4/16
* - non-int RGB images are tagged as scRGB ... matches photoshop
* convention
* 26/5/16
* - add autorotate support
* 17/11/16
* - add multi-page read
* 17/1/17
* - invalidate operation on read error
* 27/1/17
* - if rows_per_strip is large, read with scanline API instead
* 9/5/17
* - remove missing res warning
* 19/5/17
* - page > 0 could break edge tiles or strips
* 26/4/18
* - add n-pages metadata item
* 21/7/18
* - check for non-byte-multiple bits_per_sample [HongxuChen]
* 16/8/18
* - shut down the input file as soon as we can [kleisauke]
* 28/3/19 omira-sch
* - better buffer sizing
*/
/*
This file is part of VIPS.
VIPS is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
/*
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#ifdef HAVE_TIFF
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/thread.h>
#include "pforeign.h"
#include "tiff.h"
/* What we read from the tiff dir to set our read strategy. For multipage
* read, we need to read and compare lots of these, so it needs to be broken
* out as a separate thing.
*/
typedef struct _RtiffHeader {
uint32 width;
uint32 height;
int samples_per_pixel;
int bits_per_sample;
int photometric_interpretation;
int sample_format;
gboolean separate;
int orientation;
gboolean premultiplied;
/* Result of TIFFIsTiled().
*/
gboolean tiled;
/* Fields for tiled images, as returned by libtiff.
*/
uint32 tile_width;
uint32 tile_height;
tsize_t tile_size;
/* Fields for strip images, as returned by libtiff.
*/
uint32 rows_per_strip;
tsize_t strip_size;
int number_of_strips;
/* If read_scanlinewise is TRUE, the strips are too large to read in a
* single lump and we will use the scanline API.
*/
gboolean read_scanlinewise;
/* Strip read geometry. These are the read params as they will be used
* to read, with either TIFFReadScanline() or TIFFReadEncodedStrip(),
* not as they are stored in the file.
*/
uint32 read_rows_per_strip;
tsize_t read_strip_size;
} RtiffHeader;
/* Scanline-type process function.
*/
struct _Rtiff;
typedef void (*scanline_process_fn)( struct _Rtiff *,
VipsPel *q, VipsPel *p, int n, void *client );
/* Stuff we track during a read.
*/
typedef struct _Rtiff {
/* Parameters.
*/
char *filename;
VipsImage *out;
int page;
int n;
gboolean autorotate;
/* The TIFF we read.
*/
TIFF *tiff;
/* Number of pages (directories) in image.
*/
int n_pages;
/* The current page we have set.
*/
int current_page;
/* Process for this image type.
*/
scanline_process_fn sfn;
void *client;
/* Set this is the processfn is just doing a memcpy.
*/
gboolean memcpy;
/* Geometry as read from the TIFF header. This is read for the first
* page, and equal for all other pages.
*/
RtiffHeader header;
/* Hold a single strip or tile, possibly just an image plane.
*/
tdata_t plane_buf;
/* Hold a plane-assembled strip or tile ... a set of samples_per_pixel
* strips or tiles interleaved.
*/
tdata_t contig_buf;
/* The Y we are reading at. Used to verify strip read is sequential.
*/
int y_pos;
} Rtiff;
/* Test for field exists.
*/
static int
tfexists( TIFF *tif, ttag_t tag )
{
uint32 a, b;
if( TIFFGetField( tif, tag, &a, &b ) )
return( 1 );
else
return( 0 );
}
/* Get a uint32 field.
*/
static int
tfget32( TIFF *tif, ttag_t tag, uint32 *out )
{
uint32 fld;
if( !TIFFGetFieldDefaulted( tif, tag, &fld ) ) {
vips_error( "tiff2vips",
_( "required field %d missing" ), tag );
return( 0 );
}
*out = fld;
return( 1 );
}
/* Get a uint16 field.
*/
static int
tfget16( TIFF *tif, ttag_t tag, int *out )
{
uint16 fld;
if( !TIFFGetFieldDefaulted( tif, tag, &fld ) ) {
vips_error( "tiff2vips",
_( "required field %d missing" ), tag );
return( 0 );
}
*out = fld;
return( 1 );
}
static int
get_resolution( TIFF *tiff, VipsImage *out )
{
float x, y;
int ru;
if( TIFFGetFieldDefaulted( tiff, TIFFTAG_XRESOLUTION, &x ) &&
TIFFGetFieldDefaulted( tiff, TIFFTAG_YRESOLUTION, &y ) &&
tfget16( tiff, TIFFTAG_RESOLUTIONUNIT, &ru ) ) {
switch( ru ) {
case RESUNIT_NONE:
break;
case RESUNIT_INCH:
/* In pixels-per-inch ... convert to mm.
*/
x /= 10.0 * 2.54;
y /= 10.0 * 2.54;
vips_image_set_string( out,
VIPS_META_RESOLUTION_UNIT, "in" );
break;
case RESUNIT_CENTIMETER:
/* In pixels-per-centimetre ... convert to mm.
*/
x /= 10.0;
y /= 10.0;
vips_image_set_string( out,
VIPS_META_RESOLUTION_UNIT, "cm" );
break;
default:
vips_error( "tiff2vips",
"%s", _( "unknown resolution unit" ) );
return( -1 );
}
}
else {
/* We used to warn about missing res data, but it happens so
* often and is so harmless, why bother.
*/
x = 1.0;
y = 1.0;
}
out->Xres = x;
out->Yres = y;
return( 0 );
}
static int
get_sample_format( TIFF *tiff )
{
int sample_format;
uint16 v;
sample_format = SAMPLEFORMAT_INT;
if( TIFFGetFieldDefaulted( tiff, TIFFTAG_SAMPLEFORMAT, &v ) ) {
/* Some images have this set to void, bizarre.
*/
if( v == SAMPLEFORMAT_VOID )
v = SAMPLEFORMAT_UINT;
sample_format = v;
}
return( sample_format );
}
static int
get_orientation( TIFF *tiff )
{
int orientation;
uint16 v;
orientation = ORIENTATION_TOPLEFT;
if( TIFFGetFieldDefaulted( tiff, TIFFTAG_ORIENTATION, &v ) )
/* Can have mad values.
*/
orientation = VIPS_CLIP( 1, v, 8 );
return( orientation );
}
/* Can be called many times.
*/
static void
rtiff_free( Rtiff *rtiff )
{
VIPS_FREEF( TIFFClose, rtiff->tiff );
}
static void
rtiff_close_cb( VipsObject *object, Rtiff *rtiff )
{
rtiff_free( rtiff );
}
static Rtiff *
rtiff_new( VipsImage *out, int page, int n, gboolean autorotate )
{
Rtiff *rtiff;
if( !(rtiff = VIPS_NEW( out, Rtiff )) )
return( NULL );
rtiff->filename = NULL;
rtiff->out = out;
rtiff->page = page;
rtiff->n = n;
rtiff->autorotate = autorotate;
rtiff->tiff = NULL;
rtiff->n_pages = 0;
rtiff->current_page = -1;
rtiff->sfn = NULL;
rtiff->client = NULL;
rtiff->memcpy = FALSE;
rtiff->plane_buf = NULL;
rtiff->contig_buf = NULL;
rtiff->y_pos = 0;
g_signal_connect( out, "close",
G_CALLBACK( rtiff_close_cb ), rtiff );
if( rtiff->page < 0 || rtiff->page > 1000000 ) {
vips_error( "tiff2vips", _( "bad page number %d" ),
rtiff->page );
return( NULL );
}
/* We allow n == -1, meaning all pages. It gets swapped for a real n
* value when we open the TIFF.
*/
if( rtiff->n != -1 &&
(rtiff->n < 1 || rtiff->n > 1000000) ) {
vips_error( "tiff2vips", _( "bad number of pages %d" ),
rtiff->n );
return( NULL );
}
return( rtiff );
}
static int
rtiff_strip_read( Rtiff *rtiff, int strip, tdata_t buf )
{
tsize_t length;
#ifdef DEBUG
printf( "rtiff_strip_read: reading strip %d\n", strip );
#endif /*DEBUG*/
if( rtiff->header.read_scanlinewise )
length = TIFFReadScanline( rtiff->tiff,
buf, strip, (tsize_t) -1 );
else
length = TIFFReadEncodedStrip( rtiff->tiff,
strip, buf, (tsize_t) -1 );
if( length == -1 ) {
vips_foreign_load_invalidate( rtiff->out );
vips_error( "tiff2vips", "%s", _( "read error" ) );
return( -1 );
}
return( 0 );
}
static int
rtiff_set_page( Rtiff *rtiff, int page )
{
if( rtiff->current_page != page ) {
#ifdef DEBUG
printf( "rtiff_set_page: selecting page %d\n", page );
#endif /*DEBUG*/
if( !TIFFSetDirectory( rtiff->tiff, page ) ) {
vips_error( "tiff2vips",
_( "TIFF does not contain page %d" ), page );
return( -1 );
}
rtiff->current_page = page;
}
return( 0 );
}
static int
rtiff_n_pages( Rtiff *rtiff )
{
int n;
(void) TIFFSetDirectory( rtiff->tiff, 0 );
for( n = 1; TIFFReadDirectory( rtiff->tiff ); n++ )
;
(void) TIFFSetDirectory( rtiff->tiff, rtiff->current_page );
#ifdef DEBUG
printf( "rtiff_n_pages: found %d pages\n", n );
#endif /*DEBUG*/
return( n );
}
static int
rtiff_check_samples( Rtiff *rtiff, int samples_per_pixel )
{
if( rtiff->header.samples_per_pixel != samples_per_pixel ) {
vips_error( "tiff2vips",
_( "not %d bands" ), samples_per_pixel );
return( -1 );
}
return( 0 );
}
/* Check n and n+1 so we can have an alpha.
*/
static int
rtiff_check_min_samples( Rtiff *rtiff, int samples_per_pixel )
{
if( rtiff->header.samples_per_pixel < samples_per_pixel ) {
vips_error( "tiff2vips",
_( "not at least %d samples per pixel" ),
samples_per_pixel );
return( -1 );
}
return( 0 );
}
/* Only allow samples which are whole bytes in size.
*/
static int
rtiff_non_fractional( Rtiff *rtiff )
{
if( rtiff->header.bits_per_sample % 8 != 0 ||
rtiff->header.bits_per_sample == 0 ) {
vips_error( "tiff2vips", "%s", _( "samples_per_pixel "
"not a whole number of bytes" ) );
return( -1 );
}
return( 0 );
}
static int
rtiff_check_interpretation( Rtiff *rtiff, int photometric_interpretation )
{
if( rtiff->header.photometric_interpretation !=
photometric_interpretation ) {
vips_error( "tiff2vips",
_( "not photometric interpretation %d" ),
photometric_interpretation );
return( -1 );
}
return( 0 );
}
static int
rtiff_check_bits( Rtiff *rtiff, int bits_per_sample )
{
if( rtiff->header.bits_per_sample != bits_per_sample ) {
vips_error( "tiff2vips",
_( "not %d bits per sample" ), bits_per_sample );
return( -1 );
}
return( 0 );
}
static int
rtiff_check_bits_palette( Rtiff *rtiff )
{
if( rtiff->header.bits_per_sample != 16 &&
rtiff->header.bits_per_sample != 8 &&
rtiff->header.bits_per_sample != 4 &&
rtiff->header.bits_per_sample != 2 &&
rtiff->header.bits_per_sample != 1 ) {
vips_error( "tiff2vips",
_( "%d bits per sample palette image not supported" ),
rtiff->header.bits_per_sample );
return( -1 );
}
return( 0 );
}
static VipsBandFormat
rtiff_guess_format( Rtiff *rtiff )
{
int bits_per_sample = rtiff->header.bits_per_sample;
int sample_format = rtiff->header.sample_format;
switch( bits_per_sample ) {
case 1:
case 2:
case 4:
case 8:
if( sample_format == SAMPLEFORMAT_INT )
return( VIPS_FORMAT_CHAR );
if( sample_format == SAMPLEFORMAT_UINT )
return( VIPS_FORMAT_UCHAR );
break;
case 16:
if( sample_format == SAMPLEFORMAT_INT )
return( VIPS_FORMAT_SHORT );
if( sample_format == SAMPLEFORMAT_UINT )
return( VIPS_FORMAT_USHORT );
break;
case 32:
if( sample_format == SAMPLEFORMAT_INT )
return( VIPS_FORMAT_INT );
if( sample_format == SAMPLEFORMAT_UINT )
return( VIPS_FORMAT_UINT );
if( sample_format == SAMPLEFORMAT_IEEEFP )
return( VIPS_FORMAT_FLOAT );
break;
case 64:
if( sample_format == SAMPLEFORMAT_IEEEFP )
return( VIPS_FORMAT_DOUBLE );
if( sample_format == SAMPLEFORMAT_COMPLEXIEEEFP )
return( VIPS_FORMAT_COMPLEX );
break;
case 128:
if( sample_format == SAMPLEFORMAT_COMPLEXIEEEFP )
return( VIPS_FORMAT_DPCOMPLEX );
break;
default:
break;
}
vips_error( "tiff2vips", "%s", _( "unsupported tiff image type\n" ) );
return( VIPS_FORMAT_NOTSET );
}
/* Per-scanline process function for VIPS_CODING_LABQ.
*/
static void
rtiff_labpack_line( Rtiff *rtiff, VipsPel *q, VipsPel *p, int n, void *dummy )
{
int samples_per_pixel = rtiff->header.samples_per_pixel;
int x;
for( x = 0; x < n; x++ ) {
q[0] = p[0];
q[1] = p[1];
q[2] = p[2];
q[3] = 0;
q += 4;
p += samples_per_pixel;
}
}
/* Read an 8-bit LAB image.
*/
static int
rtiff_parse_labpack( Rtiff *rtiff, VipsImage *out )
{
if( rtiff_check_min_samples( rtiff, 3 ) ||
rtiff_check_bits( rtiff, 8 ) ||
rtiff_check_interpretation( rtiff, PHOTOMETRIC_CIELAB ) )
return( -1 );
out->Bands = 4;
out->BandFmt = VIPS_FORMAT_UCHAR;
out->Coding = VIPS_CODING_LABQ;
out->Type = VIPS_INTERPRETATION_LAB;
rtiff->sfn = rtiff_labpack_line;
return( 0 );
}
/* Per-scanline process function for LABS.
*/
static void
rtiff_labs_line( Rtiff *rtiff, VipsPel *q, VipsPel *p, int n, void *dummy )
{
int samples_per_pixel = rtiff->header.samples_per_pixel;
unsigned short *p1;
short *q1;
int x;
int i;
p1 = (unsigned short *) p;
q1 = (short *) q;
for( x = 0; x < n; x++ ) {
/* We use a signed int16 for L.
*/
q1[0] = p1[0] >> 1;
for( i = 1; i < samples_per_pixel; i++ )
q1[i] = p1[i];
q1 += samples_per_pixel;
p1 += samples_per_pixel;
}
}
/* Read a 16-bit LAB image.
*/
static int
rtiff_parse_labs( Rtiff *rtiff, VipsImage *out )
{
if( rtiff_check_min_samples( rtiff, 3 ) ||
rtiff_check_bits( rtiff, 16 ) ||
rtiff_check_interpretation( rtiff, PHOTOMETRIC_CIELAB ) )
return( -1 );
out->Bands = rtiff->header.samples_per_pixel;
out->BandFmt = VIPS_FORMAT_SHORT;
out->Coding = VIPS_CODING_NONE;
out->Type = VIPS_INTERPRETATION_LABS;
rtiff->sfn = rtiff_labs_line;
return( 0 );
}
/* Per-scanline process function for 1 bit images.
*/
static void
rtiff_onebit_line( Rtiff *rtiff, VipsPel *q, VipsPel *p, int n, void *flg )
{
int photometric_interpretation =
rtiff->header.photometric_interpretation;
int x, i, z;
VipsPel bits;
int black = photometric_interpretation == PHOTOMETRIC_MINISBLACK ?
0 : 255;
int white = black ^ 0xff;
/* (sigh) how many times have I written this?
*/
x = 0;
for( i = 0; i < (n >> 3); i++ ) {
bits = (VipsPel) p[i];
for( z = 0; z < 8; z++ ) {
q[x] = (bits & 128) ? white : black;
bits <<= 1;
x += 1;
}
}
/* Do last byte in line.
*/
if( n & 7 ) {
bits = p[i];
for( z = 0; z < (n & 7); z++ ) {
q[x + z] = (bits & 128) ? white : black;
bits <<= 1;
}
}
}
/* Read a 1-bit TIFF image.
*/
static int
rtiff_parse_onebit( Rtiff *rtiff, VipsImage *out )
{
if( rtiff_check_samples( rtiff, 1 ) ||
rtiff_check_bits( rtiff, 1 ) )
return( -1 );
out->Bands = 1;
out->BandFmt = VIPS_FORMAT_UCHAR;
out->Coding = VIPS_CODING_NONE;
out->Type = VIPS_INTERPRETATION_B_W;
rtiff->sfn = rtiff_onebit_line;
return( 0 );
}
/* Swap the sense of the first channel, if necessary.
*/
#define GREY_LOOP( TYPE, MAX ) { \
TYPE *p1; \
TYPE *q1; \
\
p1 = (TYPE *) p; \
q1 = (TYPE *) q; \
for( x = 0; x < n; x++ ) { \
if( invert ) \
q1[0] = MAX - p1[0]; \
else \
q1[0] = p1[0]; \
\
for( i = 1; i < samples_per_pixel; i++ ) \
q1[i] = p1[i]; \
\
q1 += samples_per_pixel; \
p1 += samples_per_pixel; \
} \
}
/* Per-scanline process function for greyscale images.
*/
static void
rtiff_greyscale_line( Rtiff *rtiff,
VipsPel *q, VipsPel *p, int n, void *client )
{
int samples_per_pixel = rtiff->header.samples_per_pixel;
int photometric_interpretation =
rtiff->header.photometric_interpretation;
gboolean invert = photometric_interpretation == PHOTOMETRIC_MINISWHITE;
VipsBandFormat format = rtiff_guess_format( rtiff );
int x, i;
switch( format ) {
case VIPS_FORMAT_UCHAR:
case VIPS_FORMAT_CHAR:
GREY_LOOP( guchar, UCHAR_MAX );
break;
case VIPS_FORMAT_SHORT:
GREY_LOOP( gshort, SHRT_MAX );
break;
case VIPS_FORMAT_USHORT:
GREY_LOOP( gushort, USHRT_MAX );
break;
case VIPS_FORMAT_INT:
GREY_LOOP( gint, INT_MAX );
break;
case VIPS_FORMAT_UINT:
GREY_LOOP( guint, UINT_MAX );
break;
case VIPS_FORMAT_FLOAT:
GREY_LOOP( float, 1.0 );
break;
case VIPS_FORMAT_DOUBLE:
GREY_LOOP( double, 1.0 );
break;
default:
g_assert_not_reached();
}
}
/* Read a grey-scale TIFF image. We have to invert the first band if
* PHOTOMETRIC_MINISBLACK is set.
*/
static int
rtiff_parse_greyscale( Rtiff *rtiff, VipsImage *out )
{
if( rtiff_check_min_samples( rtiff, 1 ) ||
rtiff_non_fractional( rtiff ) )
return( -1 );
out->Bands = rtiff->header.samples_per_pixel;
out->BandFmt = rtiff_guess_format( rtiff );
if( out->BandFmt == VIPS_FORMAT_NOTSET )
return( -1 );
out->Coding = VIPS_CODING_NONE;
if( rtiff->header.bits_per_sample == 16 )
out->Type = VIPS_INTERPRETATION_GREY16;
else
out->Type = VIPS_INTERPRETATION_B_W;
/* rtiff_greyscale_line() doesn't do complex.
*/
if( vips_check_noncomplex( "tiff2vips", out ) )
return( -1 );
rtiff->sfn = rtiff_greyscale_line;
return( 0 );
}
typedef struct {
/* LUTs mapping image indexes to RGB.
*/
VipsPel *red8;
VipsPel *green8;
VipsPel *blue8;
guint16 *red16;
guint16 *green16;
guint16 *blue16;
/* All maps equal, so we write mono.
*/
gboolean mono;
} PaletteRead;