blob: cdfcbb47b456398496e0ff7ec0b4e3e5a2d2b219 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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);
cristy0c823b62012-01-25 00:44:43 +0000633 offset=0;
cristy3ed852e2009-09-05 21:47:34 +0000634 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
635 return;
cristy7992e402011-09-16 15:24:22 +0000636 sans=NULL;
cristy3ed852e2009-09-05 21:47:34 +0000637 for (i=0; exif_info[i].tag != 0; i++)
638 {
639 *value='\0';
640 switch (exif_info[i].type)
641 {
642 case TIFF_ASCII:
643 {
644 char
645 *ascii;
646
cristy7992e402011-09-16 15:24:22 +0000647 ascii=(char *) NULL;
cristy0c823b62012-01-25 00:44:43 +0000648 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans,&sans) != 0) &&
cristy7992e402011-09-16 15:24:22 +0000649 (ascii != (char *) NULL) && (*ascii != '\0'))
cristy3ed852e2009-09-05 21:47:34 +0000650 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
651 break;
652 }
653 case TIFF_SHORT:
654 {
655 uint16
cristy7992e402011-09-16 15:24:22 +0000656 shorty[2] = { 0, 0};
cristy3ed852e2009-09-05 21:47:34 +0000657
cristy0c823b62012-01-25 00:44:43 +0000658 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans,&sans) != 0)
cristy7992e402011-09-16 15:24:22 +0000659 (void) FormatLocaleString(value,MaxTextExtent,"%d",(int) shorty[0]);
cristy3ed852e2009-09-05 21:47:34 +0000660 break;
661 }
662 case TIFF_LONG:
663 {
664 uint32
cristy7992e402011-09-16 15:24:22 +0000665 longy;
cristy3ed852e2009-09-05 21:47:34 +0000666
cristy0c823b62012-01-25 00:44:43 +0000667 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans,&sans) != 0)
cristy7992e402011-09-16 15:24:22 +0000668 (void) FormatLocaleString(value,MaxTextExtent,"%d",longy);
cristy3ed852e2009-09-05 21:47:34 +0000669 break;
670 }
cristyccf4e122011-10-29 00:29:19 +0000671#if defined(TIFF_VERSION_BIG)
672 case TIFF_LONG8:
673 {
674 uint64
675 longy;
676
cristy0c823b62012-01-25 00:44:43 +0000677 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans,&sans) != 0)
cristy02845b42011-11-11 17:22:13 +0000678 (void) FormatLocaleString(value,MaxTextExtent,"%lld",longy);
cristyccf4e122011-10-29 00:29:19 +0000679 break;
680 }
681#endif
cristy3ed852e2009-09-05 21:47:34 +0000682 case TIFF_RATIONAL:
683 case TIFF_SRATIONAL:
cristy7992e402011-09-16 15:24:22 +0000684 case TIFF_FLOAT:
685 case TIFF_DOUBLE:
cristy3ed852e2009-09-05 21:47:34 +0000686 {
687 float
cristy7992e402011-09-16 15:24:22 +0000688 rational[16];
cristy3ed852e2009-09-05 21:47:34 +0000689
cristy0c823b62012-01-25 00:44:43 +0000690 if (TIFFGetField(tiff,exif_info[i].tag,&rational,&sans,&sans) != 0)
cristy7992e402011-09-16 15:24:22 +0000691 (void) FormatLocaleString(value,MaxTextExtent,"%g",rational[0]);
cristy3ed852e2009-09-05 21:47:34 +0000692 break;
693 }
694 default:
695 break;
696 }
697 if (*value != '\0')
cristyd15e6592011-10-15 00:13:06 +0000698 (void) SetImageProperty(image,exif_info[i].property,value,exception);
cristy3ed852e2009-09-05 21:47:34 +0000699 }
700 TIFFSetDirectory(tiff,directory);
701#else
702 (void) tiff;
703 (void) image;
704#endif
705}
706
707static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
708{
709 *base=(tdata_t *) GetBlobStreamData((Image *) image);
710 if (*base != (tdata_t *) NULL)
711 *size=(toff_t) GetBlobSize((Image *) image);
712 if (*base != (tdata_t *) NULL)
713 return(1);
714 return(0);
715}
716
717static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
718{
719 tsize_t
720 count;
721
722 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
723 (unsigned char *) data);
724 return(count);
725}
726
cristybb503372010-05-27 20:51:26 +0000727static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
728 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000729{
730 int32
731 status;
732
733 (void) bits_per_sample;
734 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
735 return(status);
736}
737
738static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
739{
740 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
741}
742
cristy3ed852e2009-09-05 21:47:34 +0000743static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
744{
745 (void) image;
746 (void) base;
747 (void) size;
748}
749
750static void TIFFWarnings(const char *module,const char *format,va_list warning)
751{
752 char
753 message[MaxTextExtent];
754
755 ExceptionInfo
756 *exception;
757
758#if defined(MAGICKCORE_HAVE_VSNPRINTF)
759 (void) vsnprintf(message,MaxTextExtent,format,warning);
760#else
761 (void) vsprintf(message,format,warning);
762#endif
763 (void) ConcatenateMagickString(message,".",MaxTextExtent);
764 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
765 if (exception != (ExceptionInfo *) NULL)
766 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
767 message,"`%s'",module);
768}
769
770static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
771{
772 tsize_t
773 count;
774
775 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
776 (unsigned char *) data);
777 return(count);
778}
779
780#if defined(__cplusplus) || defined(c_plusplus)
781}
782#endif
783
784static Image *ReadTIFFImage(const ImageInfo *image_info,
785 ExceptionInfo *exception)
786{
787 typedef enum
788 {
789 ReadSingleSampleMethod,
790 ReadRGBAMethod,
791 ReadCMYKAMethod,
792 ReadStripMethod,
793 ReadTileMethod,
794 ReadGenericMethod
795 } TIFFMethodType;
796
797 const char
798 *option;
799
800 float
801 *chromaticity,
802 x_position,
803 y_position,
804 x_resolution,
805 y_resolution;
806
807 Image
808 *image;
809
cristy3ed852e2009-09-05 21:47:34 +0000810 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000811 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000812 debug,
813 status;
814
815 MagickSizeType
816 number_pixels;
817
818 QuantumInfo
819 *quantum_info;
820
821 QuantumType
822 quantum_type;
823
cristybb503372010-05-27 20:51:26 +0000824 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000825 i;
826
827 size_t
828 length,
829 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);
cristy3f590e52011-11-14 14:29:44 +0000922#if defined(MAGICKCORE_HAVE_TIFFISBIGENDIAN)
cristy6b032822010-06-29 16:52:32 +0000923 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
cristyd15e6592011-10-15 00:13:06 +0000924 "lsb" : "msb",exception);
cristy3f590e52011-11-14 14:29:44 +0000925#endif
cristy3ed852e2009-09-05 21:47:34 +0000926 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
927 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
928 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
929 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000930 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000931 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
932 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
933 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
cristyae1319c2011-01-19 20:18:53 +0000934 if (sample_format == SAMPLEFORMAT_IEEEFP)
cristyd15e6592011-10-15 00:13:06 +0000935 (void) SetImageProperty(image,"quantum:format","floating-point",
936 exception);
cristy3ed852e2009-09-05 21:47:34 +0000937 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
938 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
939 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
940 switch (photometric)
941 {
942 case PHOTOMETRIC_MINISBLACK:
943 {
cristyd15e6592011-10-15 00:13:06 +0000944 (void) SetImageProperty(image,"tiff:photometric","min-is-black",
945 exception);
cristy3ed852e2009-09-05 21:47:34 +0000946 break;
947 }
948 case PHOTOMETRIC_MINISWHITE:
949 {
cristyd15e6592011-10-15 00:13:06 +0000950 (void) SetImageProperty(image,"tiff:photometric","min-is-white",
951 exception);
cristy3ed852e2009-09-05 21:47:34 +0000952 break;
953 }
954 case PHOTOMETRIC_PALETTE:
955 {
cristyd15e6592011-10-15 00:13:06 +0000956 (void) SetImageProperty(image,"tiff:photometric","palette",exception);
cristy3ed852e2009-09-05 21:47:34 +0000957 break;
958 }
959 case PHOTOMETRIC_RGB:
960 {
cristyd15e6592011-10-15 00:13:06 +0000961 (void) SetImageProperty(image,"tiff:photometric","RGB",exception);
cristy3ed852e2009-09-05 21:47:34 +0000962 break;
963 }
964 case PHOTOMETRIC_CIELAB:
965 {
cristyd15e6592011-10-15 00:13:06 +0000966 (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception);
cristy3ed852e2009-09-05 21:47:34 +0000967 break;
968 }
969 case PHOTOMETRIC_SEPARATED:
970 {
cristyd15e6592011-10-15 00:13:06 +0000971 (void) SetImageProperty(image,"tiff:photometric","separated",exception);
cristy3ed852e2009-09-05 21:47:34 +0000972 break;
973 }
cristy79e5dad2010-09-16 19:48:33 +0000974 case PHOTOMETRIC_YCBCR:
975 {
cristyd15e6592011-10-15 00:13:06 +0000976 (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception);
cristy79e5dad2010-09-16 19:48:33 +0000977 break;
978 }
cristy3ed852e2009-09-05 21:47:34 +0000979 default:
980 {
cristyd15e6592011-10-15 00:13:06 +0000981 (void) SetImageProperty(image,"tiff:photometric","unknown",exception);
cristy3ed852e2009-09-05 21:47:34 +0000982 break;
983 }
984 }
985 if (image->debug != MagickFalse)
986 {
987 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
988 (unsigned int) width,(unsigned int) height);
989 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
990 interlace);
991 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
992 "Bits per sample: %u",bits_per_sample);
993 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
994 "Min sample value: %u",min_sample_value);
995 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
996 "Max sample value: %u",max_sample_value);
997 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
cristyd15e6592011-10-15 00:13:06 +0000998 "interpretation: %s",GetImageProperty(image,"tiff:photometric",
999 exception));
cristy3ed852e2009-09-05 21:47:34 +00001000 }
cristybb503372010-05-27 20:51:26 +00001001 image->columns=(size_t) width;
1002 image->rows=(size_t) height;
1003 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +00001004 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +00001005 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
1006 (double) image->depth);
cristyb241f382012-01-01 01:45:57 +00001007 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1008 (photometric == PHOTOMETRIC_MINISWHITE))
1009 image->colorspace=GRAYColorspace;
cristy3ed852e2009-09-05 21:47:34 +00001010 if (photometric == PHOTOMETRIC_SEPARATED)
1011 image->colorspace=CMYKColorspace;
1012 if (photometric == PHOTOMETRIC_CIELAB)
1013 image->colorspace=LabColorspace;
1014 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
1015 &samples_per_pixel);
1016 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
cristy2a11bef2011-10-28 18:33:11 +00001017 x_resolution=(float) image->resolution.x;
1018 y_resolution=(float) image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +00001019 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
1020 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
cristy2a11bef2011-10-28 18:33:11 +00001021 image->resolution.x=x_resolution;
1022 image->resolution.y=y_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001023 x_position=(float) image->page.x/x_resolution;
1024 y_position=(float) image->page.y/y_resolution;
1025 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
1026 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +00001027 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
1028 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001029 image->orientation=(OrientationType) orientation;
1030 chromaticity=(float *) NULL;
1031 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
1032 if (chromaticity != (float *) NULL)
1033 {
1034 image->chromaticity.white_point.x=chromaticity[0];
1035 image->chromaticity.white_point.y=chromaticity[1];
1036 }
1037 chromaticity=(float *) NULL;
1038 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
1039 if (chromaticity != (float *) NULL)
1040 {
1041 image->chromaticity.red_primary.x=chromaticity[0];
1042 image->chromaticity.red_primary.y=chromaticity[1];
1043 image->chromaticity.green_primary.x=chromaticity[2];
1044 image->chromaticity.green_primary.y=chromaticity[3];
1045 image->chromaticity.blue_primary.x=chromaticity[4];
1046 image->chromaticity.blue_primary.y=chromaticity[5];
1047 }
cristy3ed852e2009-09-05 21:47:34 +00001048#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1049 if ((compress_tag != COMPRESSION_NONE) &&
1050 (TIFFIsCODECConfigured(compress_tag) == 0))
1051 {
1052 TIFFClose(tiff);
1053 ThrowReaderException(CoderError,"CompressNotSupported");
1054 }
1055#endif
1056 switch (compress_tag)
1057 {
1058 case COMPRESSION_NONE: image->compression=NoCompression; break;
1059 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1060 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1061 case COMPRESSION_JPEG:
1062 {
1063 image->compression=JPEGCompression;
1064#if defined(JPEG_SUPPORT)
1065 {
1066 char
1067 sampling_factor[MaxTextExtent];
1068
1069 uint16
1070 horizontal,
1071 vertical;
1072
1073 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1074 &horizontal,&vertical);
cristyb51dff52011-05-19 16:55:47 +00001075 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
cristy3ed852e2009-09-05 21:47:34 +00001076 horizontal,vertical);
1077 (void) SetImageProperty(image,"jpeg:sampling-factor",
cristyd15e6592011-10-15 00:13:06 +00001078 sampling_factor,exception);
cristy3ed852e2009-09-05 21:47:34 +00001079 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1080 "Sampling Factors: %s",sampling_factor);
1081 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1082 {
1083 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1084 JPEGCOLORMODE_RGB);
1085 photometric=PHOTOMETRIC_RGB;
1086 }
1087 }
1088#endif
1089 break;
1090 }
1091 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001092#if defined(COMPRESSION_LZMA)
1093 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1094#endif
cristy3ed852e2009-09-05 21:47:34 +00001095 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1096 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1097 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1098 default: image->compression=RLECompression; break;
1099 }
cristy809fdbd2011-12-01 01:26:27 +00001100 TIFFGetProfiles(tiff,image,exception);
1101 TIFFGetProperties(tiff,image,exception);
cristyd93c30e2012-01-09 17:16:52 +00001102 option=GetImageOption(image_info,"tiff:exif-properties");
1103 if ((option == (const char *) NULL) ||
cristy9e8cb1c2012-01-24 23:47:47 +00001104 (IsMagickTrue(option) != MagickFalse))
cristyd93c30e2012-01-09 17:16:52 +00001105 TIFFGetEXIFProperties(tiff,image,exception);
cristy809fdbd2011-12-01 01:26:27 +00001106 /*
1107 Allocate memory for the image and pixel buffer.
1108 */
cristy3ed852e2009-09-05 21:47:34 +00001109 quantum_info=AcquireQuantumInfo(image_info,image);
1110 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001111 {
1112 TIFFClose(tiff);
1113 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1114 }
cristya4560912012-01-14 20:28:03 +00001115 quantum_info->endian=LSBEndian;
1116 if (endian == FILLORDER_LSB2MSB)
1117 quantum_info->endian=MSBEndian;
1118 image->endian=quantum_info->endian;
cristy3ed852e2009-09-05 21:47:34 +00001119 if (sample_format == SAMPLEFORMAT_UINT)
1120 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1121 if (sample_format == SAMPLEFORMAT_INT)
1122 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1123 if (sample_format == SAMPLEFORMAT_IEEEFP)
1124 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1125 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001126 {
cristya9a6ec42011-10-26 16:55:01 +00001127 quantum_info=DestroyQuantumInfo(quantum_info);
cristy8d137bf2010-04-21 23:52:04 +00001128 TIFFClose(tiff);
1129 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1130 }
cristy3ed852e2009-09-05 21:47:34 +00001131 status=MagickTrue;
1132 switch (photometric)
1133 {
1134 case PHOTOMETRIC_MINISBLACK:
1135 {
1136 quantum_info->min_is_white=MagickFalse;
1137 break;
1138 }
1139 case PHOTOMETRIC_MINISWHITE:
1140 {
1141 quantum_info->min_is_white=MagickTrue;
1142 break;
1143 }
1144 default:
1145 break;
1146 }
cristy891dc792010-03-04 01:47:16 +00001147 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001148 extra_samples=0;
1149 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1150 &sample_info);
1151 if (extra_samples == 0)
1152 {
1153 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1154 image->matte=MagickTrue;
1155 }
1156 else
cristy6c207342010-02-19 17:49:50 +00001157 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001158 {
cristy7cf69862010-03-03 13:57:24 +00001159 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001160 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001161 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001162 }
cristy891dc792010-03-04 01:47:16 +00001163 option=GetImageOption(image_info,"tiff:alpha");
1164 if (option != (const char *) NULL)
1165 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1166 MagickFalse;
1167 if (image->matte != MagickFalse)
1168 (void) SetImageProperty(image,"tiff:alpha",
cristyd15e6592011-10-15 00:13:06 +00001169 associated_alpha != MagickFalse ? "associated" : "unassociated",
1170 exception);
cristy3ed852e2009-09-05 21:47:34 +00001171 if ((photometric == PHOTOMETRIC_PALETTE) &&
1172 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1173 {
cristybb503372010-05-27 20:51:26 +00001174 size_t
cristy3ed852e2009-09-05 21:47:34 +00001175 colors;
1176
cristybb503372010-05-27 20:51:26 +00001177 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy018f07f2011-09-04 21:15:19 +00001178 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001179 {
1180 TIFFClose(tiff);
1181 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1182 }
1183 }
1184 if (units == RESUNIT_INCH)
1185 image->units=PixelsPerInchResolution;
1186 if (units == RESUNIT_CENTIMETER)
1187 image->units=PixelsPerCentimeterResolution;
1188 value=(unsigned short) image->scene;
1189 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1190 image->scene=value;
1191 if (image_info->ping != MagickFalse)
1192 {
1193 if (image_info->number_scenes != 0)
1194 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1195 break;
1196 goto next_tiff_frame;
1197 }
1198 method=ReadGenericMethod;
1199 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1200 {
1201 char
1202 value[MaxTextExtent];
1203
1204 method=ReadStripMethod;
cristyb51dff52011-05-19 16:55:47 +00001205 (void) FormatLocaleString(value,MaxTextExtent,"%u",
cristy3ed852e2009-09-05 21:47:34 +00001206 (unsigned int) rows_per_strip);
cristyd15e6592011-10-15 00:13:06 +00001207 (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception);
cristy3ed852e2009-09-05 21:47:34 +00001208 }
1209 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1210 method=ReadRGBAMethod;
1211 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1212 method=ReadCMYKAMethod;
1213 if ((photometric != PHOTOMETRIC_RGB) &&
1214 (photometric != PHOTOMETRIC_CIELAB) &&
1215 (photometric != PHOTOMETRIC_SEPARATED))
1216 method=ReadGenericMethod;
1217 if (image->storage_class == PseudoClass)
1218 method=ReadSingleSampleMethod;
1219 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1220 (photometric == PHOTOMETRIC_MINISWHITE))
1221 method=ReadSingleSampleMethod;
1222 if ((photometric != PHOTOMETRIC_SEPARATED) &&
cristybc736662011-03-09 13:39:42 +00001223 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
cristy3ed852e2009-09-05 21:47:34 +00001224 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001225 if (image->compression == JPEGCompression)
1226 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001227 if (TIFFIsTiled(tiff) != MagickFalse)
1228 method=ReadTileMethod;
1229 quantum_type=RGBQuantum;
1230 pixels=GetQuantumPixels(quantum_info);
1231 switch (method)
1232 {
1233 case ReadSingleSampleMethod:
1234 {
1235 /*
1236 Convert TIFF image to PseudoClass MIFF image.
1237 */
1238 if ((image->storage_class == PseudoClass) &&
1239 (photometric == PHOTOMETRIC_PALETTE))
1240 {
cristybb503372010-05-27 20:51:26 +00001241 size_t
cristy3ed852e2009-09-05 21:47:34 +00001242 range;
1243
1244 uint16
1245 *blue_colormap,
1246 *green_colormap,
1247 *red_colormap;
1248
1249 /*
1250 Initialize colormap.
1251 */
1252 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1253 &green_colormap,&blue_colormap);
1254 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001255 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001256 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1257 (blue_colormap[i] >= 256))
1258 {
1259 range=65535;
1260 break;
1261 }
cristybb503372010-05-27 20:51:26 +00001262 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001263 {
cristyce70c172010-01-07 17:15:30 +00001264 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001265 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001266 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001267 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001268 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001269 blue_colormap[i])/range);
1270 }
1271 }
1272 quantum_type=IndexQuantum;
1273 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1274 if (image->matte != MagickFalse)
1275 {
1276 if (image->storage_class != PseudoClass)
1277 {
1278 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1279 GrayAlphaQuantum;
1280 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1281 }
1282 else
1283 {
1284 quantum_type=IndexAlphaQuantum;
1285 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1286 }
1287 }
1288 else
1289 if (image->storage_class != PseudoClass)
1290 {
1291 quantum_type=GrayQuantum;
1292 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1293 }
1294 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1295 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001296 {
1297 TIFFClose(tiff);
1298 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1299 }
cristy3ed852e2009-09-05 21:47:34 +00001300 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001301 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001302 {
1303 int
1304 status;
1305
cristy4c08aed2011-07-01 19:47:50 +00001306 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001307 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001308
1309 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1310 if (status == -1)
1311 break;
1312 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001313 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001314 break;
1315 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1316 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00001317 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00001318 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1319 break;
1320 if (image->previous == (Image *) NULL)
1321 {
cristycee97112010-05-28 00:44:52 +00001322 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1323 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001324 if (status == MagickFalse)
1325 break;
1326 }
1327 }
1328 break;
1329 }
1330 case ReadRGBAMethod:
1331 {
1332 /*
1333 Convert TIFF image to DirectClass MIFF image.
1334 */
1335 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1336 quantum_type=RGBQuantum;
1337 if (image->matte != MagickFalse)
1338 {
1339 quantum_type=RGBAQuantum;
1340 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1341 }
1342 if (image->colorspace == CMYKColorspace)
1343 {
1344 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1345 quantum_type=CMYKQuantum;
1346 if (image->matte != MagickFalse)
1347 {
1348 quantum_type=CMYKAQuantum;
1349 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1350 }
1351 }
1352 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1353 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001354 {
1355 TIFFClose(tiff);
1356 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1357 }
cristy3ed852e2009-09-05 21:47:34 +00001358 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001359 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001360 {
1361 int
1362 status;
1363
cristy4c08aed2011-07-01 19:47:50 +00001364 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001365 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001366
1367 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1368 if (status == -1)
1369 break;
1370 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001371 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001372 break;
cristy0b334892010-03-15 02:26:46 +00001373 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1374 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001375 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1376 break;
1377 if (image->previous == (Image *) NULL)
1378 {
cristycee97112010-05-28 00:44:52 +00001379 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1380 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001381 if (status == MagickFalse)
1382 break;
1383 }
1384 }
1385 break;
1386 }
1387 case ReadCMYKAMethod:
1388 {
1389 /*
1390 Convert TIFF image to DirectClass MIFF image.
1391 */
cristybb503372010-05-27 20:51:26 +00001392 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001393 {
cristybb503372010-05-27 20:51:26 +00001394 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001395 {
cristy4c08aed2011-07-01 19:47:50 +00001396 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001397 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001398
cristy3ed852e2009-09-05 21:47:34 +00001399 int
1400 status;
1401
1402 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1403 pixels);
1404 if (status == -1)
1405 break;
1406 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001407 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001408 break;
1409 if (image->colorspace != CMYKColorspace)
1410 switch (i)
1411 {
1412 case 0: quantum_type=RedQuantum; break;
1413 case 1: quantum_type=GreenQuantum; break;
1414 case 2: quantum_type=BlueQuantum; break;
1415 case 3: quantum_type=AlphaQuantum; break;
1416 default: quantum_type=UndefinedQuantum; break;
1417 }
cristy6d9f12f2009-11-03 14:50:26 +00001418 else
cristy3ed852e2009-09-05 21:47:34 +00001419 switch (i)
1420 {
1421 case 0: quantum_type=CyanQuantum; break;
1422 case 1: quantum_type=MagentaQuantum; break;
1423 case 2: quantum_type=YellowQuantum; break;
1424 case 3: quantum_type=BlackQuantum; break;
1425 case 4: quantum_type=AlphaQuantum; break;
1426 default: quantum_type=UndefinedQuantum; break;
1427 }
1428 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1429 quantum_type,pixels,exception);
1430 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1431 break;
1432 }
1433 if (image->previous == (Image *) NULL)
1434 {
cristycee97112010-05-28 00:44:52 +00001435 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1436 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001437 if (status == MagickFalse)
1438 break;
1439 }
1440 }
1441 break;
1442 }
1443 case ReadStripMethod:
1444 {
1445 register unsigned char
1446 *p;
1447
1448 /*
1449 Convert stripped TIFF image to DirectClass MIFF image.
1450 */
1451 i=0;
1452 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001453 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001454 {
cristybb503372010-05-27 20:51:26 +00001455 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001456 x;
1457
cristy4c08aed2011-07-01 19:47:50 +00001458 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001459 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001460
1461 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001462 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001463 break;
1464 if (i == 0)
1465 {
1466 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1467 break;
cristyeaedf062010-05-29 22:36:02 +00001468 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1469 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001470 }
1471 i--;
cristy7f5d1662011-04-14 12:43:28 +00001472 p=(unsigned char *) (((uint32 *) pixels)+image->columns*i);
cristybb503372010-05-27 20:51:26 +00001473 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001474 {
cristy4c08aed2011-07-01 19:47:50 +00001475 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1476 (TIFFGetR(*p))),q);
1477 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1478 (TIFFGetG(*p))),q);
1479 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1480 (TIFFGetB(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001481 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001482 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1483 (TIFFGetA(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001484 p++;
cristyed231572011-07-14 02:18:59 +00001485 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001486 }
1487 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1488 break;
1489 if (image->previous == (Image *) NULL)
1490 {
cristycee97112010-05-28 00:44:52 +00001491 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1492 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001493 if (status == MagickFalse)
1494 break;
1495 }
1496 }
1497 break;
1498 }
1499 case ReadTileMethod:
1500 {
1501 register uint32
1502 *p;
1503
1504 uint32
1505 *tile_pixels,
1506 columns,
1507 rows;
1508
cristybb503372010-05-27 20:51:26 +00001509 size_t
cristy3ed852e2009-09-05 21:47:34 +00001510 number_pixels;
1511
1512 /*
1513 Convert tiled TIFF image to DirectClass MIFF image.
1514 */
1515 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1516 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1517 {
1518 TIFFClose(tiff);
1519 ThrowReaderException(CoderError,"ImageIsNotTiled");
1520 }
cristyc82a27b2011-10-21 01:07:16 +00001521 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +00001522 number_pixels=columns*rows;
cristyda16f162011-02-19 23:52:17 +00001523 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
cristy3ed852e2009-09-05 21:47:34 +00001524 sizeof(*tile_pixels));
1525 if (tile_pixels == (uint32 *) NULL)
1526 {
1527 TIFFClose(tiff);
1528 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1529 }
cristybb503372010-05-27 20:51:26 +00001530 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001531 {
cristybb503372010-05-27 20:51:26 +00001532 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001533 x;
1534
cristy4c08aed2011-07-01 19:47:50 +00001535 register Quantum
1536 *restrict q,
1537 *restrict tile;
cristy3ed852e2009-09-05 21:47:34 +00001538
cristybb503372010-05-27 20:51:26 +00001539 size_t
cristy3ed852e2009-09-05 21:47:34 +00001540 columns_remaining,
1541 rows_remaining;
1542
1543 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001544 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001545 rows_remaining=rows;
1546 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1547 exception);
cristy4c08aed2011-07-01 19:47:50 +00001548 if (tile == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001549 break;
cristybb503372010-05-27 20:51:26 +00001550 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001551 {
cristybb503372010-05-27 20:51:26 +00001552 size_t
cristy3ed852e2009-09-05 21:47:34 +00001553 column,
1554 row;
1555
1556 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1557 break;
1558 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001559 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001560 columns_remaining=columns;
1561 p=tile_pixels+(rows-rows_remaining)*columns;
cristy68473222011-08-17 17:39:19 +00001562 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1563 x);
cristy3ed852e2009-09-05 21:47:34 +00001564 for (row=rows_remaining; row > 0; row--)
1565 {
1566 if (image->matte != MagickFalse)
1567 for (column=columns_remaining; column > 0; column--)
1568 {
cristy4c08aed2011-07-01 19:47:50 +00001569 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1570 TIFFGetR(*p)),q);
1571 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1572 TIFFGetG(*p)),q);
1573 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1574 TIFFGetB(*p)),q);
1575 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1576 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001577 p++;
cristyed231572011-07-14 02:18:59 +00001578 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001579 }
1580 else
1581 for (column=columns_remaining; column > 0; column--)
1582 {
cristy4c08aed2011-07-01 19:47:50 +00001583 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1584 TIFFGetR(*p)),q);
1585 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1586 TIFFGetG(*p)),q);
1587 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1588 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001589 p++;
cristyed231572011-07-14 02:18:59 +00001590 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001591 }
1592 p+=columns-columns_remaining;
cristyed231572011-07-14 02:18:59 +00001593 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
cristy3ed852e2009-09-05 21:47:34 +00001594 }
1595 }
1596 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1597 break;
1598 if (image->previous == (Image *) NULL)
1599 {
cristycee97112010-05-28 00:44:52 +00001600 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1601 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001602 if (status == MagickFalse)
1603 break;
1604 }
1605 }
1606 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1607 break;
1608 }
1609 case ReadGenericMethod:
1610 default:
1611 {
1612 register uint32
1613 *p;
1614
1615 uint32
1616 *pixels;
1617
1618 /*
1619 Convert TIFF image to DirectClass MIFF image.
1620 */
1621 number_pixels=(MagickSizeType) image->columns*image->rows;
1622 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1623 (number_pixels*sizeof(uint32))))
1624 {
1625 TIFFClose(tiff);
1626 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1627 }
1628 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1629 sizeof(uint32));
1630 if (pixels == (uint32 *) NULL)
1631 {
1632 TIFFClose(tiff);
1633 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1634 }
1635 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1636 (uint32) image->rows,(uint32 *) pixels,0);
1637 /*
1638 Convert image to DirectClass pixel packets.
1639 */
1640 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001641 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001642 {
cristybb503372010-05-27 20:51:26 +00001643 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001644 x;
1645
cristy4c08aed2011-07-01 19:47:50 +00001646 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001647 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001648
1649 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001650 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001651 break;
cristyed231572011-07-14 02:18:59 +00001652 q+=GetPixelChannels(image)*(image->columns-1);
cristybb503372010-05-27 20:51:26 +00001653 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001654 {
cristy4c08aed2011-07-01 19:47:50 +00001655 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1656 TIFFGetR(*p)),q);
1657 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1658 TIFFGetG(*p)),q);
1659 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1660 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001661 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001662 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1663 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001664 p--;
cristyed231572011-07-14 02:18:59 +00001665 q-=GetPixelChannels(image);;
cristy3ed852e2009-09-05 21:47:34 +00001666 }
1667 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1668 break;
1669 if (image->previous == (Image *) NULL)
1670 {
cristycee97112010-05-28 00:44:52 +00001671 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1672 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001673 if (status == MagickFalse)
1674 break;
1675 }
1676 }
1677 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1678 break;
1679 }
1680 }
1681 SetQuantumImageType(image,quantum_type);
1682 next_tiff_frame:
cristya9a6ec42011-10-26 16:55:01 +00001683 quantum_info=DestroyQuantumInfo(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +00001684 if ((photometric == PHOTOMETRIC_LOGL) ||
1685 (photometric == PHOTOMETRIC_MINISBLACK) ||
1686 (photometric == PHOTOMETRIC_MINISWHITE))
1687 {
1688 image->type=GrayscaleType;
1689 if (bits_per_sample == 1)
1690 image->type=BilevelType;
1691 }
1692 if (image->storage_class == PseudoClass)
1693 image->depth=GetImageDepth(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001694 if ((photometric == PHOTOMETRIC_LOGL) ||
1695 (photometric == PHOTOMETRIC_MINISBLACK) ||
1696 (photometric == PHOTOMETRIC_MINISWHITE))
1697 {
1698 image->type=GrayscaleType;
1699 if (bits_per_sample == 1)
1700 image->type=BilevelType;
1701 }
1702 /*
1703 Proceed to next image.
1704 */
1705 if (image_info->number_scenes != 0)
1706 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1707 break;
1708 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1709 if (status == MagickTrue)
1710 {
1711 /*
1712 Allocate next image structure.
1713 */
cristy9950d572011-10-01 18:22:35 +00001714 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001715 if (GetNextImageInList(image) == (Image *) NULL)
1716 {
1717 image=DestroyImageList(image);
1718 return((Image *) NULL);
1719 }
1720 image=SyncNextImageInList(image);
1721 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1722 image->scene);
1723 if (status == MagickFalse)
1724 break;
1725 }
cristy3ed852e2009-09-05 21:47:34 +00001726 } while (status == MagickTrue);
1727 (void) TIFFSetWarningHandler(warning_handler);
1728 (void) TIFFSetErrorHandler(error_handler);
1729 TIFFClose(tiff);
1730 return(GetFirstImageInList(image));
1731}
1732#endif
1733
1734/*
1735%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1736% %
1737% %
1738% %
1739% R e g i s t e r T I F F I m a g e %
1740% %
1741% %
1742% %
1743%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1744%
1745% RegisterTIFFImage() adds properties for the TIFF image format to
1746% the list of supported formats. The properties include the image format
1747% tag, a method to read and/or write the format, whether the format
1748% supports the saving of more than one frame to the same file or blob,
1749% whether the format supports native in-memory I/O, and a brief
1750% description of the format.
1751%
1752% The format of the RegisterTIFFImage method is:
1753%
cristybb503372010-05-27 20:51:26 +00001754% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001755%
1756*/
cristy7e3eb0a2011-11-09 22:50:49 +00001757
1758#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
1759static TIFFExtendProc
cristy60e1bf92011-11-09 23:00:15 +00001760 tag_extender = (TIFFExtendProc) NULL;
cristy7e3eb0a2011-11-09 22:50:49 +00001761
1762static void TIFFTagExtender(TIFF *tiff)
1763{
1764 static const TIFFFieldInfo
1765 TIFFExtensions[] =
1766 {
cristy3fac9ec2011-11-17 18:04:39 +00001767 {
1768 37724, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
1769 (char *) "PhotoshopLayerData"
1770 }
cristy7e3eb0a2011-11-09 22:50:49 +00001771 };
1772
1773 TIFFMergeFieldInfo(tiff,TIFFExtensions,sizeof(TIFFExtensions)/
1774 sizeof(*TIFFExtensions));
cristy60e1bf92011-11-09 23:00:15 +00001775 if (tag_extender != (TIFFExtendProc) NULL)
1776 (*tag_extender)(tiff);
cristy7e3eb0a2011-11-09 22:50:49 +00001777}
1778#endif
1779
cristybb503372010-05-27 20:51:26 +00001780ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001781{
1782#define TIFFDescription "Tagged Image File Format"
1783
1784 char
1785 version[MaxTextExtent];
1786
1787 MagickInfo
1788 *entry;
cristy6b032822010-06-29 16:52:32 +00001789
cristy18b17442009-10-25 18:36:48 +00001790 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1791 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001792 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001793 if (instantiate_key == MagickFalse)
1794 {
1795 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1796 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristy7e3eb0a2011-11-09 22:50:49 +00001797#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
cristy60e1bf92011-11-09 23:00:15 +00001798 if (tag_extender == (TIFFExtendProc) NULL)
1799 tag_extender=TIFFSetTagExtender(TIFFTagExtender);
cristy7e3eb0a2011-11-09 22:50:49 +00001800#endif
cristy3ed852e2009-09-05 21:47:34 +00001801 instantiate_key=MagickTrue;
1802 }
cristyf84a1932010-01-03 18:00:18 +00001803 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001804 *version='\0';
1805#if defined(TIFF_VERSION)
cristyb51dff52011-05-19 16:55:47 +00001806 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
cristy3ed852e2009-09-05 21:47:34 +00001807#endif
1808#if defined(MAGICKCORE_TIFF_DELEGATE)
1809 {
1810 const char
1811 *p;
1812
cristybb503372010-05-27 20:51:26 +00001813 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001814 i;
1815
1816 p=TIFFGetVersion();
1817 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1818 version[i]=(*p++);
1819 version[i]='\0';
1820 }
1821#endif
1822
cristy3d7f8062009-09-24 20:45:35 +00001823 entry=SetMagickInfo("GROUP4");
1824#if defined(MAGICKCORE_TIFF_DELEGATE)
1825 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1826 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1827#endif
1828 entry->raw=MagickTrue;
1829 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001830 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001831 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001832 entry->seekable_stream=MagickTrue;
1833 entry->thread_support=NoThreadSupport;
1834 entry->description=ConstantString("Raw CCITT Group4");
1835 entry->module=ConstantString("TIFF");
1836 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001837 entry=SetMagickInfo("PTIF");
1838#if defined(MAGICKCORE_TIFF_DELEGATE)
1839 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1840 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1841#endif
1842 entry->endian_support=MagickTrue;
1843 entry->seekable_stream=MagickTrue;
1844 entry->thread_support=NoThreadSupport;
1845 entry->description=ConstantString("Pyramid encoded TIFF");
1846 entry->module=ConstantString("TIFF");
1847 (void) RegisterMagickInfo(entry);
1848 entry=SetMagickInfo("TIF");
1849#if defined(MAGICKCORE_TIFF_DELEGATE)
1850 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1851 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1852#endif
1853 entry->endian_support=MagickTrue;
1854 entry->seekable_stream=MagickTrue;
1855 entry->stealth=MagickTrue;
1856 entry->thread_support=NoThreadSupport;
1857 entry->description=ConstantString(TIFFDescription);
1858 if (*version != '\0')
1859 entry->version=ConstantString(version);
1860 entry->module=ConstantString("TIFF");
1861 (void) RegisterMagickInfo(entry);
1862 entry=SetMagickInfo("TIFF");
1863#if defined(MAGICKCORE_TIFF_DELEGATE)
1864 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1865 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1866#endif
1867 entry->magick=(IsImageFormatHandler *) IsTIFF;
1868 entry->endian_support=MagickTrue;
1869 entry->seekable_stream=MagickTrue;
1870 entry->thread_support=NoThreadSupport;
1871 entry->description=ConstantString(TIFFDescription);
1872 if (*version != '\0')
1873 entry->version=ConstantString(version);
1874 entry->module=ConstantString("TIFF");
1875 (void) RegisterMagickInfo(entry);
1876 entry=SetMagickInfo("TIFF64");
1877#if defined(TIFF_VERSION_BIG)
1878 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
cristy9d6964e2011-11-10 17:48:45 +00001879 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
cristy3ed852e2009-09-05 21:47:34 +00001880#endif
1881 entry->adjoin=MagickFalse;
1882 entry->endian_support=MagickTrue;
1883 entry->seekable_stream=MagickTrue;
1884 entry->thread_support=NoThreadSupport;
1885 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1886 if (*version != '\0')
1887 entry->version=ConstantString(version);
1888 entry->module=ConstantString("TIFF");
1889 (void) RegisterMagickInfo(entry);
1890 return(MagickImageCoderSignature);
1891}
1892
1893/*
1894%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1895% %
1896% %
1897% %
1898% U n r e g i s t e r T I F F I m a g e %
1899% %
1900% %
1901% %
1902%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1903%
1904% UnregisterTIFFImage() removes format registrations made by the TIFF module
1905% from the list of supported formats.
1906%
1907% The format of the UnregisterTIFFImage method is:
1908%
1909% UnregisterTIFFImage(void)
1910%
1911*/
1912ModuleExport void UnregisterTIFFImage(void)
1913{
cristy3ed852e2009-09-05 21:47:34 +00001914 (void) UnregisterMagickInfo("TIFF64");
cristy9d6964e2011-11-10 17:48:45 +00001915 (void) UnregisterMagickInfo("TIFF");
1916 (void) UnregisterMagickInfo("TIF");
1917 (void) UnregisterMagickInfo("PTIF");
cristy514e9e72009-11-20 02:12:08 +00001918 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1919 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001920 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001921 if (instantiate_key != MagickFalse)
cristy9ebdd932011-11-09 23:07:28 +00001922 {
1923#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
1924 if (tag_extender == (TIFFExtendProc) NULL)
1925 (void) TIFFSetTagExtender(tag_extender);
1926#endif
1927 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1928 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1929 instantiate_key=MagickFalse;
1930 }
cristyf84a1932010-01-03 18:00:18 +00001931 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001932 DestroySemaphoreInfo(&tiff_semaphore);
1933}
1934
1935#if defined(MAGICKCORE_TIFF_DELEGATE)
1936/*
1937%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1938% %
1939% %
1940% %
cristy3d7f8062009-09-24 20:45:35 +00001941% W r i t e G R O U P 4 I m a g e %
1942% %
1943% %
1944% %
1945%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1946%
1947% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1948%
1949% The format of the WriteGROUP4Image method is:
1950%
1951% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001952% Image *image,ExceptionInfo *)
cristy3d7f8062009-09-24 20:45:35 +00001953%
1954% A description of each parameter follows:
1955%
1956% o image_info: the image info.
1957%
1958% o image: The image.
1959%
cristy3a37efd2011-08-28 20:31:03 +00001960% o exception: return any errors or warnings in this structure.
1961%
cristy3d7f8062009-09-24 20:45:35 +00001962*/
1963static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001964 Image *image,ExceptionInfo *exception)
cristy3d7f8062009-09-24 20:45:35 +00001965{
1966 char
1967 filename[MaxTextExtent];
1968
1969 FILE
1970 *file;
1971
1972 Image
1973 *huffman_image;
1974
1975 ImageInfo
1976 *write_info;
1977
1978 int
1979 unique_file;
1980
1981 MagickBooleanType
1982 status;
1983
cristybb503372010-05-27 20:51:26 +00001984 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001985 i;
1986
1987 ssize_t
1988 count;
1989
1990 TIFF
1991 *tiff;
1992
cristy94c8fe42009-10-06 01:57:36 +00001993 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001994 *byte_count,
1995 strip_size;
1996
1997 unsigned char
1998 *buffer;
1999
2000 /*
2001 Write image as CCITT Group4 TIFF image to a temporary file.
2002 */
2003 assert(image_info != (const ImageInfo *) NULL);
2004 assert(image_info->signature == MagickSignature);
2005 assert(image != (Image *) NULL);
2006 assert(image->signature == MagickSignature);
2007 if (image->debug != MagickFalse)
2008 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002009 assert(exception != (ExceptionInfo *) NULL);
2010 assert(exception->signature == MagickSignature);
2011 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3d7f8062009-09-24 20:45:35 +00002012 if (status == MagickFalse)
2013 return(status);
cristy3a37efd2011-08-28 20:31:03 +00002014 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3d7f8062009-09-24 20:45:35 +00002015 if (huffman_image == (Image *) NULL)
2016 {
2017 (void) CloseBlob(image);
2018 return(MagickFalse);
2019 }
cristy94c8fe42009-10-06 01:57:36 +00002020 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00002021 file=(FILE *) NULL;
2022 unique_file=AcquireUniqueFileResource(filename);
2023 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00002024 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00002025 if ((unique_file == -1) || (file == (FILE *) NULL))
2026 {
cristy3a37efd2011-08-28 20:31:03 +00002027 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2028 filename);
cristy3d7f8062009-09-24 20:45:35 +00002029 return(MagickFalse);
2030 }
cristyb51dff52011-05-19 16:55:47 +00002031 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
cristy3d7f8062009-09-24 20:45:35 +00002032 filename);
cristy018f07f2011-09-04 21:15:19 +00002033 (void) SetImageType(huffman_image,BilevelType,exception);
cristy14efd992009-09-26 23:46:03 +00002034 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00002035 SetImageInfoFile(write_info,file);
2036 write_info->compression=Group4Compression;
2037 write_info->type=BilevelType;
2038 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
cristy3a37efd2011-08-28 20:31:03 +00002039 status=WriteTIFFImage(write_info,huffman_image,exception);
cristy3d7f8062009-09-24 20:45:35 +00002040 (void) fflush(file);
2041 write_info=DestroyImageInfo(write_info);
2042 if (status == MagickFalse)
2043 {
cristy3d7f8062009-09-24 20:45:35 +00002044 huffman_image=DestroyImage(huffman_image);
2045 (void) fclose(file);
2046 (void) RelinquishUniqueFileResource(filename);
2047 return(MagickFalse);
2048 }
2049 tiff=TIFFOpen(filename,"rb");
2050 if (tiff == (TIFF *) NULL)
2051 {
2052 huffman_image=DestroyImage(huffman_image);
2053 (void) fclose(file);
2054 (void) RelinquishUniqueFileResource(filename);
cristy3a37efd2011-08-28 20:31:03 +00002055 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
cristy3d7f8062009-09-24 20:45:35 +00002056 image_info->filename);
2057 return(MagickFalse);
2058 }
2059 /*
2060 Allocate raw strip buffer.
2061 */
cristy94c8fe42009-10-06 01:57:36 +00002062 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2063 {
2064 TIFFClose(tiff);
2065 huffman_image=DestroyImage(huffman_image);
2066 (void) fclose(file);
2067 (void) RelinquishUniqueFileResource(filename);
2068 return(MagickFalse);
2069 }
cristy3d7f8062009-09-24 20:45:35 +00002070 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00002071 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002072 if (byte_count[i] > strip_size)
2073 strip_size=byte_count[i];
2074 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2075 sizeof(*buffer));
2076 if (buffer == (unsigned char *) NULL)
2077 {
2078 TIFFClose(tiff);
2079 huffman_image=DestroyImage(huffman_image);
2080 (void) fclose(file);
2081 (void) RelinquishUniqueFileResource(filename);
2082 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2083 image_info->filename);
2084 }
2085 /*
2086 Compress runlength encoded to 2D Huffman pixels.
2087 */
cristybb503372010-05-27 20:51:26 +00002088 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002089 {
cristy94c8fe42009-10-06 01:57:36 +00002090 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00002091 if (WriteBlob(image,(size_t) count,buffer) != count)
2092 status=MagickFalse;
2093 }
2094 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2095 TIFFClose(tiff);
2096 huffman_image=DestroyImage(huffman_image);
2097 (void) fclose(file);
2098 (void) RelinquishUniqueFileResource(filename);
2099 (void) CloseBlob(image);
2100 return(status);
2101}
2102#endif
2103
2104#if defined(MAGICKCORE_TIFF_DELEGATE)
2105/*
2106%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2107% %
2108% %
2109% %
cristy3ed852e2009-09-05 21:47:34 +00002110% W r i t e P T I F I m a g e %
2111% %
2112% %
2113% %
2114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2115%
2116% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2117% format.
2118%
2119% The format of the WritePTIFImage method is:
2120%
2121% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002122% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002123%
2124% A description of each parameter follows:
2125%
2126% o image_info: the image info.
2127%
2128% o image: The image.
2129%
cristy3a37efd2011-08-28 20:31:03 +00002130% o exception: return any errors or warnings in this structure.
2131%
cristy3ed852e2009-09-05 21:47:34 +00002132*/
2133static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002134 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002135{
2136 Image
2137 *images,
2138 *next,
2139 *pyramid_image;
2140
2141 ImageInfo
2142 *write_info;
2143
2144 MagickBooleanType
2145 status;
2146
cristyf164a872012-01-20 18:26:34 +00002147 PointInfo
2148 resolution;
2149
cristybb503372010-05-27 20:51:26 +00002150 size_t
cristy3ed852e2009-09-05 21:47:34 +00002151 columns,
2152 rows;
2153
2154 /*
2155 Create pyramid-encoded TIFF image.
2156 */
2157 images=NewImageList();
2158 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2159 {
cristy3a37efd2011-08-28 20:31:03 +00002160 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,exception));
cristy3ed852e2009-09-05 21:47:34 +00002161 columns=next->columns;
2162 rows=next->rows;
cristyf164a872012-01-20 18:26:34 +00002163 resolution=next->resolution;
cristy3ed852e2009-09-05 21:47:34 +00002164 while ((columns > 64) && (rows > 64))
2165 {
2166 columns/=2;
2167 rows/=2;
cristyf164a872012-01-20 18:26:34 +00002168 resolution.x/=2;
2169 resolution.y/=2;
cristy98ca0f52011-10-08 23:19:10 +00002170 pyramid_image=ResizeImage(next,columns,rows,image->filter,image->blur,
cristy3a37efd2011-08-28 20:31:03 +00002171 exception);
cristyf164a872012-01-20 18:26:34 +00002172 if (pyramid_image == (Image *) NULL)
2173 break;
2174 pyramid_image->resolution=resolution;
cristy3ed852e2009-09-05 21:47:34 +00002175 AppendImageToList(&images,pyramid_image);
2176 }
2177 }
cristyf164a872012-01-20 18:26:34 +00002178 images=GetFirstImageInList(images);
cristy3ed852e2009-09-05 21:47:34 +00002179 /*
2180 Write pyramid-encoded TIFF image.
2181 */
cristy58567532009-09-26 01:13:32 +00002182 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002183 write_info->adjoin=MagickTrue;
cristyf164a872012-01-20 18:26:34 +00002184 (void) CopyMagickString(write_info->magick,"TIFF",MaxTextExtent);
2185 (void) CopyMagickString(images->magick,"TIFF",MaxTextExtent);
2186 status=WriteTIFFImage(write_info,images,exception);
cristy3ed852e2009-09-05 21:47:34 +00002187 images=DestroyImageList(images);
2188 write_info=DestroyImageInfo(write_info);
2189 return(status);
2190}
2191#endif
2192
2193#if defined(MAGICKCORE_TIFF_DELEGATE)
2194/*
2195%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2196% %
2197% %
2198% W r i t e T I F F I m a g e %
2199% %
2200% %
2201% %
2202%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2203%
2204% WriteTIFFImage() writes an image in the Tagged image file format.
2205%
2206% The format of the WriteTIFFImage method is:
2207%
2208% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002209% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002210%
2211% A description of each parameter follows:
2212%
2213% o image_info: the image info.
2214%
2215% o image: The image.
2216%
cristy3a37efd2011-08-28 20:31:03 +00002217% o exception: return any errors or warnings in this structure.
2218%
cristy3ed852e2009-09-05 21:47:34 +00002219*/
2220
2221typedef struct _TIFFInfo
2222{
2223 RectangleInfo
2224 tile_geometry;
2225
2226 unsigned char
2227 *scanline,
2228 *scanlines,
2229 *pixels;
2230} TIFFInfo;
2231
2232static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2233{
2234 assert(tiff_info != (TIFFInfo *) NULL);
2235 if (tiff_info->scanlines != (unsigned char *) NULL)
2236 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2237 tiff_info->scanlines);
2238 if (tiff_info->pixels != (unsigned char *) NULL)
2239 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2240 tiff_info->pixels);
2241}
2242
2243static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2244 TIFFInfo *tiff_info)
2245{
2246 const char
2247 *option;
2248
2249 MagickStatusType
2250 flags;
2251
cristy79e5dad2010-09-16 19:48:33 +00002252 uint32
2253 tile_columns,
2254 tile_rows;
2255
cristy3ed852e2009-09-05 21:47:34 +00002256 assert(tiff_info != (TIFFInfo *) NULL);
2257 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2258 option=GetImageOption(image_info,"tiff:tile-geometry");
2259 if (option == (const char *) NULL)
2260 return(MagickTrue);
2261 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2262 if ((flags & HeightValue) == 0)
2263 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002264 tile_columns=(uint32) tiff_info->tile_geometry.width;
2265 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002266 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2267 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2268 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2269 tiff_info->tile_geometry.width=tile_columns;
2270 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002271 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002272 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002273 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002274 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002275 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2276 (tiff_info->pixels == (unsigned char *) NULL))
2277 {
2278 DestroyTIFFInfo(tiff_info);
2279 return(MagickFalse);
2280 }
2281 return(MagickTrue);
2282}
2283
cristybb503372010-05-27 20:51:26 +00002284static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002285 tsample_t sample,Image *image)
2286{
2287 int32
2288 status;
2289
cristybb503372010-05-27 20:51:26 +00002290 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002291 i;
2292
2293 register unsigned char
2294 *p,
2295 *q;
2296
cristybb503372010-05-27 20:51:26 +00002297 size_t
cristy3ed852e2009-09-05 21:47:34 +00002298 number_tiles,
2299 tile_width;
2300
cristyc6da28e2011-04-28 01:41:35 +00002301 ssize_t
2302 bytes_per_pixel,
2303 j,
2304 k,
2305 l;
2306
cristy3ed852e2009-09-05 21:47:34 +00002307 if (TIFFIsTiled(tiff) == 0)
2308 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2309 /*
2310 Fill scanlines to tile height.
2311 */
cristybb503372010-05-27 20:51:26 +00002312 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002313 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2314 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002315 if (((size_t) (row % tiff_info->tile_geometry.height) !=
cristyc6da28e2011-04-28 01:41:35 +00002316 (tiff_info->tile_geometry.height-1)) &&
2317 (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002318 return(0);
2319 /*
2320 Write tile to TIFF image.
2321 */
2322 status=0;
cristy32fab702012-01-18 02:47:47 +00002323 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (
2324 tiff_info->tile_geometry.height*tiff_info->tile_geometry.width);
cristy3ed852e2009-09-05 21:47:34 +00002325 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2326 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002327 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002328 {
cristybb503372010-05-27 20:51:26 +00002329 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002330 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002331 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2332 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002333 {
2334 if (bytes_per_pixel == 0)
2335 {
2336 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2337 tiff_info->tile_geometry.width+k)/8);
2338 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2339 *q++=(*p++);
2340 continue;
2341 }
2342 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2343 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2344 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2345 for (l=0; l < bytes_per_pixel; l++)
2346 *q++=(*p++);
2347 }
cristydaff8092010-04-22 14:50:53 +00002348 if ((i*tiff_info->tile_geometry.width) != image->columns)
2349 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2350 tiff_info->tile_geometry.width),(uint32) ((row/
2351 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2352 sample);
cristy3ed852e2009-09-05 21:47:34 +00002353 if (status < 0)
2354 break;
2355 }
2356 return(status);
2357}
2358
2359static void TIFFSetProfiles(TIFF *tiff,Image *image)
2360{
2361 const char
2362 *name;
2363
2364 const StringInfo
2365 *profile;
2366
2367 if (image->profiles == (void *) NULL)
2368 return;
2369 ResetImageProfileIterator(image);
2370 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2371 {
2372 profile=GetImageProfile(image,name);
2373#if defined(TIFFTAG_XMLPACKET)
2374 if (LocaleCompare(name,"xmp") == 0)
2375 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2376 profile),GetStringInfoDatum(profile));
2377#endif
2378#if defined(TIFFTAG_ICCPROFILE)
2379 if (LocaleCompare(name,"icc") == 0)
2380 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2381 profile),GetStringInfoDatum(profile));
2382#endif
2383 if (LocaleCompare(name,"iptc") == 0)
2384 {
2385 size_t
2386 length;
2387
2388 StringInfo
2389 *iptc_profile;
2390
2391 iptc_profile=CloneStringInfo(profile);
2392 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2393 0x03);
2394 SetStringInfoLength(iptc_profile,length);
2395 if (TIFFIsByteSwapped(tiff))
2396 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002397 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002398 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2399 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2400 iptc_profile=DestroyStringInfo(iptc_profile);
2401 }
2402#if defined(TIFFTAG_PHOTOSHOP)
2403 if (LocaleCompare(name,"8bim") == 0)
2404 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2405 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2406#endif
2407 if (LocaleCompare(name,"tiff:37724") == 0)
cristye5d85502011-11-09 19:45:06 +00002408 (void) TIFFSetField(tiff,37724,(uint32) GetStringInfoLength(profile),
cristy3ed852e2009-09-05 21:47:34 +00002409 GetStringInfoDatum(profile));
2410 name=GetNextImageProfile(image);
2411 }
2412}
2413
cristyd15e6592011-10-15 00:13:06 +00002414static void TIFFSetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002415{
2416 const char
2417 *value;
2418
cristy15d8d212010-09-17 01:56:30 +00002419 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristyd15e6592011-10-15 00:13:06 +00002420 value=GetImageProperty(image,"tiff:hostcomputer",exception);
cristy3ed852e2009-09-05 21:47:34 +00002421 if (value != (const char *) NULL)
2422 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
cristyd15e6592011-10-15 00:13:06 +00002423 value=GetImageProperty(image,"tiff:artist",exception);
cristy3ed852e2009-09-05 21:47:34 +00002424 if (value != (const char *) NULL)
2425 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
cristyd15e6592011-10-15 00:13:06 +00002426 value=GetImageProperty(image,"tiff:timestamp",exception);
cristy3ed852e2009-09-05 21:47:34 +00002427 if (value != (const char *) NULL)
2428 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
cristyd15e6592011-10-15 00:13:06 +00002429 value=GetImageProperty(image,"tiff:make",exception);
cristy3ed852e2009-09-05 21:47:34 +00002430 if (value != (const char *) NULL)
2431 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
cristyd15e6592011-10-15 00:13:06 +00002432 value=GetImageProperty(image,"tiff:model",exception);
cristy3ed852e2009-09-05 21:47:34 +00002433 if (value != (const char *) NULL)
2434 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristyd15e6592011-10-15 00:13:06 +00002435 value=GetImageProperty(image,"tiff:software",exception);
cristy15d8d212010-09-17 01:56:30 +00002436 if (value != (const char *) NULL)
2437 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristyd15e6592011-10-15 00:13:06 +00002438 value=GetImageProperty(image,"tiff:copyright",exception);
cristy3ed852e2009-09-05 21:47:34 +00002439 if (value != (const char *) NULL)
cristy98ca0f52011-10-08 23:19:10 +00002440 (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value);
cristyd15e6592011-10-15 00:13:06 +00002441 value=GetImageProperty(image,"kodak-33423",exception);
cristy3ed852e2009-09-05 21:47:34 +00002442 if (value != (const char *) NULL)
2443 (void) TIFFSetField(tiff,33423,value);
cristyd15e6592011-10-15 00:13:06 +00002444 value=GetImageProperty(image,"kodak-36867",exception);
cristy3ed852e2009-09-05 21:47:34 +00002445 if (value != (const char *) NULL)
2446 (void) TIFFSetField(tiff,36867,value);
cristyd15e6592011-10-15 00:13:06 +00002447 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00002448 if (value != (const char *) NULL)
2449 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
cristyd15e6592011-10-15 00:13:06 +00002450 value=GetImageProperty(image,"comment",exception);
cristy3ed852e2009-09-05 21:47:34 +00002451 if (value != (const char *) NULL)
2452 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2453}
2454
cristyd15e6592011-10-15 00:13:06 +00002455static void TIFFSetEXIFProperties(TIFF *tiff,Image *image,
2456 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002457{
2458#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2459 const char
2460 *value;
2461
cristybb503372010-05-27 20:51:26 +00002462 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002463 i;
2464
2465 uint32
2466 offset;
2467
2468 /*
2469 Write EXIF properties.
2470 */
2471 offset=0;
2472 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2473 for (i=0; exif_info[i].tag != 0; i++)
2474 {
cristyd15e6592011-10-15 00:13:06 +00002475 value=GetImageProperty(image,exif_info[i].property,exception);
cristy3ed852e2009-09-05 21:47:34 +00002476 if (value == (const char *) NULL)
2477 continue;
2478 switch (exif_info[i].type)
2479 {
2480 case TIFF_ASCII:
2481 {
2482 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2483 break;
2484 }
2485 case TIFF_SHORT:
2486 {
2487 uint16
2488 shorty;
2489
cristyf2f27272009-12-17 14:48:46 +00002490 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002491 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2492 break;
2493 }
2494 case TIFF_LONG:
2495 {
2496 uint16
cristybb503372010-05-27 20:51:26 +00002497 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002498
cristybb503372010-05-27 20:51:26 +00002499 ssize_ty=(uint16) StringToLong(value);
2500 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002501 break;
2502 }
2503 case TIFF_RATIONAL:
2504 case TIFF_SRATIONAL:
2505 {
2506 float
2507 rational;
2508
cristydbdd0e32011-11-04 23:29:40 +00002509 rational=StringToDouble(value,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002510 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2511 break;
2512 }
2513 default:
2514 break;
2515 }
2516 }
2517 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2518#else
2519 (void) tiff;
2520 (void) image;
2521#endif
2522}
2523
2524static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002525 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002526{
2527#if !defined(TIFFDefaultStripSize)
2528#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2529#endif
2530
2531 const char
2532 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002533 *option;
cristy3ed852e2009-09-05 21:47:34 +00002534
2535 CompressionType
2536 compression;
2537
cristy6b032822010-06-29 16:52:32 +00002538 EndianType
2539 endian_type;
2540
cristy3ed852e2009-09-05 21:47:34 +00002541 MagickBooleanType
2542 debug,
2543 status;
2544
2545 MagickOffsetType
2546 scene;
2547
2548 QuantumInfo
2549 *quantum_info;
2550
2551 QuantumType
2552 quantum_type;
2553
cristybb503372010-05-27 20:51:26 +00002554 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002555 i;
2556
2557 size_t
cristy9945c6e2012-01-23 19:49:26 +00002558 length;
cristy3ed852e2009-09-05 21:47:34 +00002559
cristyc6da28e2011-04-28 01:41:35 +00002560 ssize_t
2561 y;
2562
cristy3ed852e2009-09-05 21:47:34 +00002563 TIFF
2564 *tiff;
2565
2566 TIFFErrorHandler
2567 error_handler,
2568 warning_handler;
2569
2570 TIFFInfo
2571 tiff_info;
2572
2573 uint16
2574 bits_per_sample,
2575 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002576 endian,
cristy3ed852e2009-09-05 21:47:34 +00002577 photometric;
2578
2579 uint32
2580 rows_per_strip;
2581
2582 unsigned char
2583 *pixels;
2584
cristy3ed852e2009-09-05 21:47:34 +00002585 /*
2586 Open TIFF file.
2587 */
2588 assert(image_info != (const ImageInfo *) NULL);
2589 assert(image_info->signature == MagickSignature);
2590 assert(image != (Image *) NULL);
2591 assert(image->signature == MagickSignature);
2592 if (image->debug != MagickFalse)
2593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002594 assert(exception != (ExceptionInfo *) NULL);
2595 assert(exception->signature == MagickSignature);
2596 assert(exception != (ExceptionInfo *) NULL);
2597 assert(exception->signature == MagickSignature);
2598 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00002599 if (status == MagickFalse)
2600 return(status);
cristy3a37efd2011-08-28 20:31:03 +00002601 (void) MagickSetThreadValue(tiff_exception,exception);
cristy3ed852e2009-09-05 21:47:34 +00002602 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2603 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002604 endian_type=UndefinedEndian;
2605 option=GetImageOption(image_info,"tiff:endian");
2606 if (option != (const char *) NULL)
2607 {
2608 if (LocaleNCompare(option,"msb",3) == 0)
2609 endian_type=MSBEndian;
2610 if (LocaleNCompare(option,"lsb",3) == 0)
2611 endian_type=LSBEndian;;
2612 }
2613 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002614 {
2615 case LSBEndian: mode="wl"; break;
2616 case MSBEndian: mode="wb"; break;
2617 default: mode="w"; break;
2618 }
2619#if defined(TIFF_VERSION_BIG)
2620 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002621 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002622 {
2623 case LSBEndian: mode="wl8"; break;
2624 case MSBEndian: mode="wb8"; break;
2625 default: mode="w8"; break;
2626 }
2627#endif
2628 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2629 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2630 TIFFUnmapBlob);
2631 if (tiff == (TIFF *) NULL)
2632 {
2633 (void) TIFFSetWarningHandler(warning_handler);
2634 (void) TIFFSetErrorHandler(error_handler);
2635 return(MagickFalse);
2636 }
2637 scene=0;
2638 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +00002639 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +00002640 do
2641 {
2642 /*
2643 Initialize TIFF fields.
2644 */
cristy5f1c1ff2010-12-23 21:38:06 +00002645 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002646 (image_info->type != OptimizeType))
cristy018f07f2011-09-04 21:15:19 +00002647 (void) SetImageType(image,image_info->type,exception);
cristy3ed852e2009-09-05 21:47:34 +00002648 quantum_info=AcquireQuantumInfo(image_info,image);
2649 if (quantum_info == (QuantumInfo *) NULL)
2650 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2651 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2652 (quantum_info->format == UndefinedQuantumFormat) &&
cristy3a37efd2011-08-28 20:31:03 +00002653 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002654 {
2655 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2656 if (status == MagickFalse)
2657 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2658 }
2659 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2660 (GetPreviousImageInList(image) != (Image *) NULL))
2661 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2662 if ((image->columns != (uint32) image->columns) ||
2663 (image->rows != (uint32) image->rows))
2664 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2665 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2666 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2667 compression=image->compression;
2668 if (image_info->compression != UndefinedCompression)
2669 compression=image_info->compression;
2670 switch (compression)
2671 {
2672 case FaxCompression:
2673 {
2674 compress_tag=COMPRESSION_CCITTFAX3;
2675 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2676 break;
2677 }
2678 case Group4Compression:
2679 {
2680 compress_tag=COMPRESSION_CCITTFAX4;
2681 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2682 break;
2683 }
cristy6d5e20f2011-04-25 13:48:54 +00002684#if defined(COMPRESSION_JBIG)
2685 case JBIG1Compression:
2686 {
2687 compress_tag=COMPRESSION_JBIG;
2688 break;
2689 }
2690#endif
cristy3ed852e2009-09-05 21:47:34 +00002691 case JPEGCompression:
2692 {
2693 compress_tag=COMPRESSION_JPEG;
2694 break;
2695 }
cristyfbb0ef02010-12-19 02:32:11 +00002696#if defined(COMPRESSION_LZMA)
2697 case LZMACompression:
2698 {
2699 compress_tag=COMPRESSION_LZMA;
2700 break;
2701 }
2702#endif
cristy3ed852e2009-09-05 21:47:34 +00002703 case LZWCompression:
2704 {
2705 compress_tag=COMPRESSION_LZW;
2706 break;
2707 }
2708 case RLECompression:
2709 {
2710 compress_tag=COMPRESSION_PACKBITS;
2711 break;
2712 }
2713 case ZipCompression:
2714 {
2715 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2716 break;
2717 }
2718 case NoCompression:
2719 default:
2720 {
2721 compress_tag=COMPRESSION_NONE;
2722 break;
2723 }
2724 }
cristy5b675872009-10-21 13:12:00 +00002725#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2726 if ((compress_tag != COMPRESSION_NONE) &&
2727 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002728 {
cristy3a37efd2011-08-28 20:31:03 +00002729 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2730 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002731 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002732 compress_tag=COMPRESSION_NONE;
2733 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002734 }
2735#else
2736 switch (compress_tag)
2737 {
2738#if defined(CCITT_SUPPORT)
2739 case COMPRESSION_CCITTFAX3:
2740 case COMPRESSION_CCITTFAX4:
2741#endif
2742#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2743 case COMPRESSION_JPEG:
2744#endif
cristyfbb0ef02010-12-19 02:32:11 +00002745#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2746 case COMPRESSION_LZMA:
2747#endif
cristy3ed852e2009-09-05 21:47:34 +00002748#if defined(LZW_SUPPORT)
2749 case COMPRESSION_LZW:
2750#endif
2751#if defined(PACKBITS_SUPPORT)
2752 case COMPRESSION_PACKBITS:
2753#endif
2754#if defined(ZIP_SUPPORT)
2755 case COMPRESSION_ADOBE_DEFLATE:
2756#endif
2757 case COMPRESSION_NONE:
2758 break;
2759 default:
2760 {
cristy3a37efd2011-08-28 20:31:03 +00002761 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2762 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002763 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002764 compress_tag=COMPRESSION_NONE;
2765 compression=NoCompression;
2766 break;
2767 }
2768 }
2769#endif
2770 switch (compression)
2771 {
2772 case FaxCompression:
2773 case Group4Compression:
2774 {
cristy018f07f2011-09-04 21:15:19 +00002775 (void) SetImageType(image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +00002776 break;
2777 }
2778 case JPEGCompression:
2779 {
cristy3a37efd2011-08-28 20:31:03 +00002780 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a11cb12011-10-19 23:53:34 +00002781 (void) SetImageDepth(image,8,exception);
cristy3ed852e2009-09-05 21:47:34 +00002782 break;
2783 }
2784 default:
2785 break;
2786 }
cristy3ed852e2009-09-05 21:47:34 +00002787 if (image->colorspace == CMYKColorspace)
2788 {
2789 photometric=PHOTOMETRIC_SEPARATED;
2790 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2791 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2792 }
2793 else
2794 {
2795 /*
2796 Full color TIFF raster.
2797 */
2798 if (image->colorspace == LabColorspace)
2799 photometric=PHOTOMETRIC_CIELAB;
2800 else
2801 if (image->colorspace == YCbCrColorspace)
2802 {
2803 photometric=PHOTOMETRIC_YCBCR;
2804 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy3a37efd2011-08-28 20:31:03 +00002805 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a11cb12011-10-19 23:53:34 +00002806 (void) SetImageDepth(image,8,exception);
cristy3ed852e2009-09-05 21:47:34 +00002807 }
2808 else
2809 {
cristy510d06a2011-07-06 23:43:54 +00002810 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +00002811 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00002812 photometric=PHOTOMETRIC_RGB;
2813 }
2814 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2815 if ((image_info->type != TrueColorType) &&
2816 (image_info->type != TrueColorMatteType))
2817 {
cristy94c8fe42009-10-06 01:57:36 +00002818 if ((image_info->type != PaletteType) &&
cristy3a37efd2011-08-28 20:31:03 +00002819 (IsImageGray(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002820 {
cristy94c8fe42009-10-06 01:57:36 +00002821 photometric=(uint16) (quantum_info->min_is_white !=
2822 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2823 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002824 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002825 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy3a37efd2011-08-28 20:31:03 +00002826 (IsImageMonochrome(image,exception) != MagickFalse))
cristy94c8fe42009-10-06 01:57:36 +00002827 {
2828 status=SetQuantumDepth(image,quantum_info,1);
2829 if (status == MagickFalse)
2830 ThrowWriterException(ResourceLimitError,
2831 "MemoryAllocationFailed");
2832 }
cristy3ed852e2009-09-05 21:47:34 +00002833 }
2834 else
cristy94c8fe42009-10-06 01:57:36 +00002835 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002836 {
cristybb503372010-05-27 20:51:26 +00002837 size_t
cristy94c8fe42009-10-06 01:57:36 +00002838 depth;
2839
2840 /*
2841 Colormapped TIFF raster.
2842 */
cristy3ed852e2009-09-05 21:47:34 +00002843 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002844 photometric=PHOTOMETRIC_PALETTE;
2845 depth=1;
2846 while ((GetQuantumRange(depth)+1) < image->colors)
2847 depth<<=1;
2848 status=SetQuantumDepth(image,quantum_info,depth);
2849 if (status == MagickFalse)
2850 ThrowWriterException(ResourceLimitError,
2851 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002852 }
2853 }
2854 }
cristyf2687ca2010-06-29 16:32:38 +00002855 switch (image->endian)
2856 {
2857 case LSBEndian:
2858 {
2859 endian=FILLORDER_LSB2MSB;
2860 break;
2861 }
2862 case MSBEndian:
2863 {
2864 endian=FILLORDER_MSB2LSB;
2865 break;
2866 }
2867 case UndefinedEndian:
2868 default:
2869 {
2870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2871 break;
2872 }
2873 }
cristydaff8092010-04-22 14:50:53 +00002874 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2875 (photometric != PHOTOMETRIC_MINISWHITE))
2876 {
2877 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002878 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002879 }
cristy0accf6e2009-10-01 13:14:28 +00002880 else
cristydaff8092010-04-22 14:50:53 +00002881 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2882 (photometric != PHOTOMETRIC_MINISWHITE))
2883 {
2884 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002885 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002886 }
cristyd15e6592011-10-15 00:13:06 +00002887 option=GetImageProperty(image,"tiff:fill-order",exception);
cristy62e282b2010-06-29 01:27:13 +00002888 if (option != (const char *) NULL)
2889 {
2890 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002891 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002892 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002893 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002894 }
cristyf2687ca2010-06-29 16:32:38 +00002895 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2896 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002897 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2898 if (image->matte != MagickFalse)
2899 {
2900 uint16
2901 extra_samples,
2902 sample_info[1],
2903 samples_per_pixel;
2904
2905 /*
2906 TIFF has a matte channel.
2907 */
2908 extra_samples=1;
2909 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
cristyd15e6592011-10-15 00:13:06 +00002910 option=GetImageProperty(image,"tiff:alpha",exception);
cristy3ed852e2009-09-05 21:47:34 +00002911 if ((option != (const char *) NULL) &&
2912 (LocaleCompare(option,"associated") == 0))
2913 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2914 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2915 &samples_per_pixel);
2916 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2917 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2918 &sample_info);
2919 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2920 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2921 }
2922 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2923 switch (quantum_info->format)
2924 {
2925 case FloatingPointQuantumFormat:
2926 {
2927 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2928 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2929 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2930 break;
2931 }
2932 case SignedQuantumFormat:
2933 {
2934 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2935 break;
2936 }
2937 case UnsignedQuantumFormat:
2938 {
2939 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2940 break;
2941 }
2942 default:
2943 break;
2944 }
cristy3ed852e2009-09-05 21:47:34 +00002945 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2946 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2947 if (photometric == PHOTOMETRIC_RGB)
2948 if ((image_info->interlace == PlaneInterlace) ||
2949 (image_info->interlace == PartitionInterlace))
2950 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2951 rows_per_strip=1;
2952 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002953 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2954 1);
cristy3ed852e2009-09-05 21:47:34 +00002955 option=GetImageOption(image_info,"tiff:rows-per-strip");
2956 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002957 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002958 switch (compress_tag)
2959 {
2960 case COMPRESSION_JPEG:
2961 {
2962#if defined(JPEG_SUPPORT)
2963 const char
2964 *sampling_factor;
2965
2966 GeometryInfo
2967 geometry_info;
2968
2969 MagickStatusType
2970 flags;
2971
2972 rows_per_strip+=(16-(rows_per_strip % 16));
2973 if (image->quality != 0)
2974 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2975 if (image_info->quality != UndefinedCompressionQuality)
2976 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2977 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
cristy510d06a2011-07-06 23:43:54 +00002978 if (IsRGBColorspace(image->colorspace) == MagickTrue)
cristy3ed852e2009-09-05 21:47:34 +00002979 {
cristy949bf5b2010-05-08 02:47:03 +00002980 const char
2981 *value;
2982
cristy3ed852e2009-09-05 21:47:34 +00002983 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2984 sampling_factor=(const char *) NULL;
cristyd15e6592011-10-15 00:13:06 +00002985 value=GetImageProperty(image,"jpeg:sampling-factor",exception);
cristy3ed852e2009-09-05 21:47:34 +00002986 if (value != (char *) NULL)
2987 {
2988 sampling_factor=value;
2989 if (image->debug != MagickFalse)
2990 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2991 " Input sampling-factors=%s",sampling_factor);
2992 }
2993 if (image_info->sampling_factor != (char *) NULL)
2994 sampling_factor=image_info->sampling_factor;
2995 if (sampling_factor != (const char *) NULL)
2996 {
2997 flags=ParseGeometry(sampling_factor,&geometry_info);
2998 if ((flags & SigmaValue) == 0)
2999 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00003000 if (image->colorspace == YCbCrColorspace)
3001 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
3002 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00003003 }
3004 }
3005 if (bits_per_sample == 12)
3006 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
3007#endif
3008 break;
3009 }
3010 case COMPRESSION_ADOBE_DEFLATE:
3011 {
cristyf6fe0a12010-05-30 00:44:47 +00003012 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003013 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3014 &bits_per_sample);
3015 if (((photometric == PHOTOMETRIC_RGB) ||
3016 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3017 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00003018 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00003019 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
3020 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00003021 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00003022 break;
3023 }
3024 case COMPRESSION_CCITTFAX3:
3025 {
3026 /*
3027 Byte-aligned EOL.
3028 */
cristyeaedf062010-05-29 22:36:02 +00003029 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003030 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
3031 break;
3032 }
3033 case COMPRESSION_CCITTFAX4:
3034 {
cristyeaedf062010-05-29 22:36:02 +00003035 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003036 break;
3037 }
cristyef8f26b2010-12-19 20:29:16 +00003038#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3039 case COMPRESSION_LZMA:
3040 {
3041 if (((photometric == PHOTOMETRIC_RGB) ||
3042 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3043 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3044 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3045 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
3046 image_info->quality == UndefinedCompressionQuality ? 7 :
3047 MagickMin((ssize_t) image_info->quality/10,9)));
3048 break;
3049 }
3050#endif
cristy3ed852e2009-09-05 21:47:34 +00003051 case COMPRESSION_LZW:
3052 {
3053 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3054 &bits_per_sample);
3055 if (((photometric == PHOTOMETRIC_RGB) ||
3056 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3057 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00003058 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00003059 break;
3060 }
3061 default:
3062 break;
3063 }
cristy79e5dad2010-09-16 19:48:33 +00003064 option=GetImageOption(image_info,"tiff:tile-geometry");
3065 if (option == (const char *) NULL)
3066 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy2a11bef2011-10-28 18:33:11 +00003067 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
cristy3ed852e2009-09-05 21:47:34 +00003068 {
3069 unsigned short
3070 units;
3071
3072 /*
3073 Set image resolution.
3074 */
3075 units=RESUNIT_NONE;
3076 if (image->units == PixelsPerInchResolution)
3077 units=RESUNIT_INCH;
3078 if (image->units == PixelsPerCentimeterResolution)
3079 units=RESUNIT_CENTIMETER;
3080 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
cristy2a11bef2011-10-28 18:33:11 +00003081 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x);
3082 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00003083 if ((image->page.x != 0) || (image->page.y != 0))
3084 {
3085 /*
3086 Set image position.
3087 */
3088 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
cristy2a11bef2011-10-28 18:33:11 +00003089 image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00003090 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
cristy2a11bef2011-10-28 18:33:11 +00003091 image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00003092 }
3093 }
3094 if (image->chromaticity.white_point.x != 0.0)
3095 {
3096 float
3097 chromaticity[6];
3098
3099 /*
3100 Set image chromaticity.
3101 */
3102 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3103 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3104 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3105 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3106 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3107 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3108 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3109 chromaticity[0]=(float) image->chromaticity.white_point.x;
3110 chromaticity[1]=(float) image->chromaticity.white_point.y;
3111 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3112 }
cristy59788de2011-11-23 13:56:19 +00003113 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3114 (image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
cristy3ed852e2009-09-05 21:47:34 +00003115 {
3116 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3117 if (image->scene != 0)
3118 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3119 GetImageListLength(image));
3120 }
3121 if (image->orientation != UndefinedOrientation)
3122 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3123 (void) TIFFSetProfiles(tiff,image);
3124 {
3125 uint16
3126 page,
3127 pages;
3128
3129 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00003130 pages=(uint16) GetImageListLength(image);
cristy59788de2011-11-23 13:56:19 +00003131 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3132 (image_info->adjoin != MagickFalse) && (pages > 1))
cristy3ed852e2009-09-05 21:47:34 +00003133 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3134 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3135 }
cristyd15e6592011-10-15 00:13:06 +00003136 (void) TIFFSetProperties(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003137 if (0)
cristyd15e6592011-10-15 00:13:06 +00003138 (void) TIFFSetEXIFProperties(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003139 /*
3140 Write image scanlines.
3141 */
3142 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3143 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristyf164a872012-01-20 18:26:34 +00003144 quantum_info->endian=LSBEndian;
3145 if (endian == FILLORDER_LSB2MSB)
3146 quantum_info->endian=MSBEndian;
cristy9945c6e2012-01-23 19:49:26 +00003147 image->endian=quantum_info->endian;
cristy3ed852e2009-09-05 21:47:34 +00003148 pixels=GetQuantumPixels(quantum_info);
3149 tiff_info.scanline=GetQuantumPixels(quantum_info);
3150 switch (photometric)
3151 {
3152 case PHOTOMETRIC_CIELAB:
3153 case PHOTOMETRIC_YCBCR:
3154 case PHOTOMETRIC_RGB:
3155 {
3156 /*
3157 RGB TIFF image.
3158 */
3159 switch (image_info->interlace)
3160 {
3161 case NoInterlace:
3162 default:
3163 {
3164 quantum_type=RGBQuantum;
3165 if (image->matte != MagickFalse)
3166 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003167 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003168 {
cristy4c08aed2011-07-01 19:47:50 +00003169 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003170 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003171
cristy3a37efd2011-08-28 20:31:03 +00003172 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003173 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003174 break;
cristy4c08aed2011-07-01 19:47:50 +00003175 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003176 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00003177 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00003178 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3179 break;
3180 if (image->previous == (Image *) NULL)
3181 {
cristy2837bcc2010-08-07 23:57:39 +00003182 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3183 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003184 if (status == MagickFalse)
3185 break;
3186 }
3187 }
3188 break;
3189 }
3190 case PlaneInterlace:
3191 case PartitionInterlace:
3192 {
3193 /*
3194 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3195 */
cristybb503372010-05-27 20:51:26 +00003196 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003197 {
cristy4c08aed2011-07-01 19:47:50 +00003198 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003199 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003200
cristy3a37efd2011-08-28 20:31:03 +00003201 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003202 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003203 break;
cristy4c08aed2011-07-01 19:47:50 +00003204 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003205 RedQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003206 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3207 break;
3208 }
3209 if (image->previous == (Image *) NULL)
3210 {
3211 status=SetImageProgress(image,SaveImageTag,100,400);
3212 if (status == MagickFalse)
3213 break;
3214 }
cristybb503372010-05-27 20:51:26 +00003215 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003216 {
cristy4c08aed2011-07-01 19:47:50 +00003217 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003218 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003219
cristy3a37efd2011-08-28 20:31:03 +00003220 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003221 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003222 break;
cristy4c08aed2011-07-01 19:47:50 +00003223 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003224 GreenQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003225 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3226 break;
3227 }
3228 if (image->previous == (Image *) NULL)
3229 {
3230 status=SetImageProgress(image,SaveImageTag,200,400);
3231 if (status == MagickFalse)
3232 break;
3233 }
cristybb503372010-05-27 20:51:26 +00003234 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003235 {
cristy4c08aed2011-07-01 19:47:50 +00003236 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003237 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003238
cristy3a37efd2011-08-28 20:31:03 +00003239 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003240 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003241 break;
cristy4c08aed2011-07-01 19:47:50 +00003242 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003243 BlueQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003244 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3245 break;
3246 }
3247 if (image->previous == (Image *) NULL)
3248 {
3249 status=SetImageProgress(image,SaveImageTag,300,400);
3250 if (status == MagickFalse)
3251 break;
3252 }
3253 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003254 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003255 {
cristy4c08aed2011-07-01 19:47:50 +00003256 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003257 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003258
cristy3a37efd2011-08-28 20:31:03 +00003259 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003260 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003261 break;
cristy4c08aed2011-07-01 19:47:50 +00003262 length=ExportQuantumPixels(image,(CacheView *) NULL,
cristy3a37efd2011-08-28 20:31:03 +00003263 quantum_info,AlphaQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003264 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3265 break;
3266 }
3267 if (image->previous == (Image *) NULL)
3268 {
3269 status=SetImageProgress(image,SaveImageTag,400,400);
3270 if (status == MagickFalse)
3271 break;
3272 }
3273 break;
3274 }
3275 }
3276 break;
3277 }
3278 case PHOTOMETRIC_SEPARATED:
3279 {
3280 /*
3281 CMYK TIFF image.
3282 */
3283 quantum_type=CMYKQuantum;
3284 if (image->matte != MagickFalse)
3285 quantum_type=CMYKAQuantum;
3286 if (image->colorspace != CMYKColorspace)
cristye941a752011-10-15 01:52:48 +00003287 (void) TransformImageColorspace(image,CMYKColorspace,exception);
cristybb503372010-05-27 20:51:26 +00003288 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003289 {
cristy4c08aed2011-07-01 19:47:50 +00003290 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003291 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003292
cristy3a37efd2011-08-28 20:31:03 +00003293 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003294 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003295 break;
cristy4c08aed2011-07-01 19:47:50 +00003296 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003297 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003298 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3299 break;
3300 if (image->previous == (Image *) NULL)
3301 {
cristycee97112010-05-28 00:44:52 +00003302 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3303 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003304 if (status == MagickFalse)
3305 break;
3306 }
3307 }
3308 break;
3309 }
3310 case PHOTOMETRIC_PALETTE:
3311 {
3312 uint16
3313 *blue,
3314 *green,
3315 *red;
3316
3317 /*
3318 Colormapped TIFF image.
3319 */
3320 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3321 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3322 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3323 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3324 (blue == (uint16 *) NULL))
3325 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3326 /*
3327 Initialize TIFF colormap.
3328 */
3329 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3330 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3331 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003332 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003333 {
3334 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3335 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3336 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3337 }
3338 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3339 red=(uint16 *) RelinquishMagickMemory(red);
3340 green=(uint16 *) RelinquishMagickMemory(green);
3341 blue=(uint16 *) RelinquishMagickMemory(blue);
3342 }
3343 default:
3344 {
3345 /*
3346 Convert PseudoClass packets to contiguous grayscale scanlines.
3347 */
3348 quantum_type=IndexQuantum;
3349 if (image->matte != MagickFalse)
3350 {
3351 if (photometric != PHOTOMETRIC_PALETTE)
3352 quantum_type=GrayAlphaQuantum;
3353 else
3354 quantum_type=IndexAlphaQuantum;
3355 }
3356 else
3357 if (photometric != PHOTOMETRIC_PALETTE)
3358 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003359 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003360 {
cristy4c08aed2011-07-01 19:47:50 +00003361 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003362 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003363
cristy3a37efd2011-08-28 20:31:03 +00003364 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003365 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003366 break;
cristy4c08aed2011-07-01 19:47:50 +00003367 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003368 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003369 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3370 break;
3371 if (image->previous == (Image *) NULL)
3372 {
cristycee97112010-05-28 00:44:52 +00003373 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3374 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003375 if (status == MagickFalse)
3376 break;
3377 }
3378 }
3379 break;
3380 }
3381 }
3382 quantum_info=DestroyQuantumInfo(quantum_info);
3383 DestroyTIFFInfo(&tiff_info);
3384 if (0 && (image_info->verbose == MagickTrue))
3385 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3386 (void) TIFFWriteDirectory(tiff);
cristy3ed852e2009-09-05 21:47:34 +00003387 image=SyncNextImageInList(image);
3388 if (image == (Image *) NULL)
3389 break;
3390 status=SetImageProgress(image,SaveImagesTag,scene++,
3391 GetImageListLength(image));
3392 if (status == MagickFalse)
3393 break;
3394 } while (image_info->adjoin != MagickFalse);
3395 (void) TIFFSetWarningHandler(warning_handler);
3396 (void) TIFFSetErrorHandler(error_handler);
3397 TIFFClose(tiff);
3398 return(MagickTrue);
3399}
3400#endif