blob: 0645dfb415aab5704e1706394ef86ef9128a109f [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% TTTTT IIIII FFFFF FFFFF %
7% T I F F %
8% T I FFF FFF %
9% T I F F %
10% T IIIII F F %
11% %
12% %
13% Read/Write TIFF Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/attribute.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colormap.h"
50#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/constitute.h"
53#include "MagickCore/enhance.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image.h"
58#include "MagickCore/image-private.h"
59#include "MagickCore/list.h"
60#include "MagickCore/log.h"
61#include "MagickCore/magick.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/module.h"
64#include "MagickCore/monitor.h"
65#include "MagickCore/monitor-private.h"
66#include "MagickCore/option.h"
67#include "MagickCore/pixel-accessor.h"
68#include "MagickCore/property.h"
69#include "MagickCore/quantum.h"
70#include "MagickCore/quantum-private.h"
71#include "MagickCore/profile.h"
72#include "MagickCore/resize.h"
73#include "MagickCore/resource_.h"
74#include "MagickCore/semaphore.h"
75#include "MagickCore/splay-tree.h"
76#include "MagickCore/static.h"
77#include "MagickCore/statistic.h"
78#include "MagickCore/string_.h"
79#include "MagickCore/string-private.h"
80#include "MagickCore/thread_.h"
81#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000082#if defined(MAGICKCORE_TIFF_DELEGATE)
83# if defined(MAGICKCORE_HAVE_TIFFCONF_H)
84# include "tiffconf.h"
cristyef8f26b2010-12-19 20:29:16 +000085# endif
cristy3ed852e2009-09-05 21:47:34 +000086# include "tiff.h"
87# include "tiffio.h"
88# if !defined(COMPRESSION_ADOBE_DEFLATE)
89# define COMPRESSION_ADOBE_DEFLATE 8
90# endif
cristyef8f26b2010-12-19 20:29:16 +000091# if !defined(PREDICTOR_HORIZONTAL)
92# define PREDICTOR_HORIZONTAL 2
93# endif
cristy98ca0f52011-10-08 23:19:10 +000094# if !defined(TIFFTAG_COPYRIGHT)
95# define TIFFTAG_COPYRIGHT 33432
96# endif
97# if !defined(TIFFTAG_OPIIMAGEID)
98# define TIFF_OPIIMAGEID 32781
99# endif
cristy3ed852e2009-09-05 21:47:34 +0000100
101/*
102 Typedef declarations.
103*/
104#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
105typedef struct _ExifInfo
106{
107 unsigned int
108 tag,
109 type;
110
111 const char
112 *property;
113} ExifInfo;
114
115static const ExifInfo
116 exif_info[] = {
117 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
118 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
119 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
120 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000121 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy7992e402011-09-16 15:24:22 +0000122 { EXIFTAG_OECF, TIFF_NOTYPE, "exif:OptoelectricConversionFactor" },
123 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, "exif:ExifVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000124 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
125 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000126 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, "exif:ComponentsConfiguration" },
cristy3ed852e2009-09-05 21:47:34 +0000127 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
128 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
129 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
130 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
131 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
132 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
133 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
134 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
135 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
136 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
137 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
cristy7992e402011-09-16 15:24:22 +0000138 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, "exif:SubjectArea" },
139 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, "exif:MakerNote" },
140 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, "exif:UserComment" },
cristy3ed852e2009-09-05 21:47:34 +0000141 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
142 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
143 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000144 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, "exif:FlashpixVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000145 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
146 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
147 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
148 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
149 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
150 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
cristy7992e402011-09-16 15:24:22 +0000151 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, "exif:SpatialFrequencyResponse" },
cristy3ed852e2009-09-05 21:47:34 +0000152 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
153 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
154 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
155 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
156 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
157 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
cristy7992e402011-09-16 15:24:22 +0000158 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, "exif:FileSource" },
159 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, "exif:SceneType" },
160 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, "exif:CFAPattern" },
cristy3ed852e2009-09-05 21:47:34 +0000161 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
162 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
163 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
164 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
165 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
166 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
167 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
168 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
169 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
170 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
cristy7992e402011-09-16 15:24:22 +0000171 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, "exif:DeviceSettingDescription" },
cristy3ed852e2009-09-05 21:47:34 +0000172 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
173 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
174 { 0, 0, (char *) NULL }
175};
176#endif
cristy0805e062011-01-30 02:56:36 +0000177#endif /* MAGICKCORE_TIFF_DELEGATE */
cristy3ed852e2009-09-05 21:47:34 +0000178
179/*
180 Global declarations.
181*/
182static MagickThreadKey
183 tiff_exception;
184
185static SemaphoreInfo
186 *tiff_semaphore = (SemaphoreInfo *) NULL;
187
188static volatile MagickBooleanType
189 instantiate_key = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000190
191/*
192 Forward declarations.
193*/
194#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000195static Image *
196 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
197
cristy3ed852e2009-09-05 21:47:34 +0000198static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +0000199 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
200 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
201 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000202#endif
203
204/*
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206% %
207% %
208% %
209% I s T I F F %
210% %
211% %
212% %
213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214%
215% IsTIFF() returns MagickTrue if the image format type, identified by the
216% magick string, is TIFF.
217%
218% The format of the IsTIFF method is:
219%
220% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
221%
222% A description of each parameter follows:
223%
224% o magick: compare image format pattern against these bytes.
225%
226% o length: Specifies the length of the magick string.
227%
228*/
229static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
230{
231 if (length < 4)
232 return(MagickFalse);
233 if (memcmp(magick,"\115\115\000\052",4) == 0)
234 return(MagickTrue);
235 if (memcmp(magick,"\111\111\052\000",4) == 0)
236 return(MagickTrue);
237#if defined(TIFF_VERSION_BIG)
238 if (length < 8)
239 return(MagickFalse);
240 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
241 return(MagickTrue);
242 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
243 return(MagickTrue);
244#endif
245 return(MagickFalse);
246}
247
248#if defined(MAGICKCORE_TIFF_DELEGATE)
249/*
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251% %
252% %
253% %
cristy3d7f8062009-09-24 20:45:35 +0000254% R e a d G R O U P 4 I m a g e %
255% %
256% %
257% %
258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259%
260% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
261% allocates the memory necessary for the new Image structure and returns a
262% pointer to the new image.
263%
264% The format of the ReadGROUP4Image method is:
265%
266% Image *ReadGROUP4Image(const ImageInfo *image_info,
267% ExceptionInfo *exception)
268%
269% A description of each parameter follows:
270%
271% o image_info: the image info.
272%
273% o exception: return any errors or warnings in this structure.
274%
275*/
276
cristyeaedf062010-05-29 22:36:02 +0000277static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000278{
279 unsigned char
280 buffer[4];
281
282 buffer[0]=(unsigned char) value;
283 buffer[1]=(unsigned char) (value >> 8);
284 buffer[2]=(unsigned char) (value >> 16);
285 buffer[3]=(unsigned char) (value >> 24);
286 return(fwrite(buffer,1,4,file));
287}
288
289static Image *ReadGROUP4Image(const ImageInfo *image_info,
290 ExceptionInfo *exception)
291{
292 char
293 filename[MaxTextExtent];
294
295 FILE
296 *file;
297
298 Image
299 *image;
300
301 ImageInfo
302 *read_info;
303
304 int
305 c,
306 unique_file;
307
308 MagickBooleanType
309 status;
310
311 size_t
312 length;
313
314 ssize_t
315 offset,
316 strip_offset;
317
318 /*
319 Open image file.
320 */
321 assert(image_info != (const ImageInfo *) NULL);
322 assert(image_info->signature == MagickSignature);
323 if (image_info->debug != MagickFalse)
324 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
325 image_info->filename);
326 assert(exception != (ExceptionInfo *) NULL);
327 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000328 image=AcquireImage(image_info,exception);
cristy3d7f8062009-09-24 20:45:35 +0000329 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
330 if (status == MagickFalse)
331 {
332 image=DestroyImageList(image);
333 return((Image *) NULL);
334 }
335 /*
336 Write raw CCITT Group 4 wrapped as a TIFF image file.
337 */
338 file=(FILE *) NULL;
339 unique_file=AcquireUniqueFileResource(filename);
340 if (unique_file != -1)
341 file=fdopen(unique_file,"wb");
342 if ((unique_file == -1) || (file == (FILE *) NULL))
343 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
344 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
345 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
346 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
347 length=WriteLSBLong(file,image->columns);
348 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
349 length=WriteLSBLong(file,image->rows);
350 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
351 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
352 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
353 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
354 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000355 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000356 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000357 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000358 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
359 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000360 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000361 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
362 offset=(ssize_t) ftell(file)-4;
363 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000364 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000365 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000366 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000367 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
368 length=fwrite("\000\000\000\000",1,4,file);
cristy2a11bef2011-10-28 18:33:11 +0000369 length=WriteLSBLong(file,(long) image->resolution.x);
cristy3d7f8062009-09-24 20:45:35 +0000370 length=WriteLSBLong(file,1);
371 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
372 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000373 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000374 length=WriteLSBLong(file,(unsigned int) length);
375 (void) fclose(file);
376 (void) CloseBlob(image);
377 image=DestroyImage(image);
378 /*
379 Read TIFF image.
380 */
cristy14efd992009-09-26 23:46:03 +0000381 read_info=CloneImageInfo((ImageInfo *) NULL);
cristyb51dff52011-05-19 16:55:47 +0000382 (void) FormatLocaleString(read_info->filename,MaxTextExtent,"%s",filename);
cristy3d7f8062009-09-24 20:45:35 +0000383 image=ReadTIFFImage(read_info,exception);
384 read_info=DestroyImageInfo(read_info);
385 if (image != (Image *) NULL)
386 {
387 (void) CopyMagickString(image->filename,image_info->filename,
388 MaxTextExtent);
389 (void) CopyMagickString(image->magick_filename,image_info->filename,
390 MaxTextExtent);
391 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
392 }
393 (void) RelinquishUniqueFileResource(filename);
394 return(image);
395}
396#endif
397
398#if defined(MAGICKCORE_TIFF_DELEGATE)
399/*
400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401% %
402% %
403% %
cristy3ed852e2009-09-05 21:47:34 +0000404% R e a d T I F F I m a g e %
405% %
406% %
407% %
408%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409%
410% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
411% memory necessary for the new Image structure and returns a pointer to the
412% new image.
413%
414% The format of the ReadTIFFImage method is:
415%
416% Image *ReadTIFFImage(const ImageInfo *image_info,
417% ExceptionInfo *exception)
418%
419% A description of each parameter follows:
420%
421% o image_info: the image info.
422%
423% o exception: return any errors or warnings in this structure.
424%
425*/
426
427static inline size_t MagickMax(const size_t x,const size_t y)
428{
429 if (x > y)
430 return(x);
431 return(y);
432}
433
cristybb503372010-05-27 20:51:26 +0000434static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000435{
436 if (x < y)
437 return(x);
438 return(y);
439}
440
441static MagickBooleanType ReadProfile(Image *image,const char *name,
cristy018f07f2011-09-04 21:15:19 +0000442 unsigned char *datum,ssize_t length,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000443{
444 MagickBooleanType
445 status;
446
cristybb503372010-05-27 20:51:26 +0000447 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000448 i;
449
450 StringInfo
451 *profile;
452
453 if (length < 4)
454 return(MagickFalse);
455 i=0;
456 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
457 {
458 for (i=0; i < (length-4); i+=2)
459 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
460 break;
461 if (i == length)
462 length-=i;
463 else
464 i=0;
465 if (length < 4)
466 return(MagickFalse);
467 }
cristye8f8f382011-09-01 13:32:37 +0000468 profile=BlobToStringInfo(datum+i,(size_t) length);
469 if (profile == (StringInfo *) NULL)
470 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
471 image->filename);
cristyd15e6592011-10-15 00:13:06 +0000472 status=SetImageProfile(image,name,profile,exception);
cristy3ed852e2009-09-05 21:47:34 +0000473 profile=DestroyStringInfo(profile);
474 if (status == MagickFalse)
475 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
476 image->filename);
477 return(MagickTrue);
478}
479
480#if defined(__cplusplus) || defined(c_plusplus)
481extern "C" {
482#endif
483
484static int TIFFCloseBlob(thandle_t image)
485{
486 (void) CloseBlob((Image *) image);
487 return(0);
488}
489
490static void TIFFErrors(const char *module,const char *format,va_list error)
491{
492 char
493 message[MaxTextExtent];
494
495 ExceptionInfo
496 *exception;
497
498#if defined(MAGICKCORE_HAVE_VSNPRINTF)
499 (void) vsnprintf(message,MaxTextExtent,format,error);
500#else
501 (void) vsprintf(message,format,error);
502#endif
503 (void) ConcatenateMagickString(message,".",MaxTextExtent);
504 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
505 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000506 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
507 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000508}
509
cristy9c47c732011-11-09 19:58:33 +0000510static toff_t TIFFGetBlobSize(thandle_t image)
511{
512 return((toff_t) GetBlobSize((Image *) image));
513}
514
cristy018f07f2011-09-04 21:15:19 +0000515static void TIFFGetProfiles(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000516{
517 uint32
518 length;
519
520 unsigned char
521 *profile;
522
523#if defined(TIFFTAG_ICCPROFILE)
524 length=0;
525 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000526 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000527#endif
528#if defined(TIFFTAG_PHOTOSHOP)
529 length=0;
530 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000531 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000532#endif
533#if defined(TIFFTAG_RICHTIFFIPTC)
534 length=0;
535 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
536 {
537 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000538 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy018f07f2011-09-04 21:15:19 +0000539 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000540 }
541#endif
542#if defined(TIFFTAG_XMLPACKET)
543 length=0;
544 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000545 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000546#endif
547 length=0;
548 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000549 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000550}
551
cristyd15e6592011-10-15 00:13:06 +0000552static void TIFFGetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000553{
554 char
cristy98ca0f52011-10-08 23:19:10 +0000555 message[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000556 *text;
557
cristy98ca0f52011-10-08 23:19:10 +0000558 uint32
559 count;
560
cristy3ed852e2009-09-05 21:47:34 +0000561 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000562 (void) SetImageProperty(image,"tiff:artist",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000563 if (TIFFGetField(tiff,TIFFTAG_COPYRIGHT,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000564 (void) SetImageProperty(image,"tiff:copyright",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000565 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000566 (void) SetImageProperty(image,"tiff:timestamp",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000567 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000568 (void) SetImageProperty(image,"tiff:document",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000569 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000570 (void) SetImageProperty(image,"tiff:hostcomputer",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000571 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000572 (void) SetImageProperty(image,"comment",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000573 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000574 (void) SetImageProperty(image,"tiff:make",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000575 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000576 (void) SetImageProperty(image,"tiff:model",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000577 if (TIFFGetField(tiff,TIFFTAG_OPIIMAGEID,&count,&text) == 1)
578 {
579 if (count >= MaxTextExtent)
580 count=MaxTextExtent-1;
581 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000582 (void) SetImageProperty(image,"tiff:image-id",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000583 }
cristy3ed852e2009-09-05 21:47:34 +0000584 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000585 (void) SetImageProperty(image,"label",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000586 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000587 (void) SetImageProperty(image,"tiff:software",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000588 if (TIFFGetField(tiff,33423,&count,&text) == 1)
589 {
590 if (count >= MaxTextExtent)
591 count=MaxTextExtent-1;
592 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000593 (void) SetImageProperty(image,"tiff:kodak-33423",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000594 }
595 if (TIFFGetField(tiff,36867,&count,&text) == 1)
596 {
597 if (count >= MaxTextExtent)
598 count=MaxTextExtent-1;
599 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000600 (void) SetImageProperty(image,"tiff:kodak-36867",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000601 }
cristy3ed852e2009-09-05 21:47:34 +0000602}
603
cristyd15e6592011-10-15 00:13:06 +0000604static void TIFFGetEXIFProperties(TIFF *tiff,Image *image,
605 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000606{
607#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
608 char
609 value[MaxTextExtent];
610
cristybb503372010-05-27 20:51:26 +0000611 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000612 i;
613
614 tdir_t
615 directory;
616
cristyccf4e122011-10-29 00:29:19 +0000617#if defined(TIFF_VERSION_BIG)
618 uint64
619#else
cristy3ed852e2009-09-05 21:47:34 +0000620 uint32
cristyccf4e122011-10-29 00:29:19 +0000621#endif
cristy3ed852e2009-09-05 21:47:34 +0000622 offset;
623
cristy7992e402011-09-16 15:24:22 +0000624 void
625 *sans;
626
cristy3ed852e2009-09-05 21:47:34 +0000627 /*
628 Read EXIF properties.
629 */
630 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
631 return;
632 directory=TIFFCurrentDirectory(tiff);
633 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
634 return;
cristy7992e402011-09-16 15:24:22 +0000635 sans=NULL;
cristy3ed852e2009-09-05 21:47:34 +0000636 for (i=0; exif_info[i].tag != 0; i++)
637 {
638 *value='\0';
639 switch (exif_info[i].type)
640 {
641 case TIFF_ASCII:
642 {
643 char
644 *ascii;
645
cristy7992e402011-09-16 15:24:22 +0000646 ascii=(char *) NULL;
647 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans) != 0) &&
648 (ascii != (char *) NULL) && (*ascii != '\0'))
cristy3ed852e2009-09-05 21:47:34 +0000649 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
650 break;
651 }
652 case TIFF_SHORT:
653 {
654 uint16
cristy7992e402011-09-16 15:24:22 +0000655 shorty[2] = { 0, 0};
cristy3ed852e2009-09-05 21:47:34 +0000656
cristy7992e402011-09-16 15:24:22 +0000657 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans) != 0)
658 (void) FormatLocaleString(value,MaxTextExtent,"%d",(int) shorty[0]);
cristy3ed852e2009-09-05 21:47:34 +0000659 break;
660 }
661 case TIFF_LONG:
662 {
663 uint32
cristy7992e402011-09-16 15:24:22 +0000664 longy;
cristy3ed852e2009-09-05 21:47:34 +0000665
cristy7992e402011-09-16 15:24:22 +0000666 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans) != 0)
667 (void) FormatLocaleString(value,MaxTextExtent,"%d",longy);
cristy3ed852e2009-09-05 21:47:34 +0000668 break;
669 }
cristyccf4e122011-10-29 00:29:19 +0000670#if defined(TIFF_VERSION_BIG)
671 case TIFF_LONG8:
672 {
673 uint64
674 longy;
675
676 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans) != 0)
cristy02845b42011-11-11 17:22:13 +0000677 (void) FormatLocaleString(value,MaxTextExtent,"%lld",longy);
cristyccf4e122011-10-29 00:29:19 +0000678 break;
679 }
680#endif
cristy3ed852e2009-09-05 21:47:34 +0000681 case TIFF_RATIONAL:
682 case TIFF_SRATIONAL:
cristy7992e402011-09-16 15:24:22 +0000683 case TIFF_FLOAT:
684 case TIFF_DOUBLE:
cristy3ed852e2009-09-05 21:47:34 +0000685 {
686 float
cristy7992e402011-09-16 15:24:22 +0000687 rational[16];
cristy3ed852e2009-09-05 21:47:34 +0000688
cristy7992e402011-09-16 15:24:22 +0000689 if (TIFFGetField(tiff,exif_info[i].tag,&rational,&sans) != 0)
690 (void) FormatLocaleString(value,MaxTextExtent,"%g",rational[0]);
cristy3ed852e2009-09-05 21:47:34 +0000691 break;
692 }
693 default:
694 break;
695 }
696 if (*value != '\0')
cristyd15e6592011-10-15 00:13:06 +0000697 (void) SetImageProperty(image,exif_info[i].property,value,exception);
cristy3ed852e2009-09-05 21:47:34 +0000698 }
699 TIFFSetDirectory(tiff,directory);
700#else
701 (void) tiff;
702 (void) image;
703#endif
704}
705
706static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
707{
708 *base=(tdata_t *) GetBlobStreamData((Image *) image);
709 if (*base != (tdata_t *) NULL)
710 *size=(toff_t) GetBlobSize((Image *) image);
711 if (*base != (tdata_t *) NULL)
712 return(1);
713 return(0);
714}
715
716static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
717{
718 tsize_t
719 count;
720
721 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
722 (unsigned char *) data);
723 return(count);
724}
725
cristybb503372010-05-27 20:51:26 +0000726static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
727 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000728{
729 int32
730 status;
731
732 (void) bits_per_sample;
733 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
734 return(status);
735}
736
737static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
738{
739 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
740}
741
cristy3ed852e2009-09-05 21:47:34 +0000742static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
743{
744 (void) image;
745 (void) base;
746 (void) size;
747}
748
749static void TIFFWarnings(const char *module,const char *format,va_list warning)
750{
751 char
752 message[MaxTextExtent];
753
754 ExceptionInfo
755 *exception;
756
757#if defined(MAGICKCORE_HAVE_VSNPRINTF)
758 (void) vsnprintf(message,MaxTextExtent,format,warning);
759#else
760 (void) vsprintf(message,format,warning);
761#endif
762 (void) ConcatenateMagickString(message,".",MaxTextExtent);
763 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
764 if (exception != (ExceptionInfo *) NULL)
765 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
766 message,"`%s'",module);
767}
768
769static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
770{
771 tsize_t
772 count;
773
774 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
775 (unsigned char *) data);
776 return(count);
777}
778
779#if defined(__cplusplus) || defined(c_plusplus)
780}
781#endif
782
783static Image *ReadTIFFImage(const ImageInfo *image_info,
784 ExceptionInfo *exception)
785{
786 typedef enum
787 {
788 ReadSingleSampleMethod,
789 ReadRGBAMethod,
790 ReadCMYKAMethod,
791 ReadStripMethod,
792 ReadTileMethod,
793 ReadGenericMethod
794 } TIFFMethodType;
795
796 const char
797 *option;
798
799 float
800 *chromaticity,
801 x_position,
802 y_position,
803 x_resolution,
804 y_resolution;
805
806 Image
807 *image;
808
cristy3ed852e2009-09-05 21:47:34 +0000809 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000810 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000811 debug,
812 status;
813
814 MagickSizeType
815 number_pixels;
816
817 QuantumInfo
818 *quantum_info;
819
820 QuantumType
821 quantum_type;
822
cristybb503372010-05-27 20:51:26 +0000823 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000824 i;
825
826 size_t
827 length,
828 pad;
829
cristyc6da28e2011-04-28 01:41:35 +0000830 ssize_t
831 y;
832
cristy3ed852e2009-09-05 21:47:34 +0000833 TIFF
834 *tiff;
835
836 TIFFErrorHandler
837 error_handler,
838 warning_handler;
839
840 TIFFMethodType
841 method;
842
843 uint16
844 compress_tag,
845 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000846 endian,
cristy3ed852e2009-09-05 21:47:34 +0000847 extra_samples,
848 interlace,
849 max_sample_value,
850 min_sample_value,
851 orientation,
852 pages,
853 photometric,
854 *sample_info,
855 sample_format,
856 samples_per_pixel,
857 units,
858 value;
859
860 uint32
861 height,
862 rows_per_strip,
863 width;
864
865 unsigned char
866 *pixels;
867
cristy3ed852e2009-09-05 21:47:34 +0000868 /*
869 Open image.
870 */
871 assert(image_info != (const ImageInfo *) NULL);
872 assert(image_info->signature == MagickSignature);
873 if (image_info->debug != MagickFalse)
874 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
875 image_info->filename);
876 assert(exception != (ExceptionInfo *) NULL);
877 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000878 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000879 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
880 if (status == MagickFalse)
881 {
882 image=DestroyImageList(image);
883 return((Image *) NULL);
884 }
885 (void) MagickSetThreadValue(tiff_exception,exception);
886 error_handler=TIFFSetErrorHandler(TIFFErrors);
887 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
cristy1630f7f2011-02-18 01:10:02 +0000888 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
cristy3ed852e2009-09-05 21:47:34 +0000889 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
890 TIFFUnmapBlob);
891 if (tiff == (TIFF *) NULL)
892 {
893 (void) TIFFSetWarningHandler(warning_handler);
894 (void) TIFFSetErrorHandler(error_handler);
895 image=DestroyImageList(image);
896 return((Image *) NULL);
897 }
898 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +0000899 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +0000900 if (image_info->number_scenes != 0)
901 {
902 /*
903 Generate blank images for subimage specification (e.g. image.tif[4].
904 */
cristybb503372010-05-27 20:51:26 +0000905 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000906 {
907 (void) TIFFReadDirectory(tiff);
cristy9950d572011-10-01 18:22:35 +0000908 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000909 if (GetNextImageInList(image) == (Image *) NULL)
910 {
911 image=DestroyImageList(image);
912 return((Image *) NULL);
913 }
914 image=SyncNextImageInList(image);
915 }
916 }
917 do
918 {
919 if (0 && (image_info->verbose != MagickFalse))
920 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristy3f590e52011-11-14 14:29:44 +0000921#if defined(MAGICKCORE_HAVE_TIFFISBIGENDIAN)
cristy6b032822010-06-29 16:52:32 +0000922 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
cristyd15e6592011-10-15 00:13:06 +0000923 "lsb" : "msb",exception);
cristy3f590e52011-11-14 14:29:44 +0000924#endif
cristy3ed852e2009-09-05 21:47:34 +0000925 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
926 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
927 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
928 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000929 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000930 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
931 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
932 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
cristyae1319c2011-01-19 20:18:53 +0000933 if (sample_format == SAMPLEFORMAT_IEEEFP)
cristyd15e6592011-10-15 00:13:06 +0000934 (void) SetImageProperty(image,"quantum:format","floating-point",
935 exception);
cristy3ed852e2009-09-05 21:47:34 +0000936 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
937 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
938 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
939 switch (photometric)
940 {
941 case PHOTOMETRIC_MINISBLACK:
942 {
cristyd15e6592011-10-15 00:13:06 +0000943 (void) SetImageProperty(image,"tiff:photometric","min-is-black",
944 exception);
cristy3ed852e2009-09-05 21:47:34 +0000945 break;
946 }
947 case PHOTOMETRIC_MINISWHITE:
948 {
cristyd15e6592011-10-15 00:13:06 +0000949 (void) SetImageProperty(image,"tiff:photometric","min-is-white",
950 exception);
cristy3ed852e2009-09-05 21:47:34 +0000951 break;
952 }
953 case PHOTOMETRIC_PALETTE:
954 {
cristyd15e6592011-10-15 00:13:06 +0000955 (void) SetImageProperty(image,"tiff:photometric","palette",exception);
cristy3ed852e2009-09-05 21:47:34 +0000956 break;
957 }
958 case PHOTOMETRIC_RGB:
959 {
cristyd15e6592011-10-15 00:13:06 +0000960 (void) SetImageProperty(image,"tiff:photometric","RGB",exception);
cristy3ed852e2009-09-05 21:47:34 +0000961 break;
962 }
963 case PHOTOMETRIC_CIELAB:
964 {
cristyd15e6592011-10-15 00:13:06 +0000965 (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception);
cristy3ed852e2009-09-05 21:47:34 +0000966 break;
967 }
968 case PHOTOMETRIC_SEPARATED:
969 {
cristyd15e6592011-10-15 00:13:06 +0000970 (void) SetImageProperty(image,"tiff:photometric","separated",exception);
cristy3ed852e2009-09-05 21:47:34 +0000971 break;
972 }
cristy79e5dad2010-09-16 19:48:33 +0000973 case PHOTOMETRIC_YCBCR:
974 {
cristyd15e6592011-10-15 00:13:06 +0000975 (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception);
cristy79e5dad2010-09-16 19:48:33 +0000976 break;
977 }
cristy3ed852e2009-09-05 21:47:34 +0000978 default:
979 {
cristyd15e6592011-10-15 00:13:06 +0000980 (void) SetImageProperty(image,"tiff:photometric","unknown",exception);
cristy3ed852e2009-09-05 21:47:34 +0000981 break;
982 }
983 }
984 if (image->debug != MagickFalse)
985 {
986 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
987 (unsigned int) width,(unsigned int) height);
988 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
989 interlace);
990 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
991 "Bits per sample: %u",bits_per_sample);
992 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
993 "Min sample value: %u",min_sample_value);
994 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
995 "Max sample value: %u",max_sample_value);
996 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
cristyd15e6592011-10-15 00:13:06 +0000997 "interpretation: %s",GetImageProperty(image,"tiff:photometric",
998 exception));
cristy3ed852e2009-09-05 21:47:34 +0000999 }
cristybb503372010-05-27 20:51:26 +00001000 image->columns=(size_t) width;
1001 image->rows=(size_t) height;
1002 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +00001003 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +00001004 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
1005 (double) image->depth);
cristyb241f382012-01-01 01:45:57 +00001006 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1007 (photometric == PHOTOMETRIC_MINISWHITE))
1008 image->colorspace=GRAYColorspace;
cristy3ed852e2009-09-05 21:47:34 +00001009 if (photometric == PHOTOMETRIC_SEPARATED)
1010 image->colorspace=CMYKColorspace;
1011 if (photometric == PHOTOMETRIC_CIELAB)
1012 image->colorspace=LabColorspace;
1013 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
1014 &samples_per_pixel);
1015 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
cristy2a11bef2011-10-28 18:33:11 +00001016 x_resolution=(float) image->resolution.x;
1017 y_resolution=(float) image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +00001018 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
1019 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
cristy2a11bef2011-10-28 18:33:11 +00001020 image->resolution.x=x_resolution;
1021 image->resolution.y=y_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001022 x_position=(float) image->page.x/x_resolution;
1023 y_position=(float) image->page.y/y_resolution;
1024 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
1025 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +00001026 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
1027 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001028 image->orientation=(OrientationType) orientation;
1029 chromaticity=(float *) NULL;
1030 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
1031 if (chromaticity != (float *) NULL)
1032 {
1033 image->chromaticity.white_point.x=chromaticity[0];
1034 image->chromaticity.white_point.y=chromaticity[1];
1035 }
1036 chromaticity=(float *) NULL;
1037 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
1038 if (chromaticity != (float *) NULL)
1039 {
1040 image->chromaticity.red_primary.x=chromaticity[0];
1041 image->chromaticity.red_primary.y=chromaticity[1];
1042 image->chromaticity.green_primary.x=chromaticity[2];
1043 image->chromaticity.green_primary.y=chromaticity[3];
1044 image->chromaticity.blue_primary.x=chromaticity[4];
1045 image->chromaticity.blue_primary.y=chromaticity[5];
1046 }
cristy3ed852e2009-09-05 21:47:34 +00001047#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1048 if ((compress_tag != COMPRESSION_NONE) &&
1049 (TIFFIsCODECConfigured(compress_tag) == 0))
1050 {
1051 TIFFClose(tiff);
1052 ThrowReaderException(CoderError,"CompressNotSupported");
1053 }
1054#endif
1055 switch (compress_tag)
1056 {
1057 case COMPRESSION_NONE: image->compression=NoCompression; break;
1058 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1059 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1060 case COMPRESSION_JPEG:
1061 {
1062 image->compression=JPEGCompression;
1063#if defined(JPEG_SUPPORT)
1064 {
1065 char
1066 sampling_factor[MaxTextExtent];
1067
1068 uint16
1069 horizontal,
1070 vertical;
1071
1072 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1073 &horizontal,&vertical);
cristyb51dff52011-05-19 16:55:47 +00001074 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
cristy3ed852e2009-09-05 21:47:34 +00001075 horizontal,vertical);
1076 (void) SetImageProperty(image,"jpeg:sampling-factor",
cristyd15e6592011-10-15 00:13:06 +00001077 sampling_factor,exception);
cristy3ed852e2009-09-05 21:47:34 +00001078 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1079 "Sampling Factors: %s",sampling_factor);
1080 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1081 {
1082 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1083 JPEGCOLORMODE_RGB);
1084 photometric=PHOTOMETRIC_RGB;
1085 }
1086 }
1087#endif
1088 break;
1089 }
1090 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001091#if defined(COMPRESSION_LZMA)
1092 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1093#endif
cristy3ed852e2009-09-05 21:47:34 +00001094 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1095 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1096 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1097 default: image->compression=RLECompression; break;
1098 }
cristy809fdbd2011-12-01 01:26:27 +00001099 TIFFGetProfiles(tiff,image,exception);
1100 TIFFGetProperties(tiff,image,exception);
cristyd93c30e2012-01-09 17:16:52 +00001101 option=GetImageOption(image_info,"tiff:exif-properties");
1102 if ((option == (const char *) NULL) ||
cristy9e8cb1c2012-01-24 23:47:47 +00001103 (IsMagickTrue(option) != MagickFalse))
cristyd93c30e2012-01-09 17:16:52 +00001104 TIFFGetEXIFProperties(tiff,image,exception);
cristy809fdbd2011-12-01 01:26:27 +00001105 /*
1106 Allocate memory for the image and pixel buffer.
1107 */
cristy3ed852e2009-09-05 21:47:34 +00001108 quantum_info=AcquireQuantumInfo(image_info,image);
1109 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001110 {
1111 TIFFClose(tiff);
1112 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1113 }
cristya4560912012-01-14 20:28:03 +00001114 quantum_info->endian=LSBEndian;
1115 if (endian == FILLORDER_LSB2MSB)
1116 quantum_info->endian=MSBEndian;
1117 image->endian=quantum_info->endian;
cristy3ed852e2009-09-05 21:47:34 +00001118 if (sample_format == SAMPLEFORMAT_UINT)
1119 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1120 if (sample_format == SAMPLEFORMAT_INT)
1121 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1122 if (sample_format == SAMPLEFORMAT_IEEEFP)
1123 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1124 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001125 {
cristya9a6ec42011-10-26 16:55:01 +00001126 quantum_info=DestroyQuantumInfo(quantum_info);
cristy8d137bf2010-04-21 23:52:04 +00001127 TIFFClose(tiff);
1128 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1129 }
cristy3ed852e2009-09-05 21:47:34 +00001130 status=MagickTrue;
1131 switch (photometric)
1132 {
1133 case PHOTOMETRIC_MINISBLACK:
1134 {
1135 quantum_info->min_is_white=MagickFalse;
1136 break;
1137 }
1138 case PHOTOMETRIC_MINISWHITE:
1139 {
1140 quantum_info->min_is_white=MagickTrue;
1141 break;
1142 }
1143 default:
1144 break;
1145 }
cristy891dc792010-03-04 01:47:16 +00001146 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001147 extra_samples=0;
1148 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1149 &sample_info);
1150 if (extra_samples == 0)
1151 {
1152 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1153 image->matte=MagickTrue;
1154 }
1155 else
cristy6c207342010-02-19 17:49:50 +00001156 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001157 {
cristy7cf69862010-03-03 13:57:24 +00001158 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001159 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001160 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001161 }
cristy891dc792010-03-04 01:47:16 +00001162 option=GetImageOption(image_info,"tiff:alpha");
1163 if (option != (const char *) NULL)
1164 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1165 MagickFalse;
1166 if (image->matte != MagickFalse)
1167 (void) SetImageProperty(image,"tiff:alpha",
cristyd15e6592011-10-15 00:13:06 +00001168 associated_alpha != MagickFalse ? "associated" : "unassociated",
1169 exception);
cristy3ed852e2009-09-05 21:47:34 +00001170 if ((photometric == PHOTOMETRIC_PALETTE) &&
1171 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1172 {
cristybb503372010-05-27 20:51:26 +00001173 size_t
cristy3ed852e2009-09-05 21:47:34 +00001174 colors;
1175
cristybb503372010-05-27 20:51:26 +00001176 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy018f07f2011-09-04 21:15:19 +00001177 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001178 {
1179 TIFFClose(tiff);
1180 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1181 }
1182 }
1183 if (units == RESUNIT_INCH)
1184 image->units=PixelsPerInchResolution;
1185 if (units == RESUNIT_CENTIMETER)
1186 image->units=PixelsPerCentimeterResolution;
1187 value=(unsigned short) image->scene;
1188 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1189 image->scene=value;
1190 if (image_info->ping != MagickFalse)
1191 {
1192 if (image_info->number_scenes != 0)
1193 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1194 break;
1195 goto next_tiff_frame;
1196 }
1197 method=ReadGenericMethod;
1198 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1199 {
1200 char
1201 value[MaxTextExtent];
1202
1203 method=ReadStripMethod;
cristyb51dff52011-05-19 16:55:47 +00001204 (void) FormatLocaleString(value,MaxTextExtent,"%u",
cristy3ed852e2009-09-05 21:47:34 +00001205 (unsigned int) rows_per_strip);
cristyd15e6592011-10-15 00:13:06 +00001206 (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception);
cristy3ed852e2009-09-05 21:47:34 +00001207 }
1208 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1209 method=ReadRGBAMethod;
1210 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1211 method=ReadCMYKAMethod;
1212 if ((photometric != PHOTOMETRIC_RGB) &&
1213 (photometric != PHOTOMETRIC_CIELAB) &&
1214 (photometric != PHOTOMETRIC_SEPARATED))
1215 method=ReadGenericMethod;
1216 if (image->storage_class == PseudoClass)
1217 method=ReadSingleSampleMethod;
1218 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1219 (photometric == PHOTOMETRIC_MINISWHITE))
1220 method=ReadSingleSampleMethod;
1221 if ((photometric != PHOTOMETRIC_SEPARATED) &&
cristybc736662011-03-09 13:39:42 +00001222 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
cristy3ed852e2009-09-05 21:47:34 +00001223 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001224 if (image->compression == JPEGCompression)
1225 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001226 if (TIFFIsTiled(tiff) != MagickFalse)
1227 method=ReadTileMethod;
1228 quantum_type=RGBQuantum;
1229 pixels=GetQuantumPixels(quantum_info);
1230 switch (method)
1231 {
1232 case ReadSingleSampleMethod:
1233 {
1234 /*
1235 Convert TIFF image to PseudoClass MIFF image.
1236 */
1237 if ((image->storage_class == PseudoClass) &&
1238 (photometric == PHOTOMETRIC_PALETTE))
1239 {
cristybb503372010-05-27 20:51:26 +00001240 size_t
cristy3ed852e2009-09-05 21:47:34 +00001241 range;
1242
1243 uint16
1244 *blue_colormap,
1245 *green_colormap,
1246 *red_colormap;
1247
1248 /*
1249 Initialize colormap.
1250 */
1251 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1252 &green_colormap,&blue_colormap);
1253 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001254 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001255 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1256 (blue_colormap[i] >= 256))
1257 {
1258 range=65535;
1259 break;
1260 }
cristybb503372010-05-27 20:51:26 +00001261 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001262 {
cristyce70c172010-01-07 17:15:30 +00001263 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001264 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001265 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001266 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001267 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001268 blue_colormap[i])/range);
1269 }
1270 }
1271 quantum_type=IndexQuantum;
1272 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1273 if (image->matte != MagickFalse)
1274 {
1275 if (image->storage_class != PseudoClass)
1276 {
1277 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1278 GrayAlphaQuantum;
1279 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1280 }
1281 else
1282 {
1283 quantum_type=IndexAlphaQuantum;
1284 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1285 }
1286 }
1287 else
1288 if (image->storage_class != PseudoClass)
1289 {
1290 quantum_type=GrayQuantum;
1291 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1292 }
1293 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1294 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001295 {
1296 TIFFClose(tiff);
1297 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1298 }
cristy3ed852e2009-09-05 21:47:34 +00001299 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001300 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001301 {
1302 int
1303 status;
1304
cristy4c08aed2011-07-01 19:47:50 +00001305 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001306 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001307
1308 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1309 if (status == -1)
1310 break;
1311 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001312 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001313 break;
1314 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1315 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00001316 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00001317 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1318 break;
1319 if (image->previous == (Image *) NULL)
1320 {
cristycee97112010-05-28 00:44:52 +00001321 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1322 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001323 if (status == MagickFalse)
1324 break;
1325 }
1326 }
1327 break;
1328 }
1329 case ReadRGBAMethod:
1330 {
1331 /*
1332 Convert TIFF image to DirectClass MIFF image.
1333 */
1334 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1335 quantum_type=RGBQuantum;
1336 if (image->matte != MagickFalse)
1337 {
1338 quantum_type=RGBAQuantum;
1339 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1340 }
1341 if (image->colorspace == CMYKColorspace)
1342 {
1343 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1344 quantum_type=CMYKQuantum;
1345 if (image->matte != MagickFalse)
1346 {
1347 quantum_type=CMYKAQuantum;
1348 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1349 }
1350 }
1351 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1352 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001353 {
1354 TIFFClose(tiff);
1355 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1356 }
cristy3ed852e2009-09-05 21:47:34 +00001357 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001358 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001359 {
1360 int
1361 status;
1362
cristy4c08aed2011-07-01 19:47:50 +00001363 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001364 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001365
1366 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1367 if (status == -1)
1368 break;
1369 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001370 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001371 break;
cristy0b334892010-03-15 02:26:46 +00001372 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1373 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001374 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1375 break;
1376 if (image->previous == (Image *) NULL)
1377 {
cristycee97112010-05-28 00:44:52 +00001378 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1379 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001380 if (status == MagickFalse)
1381 break;
1382 }
1383 }
1384 break;
1385 }
1386 case ReadCMYKAMethod:
1387 {
1388 /*
1389 Convert TIFF image to DirectClass MIFF image.
1390 */
cristybb503372010-05-27 20:51:26 +00001391 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001392 {
cristybb503372010-05-27 20:51:26 +00001393 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001394 {
cristy4c08aed2011-07-01 19:47:50 +00001395 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001396 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001397
cristy3ed852e2009-09-05 21:47:34 +00001398 int
1399 status;
1400
1401 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1402 pixels);
1403 if (status == -1)
1404 break;
1405 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001406 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001407 break;
1408 if (image->colorspace != CMYKColorspace)
1409 switch (i)
1410 {
1411 case 0: quantum_type=RedQuantum; break;
1412 case 1: quantum_type=GreenQuantum; break;
1413 case 2: quantum_type=BlueQuantum; break;
1414 case 3: quantum_type=AlphaQuantum; break;
1415 default: quantum_type=UndefinedQuantum; break;
1416 }
cristy6d9f12f2009-11-03 14:50:26 +00001417 else
cristy3ed852e2009-09-05 21:47:34 +00001418 switch (i)
1419 {
1420 case 0: quantum_type=CyanQuantum; break;
1421 case 1: quantum_type=MagentaQuantum; break;
1422 case 2: quantum_type=YellowQuantum; break;
1423 case 3: quantum_type=BlackQuantum; break;
1424 case 4: quantum_type=AlphaQuantum; break;
1425 default: quantum_type=UndefinedQuantum; break;
1426 }
1427 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1428 quantum_type,pixels,exception);
1429 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1430 break;
1431 }
1432 if (image->previous == (Image *) NULL)
1433 {
cristycee97112010-05-28 00:44:52 +00001434 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1435 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001436 if (status == MagickFalse)
1437 break;
1438 }
1439 }
1440 break;
1441 }
1442 case ReadStripMethod:
1443 {
1444 register unsigned char
1445 *p;
1446
1447 /*
1448 Convert stripped TIFF image to DirectClass MIFF image.
1449 */
1450 i=0;
1451 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001452 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001453 {
cristybb503372010-05-27 20:51:26 +00001454 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001455 x;
1456
cristy4c08aed2011-07-01 19:47:50 +00001457 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001458 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001459
1460 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001461 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001462 break;
1463 if (i == 0)
1464 {
1465 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1466 break;
cristyeaedf062010-05-29 22:36:02 +00001467 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1468 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001469 }
1470 i--;
cristy7f5d1662011-04-14 12:43:28 +00001471 p=(unsigned char *) (((uint32 *) pixels)+image->columns*i);
cristybb503372010-05-27 20:51:26 +00001472 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001473 {
cristy4c08aed2011-07-01 19:47:50 +00001474 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1475 (TIFFGetR(*p))),q);
1476 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1477 (TIFFGetG(*p))),q);
1478 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1479 (TIFFGetB(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001480 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001481 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1482 (TIFFGetA(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001483 p++;
cristyed231572011-07-14 02:18:59 +00001484 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001485 }
1486 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1487 break;
1488 if (image->previous == (Image *) NULL)
1489 {
cristycee97112010-05-28 00:44:52 +00001490 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1491 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001492 if (status == MagickFalse)
1493 break;
1494 }
1495 }
1496 break;
1497 }
1498 case ReadTileMethod:
1499 {
1500 register uint32
1501 *p;
1502
1503 uint32
1504 *tile_pixels,
1505 columns,
1506 rows;
1507
cristybb503372010-05-27 20:51:26 +00001508 size_t
cristy3ed852e2009-09-05 21:47:34 +00001509 number_pixels;
1510
1511 /*
1512 Convert tiled TIFF image to DirectClass MIFF image.
1513 */
1514 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1515 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1516 {
1517 TIFFClose(tiff);
1518 ThrowReaderException(CoderError,"ImageIsNotTiled");
1519 }
cristyc82a27b2011-10-21 01:07:16 +00001520 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +00001521 number_pixels=columns*rows;
cristyda16f162011-02-19 23:52:17 +00001522 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
cristy3ed852e2009-09-05 21:47:34 +00001523 sizeof(*tile_pixels));
1524 if (tile_pixels == (uint32 *) NULL)
1525 {
1526 TIFFClose(tiff);
1527 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1528 }
cristybb503372010-05-27 20:51:26 +00001529 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001530 {
cristybb503372010-05-27 20:51:26 +00001531 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001532 x;
1533
cristy4c08aed2011-07-01 19:47:50 +00001534 register Quantum
1535 *restrict q,
1536 *restrict tile;
cristy3ed852e2009-09-05 21:47:34 +00001537
cristybb503372010-05-27 20:51:26 +00001538 size_t
cristy3ed852e2009-09-05 21:47:34 +00001539 columns_remaining,
1540 rows_remaining;
1541
1542 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001543 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001544 rows_remaining=rows;
1545 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1546 exception);
cristy4c08aed2011-07-01 19:47:50 +00001547 if (tile == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001548 break;
cristybb503372010-05-27 20:51:26 +00001549 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001550 {
cristybb503372010-05-27 20:51:26 +00001551 size_t
cristy3ed852e2009-09-05 21:47:34 +00001552 column,
1553 row;
1554
1555 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1556 break;
1557 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001558 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001559 columns_remaining=columns;
1560 p=tile_pixels+(rows-rows_remaining)*columns;
cristy68473222011-08-17 17:39:19 +00001561 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1562 x);
cristy3ed852e2009-09-05 21:47:34 +00001563 for (row=rows_remaining; row > 0; row--)
1564 {
1565 if (image->matte != MagickFalse)
1566 for (column=columns_remaining; column > 0; column--)
1567 {
cristy4c08aed2011-07-01 19:47:50 +00001568 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1569 TIFFGetR(*p)),q);
1570 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1571 TIFFGetG(*p)),q);
1572 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1573 TIFFGetB(*p)),q);
1574 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1575 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001576 p++;
cristyed231572011-07-14 02:18:59 +00001577 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001578 }
1579 else
1580 for (column=columns_remaining; column > 0; column--)
1581 {
cristy4c08aed2011-07-01 19:47:50 +00001582 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1583 TIFFGetR(*p)),q);
1584 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1585 TIFFGetG(*p)),q);
1586 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1587 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001588 p++;
cristyed231572011-07-14 02:18:59 +00001589 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001590 }
1591 p+=columns-columns_remaining;
cristyed231572011-07-14 02:18:59 +00001592 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
cristy3ed852e2009-09-05 21:47:34 +00001593 }
1594 }
1595 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1596 break;
1597 if (image->previous == (Image *) NULL)
1598 {
cristycee97112010-05-28 00:44:52 +00001599 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1600 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001601 if (status == MagickFalse)
1602 break;
1603 }
1604 }
1605 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1606 break;
1607 }
1608 case ReadGenericMethod:
1609 default:
1610 {
1611 register uint32
1612 *p;
1613
1614 uint32
1615 *pixels;
1616
1617 /*
1618 Convert TIFF image to DirectClass MIFF image.
1619 */
1620 number_pixels=(MagickSizeType) image->columns*image->rows;
1621 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1622 (number_pixels*sizeof(uint32))))
1623 {
1624 TIFFClose(tiff);
1625 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1626 }
1627 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1628 sizeof(uint32));
1629 if (pixels == (uint32 *) NULL)
1630 {
1631 TIFFClose(tiff);
1632 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1633 }
1634 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1635 (uint32) image->rows,(uint32 *) pixels,0);
1636 /*
1637 Convert image to DirectClass pixel packets.
1638 */
1639 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001640 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001641 {
cristybb503372010-05-27 20:51:26 +00001642 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001643 x;
1644
cristy4c08aed2011-07-01 19:47:50 +00001645 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001646 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001647
1648 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001649 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001650 break;
cristyed231572011-07-14 02:18:59 +00001651 q+=GetPixelChannels(image)*(image->columns-1);
cristybb503372010-05-27 20:51:26 +00001652 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001653 {
cristy4c08aed2011-07-01 19:47:50 +00001654 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1655 TIFFGetR(*p)),q);
1656 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1657 TIFFGetG(*p)),q);
1658 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1659 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001660 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001661 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1662 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001663 p--;
cristyed231572011-07-14 02:18:59 +00001664 q-=GetPixelChannels(image);;
cristy3ed852e2009-09-05 21:47:34 +00001665 }
1666 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1667 break;
1668 if (image->previous == (Image *) NULL)
1669 {
cristycee97112010-05-28 00:44:52 +00001670 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1671 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001672 if (status == MagickFalse)
1673 break;
1674 }
1675 }
1676 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1677 break;
1678 }
1679 }
1680 SetQuantumImageType(image,quantum_type);
1681 next_tiff_frame:
cristya9a6ec42011-10-26 16:55:01 +00001682 quantum_info=DestroyQuantumInfo(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +00001683 if ((photometric == PHOTOMETRIC_LOGL) ||
1684 (photometric == PHOTOMETRIC_MINISBLACK) ||
1685 (photometric == PHOTOMETRIC_MINISWHITE))
1686 {
1687 image->type=GrayscaleType;
1688 if (bits_per_sample == 1)
1689 image->type=BilevelType;
1690 }
1691 if (image->storage_class == PseudoClass)
1692 image->depth=GetImageDepth(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001693 if ((photometric == PHOTOMETRIC_LOGL) ||
1694 (photometric == PHOTOMETRIC_MINISBLACK) ||
1695 (photometric == PHOTOMETRIC_MINISWHITE))
1696 {
1697 image->type=GrayscaleType;
1698 if (bits_per_sample == 1)
1699 image->type=BilevelType;
1700 }
1701 /*
1702 Proceed to next image.
1703 */
1704 if (image_info->number_scenes != 0)
1705 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1706 break;
1707 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1708 if (status == MagickTrue)
1709 {
1710 /*
1711 Allocate next image structure.
1712 */
cristy9950d572011-10-01 18:22:35 +00001713 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001714 if (GetNextImageInList(image) == (Image *) NULL)
1715 {
1716 image=DestroyImageList(image);
1717 return((Image *) NULL);
1718 }
1719 image=SyncNextImageInList(image);
1720 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1721 image->scene);
1722 if (status == MagickFalse)
1723 break;
1724 }
cristy3ed852e2009-09-05 21:47:34 +00001725 } while (status == MagickTrue);
1726 (void) TIFFSetWarningHandler(warning_handler);
1727 (void) TIFFSetErrorHandler(error_handler);
1728 TIFFClose(tiff);
1729 return(GetFirstImageInList(image));
1730}
1731#endif
1732
1733/*
1734%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1735% %
1736% %
1737% %
1738% R e g i s t e r T I F F I m a g e %
1739% %
1740% %
1741% %
1742%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1743%
1744% RegisterTIFFImage() adds properties for the TIFF image format to
1745% the list of supported formats. The properties include the image format
1746% tag, a method to read and/or write the format, whether the format
1747% supports the saving of more than one frame to the same file or blob,
1748% whether the format supports native in-memory I/O, and a brief
1749% description of the format.
1750%
1751% The format of the RegisterTIFFImage method is:
1752%
cristybb503372010-05-27 20:51:26 +00001753% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001754%
1755*/
cristy7e3eb0a2011-11-09 22:50:49 +00001756
1757#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
1758static TIFFExtendProc
cristy60e1bf92011-11-09 23:00:15 +00001759 tag_extender = (TIFFExtendProc) NULL;
cristy7e3eb0a2011-11-09 22:50:49 +00001760
1761static void TIFFTagExtender(TIFF *tiff)
1762{
1763 static const TIFFFieldInfo
1764 TIFFExtensions[] =
1765 {
cristy3fac9ec2011-11-17 18:04:39 +00001766 {
1767 37724, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
1768 (char *) "PhotoshopLayerData"
1769 }
cristy7e3eb0a2011-11-09 22:50:49 +00001770 };
1771
1772 TIFFMergeFieldInfo(tiff,TIFFExtensions,sizeof(TIFFExtensions)/
1773 sizeof(*TIFFExtensions));
cristy60e1bf92011-11-09 23:00:15 +00001774 if (tag_extender != (TIFFExtendProc) NULL)
1775 (*tag_extender)(tiff);
cristy7e3eb0a2011-11-09 22:50:49 +00001776}
1777#endif
1778
cristybb503372010-05-27 20:51:26 +00001779ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001780{
1781#define TIFFDescription "Tagged Image File Format"
1782
1783 char
1784 version[MaxTextExtent];
1785
1786 MagickInfo
1787 *entry;
cristy6b032822010-06-29 16:52:32 +00001788
cristy18b17442009-10-25 18:36:48 +00001789 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1790 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001791 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001792 if (instantiate_key == MagickFalse)
1793 {
1794 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1795 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristy7e3eb0a2011-11-09 22:50:49 +00001796#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
cristy60e1bf92011-11-09 23:00:15 +00001797 if (tag_extender == (TIFFExtendProc) NULL)
1798 tag_extender=TIFFSetTagExtender(TIFFTagExtender);
cristy7e3eb0a2011-11-09 22:50:49 +00001799#endif
cristy3ed852e2009-09-05 21:47:34 +00001800 instantiate_key=MagickTrue;
1801 }
cristyf84a1932010-01-03 18:00:18 +00001802 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001803 *version='\0';
1804#if defined(TIFF_VERSION)
cristyb51dff52011-05-19 16:55:47 +00001805 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
cristy3ed852e2009-09-05 21:47:34 +00001806#endif
1807#if defined(MAGICKCORE_TIFF_DELEGATE)
1808 {
1809 const char
1810 *p;
1811
cristybb503372010-05-27 20:51:26 +00001812 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001813 i;
1814
1815 p=TIFFGetVersion();
1816 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1817 version[i]=(*p++);
1818 version[i]='\0';
1819 }
1820#endif
1821
cristy3d7f8062009-09-24 20:45:35 +00001822 entry=SetMagickInfo("GROUP4");
1823#if defined(MAGICKCORE_TIFF_DELEGATE)
1824 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1825 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1826#endif
1827 entry->raw=MagickTrue;
1828 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001829 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001830 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001831 entry->seekable_stream=MagickTrue;
1832 entry->thread_support=NoThreadSupport;
1833 entry->description=ConstantString("Raw CCITT Group4");
1834 entry->module=ConstantString("TIFF");
1835 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001836 entry=SetMagickInfo("PTIF");
1837#if defined(MAGICKCORE_TIFF_DELEGATE)
1838 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1839 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1840#endif
1841 entry->endian_support=MagickTrue;
1842 entry->seekable_stream=MagickTrue;
1843 entry->thread_support=NoThreadSupport;
1844 entry->description=ConstantString("Pyramid encoded TIFF");
1845 entry->module=ConstantString("TIFF");
1846 (void) RegisterMagickInfo(entry);
1847 entry=SetMagickInfo("TIF");
1848#if defined(MAGICKCORE_TIFF_DELEGATE)
1849 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1850 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1851#endif
1852 entry->endian_support=MagickTrue;
1853 entry->seekable_stream=MagickTrue;
1854 entry->stealth=MagickTrue;
1855 entry->thread_support=NoThreadSupport;
1856 entry->description=ConstantString(TIFFDescription);
1857 if (*version != '\0')
1858 entry->version=ConstantString(version);
1859 entry->module=ConstantString("TIFF");
1860 (void) RegisterMagickInfo(entry);
1861 entry=SetMagickInfo("TIFF");
1862#if defined(MAGICKCORE_TIFF_DELEGATE)
1863 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1864 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1865#endif
1866 entry->magick=(IsImageFormatHandler *) IsTIFF;
1867 entry->endian_support=MagickTrue;
1868 entry->seekable_stream=MagickTrue;
1869 entry->thread_support=NoThreadSupport;
1870 entry->description=ConstantString(TIFFDescription);
1871 if (*version != '\0')
1872 entry->version=ConstantString(version);
1873 entry->module=ConstantString("TIFF");
1874 (void) RegisterMagickInfo(entry);
1875 entry=SetMagickInfo("TIFF64");
1876#if defined(TIFF_VERSION_BIG)
1877 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
cristy9d6964e2011-11-10 17:48:45 +00001878 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
cristy3ed852e2009-09-05 21:47:34 +00001879#endif
1880 entry->adjoin=MagickFalse;
1881 entry->endian_support=MagickTrue;
1882 entry->seekable_stream=MagickTrue;
1883 entry->thread_support=NoThreadSupport;
1884 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1885 if (*version != '\0')
1886 entry->version=ConstantString(version);
1887 entry->module=ConstantString("TIFF");
1888 (void) RegisterMagickInfo(entry);
1889 return(MagickImageCoderSignature);
1890}
1891
1892/*
1893%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1894% %
1895% %
1896% %
1897% U n r e g i s t e r T I F F I m a g e %
1898% %
1899% %
1900% %
1901%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1902%
1903% UnregisterTIFFImage() removes format registrations made by the TIFF module
1904% from the list of supported formats.
1905%
1906% The format of the UnregisterTIFFImage method is:
1907%
1908% UnregisterTIFFImage(void)
1909%
1910*/
1911ModuleExport void UnregisterTIFFImage(void)
1912{
cristy3ed852e2009-09-05 21:47:34 +00001913 (void) UnregisterMagickInfo("TIFF64");
cristy9d6964e2011-11-10 17:48:45 +00001914 (void) UnregisterMagickInfo("TIFF");
1915 (void) UnregisterMagickInfo("TIF");
1916 (void) UnregisterMagickInfo("PTIF");
cristy514e9e72009-11-20 02:12:08 +00001917 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1918 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001919 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001920 if (instantiate_key != MagickFalse)
cristy9ebdd932011-11-09 23:07:28 +00001921 {
1922#if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
1923 if (tag_extender == (TIFFExtendProc) NULL)
1924 (void) TIFFSetTagExtender(tag_extender);
1925#endif
1926 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1927 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1928 instantiate_key=MagickFalse;
1929 }
cristyf84a1932010-01-03 18:00:18 +00001930 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001931 DestroySemaphoreInfo(&tiff_semaphore);
1932}
1933
1934#if defined(MAGICKCORE_TIFF_DELEGATE)
1935/*
1936%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1937% %
1938% %
1939% %
cristy3d7f8062009-09-24 20:45:35 +00001940% W r i t e G R O U P 4 I m a g e %
1941% %
1942% %
1943% %
1944%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1945%
1946% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1947%
1948% The format of the WriteGROUP4Image method is:
1949%
1950% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001951% Image *image,ExceptionInfo *)
cristy3d7f8062009-09-24 20:45:35 +00001952%
1953% A description of each parameter follows:
1954%
1955% o image_info: the image info.
1956%
1957% o image: The image.
1958%
cristy3a37efd2011-08-28 20:31:03 +00001959% o exception: return any errors or warnings in this structure.
1960%
cristy3d7f8062009-09-24 20:45:35 +00001961*/
1962static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001963 Image *image,ExceptionInfo *exception)
cristy3d7f8062009-09-24 20:45:35 +00001964{
1965 char
1966 filename[MaxTextExtent];
1967
1968 FILE
1969 *file;
1970
1971 Image
1972 *huffman_image;
1973
1974 ImageInfo
1975 *write_info;
1976
1977 int
1978 unique_file;
1979
1980 MagickBooleanType
1981 status;
1982
cristybb503372010-05-27 20:51:26 +00001983 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001984 i;
1985
1986 ssize_t
1987 count;
1988
1989 TIFF
1990 *tiff;
1991
cristy94c8fe42009-10-06 01:57:36 +00001992 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001993 *byte_count,
1994 strip_size;
1995
1996 unsigned char
1997 *buffer;
1998
1999 /*
2000 Write image as CCITT Group4 TIFF image to a temporary file.
2001 */
2002 assert(image_info != (const ImageInfo *) NULL);
2003 assert(image_info->signature == MagickSignature);
2004 assert(image != (Image *) NULL);
2005 assert(image->signature == MagickSignature);
2006 if (image->debug != MagickFalse)
2007 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002008 assert(exception != (ExceptionInfo *) NULL);
2009 assert(exception->signature == MagickSignature);
2010 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3d7f8062009-09-24 20:45:35 +00002011 if (status == MagickFalse)
2012 return(status);
cristy3a37efd2011-08-28 20:31:03 +00002013 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3d7f8062009-09-24 20:45:35 +00002014 if (huffman_image == (Image *) NULL)
2015 {
2016 (void) CloseBlob(image);
2017 return(MagickFalse);
2018 }
cristy94c8fe42009-10-06 01:57:36 +00002019 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00002020 file=(FILE *) NULL;
2021 unique_file=AcquireUniqueFileResource(filename);
2022 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00002023 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00002024 if ((unique_file == -1) || (file == (FILE *) NULL))
2025 {
cristy3a37efd2011-08-28 20:31:03 +00002026 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2027 filename);
cristy3d7f8062009-09-24 20:45:35 +00002028 return(MagickFalse);
2029 }
cristyb51dff52011-05-19 16:55:47 +00002030 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
cristy3d7f8062009-09-24 20:45:35 +00002031 filename);
cristy018f07f2011-09-04 21:15:19 +00002032 (void) SetImageType(huffman_image,BilevelType,exception);
cristy14efd992009-09-26 23:46:03 +00002033 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00002034 SetImageInfoFile(write_info,file);
2035 write_info->compression=Group4Compression;
2036 write_info->type=BilevelType;
2037 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
cristy3a37efd2011-08-28 20:31:03 +00002038 status=WriteTIFFImage(write_info,huffman_image,exception);
cristy3d7f8062009-09-24 20:45:35 +00002039 (void) fflush(file);
2040 write_info=DestroyImageInfo(write_info);
2041 if (status == MagickFalse)
2042 {
cristy3d7f8062009-09-24 20:45:35 +00002043 huffman_image=DestroyImage(huffman_image);
2044 (void) fclose(file);
2045 (void) RelinquishUniqueFileResource(filename);
2046 return(MagickFalse);
2047 }
2048 tiff=TIFFOpen(filename,"rb");
2049 if (tiff == (TIFF *) NULL)
2050 {
2051 huffman_image=DestroyImage(huffman_image);
2052 (void) fclose(file);
2053 (void) RelinquishUniqueFileResource(filename);
cristy3a37efd2011-08-28 20:31:03 +00002054 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
cristy3d7f8062009-09-24 20:45:35 +00002055 image_info->filename);
2056 return(MagickFalse);
2057 }
2058 /*
2059 Allocate raw strip buffer.
2060 */
cristy94c8fe42009-10-06 01:57:36 +00002061 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2062 {
2063 TIFFClose(tiff);
2064 huffman_image=DestroyImage(huffman_image);
2065 (void) fclose(file);
2066 (void) RelinquishUniqueFileResource(filename);
2067 return(MagickFalse);
2068 }
cristy3d7f8062009-09-24 20:45:35 +00002069 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00002070 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002071 if (byte_count[i] > strip_size)
2072 strip_size=byte_count[i];
2073 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2074 sizeof(*buffer));
2075 if (buffer == (unsigned char *) NULL)
2076 {
2077 TIFFClose(tiff);
2078 huffman_image=DestroyImage(huffman_image);
2079 (void) fclose(file);
2080 (void) RelinquishUniqueFileResource(filename);
2081 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2082 image_info->filename);
2083 }
2084 /*
2085 Compress runlength encoded to 2D Huffman pixels.
2086 */
cristybb503372010-05-27 20:51:26 +00002087 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002088 {
cristy94c8fe42009-10-06 01:57:36 +00002089 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00002090 if (WriteBlob(image,(size_t) count,buffer) != count)
2091 status=MagickFalse;
2092 }
2093 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2094 TIFFClose(tiff);
2095 huffman_image=DestroyImage(huffman_image);
2096 (void) fclose(file);
2097 (void) RelinquishUniqueFileResource(filename);
2098 (void) CloseBlob(image);
2099 return(status);
2100}
2101#endif
2102
2103#if defined(MAGICKCORE_TIFF_DELEGATE)
2104/*
2105%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2106% %
2107% %
2108% %
cristy3ed852e2009-09-05 21:47:34 +00002109% W r i t e P T I F I m a g e %
2110% %
2111% %
2112% %
2113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2114%
2115% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2116% format.
2117%
2118% The format of the WritePTIFImage method is:
2119%
2120% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002121% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002122%
2123% A description of each parameter follows:
2124%
2125% o image_info: the image info.
2126%
2127% o image: The image.
2128%
cristy3a37efd2011-08-28 20:31:03 +00002129% o exception: return any errors or warnings in this structure.
2130%
cristy3ed852e2009-09-05 21:47:34 +00002131*/
2132static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002133 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002134{
2135 Image
2136 *images,
2137 *next,
2138 *pyramid_image;
2139
2140 ImageInfo
2141 *write_info;
2142
2143 MagickBooleanType
2144 status;
2145
cristyf164a872012-01-20 18:26:34 +00002146 PointInfo
2147 resolution;
2148
cristybb503372010-05-27 20:51:26 +00002149 size_t
cristy3ed852e2009-09-05 21:47:34 +00002150 columns,
2151 rows;
2152
2153 /*
2154 Create pyramid-encoded TIFF image.
2155 */
2156 images=NewImageList();
2157 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2158 {
cristy3a37efd2011-08-28 20:31:03 +00002159 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,exception));
cristy3ed852e2009-09-05 21:47:34 +00002160 columns=next->columns;
2161 rows=next->rows;
cristyf164a872012-01-20 18:26:34 +00002162 resolution=next->resolution;
cristy3ed852e2009-09-05 21:47:34 +00002163 while ((columns > 64) && (rows > 64))
2164 {
2165 columns/=2;
2166 rows/=2;
cristyf164a872012-01-20 18:26:34 +00002167 resolution.x/=2;
2168 resolution.y/=2;
cristy98ca0f52011-10-08 23:19:10 +00002169 pyramid_image=ResizeImage(next,columns,rows,image->filter,image->blur,
cristy3a37efd2011-08-28 20:31:03 +00002170 exception);
cristyf164a872012-01-20 18:26:34 +00002171 if (pyramid_image == (Image *) NULL)
2172 break;
2173 pyramid_image->resolution=resolution;
cristy3ed852e2009-09-05 21:47:34 +00002174 AppendImageToList(&images,pyramid_image);
2175 }
2176 }
cristyf164a872012-01-20 18:26:34 +00002177 images=GetFirstImageInList(images);
cristy3ed852e2009-09-05 21:47:34 +00002178 /*
2179 Write pyramid-encoded TIFF image.
2180 */
cristy58567532009-09-26 01:13:32 +00002181 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002182 write_info->adjoin=MagickTrue;
cristyf164a872012-01-20 18:26:34 +00002183 (void) CopyMagickString(write_info->magick,"TIFF",MaxTextExtent);
2184 (void) CopyMagickString(images->magick,"TIFF",MaxTextExtent);
2185 status=WriteTIFFImage(write_info,images,exception);
cristy3ed852e2009-09-05 21:47:34 +00002186 images=DestroyImageList(images);
2187 write_info=DestroyImageInfo(write_info);
2188 return(status);
2189}
2190#endif
2191
2192#if defined(MAGICKCORE_TIFF_DELEGATE)
2193/*
2194%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2195% %
2196% %
2197% W r i t e T I F F I m a g e %
2198% %
2199% %
2200% %
2201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2202%
2203% WriteTIFFImage() writes an image in the Tagged image file format.
2204%
2205% The format of the WriteTIFFImage method is:
2206%
2207% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002208% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002209%
2210% A description of each parameter follows:
2211%
2212% o image_info: the image info.
2213%
2214% o image: The image.
2215%
cristy3a37efd2011-08-28 20:31:03 +00002216% o exception: return any errors or warnings in this structure.
2217%
cristy3ed852e2009-09-05 21:47:34 +00002218*/
2219
2220typedef struct _TIFFInfo
2221{
2222 RectangleInfo
2223 tile_geometry;
2224
2225 unsigned char
2226 *scanline,
2227 *scanlines,
2228 *pixels;
2229} TIFFInfo;
2230
2231static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2232{
2233 assert(tiff_info != (TIFFInfo *) NULL);
2234 if (tiff_info->scanlines != (unsigned char *) NULL)
2235 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2236 tiff_info->scanlines);
2237 if (tiff_info->pixels != (unsigned char *) NULL)
2238 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2239 tiff_info->pixels);
2240}
2241
2242static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2243 TIFFInfo *tiff_info)
2244{
2245 const char
2246 *option;
2247
2248 MagickStatusType
2249 flags;
2250
cristy79e5dad2010-09-16 19:48:33 +00002251 uint32
2252 tile_columns,
2253 tile_rows;
2254
cristy3ed852e2009-09-05 21:47:34 +00002255 assert(tiff_info != (TIFFInfo *) NULL);
2256 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2257 option=GetImageOption(image_info,"tiff:tile-geometry");
2258 if (option == (const char *) NULL)
2259 return(MagickTrue);
2260 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2261 if ((flags & HeightValue) == 0)
2262 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002263 tile_columns=(uint32) tiff_info->tile_geometry.width;
2264 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002265 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2266 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2267 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2268 tiff_info->tile_geometry.width=tile_columns;
2269 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002270 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002271 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002272 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002273 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002274 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2275 (tiff_info->pixels == (unsigned char *) NULL))
2276 {
2277 DestroyTIFFInfo(tiff_info);
2278 return(MagickFalse);
2279 }
2280 return(MagickTrue);
2281}
2282
cristybb503372010-05-27 20:51:26 +00002283static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002284 tsample_t sample,Image *image)
2285{
2286 int32
2287 status;
2288
cristybb503372010-05-27 20:51:26 +00002289 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002290 i;
2291
2292 register unsigned char
2293 *p,
2294 *q;
2295
cristybb503372010-05-27 20:51:26 +00002296 size_t
cristy3ed852e2009-09-05 21:47:34 +00002297 number_tiles,
2298 tile_width;
2299
cristyc6da28e2011-04-28 01:41:35 +00002300 ssize_t
2301 bytes_per_pixel,
2302 j,
2303 k,
2304 l;
2305
cristy3ed852e2009-09-05 21:47:34 +00002306 if (TIFFIsTiled(tiff) == 0)
2307 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2308 /*
2309 Fill scanlines to tile height.
2310 */
cristybb503372010-05-27 20:51:26 +00002311 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002312 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2313 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002314 if (((size_t) (row % tiff_info->tile_geometry.height) !=
cristyc6da28e2011-04-28 01:41:35 +00002315 (tiff_info->tile_geometry.height-1)) &&
2316 (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002317 return(0);
2318 /*
2319 Write tile to TIFF image.
2320 */
2321 status=0;
cristy32fab702012-01-18 02:47:47 +00002322 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (
2323 tiff_info->tile_geometry.height*tiff_info->tile_geometry.width);
cristy3ed852e2009-09-05 21:47:34 +00002324 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2325 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002326 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002327 {
cristybb503372010-05-27 20:51:26 +00002328 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002329 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002330 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2331 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002332 {
2333 if (bytes_per_pixel == 0)
2334 {
2335 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2336 tiff_info->tile_geometry.width+k)/8);
2337 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2338 *q++=(*p++);
2339 continue;
2340 }
2341 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2342 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2343 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2344 for (l=0; l < bytes_per_pixel; l++)
2345 *q++=(*p++);
2346 }
cristydaff8092010-04-22 14:50:53 +00002347 if ((i*tiff_info->tile_geometry.width) != image->columns)
2348 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2349 tiff_info->tile_geometry.width),(uint32) ((row/
2350 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2351 sample);
cristy3ed852e2009-09-05 21:47:34 +00002352 if (status < 0)
2353 break;
2354 }
2355 return(status);
2356}
2357
2358static void TIFFSetProfiles(TIFF *tiff,Image *image)
2359{
2360 const char
2361 *name;
2362
2363 const StringInfo
2364 *profile;
2365
2366 if (image->profiles == (void *) NULL)
2367 return;
2368 ResetImageProfileIterator(image);
2369 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2370 {
2371 profile=GetImageProfile(image,name);
2372#if defined(TIFFTAG_XMLPACKET)
2373 if (LocaleCompare(name,"xmp") == 0)
2374 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2375 profile),GetStringInfoDatum(profile));
2376#endif
2377#if defined(TIFFTAG_ICCPROFILE)
2378 if (LocaleCompare(name,"icc") == 0)
2379 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2380 profile),GetStringInfoDatum(profile));
2381#endif
2382 if (LocaleCompare(name,"iptc") == 0)
2383 {
2384 size_t
2385 length;
2386
2387 StringInfo
2388 *iptc_profile;
2389
2390 iptc_profile=CloneStringInfo(profile);
2391 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2392 0x03);
2393 SetStringInfoLength(iptc_profile,length);
2394 if (TIFFIsByteSwapped(tiff))
2395 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002396 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002397 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2398 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2399 iptc_profile=DestroyStringInfo(iptc_profile);
2400 }
2401#if defined(TIFFTAG_PHOTOSHOP)
2402 if (LocaleCompare(name,"8bim") == 0)
2403 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2404 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2405#endif
2406 if (LocaleCompare(name,"tiff:37724") == 0)
cristye5d85502011-11-09 19:45:06 +00002407 (void) TIFFSetField(tiff,37724,(uint32) GetStringInfoLength(profile),
cristy3ed852e2009-09-05 21:47:34 +00002408 GetStringInfoDatum(profile));
2409 name=GetNextImageProfile(image);
2410 }
2411}
2412
cristyd15e6592011-10-15 00:13:06 +00002413static void TIFFSetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002414{
2415 const char
2416 *value;
2417
cristy15d8d212010-09-17 01:56:30 +00002418 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristyd15e6592011-10-15 00:13:06 +00002419 value=GetImageProperty(image,"tiff:hostcomputer",exception);
cristy3ed852e2009-09-05 21:47:34 +00002420 if (value != (const char *) NULL)
2421 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
cristyd15e6592011-10-15 00:13:06 +00002422 value=GetImageProperty(image,"tiff:artist",exception);
cristy3ed852e2009-09-05 21:47:34 +00002423 if (value != (const char *) NULL)
2424 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
cristyd15e6592011-10-15 00:13:06 +00002425 value=GetImageProperty(image,"tiff:timestamp",exception);
cristy3ed852e2009-09-05 21:47:34 +00002426 if (value != (const char *) NULL)
2427 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
cristyd15e6592011-10-15 00:13:06 +00002428 value=GetImageProperty(image,"tiff:make",exception);
cristy3ed852e2009-09-05 21:47:34 +00002429 if (value != (const char *) NULL)
2430 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
cristyd15e6592011-10-15 00:13:06 +00002431 value=GetImageProperty(image,"tiff:model",exception);
cristy3ed852e2009-09-05 21:47:34 +00002432 if (value != (const char *) NULL)
2433 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristyd15e6592011-10-15 00:13:06 +00002434 value=GetImageProperty(image,"tiff:software",exception);
cristy15d8d212010-09-17 01:56:30 +00002435 if (value != (const char *) NULL)
2436 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristyd15e6592011-10-15 00:13:06 +00002437 value=GetImageProperty(image,"tiff:copyright",exception);
cristy3ed852e2009-09-05 21:47:34 +00002438 if (value != (const char *) NULL)
cristy98ca0f52011-10-08 23:19:10 +00002439 (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value);
cristyd15e6592011-10-15 00:13:06 +00002440 value=GetImageProperty(image,"kodak-33423",exception);
cristy3ed852e2009-09-05 21:47:34 +00002441 if (value != (const char *) NULL)
2442 (void) TIFFSetField(tiff,33423,value);
cristyd15e6592011-10-15 00:13:06 +00002443 value=GetImageProperty(image,"kodak-36867",exception);
cristy3ed852e2009-09-05 21:47:34 +00002444 if (value != (const char *) NULL)
2445 (void) TIFFSetField(tiff,36867,value);
cristyd15e6592011-10-15 00:13:06 +00002446 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00002447 if (value != (const char *) NULL)
2448 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
cristyd15e6592011-10-15 00:13:06 +00002449 value=GetImageProperty(image,"comment",exception);
cristy3ed852e2009-09-05 21:47:34 +00002450 if (value != (const char *) NULL)
2451 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2452}
2453
cristyd15e6592011-10-15 00:13:06 +00002454static void TIFFSetEXIFProperties(TIFF *tiff,Image *image,
2455 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002456{
2457#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2458 const char
2459 *value;
2460
cristybb503372010-05-27 20:51:26 +00002461 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002462 i;
2463
2464 uint32
2465 offset;
2466
2467 /*
2468 Write EXIF properties.
2469 */
2470 offset=0;
2471 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2472 for (i=0; exif_info[i].tag != 0; i++)
2473 {
cristyd15e6592011-10-15 00:13:06 +00002474 value=GetImageProperty(image,exif_info[i].property,exception);
cristy3ed852e2009-09-05 21:47:34 +00002475 if (value == (const char *) NULL)
2476 continue;
2477 switch (exif_info[i].type)
2478 {
2479 case TIFF_ASCII:
2480 {
2481 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2482 break;
2483 }
2484 case TIFF_SHORT:
2485 {
2486 uint16
2487 shorty;
2488
cristyf2f27272009-12-17 14:48:46 +00002489 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002490 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2491 break;
2492 }
2493 case TIFF_LONG:
2494 {
2495 uint16
cristybb503372010-05-27 20:51:26 +00002496 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002497
cristybb503372010-05-27 20:51:26 +00002498 ssize_ty=(uint16) StringToLong(value);
2499 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002500 break;
2501 }
2502 case TIFF_RATIONAL:
2503 case TIFF_SRATIONAL:
2504 {
2505 float
2506 rational;
2507
cristydbdd0e32011-11-04 23:29:40 +00002508 rational=StringToDouble(value,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002509 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2510 break;
2511 }
2512 default:
2513 break;
2514 }
2515 }
2516 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2517#else
2518 (void) tiff;
2519 (void) image;
2520#endif
2521}
2522
2523static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002524 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002525{
2526#if !defined(TIFFDefaultStripSize)
2527#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2528#endif
2529
2530 const char
2531 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002532 *option;
cristy3ed852e2009-09-05 21:47:34 +00002533
2534 CompressionType
2535 compression;
2536
cristy6b032822010-06-29 16:52:32 +00002537 EndianType
2538 endian_type;
2539
cristy3ed852e2009-09-05 21:47:34 +00002540 MagickBooleanType
2541 debug,
2542 status;
2543
2544 MagickOffsetType
2545 scene;
2546
2547 QuantumInfo
2548 *quantum_info;
2549
2550 QuantumType
2551 quantum_type;
2552
cristybb503372010-05-27 20:51:26 +00002553 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002554 i;
2555
2556 size_t
cristy9945c6e2012-01-23 19:49:26 +00002557 length;
cristy3ed852e2009-09-05 21:47:34 +00002558
cristyc6da28e2011-04-28 01:41:35 +00002559 ssize_t
2560 y;
2561
cristy3ed852e2009-09-05 21:47:34 +00002562 TIFF
2563 *tiff;
2564
2565 TIFFErrorHandler
2566 error_handler,
2567 warning_handler;
2568
2569 TIFFInfo
2570 tiff_info;
2571
2572 uint16
2573 bits_per_sample,
2574 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002575 endian,
cristy3ed852e2009-09-05 21:47:34 +00002576 photometric;
2577
2578 uint32
2579 rows_per_strip;
2580
2581 unsigned char
2582 *pixels;
2583
cristy3ed852e2009-09-05 21:47:34 +00002584 /*
2585 Open TIFF file.
2586 */
2587 assert(image_info != (const ImageInfo *) NULL);
2588 assert(image_info->signature == MagickSignature);
2589 assert(image != (Image *) NULL);
2590 assert(image->signature == MagickSignature);
2591 if (image->debug != MagickFalse)
2592 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002593 assert(exception != (ExceptionInfo *) NULL);
2594 assert(exception->signature == MagickSignature);
2595 assert(exception != (ExceptionInfo *) NULL);
2596 assert(exception->signature == MagickSignature);
2597 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00002598 if (status == MagickFalse)
2599 return(status);
cristy3a37efd2011-08-28 20:31:03 +00002600 (void) MagickSetThreadValue(tiff_exception,exception);
cristy3ed852e2009-09-05 21:47:34 +00002601 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2602 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002603 endian_type=UndefinedEndian;
2604 option=GetImageOption(image_info,"tiff:endian");
2605 if (option != (const char *) NULL)
2606 {
2607 if (LocaleNCompare(option,"msb",3) == 0)
2608 endian_type=MSBEndian;
2609 if (LocaleNCompare(option,"lsb",3) == 0)
2610 endian_type=LSBEndian;;
2611 }
2612 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002613 {
2614 case LSBEndian: mode="wl"; break;
2615 case MSBEndian: mode="wb"; break;
2616 default: mode="w"; break;
2617 }
2618#if defined(TIFF_VERSION_BIG)
2619 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002620 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002621 {
2622 case LSBEndian: mode="wl8"; break;
2623 case MSBEndian: mode="wb8"; break;
2624 default: mode="w8"; break;
2625 }
2626#endif
2627 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2628 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2629 TIFFUnmapBlob);
2630 if (tiff == (TIFF *) NULL)
2631 {
2632 (void) TIFFSetWarningHandler(warning_handler);
2633 (void) TIFFSetErrorHandler(error_handler);
2634 return(MagickFalse);
2635 }
2636 scene=0;
2637 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +00002638 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +00002639 do
2640 {
2641 /*
2642 Initialize TIFF fields.
2643 */
cristy5f1c1ff2010-12-23 21:38:06 +00002644 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002645 (image_info->type != OptimizeType))
cristy018f07f2011-09-04 21:15:19 +00002646 (void) SetImageType(image,image_info->type,exception);
cristy3ed852e2009-09-05 21:47:34 +00002647 quantum_info=AcquireQuantumInfo(image_info,image);
2648 if (quantum_info == (QuantumInfo *) NULL)
2649 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2650 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2651 (quantum_info->format == UndefinedQuantumFormat) &&
cristy3a37efd2011-08-28 20:31:03 +00002652 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002653 {
2654 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2655 if (status == MagickFalse)
2656 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2657 }
2658 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2659 (GetPreviousImageInList(image) != (Image *) NULL))
2660 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2661 if ((image->columns != (uint32) image->columns) ||
2662 (image->rows != (uint32) image->rows))
2663 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2664 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2665 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2666 compression=image->compression;
2667 if (image_info->compression != UndefinedCompression)
2668 compression=image_info->compression;
2669 switch (compression)
2670 {
2671 case FaxCompression:
2672 {
2673 compress_tag=COMPRESSION_CCITTFAX3;
2674 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2675 break;
2676 }
2677 case Group4Compression:
2678 {
2679 compress_tag=COMPRESSION_CCITTFAX4;
2680 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2681 break;
2682 }
cristy6d5e20f2011-04-25 13:48:54 +00002683#if defined(COMPRESSION_JBIG)
2684 case JBIG1Compression:
2685 {
2686 compress_tag=COMPRESSION_JBIG;
2687 break;
2688 }
2689#endif
cristy3ed852e2009-09-05 21:47:34 +00002690 case JPEGCompression:
2691 {
2692 compress_tag=COMPRESSION_JPEG;
2693 break;
2694 }
cristyfbb0ef02010-12-19 02:32:11 +00002695#if defined(COMPRESSION_LZMA)
2696 case LZMACompression:
2697 {
2698 compress_tag=COMPRESSION_LZMA;
2699 break;
2700 }
2701#endif
cristy3ed852e2009-09-05 21:47:34 +00002702 case LZWCompression:
2703 {
2704 compress_tag=COMPRESSION_LZW;
2705 break;
2706 }
2707 case RLECompression:
2708 {
2709 compress_tag=COMPRESSION_PACKBITS;
2710 break;
2711 }
2712 case ZipCompression:
2713 {
2714 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2715 break;
2716 }
2717 case NoCompression:
2718 default:
2719 {
2720 compress_tag=COMPRESSION_NONE;
2721 break;
2722 }
2723 }
cristy5b675872009-10-21 13:12:00 +00002724#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2725 if ((compress_tag != COMPRESSION_NONE) &&
2726 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002727 {
cristy3a37efd2011-08-28 20:31:03 +00002728 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2729 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002730 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002731 compress_tag=COMPRESSION_NONE;
2732 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002733 }
2734#else
2735 switch (compress_tag)
2736 {
2737#if defined(CCITT_SUPPORT)
2738 case COMPRESSION_CCITTFAX3:
2739 case COMPRESSION_CCITTFAX4:
2740#endif
2741#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2742 case COMPRESSION_JPEG:
2743#endif
cristyfbb0ef02010-12-19 02:32:11 +00002744#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2745 case COMPRESSION_LZMA:
2746#endif
cristy3ed852e2009-09-05 21:47:34 +00002747#if defined(LZW_SUPPORT)
2748 case COMPRESSION_LZW:
2749#endif
2750#if defined(PACKBITS_SUPPORT)
2751 case COMPRESSION_PACKBITS:
2752#endif
2753#if defined(ZIP_SUPPORT)
2754 case COMPRESSION_ADOBE_DEFLATE:
2755#endif
2756 case COMPRESSION_NONE:
2757 break;
2758 default:
2759 {
cristy3a37efd2011-08-28 20:31:03 +00002760 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2761 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002762 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002763 compress_tag=COMPRESSION_NONE;
2764 compression=NoCompression;
2765 break;
2766 }
2767 }
2768#endif
2769 switch (compression)
2770 {
2771 case FaxCompression:
2772 case Group4Compression:
2773 {
cristy018f07f2011-09-04 21:15:19 +00002774 (void) SetImageType(image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +00002775 break;
2776 }
2777 case JPEGCompression:
2778 {
cristy3a37efd2011-08-28 20:31:03 +00002779 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a11cb12011-10-19 23:53:34 +00002780 (void) SetImageDepth(image,8,exception);
cristy3ed852e2009-09-05 21:47:34 +00002781 break;
2782 }
2783 default:
2784 break;
2785 }
cristy3ed852e2009-09-05 21:47:34 +00002786 if (image->colorspace == CMYKColorspace)
2787 {
2788 photometric=PHOTOMETRIC_SEPARATED;
2789 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2790 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2791 }
2792 else
2793 {
2794 /*
2795 Full color TIFF raster.
2796 */
2797 if (image->colorspace == LabColorspace)
2798 photometric=PHOTOMETRIC_CIELAB;
2799 else
2800 if (image->colorspace == YCbCrColorspace)
2801 {
2802 photometric=PHOTOMETRIC_YCBCR;
2803 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy3a37efd2011-08-28 20:31:03 +00002804 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a11cb12011-10-19 23:53:34 +00002805 (void) SetImageDepth(image,8,exception);
cristy3ed852e2009-09-05 21:47:34 +00002806 }
2807 else
2808 {
cristy510d06a2011-07-06 23:43:54 +00002809 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +00002810 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00002811 photometric=PHOTOMETRIC_RGB;
2812 }
2813 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2814 if ((image_info->type != TrueColorType) &&
2815 (image_info->type != TrueColorMatteType))
2816 {
cristy94c8fe42009-10-06 01:57:36 +00002817 if ((image_info->type != PaletteType) &&
cristy3a37efd2011-08-28 20:31:03 +00002818 (IsImageGray(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002819 {
cristy94c8fe42009-10-06 01:57:36 +00002820 photometric=(uint16) (quantum_info->min_is_white !=
2821 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2822 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002823 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002824 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy3a37efd2011-08-28 20:31:03 +00002825 (IsImageMonochrome(image,exception) != MagickFalse))
cristy94c8fe42009-10-06 01:57:36 +00002826 {
2827 status=SetQuantumDepth(image,quantum_info,1);
2828 if (status == MagickFalse)
2829 ThrowWriterException(ResourceLimitError,
2830 "MemoryAllocationFailed");
2831 }
cristy3ed852e2009-09-05 21:47:34 +00002832 }
2833 else
cristy94c8fe42009-10-06 01:57:36 +00002834 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002835 {
cristybb503372010-05-27 20:51:26 +00002836 size_t
cristy94c8fe42009-10-06 01:57:36 +00002837 depth;
2838
2839 /*
2840 Colormapped TIFF raster.
2841 */
cristy3ed852e2009-09-05 21:47:34 +00002842 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002843 photometric=PHOTOMETRIC_PALETTE;
2844 depth=1;
2845 while ((GetQuantumRange(depth)+1) < image->colors)
2846 depth<<=1;
2847 status=SetQuantumDepth(image,quantum_info,depth);
2848 if (status == MagickFalse)
2849 ThrowWriterException(ResourceLimitError,
2850 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002851 }
2852 }
2853 }
cristyf2687ca2010-06-29 16:32:38 +00002854 switch (image->endian)
2855 {
2856 case LSBEndian:
2857 {
2858 endian=FILLORDER_LSB2MSB;
2859 break;
2860 }
2861 case MSBEndian:
2862 {
2863 endian=FILLORDER_MSB2LSB;
2864 break;
2865 }
2866 case UndefinedEndian:
2867 default:
2868 {
2869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2870 break;
2871 }
2872 }
cristydaff8092010-04-22 14:50:53 +00002873 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2874 (photometric != PHOTOMETRIC_MINISWHITE))
2875 {
2876 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002877 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002878 }
cristy0accf6e2009-10-01 13:14:28 +00002879 else
cristydaff8092010-04-22 14:50:53 +00002880 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2881 (photometric != PHOTOMETRIC_MINISWHITE))
2882 {
2883 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002884 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002885 }
cristyd15e6592011-10-15 00:13:06 +00002886 option=GetImageProperty(image,"tiff:fill-order",exception);
cristy62e282b2010-06-29 01:27:13 +00002887 if (option != (const char *) NULL)
2888 {
2889 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002890 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002891 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002892 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002893 }
cristyf2687ca2010-06-29 16:32:38 +00002894 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2895 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002896 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2897 if (image->matte != MagickFalse)
2898 {
2899 uint16
2900 extra_samples,
2901 sample_info[1],
2902 samples_per_pixel;
2903
2904 /*
2905 TIFF has a matte channel.
2906 */
2907 extra_samples=1;
2908 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
cristyd15e6592011-10-15 00:13:06 +00002909 option=GetImageProperty(image,"tiff:alpha",exception);
cristy3ed852e2009-09-05 21:47:34 +00002910 if ((option != (const char *) NULL) &&
2911 (LocaleCompare(option,"associated") == 0))
2912 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2913 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2914 &samples_per_pixel);
2915 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2916 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2917 &sample_info);
2918 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2919 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2920 }
2921 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2922 switch (quantum_info->format)
2923 {
2924 case FloatingPointQuantumFormat:
2925 {
2926 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2927 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2928 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2929 break;
2930 }
2931 case SignedQuantumFormat:
2932 {
2933 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2934 break;
2935 }
2936 case UnsignedQuantumFormat:
2937 {
2938 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2939 break;
2940 }
2941 default:
2942 break;
2943 }
cristy3ed852e2009-09-05 21:47:34 +00002944 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2945 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2946 if (photometric == PHOTOMETRIC_RGB)
2947 if ((image_info->interlace == PlaneInterlace) ||
2948 (image_info->interlace == PartitionInterlace))
2949 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2950 rows_per_strip=1;
2951 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002952 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2953 1);
cristy3ed852e2009-09-05 21:47:34 +00002954 option=GetImageOption(image_info,"tiff:rows-per-strip");
2955 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002956 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002957 switch (compress_tag)
2958 {
2959 case COMPRESSION_JPEG:
2960 {
2961#if defined(JPEG_SUPPORT)
2962 const char
2963 *sampling_factor;
2964
2965 GeometryInfo
2966 geometry_info;
2967
2968 MagickStatusType
2969 flags;
2970
2971 rows_per_strip+=(16-(rows_per_strip % 16));
2972 if (image->quality != 0)
2973 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2974 if (image_info->quality != UndefinedCompressionQuality)
2975 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2976 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
cristy510d06a2011-07-06 23:43:54 +00002977 if (IsRGBColorspace(image->colorspace) == MagickTrue)
cristy3ed852e2009-09-05 21:47:34 +00002978 {
cristy949bf5b2010-05-08 02:47:03 +00002979 const char
2980 *value;
2981
cristy3ed852e2009-09-05 21:47:34 +00002982 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2983 sampling_factor=(const char *) NULL;
cristyd15e6592011-10-15 00:13:06 +00002984 value=GetImageProperty(image,"jpeg:sampling-factor",exception);
cristy3ed852e2009-09-05 21:47:34 +00002985 if (value != (char *) NULL)
2986 {
2987 sampling_factor=value;
2988 if (image->debug != MagickFalse)
2989 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2990 " Input sampling-factors=%s",sampling_factor);
2991 }
2992 if (image_info->sampling_factor != (char *) NULL)
2993 sampling_factor=image_info->sampling_factor;
2994 if (sampling_factor != (const char *) NULL)
2995 {
2996 flags=ParseGeometry(sampling_factor,&geometry_info);
2997 if ((flags & SigmaValue) == 0)
2998 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002999 if (image->colorspace == YCbCrColorspace)
3000 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
3001 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00003002 }
3003 }
3004 if (bits_per_sample == 12)
3005 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
3006#endif
3007 break;
3008 }
3009 case COMPRESSION_ADOBE_DEFLATE:
3010 {
cristyf6fe0a12010-05-30 00:44:47 +00003011 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003012 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3013 &bits_per_sample);
3014 if (((photometric == PHOTOMETRIC_RGB) ||
3015 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3016 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00003017 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00003018 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
3019 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00003020 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00003021 break;
3022 }
3023 case COMPRESSION_CCITTFAX3:
3024 {
3025 /*
3026 Byte-aligned EOL.
3027 */
cristyeaedf062010-05-29 22:36:02 +00003028 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003029 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
3030 break;
3031 }
3032 case COMPRESSION_CCITTFAX4:
3033 {
cristyeaedf062010-05-29 22:36:02 +00003034 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00003035 break;
3036 }
cristyef8f26b2010-12-19 20:29:16 +00003037#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3038 case COMPRESSION_LZMA:
3039 {
3040 if (((photometric == PHOTOMETRIC_RGB) ||
3041 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3042 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3043 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3044 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
3045 image_info->quality == UndefinedCompressionQuality ? 7 :
3046 MagickMin((ssize_t) image_info->quality/10,9)));
3047 break;
3048 }
3049#endif
cristy3ed852e2009-09-05 21:47:34 +00003050 case COMPRESSION_LZW:
3051 {
3052 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3053 &bits_per_sample);
3054 if (((photometric == PHOTOMETRIC_RGB) ||
3055 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3056 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00003057 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00003058 break;
3059 }
3060 default:
3061 break;
3062 }
cristy79e5dad2010-09-16 19:48:33 +00003063 option=GetImageOption(image_info,"tiff:tile-geometry");
3064 if (option == (const char *) NULL)
3065 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy2a11bef2011-10-28 18:33:11 +00003066 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
cristy3ed852e2009-09-05 21:47:34 +00003067 {
3068 unsigned short
3069 units;
3070
3071 /*
3072 Set image resolution.
3073 */
3074 units=RESUNIT_NONE;
3075 if (image->units == PixelsPerInchResolution)
3076 units=RESUNIT_INCH;
3077 if (image->units == PixelsPerCentimeterResolution)
3078 units=RESUNIT_CENTIMETER;
3079 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
cristy2a11bef2011-10-28 18:33:11 +00003080 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x);
3081 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00003082 if ((image->page.x != 0) || (image->page.y != 0))
3083 {
3084 /*
3085 Set image position.
3086 */
3087 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
cristy2a11bef2011-10-28 18:33:11 +00003088 image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00003089 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
cristy2a11bef2011-10-28 18:33:11 +00003090 image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00003091 }
3092 }
3093 if (image->chromaticity.white_point.x != 0.0)
3094 {
3095 float
3096 chromaticity[6];
3097
3098 /*
3099 Set image chromaticity.
3100 */
3101 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3102 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3103 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3104 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3105 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3106 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3107 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3108 chromaticity[0]=(float) image->chromaticity.white_point.x;
3109 chromaticity[1]=(float) image->chromaticity.white_point.y;
3110 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3111 }
cristy59788de2011-11-23 13:56:19 +00003112 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3113 (image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
cristy3ed852e2009-09-05 21:47:34 +00003114 {
3115 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3116 if (image->scene != 0)
3117 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3118 GetImageListLength(image));
3119 }
3120 if (image->orientation != UndefinedOrientation)
3121 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3122 (void) TIFFSetProfiles(tiff,image);
3123 {
3124 uint16
3125 page,
3126 pages;
3127
3128 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00003129 pages=(uint16) GetImageListLength(image);
cristy59788de2011-11-23 13:56:19 +00003130 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3131 (image_info->adjoin != MagickFalse) && (pages > 1))
cristy3ed852e2009-09-05 21:47:34 +00003132 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3133 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3134 }
cristyd15e6592011-10-15 00:13:06 +00003135 (void) TIFFSetProperties(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003136 if (0)
cristyd15e6592011-10-15 00:13:06 +00003137 (void) TIFFSetEXIFProperties(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003138 /*
3139 Write image scanlines.
3140 */
3141 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3142 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristyf164a872012-01-20 18:26:34 +00003143 quantum_info->endian=LSBEndian;
3144 if (endian == FILLORDER_LSB2MSB)
3145 quantum_info->endian=MSBEndian;
cristy9945c6e2012-01-23 19:49:26 +00003146 image->endian=quantum_info->endian;
cristy3ed852e2009-09-05 21:47:34 +00003147 pixels=GetQuantumPixels(quantum_info);
3148 tiff_info.scanline=GetQuantumPixels(quantum_info);
3149 switch (photometric)
3150 {
3151 case PHOTOMETRIC_CIELAB:
3152 case PHOTOMETRIC_YCBCR:
3153 case PHOTOMETRIC_RGB:
3154 {
3155 /*
3156 RGB TIFF image.
3157 */
3158 switch (image_info->interlace)
3159 {
3160 case NoInterlace:
3161 default:
3162 {
3163 quantum_type=RGBQuantum;
3164 if (image->matte != MagickFalse)
3165 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003166 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003167 {
cristy4c08aed2011-07-01 19:47:50 +00003168 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003169 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003170
cristy3a37efd2011-08-28 20:31:03 +00003171 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003172 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003173 break;
cristy4c08aed2011-07-01 19:47:50 +00003174 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003175 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00003176 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00003177 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3178 break;
3179 if (image->previous == (Image *) NULL)
3180 {
cristy2837bcc2010-08-07 23:57:39 +00003181 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3182 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003183 if (status == MagickFalse)
3184 break;
3185 }
3186 }
3187 break;
3188 }
3189 case PlaneInterlace:
3190 case PartitionInterlace:
3191 {
3192 /*
3193 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3194 */
cristybb503372010-05-27 20:51:26 +00003195 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003196 {
cristy4c08aed2011-07-01 19:47:50 +00003197 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003198 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003199
cristy3a37efd2011-08-28 20:31:03 +00003200 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003201 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003202 break;
cristy4c08aed2011-07-01 19:47:50 +00003203 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003204 RedQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003205 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3206 break;
3207 }
3208 if (image->previous == (Image *) NULL)
3209 {
3210 status=SetImageProgress(image,SaveImageTag,100,400);
3211 if (status == MagickFalse)
3212 break;
3213 }
cristybb503372010-05-27 20:51:26 +00003214 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003215 {
cristy4c08aed2011-07-01 19:47:50 +00003216 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003217 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003218
cristy3a37efd2011-08-28 20:31:03 +00003219 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003220 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003221 break;
cristy4c08aed2011-07-01 19:47:50 +00003222 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003223 GreenQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003224 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3225 break;
3226 }
3227 if (image->previous == (Image *) NULL)
3228 {
3229 status=SetImageProgress(image,SaveImageTag,200,400);
3230 if (status == MagickFalse)
3231 break;
3232 }
cristybb503372010-05-27 20:51:26 +00003233 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003234 {
cristy4c08aed2011-07-01 19:47:50 +00003235 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003236 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003237
cristy3a37efd2011-08-28 20:31:03 +00003238 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003239 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003240 break;
cristy4c08aed2011-07-01 19:47:50 +00003241 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003242 BlueQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003243 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3244 break;
3245 }
3246 if (image->previous == (Image *) NULL)
3247 {
3248 status=SetImageProgress(image,SaveImageTag,300,400);
3249 if (status == MagickFalse)
3250 break;
3251 }
3252 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003253 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003254 {
cristy4c08aed2011-07-01 19:47:50 +00003255 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003256 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003257
cristy3a37efd2011-08-28 20:31:03 +00003258 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003259 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003260 break;
cristy4c08aed2011-07-01 19:47:50 +00003261 length=ExportQuantumPixels(image,(CacheView *) NULL,
cristy3a37efd2011-08-28 20:31:03 +00003262 quantum_info,AlphaQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003263 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3264 break;
3265 }
3266 if (image->previous == (Image *) NULL)
3267 {
3268 status=SetImageProgress(image,SaveImageTag,400,400);
3269 if (status == MagickFalse)
3270 break;
3271 }
3272 break;
3273 }
3274 }
3275 break;
3276 }
3277 case PHOTOMETRIC_SEPARATED:
3278 {
3279 /*
3280 CMYK TIFF image.
3281 */
3282 quantum_type=CMYKQuantum;
3283 if (image->matte != MagickFalse)
3284 quantum_type=CMYKAQuantum;
3285 if (image->colorspace != CMYKColorspace)
cristye941a752011-10-15 01:52:48 +00003286 (void) TransformImageColorspace(image,CMYKColorspace,exception);
cristybb503372010-05-27 20:51:26 +00003287 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003288 {
cristy4c08aed2011-07-01 19:47:50 +00003289 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003290 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003291
cristy3a37efd2011-08-28 20:31:03 +00003292 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003293 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003294 break;
cristy4c08aed2011-07-01 19:47:50 +00003295 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003296 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003297 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3298 break;
3299 if (image->previous == (Image *) NULL)
3300 {
cristycee97112010-05-28 00:44:52 +00003301 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3302 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003303 if (status == MagickFalse)
3304 break;
3305 }
3306 }
3307 break;
3308 }
3309 case PHOTOMETRIC_PALETTE:
3310 {
3311 uint16
3312 *blue,
3313 *green,
3314 *red;
3315
3316 /*
3317 Colormapped TIFF image.
3318 */
3319 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3320 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3321 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3322 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3323 (blue == (uint16 *) NULL))
3324 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3325 /*
3326 Initialize TIFF colormap.
3327 */
3328 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3329 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3330 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003331 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003332 {
3333 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3334 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3335 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3336 }
3337 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3338 red=(uint16 *) RelinquishMagickMemory(red);
3339 green=(uint16 *) RelinquishMagickMemory(green);
3340 blue=(uint16 *) RelinquishMagickMemory(blue);
3341 }
3342 default:
3343 {
3344 /*
3345 Convert PseudoClass packets to contiguous grayscale scanlines.
3346 */
3347 quantum_type=IndexQuantum;
3348 if (image->matte != MagickFalse)
3349 {
3350 if (photometric != PHOTOMETRIC_PALETTE)
3351 quantum_type=GrayAlphaQuantum;
3352 else
3353 quantum_type=IndexAlphaQuantum;
3354 }
3355 else
3356 if (photometric != PHOTOMETRIC_PALETTE)
3357 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003358 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003359 {
cristy4c08aed2011-07-01 19:47:50 +00003360 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003361 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003362
cristy3a37efd2011-08-28 20:31:03 +00003363 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003364 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003365 break;
cristy4c08aed2011-07-01 19:47:50 +00003366 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003367 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003368 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3369 break;
3370 if (image->previous == (Image *) NULL)
3371 {
cristycee97112010-05-28 00:44:52 +00003372 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3373 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003374 if (status == MagickFalse)
3375 break;
3376 }
3377 }
3378 break;
3379 }
3380 }
3381 quantum_info=DestroyQuantumInfo(quantum_info);
3382 DestroyTIFFInfo(&tiff_info);
3383 if (0 && (image_info->verbose == MagickTrue))
3384 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3385 (void) TIFFWriteDirectory(tiff);
cristy3ed852e2009-09-05 21:47:34 +00003386 image=SyncNextImageInList(image);
3387 if (image == (Image *) NULL)
3388 break;
3389 status=SetImageProgress(image,SaveImagesTag,scene++,
3390 GetImageListLength(image));
3391 if (status == MagickFalse)
3392 break;
3393 } while (image_info->adjoin != MagickFalse);
3394 (void) TIFFSetWarningHandler(warning_handler);
3395 (void) TIFFSetErrorHandler(error_handler);
3396 TIFFClose(tiff);
3397 return(MagickTrue);
3398}
3399#endif