blob: bd5c25cb0b937b2b9e154a25e01330bbe53805ae [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% TTTTT IIIII FFFFF FFFFF %
7% T I F F %
8% T I FFF FFF %
9% T I F F %
10% T IIIII F F %
11% %
12% %
13% Read/Write TIFF Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/attribute.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colormap.h"
50#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/constitute.h"
53#include "MagickCore/enhance.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image.h"
58#include "MagickCore/image-private.h"
59#include "MagickCore/list.h"
60#include "MagickCore/log.h"
61#include "MagickCore/magick.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/module.h"
64#include "MagickCore/monitor.h"
65#include "MagickCore/monitor-private.h"
66#include "MagickCore/option.h"
67#include "MagickCore/pixel-accessor.h"
68#include "MagickCore/property.h"
69#include "MagickCore/quantum.h"
70#include "MagickCore/quantum-private.h"
71#include "MagickCore/profile.h"
72#include "MagickCore/resize.h"
73#include "MagickCore/resource_.h"
74#include "MagickCore/semaphore.h"
75#include "MagickCore/splay-tree.h"
76#include "MagickCore/static.h"
77#include "MagickCore/statistic.h"
78#include "MagickCore/string_.h"
79#include "MagickCore/string-private.h"
80#include "MagickCore/thread_.h"
81#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000082#if defined(MAGICKCORE_TIFF_DELEGATE)
83# if defined(MAGICKCORE_HAVE_TIFFCONF_H)
84# include "tiffconf.h"
cristyef8f26b2010-12-19 20:29:16 +000085# endif
cristy3ed852e2009-09-05 21:47:34 +000086# include "tiff.h"
87# include "tiffio.h"
88# if !defined(COMPRESSION_ADOBE_DEFLATE)
89# define COMPRESSION_ADOBE_DEFLATE 8
90# endif
cristyef8f26b2010-12-19 20:29:16 +000091# if !defined(PREDICTOR_HORIZONTAL)
92# define PREDICTOR_HORIZONTAL 2
93# endif
cristy98ca0f52011-10-08 23:19:10 +000094# if !defined(TIFFTAG_COPYRIGHT)
95# define TIFFTAG_COPYRIGHT 33432
96# endif
97# if !defined(TIFFTAG_OPIIMAGEID)
98# define TIFF_OPIIMAGEID 32781
99# endif
cristy3ed852e2009-09-05 21:47:34 +0000100
101/*
102 Typedef declarations.
103*/
104#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
105typedef struct _ExifInfo
106{
107 unsigned int
108 tag,
109 type;
110
111 const char
112 *property;
113} ExifInfo;
114
115static const ExifInfo
116 exif_info[] = {
117 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
118 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
119 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
120 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000121 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy7992e402011-09-16 15:24:22 +0000122 { EXIFTAG_OECF, TIFF_NOTYPE, "exif:OptoelectricConversionFactor" },
123 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, "exif:ExifVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000124 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
125 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000126 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, "exif:ComponentsConfiguration" },
cristy3ed852e2009-09-05 21:47:34 +0000127 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
128 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
129 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
130 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
131 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
132 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
133 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
134 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
135 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
136 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
137 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
cristy7992e402011-09-16 15:24:22 +0000138 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, "exif:SubjectArea" },
139 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, "exif:MakerNote" },
140 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, "exif:UserComment" },
cristy3ed852e2009-09-05 21:47:34 +0000141 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
142 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
143 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000144 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, "exif:FlashpixVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000145 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
146 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
147 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
148 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
149 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
150 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
cristy7992e402011-09-16 15:24:22 +0000151 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, "exif:SpatialFrequencyResponse" },
cristy3ed852e2009-09-05 21:47:34 +0000152 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
153 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
154 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
155 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
156 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
157 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
cristy7992e402011-09-16 15:24:22 +0000158 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, "exif:FileSource" },
159 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, "exif:SceneType" },
160 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, "exif:CFAPattern" },
cristy3ed852e2009-09-05 21:47:34 +0000161 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
162 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
163 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
164 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
165 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
166 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
167 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
168 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
169 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
170 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
cristy7992e402011-09-16 15:24:22 +0000171 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, "exif:DeviceSettingDescription" },
cristy3ed852e2009-09-05 21:47:34 +0000172 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
173 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
174 { 0, 0, (char *) NULL }
175};
176#endif
cristy0805e062011-01-30 02:56:36 +0000177#endif /* MAGICKCORE_TIFF_DELEGATE */
cristy3ed852e2009-09-05 21:47:34 +0000178
179/*
180 Global declarations.
181*/
182static MagickThreadKey
183 tiff_exception;
184
185static SemaphoreInfo
186 *tiff_semaphore = (SemaphoreInfo *) NULL;
187
188static volatile MagickBooleanType
189 instantiate_key = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000190
191/*
192 Forward declarations.
193*/
194#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000195static Image *
196 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
197
cristy3ed852e2009-09-05 21:47:34 +0000198static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +0000199 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
200 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
201 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000202#endif
203
204/*
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206% %
207% %
208% %
209% I s T I F F %
210% %
211% %
212% %
213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214%
215% IsTIFF() returns MagickTrue if the image format type, identified by the
216% magick string, is TIFF.
217%
218% The format of the IsTIFF method is:
219%
220% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
221%
222% A description of each parameter follows:
223%
224% o magick: compare image format pattern against these bytes.
225%
226% o length: Specifies the length of the magick string.
227%
228*/
229static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
230{
231 if (length < 4)
232 return(MagickFalse);
233 if (memcmp(magick,"\115\115\000\052",4) == 0)
234 return(MagickTrue);
235 if (memcmp(magick,"\111\111\052\000",4) == 0)
236 return(MagickTrue);
237#if defined(TIFF_VERSION_BIG)
238 if (length < 8)
239 return(MagickFalse);
240 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
241 return(MagickTrue);
242 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
243 return(MagickTrue);
244#endif
245 return(MagickFalse);
246}
247
248#if defined(MAGICKCORE_TIFF_DELEGATE)
249/*
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251% %
252% %
253% %
cristy3d7f8062009-09-24 20:45:35 +0000254% R e a d G R O U P 4 I m a g e %
255% %
256% %
257% %
258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259%
260% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
261% allocates the memory necessary for the new Image structure and returns a
262% pointer to the new image.
263%
264% The format of the ReadGROUP4Image method is:
265%
266% Image *ReadGROUP4Image(const ImageInfo *image_info,
267% ExceptionInfo *exception)
268%
269% A description of each parameter follows:
270%
271% o image_info: the image info.
272%
273% o exception: return any errors or warnings in this structure.
274%
275*/
276
cristyeaedf062010-05-29 22:36:02 +0000277static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000278{
279 unsigned char
280 buffer[4];
281
282 buffer[0]=(unsigned char) value;
283 buffer[1]=(unsigned char) (value >> 8);
284 buffer[2]=(unsigned char) (value >> 16);
285 buffer[3]=(unsigned char) (value >> 24);
286 return(fwrite(buffer,1,4,file));
287}
288
289static Image *ReadGROUP4Image(const ImageInfo *image_info,
290 ExceptionInfo *exception)
291{
292 char
293 filename[MaxTextExtent];
294
295 FILE
296 *file;
297
298 Image
299 *image;
300
301 ImageInfo
302 *read_info;
303
304 int
305 c,
306 unique_file;
307
308 MagickBooleanType
309 status;
310
311 size_t
312 length;
313
314 ssize_t
315 offset,
316 strip_offset;
317
318 /*
319 Open image file.
320 */
321 assert(image_info != (const ImageInfo *) NULL);
322 assert(image_info->signature == MagickSignature);
323 if (image_info->debug != MagickFalse)
324 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
325 image_info->filename);
326 assert(exception != (ExceptionInfo *) NULL);
327 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000328 image=AcquireImage(image_info,exception);
cristy3d7f8062009-09-24 20:45:35 +0000329 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
330 if (status == MagickFalse)
331 {
332 image=DestroyImageList(image);
333 return((Image *) NULL);
334 }
335 /*
336 Write raw CCITT Group 4 wrapped as a TIFF image file.
337 */
338 file=(FILE *) NULL;
339 unique_file=AcquireUniqueFileResource(filename);
340 if (unique_file != -1)
341 file=fdopen(unique_file,"wb");
342 if ((unique_file == -1) || (file == (FILE *) NULL))
343 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
344 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
345 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
346 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
347 length=WriteLSBLong(file,image->columns);
348 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
349 length=WriteLSBLong(file,image->rows);
350 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
351 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
352 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
353 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
354 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000355 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000356 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000357 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000358 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
359 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000360 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000361 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
362 offset=(ssize_t) ftell(file)-4;
363 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000364 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000365 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000366 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000367 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
368 length=fwrite("\000\000\000\000",1,4,file);
cristy2a11bef2011-10-28 18:33:11 +0000369 length=WriteLSBLong(file,(long) image->resolution.x);
cristy3d7f8062009-09-24 20:45:35 +0000370 length=WriteLSBLong(file,1);
371 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
372 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000373 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000374 length=WriteLSBLong(file,(unsigned int) length);
375 (void) fclose(file);
376 (void) CloseBlob(image);
377 image=DestroyImage(image);
378 /*
379 Read TIFF image.
380 */
cristy14efd992009-09-26 23:46:03 +0000381 read_info=CloneImageInfo((ImageInfo *) NULL);
cristyb51dff52011-05-19 16:55:47 +0000382 (void) FormatLocaleString(read_info->filename,MaxTextExtent,"%s",filename);
cristy3d7f8062009-09-24 20:45:35 +0000383 image=ReadTIFFImage(read_info,exception);
384 read_info=DestroyImageInfo(read_info);
385 if (image != (Image *) NULL)
386 {
387 (void) CopyMagickString(image->filename,image_info->filename,
388 MaxTextExtent);
389 (void) CopyMagickString(image->magick_filename,image_info->filename,
390 MaxTextExtent);
391 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
392 }
393 (void) RelinquishUniqueFileResource(filename);
394 return(image);
395}
396#endif
397
398#if defined(MAGICKCORE_TIFF_DELEGATE)
399/*
400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401% %
402% %
403% %
cristy3ed852e2009-09-05 21:47:34 +0000404% R e a d T I F F I m a g e %
405% %
406% %
407% %
408%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409%
410% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
411% memory necessary for the new Image structure and returns a pointer to the
412% new image.
413%
414% The format of the ReadTIFFImage method is:
415%
416% Image *ReadTIFFImage(const ImageInfo *image_info,
417% ExceptionInfo *exception)
418%
419% A description of each parameter follows:
420%
421% o image_info: the image info.
422%
423% o exception: return any errors or warnings in this structure.
424%
425*/
426
427static inline size_t MagickMax(const size_t x,const size_t y)
428{
429 if (x > y)
430 return(x);
431 return(y);
432}
433
cristybb503372010-05-27 20:51:26 +0000434static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000435{
436 if (x < y)
437 return(x);
438 return(y);
439}
440
441static MagickBooleanType ReadProfile(Image *image,const char *name,
cristy018f07f2011-09-04 21:15:19 +0000442 unsigned char *datum,ssize_t length,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000443{
444 MagickBooleanType
445 status;
446
cristybb503372010-05-27 20:51:26 +0000447 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000448 i;
449
450 StringInfo
451 *profile;
452
453 if (length < 4)
454 return(MagickFalse);
455 i=0;
456 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
457 {
458 for (i=0; i < (length-4); i+=2)
459 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
460 break;
461 if (i == length)
462 length-=i;
463 else
464 i=0;
465 if (length < 4)
466 return(MagickFalse);
467 }
cristye8f8f382011-09-01 13:32:37 +0000468 profile=BlobToStringInfo(datum+i,(size_t) length);
469 if (profile == (StringInfo *) NULL)
470 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
471 image->filename);
cristyd15e6592011-10-15 00:13:06 +0000472 status=SetImageProfile(image,name,profile,exception);
cristy3ed852e2009-09-05 21:47:34 +0000473 profile=DestroyStringInfo(profile);
474 if (status == MagickFalse)
475 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
476 image->filename);
477 return(MagickTrue);
478}
479
480#if defined(__cplusplus) || defined(c_plusplus)
481extern "C" {
482#endif
483
484static int TIFFCloseBlob(thandle_t image)
485{
486 (void) CloseBlob((Image *) image);
487 return(0);
488}
489
490static void TIFFErrors(const char *module,const char *format,va_list error)
491{
492 char
493 message[MaxTextExtent];
494
495 ExceptionInfo
496 *exception;
497
498#if defined(MAGICKCORE_HAVE_VSNPRINTF)
499 (void) vsnprintf(message,MaxTextExtent,format,error);
500#else
501 (void) vsprintf(message,format,error);
502#endif
503 (void) ConcatenateMagickString(message,".",MaxTextExtent);
504 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
505 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000506 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
507 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000508}
509
cristy018f07f2011-09-04 21:15:19 +0000510static void TIFFGetProfiles(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000511{
512 uint32
513 length;
514
515 unsigned char
516 *profile;
517
518#if defined(TIFFTAG_ICCPROFILE)
519 length=0;
520 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000521 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000522#endif
523#if defined(TIFFTAG_PHOTOSHOP)
524 length=0;
525 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000526 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000527#endif
528#if defined(TIFFTAG_RICHTIFFIPTC)
529 length=0;
530 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
531 {
532 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000533 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy018f07f2011-09-04 21:15:19 +0000534 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000535 }
536#endif
537#if defined(TIFFTAG_XMLPACKET)
538 length=0;
539 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000540 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000541#endif
542 length=0;
543 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000544 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000545}
546
cristyd15e6592011-10-15 00:13:06 +0000547static void TIFFGetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000548{
549 char
cristy98ca0f52011-10-08 23:19:10 +0000550 message[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000551 *text;
552
cristy98ca0f52011-10-08 23:19:10 +0000553 uint32
554 count;
555
cristy3ed852e2009-09-05 21:47:34 +0000556 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000557 (void) SetImageProperty(image,"tiff:artist",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000558 if (TIFFGetField(tiff,TIFFTAG_COPYRIGHT,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000559 (void) SetImageProperty(image,"tiff:copyright",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000560 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000561 (void) SetImageProperty(image,"tiff:timestamp",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000562 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000563 (void) SetImageProperty(image,"tiff:document",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000564 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000565 (void) SetImageProperty(image,"tiff:hostcomputer",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000566 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000567 (void) SetImageProperty(image,"comment",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000568 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000569 (void) SetImageProperty(image,"tiff:make",text,exception);
cristy3ed852e2009-09-05 21:47:34 +0000570 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000571 (void) SetImageProperty(image,"tiff:model",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000572 if (TIFFGetField(tiff,TIFFTAG_OPIIMAGEID,&count,&text) == 1)
573 {
574 if (count >= MaxTextExtent)
575 count=MaxTextExtent-1;
576 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000577 (void) SetImageProperty(image,"tiff:image-id",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000578 }
cristy3ed852e2009-09-05 21:47:34 +0000579 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000580 (void) SetImageProperty(image,"label",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000581 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
cristyd15e6592011-10-15 00:13:06 +0000582 (void) SetImageProperty(image,"tiff:software",text,exception);
cristy98ca0f52011-10-08 23:19:10 +0000583 if (TIFFGetField(tiff,33423,&count,&text) == 1)
584 {
585 if (count >= MaxTextExtent)
586 count=MaxTextExtent-1;
587 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000588 (void) SetImageProperty(image,"tiff:kodak-33423",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000589 }
590 if (TIFFGetField(tiff,36867,&count,&text) == 1)
591 {
592 if (count >= MaxTextExtent)
593 count=MaxTextExtent-1;
594 (void) CopyMagickString(message,text,count+1);
cristyd15e6592011-10-15 00:13:06 +0000595 (void) SetImageProperty(image,"tiff:kodak-36867",message,exception);
cristy98ca0f52011-10-08 23:19:10 +0000596 }
cristy3ed852e2009-09-05 21:47:34 +0000597}
598
cristyd15e6592011-10-15 00:13:06 +0000599static void TIFFGetEXIFProperties(TIFF *tiff,Image *image,
600 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000601{
602#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
603 char
604 value[MaxTextExtent];
605
cristybb503372010-05-27 20:51:26 +0000606 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000607 i;
608
609 tdir_t
610 directory;
611
cristyccf4e122011-10-29 00:29:19 +0000612#if defined(TIFF_VERSION_BIG)
613 uint64
614#else
cristy3ed852e2009-09-05 21:47:34 +0000615 uint32
cristyccf4e122011-10-29 00:29:19 +0000616#endif
cristy3ed852e2009-09-05 21:47:34 +0000617 offset;
618
cristy7992e402011-09-16 15:24:22 +0000619 void
620 *sans;
621
cristy3ed852e2009-09-05 21:47:34 +0000622 /*
623 Read EXIF properties.
624 */
625 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
626 return;
627 directory=TIFFCurrentDirectory(tiff);
628 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
629 return;
cristy7992e402011-09-16 15:24:22 +0000630 sans=NULL;
cristy3ed852e2009-09-05 21:47:34 +0000631 for (i=0; exif_info[i].tag != 0; i++)
632 {
633 *value='\0';
634 switch (exif_info[i].type)
635 {
636 case TIFF_ASCII:
637 {
638 char
639 *ascii;
640
cristy7992e402011-09-16 15:24:22 +0000641 ascii=(char *) NULL;
642 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans) != 0) &&
643 (ascii != (char *) NULL) && (*ascii != '\0'))
cristy3ed852e2009-09-05 21:47:34 +0000644 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
645 break;
646 }
647 case TIFF_SHORT:
648 {
649 uint16
cristy7992e402011-09-16 15:24:22 +0000650 shorty[2] = { 0, 0};
cristy3ed852e2009-09-05 21:47:34 +0000651
cristy7992e402011-09-16 15:24:22 +0000652 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans) != 0)
653 (void) FormatLocaleString(value,MaxTextExtent,"%d",(int) shorty[0]);
cristy3ed852e2009-09-05 21:47:34 +0000654 break;
655 }
656 case TIFF_LONG:
657 {
658 uint32
cristy7992e402011-09-16 15:24:22 +0000659 longy;
cristy3ed852e2009-09-05 21:47:34 +0000660
cristy7992e402011-09-16 15:24:22 +0000661 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans) != 0)
662 (void) FormatLocaleString(value,MaxTextExtent,"%d",longy);
cristy3ed852e2009-09-05 21:47:34 +0000663 break;
664 }
cristyccf4e122011-10-29 00:29:19 +0000665#if defined(TIFF_VERSION_BIG)
666 case TIFF_LONG8:
667 {
668 uint64
669 longy;
670
671 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans) != 0)
672 (void) FormatLocaleString(value,MaxTextExtent,"%ld",longy);
673 break;
674 }
675#endif
cristy3ed852e2009-09-05 21:47:34 +0000676 case TIFF_RATIONAL:
677 case TIFF_SRATIONAL:
cristy7992e402011-09-16 15:24:22 +0000678 case TIFF_FLOAT:
679 case TIFF_DOUBLE:
cristy3ed852e2009-09-05 21:47:34 +0000680 {
681 float
cristy7992e402011-09-16 15:24:22 +0000682 rational[16];
cristy3ed852e2009-09-05 21:47:34 +0000683
cristy7992e402011-09-16 15:24:22 +0000684 if (TIFFGetField(tiff,exif_info[i].tag,&rational,&sans) != 0)
685 (void) FormatLocaleString(value,MaxTextExtent,"%g",rational[0]);
cristy3ed852e2009-09-05 21:47:34 +0000686 break;
687 }
688 default:
689 break;
690 }
691 if (*value != '\0')
cristyd15e6592011-10-15 00:13:06 +0000692 (void) SetImageProperty(image,exif_info[i].property,value,exception);
cristy3ed852e2009-09-05 21:47:34 +0000693 }
694 TIFFSetDirectory(tiff,directory);
695#else
696 (void) tiff;
697 (void) image;
698#endif
699}
700
701static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
702{
703 *base=(tdata_t *) GetBlobStreamData((Image *) image);
704 if (*base != (tdata_t *) NULL)
705 *size=(toff_t) GetBlobSize((Image *) image);
706 if (*base != (tdata_t *) NULL)
707 return(1);
708 return(0);
709}
710
711static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
712{
713 tsize_t
714 count;
715
716 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
717 (unsigned char *) data);
718 return(count);
719}
720
cristybb503372010-05-27 20:51:26 +0000721static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
722 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000723{
724 int32
725 status;
726
727 (void) bits_per_sample;
728 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
729 return(status);
730}
731
732static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
733{
734 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
735}
736
737static toff_t TIFFGetBlobSize(thandle_t image)
738{
739 return((toff_t) GetBlobSize((Image *) image));
740}
741
742static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
743{
744 (void) image;
745 (void) base;
746 (void) size;
747}
748
749static void TIFFWarnings(const char *module,const char *format,va_list warning)
750{
751 char
752 message[MaxTextExtent];
753
754 ExceptionInfo
755 *exception;
756
757#if defined(MAGICKCORE_HAVE_VSNPRINTF)
758 (void) vsnprintf(message,MaxTextExtent,format,warning);
759#else
760 (void) vsprintf(message,format,warning);
761#endif
762 (void) ConcatenateMagickString(message,".",MaxTextExtent);
763 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
764 if (exception != (ExceptionInfo *) NULL)
765 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
766 message,"`%s'",module);
767}
768
769static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
770{
771 tsize_t
772 count;
773
774 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
775 (unsigned char *) data);
776 return(count);
777}
778
779#if defined(__cplusplus) || defined(c_plusplus)
780}
781#endif
782
783static Image *ReadTIFFImage(const ImageInfo *image_info,
784 ExceptionInfo *exception)
785{
786 typedef enum
787 {
788 ReadSingleSampleMethod,
789 ReadRGBAMethod,
790 ReadCMYKAMethod,
791 ReadStripMethod,
792 ReadTileMethod,
793 ReadGenericMethod
794 } TIFFMethodType;
795
796 const char
797 *option;
798
799 float
800 *chromaticity,
801 x_position,
802 y_position,
803 x_resolution,
804 y_resolution;
805
806 Image
807 *image;
808
cristy3ed852e2009-09-05 21:47:34 +0000809 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000810 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000811 debug,
812 status;
813
814 MagickSizeType
815 number_pixels;
816
817 QuantumInfo
818 *quantum_info;
819
820 QuantumType
821 quantum_type;
822
cristybb503372010-05-27 20:51:26 +0000823 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000824 i;
825
826 size_t
827 length,
cristyc6da28e2011-04-28 01:41:35 +0000828 lsb_first,
cristy3ed852e2009-09-05 21:47:34 +0000829 pad;
830
cristyc6da28e2011-04-28 01:41:35 +0000831 ssize_t
832 y;
833
cristy3ed852e2009-09-05 21:47:34 +0000834 TIFF
835 *tiff;
836
837 TIFFErrorHandler
838 error_handler,
839 warning_handler;
840
841 TIFFMethodType
842 method;
843
844 uint16
845 compress_tag,
846 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000847 endian,
cristy3ed852e2009-09-05 21:47:34 +0000848 extra_samples,
849 interlace,
850 max_sample_value,
851 min_sample_value,
852 orientation,
853 pages,
854 photometric,
855 *sample_info,
856 sample_format,
857 samples_per_pixel,
858 units,
859 value;
860
861 uint32
862 height,
863 rows_per_strip,
864 width;
865
866 unsigned char
867 *pixels;
868
cristy3ed852e2009-09-05 21:47:34 +0000869 /*
870 Open image.
871 */
872 assert(image_info != (const ImageInfo *) NULL);
873 assert(image_info->signature == MagickSignature);
874 if (image_info->debug != MagickFalse)
875 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
876 image_info->filename);
877 assert(exception != (ExceptionInfo *) NULL);
878 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000879 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000880 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
881 if (status == MagickFalse)
882 {
883 image=DestroyImageList(image);
884 return((Image *) NULL);
885 }
886 (void) MagickSetThreadValue(tiff_exception,exception);
887 error_handler=TIFFSetErrorHandler(TIFFErrors);
888 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
cristy1630f7f2011-02-18 01:10:02 +0000889 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
cristy3ed852e2009-09-05 21:47:34 +0000890 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
891 TIFFUnmapBlob);
892 if (tiff == (TIFF *) NULL)
893 {
894 (void) TIFFSetWarningHandler(warning_handler);
895 (void) TIFFSetErrorHandler(error_handler);
896 image=DestroyImageList(image);
897 return((Image *) NULL);
898 }
899 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +0000900 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +0000901 if (image_info->number_scenes != 0)
902 {
903 /*
904 Generate blank images for subimage specification (e.g. image.tif[4].
905 */
cristybb503372010-05-27 20:51:26 +0000906 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000907 {
908 (void) TIFFReadDirectory(tiff);
cristy9950d572011-10-01 18:22:35 +0000909 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000910 if (GetNextImageInList(image) == (Image *) NULL)
911 {
912 image=DestroyImageList(image);
913 return((Image *) NULL);
914 }
915 image=SyncNextImageInList(image);
916 }
917 }
918 do
919 {
920 if (0 && (image_info->verbose != MagickFalse))
921 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristyd15e6592011-10-15 00:13:06 +0000922 TIFFGetEXIFProperties(tiff,image,exception);
cristy5b480ae2011-10-10 13:05:30 +0000923 TIFFGetProfiles(tiff,image,exception);
cristyd15e6592011-10-15 00:13:06 +0000924 TIFFGetProperties(tiff,image,exception);
cristy6b032822010-06-29 16:52:32 +0000925 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
cristyd15e6592011-10-15 00:13:06 +0000926 "lsb" : "msb",exception);
cristy3ed852e2009-09-05 21:47:34 +0000927 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
928 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
929 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
930 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000931 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000932 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
933 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
934 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
cristyae1319c2011-01-19 20:18:53 +0000935 if (sample_format == SAMPLEFORMAT_IEEEFP)
cristyd15e6592011-10-15 00:13:06 +0000936 (void) SetImageProperty(image,"quantum:format","floating-point",
937 exception);
cristy3ed852e2009-09-05 21:47:34 +0000938 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
939 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
940 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
941 switch (photometric)
942 {
943 case PHOTOMETRIC_MINISBLACK:
944 {
cristyd15e6592011-10-15 00:13:06 +0000945 (void) SetImageProperty(image,"tiff:photometric","min-is-black",
946 exception);
cristy3ed852e2009-09-05 21:47:34 +0000947 break;
948 }
949 case PHOTOMETRIC_MINISWHITE:
950 {
cristyd15e6592011-10-15 00:13:06 +0000951 (void) SetImageProperty(image,"tiff:photometric","min-is-white",
952 exception);
cristy3ed852e2009-09-05 21:47:34 +0000953 break;
954 }
955 case PHOTOMETRIC_PALETTE:
956 {
cristyd15e6592011-10-15 00:13:06 +0000957 (void) SetImageProperty(image,"tiff:photometric","palette",exception);
cristy3ed852e2009-09-05 21:47:34 +0000958 break;
959 }
960 case PHOTOMETRIC_RGB:
961 {
cristyd15e6592011-10-15 00:13:06 +0000962 (void) SetImageProperty(image,"tiff:photometric","RGB",exception);
cristy3ed852e2009-09-05 21:47:34 +0000963 break;
964 }
965 case PHOTOMETRIC_CIELAB:
966 {
cristyd15e6592011-10-15 00:13:06 +0000967 (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception);
cristy3ed852e2009-09-05 21:47:34 +0000968 break;
969 }
970 case PHOTOMETRIC_SEPARATED:
971 {
cristyd15e6592011-10-15 00:13:06 +0000972 (void) SetImageProperty(image,"tiff:photometric","separated",exception);
cristy3ed852e2009-09-05 21:47:34 +0000973 break;
974 }
cristy79e5dad2010-09-16 19:48:33 +0000975 case PHOTOMETRIC_YCBCR:
976 {
cristyd15e6592011-10-15 00:13:06 +0000977 (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception);
cristy79e5dad2010-09-16 19:48:33 +0000978 break;
979 }
cristy3ed852e2009-09-05 21:47:34 +0000980 default:
981 {
cristyd15e6592011-10-15 00:13:06 +0000982 (void) SetImageProperty(image,"tiff:photometric","unknown",exception);
cristy3ed852e2009-09-05 21:47:34 +0000983 break;
984 }
985 }
986 if (image->debug != MagickFalse)
987 {
988 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
989 (unsigned int) width,(unsigned int) height);
990 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
991 interlace);
992 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
993 "Bits per sample: %u",bits_per_sample);
994 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
995 "Min sample value: %u",min_sample_value);
996 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
997 "Max sample value: %u",max_sample_value);
998 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
cristyd15e6592011-10-15 00:13:06 +0000999 "interpretation: %s",GetImageProperty(image,"tiff:photometric",
1000 exception));
cristy3ed852e2009-09-05 21:47:34 +00001001 }
cristybb503372010-05-27 20:51:26 +00001002 image->columns=(size_t) width;
1003 image->rows=(size_t) height;
1004 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +00001005 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +00001006 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
1007 (double) image->depth);
cristyf2687ca2010-06-29 16:32:38 +00001008 lsb_first=1;
1009 image->endian=MSBEndian;
1010 if ((int) (*(char *) &lsb_first) != 0)
1011 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001012 if (photometric == PHOTOMETRIC_SEPARATED)
1013 image->colorspace=CMYKColorspace;
1014 if (photometric == PHOTOMETRIC_CIELAB)
1015 image->colorspace=LabColorspace;
1016 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
1017 &samples_per_pixel);
1018 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
cristy2a11bef2011-10-28 18:33:11 +00001019 x_resolution=(float) image->resolution.x;
1020 y_resolution=(float) image->resolution.y;
cristy3ed852e2009-09-05 21:47:34 +00001021 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
1022 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
cristy2a11bef2011-10-28 18:33:11 +00001023 image->resolution.x=x_resolution;
1024 image->resolution.y=y_resolution;
cristy3ed852e2009-09-05 21:47:34 +00001025 x_position=(float) image->page.x/x_resolution;
1026 y_position=(float) image->page.y/y_resolution;
1027 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
1028 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +00001029 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
1030 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001031 image->orientation=(OrientationType) orientation;
1032 chromaticity=(float *) NULL;
1033 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
1034 if (chromaticity != (float *) NULL)
1035 {
1036 image->chromaticity.white_point.x=chromaticity[0];
1037 image->chromaticity.white_point.y=chromaticity[1];
1038 }
1039 chromaticity=(float *) NULL;
1040 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
1041 if (chromaticity != (float *) NULL)
1042 {
1043 image->chromaticity.red_primary.x=chromaticity[0];
1044 image->chromaticity.red_primary.y=chromaticity[1];
1045 image->chromaticity.green_primary.x=chromaticity[2];
1046 image->chromaticity.green_primary.y=chromaticity[3];
1047 image->chromaticity.blue_primary.x=chromaticity[4];
1048 image->chromaticity.blue_primary.y=chromaticity[5];
1049 }
cristy3ed852e2009-09-05 21:47:34 +00001050 /*
1051 Allocate memory for the image and pixel buffer.
1052 */
1053#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1054 if ((compress_tag != COMPRESSION_NONE) &&
1055 (TIFFIsCODECConfigured(compress_tag) == 0))
1056 {
1057 TIFFClose(tiff);
1058 ThrowReaderException(CoderError,"CompressNotSupported");
1059 }
1060#endif
1061 switch (compress_tag)
1062 {
1063 case COMPRESSION_NONE: image->compression=NoCompression; break;
1064 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1065 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1066 case COMPRESSION_JPEG:
1067 {
1068 image->compression=JPEGCompression;
1069#if defined(JPEG_SUPPORT)
1070 {
1071 char
1072 sampling_factor[MaxTextExtent];
1073
1074 uint16
1075 horizontal,
1076 vertical;
1077
1078 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1079 &horizontal,&vertical);
cristyb51dff52011-05-19 16:55:47 +00001080 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
cristy3ed852e2009-09-05 21:47:34 +00001081 horizontal,vertical);
1082 (void) SetImageProperty(image,"jpeg:sampling-factor",
cristyd15e6592011-10-15 00:13:06 +00001083 sampling_factor,exception);
cristy3ed852e2009-09-05 21:47:34 +00001084 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1085 "Sampling Factors: %s",sampling_factor);
1086 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1087 {
1088 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1089 JPEGCOLORMODE_RGB);
1090 photometric=PHOTOMETRIC_RGB;
1091 }
1092 }
1093#endif
1094 break;
1095 }
1096 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001097#if defined(COMPRESSION_LZMA)
1098 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1099#endif
cristy3ed852e2009-09-05 21:47:34 +00001100 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1101 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1102 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1103 default: image->compression=RLECompression; break;
1104 }
1105 quantum_info=AcquireQuantumInfo(image_info,image);
1106 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001107 {
1108 TIFFClose(tiff);
1109 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1110 }
cristy3ed852e2009-09-05 21:47:34 +00001111 if (sample_format == SAMPLEFORMAT_UINT)
1112 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1113 if (sample_format == SAMPLEFORMAT_INT)
1114 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1115 if (sample_format == SAMPLEFORMAT_IEEEFP)
1116 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1117 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001118 {
cristya9a6ec42011-10-26 16:55:01 +00001119 quantum_info=DestroyQuantumInfo(quantum_info);
cristy8d137bf2010-04-21 23:52:04 +00001120 TIFFClose(tiff);
1121 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1122 }
cristy3ed852e2009-09-05 21:47:34 +00001123 status=MagickTrue;
1124 switch (photometric)
1125 {
1126 case PHOTOMETRIC_MINISBLACK:
1127 {
1128 quantum_info->min_is_white=MagickFalse;
1129 break;
1130 }
1131 case PHOTOMETRIC_MINISWHITE:
1132 {
1133 quantum_info->min_is_white=MagickTrue;
1134 break;
1135 }
1136 default:
1137 break;
1138 }
cristy891dc792010-03-04 01:47:16 +00001139 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001140 extra_samples=0;
1141 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1142 &sample_info);
1143 if (extra_samples == 0)
1144 {
1145 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1146 image->matte=MagickTrue;
1147 }
1148 else
cristy6c207342010-02-19 17:49:50 +00001149 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001150 {
cristy7cf69862010-03-03 13:57:24 +00001151 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001152 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001153 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001154 }
cristy891dc792010-03-04 01:47:16 +00001155 option=GetImageOption(image_info,"tiff:alpha");
1156 if (option != (const char *) NULL)
1157 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1158 MagickFalse;
1159 if (image->matte != MagickFalse)
1160 (void) SetImageProperty(image,"tiff:alpha",
cristyd15e6592011-10-15 00:13:06 +00001161 associated_alpha != MagickFalse ? "associated" : "unassociated",
1162 exception);
cristy3ed852e2009-09-05 21:47:34 +00001163 if ((photometric == PHOTOMETRIC_PALETTE) &&
1164 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1165 {
cristybb503372010-05-27 20:51:26 +00001166 size_t
cristy3ed852e2009-09-05 21:47:34 +00001167 colors;
1168
cristybb503372010-05-27 20:51:26 +00001169 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy018f07f2011-09-04 21:15:19 +00001170 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001171 {
1172 TIFFClose(tiff);
1173 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1174 }
1175 }
1176 if (units == RESUNIT_INCH)
1177 image->units=PixelsPerInchResolution;
1178 if (units == RESUNIT_CENTIMETER)
1179 image->units=PixelsPerCentimeterResolution;
1180 value=(unsigned short) image->scene;
1181 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1182 image->scene=value;
1183 if (image_info->ping != MagickFalse)
1184 {
1185 if (image_info->number_scenes != 0)
1186 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1187 break;
1188 goto next_tiff_frame;
1189 }
1190 method=ReadGenericMethod;
1191 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1192 {
1193 char
1194 value[MaxTextExtent];
1195
1196 method=ReadStripMethod;
cristyb51dff52011-05-19 16:55:47 +00001197 (void) FormatLocaleString(value,MaxTextExtent,"%u",
cristy3ed852e2009-09-05 21:47:34 +00001198 (unsigned int) rows_per_strip);
cristyd15e6592011-10-15 00:13:06 +00001199 (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception);
cristy3ed852e2009-09-05 21:47:34 +00001200 }
1201 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1202 method=ReadRGBAMethod;
1203 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1204 method=ReadCMYKAMethod;
1205 if ((photometric != PHOTOMETRIC_RGB) &&
1206 (photometric != PHOTOMETRIC_CIELAB) &&
1207 (photometric != PHOTOMETRIC_SEPARATED))
1208 method=ReadGenericMethod;
1209 if (image->storage_class == PseudoClass)
1210 method=ReadSingleSampleMethod;
1211 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1212 (photometric == PHOTOMETRIC_MINISWHITE))
1213 method=ReadSingleSampleMethod;
1214 if ((photometric != PHOTOMETRIC_SEPARATED) &&
cristybc736662011-03-09 13:39:42 +00001215 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
cristy3ed852e2009-09-05 21:47:34 +00001216 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001217 if (image->compression == JPEGCompression)
1218 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001219 if (TIFFIsTiled(tiff) != MagickFalse)
1220 method=ReadTileMethod;
1221 quantum_type=RGBQuantum;
1222 pixels=GetQuantumPixels(quantum_info);
1223 switch (method)
1224 {
1225 case ReadSingleSampleMethod:
1226 {
1227 /*
1228 Convert TIFF image to PseudoClass MIFF image.
1229 */
1230 if ((image->storage_class == PseudoClass) &&
1231 (photometric == PHOTOMETRIC_PALETTE))
1232 {
cristybb503372010-05-27 20:51:26 +00001233 size_t
cristy3ed852e2009-09-05 21:47:34 +00001234 range;
1235
1236 uint16
1237 *blue_colormap,
1238 *green_colormap,
1239 *red_colormap;
1240
1241 /*
1242 Initialize colormap.
1243 */
1244 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1245 &green_colormap,&blue_colormap);
1246 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001247 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001248 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1249 (blue_colormap[i] >= 256))
1250 {
1251 range=65535;
1252 break;
1253 }
cristybb503372010-05-27 20:51:26 +00001254 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001255 {
cristyce70c172010-01-07 17:15:30 +00001256 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001257 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001258 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001259 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001260 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001261 blue_colormap[i])/range);
1262 }
1263 }
1264 quantum_type=IndexQuantum;
1265 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1266 if (image->matte != MagickFalse)
1267 {
1268 if (image->storage_class != PseudoClass)
1269 {
1270 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1271 GrayAlphaQuantum;
1272 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1273 }
1274 else
1275 {
1276 quantum_type=IndexAlphaQuantum;
1277 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1278 }
1279 }
1280 else
1281 if (image->storage_class != PseudoClass)
1282 {
1283 quantum_type=GrayQuantum;
1284 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1285 }
1286 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1287 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001288 {
1289 TIFFClose(tiff);
1290 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1291 }
cristy3ed852e2009-09-05 21:47:34 +00001292 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001293 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001294 {
1295 int
1296 status;
1297
cristy4c08aed2011-07-01 19:47:50 +00001298 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001299 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001300
1301 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1302 if (status == -1)
1303 break;
1304 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001305 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001306 break;
1307 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1308 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00001309 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00001310 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1311 break;
1312 if (image->previous == (Image *) NULL)
1313 {
cristycee97112010-05-28 00:44:52 +00001314 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1315 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001316 if (status == MagickFalse)
1317 break;
1318 }
1319 }
1320 break;
1321 }
1322 case ReadRGBAMethod:
1323 {
1324 /*
1325 Convert TIFF image to DirectClass MIFF image.
1326 */
1327 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1328 quantum_type=RGBQuantum;
1329 if (image->matte != MagickFalse)
1330 {
1331 quantum_type=RGBAQuantum;
1332 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1333 }
1334 if (image->colorspace == CMYKColorspace)
1335 {
1336 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1337 quantum_type=CMYKQuantum;
1338 if (image->matte != MagickFalse)
1339 {
1340 quantum_type=CMYKAQuantum;
1341 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1342 }
1343 }
1344 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1345 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001346 {
1347 TIFFClose(tiff);
1348 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1349 }
cristy3ed852e2009-09-05 21:47:34 +00001350 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001351 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001352 {
1353 int
1354 status;
1355
cristy4c08aed2011-07-01 19:47:50 +00001356 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001357 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001358
1359 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1360 if (status == -1)
1361 break;
1362 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001363 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001364 break;
cristy0b334892010-03-15 02:26:46 +00001365 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1366 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001367 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1368 break;
1369 if (image->previous == (Image *) NULL)
1370 {
cristycee97112010-05-28 00:44:52 +00001371 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1372 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001373 if (status == MagickFalse)
1374 break;
1375 }
1376 }
1377 break;
1378 }
1379 case ReadCMYKAMethod:
1380 {
1381 /*
1382 Convert TIFF image to DirectClass MIFF image.
1383 */
cristybb503372010-05-27 20:51:26 +00001384 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001385 {
cristybb503372010-05-27 20:51:26 +00001386 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001387 {
cristy4c08aed2011-07-01 19:47:50 +00001388 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001389 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001390
cristy3ed852e2009-09-05 21:47:34 +00001391 int
1392 status;
1393
1394 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1395 pixels);
1396 if (status == -1)
1397 break;
1398 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001399 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001400 break;
1401 if (image->colorspace != CMYKColorspace)
1402 switch (i)
1403 {
1404 case 0: quantum_type=RedQuantum; break;
1405 case 1: quantum_type=GreenQuantum; break;
1406 case 2: quantum_type=BlueQuantum; break;
1407 case 3: quantum_type=AlphaQuantum; break;
1408 default: quantum_type=UndefinedQuantum; break;
1409 }
cristy6d9f12f2009-11-03 14:50:26 +00001410 else
cristy3ed852e2009-09-05 21:47:34 +00001411 switch (i)
1412 {
1413 case 0: quantum_type=CyanQuantum; break;
1414 case 1: quantum_type=MagentaQuantum; break;
1415 case 2: quantum_type=YellowQuantum; break;
1416 case 3: quantum_type=BlackQuantum; break;
1417 case 4: quantum_type=AlphaQuantum; break;
1418 default: quantum_type=UndefinedQuantum; break;
1419 }
1420 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1421 quantum_type,pixels,exception);
1422 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1423 break;
1424 }
1425 if (image->previous == (Image *) NULL)
1426 {
cristycee97112010-05-28 00:44:52 +00001427 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1428 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001429 if (status == MagickFalse)
1430 break;
1431 }
1432 }
1433 break;
1434 }
1435 case ReadStripMethod:
1436 {
1437 register unsigned char
1438 *p;
1439
1440 /*
1441 Convert stripped TIFF image to DirectClass MIFF image.
1442 */
1443 i=0;
1444 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001445 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001446 {
cristybb503372010-05-27 20:51:26 +00001447 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001448 x;
1449
cristy4c08aed2011-07-01 19:47:50 +00001450 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001451 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001452
1453 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001454 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001455 break;
1456 if (i == 0)
1457 {
1458 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1459 break;
cristyeaedf062010-05-29 22:36:02 +00001460 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1461 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001462 }
1463 i--;
cristy7f5d1662011-04-14 12:43:28 +00001464 p=(unsigned char *) (((uint32 *) pixels)+image->columns*i);
cristybb503372010-05-27 20:51:26 +00001465 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001466 {
cristy4c08aed2011-07-01 19:47:50 +00001467 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1468 (TIFFGetR(*p))),q);
1469 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1470 (TIFFGetG(*p))),q);
1471 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1472 (TIFFGetB(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001473 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001474 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1475 (TIFFGetA(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001476 p++;
cristyed231572011-07-14 02:18:59 +00001477 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001478 }
1479 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1480 break;
1481 if (image->previous == (Image *) NULL)
1482 {
cristycee97112010-05-28 00:44:52 +00001483 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1484 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001485 if (status == MagickFalse)
1486 break;
1487 }
1488 }
1489 break;
1490 }
1491 case ReadTileMethod:
1492 {
1493 register uint32
1494 *p;
1495
1496 uint32
1497 *tile_pixels,
1498 columns,
1499 rows;
1500
cristybb503372010-05-27 20:51:26 +00001501 size_t
cristy3ed852e2009-09-05 21:47:34 +00001502 number_pixels;
1503
1504 /*
1505 Convert tiled TIFF image to DirectClass MIFF image.
1506 */
1507 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1508 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1509 {
1510 TIFFClose(tiff);
1511 ThrowReaderException(CoderError,"ImageIsNotTiled");
1512 }
cristyc82a27b2011-10-21 01:07:16 +00001513 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +00001514 number_pixels=columns*rows;
cristyda16f162011-02-19 23:52:17 +00001515 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
cristy3ed852e2009-09-05 21:47:34 +00001516 sizeof(*tile_pixels));
1517 if (tile_pixels == (uint32 *) NULL)
1518 {
1519 TIFFClose(tiff);
1520 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1521 }
cristybb503372010-05-27 20:51:26 +00001522 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001523 {
cristybb503372010-05-27 20:51:26 +00001524 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001525 x;
1526
cristy4c08aed2011-07-01 19:47:50 +00001527 register Quantum
1528 *restrict q,
1529 *restrict tile;
cristy3ed852e2009-09-05 21:47:34 +00001530
cristybb503372010-05-27 20:51:26 +00001531 size_t
cristy3ed852e2009-09-05 21:47:34 +00001532 columns_remaining,
1533 rows_remaining;
1534
1535 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001536 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001537 rows_remaining=rows;
1538 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1539 exception);
cristy4c08aed2011-07-01 19:47:50 +00001540 if (tile == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001541 break;
cristybb503372010-05-27 20:51:26 +00001542 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001543 {
cristybb503372010-05-27 20:51:26 +00001544 size_t
cristy3ed852e2009-09-05 21:47:34 +00001545 column,
1546 row;
1547
1548 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1549 break;
1550 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001551 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001552 columns_remaining=columns;
1553 p=tile_pixels+(rows-rows_remaining)*columns;
cristy68473222011-08-17 17:39:19 +00001554 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1555 x);
cristy3ed852e2009-09-05 21:47:34 +00001556 for (row=rows_remaining; row > 0; row--)
1557 {
1558 if (image->matte != MagickFalse)
1559 for (column=columns_remaining; column > 0; column--)
1560 {
cristy4c08aed2011-07-01 19:47:50 +00001561 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1562 TIFFGetR(*p)),q);
1563 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1564 TIFFGetG(*p)),q);
1565 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1566 TIFFGetB(*p)),q);
1567 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1568 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001569 p++;
cristyed231572011-07-14 02:18:59 +00001570 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001571 }
1572 else
1573 for (column=columns_remaining; column > 0; column--)
1574 {
cristy4c08aed2011-07-01 19:47:50 +00001575 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1576 TIFFGetR(*p)),q);
1577 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1578 TIFFGetG(*p)),q);
1579 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1580 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001581 p++;
cristyed231572011-07-14 02:18:59 +00001582 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001583 }
1584 p+=columns-columns_remaining;
cristyed231572011-07-14 02:18:59 +00001585 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
cristy3ed852e2009-09-05 21:47:34 +00001586 }
1587 }
1588 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1589 break;
1590 if (image->previous == (Image *) NULL)
1591 {
cristycee97112010-05-28 00:44:52 +00001592 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1593 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001594 if (status == MagickFalse)
1595 break;
1596 }
1597 }
1598 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1599 break;
1600 }
1601 case ReadGenericMethod:
1602 default:
1603 {
1604 register uint32
1605 *p;
1606
1607 uint32
1608 *pixels;
1609
1610 /*
1611 Convert TIFF image to DirectClass MIFF image.
1612 */
1613 number_pixels=(MagickSizeType) image->columns*image->rows;
1614 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1615 (number_pixels*sizeof(uint32))))
1616 {
1617 TIFFClose(tiff);
1618 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1619 }
1620 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1621 sizeof(uint32));
1622 if (pixels == (uint32 *) NULL)
1623 {
1624 TIFFClose(tiff);
1625 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1626 }
1627 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1628 (uint32) image->rows,(uint32 *) pixels,0);
1629 /*
1630 Convert image to DirectClass pixel packets.
1631 */
1632 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001633 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001634 {
cristybb503372010-05-27 20:51:26 +00001635 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001636 x;
1637
cristy4c08aed2011-07-01 19:47:50 +00001638 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001639 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001640
1641 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001642 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001643 break;
cristyed231572011-07-14 02:18:59 +00001644 q+=GetPixelChannels(image)*(image->columns-1);
cristybb503372010-05-27 20:51:26 +00001645 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001646 {
cristy4c08aed2011-07-01 19:47:50 +00001647 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1648 TIFFGetR(*p)),q);
1649 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1650 TIFFGetG(*p)),q);
1651 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1652 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001653 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001654 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1655 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001656 p--;
cristyed231572011-07-14 02:18:59 +00001657 q-=GetPixelChannels(image);;
cristy3ed852e2009-09-05 21:47:34 +00001658 }
1659 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1660 break;
1661 if (image->previous == (Image *) NULL)
1662 {
cristycee97112010-05-28 00:44:52 +00001663 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1664 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001665 if (status == MagickFalse)
1666 break;
1667 }
1668 }
1669 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1670 break;
1671 }
1672 }
1673 SetQuantumImageType(image,quantum_type);
1674 next_tiff_frame:
cristya9a6ec42011-10-26 16:55:01 +00001675 quantum_info=DestroyQuantumInfo(quantum_info);
cristy3ed852e2009-09-05 21:47:34 +00001676 if ((photometric == PHOTOMETRIC_LOGL) ||
1677 (photometric == PHOTOMETRIC_MINISBLACK) ||
1678 (photometric == PHOTOMETRIC_MINISWHITE))
1679 {
1680 image->type=GrayscaleType;
1681 if (bits_per_sample == 1)
1682 image->type=BilevelType;
1683 }
1684 if (image->storage_class == PseudoClass)
1685 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001686 image->endian=MSBEndian;
1687 if (endian == FILLORDER_LSB2MSB)
1688 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001689 if ((photometric == PHOTOMETRIC_LOGL) ||
1690 (photometric == PHOTOMETRIC_MINISBLACK) ||
1691 (photometric == PHOTOMETRIC_MINISWHITE))
1692 {
1693 image->type=GrayscaleType;
1694 if (bits_per_sample == 1)
1695 image->type=BilevelType;
1696 }
1697 /*
1698 Proceed to next image.
1699 */
1700 if (image_info->number_scenes != 0)
1701 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1702 break;
1703 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1704 if (status == MagickTrue)
1705 {
1706 /*
1707 Allocate next image structure.
1708 */
cristy9950d572011-10-01 18:22:35 +00001709 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001710 if (GetNextImageInList(image) == (Image *) NULL)
1711 {
1712 image=DestroyImageList(image);
1713 return((Image *) NULL);
1714 }
1715 image=SyncNextImageInList(image);
1716 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1717 image->scene);
1718 if (status == MagickFalse)
1719 break;
1720 }
cristy3ed852e2009-09-05 21:47:34 +00001721 } while (status == MagickTrue);
1722 (void) TIFFSetWarningHandler(warning_handler);
1723 (void) TIFFSetErrorHandler(error_handler);
1724 TIFFClose(tiff);
1725 return(GetFirstImageInList(image));
1726}
1727#endif
1728
1729/*
1730%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1731% %
1732% %
1733% %
1734% R e g i s t e r T I F F I m a g e %
1735% %
1736% %
1737% %
1738%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1739%
1740% RegisterTIFFImage() adds properties for the TIFF image format to
1741% the list of supported formats. The properties include the image format
1742% tag, a method to read and/or write the format, whether the format
1743% supports the saving of more than one frame to the same file or blob,
1744% whether the format supports native in-memory I/O, and a brief
1745% description of the format.
1746%
1747% The format of the RegisterTIFFImage method is:
1748%
cristybb503372010-05-27 20:51:26 +00001749% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001750%
1751*/
cristybb503372010-05-27 20:51:26 +00001752ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001753{
1754#define TIFFDescription "Tagged Image File Format"
1755
1756 char
1757 version[MaxTextExtent];
1758
1759 MagickInfo
1760 *entry;
cristy6b032822010-06-29 16:52:32 +00001761
cristy18b17442009-10-25 18:36:48 +00001762 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1763 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001764 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001765 if (instantiate_key == MagickFalse)
1766 {
1767 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1768 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1769 instantiate_key=MagickTrue;
1770 }
cristyf84a1932010-01-03 18:00:18 +00001771 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001772 *version='\0';
1773#if defined(TIFF_VERSION)
cristyb51dff52011-05-19 16:55:47 +00001774 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
cristy3ed852e2009-09-05 21:47:34 +00001775#endif
1776#if defined(MAGICKCORE_TIFF_DELEGATE)
1777 {
1778 const char
1779 *p;
1780
cristybb503372010-05-27 20:51:26 +00001781 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001782 i;
1783
1784 p=TIFFGetVersion();
1785 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1786 version[i]=(*p++);
1787 version[i]='\0';
1788 }
1789#endif
1790
cristy3d7f8062009-09-24 20:45:35 +00001791 entry=SetMagickInfo("GROUP4");
1792#if defined(MAGICKCORE_TIFF_DELEGATE)
1793 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1794 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1795#endif
1796 entry->raw=MagickTrue;
1797 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001798 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001799 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001800 entry->seekable_stream=MagickTrue;
1801 entry->thread_support=NoThreadSupport;
1802 entry->description=ConstantString("Raw CCITT Group4");
1803 entry->module=ConstantString("TIFF");
1804 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001805 entry=SetMagickInfo("PTIF");
1806#if defined(MAGICKCORE_TIFF_DELEGATE)
1807 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1808 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1809#endif
1810 entry->endian_support=MagickTrue;
1811 entry->seekable_stream=MagickTrue;
1812 entry->thread_support=NoThreadSupport;
1813 entry->description=ConstantString("Pyramid encoded TIFF");
1814 entry->module=ConstantString("TIFF");
1815 (void) RegisterMagickInfo(entry);
1816 entry=SetMagickInfo("TIF");
1817#if defined(MAGICKCORE_TIFF_DELEGATE)
1818 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1819 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1820#endif
1821 entry->endian_support=MagickTrue;
1822 entry->seekable_stream=MagickTrue;
1823 entry->stealth=MagickTrue;
1824 entry->thread_support=NoThreadSupport;
1825 entry->description=ConstantString(TIFFDescription);
1826 if (*version != '\0')
1827 entry->version=ConstantString(version);
1828 entry->module=ConstantString("TIFF");
1829 (void) RegisterMagickInfo(entry);
1830 entry=SetMagickInfo("TIFF");
1831#if defined(MAGICKCORE_TIFF_DELEGATE)
1832 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1833 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1834#endif
1835 entry->magick=(IsImageFormatHandler *) IsTIFF;
1836 entry->endian_support=MagickTrue;
1837 entry->seekable_stream=MagickTrue;
1838 entry->thread_support=NoThreadSupport;
1839 entry->description=ConstantString(TIFFDescription);
1840 if (*version != '\0')
1841 entry->version=ConstantString(version);
1842 entry->module=ConstantString("TIFF");
1843 (void) RegisterMagickInfo(entry);
1844 entry=SetMagickInfo("TIFF64");
1845#if defined(TIFF_VERSION_BIG)
1846 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1847 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1848#endif
1849 entry->adjoin=MagickFalse;
1850 entry->endian_support=MagickTrue;
1851 entry->seekable_stream=MagickTrue;
1852 entry->thread_support=NoThreadSupport;
1853 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1854 if (*version != '\0')
1855 entry->version=ConstantString(version);
1856 entry->module=ConstantString("TIFF");
1857 (void) RegisterMagickInfo(entry);
1858 return(MagickImageCoderSignature);
1859}
1860
1861/*
1862%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1863% %
1864% %
1865% %
1866% U n r e g i s t e r T I F F I m a g e %
1867% %
1868% %
1869% %
1870%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1871%
1872% UnregisterTIFFImage() removes format registrations made by the TIFF module
1873% from the list of supported formats.
1874%
1875% The format of the UnregisterTIFFImage method is:
1876%
1877% UnregisterTIFFImage(void)
1878%
1879*/
1880ModuleExport void UnregisterTIFFImage(void)
1881{
cristy3d7f8062009-09-24 20:45:35 +00001882 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001883 (void) UnregisterMagickInfo("PTIF");
1884 (void) UnregisterMagickInfo("TIF");
1885 (void) UnregisterMagickInfo("TIFF");
1886 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001887 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1888 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001889 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001890 if (instantiate_key != MagickFalse)
1891 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1892 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1893 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001894 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001895 DestroySemaphoreInfo(&tiff_semaphore);
1896}
1897
1898#if defined(MAGICKCORE_TIFF_DELEGATE)
1899/*
1900%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1901% %
1902% %
1903% %
cristy3d7f8062009-09-24 20:45:35 +00001904% W r i t e G R O U P 4 I m a g e %
1905% %
1906% %
1907% %
1908%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1909%
1910% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1911%
1912% The format of the WriteGROUP4Image method is:
1913%
1914% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001915% Image *image,ExceptionInfo *)
cristy3d7f8062009-09-24 20:45:35 +00001916%
1917% A description of each parameter follows:
1918%
1919% o image_info: the image info.
1920%
1921% o image: The image.
1922%
cristy3a37efd2011-08-28 20:31:03 +00001923% o exception: return any errors or warnings in this structure.
1924%
cristy3d7f8062009-09-24 20:45:35 +00001925*/
1926static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001927 Image *image,ExceptionInfo *exception)
cristy3d7f8062009-09-24 20:45:35 +00001928{
1929 char
1930 filename[MaxTextExtent];
1931
1932 FILE
1933 *file;
1934
1935 Image
1936 *huffman_image;
1937
1938 ImageInfo
1939 *write_info;
1940
1941 int
1942 unique_file;
1943
1944 MagickBooleanType
1945 status;
1946
cristybb503372010-05-27 20:51:26 +00001947 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001948 i;
1949
1950 ssize_t
1951 count;
1952
1953 TIFF
1954 *tiff;
1955
cristy94c8fe42009-10-06 01:57:36 +00001956 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001957 *byte_count,
1958 strip_size;
1959
1960 unsigned char
1961 *buffer;
1962
1963 /*
1964 Write image as CCITT Group4 TIFF image to a temporary file.
1965 */
1966 assert(image_info != (const ImageInfo *) NULL);
1967 assert(image_info->signature == MagickSignature);
1968 assert(image != (Image *) NULL);
1969 assert(image->signature == MagickSignature);
1970 if (image->debug != MagickFalse)
1971 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001972 assert(exception != (ExceptionInfo *) NULL);
1973 assert(exception->signature == MagickSignature);
1974 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3d7f8062009-09-24 20:45:35 +00001975 if (status == MagickFalse)
1976 return(status);
cristy3a37efd2011-08-28 20:31:03 +00001977 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3d7f8062009-09-24 20:45:35 +00001978 if (huffman_image == (Image *) NULL)
1979 {
1980 (void) CloseBlob(image);
1981 return(MagickFalse);
1982 }
cristy94c8fe42009-10-06 01:57:36 +00001983 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001984 file=(FILE *) NULL;
1985 unique_file=AcquireUniqueFileResource(filename);
1986 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001987 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001988 if ((unique_file == -1) || (file == (FILE *) NULL))
1989 {
cristy3a37efd2011-08-28 20:31:03 +00001990 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1991 filename);
cristy3d7f8062009-09-24 20:45:35 +00001992 return(MagickFalse);
1993 }
cristyb51dff52011-05-19 16:55:47 +00001994 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
cristy3d7f8062009-09-24 20:45:35 +00001995 filename);
cristy018f07f2011-09-04 21:15:19 +00001996 (void) SetImageType(huffman_image,BilevelType,exception);
cristy14efd992009-09-26 23:46:03 +00001997 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001998 SetImageInfoFile(write_info,file);
1999 write_info->compression=Group4Compression;
2000 write_info->type=BilevelType;
2001 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
cristy3a37efd2011-08-28 20:31:03 +00002002 status=WriteTIFFImage(write_info,huffman_image,exception);
cristy3d7f8062009-09-24 20:45:35 +00002003 (void) fflush(file);
2004 write_info=DestroyImageInfo(write_info);
2005 if (status == MagickFalse)
2006 {
cristy3d7f8062009-09-24 20:45:35 +00002007 huffman_image=DestroyImage(huffman_image);
2008 (void) fclose(file);
2009 (void) RelinquishUniqueFileResource(filename);
2010 return(MagickFalse);
2011 }
2012 tiff=TIFFOpen(filename,"rb");
2013 if (tiff == (TIFF *) NULL)
2014 {
2015 huffman_image=DestroyImage(huffman_image);
2016 (void) fclose(file);
2017 (void) RelinquishUniqueFileResource(filename);
cristy3a37efd2011-08-28 20:31:03 +00002018 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
cristy3d7f8062009-09-24 20:45:35 +00002019 image_info->filename);
2020 return(MagickFalse);
2021 }
2022 /*
2023 Allocate raw strip buffer.
2024 */
cristy94c8fe42009-10-06 01:57:36 +00002025 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2026 {
2027 TIFFClose(tiff);
2028 huffman_image=DestroyImage(huffman_image);
2029 (void) fclose(file);
2030 (void) RelinquishUniqueFileResource(filename);
2031 return(MagickFalse);
2032 }
cristy3d7f8062009-09-24 20:45:35 +00002033 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00002034 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002035 if (byte_count[i] > strip_size)
2036 strip_size=byte_count[i];
2037 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2038 sizeof(*buffer));
2039 if (buffer == (unsigned char *) NULL)
2040 {
2041 TIFFClose(tiff);
2042 huffman_image=DestroyImage(huffman_image);
2043 (void) fclose(file);
2044 (void) RelinquishUniqueFileResource(filename);
2045 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2046 image_info->filename);
2047 }
2048 /*
2049 Compress runlength encoded to 2D Huffman pixels.
2050 */
cristybb503372010-05-27 20:51:26 +00002051 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002052 {
cristy94c8fe42009-10-06 01:57:36 +00002053 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00002054 if (WriteBlob(image,(size_t) count,buffer) != count)
2055 status=MagickFalse;
2056 }
2057 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2058 TIFFClose(tiff);
2059 huffman_image=DestroyImage(huffman_image);
2060 (void) fclose(file);
2061 (void) RelinquishUniqueFileResource(filename);
2062 (void) CloseBlob(image);
2063 return(status);
2064}
2065#endif
2066
2067#if defined(MAGICKCORE_TIFF_DELEGATE)
2068/*
2069%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2070% %
2071% %
2072% %
cristy3ed852e2009-09-05 21:47:34 +00002073% W r i t e P T I F I m a g e %
2074% %
2075% %
2076% %
2077%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2078%
2079% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2080% format.
2081%
2082% The format of the WritePTIFImage method is:
2083%
2084% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002085% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002086%
2087% A description of each parameter follows:
2088%
2089% o image_info: the image info.
2090%
2091% o image: The image.
2092%
cristy3a37efd2011-08-28 20:31:03 +00002093% o exception: return any errors or warnings in this structure.
2094%
cristy3ed852e2009-09-05 21:47:34 +00002095*/
2096static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002097 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002098{
2099 Image
2100 *images,
2101 *next,
2102 *pyramid_image;
2103
2104 ImageInfo
2105 *write_info;
2106
2107 MagickBooleanType
2108 status;
2109
cristybb503372010-05-27 20:51:26 +00002110 size_t
cristy3ed852e2009-09-05 21:47:34 +00002111 columns,
2112 rows;
2113
2114 /*
2115 Create pyramid-encoded TIFF image.
2116 */
2117 images=NewImageList();
2118 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2119 {
cristy3a37efd2011-08-28 20:31:03 +00002120 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,exception));
cristy3ed852e2009-09-05 21:47:34 +00002121 columns=next->columns;
2122 rows=next->rows;
2123 while ((columns > 64) && (rows > 64))
2124 {
2125 columns/=2;
2126 rows/=2;
cristy98ca0f52011-10-08 23:19:10 +00002127 pyramid_image=ResizeImage(next,columns,rows,image->filter,image->blur,
cristy3a37efd2011-08-28 20:31:03 +00002128 exception);
cristy3ed852e2009-09-05 21:47:34 +00002129 AppendImageToList(&images,pyramid_image);
2130 }
2131 }
2132 /*
2133 Write pyramid-encoded TIFF image.
2134 */
cristy58567532009-09-26 01:13:32 +00002135 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002136 *write_info->magick='\0';
2137 write_info->adjoin=MagickTrue;
cristy3a37efd2011-08-28 20:31:03 +00002138 status=WriteTIFFImage(write_info,GetFirstImageInList(images),exception);
cristy3ed852e2009-09-05 21:47:34 +00002139 images=DestroyImageList(images);
2140 write_info=DestroyImageInfo(write_info);
2141 return(status);
2142}
2143#endif
2144
2145#if defined(MAGICKCORE_TIFF_DELEGATE)
2146/*
2147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2148% %
2149% %
2150% W r i t e T I F F I m a g e %
2151% %
2152% %
2153% %
2154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2155%
2156% WriteTIFFImage() writes an image in the Tagged image file format.
2157%
2158% The format of the WriteTIFFImage method is:
2159%
2160% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002161% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002162%
2163% A description of each parameter follows:
2164%
2165% o image_info: the image info.
2166%
2167% o image: The image.
2168%
cristy3a37efd2011-08-28 20:31:03 +00002169% o exception: return any errors or warnings in this structure.
2170%
cristy3ed852e2009-09-05 21:47:34 +00002171*/
2172
2173typedef struct _TIFFInfo
2174{
2175 RectangleInfo
2176 tile_geometry;
2177
2178 unsigned char
2179 *scanline,
2180 *scanlines,
2181 *pixels;
2182} TIFFInfo;
2183
2184static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2185{
2186 assert(tiff_info != (TIFFInfo *) NULL);
2187 if (tiff_info->scanlines != (unsigned char *) NULL)
2188 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2189 tiff_info->scanlines);
2190 if (tiff_info->pixels != (unsigned char *) NULL)
2191 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2192 tiff_info->pixels);
2193}
2194
2195static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2196 TIFFInfo *tiff_info)
2197{
2198 const char
2199 *option;
2200
2201 MagickStatusType
2202 flags;
2203
cristy79e5dad2010-09-16 19:48:33 +00002204 uint32
2205 tile_columns,
2206 tile_rows;
2207
cristy3ed852e2009-09-05 21:47:34 +00002208 assert(tiff_info != (TIFFInfo *) NULL);
2209 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2210 option=GetImageOption(image_info,"tiff:tile-geometry");
2211 if (option == (const char *) NULL)
2212 return(MagickTrue);
2213 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2214 if ((flags & HeightValue) == 0)
2215 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002216 tile_columns=(uint32) tiff_info->tile_geometry.width;
2217 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002218 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2219 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2220 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2221 tiff_info->tile_geometry.width=tile_columns;
2222 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002223 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002224 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002225 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002226 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002227 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2228 (tiff_info->pixels == (unsigned char *) NULL))
2229 {
2230 DestroyTIFFInfo(tiff_info);
2231 return(MagickFalse);
2232 }
2233 return(MagickTrue);
2234}
2235
cristybb503372010-05-27 20:51:26 +00002236static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002237 tsample_t sample,Image *image)
2238{
2239 int32
2240 status;
2241
cristybb503372010-05-27 20:51:26 +00002242 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002243 i;
2244
2245 register unsigned char
2246 *p,
2247 *q;
2248
cristybb503372010-05-27 20:51:26 +00002249 size_t
cristy3ed852e2009-09-05 21:47:34 +00002250 number_tiles,
2251 tile_width;
2252
cristyc6da28e2011-04-28 01:41:35 +00002253 ssize_t
2254 bytes_per_pixel,
2255 j,
2256 k,
2257 l;
2258
cristy3ed852e2009-09-05 21:47:34 +00002259 if (TIFFIsTiled(tiff) == 0)
2260 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2261 /*
2262 Fill scanlines to tile height.
2263 */
cristybb503372010-05-27 20:51:26 +00002264 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002265 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2266 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002267 if (((size_t) (row % tiff_info->tile_geometry.height) !=
cristyc6da28e2011-04-28 01:41:35 +00002268 (tiff_info->tile_geometry.height-1)) &&
2269 (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002270 return(0);
2271 /*
2272 Write tile to TIFF image.
2273 */
2274 status=0;
cristybb503372010-05-27 20:51:26 +00002275 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002276 tiff_info->tile_geometry.width);
2277 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2278 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002279 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002280 {
cristybb503372010-05-27 20:51:26 +00002281 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002282 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002283 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2284 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002285 {
2286 if (bytes_per_pixel == 0)
2287 {
2288 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2289 tiff_info->tile_geometry.width+k)/8);
2290 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2291 *q++=(*p++);
2292 continue;
2293 }
2294 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2295 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2296 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2297 for (l=0; l < bytes_per_pixel; l++)
2298 *q++=(*p++);
2299 }
cristydaff8092010-04-22 14:50:53 +00002300 if ((i*tiff_info->tile_geometry.width) != image->columns)
2301 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2302 tiff_info->tile_geometry.width),(uint32) ((row/
2303 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2304 sample);
cristy3ed852e2009-09-05 21:47:34 +00002305 if (status < 0)
2306 break;
2307 }
2308 return(status);
2309}
2310
2311static void TIFFSetProfiles(TIFF *tiff,Image *image)
2312{
2313 const char
2314 *name;
2315
2316 const StringInfo
2317 *profile;
2318
2319 if (image->profiles == (void *) NULL)
2320 return;
2321 ResetImageProfileIterator(image);
2322 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2323 {
2324 profile=GetImageProfile(image,name);
2325#if defined(TIFFTAG_XMLPACKET)
2326 if (LocaleCompare(name,"xmp") == 0)
2327 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2328 profile),GetStringInfoDatum(profile));
2329#endif
2330#if defined(TIFFTAG_ICCPROFILE)
2331 if (LocaleCompare(name,"icc") == 0)
2332 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2333 profile),GetStringInfoDatum(profile));
2334#endif
2335 if (LocaleCompare(name,"iptc") == 0)
2336 {
2337 size_t
2338 length;
2339
2340 StringInfo
2341 *iptc_profile;
2342
2343 iptc_profile=CloneStringInfo(profile);
2344 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2345 0x03);
2346 SetStringInfoLength(iptc_profile,length);
2347 if (TIFFIsByteSwapped(tiff))
2348 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002349 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002350 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2351 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2352 iptc_profile=DestroyStringInfo(iptc_profile);
2353 }
2354#if defined(TIFFTAG_PHOTOSHOP)
2355 if (LocaleCompare(name,"8bim") == 0)
2356 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2357 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2358#endif
2359 if (LocaleCompare(name,"tiff:37724") == 0)
2360 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2361 GetStringInfoDatum(profile));
2362 name=GetNextImageProfile(image);
2363 }
2364}
2365
cristyd15e6592011-10-15 00:13:06 +00002366static void TIFFSetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002367{
2368 const char
2369 *value;
2370
cristy15d8d212010-09-17 01:56:30 +00002371 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristyd15e6592011-10-15 00:13:06 +00002372 value=GetImageProperty(image,"tiff:hostcomputer",exception);
cristy3ed852e2009-09-05 21:47:34 +00002373 if (value != (const char *) NULL)
2374 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
cristyd15e6592011-10-15 00:13:06 +00002375 value=GetImageProperty(image,"tiff:artist",exception);
cristy3ed852e2009-09-05 21:47:34 +00002376 if (value != (const char *) NULL)
2377 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
cristyd15e6592011-10-15 00:13:06 +00002378 value=GetImageProperty(image,"tiff:timestamp",exception);
cristy3ed852e2009-09-05 21:47:34 +00002379 if (value != (const char *) NULL)
2380 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
cristyd15e6592011-10-15 00:13:06 +00002381 value=GetImageProperty(image,"tiff:make",exception);
cristy3ed852e2009-09-05 21:47:34 +00002382 if (value != (const char *) NULL)
2383 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
cristyd15e6592011-10-15 00:13:06 +00002384 value=GetImageProperty(image,"tiff:model",exception);
cristy3ed852e2009-09-05 21:47:34 +00002385 if (value != (const char *) NULL)
2386 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristyd15e6592011-10-15 00:13:06 +00002387 value=GetImageProperty(image,"tiff:software",exception);
cristy15d8d212010-09-17 01:56:30 +00002388 if (value != (const char *) NULL)
2389 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristyd15e6592011-10-15 00:13:06 +00002390 value=GetImageProperty(image,"tiff:copyright",exception);
cristy3ed852e2009-09-05 21:47:34 +00002391 if (value != (const char *) NULL)
cristy98ca0f52011-10-08 23:19:10 +00002392 (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value);
cristyd15e6592011-10-15 00:13:06 +00002393 value=GetImageProperty(image,"kodak-33423",exception);
cristy3ed852e2009-09-05 21:47:34 +00002394 if (value != (const char *) NULL)
2395 (void) TIFFSetField(tiff,33423,value);
cristyd15e6592011-10-15 00:13:06 +00002396 value=GetImageProperty(image,"kodak-36867",exception);
cristy3ed852e2009-09-05 21:47:34 +00002397 if (value != (const char *) NULL)
2398 (void) TIFFSetField(tiff,36867,value);
cristyd15e6592011-10-15 00:13:06 +00002399 value=GetImageProperty(image,"label",exception);
cristy3ed852e2009-09-05 21:47:34 +00002400 if (value != (const char *) NULL)
2401 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
cristyd15e6592011-10-15 00:13:06 +00002402 value=GetImageProperty(image,"comment",exception);
cristy3ed852e2009-09-05 21:47:34 +00002403 if (value != (const char *) NULL)
2404 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2405}
2406
cristyd15e6592011-10-15 00:13:06 +00002407static void TIFFSetEXIFProperties(TIFF *tiff,Image *image,
2408 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002409{
2410#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2411 const char
2412 *value;
2413
cristybb503372010-05-27 20:51:26 +00002414 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002415 i;
2416
2417 uint32
2418 offset;
2419
2420 /*
2421 Write EXIF properties.
2422 */
2423 offset=0;
2424 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2425 for (i=0; exif_info[i].tag != 0; i++)
2426 {
cristyd15e6592011-10-15 00:13:06 +00002427 value=GetImageProperty(image,exif_info[i].property,exception);
cristy3ed852e2009-09-05 21:47:34 +00002428 if (value == (const char *) NULL)
2429 continue;
2430 switch (exif_info[i].type)
2431 {
2432 case TIFF_ASCII:
2433 {
2434 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2435 break;
2436 }
2437 case TIFF_SHORT:
2438 {
2439 uint16
2440 shorty;
2441
cristyf2f27272009-12-17 14:48:46 +00002442 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002443 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2444 break;
2445 }
2446 case TIFF_LONG:
2447 {
2448 uint16
cristybb503372010-05-27 20:51:26 +00002449 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002450
cristybb503372010-05-27 20:51:26 +00002451 ssize_ty=(uint16) StringToLong(value);
2452 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002453 break;
2454 }
2455 case TIFF_RATIONAL:
2456 case TIFF_SRATIONAL:
2457 {
2458 float
2459 rational;
2460
cristyc1acd842011-05-19 23:05:47 +00002461 rational=InterpretLocaleValue(value,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002462 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2463 break;
2464 }
2465 default:
2466 break;
2467 }
2468 }
2469 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2470#else
2471 (void) tiff;
2472 (void) image;
2473#endif
2474}
2475
2476static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002477 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002478{
2479#if !defined(TIFFDefaultStripSize)
2480#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2481#endif
2482
2483 const char
2484 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002485 *option;
cristy3ed852e2009-09-05 21:47:34 +00002486
2487 CompressionType
2488 compression;
2489
cristy6b032822010-06-29 16:52:32 +00002490 EndianType
2491 endian_type;
2492
cristy3ed852e2009-09-05 21:47:34 +00002493 MagickBooleanType
2494 debug,
2495 status;
2496
2497 MagickOffsetType
2498 scene;
2499
2500 QuantumInfo
2501 *quantum_info;
2502
2503 QuantumType
2504 quantum_type;
2505
cristybb503372010-05-27 20:51:26 +00002506 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002507 i;
2508
2509 size_t
cristy5ecaba72011-02-18 02:05:32 +00002510 length,
2511 lsb_first;
cristy3ed852e2009-09-05 21:47:34 +00002512
cristyc6da28e2011-04-28 01:41:35 +00002513 ssize_t
2514 y;
2515
cristy3ed852e2009-09-05 21:47:34 +00002516 TIFF
2517 *tiff;
2518
2519 TIFFErrorHandler
2520 error_handler,
2521 warning_handler;
2522
2523 TIFFInfo
2524 tiff_info;
2525
2526 uint16
2527 bits_per_sample,
2528 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002529 endian,
cristy3ed852e2009-09-05 21:47:34 +00002530 photometric;
2531
2532 uint32
2533 rows_per_strip;
2534
2535 unsigned char
2536 *pixels;
2537
cristy3ed852e2009-09-05 21:47:34 +00002538 /*
2539 Open TIFF file.
2540 */
2541 assert(image_info != (const ImageInfo *) NULL);
2542 assert(image_info->signature == MagickSignature);
2543 assert(image != (Image *) NULL);
2544 assert(image->signature == MagickSignature);
2545 if (image->debug != MagickFalse)
2546 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002547 assert(exception != (ExceptionInfo *) NULL);
2548 assert(exception->signature == MagickSignature);
2549 assert(exception != (ExceptionInfo *) NULL);
2550 assert(exception->signature == MagickSignature);
2551 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00002552 if (status == MagickFalse)
2553 return(status);
cristy3a37efd2011-08-28 20:31:03 +00002554 (void) MagickSetThreadValue(tiff_exception,exception);
cristy3ed852e2009-09-05 21:47:34 +00002555 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2556 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002557 endian_type=UndefinedEndian;
2558 option=GetImageOption(image_info,"tiff:endian");
2559 if (option != (const char *) NULL)
2560 {
2561 if (LocaleNCompare(option,"msb",3) == 0)
2562 endian_type=MSBEndian;
2563 if (LocaleNCompare(option,"lsb",3) == 0)
2564 endian_type=LSBEndian;;
2565 }
2566 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002567 {
2568 case LSBEndian: mode="wl"; break;
2569 case MSBEndian: mode="wb"; break;
2570 default: mode="w"; break;
2571 }
2572#if defined(TIFF_VERSION_BIG)
2573 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002574 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002575 {
2576 case LSBEndian: mode="wl8"; break;
2577 case MSBEndian: mode="wb8"; break;
2578 default: mode="w8"; break;
2579 }
2580#endif
2581 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2582 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2583 TIFFUnmapBlob);
2584 if (tiff == (TIFF *) NULL)
2585 {
2586 (void) TIFFSetWarningHandler(warning_handler);
2587 (void) TIFFSetErrorHandler(error_handler);
2588 return(MagickFalse);
2589 }
2590 scene=0;
2591 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +00002592 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +00002593 do
2594 {
2595 /*
2596 Initialize TIFF fields.
2597 */
cristy5f1c1ff2010-12-23 21:38:06 +00002598 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002599 (image_info->type != OptimizeType))
cristy018f07f2011-09-04 21:15:19 +00002600 (void) SetImageType(image,image_info->type,exception);
cristy3ed852e2009-09-05 21:47:34 +00002601 quantum_info=AcquireQuantumInfo(image_info,image);
2602 if (quantum_info == (QuantumInfo *) NULL)
2603 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2604 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2605 (quantum_info->format == UndefinedQuantumFormat) &&
cristy3a37efd2011-08-28 20:31:03 +00002606 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002607 {
2608 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2609 if (status == MagickFalse)
2610 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2611 }
2612 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2613 (GetPreviousImageInList(image) != (Image *) NULL))
2614 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2615 if ((image->columns != (uint32) image->columns) ||
2616 (image->rows != (uint32) image->rows))
2617 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2618 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2619 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2620 compression=image->compression;
2621 if (image_info->compression != UndefinedCompression)
2622 compression=image_info->compression;
2623 switch (compression)
2624 {
2625 case FaxCompression:
2626 {
2627 compress_tag=COMPRESSION_CCITTFAX3;
2628 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2629 break;
2630 }
2631 case Group4Compression:
2632 {
2633 compress_tag=COMPRESSION_CCITTFAX4;
2634 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2635 break;
2636 }
cristy6d5e20f2011-04-25 13:48:54 +00002637#if defined(COMPRESSION_JBIG)
2638 case JBIG1Compression:
2639 {
2640 compress_tag=COMPRESSION_JBIG;
2641 break;
2642 }
2643#endif
cristy3ed852e2009-09-05 21:47:34 +00002644 case JPEGCompression:
2645 {
2646 compress_tag=COMPRESSION_JPEG;
2647 break;
2648 }
cristyfbb0ef02010-12-19 02:32:11 +00002649#if defined(COMPRESSION_LZMA)
2650 case LZMACompression:
2651 {
2652 compress_tag=COMPRESSION_LZMA;
2653 break;
2654 }
2655#endif
cristy3ed852e2009-09-05 21:47:34 +00002656 case LZWCompression:
2657 {
2658 compress_tag=COMPRESSION_LZW;
2659 break;
2660 }
2661 case RLECompression:
2662 {
2663 compress_tag=COMPRESSION_PACKBITS;
2664 break;
2665 }
2666 case ZipCompression:
2667 {
2668 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2669 break;
2670 }
2671 case NoCompression:
2672 default:
2673 {
2674 compress_tag=COMPRESSION_NONE;
2675 break;
2676 }
2677 }
cristy5b675872009-10-21 13:12:00 +00002678#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2679 if ((compress_tag != COMPRESSION_NONE) &&
2680 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002681 {
cristy3a37efd2011-08-28 20:31:03 +00002682 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2683 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002684 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002685 compress_tag=COMPRESSION_NONE;
2686 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002687 }
2688#else
2689 switch (compress_tag)
2690 {
2691#if defined(CCITT_SUPPORT)
2692 case COMPRESSION_CCITTFAX3:
2693 case COMPRESSION_CCITTFAX4:
2694#endif
2695#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2696 case COMPRESSION_JPEG:
2697#endif
cristyfbb0ef02010-12-19 02:32:11 +00002698#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2699 case COMPRESSION_LZMA:
2700#endif
cristy3ed852e2009-09-05 21:47:34 +00002701#if defined(LZW_SUPPORT)
2702 case COMPRESSION_LZW:
2703#endif
2704#if defined(PACKBITS_SUPPORT)
2705 case COMPRESSION_PACKBITS:
2706#endif
2707#if defined(ZIP_SUPPORT)
2708 case COMPRESSION_ADOBE_DEFLATE:
2709#endif
2710 case COMPRESSION_NONE:
2711 break;
2712 default:
2713 {
cristy3a37efd2011-08-28 20:31:03 +00002714 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2715 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002716 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002717 compress_tag=COMPRESSION_NONE;
2718 compression=NoCompression;
2719 break;
2720 }
2721 }
2722#endif
2723 switch (compression)
2724 {
2725 case FaxCompression:
2726 case Group4Compression:
2727 {
cristy018f07f2011-09-04 21:15:19 +00002728 (void) SetImageType(image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +00002729 break;
2730 }
2731 case JPEGCompression:
2732 {
cristy3a37efd2011-08-28 20:31:03 +00002733 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a11cb12011-10-19 23:53:34 +00002734 (void) SetImageDepth(image,8,exception);
cristy3ed852e2009-09-05 21:47:34 +00002735 break;
2736 }
2737 default:
2738 break;
2739 }
cristy3ed852e2009-09-05 21:47:34 +00002740 if (image->colorspace == CMYKColorspace)
2741 {
2742 photometric=PHOTOMETRIC_SEPARATED;
2743 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2744 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2745 }
2746 else
2747 {
2748 /*
2749 Full color TIFF raster.
2750 */
2751 if (image->colorspace == LabColorspace)
2752 photometric=PHOTOMETRIC_CIELAB;
2753 else
2754 if (image->colorspace == YCbCrColorspace)
2755 {
2756 photometric=PHOTOMETRIC_YCBCR;
2757 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy3a37efd2011-08-28 20:31:03 +00002758 (void) SetImageStorageClass(image,DirectClass,exception);
cristy8a11cb12011-10-19 23:53:34 +00002759 (void) SetImageDepth(image,8,exception);
cristy3ed852e2009-09-05 21:47:34 +00002760 }
2761 else
2762 {
cristy510d06a2011-07-06 23:43:54 +00002763 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +00002764 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00002765 photometric=PHOTOMETRIC_RGB;
2766 }
2767 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2768 if ((image_info->type != TrueColorType) &&
2769 (image_info->type != TrueColorMatteType))
2770 {
cristy94c8fe42009-10-06 01:57:36 +00002771 if ((image_info->type != PaletteType) &&
cristy3a37efd2011-08-28 20:31:03 +00002772 (IsImageGray(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002773 {
cristy94c8fe42009-10-06 01:57:36 +00002774 photometric=(uint16) (quantum_info->min_is_white !=
2775 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2776 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002777 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002778 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy3a37efd2011-08-28 20:31:03 +00002779 (IsImageMonochrome(image,exception) != MagickFalse))
cristy94c8fe42009-10-06 01:57:36 +00002780 {
2781 status=SetQuantumDepth(image,quantum_info,1);
2782 if (status == MagickFalse)
2783 ThrowWriterException(ResourceLimitError,
2784 "MemoryAllocationFailed");
2785 }
cristy3ed852e2009-09-05 21:47:34 +00002786 }
2787 else
cristy94c8fe42009-10-06 01:57:36 +00002788 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002789 {
cristybb503372010-05-27 20:51:26 +00002790 size_t
cristy94c8fe42009-10-06 01:57:36 +00002791 depth;
2792
2793 /*
2794 Colormapped TIFF raster.
2795 */
cristy3ed852e2009-09-05 21:47:34 +00002796 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002797 photometric=PHOTOMETRIC_PALETTE;
2798 depth=1;
2799 while ((GetQuantumRange(depth)+1) < image->colors)
2800 depth<<=1;
2801 status=SetQuantumDepth(image,quantum_info,depth);
2802 if (status == MagickFalse)
2803 ThrowWriterException(ResourceLimitError,
2804 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002805 }
2806 }
2807 }
cristyf2687ca2010-06-29 16:32:38 +00002808 switch (image->endian)
2809 {
2810 case LSBEndian:
2811 {
2812 endian=FILLORDER_LSB2MSB;
2813 break;
2814 }
2815 case MSBEndian:
2816 {
2817 endian=FILLORDER_MSB2LSB;
2818 break;
2819 }
2820 case UndefinedEndian:
2821 default:
2822 {
2823 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2824 break;
2825 }
2826 }
2827 lsb_first=1;
2828 image->endian=MSBEndian;
2829 if ((int) (*(char *) &lsb_first) != 0)
2830 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002831 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2832 (photometric != PHOTOMETRIC_MINISWHITE))
2833 {
2834 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002835 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002836 }
cristy0accf6e2009-10-01 13:14:28 +00002837 else
cristydaff8092010-04-22 14:50:53 +00002838 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2839 (photometric != PHOTOMETRIC_MINISWHITE))
2840 {
2841 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002842 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002843 }
cristyd15e6592011-10-15 00:13:06 +00002844 option=GetImageProperty(image,"tiff:fill-order",exception);
cristy62e282b2010-06-29 01:27:13 +00002845 if (option != (const char *) NULL)
2846 {
2847 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002848 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002849 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002850 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002851 }
cristyf2687ca2010-06-29 16:32:38 +00002852 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2853 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002854 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2855 if (image->matte != MagickFalse)
2856 {
2857 uint16
2858 extra_samples,
2859 sample_info[1],
2860 samples_per_pixel;
2861
2862 /*
2863 TIFF has a matte channel.
2864 */
2865 extra_samples=1;
2866 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
cristyd15e6592011-10-15 00:13:06 +00002867 option=GetImageProperty(image,"tiff:alpha",exception);
cristy3ed852e2009-09-05 21:47:34 +00002868 if ((option != (const char *) NULL) &&
2869 (LocaleCompare(option,"associated") == 0))
2870 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2872 &samples_per_pixel);
2873 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2874 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2875 &sample_info);
2876 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2877 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2878 }
2879 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2880 switch (quantum_info->format)
2881 {
2882 case FloatingPointQuantumFormat:
2883 {
2884 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2885 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2886 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2887 break;
2888 }
2889 case SignedQuantumFormat:
2890 {
2891 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2892 break;
2893 }
2894 case UnsignedQuantumFormat:
2895 {
2896 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2897 break;
2898 }
2899 default:
2900 break;
2901 }
cristy3ed852e2009-09-05 21:47:34 +00002902 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2903 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2904 if (photometric == PHOTOMETRIC_RGB)
2905 if ((image_info->interlace == PlaneInterlace) ||
2906 (image_info->interlace == PartitionInterlace))
2907 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2908 rows_per_strip=1;
2909 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002910 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2911 1);
cristy3ed852e2009-09-05 21:47:34 +00002912 option=GetImageOption(image_info,"tiff:rows-per-strip");
2913 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002914 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002915 switch (compress_tag)
2916 {
2917 case COMPRESSION_JPEG:
2918 {
2919#if defined(JPEG_SUPPORT)
2920 const char
2921 *sampling_factor;
2922
2923 GeometryInfo
2924 geometry_info;
2925
2926 MagickStatusType
2927 flags;
2928
2929 rows_per_strip+=(16-(rows_per_strip % 16));
2930 if (image->quality != 0)
2931 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2932 if (image_info->quality != UndefinedCompressionQuality)
2933 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2934 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
cristy510d06a2011-07-06 23:43:54 +00002935 if (IsRGBColorspace(image->colorspace) == MagickTrue)
cristy3ed852e2009-09-05 21:47:34 +00002936 {
cristy949bf5b2010-05-08 02:47:03 +00002937 const char
2938 *value;
2939
cristy3ed852e2009-09-05 21:47:34 +00002940 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2941 sampling_factor=(const char *) NULL;
cristyd15e6592011-10-15 00:13:06 +00002942 value=GetImageProperty(image,"jpeg:sampling-factor",exception);
cristy3ed852e2009-09-05 21:47:34 +00002943 if (value != (char *) NULL)
2944 {
2945 sampling_factor=value;
2946 if (image->debug != MagickFalse)
2947 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2948 " Input sampling-factors=%s",sampling_factor);
2949 }
2950 if (image_info->sampling_factor != (char *) NULL)
2951 sampling_factor=image_info->sampling_factor;
2952 if (sampling_factor != (const char *) NULL)
2953 {
2954 flags=ParseGeometry(sampling_factor,&geometry_info);
2955 if ((flags & SigmaValue) == 0)
2956 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002957 if (image->colorspace == YCbCrColorspace)
2958 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2959 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00002960 }
2961 }
2962 if (bits_per_sample == 12)
2963 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2964#endif
2965 break;
2966 }
2967 case COMPRESSION_ADOBE_DEFLATE:
2968 {
cristyf6fe0a12010-05-30 00:44:47 +00002969 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002970 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2971 &bits_per_sample);
2972 if (((photometric == PHOTOMETRIC_RGB) ||
2973 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2974 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002975 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00002976 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2977 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00002978 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00002979 break;
2980 }
2981 case COMPRESSION_CCITTFAX3:
2982 {
2983 /*
2984 Byte-aligned EOL.
2985 */
cristyeaedf062010-05-29 22:36:02 +00002986 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002987 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2988 break;
2989 }
2990 case COMPRESSION_CCITTFAX4:
2991 {
cristyeaedf062010-05-29 22:36:02 +00002992 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002993 break;
2994 }
cristyef8f26b2010-12-19 20:29:16 +00002995#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2996 case COMPRESSION_LZMA:
2997 {
2998 if (((photometric == PHOTOMETRIC_RGB) ||
2999 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3000 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3001 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3002 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
3003 image_info->quality == UndefinedCompressionQuality ? 7 :
3004 MagickMin((ssize_t) image_info->quality/10,9)));
3005 break;
3006 }
3007#endif
cristy3ed852e2009-09-05 21:47:34 +00003008 case COMPRESSION_LZW:
3009 {
3010 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3011 &bits_per_sample);
3012 if (((photometric == PHOTOMETRIC_RGB) ||
3013 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3014 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00003015 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00003016 break;
3017 }
3018 default:
3019 break;
3020 }
cristy79e5dad2010-09-16 19:48:33 +00003021 option=GetImageOption(image_info,"tiff:tile-geometry");
3022 if (option == (const char *) NULL)
3023 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy2a11bef2011-10-28 18:33:11 +00003024 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
cristy3ed852e2009-09-05 21:47:34 +00003025 {
3026 unsigned short
3027 units;
3028
3029 /*
3030 Set image resolution.
3031 */
3032 units=RESUNIT_NONE;
3033 if (image->units == PixelsPerInchResolution)
3034 units=RESUNIT_INCH;
3035 if (image->units == PixelsPerCentimeterResolution)
3036 units=RESUNIT_CENTIMETER;
3037 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
cristy2a11bef2011-10-28 18:33:11 +00003038 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x);
3039 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00003040 if ((image->page.x != 0) || (image->page.y != 0))
3041 {
3042 /*
3043 Set image position.
3044 */
3045 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
cristy2a11bef2011-10-28 18:33:11 +00003046 image->resolution.x);
cristy3ed852e2009-09-05 21:47:34 +00003047 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
cristy2a11bef2011-10-28 18:33:11 +00003048 image->resolution.y);
cristy3ed852e2009-09-05 21:47:34 +00003049 }
3050 }
3051 if (image->chromaticity.white_point.x != 0.0)
3052 {
3053 float
3054 chromaticity[6];
3055
3056 /*
3057 Set image chromaticity.
3058 */
3059 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3060 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3061 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3062 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3063 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3064 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3065 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3066 chromaticity[0]=(float) image->chromaticity.white_point.x;
3067 chromaticity[1]=(float) image->chromaticity.white_point.y;
3068 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3069 }
3070 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3071 {
3072 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3073 if (image->scene != 0)
3074 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3075 GetImageListLength(image));
3076 }
3077 if (image->orientation != UndefinedOrientation)
3078 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3079 (void) TIFFSetProfiles(tiff,image);
3080 {
3081 uint16
3082 page,
3083 pages;
3084
3085 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00003086 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00003087 if ((image_info->adjoin != MagickFalse) && (pages > 1))
3088 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3089 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3090 }
cristyd15e6592011-10-15 00:13:06 +00003091 (void) TIFFSetProperties(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003092 if (0)
cristyd15e6592011-10-15 00:13:06 +00003093 (void) TIFFSetEXIFProperties(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003094 /*
3095 Write image scanlines.
3096 */
3097 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3098 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3099 pixels=GetQuantumPixels(quantum_info);
3100 tiff_info.scanline=GetQuantumPixels(quantum_info);
3101 switch (photometric)
3102 {
3103 case PHOTOMETRIC_CIELAB:
3104 case PHOTOMETRIC_YCBCR:
3105 case PHOTOMETRIC_RGB:
3106 {
3107 /*
3108 RGB TIFF image.
3109 */
3110 switch (image_info->interlace)
3111 {
3112 case NoInterlace:
3113 default:
3114 {
3115 quantum_type=RGBQuantum;
3116 if (image->matte != MagickFalse)
3117 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003118 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003119 {
cristy4c08aed2011-07-01 19:47:50 +00003120 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003121 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003122
cristy3a37efd2011-08-28 20:31:03 +00003123 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003124 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003125 break;
cristy4c08aed2011-07-01 19:47:50 +00003126 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003127 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00003128 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00003129 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3130 break;
3131 if (image->previous == (Image *) NULL)
3132 {
cristy2837bcc2010-08-07 23:57:39 +00003133 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3134 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003135 if (status == MagickFalse)
3136 break;
3137 }
3138 }
3139 break;
3140 }
3141 case PlaneInterlace:
3142 case PartitionInterlace:
3143 {
3144 /*
3145 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3146 */
cristybb503372010-05-27 20:51:26 +00003147 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003148 {
cristy4c08aed2011-07-01 19:47:50 +00003149 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003150 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003151
cristy3a37efd2011-08-28 20:31:03 +00003152 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003153 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003154 break;
cristy4c08aed2011-07-01 19:47:50 +00003155 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003156 RedQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003157 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3158 break;
3159 }
3160 if (image->previous == (Image *) NULL)
3161 {
3162 status=SetImageProgress(image,SaveImageTag,100,400);
3163 if (status == MagickFalse)
3164 break;
3165 }
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 GreenQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003176 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3177 break;
3178 }
3179 if (image->previous == (Image *) NULL)
3180 {
3181 status=SetImageProgress(image,SaveImageTag,200,400);
3182 if (status == MagickFalse)
3183 break;
3184 }
cristybb503372010-05-27 20:51:26 +00003185 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003186 {
cristy4c08aed2011-07-01 19:47:50 +00003187 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003188 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003189
cristy3a37efd2011-08-28 20:31:03 +00003190 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003191 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003192 break;
cristy4c08aed2011-07-01 19:47:50 +00003193 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003194 BlueQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003195 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3196 break;
3197 }
3198 if (image->previous == (Image *) NULL)
3199 {
3200 status=SetImageProgress(image,SaveImageTag,300,400);
3201 if (status == MagickFalse)
3202 break;
3203 }
3204 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003205 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003206 {
cristy4c08aed2011-07-01 19:47:50 +00003207 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003208 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003209
cristy3a37efd2011-08-28 20:31:03 +00003210 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003211 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003212 break;
cristy4c08aed2011-07-01 19:47:50 +00003213 length=ExportQuantumPixels(image,(CacheView *) NULL,
cristy3a37efd2011-08-28 20:31:03 +00003214 quantum_info,AlphaQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003215 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3216 break;
3217 }
3218 if (image->previous == (Image *) NULL)
3219 {
3220 status=SetImageProgress(image,SaveImageTag,400,400);
3221 if (status == MagickFalse)
3222 break;
3223 }
3224 break;
3225 }
3226 }
3227 break;
3228 }
3229 case PHOTOMETRIC_SEPARATED:
3230 {
3231 /*
3232 CMYK TIFF image.
3233 */
3234 quantum_type=CMYKQuantum;
3235 if (image->matte != MagickFalse)
3236 quantum_type=CMYKAQuantum;
3237 if (image->colorspace != CMYKColorspace)
cristye941a752011-10-15 01:52:48 +00003238 (void) TransformImageColorspace(image,CMYKColorspace,exception);
cristybb503372010-05-27 20:51:26 +00003239 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003240 {
cristy4c08aed2011-07-01 19:47:50 +00003241 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003242 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003243
cristy3a37efd2011-08-28 20:31:03 +00003244 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003245 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003246 break;
cristy4c08aed2011-07-01 19:47:50 +00003247 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003248 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003249 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3250 break;
3251 if (image->previous == (Image *) NULL)
3252 {
cristycee97112010-05-28 00:44:52 +00003253 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3254 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003255 if (status == MagickFalse)
3256 break;
3257 }
3258 }
3259 break;
3260 }
3261 case PHOTOMETRIC_PALETTE:
3262 {
3263 uint16
3264 *blue,
3265 *green,
3266 *red;
3267
3268 /*
3269 Colormapped TIFF image.
3270 */
3271 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3272 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3273 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3274 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3275 (blue == (uint16 *) NULL))
3276 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3277 /*
3278 Initialize TIFF colormap.
3279 */
3280 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3281 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3282 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003283 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003284 {
3285 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3286 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3287 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3288 }
3289 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3290 red=(uint16 *) RelinquishMagickMemory(red);
3291 green=(uint16 *) RelinquishMagickMemory(green);
3292 blue=(uint16 *) RelinquishMagickMemory(blue);
3293 }
3294 default:
3295 {
3296 /*
3297 Convert PseudoClass packets to contiguous grayscale scanlines.
3298 */
3299 quantum_type=IndexQuantum;
3300 if (image->matte != MagickFalse)
3301 {
3302 if (photometric != PHOTOMETRIC_PALETTE)
3303 quantum_type=GrayAlphaQuantum;
3304 else
3305 quantum_type=IndexAlphaQuantum;
3306 }
3307 else
3308 if (photometric != PHOTOMETRIC_PALETTE)
3309 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003310 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003311 {
cristy4c08aed2011-07-01 19:47:50 +00003312 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003313 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003314
cristy3a37efd2011-08-28 20:31:03 +00003315 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003316 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003317 break;
cristy4c08aed2011-07-01 19:47:50 +00003318 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003319 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003320 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3321 break;
3322 if (image->previous == (Image *) NULL)
3323 {
cristycee97112010-05-28 00:44:52 +00003324 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3325 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003326 if (status == MagickFalse)
3327 break;
3328 }
3329 }
3330 break;
3331 }
3332 }
3333 quantum_info=DestroyQuantumInfo(quantum_info);
3334 DestroyTIFFInfo(&tiff_info);
3335 if (0 && (image_info->verbose == MagickTrue))
3336 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3337 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003338 image->endian=MSBEndian;
3339 if (endian == FILLORDER_LSB2MSB)
3340 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003341 image=SyncNextImageInList(image);
3342 if (image == (Image *) NULL)
3343 break;
3344 status=SetImageProgress(image,SaveImagesTag,scene++,
3345 GetImageListLength(image));
3346 if (status == MagickFalse)
3347 break;
3348 } while (image_info->adjoin != MagickFalse);
3349 (void) TIFFSetWarningHandler(warning_handler);
3350 (void) TIFFSetErrorHandler(error_handler);
3351 TIFFClose(tiff);
3352 return(MagickTrue);
3353}
3354#endif