blob: f5dc094143ec214adc387c798e16a6a89ed1b6bc [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% TTTTT IIIII FFFFF FFFFF %
7% T I F F %
8% T I FFF FFF %
9% T I F F %
10% T IIIII F F %
11% %
12% %
13% Read/Write TIFF Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/attribute.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colormap.h"
50#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/constitute.h"
53#include "MagickCore/enhance.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image.h"
58#include "MagickCore/image-private.h"
59#include "MagickCore/list.h"
60#include "MagickCore/log.h"
61#include "MagickCore/magick.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/module.h"
64#include "MagickCore/monitor.h"
65#include "MagickCore/monitor-private.h"
66#include "MagickCore/option.h"
67#include "MagickCore/pixel-accessor.h"
68#include "MagickCore/property.h"
69#include "MagickCore/quantum.h"
70#include "MagickCore/quantum-private.h"
71#include "MagickCore/profile.h"
72#include "MagickCore/resize.h"
73#include "MagickCore/resource_.h"
74#include "MagickCore/semaphore.h"
75#include "MagickCore/splay-tree.h"
76#include "MagickCore/static.h"
77#include "MagickCore/statistic.h"
78#include "MagickCore/string_.h"
79#include "MagickCore/string-private.h"
80#include "MagickCore/thread_.h"
81#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000082#if defined(MAGICKCORE_TIFF_DELEGATE)
83# if defined(MAGICKCORE_HAVE_TIFFCONF_H)
84# include "tiffconf.h"
cristyef8f26b2010-12-19 20:29:16 +000085# endif
cristy3ed852e2009-09-05 21:47:34 +000086# include "tiff.h"
87# include "tiffio.h"
88# if !defined(COMPRESSION_ADOBE_DEFLATE)
89# define COMPRESSION_ADOBE_DEFLATE 8
90# endif
cristyef8f26b2010-12-19 20:29:16 +000091# if !defined(PREDICTOR_HORIZONTAL)
92# define PREDICTOR_HORIZONTAL 2
93# endif
cristy98ca0f52011-10-08 23:19:10 +000094# if !defined(TIFFTAG_COPYRIGHT)
95# define TIFFTAG_COPYRIGHT 33432
96# endif
97# if !defined(TIFFTAG_OPIIMAGEID)
98# define TIFF_OPIIMAGEID 32781
99# endif
cristy3ed852e2009-09-05 21:47:34 +0000100
101/*
102 Typedef declarations.
103*/
104#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
105typedef struct _ExifInfo
106{
107 unsigned int
108 tag,
109 type;
110
111 const char
112 *property;
113} ExifInfo;
114
115static const ExifInfo
116 exif_info[] = {
117 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
118 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
119 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
120 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000121 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy7992e402011-09-16 15:24:22 +0000122 { EXIFTAG_OECF, TIFF_NOTYPE, "exif:OptoelectricConversionFactor" },
123 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, "exif:ExifVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000124 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
125 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000126 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, "exif:ComponentsConfiguration" },
cristy3ed852e2009-09-05 21:47:34 +0000127 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
128 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
129 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
130 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
131 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
132 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
133 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
134 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
135 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
136 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
137 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
cristy7992e402011-09-16 15:24:22 +0000138 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, "exif:SubjectArea" },
139 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, "exif:MakerNote" },
140 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, "exif:UserComment" },
cristy3ed852e2009-09-05 21:47:34 +0000141 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
142 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
143 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000144 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, "exif:FlashpixVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000145 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
146 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
147 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
148 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
149 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
150 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
cristy7992e402011-09-16 15:24:22 +0000151 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, "exif:SpatialFrequencyResponse" },
cristy3ed852e2009-09-05 21:47:34 +0000152 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
153 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
154 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
155 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
156 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
157 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
cristy7992e402011-09-16 15:24:22 +0000158 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, "exif:FileSource" },
159 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, "exif:SceneType" },
160 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, "exif:CFAPattern" },
cristy3ed852e2009-09-05 21:47:34 +0000161 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
162 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
163 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
164 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
165 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
166 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
167 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
168 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
169 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
170 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
cristy7992e402011-09-16 15:24:22 +0000171 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, "exif:DeviceSettingDescription" },
cristy3ed852e2009-09-05 21:47:34 +0000172 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
173 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
174 { 0, 0, (char *) NULL }
175};
176#endif
cristy0805e062011-01-30 02:56:36 +0000177#endif /* MAGICKCORE_TIFF_DELEGATE */
cristy3ed852e2009-09-05 21:47:34 +0000178
179/*
180 Global declarations.
181*/
182static MagickThreadKey
183 tiff_exception;
184
185static SemaphoreInfo
186 *tiff_semaphore = (SemaphoreInfo *) NULL;
187
188static volatile MagickBooleanType
189 instantiate_key = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000190
191/*
192 Forward declarations.
193*/
194#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000195static Image *
196 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
197
cristy3ed852e2009-09-05 21:47:34 +0000198static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +0000199 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
200 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
201 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000202#endif
203
204/*
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206% %
207% %
208% %
209% I s T I F F %
210% %
211% %
212% %
213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214%
215% IsTIFF() returns MagickTrue if the image format type, identified by the
216% magick string, is TIFF.
217%
218% The format of the IsTIFF method is:
219%
220% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
221%
222% A description of each parameter follows:
223%
224% o magick: compare image format pattern against these bytes.
225%
226% o length: Specifies the length of the magick string.
227%
228*/
229static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
230{
231 if (length < 4)
232 return(MagickFalse);
233 if (memcmp(magick,"\115\115\000\052",4) == 0)
234 return(MagickTrue);
235 if (memcmp(magick,"\111\111\052\000",4) == 0)
236 return(MagickTrue);
237#if defined(TIFF_VERSION_BIG)
238 if (length < 8)
239 return(MagickFalse);
240 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
241 return(MagickTrue);
242 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
243 return(MagickTrue);
244#endif
245 return(MagickFalse);
246}
247
248#if defined(MAGICKCORE_TIFF_DELEGATE)
249/*
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251% %
252% %
253% %
cristy3d7f8062009-09-24 20:45:35 +0000254% R e a d G R O U P 4 I m a g e %
255% %
256% %
257% %
258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259%
260% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
261% allocates the memory necessary for the new Image structure and returns a
262% pointer to the new image.
263%
264% The format of the ReadGROUP4Image method is:
265%
266% Image *ReadGROUP4Image(const ImageInfo *image_info,
267% ExceptionInfo *exception)
268%
269% A description of each parameter follows:
270%
271% o image_info: the image info.
272%
273% o exception: return any errors or warnings in this structure.
274%
275*/
276
cristyeaedf062010-05-29 22:36:02 +0000277static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000278{
279 unsigned char
280 buffer[4];
281
282 buffer[0]=(unsigned char) value;
283 buffer[1]=(unsigned char) (value >> 8);
284 buffer[2]=(unsigned char) (value >> 16);
285 buffer[3]=(unsigned char) (value >> 24);
286 return(fwrite(buffer,1,4,file));
287}
288
289static Image *ReadGROUP4Image(const ImageInfo *image_info,
290 ExceptionInfo *exception)
291{
292 char
293 filename[MaxTextExtent];
294
295 FILE
296 *file;
297
298 Image
299 *image;
300
301 ImageInfo
302 *read_info;
303
304 int
305 c,
306 unique_file;
307
308 MagickBooleanType
309 status;
310
311 size_t
312 length;
313
314 ssize_t
315 offset,
316 strip_offset;
317
318 /*
319 Open image file.
320 */
321 assert(image_info != (const ImageInfo *) NULL);
322 assert(image_info->signature == MagickSignature);
323 if (image_info->debug != MagickFalse)
324 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
325 image_info->filename);
326 assert(exception != (ExceptionInfo *) NULL);
327 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000328 image=AcquireImage(image_info,exception);
cristy3d7f8062009-09-24 20:45:35 +0000329 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
330 if (status == MagickFalse)
331 {
332 image=DestroyImageList(image);
333 return((Image *) NULL);
334 }
335 /*
336 Write raw CCITT Group 4 wrapped as a TIFF image file.
337 */
338 file=(FILE *) NULL;
339 unique_file=AcquireUniqueFileResource(filename);
340 if (unique_file != -1)
341 file=fdopen(unique_file,"wb");
342 if ((unique_file == -1) || (file == (FILE *) NULL))
343 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
344 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
345 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
346 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
347 length=WriteLSBLong(file,image->columns);
348 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
349 length=WriteLSBLong(file,image->rows);
350 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
351 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
352 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
353 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
354 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000355 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000356 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000357 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000358 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
359 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000360 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000361 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
362 offset=(ssize_t) ftell(file)-4;
363 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000364 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000365 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000366 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000367 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
368 length=fwrite("\000\000\000\000",1,4,file);
cristy2a11bef2011-10-28 18:33:11 +0000369 length=WriteLSBLong(file,(long) image->resolution.x);
cristy3d7f8062009-09-24 20:45:35 +0000370 length=WriteLSBLong(file,1);
371 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
372 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000373 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000374 length=WriteLSBLong(file,(unsigned int) length);
375 (void) fclose(file);
376 (void) CloseBlob(image);
377 image=DestroyImage(image);
378 /*
379 Read TIFF image.
380 */
cristy14efd992009-09-26 23:46:03 +0000381 read_info=CloneImageInfo((ImageInfo *) NULL);
cristyb51dff52011-05-19 16:55:47 +0000382 (void) FormatLocaleString(read_info->filename,MaxTextExtent,"%s",filename);
cristy3d7f8062009-09-24 20:45:35 +0000383 image=ReadTIFFImage(read_info,exception);
384 read_info=DestroyImageInfo(read_info);
385 if (image != (Image *) NULL)
386 {
387 (void) CopyMagickString(image->filename,image_info->filename,
388 MaxTextExtent);
389 (void) CopyMagickString(image->magick_filename,image_info->filename,
390 MaxTextExtent);
391 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
392 }
393 (void) RelinquishUniqueFileResource(filename);
394 return(image);
395}
396#endif
397
398#if defined(MAGICKCORE_TIFF_DELEGATE)
399/*
400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401% %
402% %
403% %
cristy3ed852e2009-09-05 21:47:34 +0000404% R e a d T I F F I m a g e %
405% %
406% %
407% %
408%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409%
410% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
411% memory necessary for the new Image structure and returns a pointer to the
412% new image.
413%
414% The format of the ReadTIFFImage method is:
415%
416% Image *ReadTIFFImage(const ImageInfo *image_info,
417% ExceptionInfo *exception)
418%
419% A description of each parameter follows:
420%
421% o image_info: the image info.
422%
423% o exception: return any errors or warnings in this structure.
424%
425*/
426
427static inline size_t MagickMax(const size_t x,const size_t y)
428{
429 if (x > y)
430 return(x);
431 return(y);
432}
433
cristybb503372010-05-27 20:51:26 +0000434static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000435{
436 if (x < y)
437 return(x);
438 return(y);
439}
440
441static MagickBooleanType ReadProfile(Image *image,const char *name,
cristy018f07f2011-09-04 21:15:19 +0000442 unsigned char *datum,ssize_t length,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000443{
444 MagickBooleanType
445 status;
446
cristybb503372010-05-27 20:51:26 +0000447 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000448 i;
449
450 StringInfo
451 *profile;
452
453 if (length < 4)
454 return(MagickFalse);
455 i=0;
456 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
457 {
458 for (i=0; i < (length-4); i+=2)
459 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
460 break;
461 if (i == length)
462 length-=i;
463 else
464 i=0;
465 if (length < 4)
466 return(MagickFalse);
467 }
cristye8f8f382011-09-01 13:32:37 +0000468 profile=BlobToStringInfo(datum+i,(size_t) length);
469 if (profile == (StringInfo *) NULL)
470 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
471 image->filename);
cristyd15e6592011-10-15 00:13:06 +0000472 status=SetImageProfile(image,name,profile,exception);
cristy3ed852e2009-09-05 21:47:34 +0000473 profile=DestroyStringInfo(profile);
474 if (status == MagickFalse)
475 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
476 image->filename);
477 return(MagickTrue);
478}
479
480#if defined(__cplusplus) || defined(c_plusplus)
481extern "C" {
482#endif
483
484static int TIFFCloseBlob(thandle_t image)
485{
486 (void) CloseBlob((Image *) image);
487 return(0);
488}
489
490static void TIFFErrors(const char *module,const char *format,va_list error)
491{
492 char
493 message[MaxTextExtent];
494
495 ExceptionInfo
496 *exception;
497
498#if defined(MAGICKCORE_HAVE_VSNPRINTF)
499 (void) vsnprintf(message,MaxTextExtent,format,error);
500#else
501 (void) vsprintf(message,format,error);
502#endif
503 (void) ConcatenateMagickString(message,".",MaxTextExtent);
504 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
505 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000506 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
507 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000508}
509
cristy9c47c732011-11-09 19:58:33 +0000510static toff_t TIFFGetBlobSize(thandle_t image)
511{
512 return((toff_t) GetBlobSize((Image *) image));
513}
514
cristy018f07f2011-09-04 21:15:19 +0000515static void TIFFGetProfiles(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000516{
517 uint32
518 length;
519
520 unsigned char
521 *profile;
522
523#if defined(TIFFTAG_ICCPROFILE)
524 length=0;
525 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000526 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000527#endif
528#if defined(TIFFTAG_PHOTOSHOP)
529 length=0;
530 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000531 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000532#endif
533#if defined(TIFFTAG_RICHTIFFIPTC)
534 length=0;
535 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
536 {
537 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000538 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy018f07f2011-09-04 21:15:19 +0000539 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000540 }
541#endif
542#if defined(TIFFTAG_XMLPACKET)
543 length=0;
544 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000545 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000546#endif
547 length=0;
548 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000549 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000550}
551
cristyd15e6592011-10-15 00:13:06 +0000552static void TIFFGetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000553{
554 char
cristy98ca0f52011-10-08 23:19:10 +0000555 message[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000556 *text;
557
cristy98ca0f52011-10-08 23:19:10 +0000558 uint32
559 count;
560
cristy3ed852e2009-09-05 21:47:34 +0000561 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000562 (void) SetImageProperty(image,"tiff:artist",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000563 if (TIFFGetField(tiff,TIFFTAG_COPYRIGHT,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000564 (void) SetImageProperty(image,"tiff:copyright",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000565 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000566 (void) SetImageProperty(image,"tiff:timestamp",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000567 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000568 (void) SetImageProperty(image,"tiff:document",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000569 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000570 (void) SetImageProperty(image,"tiff:hostcomputer",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000571 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000572 (void) SetImageProperty(image,"comment",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000573 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000574 (void) SetImageProperty(image,"tiff:make",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000575 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000576 (void) SetImageProperty(image,"tiff:model",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000577 if (TIFFGetField(tiff,TIFFTAG_OPIIMAGEID,&count,&text) == 1)
578 {
579 if (count >= MaxTextExtent)
580 count=MaxTextExtent-1;
581 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000582 (void) SetImageProperty(image,"tiff:image-id",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000583 }
cristy3ed852e2009-09-05 21:47:34 +0000584 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000585 (void) SetImageProperty(image,"label",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000586 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000587 (void) SetImageProperty(image,"tiff:software",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000588 if (TIFFGetField(tiff,33423,&count,&text) == 1)
589 {
590 if (count >= MaxTextExtent)
591 count=MaxTextExtent-1;
592 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000593 (void) SetImageProperty(image,"tiff:kodak-33423",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000594 }
595 if (TIFFGetField(tiff,36867,&count,&text) == 1)
596 {
597 if (count >= MaxTextExtent)
598 count=MaxTextExtent-1;
599 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000600 (void) SetImageProperty(image,"tiff:kodak-36867",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000601 }
cristy3ed852e2009-09-05 21:47:34 +0000602}
603
cristyd15e6592011-10-15 00:13:06 +0000604static void TIFFGetEXIFProperties(TIFF *tiff,Image *image,
605 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000606{
607#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
608 char
609 value[MaxTextExtent];
610
cristybb503372010-05-27 20:51:26 +0000611 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000612 i;
613
614 tdir_t
615 directory;
616
cristyccf4e122011-10-29 00:29:19 +0000617#if defined(TIFF_VERSION_BIG)
618 uint64
619#else
cristy3ed852e2009-09-05 21:47:34 +0000620 uint32
cristyccf4e122011-10-29 00:29:19 +0000621#endif
cristy3ed852e2009-09-05 21:47:34 +0000622 offset;
623
cristy7992e402011-09-16 15:24:22 +0000624 void
625 *sans;
626
cristy3ed852e2009-09-05 21:47:34 +0000627 /*
628 Read EXIF properties.
629 */
630 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
631 return;
632 directory=TIFFCurrentDirectory(tiff);
633 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
634 return;
cristy7992e402011-09-16 15:24:22 +0000635 sans=NULL;
cristy3ed852e2009-09-05 21:47:34 +0000636 for (i=0; exif_info[i].tag != 0; i++)
637 {
638 *value='\0';
639 switch (exif_info[i].type)
640 {
641 case TIFF_ASCII:
642 {
643 char
644 *ascii;
645
cristy7992e402011-09-16 15:24:22 +0000646 ascii=(char *) NULL;
647 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans) != 0) &&
648 (ascii != (char *) NULL) && (*ascii != '\0'))
cristy3ed852e2009-09-05 21:47:34 +0000649 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
650 break;
651 }
652 case TIFF_SHORT:
653 {
654 uint16
cristy7992e402011-09-16 15:24:22 +0000655 shorty[2] = { 0, 0};
cristy3ed852e2009-09-05 21:47:34 +0000656
cristy7992e402011-09-16 15:24:22 +0000657 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans) != 0)
658 (void) FormatLocaleString(value,MaxTextExtent,"%d",(int) shorty[0]);
cristy3ed852e2009-09-05 21:47:34 +0000659 break;
660 }
661 case TIFF_LONG:
662 {
663 uint32
cristy7992e402011-09-16 15:24:22 +0000664 longy;
cristy3ed852e2009-09-05 21:47:34 +0000665
cristy7992e402011-09-16 15:24:22 +0000666 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans) != 0)
667 (void) FormatLocaleString(value,MaxTextExtent,"%d",longy);
cristy3ed852e2009-09-05 21:47:34 +0000668 break;
669 }
cristyccf4e122011-10-29 00:29:19 +0000670#if defined(TIFF_VERSION_BIG)
671 case TIFF_LONG8:
672 {
673 uint64
674 longy;
675
676 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans) != 0)
cristy02845b42011-11-11 17:22:13 +0000677 (void) FormatLocaleString(value,MaxTextExtent,"%lld",longy);
cristyccf4e122011-10-29 00:29:19 +0000678 break;
679 }
680#endif
cristy3ed852e2009-09-05 21:47:34 +0000681 case TIFF_RATIONAL:
682 case TIFF_SRATIONAL:
cristy7992e402011-09-16 15:24:22 +0000683 case TIFF_FLOAT:
684 case TIFF_DOUBLE:
cristy3ed852e2009-09-05 21:47:34 +0000685 {
686 float
cristy7992e402011-09-16 15:24:22 +0000687 rational[16];
cristy3ed852e2009-09-05 21:47:34 +0000688
cristy7992e402011-09-16 15:24:22 +0000689 if (TIFFGetField(tiff,exif_info[i].tag,&rational,&sans) != 0)
690 (void) FormatLocaleString(value,MaxTextExtent,"%g",rational[0]);
cristy3ed852e2009-09-05 21:47:34 +0000691 break;
692 }
693 default:
694 break;
695 }
696 if (*value != '\0')
cristyd15e6592011-10-15 00:13:06 +0000697 (void) SetImageProperty(image,exif_info[i].property,value,exception);
cristy3ed852e2009-09-05 21:47:34 +0000698 }
699 TIFFSetDirectory(tiff,directory);
700#else
701 (void) tiff;
702 (void) image;
703#endif
704}
705
706static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
707{
708 *base=(tdata_t *) GetBlobStreamData((Image *) image);
709 if (*base != (tdata_t *) NULL)
710 *size=(toff_t) GetBlobSize((Image *) image);
711 if (*base != (tdata_t *) NULL)
712 return(1);
713 return(0);
714}
715
716static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
717{
718 tsize_t
719 count;
720
721 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
722 (unsigned char *) data);
723 return(count);
724}
725
cristybb503372010-05-27 20:51:26 +0000726static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
727 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000728{
729 int32
730 status;
731
732 (void) bits_per_sample;
733 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
734 return(status);
735}
736
737static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
738{
739 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
740}
741
cristy3ed852e2009-09-05 21:47:34 +0000742static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
743{
744 (void) image;
745 (void) base;
746 (void) size;
747}
748
749static void TIFFWarnings(const char *module,const char *format,va_list warning)
750{
751 char
752 message[MaxTextExtent];
753
754 ExceptionInfo
755 *exception;
756
757#if defined(MAGICKCORE_HAVE_VSNPRINTF)
758 (void) vsnprintf(message,MaxTextExtent,format,warning);
759#else
760 (void) vsprintf(message,format,warning);
761#endif
762 (void) ConcatenateMagickString(message,".",MaxTextExtent);
763 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
764 if (exception != (ExceptionInfo *) NULL)
765 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
766 message,"`%s'",module);
767}
768
769static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
770{
771 tsize_t
772 count;
773
774 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
775 (unsigned char *) data);
776 return(count);
777}
778
779#if defined(__cplusplus) || defined(c_plusplus)
780}
781#endif
782
783static Image *ReadTIFFImage(const ImageInfo *image_info,
784 ExceptionInfo *exception)
785{
786 typedef enum
787 {
788 ReadSingleSampleMethod,
789 ReadRGBAMethod,
790 ReadCMYKAMethod,
791 ReadStripMethod,
792 ReadTileMethod,
793 ReadGenericMethod
794 } TIFFMethodType;
795
796 const char
797 *option;
798
799 float
800 *chromaticity,
801 x_position,
802 y_position,
803 x_resolution,
804 y_resolution;
805
806 Image
807 *image;
808
cristy3ed852e2009-09-05 21:47:34 +0000809 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000810 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000811 debug,
812 status;
813
814 MagickSizeType
815 number_pixels;
816
817 QuantumInfo
818 *quantum_info;
819
820 QuantumType
821 quantum_type;
822
cristybb503372010-05-27 20:51:26 +0000823 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000824 i;
825
826 size_t
827 length,
cristyc6da28e2011-04-28 01:41:35 +0000828 lsb_first,
cristy3ed852e2009-09-05 21:47:34 +0000829 pad;
830
cristyc6da28e2011-04-28 01:41:35 +0000831 ssize_t
832 y;
833
cristy3ed852e2009-09-05 21:47:34 +0000834 TIFF
835 *tiff;
836
837 TIFFErrorHandler
838 error_handler,
839 warning_handler;
840
841 TIFFMethodType
842 method;
843
844 uint16
845 compress_tag,
846 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000847 endian,
cristy3ed852e2009-09-05 21:47:34 +0000848 extra_samples,
849 interlace,
850 max_sample_value,
851 min_sample_value,
852 orientation,
853 pages,
854 photometric,
855 *sample_info,
856 sample_format,
857 samples_per_pixel,
858 units,
859 value;
860
861 uint32
862 height,
863 rows_per_strip,
864 width;
865
866 unsigned char
867 *pixels;
868
cristy3ed852e2009-09-05 21:47:34 +0000869 /*
870 Open image.
871 */
872 assert(image_info != (const ImageInfo *) NULL);
873 assert(image_info->signature == MagickSignature);
874 if (image_info->debug != MagickFalse)
875 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
876 image_info->filename);
877 assert(exception != (ExceptionInfo *) NULL);
878 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000879 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000880 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
881 if (status == MagickFalse)
882 {
883 image=DestroyImageList(image);
884 return((Image *) NULL);
885 }
886 (void) MagickSetThreadValue(tiff_exception,exception);
887 error_handler=TIFFSetErrorHandler(TIFFErrors);
888 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
cristy1630f7f2011-02-18 01:10:02 +0000889 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
cristy3ed852e2009-09-05 21:47:34 +0000890 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
891 TIFFUnmapBlob);
892 if (tiff == (TIFF *) NULL)
893 {
894 (void) TIFFSetWarningHandler(warning_handler);
895 (void) TIFFSetErrorHandler(error_handler);
896 image=DestroyImageList(image);
897 return((Image *) NULL);
898 }
899 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +0000900 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +0000901 if (image_info->number_scenes != 0)
902 {
903 /*
904 Generate blank images for subimage specification (e.g. image.tif[4].
905 */
cristybb503372010-05-27 20:51:26 +0000906 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000907 {
908 (void) TIFFReadDirectory(tiff);
cristy9950d572011-10-01 18:22:35 +0000909 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000910 if (GetNextImageInList(image) == (Image *) NULL)
911 {
912 image=DestroyImageList(image);
913 return((Image *) NULL);
914 }
915 image=SyncNextImageInList(image);
916 }
917 }
918 do
919 {
920 if (0 && (image_info->verbose != MagickFalse))
921 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristyd15e6592011-10-15 00:13:06 +0000922 TIFFGetEXIFProperties(tiff,image,exception);
cristy5b480ae2011-10-10 13:05:30 +0000923 TIFFGetProfiles(tiff,image,exception);
cristyd15e6592011-10-15 00:13:06 +0000924 TIFFGetProperties(tiff,image,exception);
cristy3f590e52011-11-14 14:29:44 +0000925#if defined(MAGICKCORE_HAVE_TIFFISBIGENDIAN)
cristy6b032822010-06-29 16:52:32 +0000926 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
cristyd15e6592011-10-15 00:13:06 +0000927 "lsb" : "msb",exception);
cristy3f590e52011-11-14 14:29:44 +0000928#endif
cristy3ed852e2009-09-05 21:47:34 +0000929 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
930 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
931 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
932 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000933 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000934 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
935 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
936 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
cristyae1319c2011-01-19 20:18:53 +0000937 if (sample_format == SAMPLEFORMAT_IEEEFP)
cristyd15e6592011-10-15 00:13:06 +0000938 (void) SetImageProperty(image,"quantum:format","floating-point",
939 exception);
cristy3ed852e2009-09-05 21:47:34 +0000940 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
941 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
942 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
943 switch (photometric)
944 {
945 case PHOTOMETRIC_MINISBLACK:
946 {
cristyd15e6592011-10-15 00:13:06 +0000947 (void) SetImageProperty(image,"tiff:photometric","min-is-black",
948 exception);
cristy3ed852e2009-09-05 21:47:34 +0000949 break;
950 }
951 case PHOTOMETRIC_MINISWHITE:
952 {
cristyd15e6592011-10-15 00:13:06 +0000953 (void) SetImageProperty(image,"tiff:photometric","min-is-white",
954 exception);
cristy3ed852e2009-09-05 21:47:34 +0000955 break;
956 }
957 case PHOTOMETRIC_PALETTE:
958 {
cristyd15e6592011-10-15 00:13:06 +0000959 (void) SetImageProperty(image,"tiff:photometric","palette",exception);
cristy3ed852e2009-09-05 21:47:34 +0000960 break;
961 }
962 case PHOTOMETRIC_RGB:
963 {
cristyd15e6592011-10-15 00:13:06 +0000964 (void) SetImageProperty(image,"tiff:photometric","RGB",exception);
cristy3ed852e2009-09-05 21:47:34 +0000965 break;
966 }
967 case PHOTOMETRIC_CIELAB:
968 {
cristyd15e6592011-10-15 00:13:06 +0000969 (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception);
cristy3ed852e2009-09-05 21:47:34 +0000970 break;
971 }
972 case PHOTOMETRIC_SEPARATED:
973 {
cristyd15e6592011-10-15 00:13:06 +0000974 (void) SetImageProperty(image,"tiff:photometric","separated",exception);
cristy3ed852e2009-09-05 21:47:34 +0000975 break;
976 }
cristy79e5dad2010-09-16 19:48:33 +0000977 case PHOTOMETRIC_YCBCR:
978 {
cristyd15e6592011-10-15 00:13:06 +0000979 (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception);
cristy79e5dad2010-09-16 19:48:33 +0000980 break;
981 }
cristy3ed852e2009-09-05 21:47:34 +0000982 default:
983 {
cristyd15e6592011-10-15 00:13:06 +0000984 (void) SetImageProperty(image,"tiff:photometric","unknown",exception);
cristy3ed852e2009-09-05 21:47:34 +0000985 break;
986 }
987 }
988 if (image->debug != MagickFalse)
989 {
990 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
991 (unsigned int) width,(unsigned int) height);
992 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
993 interlace);
994 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
995 "Bits per sample: %u",bits_per_sample);
996 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
997 "Min sample value: %u",min_sample_value);
998 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
999 "Max sample value: %u",max_sample_value);
1000 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
cristyd15e6592011-10-15 00:13:06 +00001001 "interpretation: %s",GetImageProperty(image,"tiff:photometric",
1002 exception));
cristy3ed852e2009-09-05 21:47:34 +00001003 }
cristybb503372010-05-27 20:51:26 +00001004 image->columns=(size_t) width;
1005 image->rows=(size_t) height;
1006 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +00001007 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +00001008 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
1009 (double) image->depth);
cristyf2687ca2010-06-29 16:32:38 +00001010 lsb_first=1;
1011 image->endian=MSBEndian;
1012 if ((int) (*(char *) &lsb_first) != 0)
1013 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001014 if (photometric == PHOTOMETRIC_SEPARATED)
1015 image->colorspace=CMYKColorspace;
1016 if (photometric == PHOTOMETRIC_CIELAB)
1017 image->colorspace=LabColorspace;
1018 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
1019 &samples_per_pixel);
1020 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
cristy2a11bef2011-10-28 18:33:11 +00001021 x_resolution=(float) image->resolution.x;
1022 y_resolution=(float) image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +00001023 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
1024 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
cristy2a11bef2011-10-28 18:33:11 +00001025 image->resolution.x=x_resolution;
1026 image->resolution.y=y_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001027 x_position=(float) image->page.x/x_resolution;
1028 y_position=(float) image->page.y/y_resolution;
1029 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
1030 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +00001031 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
1032 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001033 image->orientation=(OrientationType) orientation;
1034 chromaticity=(float *) NULL;
1035 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
1036 if (chromaticity != (float *) NULL)
1037 {
1038 image->chromaticity.white_point.x=chromaticity[0];
1039 image->chromaticity.white_point.y=chromaticity[1];
1040 }
1041 chromaticity=(float *) NULL;
1042 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
1043 if (chromaticity != (float *) NULL)
1044 {
1045 image->chromaticity.red_primary.x=chromaticity[0];
1046 image->chromaticity.red_primary.y=chromaticity[1];
1047 image->chromaticity.green_primary.x=chromaticity[2];
1048 image->chromaticity.green_primary.y=chromaticity[3];
1049 image->chromaticity.blue_primary.x=chromaticity[4];
1050 image->chromaticity.blue_primary.y=chromaticity[5];
1051 }
cristy3ed852e2009-09-05 21:47:34 +00001052 /*
1053 Allocate memory for the image and pixel buffer.
1054 */
1055#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1056 if ((compress_tag != COMPRESSION_NONE) &&
1057 (TIFFIsCODECConfigured(compress_tag) == 0))
1058 {
1059 TIFFClose(tiff);
1060 ThrowReaderException(CoderError,"CompressNotSupported");
1061 }
1062#endif
1063 switch (compress_tag)
1064 {
1065 case COMPRESSION_NONE: image->compression=NoCompression; break;
1066 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1067 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1068 case COMPRESSION_JPEG:
1069 {
1070 image->compression=JPEGCompression;
1071#if defined(JPEG_SUPPORT)
1072 {
1073 char
1074 sampling_factor[MaxTextExtent];
1075
1076 uint16
1077 horizontal,
1078 vertical;
1079
1080 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1081 &horizontal,&vertical);
cristyb51dff52011-05-19 16:55:47 +00001082 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
cristy3ed852e2009-09-05 21:47:34 +00001083 horizontal,vertical);
1084 (void) SetImageProperty(image,"jpeg:sampling-factor",
cristyd15e6592011-10-15 00:13:06 +00001085 sampling_factor,exception);
cristy3ed852e2009-09-05 21:47:34 +00001086 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1087 "Sampling Factors: %s",sampling_factor);
1088 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1089 {
1090 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1091 JPEGCOLORMODE_RGB);
1092 photometric=PHOTOMETRIC_RGB;
1093 }
1094 }
1095#endif
1096 break;
1097 }
1098 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001099#if defined(COMPRESSION_LZMA)
1100 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1101#endif
cristy3ed852e2009-09-05 21:47:34 +00001102 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1103 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1104 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1105 default: image->compression=RLECompression; break;
1106 }
1107 quantum_info=AcquireQuantumInfo(image_info,image);
1108 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001109 {
1110 TIFFClose(tiff);
1111 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1112 }
cristy3ed852e2009-09-05 21:47:34 +00001113 if (sample_format == SAMPLEFORMAT_UINT)
1114 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1115 if (sample_format == SAMPLEFORMAT_INT)
1116 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1117 if (sample_format == SAMPLEFORMAT_IEEEFP)
1118 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1119 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001120 {
cristya9a6ec42011-10-26 16:55:01 +00001121 quantum_info=DestroyQuantumInfo(quantum_info);
cristy8d137bf2010-04-21 23:52:04 +00001122 TIFFClose(tiff);
1123 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1124 }
cristy3ed852e2009-09-05 21:47:34 +00001125 status=MagickTrue;
1126 switch (photometric)
1127 {
1128 case PHOTOMETRIC_MINISBLACK:
1129 {
1130 quantum_info->min_is_white=MagickFalse;
1131 break;
1132 }
1133 case PHOTOMETRIC_MINISWHITE:
1134 {
1135 quantum_info->min_is_white=MagickTrue;
1136 break;
1137 }
1138 default:
1139 break;
1140 }
cristy891dc792010-03-04 01:47:16 +00001141 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001142 extra_samples=0;
1143 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1144 &sample_info);
1145 if (extra_samples == 0)
1146 {
1147 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1148 image->matte=MagickTrue;
1149 }
1150 else
cristy6c207342010-02-19 17:49:50 +00001151 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001152 {
cristy7cf69862010-03-03 13:57:24 +00001153 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001154 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001155 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001156 }
cristy891dc792010-03-04 01:47:16 +00001157 option=GetImageOption(image_info,"tiff:alpha");
1158 if (option != (const char *) NULL)
1159 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1160 MagickFalse;
1161 if (image->matte != MagickFalse)
1162 (void) SetImageProperty(image,"tiff:alpha",
cristyd15e6592011-10-15 00:13:06 +00001163 associated_alpha != MagickFalse ? "associated" : "unassociated",
1164 exception);
cristy3ed852e2009-09-05 21:47:34 +00001165 if ((photometric == PHOTOMETRIC_PALETTE) &&
1166 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1167 {
cristybb503372010-05-27 20:51:26 +00001168 size_t
cristy3ed852e2009-09-05 21:47:34 +00001169 colors;
1170
cristybb503372010-05-27 20:51:26 +00001171 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy018f07f2011-09-04 21:15:19 +00001172 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001173 {
1174 TIFFClose(tiff);
1175 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1176 }
1177 }
1178 if (units == RESUNIT_INCH)
1179 image->units=PixelsPerInchResolution;
1180 if (units == RESUNIT_CENTIMETER)
1181 image->units=PixelsPerCentimeterResolution;
1182 value=(unsigned short) image->scene;
1183 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1184 image->scene=value;
1185 if (image_info->ping != MagickFalse)
1186 {
1187 if (image_info->number_scenes != 0)
1188 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1189 break;
1190 goto next_tiff_frame;
1191 }
1192 method=ReadGenericMethod;
1193 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1194 {
1195 char
1196 value[MaxTextExtent];
1197
1198 method=ReadStripMethod;
cristyb51dff52011-05-19 16:55:47 +00001199 (void) FormatLocaleString(value,MaxTextExtent,"%u",
cristy3ed852e2009-09-05 21:47:34 +00001200 (unsigned int) rows_per_strip);
cristyd15e6592011-10-15 00:13:06 +00001201 (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception);
cristy3ed852e2009-09-05 21:47:34 +00001202 }
1203 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1204 method=ReadRGBAMethod;
1205 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1206 method=ReadCMYKAMethod;
1207 if ((photometric != PHOTOMETRIC_RGB) &&
1208 (photometric != PHOTOMETRIC_CIELAB) &&
1209 (photometric != PHOTOMETRIC_SEPARATED))
1210 method=ReadGenericMethod;
1211 if (image->storage_class == PseudoClass)
1212 method=ReadSingleSampleMethod;
1213 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1214 (photometric == PHOTOMETRIC_MINISWHITE))
1215 method=ReadSingleSampleMethod;
1216 if ((photometric != PHOTOMETRIC_SEPARATED) &&
cristybc736662011-03-09 13:39:42 +00001217 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
cristy3ed852e2009-09-05 21:47:34 +00001218 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001219 if (image->compression == JPEGCompression)
1220 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001221 if (TIFFIsTiled(tiff) != MagickFalse)
1222 method=ReadTileMethod;
1223 quantum_type=RGBQuantum;
1224 pixels=GetQuantumPixels(quantum_info);
1225 switch (method)
1226 {
1227 case ReadSingleSampleMethod:
1228 {
1229 /*
1230 Convert TIFF image to PseudoClass MIFF image.
1231 */
1232 if ((image->storage_class == PseudoClass) &&
1233 (photometric == PHOTOMETRIC_PALETTE))
1234 {
cristybb503372010-05-27 20:51:26 +00001235 size_t
cristy3ed852e2009-09-05 21:47:34 +00001236 range;
1237
1238 uint16
1239 *blue_colormap,
1240 *green_colormap,
1241 *red_colormap;
1242
1243 /*
1244 Initialize colormap.
1245 */
1246 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1247 &green_colormap,&blue_colormap);
1248 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001249 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001250 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1251 (blue_colormap[i] >= 256))
1252 {
1253 range=65535;
1254 break;
1255 }
cristybb503372010-05-27 20:51:26 +00001256 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001257 {
cristyce70c172010-01-07 17:15:30 +00001258 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001259 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001260 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001261 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001262 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001263 blue_colormap[i])/range);
1264 }
1265 }
1266 quantum_type=IndexQuantum;
1267 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1268 if (image->matte != MagickFalse)
1269 {
1270 if (image->storage_class != PseudoClass)
1271 {
1272 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1273 GrayAlphaQuantum;
1274 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1275 }
1276 else
1277 {
1278 quantum_type=IndexAlphaQuantum;
1279 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1280 }
1281 }
1282 else
1283 if (image->storage_class != PseudoClass)
1284 {
1285 quantum_type=GrayQuantum;
1286 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1287 }
1288 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1289 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001290 {
1291 TIFFClose(tiff);
1292 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1293 }
cristy3ed852e2009-09-05 21:47:34 +00001294 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001295 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001296 {
1297 int
1298 status;
1299
cristy4c08aed2011-07-01 19:47:50 +00001300 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001301 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001302
1303 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1304 if (status == -1)
1305 break;
1306 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001307 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001308 break;
1309 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1310 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00001311 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00001312 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1313 break;
1314 if (image->previous == (Image *) NULL)
1315 {
cristycee97112010-05-28 00:44:52 +00001316 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1317 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001318 if (status == MagickFalse)
1319 break;
1320 }
1321 }
1322 break;
1323 }
1324 case ReadRGBAMethod:
1325 {
1326 /*
1327 Convert TIFF image to DirectClass MIFF image.
1328 */
1329 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1330 quantum_type=RGBQuantum;
1331 if (image->matte != MagickFalse)
1332 {
1333 quantum_type=RGBAQuantum;
1334 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1335 }
1336 if (image->colorspace == CMYKColorspace)
1337 {
1338 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1339 quantum_type=CMYKQuantum;
1340 if (image->matte != MagickFalse)
1341 {
1342 quantum_type=CMYKAQuantum;
1343 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1344 }
1345 }
1346 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1347 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001348 {
1349 TIFFClose(tiff);
1350 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1351 }
cristy3ed852e2009-09-05 21:47:34 +00001352 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001353 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001354 {
1355 int
1356 status;
1357
cristy4c08aed2011-07-01 19:47:50 +00001358 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001359 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001360
1361 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1362 if (status == -1)
1363 break;
1364 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001365 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001366 break;
cristy0b334892010-03-15 02:26:46 +00001367 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1368 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001369 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1370 break;
1371 if (image->previous == (Image *) NULL)
1372 {
cristycee97112010-05-28 00:44:52 +00001373 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1374 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001375 if (status == MagickFalse)
1376 break;
1377 }
1378 }
1379 break;
1380 }
1381 case ReadCMYKAMethod:
1382 {
1383 /*
1384 Convert TIFF image to DirectClass MIFF image.
1385 */
cristybb503372010-05-27 20:51:26 +00001386 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001387 {
cristybb503372010-05-27 20:51:26 +00001388 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001389 {
cristy4c08aed2011-07-01 19:47:50 +00001390 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001391 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001392
cristy3ed852e2009-09-05 21:47:34 +00001393 int
1394 status;
1395
1396 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1397 pixels);
1398 if (status == -1)
1399 break;
1400 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001401 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001402 break;
1403 if (image->colorspace != CMYKColorspace)
1404 switch (i)
1405 {
1406 case 0: quantum_type=RedQuantum; break;
1407 case 1: quantum_type=GreenQuantum; break;
1408 case 2: quantum_type=BlueQuantum; break;
1409 case 3: quantum_type=AlphaQuantum; break;
1410 default: quantum_type=UndefinedQuantum; break;
1411 }
cristy6d9f12f2009-11-03 14:50:26 +00001412 else
cristy3ed852e2009-09-05 21:47:34 +00001413 switch (i)
1414 {
1415 case 0: quantum_type=CyanQuantum; break;
1416 case 1: quantum_type=MagentaQuantum; break;
1417 case 2: quantum_type=YellowQuantum; break;
1418 case 3: quantum_type=BlackQuantum; break;
1419 case 4: quantum_type=AlphaQuantum; break;
1420 default: quantum_type=UndefinedQuantum; break;
1421 }
1422 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1423 quantum_type,pixels,exception);
1424 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1425 break;
1426 }
1427 if (image->previous == (Image *) NULL)
1428 {
cristycee97112010-05-28 00:44:52 +00001429 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1430 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001431 if (status == MagickFalse)
1432 break;
1433 }
1434 }
1435 break;
1436 }
1437 case ReadStripMethod:
1438 {
1439 register unsigned char
1440 *p;
1441
1442 /*
1443 Convert stripped TIFF image to DirectClass MIFF image.
1444 */
1445 i=0;
1446 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001447 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001448 {
cristybb503372010-05-27 20:51:26 +00001449 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001450 x;
1451
cristy4c08aed2011-07-01 19:47:50 +00001452 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001453 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001454
1455 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001456 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001457 break;
1458 if (i == 0)
1459 {
1460 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1461 break;
cristyeaedf062010-05-29 22:36:02 +00001462 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1463 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001464 }
1465 i--;
cristy7f5d1662011-04-14 12:43:28 +00001466 p=(unsigned char *) (((uint32 *) pixels)+image->columns*i);
cristybb503372010-05-27 20:51:26 +00001467 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001468 {
cristy4c08aed2011-07-01 19:47:50 +00001469 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1470 (TIFFGetR(*p))),q);
1471 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1472 (TIFFGetG(*p))),q);
1473 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1474 (TIFFGetB(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001475 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001476 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1477 (TIFFGetA(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001478 p++;
cristyed231572011-07-14 02:18:59 +00001479 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001480 }
1481 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1482 break;
1483 if (image->previous == (Image *) NULL)
1484 {
cristycee97112010-05-28 00:44:52 +00001485 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1486 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001487 if (status == MagickFalse)
1488 break;
1489 }
1490 }
1491 break;
1492 }
1493 case ReadTileMethod:
1494 {
1495 register uint32
1496 *p;
1497
1498 uint32
1499 *tile_pixels,
1500 columns,
1501 rows;
1502
cristybb503372010-05-27 20:51:26 +00001503 size_t
cristy3ed852e2009-09-05 21:47:34 +00001504 number_pixels;
1505
1506 /*
1507 Convert tiled TIFF image to DirectClass MIFF image.
1508 */
1509 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1510 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1511 {
1512 TIFFClose(tiff);
1513 ThrowReaderException(CoderError,"ImageIsNotTiled");
1514 }
cristyc82a27b2011-10-21 01:07:16 +00001515 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +00001516 number_pixels=columns*rows;
cristyda16f162011-02-19 23:52:17 +00001517 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
cristy3ed852e2009-09-05 21:47:34 +00001518 sizeof(*tile_pixels));
1519 if (tile_pixels == (uint32 *) NULL)
1520 {
1521 TIFFClose(tiff);
1522 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1523 }
cristybb503372010-05-27 20:51:26 +00001524 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001525 {
cristybb503372010-05-27 20:51:26 +00001526 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001527 x;
1528
cristy4c08aed2011-07-01 19:47:50 +00001529 register Quantum
1530 *restrict q,
1531 *restrict tile;
cristy3ed852e2009-09-05 21:47:34 +00001532
cristybb503372010-05-27 20:51:26 +00001533 size_t
cristy3ed852e2009-09-05 21:47:34 +00001534 columns_remaining,
1535 rows_remaining;
1536
1537 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001538 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001539 rows_remaining=rows;
1540 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1541 exception);
cristy4c08aed2011-07-01 19:47:50 +00001542 if (tile == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001543 break;
cristybb503372010-05-27 20:51:26 +00001544 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001545 {
cristybb503372010-05-27 20:51:26 +00001546 size_t
cristy3ed852e2009-09-05 21:47:34 +00001547 column,
1548 row;
1549
1550 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1551 break;
1552 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001553 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001554 columns_remaining=columns;
1555 p=tile_pixels+(rows-rows_remaining)*columns;
cristy68473222011-08-17 17:39:19 +00001556 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1557 x);
cristy3ed852e2009-09-05 21:47:34 +00001558 for (row=rows_remaining; row > 0; row--)
1559 {
1560 if (image->matte != MagickFalse)
1561 for (column=columns_remaining; column > 0; column--)
1562 {
cristy4c08aed2011-07-01 19:47:50 +00001563 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1564 TIFFGetR(*p)),q);
1565 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1566 TIFFGetG(*p)),q);
1567 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1568 TIFFGetB(*p)),q);
1569 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1570 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001571 p++;
cristyed231572011-07-14 02:18:59 +00001572 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001573 }
1574 else
1575 for (column=columns_remaining; column > 0; column--)
1576 {
cristy4c08aed2011-07-01 19:47:50 +00001577 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1578 TIFFGetR(*p)),q);
1579 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1580 TIFFGetG(*p)),q);
1581 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1582 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001583 p++;
cristyed231572011-07-14 02:18:59 +00001584 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001585 }
1586 p+=columns-columns_remaining;
cristyed231572011-07-14 02:18:59 +00001587 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
cristy3ed852e2009-09-05 21:47:34 +00001588 }
1589 }
1590 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1591 break;
1592 if (image->previous == (Image *) NULL)
1593 {
cristycee97112010-05-28 00:44:52 +00001594 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1595 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001596 if (status == MagickFalse)
1597 break;
1598 }
1599 }
1600 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1601 break;
1602 }
1603 case ReadGenericMethod:
1604 default:
1605 {
1606 register uint32
1607 *p;
1608
1609 uint32
1610 *pixels;
1611
1612 /*
1613 Convert TIFF image to DirectClass MIFF image.
1614 */
1615 number_pixels=(MagickSizeType) image->columns*image->rows;
1616 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1617 (number_pixels*sizeof(uint32))))
1618 {
1619 TIFFClose(tiff);
1620 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1621 }
1622 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1623 sizeof(uint32));
1624 if (pixels == (uint32 *) NULL)
1625 {
1626 TIFFClose(tiff);
1627 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1628 }
1629 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1630 (uint32) image->rows,(uint32 *) pixels,0);
1631 /*
1632 Convert image to DirectClass pixel packets.
1633 */
1634 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001635 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001636 {
cristybb503372010-05-27 20:51:26 +00001637 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001638 x;
1639
cristy4c08aed2011-07-01 19:47:50 +00001640 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001641 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001642
1643 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001644 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001645 break;
cristyed231572011-07-14 02:18:59 +00001646 q+=GetPixelChannels(image)*(image->columns-1);
cristybb503372010-05-27 20:51:26 +00001647 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001648 {
cristy4c08aed2011-07-01 19:47:50 +00001649 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1650 TIFFGetR(*p)),q);
1651 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1652 TIFFGetG(*p)),q);
1653 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1654 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001655 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001656 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1657 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001658 p--;
cristyed231572011-07-14 02:18:59 +00001659 q-=GetPixelChannels(image);;
cristy3ed852e2009-09-05 21:47:34 +00001660 }
1661 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1662 break;
1663 if (image->previous == (Image *) NULL)
1664 {
cristycee97112010-05-28 00:44:52 +00001665 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1666 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001667 if (status == MagickFalse)
1668 break;
1669 }
1670 }
1671 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1672 break;
1673 }
1674 }
1675 SetQuantumImageType(image,quantum_type);
1676 next_tiff_frame:
cristya9a6ec42011-10-26 16:55:01 +00001677 quantum_info=DestroyQuantumInfo(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +00001678 if ((photometric == PHOTOMETRIC_LOGL) ||
1679 (photometric == PHOTOMETRIC_MINISBLACK) ||
1680 (photometric == PHOTOMETRIC_MINISWHITE))
1681 {
1682 image->type=GrayscaleType;
1683 if (bits_per_sample == 1)
1684 image->type=BilevelType;
1685 }
1686 if (image->storage_class == PseudoClass)
1687 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001688 image->endian=MSBEndian;
1689 if (endian == FILLORDER_LSB2MSB)
1690 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001691 if ((photometric == PHOTOMETRIC_LOGL) ||
1692 (photometric == PHOTOMETRIC_MINISBLACK) ||
1693 (photometric == PHOTOMETRIC_MINISWHITE))
1694 {
1695 image->type=GrayscaleType;
1696 if (bits_per_sample == 1)
1697 image->type=BilevelType;
1698 }
1699 /*
1700 Proceed to next image.
1701 */
1702 if (image_info->number_scenes != 0)
1703 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1704 break;
1705 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1706 if (status == MagickTrue)
1707 {
1708 /*
1709 Allocate next image structure.
1710 */
cristy9950d572011-10-01 18:22:35 +00001711 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001712 if (GetNextImageInList(image) == (Image *) NULL)
1713 {
1714 image=DestroyImageList(image);
1715 return((Image *) NULL);
1716 }
1717 image=SyncNextImageInList(image);
1718 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1719 image->scene);
1720 if (status == MagickFalse)
1721 break;
1722 }
cristy3ed852e2009-09-05 21:47:34 +00001723 } while (status == MagickTrue);
1724 (void) TIFFSetWarningHandler(warning_handler);
1725 (void) TIFFSetErrorHandler(error_handler);
1726 TIFFClose(tiff);
1727 return(GetFirstImageInList(image));
1728}
1729#endif
1730
1731/*
1732%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1733% %
1734% %
1735% %
1736% R e g i s t e r T I F F I m a g e %
1737% %
1738% %
1739% %
1740%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1741%
1742% RegisterTIFFImage() adds properties for the TIFF image format to
1743% the list of supported formats. The properties include the image format
1744% tag, a method to read and/or write the format, whether the format
1745% supports the saving of more than one frame to the same file or blob,
1746% whether the format supports native in-memory I/O, and a brief
1747% description of the format.
1748%
1749% The format of the RegisterTIFFImage method is:
1750%
cristybb503372010-05-27 20:51:26 +00001751% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001752%
1753*/
cristy7e3eb0a2011-11-09 22:50:49 +00001754
1755#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
1756static TIFFExtendProc
cristy60e1bf92011-11-09 23:00:15 +00001757 tag_extender = (TIFFExtendProc) NULL;
cristy7e3eb0a2011-11-09 22:50:49 +00001758
1759static void TIFFTagExtender(TIFF *tiff)
1760{
1761 static const TIFFFieldInfo
1762 TIFFExtensions[] =
1763 {
1764 { 37724, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1, "PhotoshopLayerData" }
1765 };
1766
1767 TIFFMergeFieldInfo(tiff,TIFFExtensions,sizeof(TIFFExtensions)/
1768 sizeof(*TIFFExtensions));
cristy60e1bf92011-11-09 23:00:15 +00001769 if (tag_extender != (TIFFExtendProc) NULL)
1770 (*tag_extender)(tiff);
cristy7e3eb0a2011-11-09 22:50:49 +00001771}
1772#endif
1773
cristybb503372010-05-27 20:51:26 +00001774ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001775{
1776#define TIFFDescription "Tagged Image File Format"
1777
1778 char
1779 version[MaxTextExtent];
1780
1781 MagickInfo
1782 *entry;
cristy6b032822010-06-29 16:52:32 +00001783
cristy18b17442009-10-25 18:36:48 +00001784 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1785 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001786 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001787 if (instantiate_key == MagickFalse)
1788 {
1789 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1790 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristy7e3eb0a2011-11-09 22:50:49 +00001791#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
cristy60e1bf92011-11-09 23:00:15 +00001792 if (tag_extender == (TIFFExtendProc) NULL)
1793 tag_extender=TIFFSetTagExtender(TIFFTagExtender);
cristy7e3eb0a2011-11-09 22:50:49 +00001794#endif
cristy3ed852e2009-09-05 21:47:34 +00001795 instantiate_key=MagickTrue;
1796 }
cristyf84a1932010-01-03 18:00:18 +00001797 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001798 *version='\0';
1799#if defined(TIFF_VERSION)
cristyb51dff52011-05-19 16:55:47 +00001800 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
cristy3ed852e2009-09-05 21:47:34 +00001801#endif
1802#if defined(MAGICKCORE_TIFF_DELEGATE)
1803 {
1804 const char
1805 *p;
1806
cristybb503372010-05-27 20:51:26 +00001807 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001808 i;
1809
1810 p=TIFFGetVersion();
1811 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1812 version[i]=(*p++);
1813 version[i]='\0';
1814 }
1815#endif
1816
cristy3d7f8062009-09-24 20:45:35 +00001817 entry=SetMagickInfo("GROUP4");
1818#if defined(MAGICKCORE_TIFF_DELEGATE)
1819 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1820 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1821#endif
1822 entry->raw=MagickTrue;
1823 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001824 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001825 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001826 entry->seekable_stream=MagickTrue;
1827 entry->thread_support=NoThreadSupport;
1828 entry->description=ConstantString("Raw CCITT Group4");
1829 entry->module=ConstantString("TIFF");
1830 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001831 entry=SetMagickInfo("PTIF");
1832#if defined(MAGICKCORE_TIFF_DELEGATE)
1833 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1834 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1835#endif
1836 entry->endian_support=MagickTrue;
1837 entry->seekable_stream=MagickTrue;
1838 entry->thread_support=NoThreadSupport;
1839 entry->description=ConstantString("Pyramid encoded TIFF");
1840 entry->module=ConstantString("TIFF");
1841 (void) RegisterMagickInfo(entry);
1842 entry=SetMagickInfo("TIF");
1843#if defined(MAGICKCORE_TIFF_DELEGATE)
1844 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1845 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1846#endif
1847 entry->endian_support=MagickTrue;
1848 entry->seekable_stream=MagickTrue;
1849 entry->stealth=MagickTrue;
1850 entry->thread_support=NoThreadSupport;
1851 entry->description=ConstantString(TIFFDescription);
1852 if (*version != '\0')
1853 entry->version=ConstantString(version);
1854 entry->module=ConstantString("TIFF");
1855 (void) RegisterMagickInfo(entry);
1856 entry=SetMagickInfo("TIFF");
1857#if defined(MAGICKCORE_TIFF_DELEGATE)
1858 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1859 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1860#endif
1861 entry->magick=(IsImageFormatHandler *) IsTIFF;
1862 entry->endian_support=MagickTrue;
1863 entry->seekable_stream=MagickTrue;
1864 entry->thread_support=NoThreadSupport;
1865 entry->description=ConstantString(TIFFDescription);
1866 if (*version != '\0')
1867 entry->version=ConstantString(version);
1868 entry->module=ConstantString("TIFF");
1869 (void) RegisterMagickInfo(entry);
1870 entry=SetMagickInfo("TIFF64");
1871#if defined(TIFF_VERSION_BIG)
1872 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
cristy9d6964e2011-11-10 17:48:45 +00001873 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
cristy3ed852e2009-09-05 21:47:34 +00001874#endif
1875 entry->adjoin=MagickFalse;
1876 entry->endian_support=MagickTrue;
1877 entry->seekable_stream=MagickTrue;
1878 entry->thread_support=NoThreadSupport;
1879 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1880 if (*version != '\0')
1881 entry->version=ConstantString(version);
1882 entry->module=ConstantString("TIFF");
1883 (void) RegisterMagickInfo(entry);
1884 return(MagickImageCoderSignature);
1885}
1886
1887/*
1888%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1889% %
1890% %
1891% %
1892% U n r e g i s t e r T I F F I m a g e %
1893% %
1894% %
1895% %
1896%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1897%
1898% UnregisterTIFFImage() removes format registrations made by the TIFF module
1899% from the list of supported formats.
1900%
1901% The format of the UnregisterTIFFImage method is:
1902%
1903% UnregisterTIFFImage(void)
1904%
1905*/
1906ModuleExport void UnregisterTIFFImage(void)
1907{
cristy3ed852e2009-09-05 21:47:34 +00001908 (void) UnregisterMagickInfo("TIFF64");
cristy9d6964e2011-11-10 17:48:45 +00001909 (void) UnregisterMagickInfo("TIFF");
1910 (void) UnregisterMagickInfo("TIF");
1911 (void) UnregisterMagickInfo("PTIF");
cristy514e9e72009-11-20 02:12:08 +00001912 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1913 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001914 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001915 if (instantiate_key != MagickFalse)
cristy9ebdd932011-11-09 23:07:28 +00001916 {
1917#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
1918 if (tag_extender == (TIFFExtendProc) NULL)
1919 (void) TIFFSetTagExtender(tag_extender);
1920#endif
1921 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1922 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1923 instantiate_key=MagickFalse;
1924 }
cristyf84a1932010-01-03 18:00:18 +00001925 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001926 DestroySemaphoreInfo(&tiff_semaphore);
1927}
1928
1929#if defined(MAGICKCORE_TIFF_DELEGATE)
1930/*
1931%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1932% %
1933% %
1934% %
cristy3d7f8062009-09-24 20:45:35 +00001935% W r i t e G R O U P 4 I m a g e %
1936% %
1937% %
1938% %
1939%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1940%
1941% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1942%
1943% The format of the WriteGROUP4Image method is:
1944%
1945% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001946% Image *image,ExceptionInfo *)
cristy3d7f8062009-09-24 20:45:35 +00001947%
1948% A description of each parameter follows:
1949%
1950% o image_info: the image info.
1951%
1952% o image: The image.
1953%
cristy3a37efd2011-08-28 20:31:03 +00001954% o exception: return any errors or warnings in this structure.
1955%
cristy3d7f8062009-09-24 20:45:35 +00001956*/
1957static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001958 Image *image,ExceptionInfo *exception)
cristy3d7f8062009-09-24 20:45:35 +00001959{
1960 char
1961 filename[MaxTextExtent];
1962
1963 FILE
1964 *file;
1965
1966 Image
1967 *huffman_image;
1968
1969 ImageInfo
1970 *write_info;
1971
1972 int
1973 unique_file;
1974
1975 MagickBooleanType
1976 status;
1977
cristybb503372010-05-27 20:51:26 +00001978 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001979 i;
1980
1981 ssize_t
1982 count;
1983
1984 TIFF
1985 *tiff;
1986
cristy94c8fe42009-10-06 01:57:36 +00001987 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001988 *byte_count,
1989 strip_size;
1990
1991 unsigned char
1992 *buffer;
1993
1994 /*
1995 Write image as CCITT Group4 TIFF image to a temporary file.
1996 */
1997 assert(image_info != (const ImageInfo *) NULL);
1998 assert(image_info->signature == MagickSignature);
1999 assert(image != (Image *) NULL);
2000 assert(image->signature == MagickSignature);
2001 if (image->debug != MagickFalse)
2002 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002003 assert(exception != (ExceptionInfo *) NULL);
2004 assert(exception->signature == MagickSignature);
2005 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3d7f8062009-09-24 20:45:35 +00002006 if (status == MagickFalse)
2007 return(status);
cristy3a37efd2011-08-28 20:31:03 +00002008 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3d7f8062009-09-24 20:45:35 +00002009 if (huffman_image == (Image *) NULL)
2010 {
2011 (void) CloseBlob(image);
2012 return(MagickFalse);
2013 }
cristy94c8fe42009-10-06 01:57:36 +00002014 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00002015 file=(FILE *) NULL;
2016 unique_file=AcquireUniqueFileResource(filename);
2017 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00002018 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00002019 if ((unique_file == -1) || (file == (FILE *) NULL))
2020 {
cristy3a37efd2011-08-28 20:31:03 +00002021 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2022 filename);
cristy3d7f8062009-09-24 20:45:35 +00002023 return(MagickFalse);
2024 }
cristyb51dff52011-05-19 16:55:47 +00002025 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
cristy3d7f8062009-09-24 20:45:35 +00002026 filename);
cristy018f07f2011-09-04 21:15:19 +00002027 (void) SetImageType(huffman_image,BilevelType,exception);
cristy14efd992009-09-26 23:46:03 +00002028 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00002029 SetImageInfoFile(write_info,file);
2030 write_info->compression=Group4Compression;
2031 write_info->type=BilevelType;
2032 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
cristy3a37efd2011-08-28 20:31:03 +00002033 status=WriteTIFFImage(write_info,huffman_image,exception);
cristy3d7f8062009-09-24 20:45:35 +00002034 (void) fflush(file);
2035 write_info=DestroyImageInfo(write_info);
2036 if (status == MagickFalse)
2037 {
cristy3d7f8062009-09-24 20:45:35 +00002038 huffman_image=DestroyImage(huffman_image);
2039 (void) fclose(file);
2040 (void) RelinquishUniqueFileResource(filename);
2041 return(MagickFalse);
2042 }
2043 tiff=TIFFOpen(filename,"rb");
2044 if (tiff == (TIFF *) NULL)
2045 {
2046 huffman_image=DestroyImage(huffman_image);
2047 (void) fclose(file);
2048 (void) RelinquishUniqueFileResource(filename);
cristy3a37efd2011-08-28 20:31:03 +00002049 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
cristy3d7f8062009-09-24 20:45:35 +00002050 image_info->filename);
2051 return(MagickFalse);
2052 }
2053 /*
2054 Allocate raw strip buffer.
2055 */
cristy94c8fe42009-10-06 01:57:36 +00002056 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2057 {
2058 TIFFClose(tiff);
2059 huffman_image=DestroyImage(huffman_image);
2060 (void) fclose(file);
2061 (void) RelinquishUniqueFileResource(filename);
2062 return(MagickFalse);
2063 }
cristy3d7f8062009-09-24 20:45:35 +00002064 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00002065 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002066 if (byte_count[i] > strip_size)
2067 strip_size=byte_count[i];
2068 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2069 sizeof(*buffer));
2070 if (buffer == (unsigned char *) NULL)
2071 {
2072 TIFFClose(tiff);
2073 huffman_image=DestroyImage(huffman_image);
2074 (void) fclose(file);
2075 (void) RelinquishUniqueFileResource(filename);
2076 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2077 image_info->filename);
2078 }
2079 /*
2080 Compress runlength encoded to 2D Huffman pixels.
2081 */
cristybb503372010-05-27 20:51:26 +00002082 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002083 {
cristy94c8fe42009-10-06 01:57:36 +00002084 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00002085 if (WriteBlob(image,(size_t) count,buffer) != count)
2086 status=MagickFalse;
2087 }
2088 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2089 TIFFClose(tiff);
2090 huffman_image=DestroyImage(huffman_image);
2091 (void) fclose(file);
2092 (void) RelinquishUniqueFileResource(filename);
2093 (void) CloseBlob(image);
2094 return(status);
2095}
2096#endif
2097
2098#if defined(MAGICKCORE_TIFF_DELEGATE)
2099/*
2100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2101% %
2102% %
2103% %
cristy3ed852e2009-09-05 21:47:34 +00002104% W r i t e P T I F I m a g e %
2105% %
2106% %
2107% %
2108%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2109%
2110% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2111% format.
2112%
2113% The format of the WritePTIFImage method is:
2114%
2115% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002116% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002117%
2118% A description of each parameter follows:
2119%
2120% o image_info: the image info.
2121%
2122% o image: The image.
2123%
cristy3a37efd2011-08-28 20:31:03 +00002124% o exception: return any errors or warnings in this structure.
2125%
cristy3ed852e2009-09-05 21:47:34 +00002126*/
2127static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002128 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002129{
2130 Image
2131 *images,
2132 *next,
2133 *pyramid_image;
2134
2135 ImageInfo
2136 *write_info;
2137
2138 MagickBooleanType
2139 status;
2140
cristybb503372010-05-27 20:51:26 +00002141 size_t
cristy3ed852e2009-09-05 21:47:34 +00002142 columns,
2143 rows;
2144
2145 /*
2146 Create pyramid-encoded TIFF image.
2147 */
2148 images=NewImageList();
2149 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2150 {
cristy3a37efd2011-08-28 20:31:03 +00002151 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,exception));
cristy3ed852e2009-09-05 21:47:34 +00002152 columns=next->columns;
2153 rows=next->rows;
2154 while ((columns > 64) && (rows > 64))
2155 {
2156 columns/=2;
2157 rows/=2;
cristy98ca0f52011-10-08 23:19:10 +00002158 pyramid_image=ResizeImage(next,columns,rows,image->filter,image->blur,
cristy3a37efd2011-08-28 20:31:03 +00002159 exception);
cristy3ed852e2009-09-05 21:47:34 +00002160 AppendImageToList(&images,pyramid_image);
2161 }
2162 }
2163 /*
2164 Write pyramid-encoded TIFF image.
2165 */
cristy58567532009-09-26 01:13:32 +00002166 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002167 *write_info->magick='\0';
2168 write_info->adjoin=MagickTrue;
cristy3a37efd2011-08-28 20:31:03 +00002169 status=WriteTIFFImage(write_info,GetFirstImageInList(images),exception);
cristy3ed852e2009-09-05 21:47:34 +00002170 images=DestroyImageList(images);
2171 write_info=DestroyImageInfo(write_info);
2172 return(status);
2173}
2174#endif
2175
2176#if defined(MAGICKCORE_TIFF_DELEGATE)
2177/*
2178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2179% %
2180% %
2181% W r i t e T I F F I m a g e %
2182% %
2183% %
2184% %
2185%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2186%
2187% WriteTIFFImage() writes an image in the Tagged image file format.
2188%
2189% The format of the WriteTIFFImage method is:
2190%
2191% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002192% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002193%
2194% A description of each parameter follows:
2195%
2196% o image_info: the image info.
2197%
2198% o image: The image.
2199%
cristy3a37efd2011-08-28 20:31:03 +00002200% o exception: return any errors or warnings in this structure.
2201%
cristy3ed852e2009-09-05 21:47:34 +00002202*/
2203
2204typedef struct _TIFFInfo
2205{
2206 RectangleInfo
2207 tile_geometry;
2208
2209 unsigned char
2210 *scanline,
2211 *scanlines,
2212 *pixels;
2213} TIFFInfo;
2214
2215static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2216{
2217 assert(tiff_info != (TIFFInfo *) NULL);
2218 if (tiff_info->scanlines != (unsigned char *) NULL)
2219 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2220 tiff_info->scanlines);
2221 if (tiff_info->pixels != (unsigned char *) NULL)
2222 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2223 tiff_info->pixels);
2224}
2225
2226static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2227 TIFFInfo *tiff_info)
2228{
2229 const char
2230 *option;
2231
2232 MagickStatusType
2233 flags;
2234
cristy79e5dad2010-09-16 19:48:33 +00002235 uint32
2236 tile_columns,
2237 tile_rows;
2238
cristy3ed852e2009-09-05 21:47:34 +00002239 assert(tiff_info != (TIFFInfo *) NULL);
2240 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2241 option=GetImageOption(image_info,"tiff:tile-geometry");
2242 if (option == (const char *) NULL)
2243 return(MagickTrue);
2244 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2245 if ((flags & HeightValue) == 0)
2246 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002247 tile_columns=(uint32) tiff_info->tile_geometry.width;
2248 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002249 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2250 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2251 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2252 tiff_info->tile_geometry.width=tile_columns;
2253 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002254 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002255 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002256 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002257 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002258 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2259 (tiff_info->pixels == (unsigned char *) NULL))
2260 {
2261 DestroyTIFFInfo(tiff_info);
2262 return(MagickFalse);
2263 }
2264 return(MagickTrue);
2265}
2266
cristybb503372010-05-27 20:51:26 +00002267static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002268 tsample_t sample,Image *image)
2269{
2270 int32
2271 status;
2272
cristybb503372010-05-27 20:51:26 +00002273 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002274 i;
2275
2276 register unsigned char
2277 *p,
2278 *q;
2279
cristybb503372010-05-27 20:51:26 +00002280 size_t
cristy3ed852e2009-09-05 21:47:34 +00002281 number_tiles,
2282 tile_width;
2283
cristyc6da28e2011-04-28 01:41:35 +00002284 ssize_t
2285 bytes_per_pixel,
2286 j,
2287 k,
2288 l;
2289
cristy3ed852e2009-09-05 21:47:34 +00002290 if (TIFFIsTiled(tiff) == 0)
2291 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2292 /*
2293 Fill scanlines to tile height.
2294 */
cristybb503372010-05-27 20:51:26 +00002295 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002296 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2297 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002298 if (((size_t) (row % tiff_info->tile_geometry.height) !=
cristyc6da28e2011-04-28 01:41:35 +00002299 (tiff_info->tile_geometry.height-1)) &&
2300 (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002301 return(0);
2302 /*
2303 Write tile to TIFF image.
2304 */
2305 status=0;
cristybb503372010-05-27 20:51:26 +00002306 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002307 tiff_info->tile_geometry.width);
2308 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2309 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002310 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002311 {
cristybb503372010-05-27 20:51:26 +00002312 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002313 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002314 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2315 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002316 {
2317 if (bytes_per_pixel == 0)
2318 {
2319 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2320 tiff_info->tile_geometry.width+k)/8);
2321 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2322 *q++=(*p++);
2323 continue;
2324 }
2325 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2326 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2327 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2328 for (l=0; l < bytes_per_pixel; l++)
2329 *q++=(*p++);
2330 }
cristydaff8092010-04-22 14:50:53 +00002331 if ((i*tiff_info->tile_geometry.width) != image->columns)
2332 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2333 tiff_info->tile_geometry.width),(uint32) ((row/
2334 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2335 sample);
cristy3ed852e2009-09-05 21:47:34 +00002336 if (status < 0)
2337 break;
2338 }
2339 return(status);
2340}
2341
2342static void TIFFSetProfiles(TIFF *tiff,Image *image)
2343{
2344 const char
2345 *name;
2346
2347 const StringInfo
2348 *profile;
2349
2350 if (image->profiles == (void *) NULL)
2351 return;
2352 ResetImageProfileIterator(image);
2353 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2354 {
2355 profile=GetImageProfile(image,name);
2356#if defined(TIFFTAG_XMLPACKET)
2357 if (LocaleCompare(name,"xmp") == 0)
2358 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2359 profile),GetStringInfoDatum(profile));
2360#endif
2361#if defined(TIFFTAG_ICCPROFILE)
2362 if (LocaleCompare(name,"icc") == 0)
2363 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2364 profile),GetStringInfoDatum(profile));
2365#endif
2366 if (LocaleCompare(name,"iptc") == 0)
2367 {
2368 size_t
2369 length;
2370
2371 StringInfo
2372 *iptc_profile;
2373
2374 iptc_profile=CloneStringInfo(profile);
2375 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2376 0x03);
2377 SetStringInfoLength(iptc_profile,length);
2378 if (TIFFIsByteSwapped(tiff))
2379 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002380 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002381 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2382 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2383 iptc_profile=DestroyStringInfo(iptc_profile);
2384 }
2385#if defined(TIFFTAG_PHOTOSHOP)
2386 if (LocaleCompare(name,"8bim") == 0)
2387 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2388 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2389#endif
2390 if (LocaleCompare(name,"tiff:37724") == 0)
cristye5d85502011-11-09 19:45:06 +00002391 (void) TIFFSetField(tiff,37724,(uint32) GetStringInfoLength(profile),
cristy3ed852e2009-09-05 21:47:34 +00002392 GetStringInfoDatum(profile));
2393 name=GetNextImageProfile(image);
2394 }
2395}
2396
cristyd15e6592011-10-15 00:13:06 +00002397static void TIFFSetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002398{
2399 const char
2400 *value;
2401
cristy15d8d212010-09-17 01:56:30 +00002402 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristyd15e6592011-10-15 00:13:06 +00002403 value=GetImageProperty(image,"tiff:hostcomputer",exception);
cristy3ed852e2009-09-05 21:47:34 +00002404 if (value != (const char *) NULL)
2405 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
cristyd15e6592011-10-15 00:13:06 +00002406 value=GetImageProperty(image,"tiff:artist",exception);
cristy3ed852e2009-09-05 21:47:34 +00002407 if (value != (const char *) NULL)
2408 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
cristyd15e6592011-10-15 00:13:06 +00002409 value=GetImageProperty(image,"tiff:timestamp",exception);
cristy3ed852e2009-09-05 21:47:34 +00002410 if (value != (const char *) NULL)
2411 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
cristyd15e6592011-10-15 00:13:06 +00002412 value=GetImageProperty(image,"tiff:make",exception);
cristy3ed852e2009-09-05 21:47:34 +00002413 if (value != (const char *) NULL)
2414 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
cristyd15e6592011-10-15 00:13:06 +00002415 value=GetImageProperty(image,"tiff:model",exception);
cristy3ed852e2009-09-05 21:47:34 +00002416 if (value != (const char *) NULL)
2417 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristyd15e6592011-10-15 00:13:06 +00002418 value=GetImageProperty(image,"tiff:software",exception);
cristy15d8d212010-09-17 01:56:30 +00002419 if (value != (const char *) NULL)
2420 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristyd15e6592011-10-15 00:13:06 +00002421 value=GetImageProperty(image,"tiff:copyright",exception);
cristy3ed852e2009-09-05 21:47:34 +00002422 if (value != (const char *) NULL)
cristy98ca0f52011-10-08 23:19:10 +00002423 (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value);
cristyd15e6592011-10-15 00:13:06 +00002424 value=GetImageProperty(image,"kodak-33423",exception);
cristy3ed852e2009-09-05 21:47:34 +00002425 if (value != (const char *) NULL)
2426 (void) TIFFSetField(tiff,33423,value);
cristyd15e6592011-10-15 00:13:06 +00002427 value=GetImageProperty(image,"kodak-36867",exception);
cristy3ed852e2009-09-05 21:47:34 +00002428 if (value != (const char *) NULL)
2429 (void) TIFFSetField(tiff,36867,value);
cristyd15e6592011-10-15 00:13:06 +00002430 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00002431 if (value != (const char *) NULL)
2432 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
cristyd15e6592011-10-15 00:13:06 +00002433 value=GetImageProperty(image,"comment",exception);
cristy3ed852e2009-09-05 21:47:34 +00002434 if (value != (const char *) NULL)
2435 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2436}
2437
cristyd15e6592011-10-15 00:13:06 +00002438static void TIFFSetEXIFProperties(TIFF *tiff,Image *image,
2439 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002440{
2441#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2442 const char
2443 *value;
2444
cristybb503372010-05-27 20:51:26 +00002445 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002446 i;
2447
2448 uint32
2449 offset;
2450
2451 /*
2452 Write EXIF properties.
2453 */
2454 offset=0;
2455 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2456 for (i=0; exif_info[i].tag != 0; i++)
2457 {
cristyd15e6592011-10-15 00:13:06 +00002458 value=GetImageProperty(image,exif_info[i].property,exception);
cristy3ed852e2009-09-05 21:47:34 +00002459 if (value == (const char *) NULL)
2460 continue;
2461 switch (exif_info[i].type)
2462 {
2463 case TIFF_ASCII:
2464 {
2465 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2466 break;
2467 }
2468 case TIFF_SHORT:
2469 {
2470 uint16
2471 shorty;
2472
cristyf2f27272009-12-17 14:48:46 +00002473 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002474 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2475 break;
2476 }
2477 case TIFF_LONG:
2478 {
2479 uint16
cristybb503372010-05-27 20:51:26 +00002480 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002481
cristybb503372010-05-27 20:51:26 +00002482 ssize_ty=(uint16) StringToLong(value);
2483 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002484 break;
2485 }
2486 case TIFF_RATIONAL:
2487 case TIFF_SRATIONAL:
2488 {
2489 float
2490 rational;
2491
cristydbdd0e32011-11-04 23:29:40 +00002492 rational=StringToDouble(value,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002493 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2494 break;
2495 }
2496 default:
2497 break;
2498 }
2499 }
2500 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2501#else
2502 (void) tiff;
2503 (void) image;
2504#endif
2505}
2506
2507static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002508 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002509{
2510#if !defined(TIFFDefaultStripSize)
2511#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2512#endif
2513
2514 const char
2515 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002516 *option;
cristy3ed852e2009-09-05 21:47:34 +00002517
2518 CompressionType
2519 compression;
2520
cristy6b032822010-06-29 16:52:32 +00002521 EndianType
2522 endian_type;
2523
cristy3ed852e2009-09-05 21:47:34 +00002524 MagickBooleanType
2525 debug,
2526 status;
2527
2528 MagickOffsetType
2529 scene;
2530
2531 QuantumInfo
2532 *quantum_info;
2533
2534 QuantumType
2535 quantum_type;
2536
cristybb503372010-05-27 20:51:26 +00002537 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002538 i;
2539
2540 size_t
cristy5ecaba72011-02-18 02:05:32 +00002541 length,
2542 lsb_first;
cristy3ed852e2009-09-05 21:47:34 +00002543
cristyc6da28e2011-04-28 01:41:35 +00002544 ssize_t
2545 y;
2546
cristy3ed852e2009-09-05 21:47:34 +00002547 TIFF
2548 *tiff;
2549
2550 TIFFErrorHandler
2551 error_handler,
2552 warning_handler;
2553
2554 TIFFInfo
2555 tiff_info;
2556
2557 uint16
2558 bits_per_sample,
2559 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002560 endian,
cristy3ed852e2009-09-05 21:47:34 +00002561 photometric;
2562
2563 uint32
2564 rows_per_strip;
2565
2566 unsigned char
2567 *pixels;
2568
cristy3ed852e2009-09-05 21:47:34 +00002569 /*
2570 Open TIFF file.
2571 */
2572 assert(image_info != (const ImageInfo *) NULL);
2573 assert(image_info->signature == MagickSignature);
2574 assert(image != (Image *) NULL);
2575 assert(image->signature == MagickSignature);
2576 if (image->debug != MagickFalse)
2577 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002578 assert(exception != (ExceptionInfo *) NULL);
2579 assert(exception->signature == MagickSignature);
2580 assert(exception != (ExceptionInfo *) NULL);
2581 assert(exception->signature == MagickSignature);
2582 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00002583 if (status == MagickFalse)
2584 return(status);
cristy3a37efd2011-08-28 20:31:03 +00002585 (void) MagickSetThreadValue(tiff_exception,exception);
cristy3ed852e2009-09-05 21:47:34 +00002586 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2587 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002588 endian_type=UndefinedEndian;
2589 option=GetImageOption(image_info,"tiff:endian");
2590 if (option != (const char *) NULL)
2591 {
2592 if (LocaleNCompare(option,"msb",3) == 0)
2593 endian_type=MSBEndian;
2594 if (LocaleNCompare(option,"lsb",3) == 0)
2595 endian_type=LSBEndian;;
2596 }
2597 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002598 {
2599 case LSBEndian: mode="wl"; break;
2600 case MSBEndian: mode="wb"; break;
2601 default: mode="w"; break;
2602 }
2603#if defined(TIFF_VERSION_BIG)
2604 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002605 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002606 {
2607 case LSBEndian: mode="wl8"; break;
2608 case MSBEndian: mode="wb8"; break;
2609 default: mode="w8"; break;
2610 }
2611#endif
2612 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2613 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2614 TIFFUnmapBlob);
2615 if (tiff == (TIFF *) NULL)
2616 {
2617 (void) TIFFSetWarningHandler(warning_handler);
2618 (void) TIFFSetErrorHandler(error_handler);
2619 return(MagickFalse);
2620 }
2621 scene=0;
2622 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +00002623 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +00002624 do
2625 {
2626 /*
2627 Initialize TIFF fields.
2628 */
cristy5f1c1ff2010-12-23 21:38:06 +00002629 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002630 (image_info->type != OptimizeType))
cristy018f07f2011-09-04 21:15:19 +00002631 (void) SetImageType(image,image_info->type,exception);
cristy3ed852e2009-09-05 21:47:34 +00002632 quantum_info=AcquireQuantumInfo(image_info,image);
2633 if (quantum_info == (QuantumInfo *) NULL)
2634 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2635 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2636 (quantum_info->format == UndefinedQuantumFormat) &&
cristy3a37efd2011-08-28 20:31:03 +00002637 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002638 {
2639 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2640 if (status == MagickFalse)
2641 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2642 }
2643 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2644 (GetPreviousImageInList(image) != (Image *) NULL))
2645 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2646 if ((image->columns != (uint32) image->columns) ||
2647 (image->rows != (uint32) image->rows))
2648 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2649 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2650 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2651 compression=image->compression;
2652 if (image_info->compression != UndefinedCompression)
2653 compression=image_info->compression;
2654 switch (compression)
2655 {
2656 case FaxCompression:
2657 {
2658 compress_tag=COMPRESSION_CCITTFAX3;
2659 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2660 break;
2661 }
2662 case Group4Compression:
2663 {
2664 compress_tag=COMPRESSION_CCITTFAX4;
2665 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2666 break;
2667 }
cristy6d5e20f2011-04-25 13:48:54 +00002668#if defined(COMPRESSION_JBIG)
2669 case JBIG1Compression:
2670 {
2671 compress_tag=COMPRESSION_JBIG;
2672 break;
2673 }
2674#endif
cristy3ed852e2009-09-05 21:47:34 +00002675 case JPEGCompression:
2676 {
2677 compress_tag=COMPRESSION_JPEG;
2678 break;
2679 }
cristyfbb0ef02010-12-19 02:32:11 +00002680#if defined(COMPRESSION_LZMA)
2681 case LZMACompression:
2682 {
2683 compress_tag=COMPRESSION_LZMA;
2684 break;
2685 }
2686#endif
cristy3ed852e2009-09-05 21:47:34 +00002687 case LZWCompression:
2688 {
2689 compress_tag=COMPRESSION_LZW;
2690 break;
2691 }
2692 case RLECompression:
2693 {
2694 compress_tag=COMPRESSION_PACKBITS;
2695 break;
2696 }
2697 case ZipCompression:
2698 {
2699 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2700 break;
2701 }
2702 case NoCompression:
2703 default:
2704 {
2705 compress_tag=COMPRESSION_NONE;
2706 break;
2707 }
2708 }
cristy5b675872009-10-21 13:12:00 +00002709#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2710 if ((compress_tag != COMPRESSION_NONE) &&
2711 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002712 {
cristy3a37efd2011-08-28 20:31:03 +00002713 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2714 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002715 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002716 compress_tag=COMPRESSION_NONE;
2717 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002718 }
2719#else
2720 switch (compress_tag)
2721 {
2722#if defined(CCITT_SUPPORT)
2723 case COMPRESSION_CCITTFAX3:
2724 case COMPRESSION_CCITTFAX4:
2725#endif
2726#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2727 case COMPRESSION_JPEG:
2728#endif
cristyfbb0ef02010-12-19 02:32:11 +00002729#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2730 case COMPRESSION_LZMA:
2731#endif
cristy3ed852e2009-09-05 21:47:34 +00002732#if defined(LZW_SUPPORT)
2733 case COMPRESSION_LZW:
2734#endif
2735#if defined(PACKBITS_SUPPORT)
2736 case COMPRESSION_PACKBITS:
2737#endif
2738#if defined(ZIP_SUPPORT)
2739 case COMPRESSION_ADOBE_DEFLATE:
2740#endif
2741 case COMPRESSION_NONE:
2742 break;
2743 default:
2744 {
cristy3a37efd2011-08-28 20:31:03 +00002745 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2746 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002747 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002748 compress_tag=COMPRESSION_NONE;
2749 compression=NoCompression;
2750 break;
2751 }
2752 }
2753#endif
2754 switch (compression)
2755 {
2756 case FaxCompression:
2757 case Group4Compression:
2758 {
cristy018f07f2011-09-04 21:15:19 +00002759 (void) SetImageType(image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +00002760 break;
2761 }
2762 case JPEGCompression:
2763 {
cristy3a37efd2011-08-28 20:31:03 +00002764 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a11cb12011-10-19 23:53:34 +00002765 (void) SetImageDepth(image,8,exception);
cristy3ed852e2009-09-05 21:47:34 +00002766 break;
2767 }
2768 default:
2769 break;
2770 }
cristy3ed852e2009-09-05 21:47:34 +00002771 if (image->colorspace == CMYKColorspace)
2772 {
2773 photometric=PHOTOMETRIC_SEPARATED;
2774 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2775 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2776 }
2777 else
2778 {
2779 /*
2780 Full color TIFF raster.
2781 */
2782 if (image->colorspace == LabColorspace)
2783 photometric=PHOTOMETRIC_CIELAB;
2784 else
2785 if (image->colorspace == YCbCrColorspace)
2786 {
2787 photometric=PHOTOMETRIC_YCBCR;
2788 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy3a37efd2011-08-28 20:31:03 +00002789 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a11cb12011-10-19 23:53:34 +00002790 (void) SetImageDepth(image,8,exception);
cristy3ed852e2009-09-05 21:47:34 +00002791 }
2792 else
2793 {
cristy510d06a2011-07-06 23:43:54 +00002794 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +00002795 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00002796 photometric=PHOTOMETRIC_RGB;
2797 }
2798 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2799 if ((image_info->type != TrueColorType) &&
2800 (image_info->type != TrueColorMatteType))
2801 {
cristy94c8fe42009-10-06 01:57:36 +00002802 if ((image_info->type != PaletteType) &&
cristy3a37efd2011-08-28 20:31:03 +00002803 (IsImageGray(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002804 {
cristy94c8fe42009-10-06 01:57:36 +00002805 photometric=(uint16) (quantum_info->min_is_white !=
2806 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2807 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002808 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002809 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy3a37efd2011-08-28 20:31:03 +00002810 (IsImageMonochrome(image,exception) != MagickFalse))
cristy94c8fe42009-10-06 01:57:36 +00002811 {
2812 status=SetQuantumDepth(image,quantum_info,1);
2813 if (status == MagickFalse)
2814 ThrowWriterException(ResourceLimitError,
2815 "MemoryAllocationFailed");
2816 }
cristy3ed852e2009-09-05 21:47:34 +00002817 }
2818 else
cristy94c8fe42009-10-06 01:57:36 +00002819 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002820 {
cristybb503372010-05-27 20:51:26 +00002821 size_t
cristy94c8fe42009-10-06 01:57:36 +00002822 depth;
2823
2824 /*
2825 Colormapped TIFF raster.
2826 */
cristy3ed852e2009-09-05 21:47:34 +00002827 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002828 photometric=PHOTOMETRIC_PALETTE;
2829 depth=1;
2830 while ((GetQuantumRange(depth)+1) < image->colors)
2831 depth<<=1;
2832 status=SetQuantumDepth(image,quantum_info,depth);
2833 if (status == MagickFalse)
2834 ThrowWriterException(ResourceLimitError,
2835 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002836 }
2837 }
2838 }
cristyf2687ca2010-06-29 16:32:38 +00002839 switch (image->endian)
2840 {
2841 case LSBEndian:
2842 {
2843 endian=FILLORDER_LSB2MSB;
2844 break;
2845 }
2846 case MSBEndian:
2847 {
2848 endian=FILLORDER_MSB2LSB;
2849 break;
2850 }
2851 case UndefinedEndian:
2852 default:
2853 {
2854 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2855 break;
2856 }
2857 }
2858 lsb_first=1;
2859 image->endian=MSBEndian;
2860 if ((int) (*(char *) &lsb_first) != 0)
2861 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002862 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2863 (photometric != PHOTOMETRIC_MINISWHITE))
2864 {
2865 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002866 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002867 }
cristy0accf6e2009-10-01 13:14:28 +00002868 else
cristydaff8092010-04-22 14:50:53 +00002869 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2870 (photometric != PHOTOMETRIC_MINISWHITE))
2871 {
2872 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002873 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002874 }
cristyd15e6592011-10-15 00:13:06 +00002875 option=GetImageProperty(image,"tiff:fill-order",exception);
cristy62e282b2010-06-29 01:27:13 +00002876 if (option != (const char *) NULL)
2877 {
2878 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002879 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002880 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002881 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002882 }
cristyf2687ca2010-06-29 16:32:38 +00002883 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2884 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002885 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2886 if (image->matte != MagickFalse)
2887 {
2888 uint16
2889 extra_samples,
2890 sample_info[1],
2891 samples_per_pixel;
2892
2893 /*
2894 TIFF has a matte channel.
2895 */
2896 extra_samples=1;
2897 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
cristyd15e6592011-10-15 00:13:06 +00002898 option=GetImageProperty(image,"tiff:alpha",exception);
cristy3ed852e2009-09-05 21:47:34 +00002899 if ((option != (const char *) NULL) &&
2900 (LocaleCompare(option,"associated") == 0))
2901 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2902 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2903 &samples_per_pixel);
2904 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2905 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2906 &sample_info);
2907 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2908 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2909 }
2910 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2911 switch (quantum_info->format)
2912 {
2913 case FloatingPointQuantumFormat:
2914 {
2915 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2916 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2917 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2918 break;
2919 }
2920 case SignedQuantumFormat:
2921 {
2922 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2923 break;
2924 }
2925 case UnsignedQuantumFormat:
2926 {
2927 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2928 break;
2929 }
2930 default:
2931 break;
2932 }
cristy3ed852e2009-09-05 21:47:34 +00002933 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2934 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2935 if (photometric == PHOTOMETRIC_RGB)
2936 if ((image_info->interlace == PlaneInterlace) ||
2937 (image_info->interlace == PartitionInterlace))
2938 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2939 rows_per_strip=1;
2940 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002941 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2942 1);
cristy3ed852e2009-09-05 21:47:34 +00002943 option=GetImageOption(image_info,"tiff:rows-per-strip");
2944 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002945 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002946 switch (compress_tag)
2947 {
2948 case COMPRESSION_JPEG:
2949 {
2950#if defined(JPEG_SUPPORT)
2951 const char
2952 *sampling_factor;
2953
2954 GeometryInfo
2955 geometry_info;
2956
2957 MagickStatusType
2958 flags;
2959
2960 rows_per_strip+=(16-(rows_per_strip % 16));
2961 if (image->quality != 0)
2962 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2963 if (image_info->quality != UndefinedCompressionQuality)
2964 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2965 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
cristy510d06a2011-07-06 23:43:54 +00002966 if (IsRGBColorspace(image->colorspace) == MagickTrue)
cristy3ed852e2009-09-05 21:47:34 +00002967 {
cristy949bf5b2010-05-08 02:47:03 +00002968 const char
2969 *value;
2970
cristy3ed852e2009-09-05 21:47:34 +00002971 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2972 sampling_factor=(const char *) NULL;
cristyd15e6592011-10-15 00:13:06 +00002973 value=GetImageProperty(image,"jpeg:sampling-factor",exception);
cristy3ed852e2009-09-05 21:47:34 +00002974 if (value != (char *) NULL)
2975 {
2976 sampling_factor=value;
2977 if (image->debug != MagickFalse)
2978 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2979 " Input sampling-factors=%s",sampling_factor);
2980 }
2981 if (image_info->sampling_factor != (char *) NULL)
2982 sampling_factor=image_info->sampling_factor;
2983 if (sampling_factor != (const char *) NULL)
2984 {
2985 flags=ParseGeometry(sampling_factor,&geometry_info);
2986 if ((flags & SigmaValue) == 0)
2987 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002988 if (image->colorspace == YCbCrColorspace)
2989 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2990 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00002991 }
2992 }
2993 if (bits_per_sample == 12)
2994 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2995#endif
2996 break;
2997 }
2998 case COMPRESSION_ADOBE_DEFLATE:
2999 {
cristyf6fe0a12010-05-30 00:44:47 +00003000 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003001 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3002 &bits_per_sample);
3003 if (((photometric == PHOTOMETRIC_RGB) ||
3004 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3005 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00003006 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00003007 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
3008 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00003009 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00003010 break;
3011 }
3012 case COMPRESSION_CCITTFAX3:
3013 {
3014 /*
3015 Byte-aligned EOL.
3016 */
cristyeaedf062010-05-29 22:36:02 +00003017 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003018 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
3019 break;
3020 }
3021 case COMPRESSION_CCITTFAX4:
3022 {
cristyeaedf062010-05-29 22:36:02 +00003023 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003024 break;
3025 }
cristyef8f26b2010-12-19 20:29:16 +00003026#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3027 case COMPRESSION_LZMA:
3028 {
3029 if (((photometric == PHOTOMETRIC_RGB) ||
3030 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3031 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3032 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3033 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
3034 image_info->quality == UndefinedCompressionQuality ? 7 :
3035 MagickMin((ssize_t) image_info->quality/10,9)));
3036 break;
3037 }
3038#endif
cristy3ed852e2009-09-05 21:47:34 +00003039 case COMPRESSION_LZW:
3040 {
3041 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3042 &bits_per_sample);
3043 if (((photometric == PHOTOMETRIC_RGB) ||
3044 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3045 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00003046 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00003047 break;
3048 }
3049 default:
3050 break;
3051 }
cristy79e5dad2010-09-16 19:48:33 +00003052 option=GetImageOption(image_info,"tiff:tile-geometry");
3053 if (option == (const char *) NULL)
3054 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy2a11bef2011-10-28 18:33:11 +00003055 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
cristy3ed852e2009-09-05 21:47:34 +00003056 {
3057 unsigned short
3058 units;
3059
3060 /*
3061 Set image resolution.
3062 */
3063 units=RESUNIT_NONE;
3064 if (image->units == PixelsPerInchResolution)
3065 units=RESUNIT_INCH;
3066 if (image->units == PixelsPerCentimeterResolution)
3067 units=RESUNIT_CENTIMETER;
3068 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
cristy2a11bef2011-10-28 18:33:11 +00003069 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x);
3070 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00003071 if ((image->page.x != 0) || (image->page.y != 0))
3072 {
3073 /*
3074 Set image position.
3075 */
3076 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
cristy2a11bef2011-10-28 18:33:11 +00003077 image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00003078 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
cristy2a11bef2011-10-28 18:33:11 +00003079 image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00003080 }
3081 }
3082 if (image->chromaticity.white_point.x != 0.0)
3083 {
3084 float
3085 chromaticity[6];
3086
3087 /*
3088 Set image chromaticity.
3089 */
3090 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3091 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3092 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3093 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3094 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3095 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3096 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3097 chromaticity[0]=(float) image->chromaticity.white_point.x;
3098 chromaticity[1]=(float) image->chromaticity.white_point.y;
3099 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3100 }
3101 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3102 {
3103 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3104 if (image->scene != 0)
3105 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3106 GetImageListLength(image));
3107 }
3108 if (image->orientation != UndefinedOrientation)
3109 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3110 (void) TIFFSetProfiles(tiff,image);
3111 {
3112 uint16
3113 page,
3114 pages;
3115
3116 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00003117 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00003118 if ((image_info->adjoin != MagickFalse) && (pages > 1))
3119 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3120 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3121 }
cristyd15e6592011-10-15 00:13:06 +00003122 (void) TIFFSetProperties(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003123 if (0)
cristyd15e6592011-10-15 00:13:06 +00003124 (void) TIFFSetEXIFProperties(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003125 /*
3126 Write image scanlines.
3127 */
3128 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3129 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3130 pixels=GetQuantumPixels(quantum_info);
3131 tiff_info.scanline=GetQuantumPixels(quantum_info);
3132 switch (photometric)
3133 {
3134 case PHOTOMETRIC_CIELAB:
3135 case PHOTOMETRIC_YCBCR:
3136 case PHOTOMETRIC_RGB:
3137 {
3138 /*
3139 RGB TIFF image.
3140 */
3141 switch (image_info->interlace)
3142 {
3143 case NoInterlace:
3144 default:
3145 {
3146 quantum_type=RGBQuantum;
3147 if (image->matte != MagickFalse)
3148 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003149 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003150 {
cristy4c08aed2011-07-01 19:47:50 +00003151 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003152 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003153
cristy3a37efd2011-08-28 20:31:03 +00003154 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003155 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003156 break;
cristy4c08aed2011-07-01 19:47:50 +00003157 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003158 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00003159 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00003160 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3161 break;
3162 if (image->previous == (Image *) NULL)
3163 {
cristy2837bcc2010-08-07 23:57:39 +00003164 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3165 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003166 if (status == MagickFalse)
3167 break;
3168 }
3169 }
3170 break;
3171 }
3172 case PlaneInterlace:
3173 case PartitionInterlace:
3174 {
3175 /*
3176 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3177 */
cristybb503372010-05-27 20:51:26 +00003178 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003179 {
cristy4c08aed2011-07-01 19:47:50 +00003180 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003181 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003182
cristy3a37efd2011-08-28 20:31:03 +00003183 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003184 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003185 break;
cristy4c08aed2011-07-01 19:47:50 +00003186 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003187 RedQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003188 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3189 break;
3190 }
3191 if (image->previous == (Image *) NULL)
3192 {
3193 status=SetImageProgress(image,SaveImageTag,100,400);
3194 if (status == MagickFalse)
3195 break;
3196 }
cristybb503372010-05-27 20:51:26 +00003197 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003198 {
cristy4c08aed2011-07-01 19:47:50 +00003199 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003200 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003201
cristy3a37efd2011-08-28 20:31:03 +00003202 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003203 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003204 break;
cristy4c08aed2011-07-01 19:47:50 +00003205 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003206 GreenQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003207 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3208 break;
3209 }
3210 if (image->previous == (Image *) NULL)
3211 {
3212 status=SetImageProgress(image,SaveImageTag,200,400);
3213 if (status == MagickFalse)
3214 break;
3215 }
cristybb503372010-05-27 20:51:26 +00003216 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003217 {
cristy4c08aed2011-07-01 19:47:50 +00003218 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003219 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003220
cristy3a37efd2011-08-28 20:31:03 +00003221 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003222 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003223 break;
cristy4c08aed2011-07-01 19:47:50 +00003224 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003225 BlueQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003226 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3227 break;
3228 }
3229 if (image->previous == (Image *) NULL)
3230 {
3231 status=SetImageProgress(image,SaveImageTag,300,400);
3232 if (status == MagickFalse)
3233 break;
3234 }
3235 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003236 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003237 {
cristy4c08aed2011-07-01 19:47:50 +00003238 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003239 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003240
cristy3a37efd2011-08-28 20:31:03 +00003241 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003242 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003243 break;
cristy4c08aed2011-07-01 19:47:50 +00003244 length=ExportQuantumPixels(image,(CacheView *) NULL,
cristy3a37efd2011-08-28 20:31:03 +00003245 quantum_info,AlphaQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003246 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3247 break;
3248 }
3249 if (image->previous == (Image *) NULL)
3250 {
3251 status=SetImageProgress(image,SaveImageTag,400,400);
3252 if (status == MagickFalse)
3253 break;
3254 }
3255 break;
3256 }
3257 }
3258 break;
3259 }
3260 case PHOTOMETRIC_SEPARATED:
3261 {
3262 /*
3263 CMYK TIFF image.
3264 */
3265 quantum_type=CMYKQuantum;
3266 if (image->matte != MagickFalse)
3267 quantum_type=CMYKAQuantum;
3268 if (image->colorspace != CMYKColorspace)
cristye941a752011-10-15 01:52:48 +00003269 (void) TransformImageColorspace(image,CMYKColorspace,exception);
cristybb503372010-05-27 20:51:26 +00003270 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003271 {
cristy4c08aed2011-07-01 19:47:50 +00003272 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003273 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003274
cristy3a37efd2011-08-28 20:31:03 +00003275 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003276 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003277 break;
cristy4c08aed2011-07-01 19:47:50 +00003278 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003279 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003280 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3281 break;
3282 if (image->previous == (Image *) NULL)
3283 {
cristycee97112010-05-28 00:44:52 +00003284 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3285 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003286 if (status == MagickFalse)
3287 break;
3288 }
3289 }
3290 break;
3291 }
3292 case PHOTOMETRIC_PALETTE:
3293 {
3294 uint16
3295 *blue,
3296 *green,
3297 *red;
3298
3299 /*
3300 Colormapped TIFF image.
3301 */
3302 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3303 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3304 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3305 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3306 (blue == (uint16 *) NULL))
3307 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3308 /*
3309 Initialize TIFF colormap.
3310 */
3311 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3312 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3313 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003314 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003315 {
3316 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3317 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3318 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3319 }
3320 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3321 red=(uint16 *) RelinquishMagickMemory(red);
3322 green=(uint16 *) RelinquishMagickMemory(green);
3323 blue=(uint16 *) RelinquishMagickMemory(blue);
3324 }
3325 default:
3326 {
3327 /*
3328 Convert PseudoClass packets to contiguous grayscale scanlines.
3329 */
3330 quantum_type=IndexQuantum;
3331 if (image->matte != MagickFalse)
3332 {
3333 if (photometric != PHOTOMETRIC_PALETTE)
3334 quantum_type=GrayAlphaQuantum;
3335 else
3336 quantum_type=IndexAlphaQuantum;
3337 }
3338 else
3339 if (photometric != PHOTOMETRIC_PALETTE)
3340 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003341 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003342 {
cristy4c08aed2011-07-01 19:47:50 +00003343 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003344 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003345
cristy3a37efd2011-08-28 20:31:03 +00003346 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003347 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003348 break;
cristy4c08aed2011-07-01 19:47:50 +00003349 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003350 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003351 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3352 break;
3353 if (image->previous == (Image *) NULL)
3354 {
cristycee97112010-05-28 00:44:52 +00003355 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3356 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003357 if (status == MagickFalse)
3358 break;
3359 }
3360 }
3361 break;
3362 }
3363 }
3364 quantum_info=DestroyQuantumInfo(quantum_info);
3365 DestroyTIFFInfo(&tiff_info);
3366 if (0 && (image_info->verbose == MagickTrue))
3367 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3368 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003369 image->endian=MSBEndian;
3370 if (endian == FILLORDER_LSB2MSB)
3371 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003372 image=SyncNextImageInList(image);
3373 if (image == (Image *) NULL)
3374 break;
3375 status=SetImageProgress(image,SaveImagesTag,scene++,
3376 GetImageListLength(image));
3377 if (status == MagickFalse)
3378 break;
3379 } while (image_info->adjoin != MagickFalse);
3380 (void) TIFFSetWarningHandler(warning_handler);
3381 (void) TIFFSetErrorHandler(error_handler);
3382 TIFFClose(tiff);
3383 return(MagickTrue);
3384}
3385#endif