blob: b60b09b7a27b8e264f9b874ca320637261c8fbd4 [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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 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*/
42#include "magick/studio.h"
cristy5a2ca482009-10-14 18:24:56 +000043#include "magick/attribute.h"
cristy3ed852e2009-09-05 21:47:34 +000044#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/cache.h"
47#include "magick/color.h"
48#include "magick/color-private.h"
cristyf4cf3072010-04-25 20:07:02 +000049#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000050#include "magick/colorspace.h"
51#include "magick/constitute.h"
52#include "magick/enhance.h"
53#include "magick/exception.h"
54#include "magick/exception-private.h"
55#include "magick/geometry.h"
56#include "magick/image.h"
57#include "magick/image-private.h"
58#include "magick/list.h"
59#include "magick/log.h"
60#include "magick/magick.h"
61#include "magick/memory_.h"
62#include "magick/module.h"
63#include "magick/monitor.h"
64#include "magick/monitor-private.h"
65#include "magick/option.h"
66#include "magick/property.h"
67#include "magick/quantum.h"
68#include "magick/quantum-private.h"
69#include "magick/profile.h"
70#include "magick/resize.h"
cristy3d7f8062009-09-24 20:45:35 +000071#include "magick/resource_.h"
cristy3ed852e2009-09-05 21:47:34 +000072#include "magick/semaphore.h"
73#include "magick/splay-tree.h"
74#include "magick/static.h"
75#include "magick/statistic.h"
76#include "magick/string_.h"
cristyf2f27272009-12-17 14:48:46 +000077#include "magick/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000078#include "magick/thread_.h"
79#include "magick/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000080#if defined(MAGICKCORE_TIFF_DELEGATE)
81# if defined(MAGICKCORE_HAVE_TIFFCONF_H)
82# include "tiffconf.h"
83#endif
84# include "tiff.h"
85# include "tiffio.h"
86# if !defined(COMPRESSION_ADOBE_DEFLATE)
87# define COMPRESSION_ADOBE_DEFLATE 8
88# endif
89
90/*
91 Typedef declarations.
92*/
93#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
94typedef struct _ExifInfo
95{
96 unsigned int
97 tag,
98 type;
99
100 const char
101 *property;
102} ExifInfo;
103
104static const ExifInfo
105 exif_info[] = {
106 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
107 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
108 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
109 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000110 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy3ed852e2009-09-05 21:47:34 +0000111 { EXIFTAG_OECF, TIFF_UNDEFINED, "exif:OptoelectricConversionFactor" },
112 { EXIFTAG_EXIFVERSION, TIFF_UNDEFINED, "exif:ExifVersion" },
113 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
114 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
115 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_UNDEFINED, "exif:ComponentsConfiguration" },
116 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
117 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
118 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
119 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
120 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
121 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
122 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
123 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
124 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
125 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
126 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
127 /* { EXIFTAG_SUBJECTAREA, TIFF_SHORT, "exif:SubjectArea" }, */
128 { EXIFTAG_MAKERNOTE, TIFF_UNDEFINED, "exif:MakerNote" },
129 { EXIFTAG_USERCOMMENT, TIFF_UNDEFINED, "exif:UserComment" },
130 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
131 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
132 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
133 { EXIFTAG_FLASHPIXVERSION, TIFF_UNDEFINED, "exif:FlashpixVersion" },
134 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
135 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
136 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
137 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
138 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
139 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
140 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_UNDEFINED, "exif:SpatialFrequencyResponse" },
141 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
142 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
143 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
144 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
145 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
146 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
147 { EXIFTAG_FILESOURCE, TIFF_UNDEFINED, "exif:FileSource" },
148 { EXIFTAG_SCENETYPE, TIFF_UNDEFINED, "exif:SceneType" },
149 { EXIFTAG_CFAPATTERN, TIFF_UNDEFINED, "exif:CFAPattern" },
150 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
151 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
152 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
153 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
154 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
155 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
156 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
157 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
158 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
159 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
160 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_UNDEFINED, "exif:DeviceSettingDescription" },
161 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
162 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
163 { 0, 0, (char *) NULL }
164};
165#endif
166
167/*
168 Global declarations.
169*/
170static MagickThreadKey
171 tiff_exception;
172
173static SemaphoreInfo
174 *tiff_semaphore = (SemaphoreInfo *) NULL;
175
176static volatile MagickBooleanType
177 instantiate_key = MagickFalse;
178#endif
179
180/*
181 Forward declarations.
182*/
183#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000184static Image *
185 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
186
cristy3ed852e2009-09-05 21:47:34 +0000187static MagickBooleanType
cristy3d7f8062009-09-24 20:45:35 +0000188 WriteGROUP4Image(const ImageInfo *,Image *),
cristy3ed852e2009-09-05 21:47:34 +0000189 WritePTIFImage(const ImageInfo *,Image *),
190 WriteTIFFImage(const ImageInfo *,Image *);
191#endif
192
193/*
194%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195% %
196% %
197% %
198% I s T I F F %
199% %
200% %
201% %
202%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203%
204% IsTIFF() returns MagickTrue if the image format type, identified by the
205% magick string, is TIFF.
206%
207% The format of the IsTIFF method is:
208%
209% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
210%
211% A description of each parameter follows:
212%
213% o magick: compare image format pattern against these bytes.
214%
215% o length: Specifies the length of the magick string.
216%
217*/
218static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
219{
220 if (length < 4)
221 return(MagickFalse);
222 if (memcmp(magick,"\115\115\000\052",4) == 0)
223 return(MagickTrue);
224 if (memcmp(magick,"\111\111\052\000",4) == 0)
225 return(MagickTrue);
226#if defined(TIFF_VERSION_BIG)
227 if (length < 8)
228 return(MagickFalse);
229 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
230 return(MagickTrue);
231 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
232 return(MagickTrue);
233#endif
234 return(MagickFalse);
235}
236
237#if defined(MAGICKCORE_TIFF_DELEGATE)
238/*
239%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240% %
241% %
242% %
cristy3d7f8062009-09-24 20:45:35 +0000243% R e a d G R O U P 4 I m a g e %
244% %
245% %
246% %
247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248%
249% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
250% allocates the memory necessary for the new Image structure and returns a
251% pointer to the new image.
252%
253% The format of the ReadGROUP4Image method is:
254%
255% Image *ReadGROUP4Image(const ImageInfo *image_info,
256% ExceptionInfo *exception)
257%
258% A description of each parameter follows:
259%
260% o image_info: the image info.
261%
262% o exception: return any errors or warnings in this structure.
263%
264*/
265
cristyeaedf062010-05-29 22:36:02 +0000266static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000267{
268 unsigned char
269 buffer[4];
270
271 buffer[0]=(unsigned char) value;
272 buffer[1]=(unsigned char) (value >> 8);
273 buffer[2]=(unsigned char) (value >> 16);
274 buffer[3]=(unsigned char) (value >> 24);
275 return(fwrite(buffer,1,4,file));
276}
277
278static Image *ReadGROUP4Image(const ImageInfo *image_info,
279 ExceptionInfo *exception)
280{
281 char
282 filename[MaxTextExtent];
283
284 FILE
285 *file;
286
287 Image
288 *image;
289
290 ImageInfo
291 *read_info;
292
293 int
294 c,
295 unique_file;
296
297 MagickBooleanType
298 status;
299
300 size_t
301 length;
302
303 ssize_t
304 offset,
305 strip_offset;
306
307 /*
308 Open image file.
309 */
310 assert(image_info != (const ImageInfo *) NULL);
311 assert(image_info->signature == MagickSignature);
312 if (image_info->debug != MagickFalse)
313 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
314 image_info->filename);
315 assert(exception != (ExceptionInfo *) NULL);
316 assert(exception->signature == MagickSignature);
317 image=AcquireImage(image_info);
318 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
319 if (status == MagickFalse)
320 {
321 image=DestroyImageList(image);
322 return((Image *) NULL);
323 }
324 /*
325 Write raw CCITT Group 4 wrapped as a TIFF image file.
326 */
327 file=(FILE *) NULL;
328 unique_file=AcquireUniqueFileResource(filename);
329 if (unique_file != -1)
330 file=fdopen(unique_file,"wb");
331 if ((unique_file == -1) || (file == (FILE *) NULL))
332 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
333 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
334 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
335 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
336 length=WriteLSBLong(file,image->columns);
337 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
338 length=WriteLSBLong(file,image->rows);
339 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
340 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
341 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
342 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
343 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000344 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000345 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000346 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000347 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
348 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000349 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000350 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
351 offset=(ssize_t) ftell(file)-4;
352 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000353 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000354 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000355 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000356 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
357 length=fwrite("\000\000\000\000",1,4,file);
358 length=WriteLSBLong(file,image->x_resolution);
359 length=WriteLSBLong(file,1);
360 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
361 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000362 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000363 length=WriteLSBLong(file,(unsigned int) length);
364 (void) fclose(file);
365 (void) CloseBlob(image);
366 image=DestroyImage(image);
367 /*
368 Read TIFF image.
369 */
cristy14efd992009-09-26 23:46:03 +0000370 read_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +0000371 (void) FormatMagickString(read_info->filename,MaxTextExtent,"%.1024s",
372 filename);
373 image=ReadTIFFImage(read_info,exception);
374 read_info=DestroyImageInfo(read_info);
375 if (image != (Image *) NULL)
376 {
377 (void) CopyMagickString(image->filename,image_info->filename,
378 MaxTextExtent);
379 (void) CopyMagickString(image->magick_filename,image_info->filename,
380 MaxTextExtent);
381 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
382 }
383 (void) RelinquishUniqueFileResource(filename);
384 return(image);
385}
386#endif
387
388#if defined(MAGICKCORE_TIFF_DELEGATE)
389/*
390%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391% %
392% %
393% %
cristy3ed852e2009-09-05 21:47:34 +0000394% R e a d T I F F I m a g e %
395% %
396% %
397% %
398%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399%
400% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
401% memory necessary for the new Image structure and returns a pointer to the
402% new image.
403%
404% The format of the ReadTIFFImage method is:
405%
406% Image *ReadTIFFImage(const ImageInfo *image_info,
407% ExceptionInfo *exception)
408%
409% A description of each parameter follows:
410%
411% o image_info: the image info.
412%
413% o exception: return any errors or warnings in this structure.
414%
415*/
416
417static inline size_t MagickMax(const size_t x,const size_t y)
418{
419 if (x > y)
420 return(x);
421 return(y);
422}
423
cristybb503372010-05-27 20:51:26 +0000424static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000425{
426 if (x < y)
427 return(x);
428 return(y);
429}
430
431static MagickBooleanType ReadProfile(Image *image,const char *name,
cristybb503372010-05-27 20:51:26 +0000432 unsigned char *datum,ssize_t length)
cristy3ed852e2009-09-05 21:47:34 +0000433{
434 MagickBooleanType
435 status;
436
cristybb503372010-05-27 20:51:26 +0000437 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000438 i;
439
440 StringInfo
441 *profile;
442
443 if (length < 4)
444 return(MagickFalse);
445 i=0;
446 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
447 {
448 for (i=0; i < (length-4); i+=2)
449 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
450 break;
451 if (i == length)
452 length-=i;
453 else
454 i=0;
455 if (length < 4)
456 return(MagickFalse);
457 }
458 profile=AcquireStringInfo((size_t) length);
459 SetStringInfoDatum(profile,datum+i);
460 status=SetImageProfile(image,name,profile);
461 profile=DestroyStringInfo(profile);
462 if (status == MagickFalse)
463 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
464 image->filename);
465 return(MagickTrue);
466}
467
468#if defined(__cplusplus) || defined(c_plusplus)
469extern "C" {
470#endif
471
472static int TIFFCloseBlob(thandle_t image)
473{
474 (void) CloseBlob((Image *) image);
475 return(0);
476}
477
478static void TIFFErrors(const char *module,const char *format,va_list error)
479{
480 char
481 message[MaxTextExtent];
482
483 ExceptionInfo
484 *exception;
485
486#if defined(MAGICKCORE_HAVE_VSNPRINTF)
487 (void) vsnprintf(message,MaxTextExtent,format,error);
488#else
489 (void) vsprintf(message,format,error);
490#endif
491 (void) ConcatenateMagickString(message,".",MaxTextExtent);
492 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
493 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000494 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
495 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000496}
497
498static void TIFFGetProfiles(TIFF *tiff,Image *image)
499{
500 uint32
501 length;
502
503 unsigned char
504 *profile;
505
506#if defined(TIFFTAG_ICCPROFILE)
507 length=0;
508 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000509 (void) ReadProfile(image,"icc",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000510#endif
511#if defined(TIFFTAG_PHOTOSHOP)
512 length=0;
513 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000514 (void) ReadProfile(image,"8bim",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000515#endif
516#if defined(TIFFTAG_RICHTIFFIPTC)
517 length=0;
518 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
519 {
520 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000521 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000522 (void) ReadProfile(image,"iptc",profile,4L*length);
523 }
524#endif
525#if defined(TIFFTAG_XMLPACKET)
526 length=0;
527 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000528 (void) ReadProfile(image,"xmp",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000529#endif
530 length=0;
531 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000532 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000533}
534
535static void TIFFGetProperties(TIFF *tiff,Image *image)
536{
537 char
538 *text;
539
540 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
541 (void) SetImageProperty(image,"tiff:artist",text);
542 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
543 (void) SetImageProperty(image,"tiff:timestamp",text);
544 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
545 (void) SetImageProperty(image,"tiff:software",text);
546 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
547 (void) SetImageProperty(image,"tiff:hostcomputer",text);
548 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
549 (void) SetImageProperty(image,"tiff:document",text);
550 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
551 (void) SetImageProperty(image,"tiff:make",text);
552 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
553 (void) SetImageProperty(image,"tiff:model",text);
554 if (TIFFGetField(tiff,33432,&text) == 1)
555 (void) SetImageProperty(image,"tiff:copyright",text);
556 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
557 (void) SetImageProperty(image,"label",text);
558 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
559 (void) SetImageProperty(image,"comment",text);
560}
561
562static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
563{
564#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
565 char
566 value[MaxTextExtent];
567
cristybb503372010-05-27 20:51:26 +0000568 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000569 i;
570
571 tdir_t
572 directory;
573
574 uint32
575 offset;
576
577 /*
578 Read EXIF properties.
579 */
580 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
581 return;
582 directory=TIFFCurrentDirectory(tiff);
583 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
584 return;
585 for (i=0; exif_info[i].tag != 0; i++)
586 {
587 *value='\0';
588 switch (exif_info[i].type)
589 {
590 case TIFF_ASCII:
591 {
592 char
593 *ascii;
594
595 if (TIFFGetField(tiff,exif_info[i].tag,&ascii) != 0)
596 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
597 break;
598 }
599 case TIFF_SHORT:
600 {
601 uint16
602 shorty;
603
604 if (TIFFGetField(tiff,exif_info[i].tag,&shorty) != 0)
605 (void) FormatMagickString(value,MaxTextExtent,"%d",shorty);
606 break;
607 }
608 case TIFF_LONG:
609 {
610 uint32
cristybb503372010-05-27 20:51:26 +0000611 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +0000612
cristybb503372010-05-27 20:51:26 +0000613 if (TIFFGetField(tiff,exif_info[i].tag,&ssize_ty) != 0)
614 (void) FormatMagickString(value,MaxTextExtent,"%d",ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +0000615 break;
616 }
617 case TIFF_RATIONAL:
618 case TIFF_SRATIONAL:
619 {
620 float
621 rational;
622
623 if (TIFFGetField(tiff,exif_info[i].tag,&rational) != 0)
cristye7f51092010-01-17 00:39:37 +0000624 (void) FormatMagickString(value,MaxTextExtent,"%g",rational);
cristy3ed852e2009-09-05 21:47:34 +0000625 break;
626 }
627 default:
628 break;
629 }
630 if (*value != '\0')
631 (void) SetImageProperty(image,exif_info[i].property,value);
632 }
633 TIFFSetDirectory(tiff,directory);
634#else
635 (void) tiff;
636 (void) image;
637#endif
638}
639
640static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
641{
642 *base=(tdata_t *) GetBlobStreamData((Image *) image);
643 if (*base != (tdata_t *) NULL)
644 *size=(toff_t) GetBlobSize((Image *) image);
645 if (*base != (tdata_t *) NULL)
646 return(1);
647 return(0);
648}
649
650static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
651{
652 tsize_t
653 count;
654
655 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
656 (unsigned char *) data);
657 return(count);
658}
659
cristybb503372010-05-27 20:51:26 +0000660static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
661 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000662{
663 int32
664 status;
665
666 (void) bits_per_sample;
667 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
668 return(status);
669}
670
671static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
672{
673 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
674}
675
676static toff_t TIFFGetBlobSize(thandle_t image)
677{
678 return((toff_t) GetBlobSize((Image *) image));
679}
680
681static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
682{
683 (void) image;
684 (void) base;
685 (void) size;
686}
687
688static void TIFFWarnings(const char *module,const char *format,va_list warning)
689{
690 char
691 message[MaxTextExtent];
692
693 ExceptionInfo
694 *exception;
695
696#if defined(MAGICKCORE_HAVE_VSNPRINTF)
697 (void) vsnprintf(message,MaxTextExtent,format,warning);
698#else
699 (void) vsprintf(message,format,warning);
700#endif
701 (void) ConcatenateMagickString(message,".",MaxTextExtent);
702 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
703 if (exception != (ExceptionInfo *) NULL)
704 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
705 message,"`%s'",module);
706}
707
708static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
709{
710 tsize_t
711 count;
712
713 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
714 (unsigned char *) data);
715 return(count);
716}
717
718#if defined(__cplusplus) || defined(c_plusplus)
719}
720#endif
721
722static Image *ReadTIFFImage(const ImageInfo *image_info,
723 ExceptionInfo *exception)
724{
725 typedef enum
726 {
727 ReadSingleSampleMethod,
728 ReadRGBAMethod,
729 ReadCMYKAMethod,
730 ReadStripMethod,
731 ReadTileMethod,
732 ReadGenericMethod
733 } TIFFMethodType;
734
735 const char
736 *option;
737
738 float
739 *chromaticity,
740 x_position,
741 y_position,
742 x_resolution,
743 y_resolution;
744
745 Image
746 *image;
747
cristybb503372010-05-27 20:51:26 +0000748 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000749 y;
750
751 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000752 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000753 debug,
754 status;
755
756 MagickSizeType
757 number_pixels;
758
759 QuantumInfo
760 *quantum_info;
761
762 QuantumType
763 quantum_type;
764
cristybb503372010-05-27 20:51:26 +0000765 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000766 i;
767
768 size_t
769 length,
770 pad;
771
772 TIFF
773 *tiff;
774
775 TIFFErrorHandler
776 error_handler,
777 warning_handler;
778
779 TIFFMethodType
780 method;
781
782 uint16
783 compress_tag,
784 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000785 endian,
cristy3ed852e2009-09-05 21:47:34 +0000786 extra_samples,
787 interlace,
788 max_sample_value,
789 min_sample_value,
790 orientation,
791 pages,
792 photometric,
793 *sample_info,
794 sample_format,
795 samples_per_pixel,
796 units,
797 value;
798
799 uint32
800 height,
801 rows_per_strip,
802 width;
803
804 unsigned char
805 *pixels;
806
cristyf2687ca2010-06-29 16:32:38 +0000807 size_t
808 lsb_first;
809
cristy3ed852e2009-09-05 21:47:34 +0000810 /*
811 Open image.
812 */
813 assert(image_info != (const ImageInfo *) NULL);
814 assert(image_info->signature == MagickSignature);
815 if (image_info->debug != MagickFalse)
816 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
817 image_info->filename);
818 assert(exception != (ExceptionInfo *) NULL);
819 assert(exception->signature == MagickSignature);
820 image=AcquireImage(image_info);
821 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
822 if (status == MagickFalse)
823 {
824 image=DestroyImageList(image);
825 return((Image *) NULL);
826 }
827 (void) MagickSetThreadValue(tiff_exception,exception);
828 error_handler=TIFFSetErrorHandler(TIFFErrors);
829 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
830 tiff=TIFFClientOpen(image->filename,"r",(thandle_t) image,TIFFReadBlob,
831 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
832 TIFFUnmapBlob);
833 if (tiff == (TIFF *) NULL)
834 {
835 (void) TIFFSetWarningHandler(warning_handler);
836 (void) TIFFSetErrorHandler(error_handler);
837 image=DestroyImageList(image);
838 return((Image *) NULL);
839 }
840 debug=IsEventLogging();
841 if (image_info->number_scenes != 0)
842 {
843 /*
844 Generate blank images for subimage specification (e.g. image.tif[4].
845 */
cristybb503372010-05-27 20:51:26 +0000846 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000847 {
848 (void) TIFFReadDirectory(tiff);
849 AcquireNextImage(image_info,image);
850 if (GetNextImageInList(image) == (Image *) NULL)
851 {
852 image=DestroyImageList(image);
853 return((Image *) NULL);
854 }
855 image=SyncNextImageInList(image);
856 }
857 }
858 do
859 {
860 if (0 && (image_info->verbose != MagickFalse))
861 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristy6b032822010-06-29 16:52:32 +0000862 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
863 "lsb" : "msb");
cristy3ed852e2009-09-05 21:47:34 +0000864 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
865 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
866 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
867 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
872 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
873 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
874 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
875 switch (photometric)
876 {
877 case PHOTOMETRIC_MINISBLACK:
878 {
879 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
880 break;
881 }
882 case PHOTOMETRIC_MINISWHITE:
883 {
884 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
885 break;
886 }
887 case PHOTOMETRIC_PALETTE:
888 {
889 (void) SetImageProperty(image,"tiff:photometric","palette");
890 break;
891 }
892 case PHOTOMETRIC_RGB:
893 {
894 (void) SetImageProperty(image,"tiff:photometric","RGB");
895 break;
896 }
897 case PHOTOMETRIC_CIELAB:
898 {
899 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
900 break;
901 }
902 case PHOTOMETRIC_SEPARATED:
903 {
904 (void) SetImageProperty(image,"tiff:photometric","separated");
905 break;
906 }
907 default:
908 {
909 (void) SetImageProperty(image,"tiff:photometric","unknown");
910 break;
911 }
912 }
913 if (image->debug != MagickFalse)
914 {
915 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
916 (unsigned int) width,(unsigned int) height);
917 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
918 interlace);
919 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
920 "Bits per sample: %u",bits_per_sample);
921 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
922 "Min sample value: %u",min_sample_value);
923 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
924 "Max sample value: %u",max_sample_value);
925 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
926 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
927 }
cristybb503372010-05-27 20:51:26 +0000928 image->columns=(size_t) width;
929 image->rows=(size_t) height;
930 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +0000931 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +0000932 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
933 (double) image->depth);
cristyf2687ca2010-06-29 16:32:38 +0000934 lsb_first=1;
935 image->endian=MSBEndian;
936 if ((int) (*(char *) &lsb_first) != 0)
937 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +0000938 if (photometric == PHOTOMETRIC_SEPARATED)
939 image->colorspace=CMYKColorspace;
940 if (photometric == PHOTOMETRIC_CIELAB)
941 image->colorspace=LabColorspace;
942 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
943 &samples_per_pixel);
944 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
945 x_resolution=(float) image->x_resolution;
946 y_resolution=(float) image->y_resolution;
947 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
948 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
949 image->x_resolution=x_resolution;
950 image->y_resolution=y_resolution;
951 x_position=(float) image->page.x/x_resolution;
952 y_position=(float) image->page.y/y_resolution;
953 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
954 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +0000955 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
956 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000957 image->orientation=(OrientationType) orientation;
958 chromaticity=(float *) NULL;
959 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
960 if (chromaticity != (float *) NULL)
961 {
962 image->chromaticity.white_point.x=chromaticity[0];
963 image->chromaticity.white_point.y=chromaticity[1];
964 }
965 chromaticity=(float *) NULL;
966 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
967 if (chromaticity != (float *) NULL)
968 {
969 image->chromaticity.red_primary.x=chromaticity[0];
970 image->chromaticity.red_primary.y=chromaticity[1];
971 image->chromaticity.green_primary.x=chromaticity[2];
972 image->chromaticity.green_primary.y=chromaticity[3];
973 image->chromaticity.blue_primary.x=chromaticity[4];
974 image->chromaticity.blue_primary.y=chromaticity[5];
975 }
976 TIFFGetProperties(tiff,image);
977 option=GetImageOption(image_info,"tiff:exif");
978 if ((option != (const char *) NULL) &&
979 (IsMagickTrue(option) != MagickFalse))
980 TIFFGetEXIFProperties(tiff,image);
981 TIFFGetProfiles(tiff,image);
982 /*
983 Allocate memory for the image and pixel buffer.
984 */
985#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
986 if ((compress_tag != COMPRESSION_NONE) &&
987 (TIFFIsCODECConfigured(compress_tag) == 0))
988 {
989 TIFFClose(tiff);
990 ThrowReaderException(CoderError,"CompressNotSupported");
991 }
992#endif
993 switch (compress_tag)
994 {
995 case COMPRESSION_NONE: image->compression=NoCompression; break;
996 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
997 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
998 case COMPRESSION_JPEG:
999 {
1000 image->compression=JPEGCompression;
1001#if defined(JPEG_SUPPORT)
1002 {
1003 char
1004 sampling_factor[MaxTextExtent];
1005
1006 uint16
1007 horizontal,
1008 vertical;
1009
1010 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1011 &horizontal,&vertical);
1012 (void) FormatMagickString(sampling_factor,MaxTextExtent,"%dx%d",
1013 horizontal,vertical);
1014 (void) SetImageProperty(image,"jpeg:sampling-factor",
1015 sampling_factor);
1016 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1017 "Sampling Factors: %s",sampling_factor);
1018 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1019 {
1020 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1021 JPEGCOLORMODE_RGB);
1022 photometric=PHOTOMETRIC_RGB;
1023 }
1024 }
1025#endif
1026 break;
1027 }
1028 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1029 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1030 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1031 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1032 default: image->compression=RLECompression; break;
1033 }
1034 quantum_info=AcquireQuantumInfo(image_info,image);
1035 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001036 {
1037 TIFFClose(tiff);
1038 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1039 }
cristy3ed852e2009-09-05 21:47:34 +00001040 if (sample_format == SAMPLEFORMAT_UINT)
1041 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1042 if (sample_format == SAMPLEFORMAT_INT)
1043 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1044 if (sample_format == SAMPLEFORMAT_IEEEFP)
1045 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1046 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001047 {
1048 TIFFClose(tiff);
1049 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1050 }
cristy3ed852e2009-09-05 21:47:34 +00001051 status=MagickTrue;
1052 switch (photometric)
1053 {
1054 case PHOTOMETRIC_MINISBLACK:
1055 {
1056 quantum_info->min_is_white=MagickFalse;
1057 break;
1058 }
1059 case PHOTOMETRIC_MINISWHITE:
1060 {
1061 quantum_info->min_is_white=MagickTrue;
1062 break;
1063 }
1064 default:
1065 break;
1066 }
cristy891dc792010-03-04 01:47:16 +00001067 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001068 extra_samples=0;
1069 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1070 &sample_info);
1071 if (extra_samples == 0)
1072 {
1073 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1074 image->matte=MagickTrue;
1075 }
1076 else
cristy6c207342010-02-19 17:49:50 +00001077 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001078 {
cristy7cf69862010-03-03 13:57:24 +00001079 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001080 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001081 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001082 }
cristy891dc792010-03-04 01:47:16 +00001083 option=GetImageOption(image_info,"tiff:alpha");
1084 if (option != (const char *) NULL)
1085 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1086 MagickFalse;
1087 if (image->matte != MagickFalse)
1088 (void) SetImageProperty(image,"tiff:alpha",
1089 associated_alpha != MagickFalse ? "associated" : "unassociated");
cristy3ed852e2009-09-05 21:47:34 +00001090 if ((photometric == PHOTOMETRIC_PALETTE) &&
1091 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1092 {
cristybb503372010-05-27 20:51:26 +00001093 size_t
cristy3ed852e2009-09-05 21:47:34 +00001094 colors;
1095
cristybb503372010-05-27 20:51:26 +00001096 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy3ed852e2009-09-05 21:47:34 +00001097 if (AcquireImageColormap(image,colors) == MagickFalse)
1098 {
1099 TIFFClose(tiff);
1100 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1101 }
1102 }
1103 if (units == RESUNIT_INCH)
1104 image->units=PixelsPerInchResolution;
1105 if (units == RESUNIT_CENTIMETER)
1106 image->units=PixelsPerCentimeterResolution;
1107 value=(unsigned short) image->scene;
1108 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1109 image->scene=value;
1110 if (image_info->ping != MagickFalse)
1111 {
1112 if (image_info->number_scenes != 0)
1113 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1114 break;
1115 goto next_tiff_frame;
1116 }
1117 method=ReadGenericMethod;
1118 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1119 {
1120 char
1121 value[MaxTextExtent];
1122
1123 method=ReadStripMethod;
1124 (void) FormatMagickString(value,MaxTextExtent,"%u",
1125 (unsigned int) rows_per_strip);
1126 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1127 }
1128 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1129 method=ReadRGBAMethod;
1130 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1131 method=ReadCMYKAMethod;
1132 if ((photometric != PHOTOMETRIC_RGB) &&
1133 (photometric != PHOTOMETRIC_CIELAB) &&
1134 (photometric != PHOTOMETRIC_SEPARATED))
1135 method=ReadGenericMethod;
1136 if (image->storage_class == PseudoClass)
1137 method=ReadSingleSampleMethod;
1138 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1139 (photometric == PHOTOMETRIC_MINISWHITE))
1140 method=ReadSingleSampleMethod;
cristy8968f402010-08-14 01:17:10 +00001141 if ((photometric == PHOTOMETRIC_RGB) &&
1142 (bits_per_sample <= 8))
1143 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001144 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1145 (interlace == PLANARCONFIG_SEPARATE))
1146 method=ReadGenericMethod;
1147 if (TIFFIsTiled(tiff) != MagickFalse)
1148 method=ReadTileMethod;
1149 quantum_type=RGBQuantum;
1150 pixels=GetQuantumPixels(quantum_info);
1151 switch (method)
1152 {
1153 case ReadSingleSampleMethod:
1154 {
1155 /*
1156 Convert TIFF image to PseudoClass MIFF image.
1157 */
1158 if ((image->storage_class == PseudoClass) &&
1159 (photometric == PHOTOMETRIC_PALETTE))
1160 {
cristybb503372010-05-27 20:51:26 +00001161 size_t
cristy3ed852e2009-09-05 21:47:34 +00001162 range;
1163
1164 uint16
1165 *blue_colormap,
1166 *green_colormap,
1167 *red_colormap;
1168
1169 /*
1170 Initialize colormap.
1171 */
1172 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1173 &green_colormap,&blue_colormap);
1174 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001175 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001176 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1177 (blue_colormap[i] >= 256))
1178 {
1179 range=65535;
1180 break;
1181 }
cristybb503372010-05-27 20:51:26 +00001182 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001183 {
cristyce70c172010-01-07 17:15:30 +00001184 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001185 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001186 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001187 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001188 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001189 blue_colormap[i])/range);
1190 }
1191 }
1192 quantum_type=IndexQuantum;
1193 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1194 if (image->matte != MagickFalse)
1195 {
1196 if (image->storage_class != PseudoClass)
1197 {
1198 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1199 GrayAlphaQuantum;
1200 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1201 }
1202 else
1203 {
1204 quantum_type=IndexAlphaQuantum;
1205 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1206 }
1207 }
1208 else
1209 if (image->storage_class != PseudoClass)
1210 {
1211 quantum_type=GrayQuantum;
1212 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1213 }
1214 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1215 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001216 {
1217 TIFFClose(tiff);
1218 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1219 }
cristy3ed852e2009-09-05 21:47:34 +00001220 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001221 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001222 {
1223 int
1224 status;
1225
1226 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001227 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001228
1229 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1230 if (status == -1)
1231 break;
1232 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1233 if (q == (PixelPacket *) NULL)
1234 break;
1235 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1236 quantum_type,pixels,exception);
1237 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1238 break;
1239 if (image->previous == (Image *) NULL)
1240 {
cristycee97112010-05-28 00:44:52 +00001241 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1242 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001243 if (status == MagickFalse)
1244 break;
1245 }
1246 }
1247 break;
1248 }
1249 case ReadRGBAMethod:
1250 {
1251 /*
1252 Convert TIFF image to DirectClass MIFF image.
1253 */
1254 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1255 quantum_type=RGBQuantum;
1256 if (image->matte != MagickFalse)
1257 {
1258 quantum_type=RGBAQuantum;
1259 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1260 }
1261 if (image->colorspace == CMYKColorspace)
1262 {
1263 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1264 quantum_type=CMYKQuantum;
1265 if (image->matte != MagickFalse)
1266 {
1267 quantum_type=CMYKAQuantum;
1268 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1269 }
1270 }
1271 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1272 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001273 {
1274 TIFFClose(tiff);
1275 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1276 }
cristy3ed852e2009-09-05 21:47:34 +00001277 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001278 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001279 {
1280 int
1281 status;
1282
1283 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001284 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001285
1286 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1287 if (status == -1)
1288 break;
1289 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1290 if (q == (PixelPacket *) NULL)
1291 break;
cristy0b334892010-03-15 02:26:46 +00001292 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1293 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001294 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1295 break;
1296 if (image->previous == (Image *) NULL)
1297 {
cristycee97112010-05-28 00:44:52 +00001298 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1299 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001300 if (status == MagickFalse)
1301 break;
1302 }
1303 }
1304 break;
1305 }
1306 case ReadCMYKAMethod:
1307 {
1308 /*
1309 Convert TIFF image to DirectClass MIFF image.
1310 */
cristybb503372010-05-27 20:51:26 +00001311 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001312 {
cristybb503372010-05-27 20:51:26 +00001313 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001314 {
cristy6d9f12f2009-11-03 14:50:26 +00001315 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001316 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001317
cristy3ed852e2009-09-05 21:47:34 +00001318 int
1319 status;
1320
1321 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1322 pixels);
1323 if (status == -1)
1324 break;
1325 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1326 if (q == (PixelPacket *) NULL)
1327 break;
1328 if (image->colorspace != CMYKColorspace)
1329 switch (i)
1330 {
1331 case 0: quantum_type=RedQuantum; break;
1332 case 1: quantum_type=GreenQuantum; break;
1333 case 2: quantum_type=BlueQuantum; break;
1334 case 3: quantum_type=AlphaQuantum; break;
1335 default: quantum_type=UndefinedQuantum; break;
1336 }
cristy6d9f12f2009-11-03 14:50:26 +00001337 else
cristy3ed852e2009-09-05 21:47:34 +00001338 switch (i)
1339 {
1340 case 0: quantum_type=CyanQuantum; break;
1341 case 1: quantum_type=MagentaQuantum; break;
1342 case 2: quantum_type=YellowQuantum; break;
1343 case 3: quantum_type=BlackQuantum; break;
1344 case 4: quantum_type=AlphaQuantum; break;
1345 default: quantum_type=UndefinedQuantum; break;
1346 }
1347 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1348 quantum_type,pixels,exception);
1349 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1350 break;
1351 }
1352 if (image->previous == (Image *) NULL)
1353 {
cristycee97112010-05-28 00:44:52 +00001354 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1355 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001356 if (status == MagickFalse)
1357 break;
1358 }
1359 }
1360 break;
1361 }
1362 case ReadStripMethod:
1363 {
1364 register unsigned char
1365 *p;
1366
1367 /*
1368 Convert stripped TIFF image to DirectClass MIFF image.
1369 */
1370 i=0;
1371 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001372 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001373 {
cristybb503372010-05-27 20:51:26 +00001374 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001375 x;
1376
1377 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001378 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001379
1380 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1381 if (q == (PixelPacket *) NULL)
1382 break;
1383 if (i == 0)
1384 {
1385 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1386 break;
cristyeaedf062010-05-29 22:36:02 +00001387 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1388 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001389 }
1390 i--;
1391 p=pixels+image->columns*i;
cristybb503372010-05-27 20:51:26 +00001392 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001393 {
1394 q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
1395 q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
1396 q->blue=ScaleCharToQuantum((unsigned char) (TIFFGetB(*p)));
1397 if (image->matte != MagickFalse)
1398 q->opacity=ScaleCharToQuantum((unsigned char) (TIFFGetA(*p)));
1399 p++;
1400 q++;
1401 }
1402 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1403 break;
1404 if (image->previous == (Image *) NULL)
1405 {
cristycee97112010-05-28 00:44:52 +00001406 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1407 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001408 if (status == MagickFalse)
1409 break;
1410 }
1411 }
1412 break;
1413 }
1414 case ReadTileMethod:
1415 {
1416 register uint32
1417 *p;
1418
1419 uint32
1420 *tile_pixels,
1421 columns,
1422 rows;
1423
cristybb503372010-05-27 20:51:26 +00001424 size_t
cristy3ed852e2009-09-05 21:47:34 +00001425 number_pixels;
1426
1427 /*
1428 Convert tiled TIFF image to DirectClass MIFF image.
1429 */
1430 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1431 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1432 {
1433 TIFFClose(tiff);
1434 ThrowReaderException(CoderError,"ImageIsNotTiled");
1435 }
1436 (void) SetImageStorageClass(image,DirectClass);
1437 number_pixels=columns*rows;
1438 tile_pixels=(uint32 *) AcquireQuantumMemory((size_t) columns*rows,
1439 sizeof(*tile_pixels));
1440 if (tile_pixels == (uint32 *) NULL)
1441 {
1442 TIFFClose(tiff);
1443 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1444 }
cristybb503372010-05-27 20:51:26 +00001445 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001446 {
1447 PixelPacket
1448 *tile;
1449
cristybb503372010-05-27 20:51:26 +00001450 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001451 x;
1452
1453 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001454 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001455
cristybb503372010-05-27 20:51:26 +00001456 size_t
cristy3ed852e2009-09-05 21:47:34 +00001457 columns_remaining,
1458 rows_remaining;
1459
1460 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001461 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001462 rows_remaining=rows;
1463 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1464 exception);
1465 if (tile == (PixelPacket *) NULL)
1466 break;
cristybb503372010-05-27 20:51:26 +00001467 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001468 {
cristybb503372010-05-27 20:51:26 +00001469 size_t
cristy3ed852e2009-09-05 21:47:34 +00001470 column,
1471 row;
1472
1473 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1474 break;
1475 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001476 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001477 columns_remaining=columns;
1478 p=tile_pixels+(rows-rows_remaining)*columns;
1479 q=tile+(image->columns*(rows_remaining-1)+x);
1480 for (row=rows_remaining; row > 0; row--)
1481 {
1482 if (image->matte != MagickFalse)
1483 for (column=columns_remaining; column > 0; column--)
1484 {
1485 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1486 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1487 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1488 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1489 (unsigned char) TIFFGetA(*p)));
1490 q++;
1491 p++;
1492 }
1493 else
1494 for (column=columns_remaining; column > 0; column--)
1495 {
1496 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1497 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1498 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1499 q++;
1500 p++;
1501 }
1502 p+=columns-columns_remaining;
1503 q-=(image->columns+columns_remaining);
1504 }
1505 }
1506 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1507 break;
1508 if (image->previous == (Image *) NULL)
1509 {
cristycee97112010-05-28 00:44:52 +00001510 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1511 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001512 if (status == MagickFalse)
1513 break;
1514 }
1515 }
1516 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1517 break;
1518 }
1519 case ReadGenericMethod:
1520 default:
1521 {
1522 register uint32
1523 *p;
1524
1525 uint32
1526 *pixels;
1527
1528 /*
1529 Convert TIFF image to DirectClass MIFF image.
1530 */
1531 number_pixels=(MagickSizeType) image->columns*image->rows;
1532 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1533 (number_pixels*sizeof(uint32))))
1534 {
1535 TIFFClose(tiff);
1536 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1537 }
1538 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1539 sizeof(uint32));
1540 if (pixels == (uint32 *) NULL)
1541 {
1542 TIFFClose(tiff);
1543 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1544 }
1545 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1546 (uint32) image->rows,(uint32 *) pixels,0);
1547 /*
1548 Convert image to DirectClass pixel packets.
1549 */
1550 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001551 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001552 {
cristybb503372010-05-27 20:51:26 +00001553 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001554 x;
1555
1556 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001557 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001558
1559 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1560 if (q == (PixelPacket *) NULL)
1561 break;
1562 q+=image->columns-1;
cristybb503372010-05-27 20:51:26 +00001563 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001564 {
1565 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1566 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1567 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1568 if (image->matte != MagickFalse)
1569 q->opacity=ScaleCharToQuantum((unsigned char) TIFFGetA(*p));
1570 p--;
1571 q--;
1572 }
1573 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1574 break;
1575 if (image->previous == (Image *) NULL)
1576 {
cristycee97112010-05-28 00:44:52 +00001577 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1578 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001579 if (status == MagickFalse)
1580 break;
1581 }
1582 }
1583 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1584 break;
1585 }
1586 }
1587 SetQuantumImageType(image,quantum_type);
1588 next_tiff_frame:
1589 if ((photometric == PHOTOMETRIC_LOGL) ||
1590 (photometric == PHOTOMETRIC_MINISBLACK) ||
1591 (photometric == PHOTOMETRIC_MINISWHITE))
1592 {
1593 image->type=GrayscaleType;
1594 if (bits_per_sample == 1)
1595 image->type=BilevelType;
1596 }
1597 if (image->storage_class == PseudoClass)
1598 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001599 image->endian=MSBEndian;
1600 if (endian == FILLORDER_LSB2MSB)
1601 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001602 if ((photometric == PHOTOMETRIC_LOGL) ||
1603 (photometric == PHOTOMETRIC_MINISBLACK) ||
1604 (photometric == PHOTOMETRIC_MINISWHITE))
1605 {
1606 image->type=GrayscaleType;
1607 if (bits_per_sample == 1)
1608 image->type=BilevelType;
1609 }
1610 /*
1611 Proceed to next image.
1612 */
1613 if (image_info->number_scenes != 0)
1614 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1615 break;
1616 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1617 if (status == MagickTrue)
1618 {
1619 /*
1620 Allocate next image structure.
1621 */
1622 AcquireNextImage(image_info,image);
1623 if (GetNextImageInList(image) == (Image *) NULL)
1624 {
1625 image=DestroyImageList(image);
1626 return((Image *) NULL);
1627 }
1628 image=SyncNextImageInList(image);
1629 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1630 image->scene);
1631 if (status == MagickFalse)
1632 break;
1633 }
1634 quantum_info=DestroyQuantumInfo(quantum_info);
1635 } while (status == MagickTrue);
1636 (void) TIFFSetWarningHandler(warning_handler);
1637 (void) TIFFSetErrorHandler(error_handler);
1638 TIFFClose(tiff);
1639 return(GetFirstImageInList(image));
1640}
1641#endif
1642
1643/*
1644%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1645% %
1646% %
1647% %
1648% R e g i s t e r T I F F I m a g e %
1649% %
1650% %
1651% %
1652%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1653%
1654% RegisterTIFFImage() adds properties for the TIFF image format to
1655% the list of supported formats. The properties include the image format
1656% tag, a method to read and/or write the format, whether the format
1657% supports the saving of more than one frame to the same file or blob,
1658% whether the format supports native in-memory I/O, and a brief
1659% description of the format.
1660%
1661% The format of the RegisterTIFFImage method is:
1662%
cristybb503372010-05-27 20:51:26 +00001663% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001664%
1665*/
cristybb503372010-05-27 20:51:26 +00001666ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001667{
1668#define TIFFDescription "Tagged Image File Format"
1669
1670 char
1671 version[MaxTextExtent];
1672
1673 MagickInfo
1674 *entry;
cristy6b032822010-06-29 16:52:32 +00001675
cristy18b17442009-10-25 18:36:48 +00001676 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1677 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001678 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001679 if (instantiate_key == MagickFalse)
1680 {
1681 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1682 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1683 instantiate_key=MagickTrue;
1684 }
cristyf84a1932010-01-03 18:00:18 +00001685 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001686 *version='\0';
1687#if defined(TIFF_VERSION)
1688 (void) FormatMagickString(version,MaxTextExtent,"%d",TIFF_VERSION);
1689#endif
1690#if defined(MAGICKCORE_TIFF_DELEGATE)
1691 {
1692 const char
1693 *p;
1694
cristybb503372010-05-27 20:51:26 +00001695 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001696 i;
1697
1698 p=TIFFGetVersion();
1699 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1700 version[i]=(*p++);
1701 version[i]='\0';
1702 }
1703#endif
1704
cristy3d7f8062009-09-24 20:45:35 +00001705 entry=SetMagickInfo("GROUP4");
1706#if defined(MAGICKCORE_TIFF_DELEGATE)
1707 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1708 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1709#endif
1710 entry->raw=MagickTrue;
1711 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001712 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001713 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001714 entry->seekable_stream=MagickTrue;
1715 entry->thread_support=NoThreadSupport;
1716 entry->description=ConstantString("Raw CCITT Group4");
1717 entry->module=ConstantString("TIFF");
1718 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001719 entry=SetMagickInfo("PTIF");
1720#if defined(MAGICKCORE_TIFF_DELEGATE)
1721 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1722 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1723#endif
1724 entry->endian_support=MagickTrue;
1725 entry->seekable_stream=MagickTrue;
1726 entry->thread_support=NoThreadSupport;
1727 entry->description=ConstantString("Pyramid encoded TIFF");
1728 entry->module=ConstantString("TIFF");
1729 (void) RegisterMagickInfo(entry);
1730 entry=SetMagickInfo("TIF");
1731#if defined(MAGICKCORE_TIFF_DELEGATE)
1732 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1733 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1734#endif
1735 entry->endian_support=MagickTrue;
1736 entry->seekable_stream=MagickTrue;
1737 entry->stealth=MagickTrue;
1738 entry->thread_support=NoThreadSupport;
1739 entry->description=ConstantString(TIFFDescription);
1740 if (*version != '\0')
1741 entry->version=ConstantString(version);
1742 entry->module=ConstantString("TIFF");
1743 (void) RegisterMagickInfo(entry);
1744 entry=SetMagickInfo("TIFF");
1745#if defined(MAGICKCORE_TIFF_DELEGATE)
1746 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1747 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1748#endif
1749 entry->magick=(IsImageFormatHandler *) IsTIFF;
1750 entry->endian_support=MagickTrue;
1751 entry->seekable_stream=MagickTrue;
1752 entry->thread_support=NoThreadSupport;
1753 entry->description=ConstantString(TIFFDescription);
1754 if (*version != '\0')
1755 entry->version=ConstantString(version);
1756 entry->module=ConstantString("TIFF");
1757 (void) RegisterMagickInfo(entry);
1758 entry=SetMagickInfo("TIFF64");
1759#if defined(TIFF_VERSION_BIG)
1760 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1761 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1762#endif
1763 entry->adjoin=MagickFalse;
1764 entry->endian_support=MagickTrue;
1765 entry->seekable_stream=MagickTrue;
1766 entry->thread_support=NoThreadSupport;
1767 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1768 if (*version != '\0')
1769 entry->version=ConstantString(version);
1770 entry->module=ConstantString("TIFF");
1771 (void) RegisterMagickInfo(entry);
1772 return(MagickImageCoderSignature);
1773}
1774
1775/*
1776%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1777% %
1778% %
1779% %
1780% U n r e g i s t e r T I F F I m a g e %
1781% %
1782% %
1783% %
1784%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1785%
1786% UnregisterTIFFImage() removes format registrations made by the TIFF module
1787% from the list of supported formats.
1788%
1789% The format of the UnregisterTIFFImage method is:
1790%
1791% UnregisterTIFFImage(void)
1792%
1793*/
1794ModuleExport void UnregisterTIFFImage(void)
1795{
cristy3d7f8062009-09-24 20:45:35 +00001796 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001797 (void) UnregisterMagickInfo("PTIF");
1798 (void) UnregisterMagickInfo("TIF");
1799 (void) UnregisterMagickInfo("TIFF");
1800 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001801 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1802 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001803 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001804 if (instantiate_key != MagickFalse)
1805 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1806 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1807 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001808 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001809 DestroySemaphoreInfo(&tiff_semaphore);
1810}
1811
1812#if defined(MAGICKCORE_TIFF_DELEGATE)
1813/*
1814%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1815% %
1816% %
1817% %
cristy3d7f8062009-09-24 20:45:35 +00001818% W r i t e G R O U P 4 I m a g e %
1819% %
1820% %
1821% %
1822%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1823%
1824% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1825%
1826% The format of the WriteGROUP4Image method is:
1827%
1828% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1829% Image *image)
1830%
1831% A description of each parameter follows:
1832%
1833% o image_info: the image info.
1834%
1835% o image: The image.
1836%
1837*/
1838static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1839 Image *image)
1840{
1841 char
1842 filename[MaxTextExtent];
1843
1844 FILE
1845 *file;
1846
1847 Image
1848 *huffman_image;
1849
1850 ImageInfo
1851 *write_info;
1852
1853 int
1854 unique_file;
1855
1856 MagickBooleanType
1857 status;
1858
cristybb503372010-05-27 20:51:26 +00001859 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001860 i;
1861
1862 ssize_t
1863 count;
1864
1865 TIFF
1866 *tiff;
1867
cristy94c8fe42009-10-06 01:57:36 +00001868 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001869 *byte_count,
1870 strip_size;
1871
1872 unsigned char
1873 *buffer;
1874
1875 /*
1876 Write image as CCITT Group4 TIFF image to a temporary file.
1877 */
1878 assert(image_info != (const ImageInfo *) NULL);
1879 assert(image_info->signature == MagickSignature);
1880 assert(image != (Image *) NULL);
1881 assert(image->signature == MagickSignature);
1882 if (image->debug != MagickFalse)
1883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1884 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1885 if (status == MagickFalse)
1886 return(status);
1887 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1888 if (huffman_image == (Image *) NULL)
1889 {
1890 (void) CloseBlob(image);
1891 return(MagickFalse);
1892 }
cristy94c8fe42009-10-06 01:57:36 +00001893 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001894 file=(FILE *) NULL;
1895 unique_file=AcquireUniqueFileResource(filename);
1896 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001897 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001898 if ((unique_file == -1) || (file == (FILE *) NULL))
1899 {
1900 ThrowFileException(&image->exception,FileOpenError,
1901 "UnableToCreateTemporaryFile",filename);
1902 return(MagickFalse);
1903 }
1904 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1905 filename);
1906 (void) SetImageType(huffman_image,BilevelType);
cristy14efd992009-09-26 23:46:03 +00001907 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001908 SetImageInfoFile(write_info,file);
1909 write_info->compression=Group4Compression;
1910 write_info->type=BilevelType;
1911 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1912 status=WriteTIFFImage(write_info,huffman_image);
1913 (void) fflush(file);
1914 write_info=DestroyImageInfo(write_info);
1915 if (status == MagickFalse)
1916 {
1917 InheritException(&image->exception,&huffman_image->exception);
1918 huffman_image=DestroyImage(huffman_image);
1919 (void) fclose(file);
1920 (void) RelinquishUniqueFileResource(filename);
1921 return(MagickFalse);
1922 }
1923 tiff=TIFFOpen(filename,"rb");
1924 if (tiff == (TIFF *) NULL)
1925 {
1926 huffman_image=DestroyImage(huffman_image);
1927 (void) fclose(file);
1928 (void) RelinquishUniqueFileResource(filename);
1929 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1930 image_info->filename);
1931 return(MagickFalse);
1932 }
1933 /*
1934 Allocate raw strip buffer.
1935 */
cristy94c8fe42009-10-06 01:57:36 +00001936 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1937 {
1938 TIFFClose(tiff);
1939 huffman_image=DestroyImage(huffman_image);
1940 (void) fclose(file);
1941 (void) RelinquishUniqueFileResource(filename);
1942 return(MagickFalse);
1943 }
cristy3d7f8062009-09-24 20:45:35 +00001944 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00001945 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001946 if (byte_count[i] > strip_size)
1947 strip_size=byte_count[i];
1948 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1949 sizeof(*buffer));
1950 if (buffer == (unsigned char *) NULL)
1951 {
1952 TIFFClose(tiff);
1953 huffman_image=DestroyImage(huffman_image);
1954 (void) fclose(file);
1955 (void) RelinquishUniqueFileResource(filename);
1956 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1957 image_info->filename);
1958 }
1959 /*
1960 Compress runlength encoded to 2D Huffman pixels.
1961 */
cristybb503372010-05-27 20:51:26 +00001962 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001963 {
cristy94c8fe42009-10-06 01:57:36 +00001964 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00001965 if (WriteBlob(image,(size_t) count,buffer) != count)
1966 status=MagickFalse;
1967 }
1968 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1969 TIFFClose(tiff);
1970 huffman_image=DestroyImage(huffman_image);
1971 (void) fclose(file);
1972 (void) RelinquishUniqueFileResource(filename);
1973 (void) CloseBlob(image);
1974 return(status);
1975}
1976#endif
1977
1978#if defined(MAGICKCORE_TIFF_DELEGATE)
1979/*
1980%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1981% %
1982% %
1983% %
cristy3ed852e2009-09-05 21:47:34 +00001984% W r i t e P T I F I m a g e %
1985% %
1986% %
1987% %
1988%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1989%
1990% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
1991% format.
1992%
1993% The format of the WritePTIFImage method is:
1994%
1995% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
1996% Image *image)
1997%
1998% A description of each parameter follows:
1999%
2000% o image_info: the image info.
2001%
2002% o image: The image.
2003%
2004*/
2005static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2006 Image *image)
2007{
2008 Image
2009 *images,
2010 *next,
2011 *pyramid_image;
2012
2013 ImageInfo
2014 *write_info;
2015
2016 MagickBooleanType
2017 status;
2018
cristybb503372010-05-27 20:51:26 +00002019 size_t
cristy3ed852e2009-09-05 21:47:34 +00002020 columns,
2021 rows;
2022
2023 /*
2024 Create pyramid-encoded TIFF image.
2025 */
2026 images=NewImageList();
2027 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2028 {
2029 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2030 &image->exception));
2031 columns=next->columns;
2032 rows=next->rows;
2033 while ((columns > 64) && (rows > 64))
2034 {
2035 columns/=2;
2036 rows/=2;
2037 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2038 &image->exception);
2039 AppendImageToList(&images,pyramid_image);
2040 }
2041 }
2042 /*
2043 Write pyramid-encoded TIFF image.
2044 */
cristy58567532009-09-26 01:13:32 +00002045 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002046 *write_info->magick='\0';
2047 write_info->adjoin=MagickTrue;
2048 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2049 images=DestroyImageList(images);
2050 write_info=DestroyImageInfo(write_info);
2051 return(status);
2052}
2053#endif
2054
2055#if defined(MAGICKCORE_TIFF_DELEGATE)
2056/*
2057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2058% %
2059% %
2060% W r i t e T I F F I m a g e %
2061% %
2062% %
2063% %
2064%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2065%
2066% WriteTIFFImage() writes an image in the Tagged image file format.
2067%
2068% The format of the WriteTIFFImage method is:
2069%
2070% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2071% Image *image)
2072%
2073% A description of each parameter follows:
2074%
2075% o image_info: the image info.
2076%
2077% o image: The image.
2078%
2079*/
2080
2081typedef struct _TIFFInfo
2082{
2083 RectangleInfo
2084 tile_geometry;
2085
2086 unsigned char
2087 *scanline,
2088 *scanlines,
2089 *pixels;
2090} TIFFInfo;
2091
2092static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2093{
2094 assert(tiff_info != (TIFFInfo *) NULL);
2095 if (tiff_info->scanlines != (unsigned char *) NULL)
2096 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2097 tiff_info->scanlines);
2098 if (tiff_info->pixels != (unsigned char *) NULL)
2099 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2100 tiff_info->pixels);
2101}
2102
2103static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2104 TIFFInfo *tiff_info)
2105{
2106 const char
2107 *option;
2108
2109 MagickStatusType
2110 flags;
2111
2112 assert(tiff_info != (TIFFInfo *) NULL);
2113 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2114 option=GetImageOption(image_info,"tiff:tile-geometry");
2115 if (option == (const char *) NULL)
2116 return(MagickTrue);
2117 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2118 if ((flags & HeightValue) == 0)
2119 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2120 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,(uint32)
2121 tiff_info->tile_geometry.width);
2122 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,(uint32)
2123 tiff_info->tile_geometry.height);
2124 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2125 tiff_info->tile_geometry.height*TIFFScanlineSize(tiff),
2126 sizeof(*tiff_info->scanlines));
2127 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2128 TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2129 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2130 (tiff_info->pixels == (unsigned char *) NULL))
2131 {
2132 DestroyTIFFInfo(tiff_info);
2133 return(MagickFalse);
2134 }
2135 return(MagickTrue);
2136}
2137
cristybb503372010-05-27 20:51:26 +00002138static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002139 tsample_t sample,Image *image)
2140{
2141 int32
2142 status;
2143
cristybb503372010-05-27 20:51:26 +00002144 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002145 bytes_per_pixel,
2146 j,
2147 k,
2148 l;
2149
cristybb503372010-05-27 20:51:26 +00002150 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002151 i;
2152
2153 register unsigned char
2154 *p,
2155 *q;
2156
cristybb503372010-05-27 20:51:26 +00002157 size_t
cristy3ed852e2009-09-05 21:47:34 +00002158 number_tiles,
2159 tile_width;
2160
2161 if (TIFFIsTiled(tiff) == 0)
2162 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2163 /*
2164 Fill scanlines to tile height.
2165 */
cristybb503372010-05-27 20:51:26 +00002166 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002167 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2168 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002169 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2170 (tiff_info->tile_geometry.height-1)) && (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002171 return(0);
2172 /*
2173 Write tile to TIFF image.
2174 */
2175 status=0;
cristybb503372010-05-27 20:51:26 +00002176 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002177 tiff_info->tile_geometry.width);
2178 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2179 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002180 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002181 {
cristybb503372010-05-27 20:51:26 +00002182 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002183 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002184 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2185 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002186 {
2187 if (bytes_per_pixel == 0)
2188 {
2189 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2190 tiff_info->tile_geometry.width+k)/8);
2191 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2192 *q++=(*p++);
2193 continue;
2194 }
2195 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2196 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2197 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2198 for (l=0; l < bytes_per_pixel; l++)
2199 *q++=(*p++);
2200 }
cristydaff8092010-04-22 14:50:53 +00002201 if ((i*tiff_info->tile_geometry.width) != image->columns)
2202 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2203 tiff_info->tile_geometry.width),(uint32) ((row/
2204 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2205 sample);
cristy3ed852e2009-09-05 21:47:34 +00002206 if (status < 0)
2207 break;
2208 }
2209 return(status);
2210}
2211
2212static void TIFFSetProfiles(TIFF *tiff,Image *image)
2213{
2214 const char
2215 *name;
2216
2217 const StringInfo
2218 *profile;
2219
2220 if (image->profiles == (void *) NULL)
2221 return;
2222 ResetImageProfileIterator(image);
2223 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2224 {
2225 profile=GetImageProfile(image,name);
2226#if defined(TIFFTAG_XMLPACKET)
2227 if (LocaleCompare(name,"xmp") == 0)
2228 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2229 profile),GetStringInfoDatum(profile));
2230#endif
2231#if defined(TIFFTAG_ICCPROFILE)
2232 if (LocaleCompare(name,"icc") == 0)
2233 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2234 profile),GetStringInfoDatum(profile));
2235#endif
2236 if (LocaleCompare(name,"iptc") == 0)
2237 {
2238 size_t
2239 length;
2240
2241 StringInfo
2242 *iptc_profile;
2243
2244 iptc_profile=CloneStringInfo(profile);
2245 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2246 0x03);
2247 SetStringInfoLength(iptc_profile,length);
2248 if (TIFFIsByteSwapped(tiff))
2249 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002250 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002251 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2252 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2253 iptc_profile=DestroyStringInfo(iptc_profile);
2254 }
2255#if defined(TIFFTAG_PHOTOSHOP)
2256 if (LocaleCompare(name,"8bim") == 0)
2257 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2258 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2259#endif
2260 if (LocaleCompare(name,"tiff:37724") == 0)
2261 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2262 GetStringInfoDatum(profile));
2263 name=GetNextImageProfile(image);
2264 }
2265}
2266
2267static void TIFFSetProperties(TIFF *tiff,Image *image)
2268{
2269 const char
2270 *value;
2271
2272 value=GetImageProperty(image,"tiff:hostcomputer");
2273 if (value != (const char *) NULL)
2274 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2275 value=GetImageProperty(image,"tiff:artist");
2276 if (value != (const char *) NULL)
2277 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2278 value=GetImageProperty(image,"tiff:timestamp");
2279 if (value != (const char *) NULL)
2280 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2281 value=GetImageProperty(image,"tiff:make");
2282 if (value != (const char *) NULL)
2283 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2284 value=GetImageProperty(image,"tiff:model");
2285 if (value != (const char *) NULL)
2286 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2287 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,
cristybb503372010-05-27 20:51:26 +00002288 GetMagickVersion((size_t *) NULL));
cristy3ed852e2009-09-05 21:47:34 +00002289 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
2290 value=GetImageProperty(image,"tiff:copyright");
2291 if (value != (const char *) NULL)
2292 (void) TIFFSetField(tiff,33432,value);
2293 value=GetImageProperty(image,"kodak-33423");
2294 if (value != (const char *) NULL)
2295 (void) TIFFSetField(tiff,33423,value);
2296 value=GetImageProperty(image,"kodak-36867");
2297 if (value != (const char *) NULL)
2298 (void) TIFFSetField(tiff,36867,value);
2299 value=GetImageProperty(image,"label");
2300 if (value != (const char *) NULL)
2301 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2302 value=GetImageProperty(image,"comment");
2303 if (value != (const char *) NULL)
2304 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2305}
2306
2307static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2308{
2309#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2310 const char
2311 *value;
2312
cristybb503372010-05-27 20:51:26 +00002313 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002314 i;
2315
2316 uint32
2317 offset;
2318
2319 /*
2320 Write EXIF properties.
2321 */
2322 offset=0;
2323 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2324 for (i=0; exif_info[i].tag != 0; i++)
2325 {
2326 value=GetImageProperty(image,exif_info[i].property);
2327 if (value == (const char *) NULL)
2328 continue;
2329 switch (exif_info[i].type)
2330 {
2331 case TIFF_ASCII:
2332 {
2333 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2334 break;
2335 }
2336 case TIFF_SHORT:
2337 {
2338 uint16
2339 shorty;
2340
cristyf2f27272009-12-17 14:48:46 +00002341 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002342 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2343 break;
2344 }
2345 case TIFF_LONG:
2346 {
2347 uint16
cristybb503372010-05-27 20:51:26 +00002348 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002349
cristybb503372010-05-27 20:51:26 +00002350 ssize_ty=(uint16) StringToLong(value);
2351 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002352 break;
2353 }
2354 case TIFF_RATIONAL:
2355 case TIFF_SRATIONAL:
2356 {
2357 float
2358 rational;
2359
cristyf2f27272009-12-17 14:48:46 +00002360 rational=StringToDouble(value);
cristy3ed852e2009-09-05 21:47:34 +00002361 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2362 break;
2363 }
2364 default:
2365 break;
2366 }
2367 }
2368 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2369#else
2370 (void) tiff;
2371 (void) image;
2372#endif
2373}
2374
2375static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2376 Image *image)
2377{
2378#if !defined(TIFFDefaultStripSize)
2379#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2380#endif
2381
2382 const char
2383 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002384 *option;
cristy3ed852e2009-09-05 21:47:34 +00002385
2386 CompressionType
2387 compression;
2388
cristy6b032822010-06-29 16:52:32 +00002389 EndianType
2390 endian_type;
2391
cristybb503372010-05-27 20:51:26 +00002392 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002393 y;
2394
2395 MagickBooleanType
2396 debug,
2397 status;
2398
2399 MagickOffsetType
2400 scene;
2401
2402 QuantumInfo
2403 *quantum_info;
2404
2405 QuantumType
2406 quantum_type;
2407
cristybb503372010-05-27 20:51:26 +00002408 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002409 i;
2410
2411 size_t
2412 length;
2413
2414 TIFF
2415 *tiff;
2416
2417 TIFFErrorHandler
2418 error_handler,
2419 warning_handler;
2420
2421 TIFFInfo
2422 tiff_info;
2423
2424 uint16
2425 bits_per_sample,
2426 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002427 endian,
cristy3ed852e2009-09-05 21:47:34 +00002428 photometric;
2429
2430 uint32
2431 rows_per_strip;
2432
2433 unsigned char
2434 *pixels;
2435
cristyf2687ca2010-06-29 16:32:38 +00002436 size_t
2437 lsb_first;
2438
cristy3ed852e2009-09-05 21:47:34 +00002439 /*
2440 Open TIFF file.
2441 */
2442 assert(image_info != (const ImageInfo *) NULL);
2443 assert(image_info->signature == MagickSignature);
2444 assert(image != (Image *) NULL);
2445 assert(image->signature == MagickSignature);
2446 if (image->debug != MagickFalse)
2447 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2448 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2449 if (status == MagickFalse)
2450 return(status);
2451 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2452 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2453 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002454 endian_type=UndefinedEndian;
2455 option=GetImageOption(image_info,"tiff:endian");
2456 if (option != (const char *) NULL)
2457 {
2458 if (LocaleNCompare(option,"msb",3) == 0)
2459 endian_type=MSBEndian;
2460 if (LocaleNCompare(option,"lsb",3) == 0)
2461 endian_type=LSBEndian;;
2462 }
2463 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002464 {
2465 case LSBEndian: mode="wl"; break;
2466 case MSBEndian: mode="wb"; break;
2467 default: mode="w"; break;
2468 }
2469#if defined(TIFF_VERSION_BIG)
2470 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002471 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002472 {
2473 case LSBEndian: mode="wl8"; break;
2474 case MSBEndian: mode="wb8"; break;
2475 default: mode="w8"; break;
2476 }
2477#endif
2478 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2479 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2480 TIFFUnmapBlob);
2481 if (tiff == (TIFF *) NULL)
2482 {
2483 (void) TIFFSetWarningHandler(warning_handler);
2484 (void) TIFFSetErrorHandler(error_handler);
2485 return(MagickFalse);
2486 }
2487 scene=0;
2488 debug=IsEventLogging();
2489 do
2490 {
2491 /*
2492 Initialize TIFF fields.
2493 */
2494 if ((image_info->type != UndefinedType) &&
2495 (image_info->type != OptimizeType))
2496 (void) SetImageType(image,image_info->type);
2497 quantum_info=AcquireQuantumInfo(image_info,image);
2498 if (quantum_info == (QuantumInfo *) NULL)
2499 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2500 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2501 (quantum_info->format == UndefinedQuantumFormat) &&
2502 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2503 {
2504 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2505 if (status == MagickFalse)
2506 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2507 }
2508 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2509 (GetPreviousImageInList(image) != (Image *) NULL))
2510 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2511 if ((image->columns != (uint32) image->columns) ||
2512 (image->rows != (uint32) image->rows))
2513 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2514 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2515 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2516 compression=image->compression;
2517 if (image_info->compression != UndefinedCompression)
2518 compression=image_info->compression;
2519 switch (compression)
2520 {
2521 case FaxCompression:
2522 {
2523 compress_tag=COMPRESSION_CCITTFAX3;
2524 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2525 break;
2526 }
2527 case Group4Compression:
2528 {
2529 compress_tag=COMPRESSION_CCITTFAX4;
2530 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2531 break;
2532 }
2533 case JPEGCompression:
2534 {
2535 compress_tag=COMPRESSION_JPEG;
2536 break;
2537 }
2538 case LZWCompression:
2539 {
2540 compress_tag=COMPRESSION_LZW;
2541 break;
2542 }
2543 case RLECompression:
2544 {
2545 compress_tag=COMPRESSION_PACKBITS;
2546 break;
2547 }
2548 case ZipCompression:
2549 {
2550 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2551 break;
2552 }
2553 case NoCompression:
2554 default:
2555 {
2556 compress_tag=COMPRESSION_NONE;
2557 break;
2558 }
2559 }
cristy5b675872009-10-21 13:12:00 +00002560#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2561 if ((compress_tag != COMPRESSION_NONE) &&
2562 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002563 {
cristy5b675872009-10-21 13:12:00 +00002564 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2565 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002566 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002567 compress_tag=COMPRESSION_NONE;
2568 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002569 }
2570#else
2571 switch (compress_tag)
2572 {
2573#if defined(CCITT_SUPPORT)
2574 case COMPRESSION_CCITTFAX3:
2575 case COMPRESSION_CCITTFAX4:
2576#endif
2577#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2578 case COMPRESSION_JPEG:
2579#endif
2580#if defined(LZW_SUPPORT)
2581 case COMPRESSION_LZW:
2582#endif
2583#if defined(PACKBITS_SUPPORT)
2584 case COMPRESSION_PACKBITS:
2585#endif
2586#if defined(ZIP_SUPPORT)
2587 case COMPRESSION_ADOBE_DEFLATE:
2588#endif
2589 case COMPRESSION_NONE:
2590 break;
2591 default:
2592 {
2593 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2594 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002595 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002596 compress_tag=COMPRESSION_NONE;
2597 compression=NoCompression;
2598 break;
2599 }
2600 }
2601#endif
2602 switch (compression)
2603 {
2604 case FaxCompression:
2605 case Group4Compression:
2606 {
2607 (void) SetImageType(image,BilevelType);
2608 break;
2609 }
2610 case JPEGCompression:
2611 {
2612 (void) SetImageStorageClass(image,DirectClass);
2613 (void) SetImageDepth(image,8);
2614 break;
2615 }
2616 default:
2617 break;
2618 }
cristy3ed852e2009-09-05 21:47:34 +00002619 if (image->colorspace == CMYKColorspace)
2620 {
2621 photometric=PHOTOMETRIC_SEPARATED;
2622 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2623 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2624 }
2625 else
2626 {
2627 /*
2628 Full color TIFF raster.
2629 */
2630 if (image->colorspace == LabColorspace)
2631 photometric=PHOTOMETRIC_CIELAB;
2632 else
2633 if (image->colorspace == YCbCrColorspace)
2634 {
2635 photometric=PHOTOMETRIC_YCBCR;
2636 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
2637 (void) SetImageDepth(image,8);
2638 }
2639 else
2640 {
2641 if (image->colorspace != RGBColorspace)
2642 (void) TransformImageColorspace(image,RGBColorspace);
2643 photometric=PHOTOMETRIC_RGB;
2644 }
2645 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2646 if ((image_info->type != TrueColorType) &&
2647 (image_info->type != TrueColorMatteType))
2648 {
cristy94c8fe42009-10-06 01:57:36 +00002649 if ((image_info->type != PaletteType) &&
2650 (IsGrayImage(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002651 {
cristy94c8fe42009-10-06 01:57:36 +00002652 photometric=(uint16) (quantum_info->min_is_white !=
2653 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2654 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002655 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002656 if ((image_info->depth == 0) &&
2657 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2658 {
2659 status=SetQuantumDepth(image,quantum_info,1);
2660 if (status == MagickFalse)
2661 ThrowWriterException(ResourceLimitError,
2662 "MemoryAllocationFailed");
2663 }
cristy3ed852e2009-09-05 21:47:34 +00002664 }
2665 else
cristy94c8fe42009-10-06 01:57:36 +00002666 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002667 {
cristybb503372010-05-27 20:51:26 +00002668 size_t
cristy94c8fe42009-10-06 01:57:36 +00002669 depth;
2670
2671 /*
2672 Colormapped TIFF raster.
2673 */
cristy3ed852e2009-09-05 21:47:34 +00002674 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002675 photometric=PHOTOMETRIC_PALETTE;
2676 depth=1;
2677 while ((GetQuantumRange(depth)+1) < image->colors)
2678 depth<<=1;
2679 status=SetQuantumDepth(image,quantum_info,depth);
2680 if (status == MagickFalse)
2681 ThrowWriterException(ResourceLimitError,
2682 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002683 }
2684 }
2685 }
cristyf2687ca2010-06-29 16:32:38 +00002686 switch (image->endian)
2687 {
2688 case LSBEndian:
2689 {
2690 endian=FILLORDER_LSB2MSB;
2691 break;
2692 }
2693 case MSBEndian:
2694 {
2695 endian=FILLORDER_MSB2LSB;
2696 break;
2697 }
2698 case UndefinedEndian:
2699 default:
2700 {
2701 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2702 break;
2703 }
2704 }
2705 lsb_first=1;
2706 image->endian=MSBEndian;
2707 if ((int) (*(char *) &lsb_first) != 0)
2708 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002709 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2710 (photometric != PHOTOMETRIC_MINISWHITE))
2711 {
2712 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002713 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002714 }
cristy0accf6e2009-10-01 13:14:28 +00002715 else
cristydaff8092010-04-22 14:50:53 +00002716 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2717 (photometric != PHOTOMETRIC_MINISWHITE))
2718 {
2719 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002720 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002721 }
cristy62e282b2010-06-29 01:27:13 +00002722 option=GetImageProperty(image,"tiff:fill-order");
2723 if (option != (const char *) NULL)
2724 {
2725 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002726 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002727 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002728 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002729 }
cristyf2687ca2010-06-29 16:32:38 +00002730 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2731 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002732 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2733 if (image->matte != MagickFalse)
2734 {
2735 uint16
2736 extra_samples,
2737 sample_info[1],
2738 samples_per_pixel;
2739
2740 /*
2741 TIFF has a matte channel.
2742 */
2743 extra_samples=1;
2744 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2745 option=GetImageProperty(image,"tiff:alpha");
2746 if ((option != (const char *) NULL) &&
2747 (LocaleCompare(option,"associated") == 0))
2748 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2749 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2750 &samples_per_pixel);
2751 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2752 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2753 &sample_info);
2754 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2755 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2756 }
2757 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2758 switch (quantum_info->format)
2759 {
2760 case FloatingPointQuantumFormat:
2761 {
2762 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2763 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2764 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2765 break;
2766 }
2767 case SignedQuantumFormat:
2768 {
2769 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2770 break;
2771 }
2772 case UnsignedQuantumFormat:
2773 {
2774 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2775 break;
2776 }
2777 default:
2778 break;
2779 }
cristy3ed852e2009-09-05 21:47:34 +00002780 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2781 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2782 if (photometric == PHOTOMETRIC_RGB)
2783 if ((image_info->interlace == PlaneInterlace) ||
2784 (image_info->interlace == PartitionInterlace))
2785 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2786 rows_per_strip=1;
2787 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002788 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2789 1);
cristy3ed852e2009-09-05 21:47:34 +00002790 option=GetImageOption(image_info,"tiff:rows-per-strip");
2791 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002792 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002793 switch (compress_tag)
2794 {
2795 case COMPRESSION_JPEG:
2796 {
2797#if defined(JPEG_SUPPORT)
2798 const char
2799 *sampling_factor;
2800
2801 GeometryInfo
2802 geometry_info;
2803
2804 MagickStatusType
2805 flags;
2806
2807 rows_per_strip+=(16-(rows_per_strip % 16));
2808 if (image->quality != 0)
2809 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2810 if (image_info->quality != UndefinedCompressionQuality)
2811 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2812 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2813 if (image->colorspace == RGBColorspace)
2814 {
cristy949bf5b2010-05-08 02:47:03 +00002815 const char
2816 *value;
2817
cristy3ed852e2009-09-05 21:47:34 +00002818 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2819 sampling_factor=(const char *) NULL;
2820 value=GetImageProperty(image,"jpeg:sampling-factor");
2821 if (value != (char *) NULL)
2822 {
2823 sampling_factor=value;
2824 if (image->debug != MagickFalse)
2825 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2826 " Input sampling-factors=%s",sampling_factor);
2827 }
2828 if (image_info->sampling_factor != (char *) NULL)
2829 sampling_factor=image_info->sampling_factor;
2830 if (sampling_factor != (const char *) NULL)
2831 {
2832 flags=ParseGeometry(sampling_factor,&geometry_info);
2833 if ((flags & SigmaValue) == 0)
2834 geometry_info.sigma=geometry_info.rho;
2835 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2836 geometry_info.rho,(uint16) geometry_info.sigma);
2837 }
2838 }
2839 if (bits_per_sample == 12)
2840 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2841#endif
2842 break;
2843 }
2844 case COMPRESSION_ADOBE_DEFLATE:
2845 {
cristyf6fe0a12010-05-30 00:44:47 +00002846 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002847 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2848 &bits_per_sample);
2849 if (((photometric == PHOTOMETRIC_RGB) ||
2850 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2851 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2852 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
cristyf6fe0a12010-05-30 00:44:47 +00002853 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2854 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00002855 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00002856 break;
2857 }
2858 case COMPRESSION_CCITTFAX3:
2859 {
2860 /*
2861 Byte-aligned EOL.
2862 */
cristyeaedf062010-05-29 22:36:02 +00002863 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002864 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2865 break;
2866 }
2867 case COMPRESSION_CCITTFAX4:
2868 {
cristyeaedf062010-05-29 22:36:02 +00002869 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002870 break;
2871 }
2872 case COMPRESSION_LZW:
2873 {
2874 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2875 &bits_per_sample);
2876 if (((photometric == PHOTOMETRIC_RGB) ||
2877 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2878 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2879 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
2880 break;
2881 }
2882 default:
2883 break;
2884 }
2885 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
2886 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2887 {
2888 unsigned short
2889 units;
2890
2891 /*
2892 Set image resolution.
2893 */
2894 units=RESUNIT_NONE;
2895 if (image->units == PixelsPerInchResolution)
2896 units=RESUNIT_INCH;
2897 if (image->units == PixelsPerCentimeterResolution)
2898 units=RESUNIT_CENTIMETER;
2899 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2900 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2901 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2902 if ((image->page.x != 0) || (image->page.y != 0))
2903 {
2904 /*
2905 Set image position.
2906 */
2907 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2908 image->x_resolution);
2909 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2910 image->y_resolution);
2911 }
2912 }
2913 if (image->chromaticity.white_point.x != 0.0)
2914 {
2915 float
2916 chromaticity[6];
2917
2918 /*
2919 Set image chromaticity.
2920 */
2921 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2922 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2923 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2924 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2925 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2926 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2927 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2928 chromaticity[0]=(float) image->chromaticity.white_point.x;
2929 chromaticity[1]=(float) image->chromaticity.white_point.y;
2930 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2931 }
2932 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2933 {
2934 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2935 if (image->scene != 0)
2936 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
2937 GetImageListLength(image));
2938 }
2939 if (image->orientation != UndefinedOrientation)
2940 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
2941 (void) TIFFSetProfiles(tiff,image);
2942 {
2943 uint16
2944 page,
2945 pages;
2946
2947 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00002948 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00002949 if ((image_info->adjoin != MagickFalse) && (pages > 1))
2950 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2951 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
2952 }
2953 (void) TIFFSetProperties(tiff,image);
2954 if (0)
2955 (void) TIFFSetEXIFProperties(tiff,image);
2956 /*
2957 Write image scanlines.
2958 */
2959 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
2960 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2961 pixels=GetQuantumPixels(quantum_info);
2962 tiff_info.scanline=GetQuantumPixels(quantum_info);
2963 switch (photometric)
2964 {
2965 case PHOTOMETRIC_CIELAB:
2966 case PHOTOMETRIC_YCBCR:
2967 case PHOTOMETRIC_RGB:
2968 {
2969 /*
2970 RGB TIFF image.
2971 */
2972 switch (image_info->interlace)
2973 {
2974 case NoInterlace:
2975 default:
2976 {
2977 quantum_type=RGBQuantum;
2978 if (image->matte != MagickFalse)
2979 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00002980 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002981 {
2982 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002983 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002984
2985 p=GetVirtualPixels(image,0,y,image->columns,1,
2986 &image->exception);
2987 if (p == (const PixelPacket *) NULL)
2988 break;
2989 length=ExportQuantumPixels(image,(const CacheView *) NULL,
2990 quantum_info,quantum_type,pixels,&image->exception);
2991 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
2992 break;
2993 if (image->previous == (Image *) NULL)
2994 {
cristy2837bcc2010-08-07 23:57:39 +00002995 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
2996 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002997 if (status == MagickFalse)
2998 break;
2999 }
3000 }
3001 break;
3002 }
3003 case PlaneInterlace:
3004 case PartitionInterlace:
3005 {
3006 /*
3007 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3008 */
cristybb503372010-05-27 20:51:26 +00003009 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003010 {
3011 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003012 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003013
3014 p=GetVirtualPixels(image,0,y,image->columns,1,
3015 &image->exception);
3016 if (p == (const PixelPacket *) NULL)
3017 break;
3018 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3019 quantum_info,RedQuantum,pixels,&image->exception);
3020 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3021 break;
3022 }
3023 if (image->previous == (Image *) NULL)
3024 {
3025 status=SetImageProgress(image,SaveImageTag,100,400);
3026 if (status == MagickFalse)
3027 break;
3028 }
cristybb503372010-05-27 20:51:26 +00003029 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003030 {
3031 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003032 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003033
3034 p=GetVirtualPixels(image,0,y,image->columns,1,
3035 &image->exception);
3036 if (p == (const PixelPacket *) NULL)
3037 break;
3038 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3039 quantum_info,GreenQuantum,pixels,&image->exception);
3040 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3041 break;
3042 }
3043 if (image->previous == (Image *) NULL)
3044 {
3045 status=SetImageProgress(image,SaveImageTag,200,400);
3046 if (status == MagickFalse)
3047 break;
3048 }
cristybb503372010-05-27 20:51:26 +00003049 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003050 {
3051 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003052 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003053
3054 p=GetVirtualPixels(image,0,y,image->columns,1,
3055 &image->exception);
3056 if (p == (const PixelPacket *) NULL)
3057 break;
3058 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3059 quantum_info,BlueQuantum,pixels,&image->exception);
3060 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3061 break;
3062 }
3063 if (image->previous == (Image *) NULL)
3064 {
3065 status=SetImageProgress(image,SaveImageTag,300,400);
3066 if (status == MagickFalse)
3067 break;
3068 }
3069 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003070 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003071 {
3072 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003073 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003074
3075 p=GetVirtualPixels(image,0,y,image->columns,1,
3076 &image->exception);
3077 if (p == (const PixelPacket *) NULL)
3078 break;
3079 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3080 quantum_info,AlphaQuantum,pixels,&image->exception);
3081 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3082 break;
3083 }
3084 if (image->previous == (Image *) NULL)
3085 {
3086 status=SetImageProgress(image,SaveImageTag,400,400);
3087 if (status == MagickFalse)
3088 break;
3089 }
3090 break;
3091 }
3092 }
3093 break;
3094 }
3095 case PHOTOMETRIC_SEPARATED:
3096 {
3097 /*
3098 CMYK TIFF image.
3099 */
3100 quantum_type=CMYKQuantum;
3101 if (image->matte != MagickFalse)
3102 quantum_type=CMYKAQuantum;
3103 if (image->colorspace != CMYKColorspace)
3104 (void) TransformImageColorspace(image,CMYKColorspace);
cristybb503372010-05-27 20:51:26 +00003105 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003106 {
3107 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003108 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003109
3110 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3111 if (p == (const PixelPacket *) NULL)
3112 break;
3113 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3114 quantum_info,quantum_type,pixels,&image->exception);
3115 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3116 break;
3117 if (image->previous == (Image *) NULL)
3118 {
cristycee97112010-05-28 00:44:52 +00003119 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3120 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003121 if (status == MagickFalse)
3122 break;
3123 }
3124 }
3125 break;
3126 }
3127 case PHOTOMETRIC_PALETTE:
3128 {
3129 uint16
3130 *blue,
3131 *green,
3132 *red;
3133
3134 /*
3135 Colormapped TIFF image.
3136 */
3137 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3138 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3139 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3140 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3141 (blue == (uint16 *) NULL))
3142 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3143 /*
3144 Initialize TIFF colormap.
3145 */
3146 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3147 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3148 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003149 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003150 {
3151 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3152 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3153 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3154 }
3155 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3156 red=(uint16 *) RelinquishMagickMemory(red);
3157 green=(uint16 *) RelinquishMagickMemory(green);
3158 blue=(uint16 *) RelinquishMagickMemory(blue);
3159 }
3160 default:
3161 {
3162 /*
3163 Convert PseudoClass packets to contiguous grayscale scanlines.
3164 */
3165 quantum_type=IndexQuantum;
3166 if (image->matte != MagickFalse)
3167 {
3168 if (photometric != PHOTOMETRIC_PALETTE)
3169 quantum_type=GrayAlphaQuantum;
3170 else
3171 quantum_type=IndexAlphaQuantum;
3172 }
3173 else
3174 if (photometric != PHOTOMETRIC_PALETTE)
3175 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003176 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003177 {
3178 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003179 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003180
3181 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3182 if (p == (const PixelPacket *) NULL)
3183 break;
3184 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3185 quantum_info,quantum_type,pixels,&image->exception);
3186 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3187 break;
3188 if (image->previous == (Image *) NULL)
3189 {
cristycee97112010-05-28 00:44:52 +00003190 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3191 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003192 if (status == MagickFalse)
3193 break;
3194 }
3195 }
3196 break;
3197 }
3198 }
3199 quantum_info=DestroyQuantumInfo(quantum_info);
3200 DestroyTIFFInfo(&tiff_info);
3201 if (0 && (image_info->verbose == MagickTrue))
3202 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3203 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003204 image->endian=MSBEndian;
3205 if (endian == FILLORDER_LSB2MSB)
3206 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003207 image=SyncNextImageInList(image);
3208 if (image == (Image *) NULL)
3209 break;
3210 status=SetImageProgress(image,SaveImagesTag,scene++,
3211 GetImageListLength(image));
3212 if (status == MagickFalse)
3213 break;
3214 } while (image_info->adjoin != MagickFalse);
3215 (void) TIFFSetWarningHandler(warning_handler);
3216 (void) TIFFSetErrorHandler(error_handler);
3217 TIFFClose(tiff);
3218 return(MagickTrue);
3219}
3220#endif