blob: c7a599219e12f5598e9b5af7fdd8097b965037cf [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"
cristyc4debe62012-02-09 13:51:53 +000043#include "MagickCore/artifact.h"
cristy4c08aed2011-07-01 19:47:50 +000044#include "MagickCore/attribute.h"
45#include "MagickCore/blob.h"
46#include "MagickCore/blob-private.h"
47#include "MagickCore/cache.h"
48#include "MagickCore/color.h"
49#include "MagickCore/color-private.h"
50#include "MagickCore/colormap.h"
51#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000052#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000053#include "MagickCore/constitute.h"
54#include "MagickCore/enhance.h"
55#include "MagickCore/exception.h"
56#include "MagickCore/exception-private.h"
57#include "MagickCore/geometry.h"
58#include "MagickCore/image.h"
59#include "MagickCore/image-private.h"
60#include "MagickCore/list.h"
61#include "MagickCore/log.h"
62#include "MagickCore/magick.h"
63#include "MagickCore/memory_.h"
64#include "MagickCore/module.h"
65#include "MagickCore/monitor.h"
66#include "MagickCore/monitor-private.h"
67#include "MagickCore/option.h"
68#include "MagickCore/pixel-accessor.h"
69#include "MagickCore/property.h"
70#include "MagickCore/quantum.h"
71#include "MagickCore/quantum-private.h"
72#include "MagickCore/profile.h"
73#include "MagickCore/resize.h"
74#include "MagickCore/resource_.h"
75#include "MagickCore/semaphore.h"
76#include "MagickCore/splay-tree.h"
77#include "MagickCore/static.h"
78#include "MagickCore/statistic.h"
79#include "MagickCore/string_.h"
80#include "MagickCore/string-private.h"
81#include "MagickCore/thread_.h"
cristye40005d2012-03-23 12:18:45 +000082#include "MagickCore/token.h"
cristy4c08aed2011-07-01 19:47:50 +000083#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000084#if defined(MAGICKCORE_TIFF_DELEGATE)
85# if defined(MAGICKCORE_HAVE_TIFFCONF_H)
86# include "tiffconf.h"
cristyef8f26b2010-12-19 20:29:16 +000087# endif
cristy3ed852e2009-09-05 21:47:34 +000088# include "tiff.h"
89# include "tiffio.h"
90# if !defined(COMPRESSION_ADOBE_DEFLATE)
91# define COMPRESSION_ADOBE_DEFLATE 8
92# endif
cristyef8f26b2010-12-19 20:29:16 +000093# if !defined(PREDICTOR_HORIZONTAL)
94# define PREDICTOR_HORIZONTAL 2
95# endif
cristy98ca0f52011-10-08 23:19:10 +000096# if !defined(TIFFTAG_COPYRIGHT)
97# define TIFFTAG_COPYRIGHT 33432
98# endif
99# if !defined(TIFFTAG_OPIIMAGEID)
100# define TIFF_OPIIMAGEID 32781
101# endif
cristy3ed852e2009-09-05 21:47:34 +0000102
103/*
104 Typedef declarations.
105*/
106#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
107typedef struct _ExifInfo
108{
109 unsigned int
110 tag,
111 type;
112
113 const char
114 *property;
115} ExifInfo;
116
117static const ExifInfo
118 exif_info[] = {
119 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
120 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
121 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
122 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000123 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy7992e402011-09-16 15:24:22 +0000124 { EXIFTAG_OECF, TIFF_NOTYPE, "exif:OptoelectricConversionFactor" },
125 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, "exif:ExifVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000126 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
127 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000128 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, "exif:ComponentsConfiguration" },
cristy3ed852e2009-09-05 21:47:34 +0000129 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
130 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
131 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
132 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
133 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
134 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
135 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
136 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
137 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
138 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
139 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
cristy7992e402011-09-16 15:24:22 +0000140 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, "exif:SubjectArea" },
141 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, "exif:MakerNote" },
142 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, "exif:UserComment" },
cristy3ed852e2009-09-05 21:47:34 +0000143 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
144 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
145 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000146 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, "exif:FlashpixVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000147 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
148 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
149 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
150 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
151 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
152 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
cristy7992e402011-09-16 15:24:22 +0000153 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, "exif:SpatialFrequencyResponse" },
cristy3ed852e2009-09-05 21:47:34 +0000154 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
155 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
156 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
157 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
158 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
159 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
cristy7992e402011-09-16 15:24:22 +0000160 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, "exif:FileSource" },
161 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, "exif:SceneType" },
162 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, "exif:CFAPattern" },
cristy3ed852e2009-09-05 21:47:34 +0000163 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
164 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
165 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
166 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
167 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
168 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
169 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
170 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
171 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
172 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
cristy7992e402011-09-16 15:24:22 +0000173 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, "exif:DeviceSettingDescription" },
cristy3ed852e2009-09-05 21:47:34 +0000174 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
175 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
176 { 0, 0, (char *) NULL }
177};
178#endif
cristy0805e062011-01-30 02:56:36 +0000179#endif /* MAGICKCORE_TIFF_DELEGATE */
cristy3ed852e2009-09-05 21:47:34 +0000180
181/*
182 Global declarations.
183*/
184static MagickThreadKey
185 tiff_exception;
186
187static SemaphoreInfo
188 *tiff_semaphore = (SemaphoreInfo *) NULL;
189
190static volatile MagickBooleanType
191 instantiate_key = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000192
193/*
194 Forward declarations.
195*/
196#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000197static Image *
198 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
199
cristy3ed852e2009-09-05 21:47:34 +0000200static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +0000201 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
202 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
203 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000204#endif
205
206/*
207%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208% %
209% %
210% %
211% I s T I F F %
212% %
213% %
214% %
215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216%
217% IsTIFF() returns MagickTrue if the image format type, identified by the
218% magick string, is TIFF.
219%
220% The format of the IsTIFF method is:
221%
222% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
223%
224% A description of each parameter follows:
225%
226% o magick: compare image format pattern against these bytes.
227%
228% o length: Specifies the length of the magick string.
229%
230*/
231static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
232{
233 if (length < 4)
234 return(MagickFalse);
235 if (memcmp(magick,"\115\115\000\052",4) == 0)
236 return(MagickTrue);
237 if (memcmp(magick,"\111\111\052\000",4) == 0)
238 return(MagickTrue);
239#if defined(TIFF_VERSION_BIG)
240 if (length < 8)
241 return(MagickFalse);
242 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
243 return(MagickTrue);
244 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
245 return(MagickTrue);
246#endif
247 return(MagickFalse);
248}
249
250#if defined(MAGICKCORE_TIFF_DELEGATE)
251/*
252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253% %
254% %
255% %
cristy3d7f8062009-09-24 20:45:35 +0000256% R e a d G R O U P 4 I m a g e %
257% %
258% %
259% %
260%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261%
262% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
263% allocates the memory necessary for the new Image structure and returns a
264% pointer to the new image.
265%
266% The format of the ReadGROUP4Image method is:
267%
268% Image *ReadGROUP4Image(const ImageInfo *image_info,
269% ExceptionInfo *exception)
270%
271% A description of each parameter follows:
272%
273% o image_info: the image info.
274%
275% o exception: return any errors or warnings in this structure.
276%
277*/
278
cristyeaedf062010-05-29 22:36:02 +0000279static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000280{
281 unsigned char
282 buffer[4];
283
284 buffer[0]=(unsigned char) value;
285 buffer[1]=(unsigned char) (value >> 8);
286 buffer[2]=(unsigned char) (value >> 16);
287 buffer[3]=(unsigned char) (value >> 24);
288 return(fwrite(buffer,1,4,file));
289}
290
291static Image *ReadGROUP4Image(const ImageInfo *image_info,
292 ExceptionInfo *exception)
293{
294 char
295 filename[MaxTextExtent];
296
297 FILE
298 *file;
299
300 Image
301 *image;
302
303 ImageInfo
304 *read_info;
305
306 int
307 c,
308 unique_file;
309
310 MagickBooleanType
311 status;
312
313 size_t
314 length;
315
316 ssize_t
317 offset,
318 strip_offset;
319
320 /*
321 Open image file.
322 */
323 assert(image_info != (const ImageInfo *) NULL);
324 assert(image_info->signature == MagickSignature);
325 if (image_info->debug != MagickFalse)
326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
327 image_info->filename);
328 assert(exception != (ExceptionInfo *) NULL);
329 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000330 image=AcquireImage(image_info,exception);
cristy3d7f8062009-09-24 20:45:35 +0000331 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
332 if (status == MagickFalse)
333 {
334 image=DestroyImageList(image);
335 return((Image *) NULL);
336 }
337 /*
338 Write raw CCITT Group 4 wrapped as a TIFF image file.
339 */
340 file=(FILE *) NULL;
341 unique_file=AcquireUniqueFileResource(filename);
342 if (unique_file != -1)
343 file=fdopen(unique_file,"wb");
344 if ((unique_file == -1) || (file == (FILE *) NULL))
345 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
346 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
347 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
348 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
349 length=WriteLSBLong(file,image->columns);
350 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
351 length=WriteLSBLong(file,image->rows);
352 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
353 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
354 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
355 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
356 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000357 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000358 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000359 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000360 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
361 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000362 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000363 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
364 offset=(ssize_t) ftell(file)-4;
365 length=fwrite("\032\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("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000368 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000369 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
370 length=fwrite("\000\000\000\000",1,4,file);
cristy2a11bef2011-10-28 18:33:11 +0000371 length=WriteLSBLong(file,(long) image->resolution.x);
cristy3d7f8062009-09-24 20:45:35 +0000372 length=WriteLSBLong(file,1);
373 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
374 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000375 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000376 length=WriteLSBLong(file,(unsigned int) length);
377 (void) fclose(file);
378 (void) CloseBlob(image);
379 image=DestroyImage(image);
380 /*
381 Read TIFF image.
382 */
cristy14efd992009-09-26 23:46:03 +0000383 read_info=CloneImageInfo((ImageInfo *) NULL);
cristyb51dff52011-05-19 16:55:47 +0000384 (void) FormatLocaleString(read_info->filename,MaxTextExtent,"%s",filename);
cristy3d7f8062009-09-24 20:45:35 +0000385 image=ReadTIFFImage(read_info,exception);
386 read_info=DestroyImageInfo(read_info);
387 if (image != (Image *) NULL)
388 {
389 (void) CopyMagickString(image->filename,image_info->filename,
390 MaxTextExtent);
391 (void) CopyMagickString(image->magick_filename,image_info->filename,
392 MaxTextExtent);
393 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
394 }
395 (void) RelinquishUniqueFileResource(filename);
396 return(image);
397}
398#endif
399
400#if defined(MAGICKCORE_TIFF_DELEGATE)
401/*
402%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403% %
404% %
405% %
cristy3ed852e2009-09-05 21:47:34 +0000406% R e a d T I F F I m a g e %
407% %
408% %
409% %
410%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411%
412% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
413% memory necessary for the new Image structure and returns a pointer to the
414% new image.
415%
416% The format of the ReadTIFFImage method is:
417%
418% Image *ReadTIFFImage(const ImageInfo *image_info,
419% ExceptionInfo *exception)
420%
421% A description of each parameter follows:
422%
423% o image_info: the image info.
424%
425% o exception: return any errors or warnings in this structure.
426%
427*/
428
429static inline size_t MagickMax(const size_t x,const size_t y)
430{
431 if (x > y)
432 return(x);
433 return(y);
434}
435
cristybb503372010-05-27 20:51:26 +0000436static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000437{
438 if (x < y)
439 return(x);
440 return(y);
441}
442
443static MagickBooleanType ReadProfile(Image *image,const char *name,
cristy018f07f2011-09-04 21:15:19 +0000444 unsigned char *datum,ssize_t length,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000445{
446 MagickBooleanType
447 status;
448
cristybb503372010-05-27 20:51:26 +0000449 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000450 i;
451
452 StringInfo
453 *profile;
454
455 if (length < 4)
456 return(MagickFalse);
457 i=0;
458 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
459 {
460 for (i=0; i < (length-4); i+=2)
461 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
462 break;
463 if (i == length)
464 length-=i;
465 else
466 i=0;
467 if (length < 4)
468 return(MagickFalse);
469 }
cristye8f8f382011-09-01 13:32:37 +0000470 profile=BlobToStringInfo(datum+i,(size_t) length);
471 if (profile == (StringInfo *) NULL)
472 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
473 image->filename);
cristyd15e6592011-10-15 00:13:06 +0000474 status=SetImageProfile(image,name,profile,exception);
cristy3ed852e2009-09-05 21:47:34 +0000475 profile=DestroyStringInfo(profile);
476 if (status == MagickFalse)
477 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
478 image->filename);
479 return(MagickTrue);
480}
481
482#if defined(__cplusplus) || defined(c_plusplus)
483extern "C" {
484#endif
485
486static int TIFFCloseBlob(thandle_t image)
487{
488 (void) CloseBlob((Image *) image);
489 return(0);
490}
491
492static void TIFFErrors(const char *module,const char *format,va_list error)
493{
494 char
495 message[MaxTextExtent];
496
497 ExceptionInfo
498 *exception;
499
500#if defined(MAGICKCORE_HAVE_VSNPRINTF)
501 (void) vsnprintf(message,MaxTextExtent,format,error);
502#else
503 (void) vsprintf(message,format,error);
504#endif
505 (void) ConcatenateMagickString(message,".",MaxTextExtent);
506 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
507 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000508 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
509 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000510}
511
cristy9c47c732011-11-09 19:58:33 +0000512static toff_t TIFFGetBlobSize(thandle_t image)
513{
514 return((toff_t) GetBlobSize((Image *) image));
515}
516
cristy018f07f2011-09-04 21:15:19 +0000517static void TIFFGetProfiles(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000518{
519 uint32
520 length;
521
522 unsigned char
523 *profile;
524
525#if defined(TIFFTAG_ICCPROFILE)
526 length=0;
527 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000528 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000529#endif
530#if defined(TIFFTAG_PHOTOSHOP)
531 length=0;
532 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000533 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000534#endif
535#if defined(TIFFTAG_RICHTIFFIPTC)
536 length=0;
537 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
538 {
539 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000540 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy018f07f2011-09-04 21:15:19 +0000541 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000542 }
543#endif
544#if defined(TIFFTAG_XMLPACKET)
545 length=0;
546 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000547 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000548#endif
549 length=0;
550 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000551 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000552}
553
cristyd15e6592011-10-15 00:13:06 +0000554static void TIFFGetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000555{
556 char
cristy98ca0f52011-10-08 23:19:10 +0000557 message[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000558 *text;
559
cristy98ca0f52011-10-08 23:19:10 +0000560 uint32
561 count;
562
cristy3ed852e2009-09-05 21:47:34 +0000563 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000564 (void) SetImageProperty(image,"tiff:artist",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000565 if (TIFFGetField(tiff,TIFFTAG_COPYRIGHT,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000566 (void) SetImageProperty(image,"tiff:copyright",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000567 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000568 (void) SetImageProperty(image,"tiff:timestamp",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000569 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000570 (void) SetImageProperty(image,"tiff:document",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000571 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000572 (void) SetImageProperty(image,"tiff:hostcomputer",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000573 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000574 (void) SetImageProperty(image,"comment",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000575 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000576 (void) SetImageProperty(image,"tiff:make",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000577 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000578 (void) SetImageProperty(image,"tiff:model",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000579 if (TIFFGetField(tiff,TIFFTAG_OPIIMAGEID,&count,&text) == 1)
580 {
581 if (count >= MaxTextExtent)
582 count=MaxTextExtent-1;
583 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000584 (void) SetImageProperty(image,"tiff:image-id",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000585 }
cristy3ed852e2009-09-05 21:47:34 +0000586 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000587 (void) SetImageProperty(image,"label",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000588 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000589 (void) SetImageProperty(image,"tiff:software",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000590 if (TIFFGetField(tiff,33423,&count,&text) == 1)
591 {
592 if (count >= MaxTextExtent)
593 count=MaxTextExtent-1;
594 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000595 (void) SetImageProperty(image,"tiff:kodak-33423",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000596 }
597 if (TIFFGetField(tiff,36867,&count,&text) == 1)
598 {
599 if (count >= MaxTextExtent)
600 count=MaxTextExtent-1;
601 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000602 (void) SetImageProperty(image,"tiff:kodak-36867",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000603 }
cristy3ed852e2009-09-05 21:47:34 +0000604}
605
cristyd15e6592011-10-15 00:13:06 +0000606static void TIFFGetEXIFProperties(TIFF *tiff,Image *image,
607 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000608{
609#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
610 char
611 value[MaxTextExtent];
612
cristybb503372010-05-27 20:51:26 +0000613 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000614 i;
615
616 tdir_t
617 directory;
618
cristyccf4e122011-10-29 00:29:19 +0000619#if defined(TIFF_VERSION_BIG)
620 uint64
621#else
cristy3ed852e2009-09-05 21:47:34 +0000622 uint32
cristyccf4e122011-10-29 00:29:19 +0000623#endif
cristy3ed852e2009-09-05 21:47:34 +0000624 offset;
625
cristy7992e402011-09-16 15:24:22 +0000626 void
627 *sans;
628
cristy3ed852e2009-09-05 21:47:34 +0000629 /*
630 Read EXIF properties.
631 */
cristy00170cd2012-01-25 00:55:46 +0000632 offset=0;
cristy3ed852e2009-09-05 21:47:34 +0000633 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
634 return;
635 directory=TIFFCurrentDirectory(tiff);
636 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
637 return;
cristy7992e402011-09-16 15:24:22 +0000638 sans=NULL;
cristy3ed852e2009-09-05 21:47:34 +0000639 for (i=0; exif_info[i].tag != 0; i++)
640 {
641 *value='\0';
642 switch (exif_info[i].type)
643 {
644 case TIFF_ASCII:
645 {
646 char
647 *ascii;
648
cristy7992e402011-09-16 15:24:22 +0000649 ascii=(char *) NULL;
cristy0c823b62012-01-25 00:44:43 +0000650 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans,&sans) != 0) &&
cristy7992e402011-09-16 15:24:22 +0000651 (ascii != (char *) NULL) && (*ascii != '\0'))
cristyf162e012012-03-28 12:54:33 +0000652 (void) CopyMagickString(value,ascii,MaxTextExtent);
cristy3ed852e2009-09-05 21:47:34 +0000653 break;
654 }
655 case TIFF_SHORT:
656 {
657 uint16
cristy7992e402011-09-16 15:24:22 +0000658 shorty[2] = { 0, 0};
cristy3ed852e2009-09-05 21:47:34 +0000659
cristy0c823b62012-01-25 00:44:43 +0000660 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans,&sans) != 0)
cristy7992e402011-09-16 15:24:22 +0000661 (void) FormatLocaleString(value,MaxTextExtent,"%d",(int) shorty[0]);
cristy3ed852e2009-09-05 21:47:34 +0000662 break;
663 }
664 case TIFF_LONG:
665 {
666 uint32
cristy7992e402011-09-16 15:24:22 +0000667 longy;
cristy3ed852e2009-09-05 21:47:34 +0000668
cristy0c823b62012-01-25 00:44:43 +0000669 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans,&sans) != 0)
cristy7992e402011-09-16 15:24:22 +0000670 (void) FormatLocaleString(value,MaxTextExtent,"%d",longy);
cristy3ed852e2009-09-05 21:47:34 +0000671 break;
672 }
cristyccf4e122011-10-29 00:29:19 +0000673#if defined(TIFF_VERSION_BIG)
674 case TIFF_LONG8:
675 {
676 uint64
677 longy;
678
cristy0c823b62012-01-25 00:44:43 +0000679 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans,&sans) != 0)
cristybcb0f452012-02-12 19:26:56 +0000680 (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
681 ((MagickOffsetType) longy));
cristyccf4e122011-10-29 00:29:19 +0000682 break;
683 }
684#endif
cristy3ed852e2009-09-05 21:47:34 +0000685 case TIFF_RATIONAL:
686 case TIFF_SRATIONAL:
cristy7992e402011-09-16 15:24:22 +0000687 case TIFF_FLOAT:
688 case TIFF_DOUBLE:
cristy3ed852e2009-09-05 21:47:34 +0000689 {
690 float
cristy7992e402011-09-16 15:24:22 +0000691 rational[16];
cristy3ed852e2009-09-05 21:47:34 +0000692
cristy0c823b62012-01-25 00:44:43 +0000693 if (TIFFGetField(tiff,exif_info[i].tag,&rational,&sans,&sans) != 0)
cristy7992e402011-09-16 15:24:22 +0000694 (void) FormatLocaleString(value,MaxTextExtent,"%g",rational[0]);
cristy3ed852e2009-09-05 21:47:34 +0000695 break;
696 }
697 default:
698 break;
699 }
700 if (*value != '\0')
cristyd15e6592011-10-15 00:13:06 +0000701 (void) SetImageProperty(image,exif_info[i].property,value,exception);
cristy3ed852e2009-09-05 21:47:34 +0000702 }
703 TIFFSetDirectory(tiff,directory);
704#else
705 (void) tiff;
706 (void) image;
707#endif
708}
709
710static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
711{
712 *base=(tdata_t *) GetBlobStreamData((Image *) image);
713 if (*base != (tdata_t *) NULL)
714 *size=(toff_t) GetBlobSize((Image *) image);
715 if (*base != (tdata_t *) NULL)
716 return(1);
717 return(0);
718}
719
720static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
721{
722 tsize_t
723 count;
724
725 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
726 (unsigned char *) data);
727 return(count);
728}
729
cristybb503372010-05-27 20:51:26 +0000730static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
731 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000732{
733 int32
734 status;
735
736 (void) bits_per_sample;
737 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
738 return(status);
739}
740
741static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
742{
743 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
744}
745
cristy3ed852e2009-09-05 21:47:34 +0000746static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
747{
748 (void) image;
749 (void) base;
750 (void) size;
751}
752
753static void TIFFWarnings(const char *module,const char *format,va_list warning)
754{
755 char
756 message[MaxTextExtent];
757
758 ExceptionInfo
759 *exception;
760
761#if defined(MAGICKCORE_HAVE_VSNPRINTF)
762 (void) vsnprintf(message,MaxTextExtent,format,warning);
763#else
764 (void) vsprintf(message,format,warning);
765#endif
766 (void) ConcatenateMagickString(message,".",MaxTextExtent);
767 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
768 if (exception != (ExceptionInfo *) NULL)
769 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
770 message,"`%s'",module);
771}
772
773static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
774{
775 tsize_t
776 count;
777
778 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
779 (unsigned char *) data);
780 return(count);
781}
782
783#if defined(__cplusplus) || defined(c_plusplus)
784}
785#endif
786
787static Image *ReadTIFFImage(const ImageInfo *image_info,
788 ExceptionInfo *exception)
789{
790 typedef enum
791 {
792 ReadSingleSampleMethod,
793 ReadRGBAMethod,
794 ReadCMYKAMethod,
795 ReadStripMethod,
796 ReadTileMethod,
797 ReadGenericMethod
798 } TIFFMethodType;
799
800 const char
801 *option;
802
803 float
804 *chromaticity,
805 x_position,
806 y_position,
807 x_resolution,
808 y_resolution;
809
810 Image
811 *image;
812
cristy3ed852e2009-09-05 21:47:34 +0000813 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000814 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000815 debug,
816 status;
817
818 MagickSizeType
819 number_pixels;
820
821 QuantumInfo
822 *quantum_info;
823
824 QuantumType
825 quantum_type;
826
cristybb503372010-05-27 20:51:26 +0000827 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000828 i;
829
830 size_t
831 length,
832 pad;
833
cristyc6da28e2011-04-28 01:41:35 +0000834 ssize_t
835 y;
836
cristy3ed852e2009-09-05 21:47:34 +0000837 TIFF
838 *tiff;
839
840 TIFFErrorHandler
841 error_handler,
842 warning_handler;
843
844 TIFFMethodType
845 method;
846
847 uint16
848 compress_tag,
849 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000850 endian,
cristy3ed852e2009-09-05 21:47:34 +0000851 extra_samples,
852 interlace,
853 max_sample_value,
854 min_sample_value,
855 orientation,
856 pages,
857 photometric,
858 *sample_info,
859 sample_format,
860 samples_per_pixel,
861 units,
862 value;
863
864 uint32
865 height,
866 rows_per_strip,
867 width;
868
869 unsigned char
870 *pixels;
871
cristy3ed852e2009-09-05 21:47:34 +0000872 /*
873 Open image.
874 */
875 assert(image_info != (const ImageInfo *) NULL);
876 assert(image_info->signature == MagickSignature);
877 if (image_info->debug != MagickFalse)
878 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
879 image_info->filename);
880 assert(exception != (ExceptionInfo *) NULL);
881 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000882 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000883 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
884 if (status == MagickFalse)
885 {
886 image=DestroyImageList(image);
887 return((Image *) NULL);
888 }
889 (void) MagickSetThreadValue(tiff_exception,exception);
890 error_handler=TIFFSetErrorHandler(TIFFErrors);
891 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
cristy1630f7f2011-02-18 01:10:02 +0000892 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
cristy3ed852e2009-09-05 21:47:34 +0000893 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
894 TIFFUnmapBlob);
895 if (tiff == (TIFF *) NULL)
896 {
897 (void) TIFFSetWarningHandler(warning_handler);
898 (void) TIFFSetErrorHandler(error_handler);
899 image=DestroyImageList(image);
900 return((Image *) NULL);
901 }
902 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +0000903 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +0000904 if (image_info->number_scenes != 0)
905 {
906 /*
907 Generate blank images for subimage specification (e.g. image.tif[4].
908 */
cristybb503372010-05-27 20:51:26 +0000909 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000910 {
911 (void) TIFFReadDirectory(tiff);
cristy9950d572011-10-01 18:22:35 +0000912 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000913 if (GetNextImageInList(image) == (Image *) NULL)
914 {
915 image=DestroyImageList(image);
916 return((Image *) NULL);
917 }
918 image=SyncNextImageInList(image);
919 }
920 }
921 do
922 {
923 if (0 && (image_info->verbose != MagickFalse))
924 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristy3f590e52011-11-14 14:29:44 +0000925#if defined(MAGICKCORE_HAVE_TIFFISBIGENDIAN)
cristy6b032822010-06-29 16:52:32 +0000926 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
cristyd15e6592011-10-15 00:13:06 +0000927 "lsb" : "msb",exception);
cristy3f590e52011-11-14 14:29:44 +0000928#endif
cristy3ed852e2009-09-05 21:47:34 +0000929 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
930 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
931 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
932 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000933 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000934 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
935 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
936 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
cristyae1319c2011-01-19 20:18:53 +0000937 if (sample_format == SAMPLEFORMAT_IEEEFP)
cristyd15e6592011-10-15 00:13:06 +0000938 (void) SetImageProperty(image,"quantum:format","floating-point",
939 exception);
cristy3ed852e2009-09-05 21:47:34 +0000940 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
941 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
942 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
943 switch (photometric)
944 {
945 case PHOTOMETRIC_MINISBLACK:
946 {
cristyd15e6592011-10-15 00:13:06 +0000947 (void) SetImageProperty(image,"tiff:photometric","min-is-black",
948 exception);
cristy3ed852e2009-09-05 21:47:34 +0000949 break;
950 }
951 case PHOTOMETRIC_MINISWHITE:
952 {
cristyd15e6592011-10-15 00:13:06 +0000953 (void) SetImageProperty(image,"tiff:photometric","min-is-white",
954 exception);
cristy3ed852e2009-09-05 21:47:34 +0000955 break;
956 }
957 case PHOTOMETRIC_PALETTE:
958 {
cristyd15e6592011-10-15 00:13:06 +0000959 (void) SetImageProperty(image,"tiff:photometric","palette",exception);
cristy3ed852e2009-09-05 21:47:34 +0000960 break;
961 }
962 case PHOTOMETRIC_RGB:
963 {
cristyd15e6592011-10-15 00:13:06 +0000964 (void) SetImageProperty(image,"tiff:photometric","RGB",exception);
cristy3ed852e2009-09-05 21:47:34 +0000965 break;
966 }
967 case PHOTOMETRIC_CIELAB:
968 {
cristyd15e6592011-10-15 00:13:06 +0000969 (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception);
cristy3ed852e2009-09-05 21:47:34 +0000970 break;
971 }
972 case PHOTOMETRIC_SEPARATED:
973 {
cristyd15e6592011-10-15 00:13:06 +0000974 (void) SetImageProperty(image,"tiff:photometric","separated",exception);
cristy3ed852e2009-09-05 21:47:34 +0000975 break;
976 }
cristy79e5dad2010-09-16 19:48:33 +0000977 case PHOTOMETRIC_YCBCR:
978 {
cristyd15e6592011-10-15 00:13:06 +0000979 (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception);
cristy79e5dad2010-09-16 19:48:33 +0000980 break;
981 }
cristy3ed852e2009-09-05 21:47:34 +0000982 default:
983 {
cristyd15e6592011-10-15 00:13:06 +0000984 (void) SetImageProperty(image,"tiff:photometric","unknown",exception);
cristy3ed852e2009-09-05 21:47:34 +0000985 break;
986 }
987 }
988 if (image->debug != MagickFalse)
989 {
990 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
991 (unsigned int) width,(unsigned int) height);
992 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
993 interlace);
994 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
995 "Bits per sample: %u",bits_per_sample);
996 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
997 "Min sample value: %u",min_sample_value);
998 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
999 "Max sample value: %u",max_sample_value);
1000 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
cristyd15e6592011-10-15 00:13:06 +00001001 "interpretation: %s",GetImageProperty(image,"tiff:photometric",
1002 exception));
cristy3ed852e2009-09-05 21:47:34 +00001003 }
cristybb503372010-05-27 20:51:26 +00001004 image->columns=(size_t) width;
1005 image->rows=(size_t) height;
1006 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +00001007 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +00001008 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
1009 (double) image->depth);
cristye669ae42012-02-08 19:07:45 +00001010 image->endian=MSBEndian;
cristy50c2b692012-02-06 14:45:10 +00001011 if (endian == FILLORDER_LSB2MSB)
cristye669ae42012-02-08 19:07:45 +00001012 image->endian=LSBEndian;
cristyb241f382012-01-01 01:45:57 +00001013 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1014 (photometric == PHOTOMETRIC_MINISWHITE))
1015 image->colorspace=GRAYColorspace;
cristy3ed852e2009-09-05 21:47:34 +00001016 if (photometric == PHOTOMETRIC_SEPARATED)
1017 image->colorspace=CMYKColorspace;
1018 if (photometric == PHOTOMETRIC_CIELAB)
1019 image->colorspace=LabColorspace;
1020 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
1021 &samples_per_pixel);
1022 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
cristy2a11bef2011-10-28 18:33:11 +00001023 x_resolution=(float) image->resolution.x;
1024 y_resolution=(float) image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +00001025 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
1026 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
cristy2a11bef2011-10-28 18:33:11 +00001027 image->resolution.x=x_resolution;
1028 image->resolution.y=y_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001029 x_position=(float) image->page.x/x_resolution;
1030 y_position=(float) image->page.y/y_resolution;
1031 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
1032 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +00001033 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
1034 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001035 image->orientation=(OrientationType) orientation;
1036 chromaticity=(float *) NULL;
1037 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
1038 if (chromaticity != (float *) NULL)
1039 {
1040 image->chromaticity.white_point.x=chromaticity[0];
1041 image->chromaticity.white_point.y=chromaticity[1];
1042 }
1043 chromaticity=(float *) NULL;
1044 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
1045 if (chromaticity != (float *) NULL)
1046 {
1047 image->chromaticity.red_primary.x=chromaticity[0];
1048 image->chromaticity.red_primary.y=chromaticity[1];
1049 image->chromaticity.green_primary.x=chromaticity[2];
1050 image->chromaticity.green_primary.y=chromaticity[3];
1051 image->chromaticity.blue_primary.x=chromaticity[4];
1052 image->chromaticity.blue_primary.y=chromaticity[5];
1053 }
cristy3ed852e2009-09-05 21:47:34 +00001054#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1055 if ((compress_tag != COMPRESSION_NONE) &&
1056 (TIFFIsCODECConfigured(compress_tag) == 0))
1057 {
1058 TIFFClose(tiff);
1059 ThrowReaderException(CoderError,"CompressNotSupported");
1060 }
1061#endif
1062 switch (compress_tag)
1063 {
1064 case COMPRESSION_NONE: image->compression=NoCompression; break;
1065 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1066 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1067 case COMPRESSION_JPEG:
1068 {
1069 image->compression=JPEGCompression;
1070#if defined(JPEG_SUPPORT)
1071 {
1072 char
1073 sampling_factor[MaxTextExtent];
1074
1075 uint16
1076 horizontal,
1077 vertical;
1078
1079 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1080 &horizontal,&vertical);
cristyb51dff52011-05-19 16:55:47 +00001081 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
cristy3ed852e2009-09-05 21:47:34 +00001082 horizontal,vertical);
1083 (void) SetImageProperty(image,"jpeg:sampling-factor",
cristyd15e6592011-10-15 00:13:06 +00001084 sampling_factor,exception);
cristy3ed852e2009-09-05 21:47:34 +00001085 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1086 "Sampling Factors: %s",sampling_factor);
1087 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1088 {
1089 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1090 JPEGCOLORMODE_RGB);
1091 photometric=PHOTOMETRIC_RGB;
1092 }
1093 }
1094#endif
1095 break;
1096 }
1097 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001098#if defined(COMPRESSION_LZMA)
1099 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1100#endif
cristy3ed852e2009-09-05 21:47:34 +00001101 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1102 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1103 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1104 default: image->compression=RLECompression; break;
1105 }
cristy809fdbd2011-12-01 01:26:27 +00001106 TIFFGetProfiles(tiff,image,exception);
1107 TIFFGetProperties(tiff,image,exception);
cristyd93c30e2012-01-09 17:16:52 +00001108 option=GetImageOption(image_info,"tiff:exif-properties");
1109 if ((option == (const char *) NULL) ||
cristy9e8cb1c2012-01-24 23:47:47 +00001110 (IsMagickTrue(option) != MagickFalse))
cristyd93c30e2012-01-09 17:16:52 +00001111 TIFFGetEXIFProperties(tiff,image,exception);
cristy809fdbd2011-12-01 01:26:27 +00001112 /*
1113 Allocate memory for the image and pixel buffer.
1114 */
cristy3ed852e2009-09-05 21:47:34 +00001115 quantum_info=AcquireQuantumInfo(image_info,image);
1116 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001117 {
1118 TIFFClose(tiff);
1119 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1120 }
cristy3ed852e2009-09-05 21:47:34 +00001121 if (sample_format == SAMPLEFORMAT_UINT)
1122 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1123 if (sample_format == SAMPLEFORMAT_INT)
1124 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1125 if (sample_format == SAMPLEFORMAT_IEEEFP)
1126 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1127 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001128 {
cristya9a6ec42011-10-26 16:55:01 +00001129 quantum_info=DestroyQuantumInfo(quantum_info);
cristy8d137bf2010-04-21 23:52:04 +00001130 TIFFClose(tiff);
1131 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1132 }
cristy3ed852e2009-09-05 21:47:34 +00001133 status=MagickTrue;
1134 switch (photometric)
1135 {
1136 case PHOTOMETRIC_MINISBLACK:
1137 {
1138 quantum_info->min_is_white=MagickFalse;
1139 break;
1140 }
1141 case PHOTOMETRIC_MINISWHITE:
1142 {
1143 quantum_info->min_is_white=MagickTrue;
1144 break;
1145 }
1146 default:
1147 break;
1148 }
cristy891dc792010-03-04 01:47:16 +00001149 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001150 extra_samples=0;
1151 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1152 &sample_info);
1153 if (extra_samples == 0)
1154 {
1155 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1156 image->matte=MagickTrue;
1157 }
1158 else
cristy6c207342010-02-19 17:49:50 +00001159 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001160 {
cristy7cf69862010-03-03 13:57:24 +00001161 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001162 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001163 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001164 }
cristy891dc792010-03-04 01:47:16 +00001165 option=GetImageOption(image_info,"tiff:alpha");
1166 if (option != (const char *) NULL)
1167 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1168 MagickFalse;
1169 if (image->matte != MagickFalse)
1170 (void) SetImageProperty(image,"tiff:alpha",
cristyd15e6592011-10-15 00:13:06 +00001171 associated_alpha != MagickFalse ? "associated" : "unassociated",
1172 exception);
cristy3ed852e2009-09-05 21:47:34 +00001173 if ((photometric == PHOTOMETRIC_PALETTE) &&
1174 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1175 {
cristybb503372010-05-27 20:51:26 +00001176 size_t
cristy3ed852e2009-09-05 21:47:34 +00001177 colors;
1178
cristybb503372010-05-27 20:51:26 +00001179 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy018f07f2011-09-04 21:15:19 +00001180 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001181 {
1182 TIFFClose(tiff);
1183 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1184 }
1185 }
1186 if (units == RESUNIT_INCH)
1187 image->units=PixelsPerInchResolution;
1188 if (units == RESUNIT_CENTIMETER)
1189 image->units=PixelsPerCentimeterResolution;
1190 value=(unsigned short) image->scene;
1191 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1192 image->scene=value;
1193 if (image_info->ping != MagickFalse)
1194 {
1195 if (image_info->number_scenes != 0)
1196 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1197 break;
1198 goto next_tiff_frame;
1199 }
1200 method=ReadGenericMethod;
1201 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1202 {
1203 char
1204 value[MaxTextExtent];
1205
1206 method=ReadStripMethod;
cristyb51dff52011-05-19 16:55:47 +00001207 (void) FormatLocaleString(value,MaxTextExtent,"%u",
cristy3ed852e2009-09-05 21:47:34 +00001208 (unsigned int) rows_per_strip);
cristyd15e6592011-10-15 00:13:06 +00001209 (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception);
cristy3ed852e2009-09-05 21:47:34 +00001210 }
1211 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1212 method=ReadRGBAMethod;
1213 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1214 method=ReadCMYKAMethod;
1215 if ((photometric != PHOTOMETRIC_RGB) &&
1216 (photometric != PHOTOMETRIC_CIELAB) &&
1217 (photometric != PHOTOMETRIC_SEPARATED))
1218 method=ReadGenericMethod;
1219 if (image->storage_class == PseudoClass)
1220 method=ReadSingleSampleMethod;
1221 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1222 (photometric == PHOTOMETRIC_MINISWHITE))
1223 method=ReadSingleSampleMethod;
1224 if ((photometric != PHOTOMETRIC_SEPARATED) &&
cristybc736662011-03-09 13:39:42 +00001225 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
cristy3ed852e2009-09-05 21:47:34 +00001226 method=ReadGenericMethod;
1227 if (TIFFIsTiled(tiff) != MagickFalse)
1228 method=ReadTileMethod;
cristy50c2b692012-02-06 14:45:10 +00001229 quantum_info->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001230 quantum_type=RGBQuantum;
1231 pixels=GetQuantumPixels(quantum_info);
1232 switch (method)
1233 {
1234 case ReadSingleSampleMethod:
1235 {
1236 /*
1237 Convert TIFF image to PseudoClass MIFF image.
1238 */
1239 if ((image->storage_class == PseudoClass) &&
1240 (photometric == PHOTOMETRIC_PALETTE))
1241 {
cristybb503372010-05-27 20:51:26 +00001242 size_t
cristy3ed852e2009-09-05 21:47:34 +00001243 range;
1244
1245 uint16
1246 *blue_colormap,
1247 *green_colormap,
1248 *red_colormap;
1249
1250 /*
1251 Initialize colormap.
1252 */
1253 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1254 &green_colormap,&blue_colormap);
1255 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001256 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001257 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1258 (blue_colormap[i] >= 256))
1259 {
1260 range=65535;
1261 break;
1262 }
cristybb503372010-05-27 20:51:26 +00001263 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001264 {
cristyce70c172010-01-07 17:15:30 +00001265 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001266 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001267 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001268 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001269 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001270 blue_colormap[i])/range);
1271 }
1272 }
1273 quantum_type=IndexQuantum;
1274 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1275 if (image->matte != MagickFalse)
1276 {
1277 if (image->storage_class != PseudoClass)
1278 {
1279 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1280 GrayAlphaQuantum;
1281 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1282 }
1283 else
1284 {
1285 quantum_type=IndexAlphaQuantum;
1286 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1287 }
1288 }
1289 else
1290 if (image->storage_class != PseudoClass)
1291 {
1292 quantum_type=GrayQuantum;
1293 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1294 }
1295 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1296 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001297 {
1298 TIFFClose(tiff);
1299 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1300 }
cristy3ed852e2009-09-05 21:47:34 +00001301 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001302 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001303 {
1304 int
1305 status;
1306
cristy4c08aed2011-07-01 19:47:50 +00001307 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001308 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001309
1310 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1311 if (status == -1)
1312 break;
1313 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001314 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001315 break;
1316 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1317 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00001318 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00001319 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1320 break;
1321 if (image->previous == (Image *) NULL)
1322 {
cristycee97112010-05-28 00:44:52 +00001323 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1324 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001325 if (status == MagickFalse)
1326 break;
1327 }
1328 }
1329 break;
1330 }
1331 case ReadRGBAMethod:
1332 {
1333 /*
1334 Convert TIFF image to DirectClass MIFF image.
1335 */
1336 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1337 quantum_type=RGBQuantum;
1338 if (image->matte != MagickFalse)
1339 {
1340 quantum_type=RGBAQuantum;
1341 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1342 }
1343 if (image->colorspace == CMYKColorspace)
1344 {
1345 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1346 quantum_type=CMYKQuantum;
1347 if (image->matte != MagickFalse)
1348 {
1349 quantum_type=CMYKAQuantum;
1350 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1351 }
1352 }
1353 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1354 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001355 {
1356 TIFFClose(tiff);
1357 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1358 }
cristy3ed852e2009-09-05 21:47:34 +00001359 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001360 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001361 {
1362 int
1363 status;
1364
cristy4c08aed2011-07-01 19:47:50 +00001365 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001366 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001367
1368 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1369 if (status == -1)
1370 break;
1371 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001372 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001373 break;
cristy0b334892010-03-15 02:26:46 +00001374 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1375 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001376 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1377 break;
1378 if (image->previous == (Image *) NULL)
1379 {
cristycee97112010-05-28 00:44:52 +00001380 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1381 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001382 if (status == MagickFalse)
1383 break;
1384 }
1385 }
1386 break;
1387 }
1388 case ReadCMYKAMethod:
1389 {
1390 /*
1391 Convert TIFF image to DirectClass MIFF image.
1392 */
cristybb503372010-05-27 20:51:26 +00001393 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001394 {
cristybb503372010-05-27 20:51:26 +00001395 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001396 {
cristy4c08aed2011-07-01 19:47:50 +00001397 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001398 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001399
cristy3ed852e2009-09-05 21:47:34 +00001400 int
1401 status;
1402
1403 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1404 pixels);
1405 if (status == -1)
1406 break;
1407 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001408 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001409 break;
1410 if (image->colorspace != CMYKColorspace)
1411 switch (i)
1412 {
1413 case 0: quantum_type=RedQuantum; break;
1414 case 1: quantum_type=GreenQuantum; break;
1415 case 2: quantum_type=BlueQuantum; break;
1416 case 3: quantum_type=AlphaQuantum; break;
1417 default: quantum_type=UndefinedQuantum; break;
1418 }
cristy6d9f12f2009-11-03 14:50:26 +00001419 else
cristy3ed852e2009-09-05 21:47:34 +00001420 switch (i)
1421 {
1422 case 0: quantum_type=CyanQuantum; break;
1423 case 1: quantum_type=MagentaQuantum; break;
1424 case 2: quantum_type=YellowQuantum; break;
1425 case 3: quantum_type=BlackQuantum; break;
1426 case 4: quantum_type=AlphaQuantum; break;
1427 default: quantum_type=UndefinedQuantum; break;
1428 }
1429 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1430 quantum_type,pixels,exception);
1431 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1432 break;
1433 }
1434 if (image->previous == (Image *) NULL)
1435 {
cristycee97112010-05-28 00:44:52 +00001436 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1437 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001438 if (status == MagickFalse)
1439 break;
1440 }
1441 }
1442 break;
1443 }
1444 case ReadStripMethod:
1445 {
cristyd8192d32012-03-14 01:35:47 +00001446 register uint32
cristy3ed852e2009-09-05 21:47:34 +00001447 *p;
1448
1449 /*
1450 Convert stripped TIFF image to DirectClass MIFF image.
1451 */
1452 i=0;
cristyd8192d32012-03-14 01:35:47 +00001453 p=(uint32 *) NULL;
cristybb503372010-05-27 20:51:26 +00001454 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001455 {
cristybb503372010-05-27 20:51:26 +00001456 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001457 x;
1458
cristy4c08aed2011-07-01 19:47:50 +00001459 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001460 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001461
1462 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001463 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001464 break;
1465 if (i == 0)
1466 {
1467 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1468 break;
cristyeaedf062010-05-29 22:36:02 +00001469 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1470 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001471 }
1472 i--;
cristyd8192d32012-03-14 01:35:47 +00001473 p=((uint32 *) pixels)+image->columns*i;
cristybb503372010-05-27 20:51:26 +00001474 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001475 {
cristy4c08aed2011-07-01 19:47:50 +00001476 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1477 (TIFFGetR(*p))),q);
1478 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1479 (TIFFGetG(*p))),q);
1480 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1481 (TIFFGetB(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001482 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001483 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1484 (TIFFGetA(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001485 p++;
cristyed231572011-07-14 02:18:59 +00001486 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001487 }
1488 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1489 break;
1490 if (image->previous == (Image *) NULL)
1491 {
cristycee97112010-05-28 00:44:52 +00001492 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1493 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001494 if (status == MagickFalse)
1495 break;
1496 }
1497 }
1498 break;
1499 }
1500 case ReadTileMethod:
1501 {
1502 register uint32
1503 *p;
1504
1505 uint32
1506 *tile_pixels,
1507 columns,
1508 rows;
1509
cristybb503372010-05-27 20:51:26 +00001510 size_t
cristy3ed852e2009-09-05 21:47:34 +00001511 number_pixels;
1512
1513 /*
1514 Convert tiled TIFF image to DirectClass MIFF image.
1515 */
1516 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1517 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1518 {
1519 TIFFClose(tiff);
1520 ThrowReaderException(CoderError,"ImageIsNotTiled");
1521 }
cristyc82a27b2011-10-21 01:07:16 +00001522 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +00001523 number_pixels=columns*rows;
cristyda16f162011-02-19 23:52:17 +00001524 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
cristy3ed852e2009-09-05 21:47:34 +00001525 sizeof(*tile_pixels));
1526 if (tile_pixels == (uint32 *) NULL)
1527 {
1528 TIFFClose(tiff);
1529 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1530 }
cristybb503372010-05-27 20:51:26 +00001531 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001532 {
cristybb503372010-05-27 20:51:26 +00001533 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001534 x;
1535
cristy4c08aed2011-07-01 19:47:50 +00001536 register Quantum
1537 *restrict q,
1538 *restrict tile;
cristy3ed852e2009-09-05 21:47:34 +00001539
cristybb503372010-05-27 20:51:26 +00001540 size_t
cristy3ed852e2009-09-05 21:47:34 +00001541 columns_remaining,
1542 rows_remaining;
1543
1544 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001545 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001546 rows_remaining=rows;
1547 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1548 exception);
cristy4c08aed2011-07-01 19:47:50 +00001549 if (tile == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001550 break;
cristybb503372010-05-27 20:51:26 +00001551 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001552 {
cristybb503372010-05-27 20:51:26 +00001553 size_t
cristy3ed852e2009-09-05 21:47:34 +00001554 column,
1555 row;
1556
1557 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1558 break;
1559 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001560 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001561 columns_remaining=columns;
1562 p=tile_pixels+(rows-rows_remaining)*columns;
cristy68473222011-08-17 17:39:19 +00001563 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1564 x);
cristy3ed852e2009-09-05 21:47:34 +00001565 for (row=rows_remaining; row > 0; row--)
1566 {
1567 if (image->matte != MagickFalse)
1568 for (column=columns_remaining; column > 0; column--)
1569 {
cristy4c08aed2011-07-01 19:47:50 +00001570 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1571 TIFFGetR(*p)),q);
1572 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1573 TIFFGetG(*p)),q);
1574 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1575 TIFFGetB(*p)),q);
1576 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1577 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001578 p++;
cristyed231572011-07-14 02:18:59 +00001579 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001580 }
1581 else
1582 for (column=columns_remaining; column > 0; column--)
1583 {
cristy4c08aed2011-07-01 19:47:50 +00001584 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1585 TIFFGetR(*p)),q);
1586 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1587 TIFFGetG(*p)),q);
1588 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1589 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001590 p++;
cristyed231572011-07-14 02:18:59 +00001591 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001592 }
1593 p+=columns-columns_remaining;
cristyed231572011-07-14 02:18:59 +00001594 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
cristy3ed852e2009-09-05 21:47:34 +00001595 }
1596 }
1597 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1598 break;
1599 if (image->previous == (Image *) NULL)
1600 {
cristycee97112010-05-28 00:44:52 +00001601 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1602 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001603 if (status == MagickFalse)
1604 break;
1605 }
1606 }
1607 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1608 break;
1609 }
1610 case ReadGenericMethod:
1611 default:
1612 {
1613 register uint32
1614 *p;
1615
1616 uint32
1617 *pixels;
1618
1619 /*
1620 Convert TIFF image to DirectClass MIFF image.
1621 */
1622 number_pixels=(MagickSizeType) image->columns*image->rows;
1623 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1624 (number_pixels*sizeof(uint32))))
1625 {
1626 TIFFClose(tiff);
1627 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1628 }
1629 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1630 sizeof(uint32));
1631 if (pixels == (uint32 *) NULL)
1632 {
1633 TIFFClose(tiff);
1634 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1635 }
1636 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1637 (uint32) image->rows,(uint32 *) pixels,0);
1638 /*
1639 Convert image to DirectClass pixel packets.
1640 */
1641 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001642 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001643 {
cristybb503372010-05-27 20:51:26 +00001644 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001645 x;
1646
cristy4c08aed2011-07-01 19:47:50 +00001647 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001648 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001649
1650 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001651 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001652 break;
cristyed231572011-07-14 02:18:59 +00001653 q+=GetPixelChannels(image)*(image->columns-1);
cristybb503372010-05-27 20:51:26 +00001654 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001655 {
cristy4c08aed2011-07-01 19:47:50 +00001656 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1657 TIFFGetR(*p)),q);
1658 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1659 TIFFGetG(*p)),q);
1660 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1661 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001662 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001663 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1664 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001665 p--;
cristyed231572011-07-14 02:18:59 +00001666 q-=GetPixelChannels(image);;
cristy3ed852e2009-09-05 21:47:34 +00001667 }
1668 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1669 break;
1670 if (image->previous == (Image *) NULL)
1671 {
cristycee97112010-05-28 00:44:52 +00001672 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1673 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001674 if (status == MagickFalse)
1675 break;
1676 }
1677 }
1678 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1679 break;
1680 }
1681 }
1682 SetQuantumImageType(image,quantum_type);
1683 next_tiff_frame:
cristya9a6ec42011-10-26 16:55:01 +00001684 quantum_info=DestroyQuantumInfo(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +00001685 if ((photometric == PHOTOMETRIC_LOGL) ||
1686 (photometric == PHOTOMETRIC_MINISBLACK) ||
1687 (photometric == PHOTOMETRIC_MINISWHITE))
1688 {
1689 image->type=GrayscaleType;
1690 if (bits_per_sample == 1)
1691 image->type=BilevelType;
1692 }
1693 if (image->storage_class == PseudoClass)
1694 image->depth=GetImageDepth(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001695 if ((photometric == PHOTOMETRIC_LOGL) ||
1696 (photometric == PHOTOMETRIC_MINISBLACK) ||
1697 (photometric == PHOTOMETRIC_MINISWHITE))
1698 {
1699 image->type=GrayscaleType;
1700 if (bits_per_sample == 1)
1701 image->type=BilevelType;
1702 }
1703 /*
1704 Proceed to next image.
1705 */
1706 if (image_info->number_scenes != 0)
1707 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1708 break;
1709 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1710 if (status == MagickTrue)
1711 {
1712 /*
1713 Allocate next image structure.
1714 */
cristy9950d572011-10-01 18:22:35 +00001715 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001716 if (GetNextImageInList(image) == (Image *) NULL)
1717 {
1718 image=DestroyImageList(image);
1719 return((Image *) NULL);
1720 }
1721 image=SyncNextImageInList(image);
1722 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1723 image->scene);
1724 if (status == MagickFalse)
1725 break;
1726 }
cristy3ed852e2009-09-05 21:47:34 +00001727 } while (status == MagickTrue);
1728 (void) TIFFSetWarningHandler(warning_handler);
1729 (void) TIFFSetErrorHandler(error_handler);
1730 TIFFClose(tiff);
1731 return(GetFirstImageInList(image));
1732}
1733#endif
1734
1735/*
1736%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1737% %
1738% %
1739% %
1740% R e g i s t e r T I F F I m a g e %
1741% %
1742% %
1743% %
1744%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1745%
1746% RegisterTIFFImage() adds properties for the TIFF image format to
1747% the list of supported formats. The properties include the image format
1748% tag, a method to read and/or write the format, whether the format
1749% supports the saving of more than one frame to the same file or blob,
1750% whether the format supports native in-memory I/O, and a brief
1751% description of the format.
1752%
1753% The format of the RegisterTIFFImage method is:
1754%
cristybb503372010-05-27 20:51:26 +00001755% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001756%
1757*/
cristy7e3eb0a2011-11-09 22:50:49 +00001758
1759#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
1760static TIFFExtendProc
cristy60e1bf92011-11-09 23:00:15 +00001761 tag_extender = (TIFFExtendProc) NULL;
cristy7e3eb0a2011-11-09 22:50:49 +00001762
1763static void TIFFTagExtender(TIFF *tiff)
1764{
1765 static const TIFFFieldInfo
1766 TIFFExtensions[] =
1767 {
cristy3fac9ec2011-11-17 18:04:39 +00001768 {
1769 37724, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
1770 (char *) "PhotoshopLayerData"
1771 }
cristy7e3eb0a2011-11-09 22:50:49 +00001772 };
1773
1774 TIFFMergeFieldInfo(tiff,TIFFExtensions,sizeof(TIFFExtensions)/
1775 sizeof(*TIFFExtensions));
cristy60e1bf92011-11-09 23:00:15 +00001776 if (tag_extender != (TIFFExtendProc) NULL)
1777 (*tag_extender)(tiff);
cristy7e3eb0a2011-11-09 22:50:49 +00001778}
1779#endif
1780
cristybb503372010-05-27 20:51:26 +00001781ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001782{
1783#define TIFFDescription "Tagged Image File Format"
1784
1785 char
1786 version[MaxTextExtent];
1787
1788 MagickInfo
1789 *entry;
cristy6b032822010-06-29 16:52:32 +00001790
cristy18b17442009-10-25 18:36:48 +00001791 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1792 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001793 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001794 if (instantiate_key == MagickFalse)
1795 {
1796 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1797 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristy7e3eb0a2011-11-09 22:50:49 +00001798#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
cristy60e1bf92011-11-09 23:00:15 +00001799 if (tag_extender == (TIFFExtendProc) NULL)
1800 tag_extender=TIFFSetTagExtender(TIFFTagExtender);
cristy7e3eb0a2011-11-09 22:50:49 +00001801#endif
cristy3ed852e2009-09-05 21:47:34 +00001802 instantiate_key=MagickTrue;
1803 }
cristyf84a1932010-01-03 18:00:18 +00001804 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001805 *version='\0';
1806#if defined(TIFF_VERSION)
cristyb51dff52011-05-19 16:55:47 +00001807 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
cristy3ed852e2009-09-05 21:47:34 +00001808#endif
1809#if defined(MAGICKCORE_TIFF_DELEGATE)
1810 {
1811 const char
1812 *p;
1813
cristybb503372010-05-27 20:51:26 +00001814 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001815 i;
1816
1817 p=TIFFGetVersion();
1818 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1819 version[i]=(*p++);
1820 version[i]='\0';
1821 }
1822#endif
1823
cristy3d7f8062009-09-24 20:45:35 +00001824 entry=SetMagickInfo("GROUP4");
1825#if defined(MAGICKCORE_TIFF_DELEGATE)
1826 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1827 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1828#endif
1829 entry->raw=MagickTrue;
1830 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001831 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001832 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001833 entry->seekable_stream=MagickTrue;
1834 entry->thread_support=NoThreadSupport;
1835 entry->description=ConstantString("Raw CCITT Group4");
1836 entry->module=ConstantString("TIFF");
1837 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001838 entry=SetMagickInfo("PTIF");
1839#if defined(MAGICKCORE_TIFF_DELEGATE)
1840 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1841 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1842#endif
1843 entry->endian_support=MagickTrue;
1844 entry->seekable_stream=MagickTrue;
1845 entry->thread_support=NoThreadSupport;
1846 entry->description=ConstantString("Pyramid encoded TIFF");
1847 entry->module=ConstantString("TIFF");
1848 (void) RegisterMagickInfo(entry);
1849 entry=SetMagickInfo("TIF");
1850#if defined(MAGICKCORE_TIFF_DELEGATE)
1851 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1852 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1853#endif
1854 entry->endian_support=MagickTrue;
1855 entry->seekable_stream=MagickTrue;
1856 entry->stealth=MagickTrue;
1857 entry->thread_support=NoThreadSupport;
1858 entry->description=ConstantString(TIFFDescription);
1859 if (*version != '\0')
1860 entry->version=ConstantString(version);
1861 entry->module=ConstantString("TIFF");
1862 (void) RegisterMagickInfo(entry);
1863 entry=SetMagickInfo("TIFF");
1864#if defined(MAGICKCORE_TIFF_DELEGATE)
1865 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1866 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1867#endif
1868 entry->magick=(IsImageFormatHandler *) IsTIFF;
1869 entry->endian_support=MagickTrue;
1870 entry->seekable_stream=MagickTrue;
1871 entry->thread_support=NoThreadSupport;
1872 entry->description=ConstantString(TIFFDescription);
1873 if (*version != '\0')
1874 entry->version=ConstantString(version);
1875 entry->module=ConstantString("TIFF");
1876 (void) RegisterMagickInfo(entry);
1877 entry=SetMagickInfo("TIFF64");
1878#if defined(TIFF_VERSION_BIG)
1879 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
cristy9d6964e2011-11-10 17:48:45 +00001880 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
cristy3ed852e2009-09-05 21:47:34 +00001881#endif
1882 entry->adjoin=MagickFalse;
1883 entry->endian_support=MagickTrue;
1884 entry->seekable_stream=MagickTrue;
1885 entry->thread_support=NoThreadSupport;
1886 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1887 if (*version != '\0')
1888 entry->version=ConstantString(version);
1889 entry->module=ConstantString("TIFF");
1890 (void) RegisterMagickInfo(entry);
1891 return(MagickImageCoderSignature);
1892}
1893
1894/*
1895%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1896% %
1897% %
1898% %
1899% U n r e g i s t e r T I F F I m a g e %
1900% %
1901% %
1902% %
1903%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1904%
1905% UnregisterTIFFImage() removes format registrations made by the TIFF module
1906% from the list of supported formats.
1907%
1908% The format of the UnregisterTIFFImage method is:
1909%
1910% UnregisterTIFFImage(void)
1911%
1912*/
1913ModuleExport void UnregisterTIFFImage(void)
1914{
cristy3ed852e2009-09-05 21:47:34 +00001915 (void) UnregisterMagickInfo("TIFF64");
cristy9d6964e2011-11-10 17:48:45 +00001916 (void) UnregisterMagickInfo("TIFF");
1917 (void) UnregisterMagickInfo("TIF");
1918 (void) UnregisterMagickInfo("PTIF");
cristy514e9e72009-11-20 02:12:08 +00001919 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1920 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001921 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001922 if (instantiate_key != MagickFalse)
cristy9ebdd932011-11-09 23:07:28 +00001923 {
1924#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
1925 if (tag_extender == (TIFFExtendProc) NULL)
1926 (void) TIFFSetTagExtender(tag_extender);
1927#endif
1928 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1929 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1930 instantiate_key=MagickFalse;
1931 }
cristyf84a1932010-01-03 18:00:18 +00001932 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001933 DestroySemaphoreInfo(&tiff_semaphore);
1934}
1935
1936#if defined(MAGICKCORE_TIFF_DELEGATE)
1937/*
1938%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1939% %
1940% %
1941% %
cristy3d7f8062009-09-24 20:45:35 +00001942% W r i t e G R O U P 4 I m a g e %
1943% %
1944% %
1945% %
1946%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1947%
1948% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1949%
1950% The format of the WriteGROUP4Image method is:
1951%
1952% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001953% Image *image,ExceptionInfo *)
cristy3d7f8062009-09-24 20:45:35 +00001954%
1955% A description of each parameter follows:
1956%
1957% o image_info: the image info.
1958%
1959% o image: The image.
1960%
cristy3a37efd2011-08-28 20:31:03 +00001961% o exception: return any errors or warnings in this structure.
1962%
cristy3d7f8062009-09-24 20:45:35 +00001963*/
1964static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001965 Image *image,ExceptionInfo *exception)
cristy3d7f8062009-09-24 20:45:35 +00001966{
1967 char
1968 filename[MaxTextExtent];
1969
1970 FILE
1971 *file;
1972
1973 Image
1974 *huffman_image;
1975
1976 ImageInfo
1977 *write_info;
1978
1979 int
1980 unique_file;
1981
1982 MagickBooleanType
1983 status;
1984
cristybb503372010-05-27 20:51:26 +00001985 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001986 i;
1987
1988 ssize_t
1989 count;
1990
1991 TIFF
1992 *tiff;
1993
cristy94c8fe42009-10-06 01:57:36 +00001994 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001995 *byte_count,
1996 strip_size;
1997
1998 unsigned char
1999 *buffer;
2000
2001 /*
2002 Write image as CCITT Group4 TIFF image to a temporary file.
2003 */
2004 assert(image_info != (const ImageInfo *) NULL);
2005 assert(image_info->signature == MagickSignature);
2006 assert(image != (Image *) NULL);
2007 assert(image->signature == MagickSignature);
2008 if (image->debug != MagickFalse)
2009 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002010 assert(exception != (ExceptionInfo *) NULL);
2011 assert(exception->signature == MagickSignature);
2012 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3d7f8062009-09-24 20:45:35 +00002013 if (status == MagickFalse)
2014 return(status);
cristy3a37efd2011-08-28 20:31:03 +00002015 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3d7f8062009-09-24 20:45:35 +00002016 if (huffman_image == (Image *) NULL)
2017 {
2018 (void) CloseBlob(image);
2019 return(MagickFalse);
2020 }
cristy94c8fe42009-10-06 01:57:36 +00002021 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00002022 file=(FILE *) NULL;
2023 unique_file=AcquireUniqueFileResource(filename);
2024 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00002025 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00002026 if ((unique_file == -1) || (file == (FILE *) NULL))
2027 {
cristy3a37efd2011-08-28 20:31:03 +00002028 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2029 filename);
cristy3d7f8062009-09-24 20:45:35 +00002030 return(MagickFalse);
2031 }
cristyb51dff52011-05-19 16:55:47 +00002032 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
cristy3d7f8062009-09-24 20:45:35 +00002033 filename);
cristy018f07f2011-09-04 21:15:19 +00002034 (void) SetImageType(huffman_image,BilevelType,exception);
cristy14efd992009-09-26 23:46:03 +00002035 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00002036 SetImageInfoFile(write_info,file);
2037 write_info->compression=Group4Compression;
2038 write_info->type=BilevelType;
2039 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
cristy3a37efd2011-08-28 20:31:03 +00002040 status=WriteTIFFImage(write_info,huffman_image,exception);
cristy3d7f8062009-09-24 20:45:35 +00002041 (void) fflush(file);
2042 write_info=DestroyImageInfo(write_info);
2043 if (status == MagickFalse)
2044 {
cristy3d7f8062009-09-24 20:45:35 +00002045 huffman_image=DestroyImage(huffman_image);
2046 (void) fclose(file);
2047 (void) RelinquishUniqueFileResource(filename);
2048 return(MagickFalse);
2049 }
2050 tiff=TIFFOpen(filename,"rb");
2051 if (tiff == (TIFF *) NULL)
2052 {
2053 huffman_image=DestroyImage(huffman_image);
2054 (void) fclose(file);
2055 (void) RelinquishUniqueFileResource(filename);
cristy3a37efd2011-08-28 20:31:03 +00002056 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
cristy3d7f8062009-09-24 20:45:35 +00002057 image_info->filename);
2058 return(MagickFalse);
2059 }
2060 /*
2061 Allocate raw strip buffer.
2062 */
cristy94c8fe42009-10-06 01:57:36 +00002063 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2064 {
2065 TIFFClose(tiff);
2066 huffman_image=DestroyImage(huffman_image);
2067 (void) fclose(file);
2068 (void) RelinquishUniqueFileResource(filename);
2069 return(MagickFalse);
2070 }
cristy3d7f8062009-09-24 20:45:35 +00002071 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00002072 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002073 if (byte_count[i] > strip_size)
2074 strip_size=byte_count[i];
2075 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2076 sizeof(*buffer));
2077 if (buffer == (unsigned char *) NULL)
2078 {
2079 TIFFClose(tiff);
2080 huffman_image=DestroyImage(huffman_image);
2081 (void) fclose(file);
2082 (void) RelinquishUniqueFileResource(filename);
2083 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2084 image_info->filename);
2085 }
2086 /*
2087 Compress runlength encoded to 2D Huffman pixels.
2088 */
cristybb503372010-05-27 20:51:26 +00002089 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002090 {
cristy94c8fe42009-10-06 01:57:36 +00002091 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00002092 if (WriteBlob(image,(size_t) count,buffer) != count)
2093 status=MagickFalse;
2094 }
2095 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2096 TIFFClose(tiff);
2097 huffman_image=DestroyImage(huffman_image);
2098 (void) fclose(file);
2099 (void) RelinquishUniqueFileResource(filename);
2100 (void) CloseBlob(image);
2101 return(status);
2102}
2103#endif
2104
2105#if defined(MAGICKCORE_TIFF_DELEGATE)
2106/*
2107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2108% %
2109% %
2110% %
cristy3ed852e2009-09-05 21:47:34 +00002111% W r i t e P T I F I m a g e %
2112% %
2113% %
2114% %
2115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2116%
2117% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2118% format.
2119%
2120% The format of the WritePTIFImage method is:
2121%
2122% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002123% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002124%
2125% A description of each parameter follows:
2126%
2127% o image_info: the image info.
2128%
2129% o image: The image.
2130%
cristy3a37efd2011-08-28 20:31:03 +00002131% o exception: return any errors or warnings in this structure.
2132%
cristy3ed852e2009-09-05 21:47:34 +00002133*/
2134static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002135 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002136{
2137 Image
2138 *images,
2139 *next,
2140 *pyramid_image;
2141
2142 ImageInfo
2143 *write_info;
2144
2145 MagickBooleanType
2146 status;
2147
cristyf164a872012-01-20 18:26:34 +00002148 PointInfo
2149 resolution;
2150
cristybb503372010-05-27 20:51:26 +00002151 size_t
cristy3ed852e2009-09-05 21:47:34 +00002152 columns,
2153 rows;
2154
2155 /*
2156 Create pyramid-encoded TIFF image.
2157 */
2158 images=NewImageList();
2159 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2160 {
cristy3a37efd2011-08-28 20:31:03 +00002161 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,exception));
cristy3ed852e2009-09-05 21:47:34 +00002162 columns=next->columns;
2163 rows=next->rows;
cristyf164a872012-01-20 18:26:34 +00002164 resolution=next->resolution;
cristy3ed852e2009-09-05 21:47:34 +00002165 while ((columns > 64) && (rows > 64))
2166 {
2167 columns/=2;
2168 rows/=2;
cristyf164a872012-01-20 18:26:34 +00002169 resolution.x/=2;
2170 resolution.y/=2;
cristyaa2c16c2012-03-25 22:21:35 +00002171 pyramid_image=ResizeImage(next,columns,rows,image->filter,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);
cristy3a37efd2011-08-28 20:31:03 +00002596 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00002597 if (status == MagickFalse)
2598 return(status);
cristy3a37efd2011-08-28 20:31:03 +00002599 (void) MagickSetThreadValue(tiff_exception,exception);
cristy3ed852e2009-09-05 21:47:34 +00002600 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2601 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002602 endian_type=UndefinedEndian;
cristyc4debe62012-02-09 13:51:53 +00002603 option=GetImageArtifact(image,"tiff:endian");
cristy6b032822010-06-29 16:52:32 +00002604 if (option != (const char *) NULL)
2605 {
2606 if (LocaleNCompare(option,"msb",3) == 0)
2607 endian_type=MSBEndian;
2608 if (LocaleNCompare(option,"lsb",3) == 0)
2609 endian_type=LSBEndian;;
2610 }
2611 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002612 {
2613 case LSBEndian: mode="wl"; break;
2614 case MSBEndian: mode="wb"; break;
2615 default: mode="w"; break;
2616 }
2617#if defined(TIFF_VERSION_BIG)
2618 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002619 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002620 {
2621 case LSBEndian: mode="wl8"; break;
2622 case MSBEndian: mode="wb8"; break;
2623 default: mode="w8"; break;
2624 }
2625#endif
2626 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2627 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2628 TIFFUnmapBlob);
2629 if (tiff == (TIFF *) NULL)
2630 {
2631 (void) TIFFSetWarningHandler(warning_handler);
2632 (void) TIFFSetErrorHandler(error_handler);
2633 return(MagickFalse);
2634 }
2635 scene=0;
2636 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +00002637 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +00002638 do
2639 {
2640 /*
2641 Initialize TIFF fields.
2642 */
cristy5f1c1ff2010-12-23 21:38:06 +00002643 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002644 (image_info->type != OptimizeType))
cristy018f07f2011-09-04 21:15:19 +00002645 (void) SetImageType(image,image_info->type,exception);
cristy3ed852e2009-09-05 21:47:34 +00002646 quantum_info=AcquireQuantumInfo(image_info,image);
2647 if (quantum_info == (QuantumInfo *) NULL)
2648 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2649 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2650 (quantum_info->format == UndefinedQuantumFormat) &&
cristy3a37efd2011-08-28 20:31:03 +00002651 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002652 {
2653 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2654 if (status == MagickFalse)
2655 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2656 }
2657 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2658 (GetPreviousImageInList(image) != (Image *) NULL))
2659 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2660 if ((image->columns != (uint32) image->columns) ||
2661 (image->rows != (uint32) image->rows))
2662 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2663 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2664 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2665 compression=image->compression;
2666 if (image_info->compression != UndefinedCompression)
2667 compression=image_info->compression;
2668 switch (compression)
2669 {
2670 case FaxCompression:
2671 {
2672 compress_tag=COMPRESSION_CCITTFAX3;
2673 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2674 break;
2675 }
2676 case Group4Compression:
2677 {
2678 compress_tag=COMPRESSION_CCITTFAX4;
2679 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2680 break;
2681 }
cristy6d5e20f2011-04-25 13:48:54 +00002682#if defined(COMPRESSION_JBIG)
2683 case JBIG1Compression:
2684 {
2685 compress_tag=COMPRESSION_JBIG;
2686 break;
2687 }
2688#endif
cristy3ed852e2009-09-05 21:47:34 +00002689 case JPEGCompression:
2690 {
2691 compress_tag=COMPRESSION_JPEG;
2692 break;
2693 }
cristyfbb0ef02010-12-19 02:32:11 +00002694#if defined(COMPRESSION_LZMA)
2695 case LZMACompression:
2696 {
2697 compress_tag=COMPRESSION_LZMA;
2698 break;
2699 }
2700#endif
cristy3ed852e2009-09-05 21:47:34 +00002701 case LZWCompression:
2702 {
2703 compress_tag=COMPRESSION_LZW;
2704 break;
2705 }
2706 case RLECompression:
2707 {
2708 compress_tag=COMPRESSION_PACKBITS;
2709 break;
2710 }
2711 case ZipCompression:
2712 {
2713 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2714 break;
2715 }
2716 case NoCompression:
2717 default:
2718 {
2719 compress_tag=COMPRESSION_NONE;
2720 break;
2721 }
2722 }
cristy5b675872009-10-21 13:12:00 +00002723#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2724 if ((compress_tag != COMPRESSION_NONE) &&
2725 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002726 {
cristy3a37efd2011-08-28 20:31:03 +00002727 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2728 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002729 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002730 compress_tag=COMPRESSION_NONE;
2731 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002732 }
2733#else
2734 switch (compress_tag)
2735 {
2736#if defined(CCITT_SUPPORT)
2737 case COMPRESSION_CCITTFAX3:
2738 case COMPRESSION_CCITTFAX4:
2739#endif
2740#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2741 case COMPRESSION_JPEG:
2742#endif
cristyfbb0ef02010-12-19 02:32:11 +00002743#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2744 case COMPRESSION_LZMA:
2745#endif
cristy3ed852e2009-09-05 21:47:34 +00002746#if defined(LZW_SUPPORT)
2747 case COMPRESSION_LZW:
2748#endif
2749#if defined(PACKBITS_SUPPORT)
2750 case COMPRESSION_PACKBITS:
2751#endif
2752#if defined(ZIP_SUPPORT)
2753 case COMPRESSION_ADOBE_DEFLATE:
2754#endif
2755 case COMPRESSION_NONE:
2756 break;
2757 default:
2758 {
cristy3a37efd2011-08-28 20:31:03 +00002759 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2760 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002761 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002762 compress_tag=COMPRESSION_NONE;
2763 compression=NoCompression;
2764 break;
2765 }
2766 }
2767#endif
2768 switch (compression)
2769 {
2770 case FaxCompression:
2771 case Group4Compression:
2772 {
cristy018f07f2011-09-04 21:15:19 +00002773 (void) SetImageType(image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +00002774 break;
2775 }
2776 case JPEGCompression:
2777 {
cristy3a37efd2011-08-28 20:31:03 +00002778 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a11cb12011-10-19 23:53:34 +00002779 (void) SetImageDepth(image,8,exception);
cristy3ed852e2009-09-05 21:47:34 +00002780 break;
2781 }
2782 default:
2783 break;
2784 }
cristy3ed852e2009-09-05 21:47:34 +00002785 if (image->colorspace == CMYKColorspace)
2786 {
2787 photometric=PHOTOMETRIC_SEPARATED;
2788 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2789 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2790 }
2791 else
2792 {
2793 /*
2794 Full color TIFF raster.
2795 */
2796 if (image->colorspace == LabColorspace)
2797 photometric=PHOTOMETRIC_CIELAB;
2798 else
2799 if (image->colorspace == YCbCrColorspace)
2800 {
2801 photometric=PHOTOMETRIC_YCBCR;
2802 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy3a37efd2011-08-28 20:31:03 +00002803 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a11cb12011-10-19 23:53:34 +00002804 (void) SetImageDepth(image,8,exception);
cristy3ed852e2009-09-05 21:47:34 +00002805 }
2806 else
2807 {
cristy510d06a2011-07-06 23:43:54 +00002808 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy8d951092012-02-08 18:54:56 +00002809 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00002810 photometric=PHOTOMETRIC_RGB;
2811 }
2812 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2813 if ((image_info->type != TrueColorType) &&
2814 (image_info->type != TrueColorMatteType))
2815 {
cristy94c8fe42009-10-06 01:57:36 +00002816 if ((image_info->type != PaletteType) &&
cristy3a37efd2011-08-28 20:31:03 +00002817 (IsImageGray(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002818 {
cristy94c8fe42009-10-06 01:57:36 +00002819 photometric=(uint16) (quantum_info->min_is_white !=
2820 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2821 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002822 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002823 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy3a37efd2011-08-28 20:31:03 +00002824 (IsImageMonochrome(image,exception) != MagickFalse))
cristy94c8fe42009-10-06 01:57:36 +00002825 {
2826 status=SetQuantumDepth(image,quantum_info,1);
2827 if (status == MagickFalse)
2828 ThrowWriterException(ResourceLimitError,
2829 "MemoryAllocationFailed");
2830 }
cristy3ed852e2009-09-05 21:47:34 +00002831 }
2832 else
cristy94c8fe42009-10-06 01:57:36 +00002833 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002834 {
cristybb503372010-05-27 20:51:26 +00002835 size_t
cristy94c8fe42009-10-06 01:57:36 +00002836 depth;
2837
2838 /*
2839 Colormapped TIFF raster.
2840 */
cristy3ed852e2009-09-05 21:47:34 +00002841 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002842 photometric=PHOTOMETRIC_PALETTE;
2843 depth=1;
2844 while ((GetQuantumRange(depth)+1) < image->colors)
2845 depth<<=1;
2846 status=SetQuantumDepth(image,quantum_info,depth);
2847 if (status == MagickFalse)
2848 ThrowWriterException(ResourceLimitError,
2849 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002850 }
2851 }
2852 }
cristyf2687ca2010-06-29 16:32:38 +00002853 switch (image->endian)
2854 {
2855 case LSBEndian:
2856 {
2857 endian=FILLORDER_LSB2MSB;
2858 break;
2859 }
2860 case MSBEndian:
2861 {
2862 endian=FILLORDER_MSB2LSB;
2863 break;
2864 }
2865 case UndefinedEndian:
2866 default:
2867 {
2868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2869 break;
2870 }
2871 }
cristydaff8092010-04-22 14:50:53 +00002872 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2873 (photometric != PHOTOMETRIC_MINISWHITE))
2874 {
2875 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002876 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002877 }
cristy0accf6e2009-10-01 13:14:28 +00002878 else
cristydaff8092010-04-22 14:50:53 +00002879 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2880 (photometric != PHOTOMETRIC_MINISWHITE))
2881 {
2882 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002883 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002884 }
cristyc4debe62012-02-09 13:51:53 +00002885 option=GetImageArtifact(image,"tiff:fill-order");
cristy62e282b2010-06-29 01:27:13 +00002886 if (option != (const char *) NULL)
2887 {
2888 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002889 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002890 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002891 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002892 }
cristyf2687ca2010-06-29 16:32:38 +00002893 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2894 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002895 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2896 if (image->matte != MagickFalse)
2897 {
2898 uint16
2899 extra_samples,
2900 sample_info[1],
2901 samples_per_pixel;
2902
2903 /*
2904 TIFF has a matte channel.
2905 */
2906 extra_samples=1;
2907 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
cristyc4debe62012-02-09 13:51:53 +00002908 option=GetImageArtifact(image,"tiff:alpha");
cristy3ed852e2009-09-05 21:47:34 +00002909 if ((option != (const char *) NULL) &&
2910 (LocaleCompare(option,"associated") == 0))
2911 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2912 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2913 &samples_per_pixel);
2914 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2915 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2916 &sample_info);
2917 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2918 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2919 }
2920 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2921 switch (quantum_info->format)
2922 {
2923 case FloatingPointQuantumFormat:
2924 {
2925 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2926 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2927 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2928 break;
2929 }
2930 case SignedQuantumFormat:
2931 {
2932 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2933 break;
2934 }
2935 case UnsignedQuantumFormat:
2936 {
2937 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2938 break;
2939 }
2940 default:
2941 break;
2942 }
cristy3ed852e2009-09-05 21:47:34 +00002943 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2944 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2945 if (photometric == PHOTOMETRIC_RGB)
2946 if ((image_info->interlace == PlaneInterlace) ||
2947 (image_info->interlace == PartitionInterlace))
2948 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2949 rows_per_strip=1;
2950 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002951 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2952 1);
cristyc4debe62012-02-09 13:51:53 +00002953 option=GetImageArtifact(image,"tiff:rows-per-strip");
cristy3ed852e2009-09-05 21:47:34 +00002954 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002955 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002956 switch (compress_tag)
2957 {
2958 case COMPRESSION_JPEG:
2959 {
2960#if defined(JPEG_SUPPORT)
2961 const char
2962 *sampling_factor;
2963
2964 GeometryInfo
2965 geometry_info;
2966
2967 MagickStatusType
2968 flags;
2969
2970 rows_per_strip+=(16-(rows_per_strip % 16));
2971 if (image->quality != 0)
2972 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2973 if (image_info->quality != UndefinedCompressionQuality)
2974 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2975 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
cristy510d06a2011-07-06 23:43:54 +00002976 if (IsRGBColorspace(image->colorspace) == MagickTrue)
cristy3ed852e2009-09-05 21:47:34 +00002977 {
cristy949bf5b2010-05-08 02:47:03 +00002978 const char
2979 *value;
2980
cristy3ed852e2009-09-05 21:47:34 +00002981 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2982 sampling_factor=(const char *) NULL;
cristyd15e6592011-10-15 00:13:06 +00002983 value=GetImageProperty(image,"jpeg:sampling-factor",exception);
cristy3ed852e2009-09-05 21:47:34 +00002984 if (value != (char *) NULL)
2985 {
2986 sampling_factor=value;
2987 if (image->debug != MagickFalse)
2988 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2989 " Input sampling-factors=%s",sampling_factor);
2990 }
2991 if (image_info->sampling_factor != (char *) NULL)
2992 sampling_factor=image_info->sampling_factor;
2993 if (sampling_factor != (const char *) NULL)
2994 {
2995 flags=ParseGeometry(sampling_factor,&geometry_info);
2996 if ((flags & SigmaValue) == 0)
2997 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002998 if (image->colorspace == YCbCrColorspace)
2999 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
3000 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00003001 }
3002 }
3003 if (bits_per_sample == 12)
3004 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
3005#endif
3006 break;
3007 }
3008 case COMPRESSION_ADOBE_DEFLATE:
3009 {
cristyf6fe0a12010-05-30 00:44:47 +00003010 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003011 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3012 &bits_per_sample);
3013 if (((photometric == PHOTOMETRIC_RGB) ||
3014 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3015 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00003016 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00003017 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
3018 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00003019 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00003020 break;
3021 }
3022 case COMPRESSION_CCITTFAX3:
3023 {
3024 /*
3025 Byte-aligned EOL.
3026 */
cristyeaedf062010-05-29 22:36:02 +00003027 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003028 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
3029 break;
3030 }
3031 case COMPRESSION_CCITTFAX4:
3032 {
cristyeaedf062010-05-29 22:36:02 +00003033 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003034 break;
3035 }
cristyef8f26b2010-12-19 20:29:16 +00003036#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3037 case COMPRESSION_LZMA:
3038 {
3039 if (((photometric == PHOTOMETRIC_RGB) ||
3040 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3041 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3042 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3043 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
3044 image_info->quality == UndefinedCompressionQuality ? 7 :
3045 MagickMin((ssize_t) image_info->quality/10,9)));
3046 break;
3047 }
3048#endif
cristy3ed852e2009-09-05 21:47:34 +00003049 case COMPRESSION_LZW:
3050 {
3051 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3052 &bits_per_sample);
3053 if (((photometric == PHOTOMETRIC_RGB) ||
3054 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3055 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00003056 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00003057 break;
3058 }
3059 default:
3060 break;
3061 }
cristycd0f0882012-02-09 13:59:14 +00003062 option=GetImageArtifact(image,"tiff:rows-per-strip");
cristy79e5dad2010-09-16 19:48:33 +00003063 if (option == (const char *) NULL)
3064 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy2a11bef2011-10-28 18:33:11 +00003065 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
cristy3ed852e2009-09-05 21:47:34 +00003066 {
3067 unsigned short
3068 units;
3069
3070 /*
3071 Set image resolution.
3072 */
3073 units=RESUNIT_NONE;
3074 if (image->units == PixelsPerInchResolution)
3075 units=RESUNIT_INCH;
3076 if (image->units == PixelsPerCentimeterResolution)
3077 units=RESUNIT_CENTIMETER;
3078 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
cristy2a11bef2011-10-28 18:33:11 +00003079 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x);
3080 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00003081 if ((image->page.x != 0) || (image->page.y != 0))
3082 {
3083 /*
3084 Set image position.
3085 */
3086 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
cristy2a11bef2011-10-28 18:33:11 +00003087 image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00003088 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
cristy2a11bef2011-10-28 18:33:11 +00003089 image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00003090 }
3091 }
3092 if (image->chromaticity.white_point.x != 0.0)
3093 {
3094 float
3095 chromaticity[6];
3096
3097 /*
3098 Set image chromaticity.
3099 */
3100 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3101 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3102 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3103 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3104 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3105 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3106 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3107 chromaticity[0]=(float) image->chromaticity.white_point.x;
3108 chromaticity[1]=(float) image->chromaticity.white_point.y;
3109 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3110 }
cristy59788de2011-11-23 13:56:19 +00003111 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3112 (image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
cristy3ed852e2009-09-05 21:47:34 +00003113 {
3114 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3115 if (image->scene != 0)
3116 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3117 GetImageListLength(image));
3118 }
3119 if (image->orientation != UndefinedOrientation)
3120 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3121 (void) TIFFSetProfiles(tiff,image);
3122 {
3123 uint16
3124 page,
3125 pages;
3126
3127 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00003128 pages=(uint16) GetImageListLength(image);
cristy59788de2011-11-23 13:56:19 +00003129 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3130 (image_info->adjoin != MagickFalse) && (pages > 1))
cristy3ed852e2009-09-05 21:47:34 +00003131 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3132 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3133 }
cristyd15e6592011-10-15 00:13:06 +00003134 (void) TIFFSetProperties(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003135 if (0)
cristyd15e6592011-10-15 00:13:06 +00003136 (void) TIFFSetEXIFProperties(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003137 /*
3138 Write image scanlines.
3139 */
3140 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3141 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristyf164a872012-01-20 18:26:34 +00003142 quantum_info->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003143 pixels=GetQuantumPixels(quantum_info);
3144 tiff_info.scanline=GetQuantumPixels(quantum_info);
3145 switch (photometric)
3146 {
3147 case PHOTOMETRIC_CIELAB:
3148 case PHOTOMETRIC_YCBCR:
3149 case PHOTOMETRIC_RGB:
3150 {
3151 /*
3152 RGB TIFF image.
3153 */
3154 switch (image_info->interlace)
3155 {
3156 case NoInterlace:
3157 default:
3158 {
3159 quantum_type=RGBQuantum;
3160 if (image->matte != MagickFalse)
3161 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003162 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003163 {
cristy4c08aed2011-07-01 19:47:50 +00003164 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003165 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003166
cristy3a37efd2011-08-28 20:31:03 +00003167 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003168 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003169 break;
cristy4c08aed2011-07-01 19:47:50 +00003170 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003171 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00003172 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00003173 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3174 break;
3175 if (image->previous == (Image *) NULL)
3176 {
cristy2837bcc2010-08-07 23:57:39 +00003177 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3178 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003179 if (status == MagickFalse)
3180 break;
3181 }
3182 }
3183 break;
3184 }
3185 case PlaneInterlace:
3186 case PartitionInterlace:
3187 {
3188 /*
3189 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3190 */
cristybb503372010-05-27 20:51:26 +00003191 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003192 {
cristy4c08aed2011-07-01 19:47:50 +00003193 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003194 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003195
cristy3a37efd2011-08-28 20:31:03 +00003196 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003197 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003198 break;
cristy4c08aed2011-07-01 19:47:50 +00003199 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003200 RedQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003201 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3202 break;
3203 }
3204 if (image->previous == (Image *) NULL)
3205 {
3206 status=SetImageProgress(image,SaveImageTag,100,400);
3207 if (status == MagickFalse)
3208 break;
3209 }
cristybb503372010-05-27 20:51:26 +00003210 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003211 {
cristy4c08aed2011-07-01 19:47:50 +00003212 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003213 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003214
cristy3a37efd2011-08-28 20:31:03 +00003215 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003216 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003217 break;
cristy4c08aed2011-07-01 19:47:50 +00003218 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003219 GreenQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003220 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3221 break;
3222 }
3223 if (image->previous == (Image *) NULL)
3224 {
3225 status=SetImageProgress(image,SaveImageTag,200,400);
3226 if (status == MagickFalse)
3227 break;
3228 }
cristybb503372010-05-27 20:51:26 +00003229 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003230 {
cristy4c08aed2011-07-01 19:47:50 +00003231 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003232 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003233
cristy3a37efd2011-08-28 20:31:03 +00003234 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003235 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003236 break;
cristy4c08aed2011-07-01 19:47:50 +00003237 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003238 BlueQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003239 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3240 break;
3241 }
3242 if (image->previous == (Image *) NULL)
3243 {
3244 status=SetImageProgress(image,SaveImageTag,300,400);
3245 if (status == MagickFalse)
3246 break;
3247 }
3248 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003249 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003250 {
cristy4c08aed2011-07-01 19:47:50 +00003251 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003252 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003253
cristy3a37efd2011-08-28 20:31:03 +00003254 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003255 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003256 break;
cristy4c08aed2011-07-01 19:47:50 +00003257 length=ExportQuantumPixels(image,(CacheView *) NULL,
cristy3a37efd2011-08-28 20:31:03 +00003258 quantum_info,AlphaQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003259 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3260 break;
3261 }
3262 if (image->previous == (Image *) NULL)
3263 {
3264 status=SetImageProgress(image,SaveImageTag,400,400);
3265 if (status == MagickFalse)
3266 break;
3267 }
3268 break;
3269 }
3270 }
3271 break;
3272 }
3273 case PHOTOMETRIC_SEPARATED:
3274 {
3275 /*
3276 CMYK TIFF image.
3277 */
3278 quantum_type=CMYKQuantum;
3279 if (image->matte != MagickFalse)
3280 quantum_type=CMYKAQuantum;
3281 if (image->colorspace != CMYKColorspace)
cristye941a752011-10-15 01:52:48 +00003282 (void) TransformImageColorspace(image,CMYKColorspace,exception);
cristybb503372010-05-27 20:51:26 +00003283 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003284 {
cristy4c08aed2011-07-01 19:47:50 +00003285 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003286 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003287
cristy3a37efd2011-08-28 20:31:03 +00003288 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003289 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003290 break;
cristy4c08aed2011-07-01 19:47:50 +00003291 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003292 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003293 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3294 break;
3295 if (image->previous == (Image *) NULL)
3296 {
cristycee97112010-05-28 00:44:52 +00003297 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3298 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003299 if (status == MagickFalse)
3300 break;
3301 }
3302 }
3303 break;
3304 }
3305 case PHOTOMETRIC_PALETTE:
3306 {
3307 uint16
3308 *blue,
3309 *green,
3310 *red;
3311
3312 /*
3313 Colormapped TIFF image.
3314 */
3315 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3316 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3317 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3318 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3319 (blue == (uint16 *) NULL))
3320 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3321 /*
3322 Initialize TIFF colormap.
3323 */
3324 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3325 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3326 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003327 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003328 {
3329 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3330 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3331 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3332 }
3333 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3334 red=(uint16 *) RelinquishMagickMemory(red);
3335 green=(uint16 *) RelinquishMagickMemory(green);
3336 blue=(uint16 *) RelinquishMagickMemory(blue);
3337 }
3338 default:
3339 {
3340 /*
3341 Convert PseudoClass packets to contiguous grayscale scanlines.
3342 */
3343 quantum_type=IndexQuantum;
3344 if (image->matte != MagickFalse)
3345 {
3346 if (photometric != PHOTOMETRIC_PALETTE)
3347 quantum_type=GrayAlphaQuantum;
3348 else
3349 quantum_type=IndexAlphaQuantum;
3350 }
3351 else
3352 if (photometric != PHOTOMETRIC_PALETTE)
3353 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003354 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003355 {
cristy4c08aed2011-07-01 19:47:50 +00003356 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003357 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003358
cristy3a37efd2011-08-28 20:31:03 +00003359 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003360 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003361 break;
cristy4c08aed2011-07-01 19:47:50 +00003362 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003363 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003364 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3365 break;
3366 if (image->previous == (Image *) NULL)
3367 {
cristycee97112010-05-28 00:44:52 +00003368 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3369 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003370 if (status == MagickFalse)
3371 break;
3372 }
3373 }
3374 break;
3375 }
3376 }
3377 quantum_info=DestroyQuantumInfo(quantum_info);
3378 DestroyTIFFInfo(&tiff_info);
3379 if (0 && (image_info->verbose == MagickTrue))
3380 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3381 (void) TIFFWriteDirectory(tiff);
cristy3ed852e2009-09-05 21:47:34 +00003382 image=SyncNextImageInList(image);
3383 if (image == (Image *) NULL)
3384 break;
3385 status=SetImageProgress(image,SaveImagesTag,scene++,
3386 GetImageListLength(image));
3387 if (status == MagickFalse)
3388 break;
3389 } while (image_info->adjoin != MagickFalse);
3390 (void) TIFFSetWarningHandler(warning_handler);
3391 (void) TIFFSetErrorHandler(error_handler);
3392 TIFFClose(tiff);
3393 return(MagickTrue);
3394}
3395#endif