blob: 2afca7bb2653e126f8ffdcd206068657504ac77f [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% TTTTT IIIII FFFFF FFFFF %
7% T I F F %
8% T I FFF FFF %
9% T I F F %
10% T IIIII F F %
11% %
12% %
13% Read/Write TIFF Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
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"
cristyef8f26b2010-12-19 20:29:16 +000083# endif
cristy3ed852e2009-09-05 21:47:34 +000084# include "tiff.h"
85# include "tiffio.h"
86# if !defined(COMPRESSION_ADOBE_DEFLATE)
87# define COMPRESSION_ADOBE_DEFLATE 8
88# endif
cristyef8f26b2010-12-19 20:29:16 +000089# if !defined(PREDICTOR_HORIZONTAL)
90# define PREDICTOR_HORIZONTAL 2
91# endif
cristy3ed852e2009-09-05 21:47:34 +000092
93/*
94 Typedef declarations.
95*/
96#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
97typedef struct _ExifInfo
98{
99 unsigned int
100 tag,
101 type;
102
103 const char
104 *property;
105} ExifInfo;
106
107static const ExifInfo
108 exif_info[] = {
109 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
110 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
111 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
112 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000113 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy3ed852e2009-09-05 21:47:34 +0000114 { EXIFTAG_OECF, TIFF_UNDEFINED, "exif:OptoelectricConversionFactor" },
115 { EXIFTAG_EXIFVERSION, TIFF_UNDEFINED, "exif:ExifVersion" },
116 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
117 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
118 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_UNDEFINED, "exif:ComponentsConfiguration" },
119 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
120 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
121 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
122 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
123 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
124 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
125 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
126 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
127 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
128 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
129 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
130 /* { EXIFTAG_SUBJECTAREA, TIFF_SHORT, "exif:SubjectArea" }, */
131 { EXIFTAG_MAKERNOTE, TIFF_UNDEFINED, "exif:MakerNote" },
132 { EXIFTAG_USERCOMMENT, TIFF_UNDEFINED, "exif:UserComment" },
133 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
134 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
135 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
136 { EXIFTAG_FLASHPIXVERSION, TIFF_UNDEFINED, "exif:FlashpixVersion" },
137 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
138 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
139 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
140 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
141 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
142 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
143 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_UNDEFINED, "exif:SpatialFrequencyResponse" },
144 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
145 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
146 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
147 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
148 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
149 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
150 { EXIFTAG_FILESOURCE, TIFF_UNDEFINED, "exif:FileSource" },
151 { EXIFTAG_SCENETYPE, TIFF_UNDEFINED, "exif:SceneType" },
152 { EXIFTAG_CFAPATTERN, TIFF_UNDEFINED, "exif:CFAPattern" },
153 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
154 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
155 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
156 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
157 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
158 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
159 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
160 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
161 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
162 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
163 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_UNDEFINED, "exif:DeviceSettingDescription" },
164 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
165 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
166 { 0, 0, (char *) NULL }
167};
168#endif
cristy0805e062011-01-30 02:56:36 +0000169#endif /* MAGICKCORE_TIFF_DELEGATE */
cristy3ed852e2009-09-05 21:47:34 +0000170
171/*
172 Global declarations.
173*/
174static MagickThreadKey
175 tiff_exception;
176
177static SemaphoreInfo
178 *tiff_semaphore = (SemaphoreInfo *) NULL;
179
180static volatile MagickBooleanType
181 instantiate_key = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000182
183/*
184 Forward declarations.
185*/
186#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000187static Image *
188 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
189
cristy3ed852e2009-09-05 21:47:34 +0000190static MagickBooleanType
cristy3d7f8062009-09-24 20:45:35 +0000191 WriteGROUP4Image(const ImageInfo *,Image *),
cristy3ed852e2009-09-05 21:47:34 +0000192 WritePTIFImage(const ImageInfo *,Image *),
193 WriteTIFFImage(const ImageInfo *,Image *);
194#endif
195
196/*
197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198% %
199% %
200% %
201% I s T I F F %
202% %
203% %
204% %
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206%
207% IsTIFF() returns MagickTrue if the image format type, identified by the
208% magick string, is TIFF.
209%
210% The format of the IsTIFF method is:
211%
212% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
213%
214% A description of each parameter follows:
215%
216% o magick: compare image format pattern against these bytes.
217%
218% o length: Specifies the length of the magick string.
219%
220*/
221static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
222{
223 if (length < 4)
224 return(MagickFalse);
225 if (memcmp(magick,"\115\115\000\052",4) == 0)
226 return(MagickTrue);
227 if (memcmp(magick,"\111\111\052\000",4) == 0)
228 return(MagickTrue);
229#if defined(TIFF_VERSION_BIG)
230 if (length < 8)
231 return(MagickFalse);
232 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
233 return(MagickTrue);
234 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
235 return(MagickTrue);
236#endif
237 return(MagickFalse);
238}
239
240#if defined(MAGICKCORE_TIFF_DELEGATE)
241/*
242%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243% %
244% %
245% %
cristy3d7f8062009-09-24 20:45:35 +0000246% R e a d G R O U P 4 I m a g e %
247% %
248% %
249% %
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251%
252% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
253% allocates the memory necessary for the new Image structure and returns a
254% pointer to the new image.
255%
256% The format of the ReadGROUP4Image method is:
257%
258% Image *ReadGROUP4Image(const ImageInfo *image_info,
259% ExceptionInfo *exception)
260%
261% A description of each parameter follows:
262%
263% o image_info: the image info.
264%
265% o exception: return any errors or warnings in this structure.
266%
267*/
268
cristyeaedf062010-05-29 22:36:02 +0000269static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000270{
271 unsigned char
272 buffer[4];
273
274 buffer[0]=(unsigned char) value;
275 buffer[1]=(unsigned char) (value >> 8);
276 buffer[2]=(unsigned char) (value >> 16);
277 buffer[3]=(unsigned char) (value >> 24);
278 return(fwrite(buffer,1,4,file));
279}
280
281static Image *ReadGROUP4Image(const ImageInfo *image_info,
282 ExceptionInfo *exception)
283{
284 char
285 filename[MaxTextExtent];
286
287 FILE
288 *file;
289
290 Image
291 *image;
292
293 ImageInfo
294 *read_info;
295
296 int
297 c,
298 unique_file;
299
300 MagickBooleanType
301 status;
302
303 size_t
304 length;
305
306 ssize_t
307 offset,
308 strip_offset;
309
310 /*
311 Open image file.
312 */
313 assert(image_info != (const ImageInfo *) NULL);
314 assert(image_info->signature == MagickSignature);
315 if (image_info->debug != MagickFalse)
316 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
317 image_info->filename);
318 assert(exception != (ExceptionInfo *) NULL);
319 assert(exception->signature == MagickSignature);
320 image=AcquireImage(image_info);
321 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
322 if (status == MagickFalse)
323 {
324 image=DestroyImageList(image);
325 return((Image *) NULL);
326 }
327 /*
328 Write raw CCITT Group 4 wrapped as a TIFF image file.
329 */
330 file=(FILE *) NULL;
331 unique_file=AcquireUniqueFileResource(filename);
332 if (unique_file != -1)
333 file=fdopen(unique_file,"wb");
334 if ((unique_file == -1) || (file == (FILE *) NULL))
335 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
336 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
337 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
338 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
339 length=WriteLSBLong(file,image->columns);
340 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
341 length=WriteLSBLong(file,image->rows);
342 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
343 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
344 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
345 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
346 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000347 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000348 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000349 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000350 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
351 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000352 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000353 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
354 offset=(ssize_t) ftell(file)-4;
355 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000356 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000357 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000358 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000359 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
360 length=fwrite("\000\000\000\000",1,4,file);
361 length=WriteLSBLong(file,image->x_resolution);
362 length=WriteLSBLong(file,1);
363 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
364 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000365 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000366 length=WriteLSBLong(file,(unsigned int) length);
367 (void) fclose(file);
368 (void) CloseBlob(image);
369 image=DestroyImage(image);
370 /*
371 Read TIFF image.
372 */
cristy14efd992009-09-26 23:46:03 +0000373 read_info=CloneImageInfo((ImageInfo *) NULL);
cristy53a8bc92011-01-10 19:15:17 +0000374 (void) FormatMagickString(read_info->filename,MaxTextExtent,"%s",filename);
cristy3d7f8062009-09-24 20:45:35 +0000375 image=ReadTIFFImage(read_info,exception);
376 read_info=DestroyImageInfo(read_info);
377 if (image != (Image *) NULL)
378 {
379 (void) CopyMagickString(image->filename,image_info->filename,
380 MaxTextExtent);
381 (void) CopyMagickString(image->magick_filename,image_info->filename,
382 MaxTextExtent);
383 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
384 }
385 (void) RelinquishUniqueFileResource(filename);
386 return(image);
387}
388#endif
389
390#if defined(MAGICKCORE_TIFF_DELEGATE)
391/*
392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393% %
394% %
395% %
cristy3ed852e2009-09-05 21:47:34 +0000396% R e a d T I F F I m a g e %
397% %
398% %
399% %
400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401%
402% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
403% memory necessary for the new Image structure and returns a pointer to the
404% new image.
405%
406% The format of the ReadTIFFImage method is:
407%
408% Image *ReadTIFFImage(const ImageInfo *image_info,
409% ExceptionInfo *exception)
410%
411% A description of each parameter follows:
412%
413% o image_info: the image info.
414%
415% o exception: return any errors or warnings in this structure.
416%
417*/
418
419static inline size_t MagickMax(const size_t x,const size_t y)
420{
421 if (x > y)
422 return(x);
423 return(y);
424}
425
cristybb503372010-05-27 20:51:26 +0000426static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000427{
428 if (x < y)
429 return(x);
430 return(y);
431}
432
433static MagickBooleanType ReadProfile(Image *image,const char *name,
cristybb503372010-05-27 20:51:26 +0000434 unsigned char *datum,ssize_t length)
cristy3ed852e2009-09-05 21:47:34 +0000435{
436 MagickBooleanType
437 status;
438
cristybb503372010-05-27 20:51:26 +0000439 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000440 i;
441
442 StringInfo
443 *profile;
444
445 if (length < 4)
446 return(MagickFalse);
447 i=0;
448 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
449 {
450 for (i=0; i < (length-4); i+=2)
451 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
452 break;
453 if (i == length)
454 length-=i;
455 else
456 i=0;
457 if (length < 4)
458 return(MagickFalse);
459 }
460 profile=AcquireStringInfo((size_t) length);
461 SetStringInfoDatum(profile,datum+i);
462 status=SetImageProfile(image,name,profile);
463 profile=DestroyStringInfo(profile);
464 if (status == MagickFalse)
465 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
466 image->filename);
467 return(MagickTrue);
468}
469
470#if defined(__cplusplus) || defined(c_plusplus)
471extern "C" {
472#endif
473
474static int TIFFCloseBlob(thandle_t image)
475{
476 (void) CloseBlob((Image *) image);
477 return(0);
478}
479
480static void TIFFErrors(const char *module,const char *format,va_list error)
481{
482 char
483 message[MaxTextExtent];
484
485 ExceptionInfo
486 *exception;
487
488#if defined(MAGICKCORE_HAVE_VSNPRINTF)
489 (void) vsnprintf(message,MaxTextExtent,format,error);
490#else
491 (void) vsprintf(message,format,error);
492#endif
493 (void) ConcatenateMagickString(message,".",MaxTextExtent);
494 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
495 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000496 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
497 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000498}
499
500static void TIFFGetProfiles(TIFF *tiff,Image *image)
501{
502 uint32
503 length;
504
505 unsigned char
506 *profile;
507
508#if defined(TIFFTAG_ICCPROFILE)
509 length=0;
510 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000511 (void) ReadProfile(image,"icc",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000512#endif
513#if defined(TIFFTAG_PHOTOSHOP)
514 length=0;
515 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000516 (void) ReadProfile(image,"8bim",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000517#endif
518#if defined(TIFFTAG_RICHTIFFIPTC)
519 length=0;
520 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
521 {
522 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000523 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000524 (void) ReadProfile(image,"iptc",profile,4L*length);
525 }
526#endif
527#if defined(TIFFTAG_XMLPACKET)
528 length=0;
529 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000530 (void) ReadProfile(image,"xmp",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000531#endif
532 length=0;
533 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000534 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000535}
536
537static void TIFFGetProperties(TIFF *tiff,Image *image)
538{
539 char
540 *text;
541
542 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
543 (void) SetImageProperty(image,"tiff:artist",text);
544 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
545 (void) SetImageProperty(image,"tiff:timestamp",text);
546 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
547 (void) SetImageProperty(image,"tiff:software",text);
548 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
549 (void) SetImageProperty(image,"tiff:hostcomputer",text);
550 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
551 (void) SetImageProperty(image,"tiff:document",text);
552 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
553 (void) SetImageProperty(image,"tiff:make",text);
554 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
555 (void) SetImageProperty(image,"tiff:model",text);
556 if (TIFFGetField(tiff,33432,&text) == 1)
557 (void) SetImageProperty(image,"tiff:copyright",text);
558 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
559 (void) SetImageProperty(image,"label",text);
560 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
561 (void) SetImageProperty(image,"comment",text);
562}
563
564static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
565{
566#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
567 char
568 value[MaxTextExtent];
569
cristybb503372010-05-27 20:51:26 +0000570 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000571 i;
572
573 tdir_t
574 directory;
575
576 uint32
577 offset;
578
579 /*
580 Read EXIF properties.
581 */
582 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
583 return;
584 directory=TIFFCurrentDirectory(tiff);
585 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
586 return;
587 for (i=0; exif_info[i].tag != 0; i++)
588 {
589 *value='\0';
590 switch (exif_info[i].type)
591 {
592 case TIFF_ASCII:
593 {
594 char
595 *ascii;
596
597 if (TIFFGetField(tiff,exif_info[i].tag,&ascii) != 0)
598 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
599 break;
600 }
601 case TIFF_SHORT:
602 {
603 uint16
604 shorty;
605
606 if (TIFFGetField(tiff,exif_info[i].tag,&shorty) != 0)
607 (void) FormatMagickString(value,MaxTextExtent,"%d",shorty);
608 break;
609 }
610 case TIFF_LONG:
611 {
612 uint32
cristybb503372010-05-27 20:51:26 +0000613 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +0000614
cristybb503372010-05-27 20:51:26 +0000615 if (TIFFGetField(tiff,exif_info[i].tag,&ssize_ty) != 0)
616 (void) FormatMagickString(value,MaxTextExtent,"%d",ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +0000617 break;
618 }
619 case TIFF_RATIONAL:
620 case TIFF_SRATIONAL:
621 {
622 float
623 rational;
624
625 if (TIFFGetField(tiff,exif_info[i].tag,&rational) != 0)
cristye7f51092010-01-17 00:39:37 +0000626 (void) FormatMagickString(value,MaxTextExtent,"%g",rational);
cristy3ed852e2009-09-05 21:47:34 +0000627 break;
628 }
629 default:
630 break;
631 }
632 if (*value != '\0')
633 (void) SetImageProperty(image,exif_info[i].property,value);
634 }
635 TIFFSetDirectory(tiff,directory);
636#else
637 (void) tiff;
638 (void) image;
639#endif
640}
641
642static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
643{
644 *base=(tdata_t *) GetBlobStreamData((Image *) image);
645 if (*base != (tdata_t *) NULL)
646 *size=(toff_t) GetBlobSize((Image *) image);
647 if (*base != (tdata_t *) NULL)
648 return(1);
649 return(0);
650}
651
652static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
653{
654 tsize_t
655 count;
656
657 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
658 (unsigned char *) data);
659 return(count);
660}
661
cristybb503372010-05-27 20:51:26 +0000662static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
663 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000664{
665 int32
666 status;
667
668 (void) bits_per_sample;
669 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
670 return(status);
671}
672
673static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
674{
675 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
676}
677
678static toff_t TIFFGetBlobSize(thandle_t image)
679{
680 return((toff_t) GetBlobSize((Image *) image));
681}
682
683static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
684{
685 (void) image;
686 (void) base;
687 (void) size;
688}
689
690static void TIFFWarnings(const char *module,const char *format,va_list warning)
691{
692 char
693 message[MaxTextExtent];
694
695 ExceptionInfo
696 *exception;
697
698#if defined(MAGICKCORE_HAVE_VSNPRINTF)
699 (void) vsnprintf(message,MaxTextExtent,format,warning);
700#else
701 (void) vsprintf(message,format,warning);
702#endif
703 (void) ConcatenateMagickString(message,".",MaxTextExtent);
704 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
705 if (exception != (ExceptionInfo *) NULL)
706 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
707 message,"`%s'",module);
708}
709
710static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
711{
712 tsize_t
713 count;
714
715 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
716 (unsigned char *) data);
717 return(count);
718}
719
720#if defined(__cplusplus) || defined(c_plusplus)
721}
722#endif
723
724static Image *ReadTIFFImage(const ImageInfo *image_info,
725 ExceptionInfo *exception)
726{
727 typedef enum
728 {
729 ReadSingleSampleMethod,
730 ReadRGBAMethod,
731 ReadCMYKAMethod,
732 ReadStripMethod,
733 ReadTileMethod,
734 ReadGenericMethod
735 } TIFFMethodType;
736
737 const char
738 *option;
739
740 float
741 *chromaticity,
742 x_position,
743 y_position,
744 x_resolution,
745 y_resolution;
746
747 Image
748 *image;
749
cristybb503372010-05-27 20:51:26 +0000750 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000751 y;
752
753 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000754 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000755 debug,
756 status;
757
758 MagickSizeType
759 number_pixels;
760
761 QuantumInfo
762 *quantum_info;
763
764 QuantumType
765 quantum_type;
766
cristybb503372010-05-27 20:51:26 +0000767 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000768 i;
769
770 size_t
771 length,
772 pad;
773
774 TIFF
775 *tiff;
776
777 TIFFErrorHandler
778 error_handler,
779 warning_handler;
780
781 TIFFMethodType
782 method;
783
784 uint16
785 compress_tag,
786 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000787 endian,
cristy3ed852e2009-09-05 21:47:34 +0000788 extra_samples,
789 interlace,
790 max_sample_value,
791 min_sample_value,
792 orientation,
793 pages,
794 photometric,
795 *sample_info,
796 sample_format,
797 samples_per_pixel,
798 units,
799 value;
800
801 uint32
802 height,
803 rows_per_strip,
804 width;
805
806 unsigned char
807 *pixels;
808
cristyf2687ca2010-06-29 16:32:38 +0000809 size_t
810 lsb_first;
811
cristy3ed852e2009-09-05 21:47:34 +0000812 /*
813 Open image.
814 */
815 assert(image_info != (const ImageInfo *) NULL);
816 assert(image_info->signature == MagickSignature);
817 if (image_info->debug != MagickFalse)
818 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
819 image_info->filename);
820 assert(exception != (ExceptionInfo *) NULL);
821 assert(exception->signature == MagickSignature);
822 image=AcquireImage(image_info);
823 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
824 if (status == MagickFalse)
825 {
826 image=DestroyImageList(image);
827 return((Image *) NULL);
828 }
829 (void) MagickSetThreadValue(tiff_exception,exception);
830 error_handler=TIFFSetErrorHandler(TIFFErrors);
831 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
cristy1630f7f2011-02-18 01:10:02 +0000832 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
cristy3ed852e2009-09-05 21:47:34 +0000833 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
834 TIFFUnmapBlob);
835 if (tiff == (TIFF *) NULL)
836 {
837 (void) TIFFSetWarningHandler(warning_handler);
838 (void) TIFFSetErrorHandler(error_handler);
839 image=DestroyImageList(image);
840 return((Image *) NULL);
841 }
842 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +0000843 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +0000844 if (image_info->number_scenes != 0)
845 {
846 /*
847 Generate blank images for subimage specification (e.g. image.tif[4].
848 */
cristybb503372010-05-27 20:51:26 +0000849 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000850 {
851 (void) TIFFReadDirectory(tiff);
852 AcquireNextImage(image_info,image);
853 if (GetNextImageInList(image) == (Image *) NULL)
854 {
855 image=DestroyImageList(image);
856 return((Image *) NULL);
857 }
858 image=SyncNextImageInList(image);
859 }
860 }
861 do
862 {
863 if (0 && (image_info->verbose != MagickFalse))
864 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristy6b032822010-06-29 16:52:32 +0000865 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
866 "lsb" : "msb");
cristy3ed852e2009-09-05 21:47:34 +0000867 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000872 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
873 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
874 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
cristyae1319c2011-01-19 20:18:53 +0000875 if (sample_format == SAMPLEFORMAT_IEEEFP)
876 (void) SetImageProperty(image,"quantum:format","floating-point");
cristy3ed852e2009-09-05 21:47:34 +0000877 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
878 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
879 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
880 switch (photometric)
881 {
882 case PHOTOMETRIC_MINISBLACK:
883 {
884 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
885 break;
886 }
887 case PHOTOMETRIC_MINISWHITE:
888 {
889 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
890 break;
891 }
892 case PHOTOMETRIC_PALETTE:
893 {
894 (void) SetImageProperty(image,"tiff:photometric","palette");
895 break;
896 }
897 case PHOTOMETRIC_RGB:
898 {
899 (void) SetImageProperty(image,"tiff:photometric","RGB");
900 break;
901 }
902 case PHOTOMETRIC_CIELAB:
903 {
904 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
905 break;
906 }
907 case PHOTOMETRIC_SEPARATED:
908 {
909 (void) SetImageProperty(image,"tiff:photometric","separated");
910 break;
911 }
cristy79e5dad2010-09-16 19:48:33 +0000912 case PHOTOMETRIC_YCBCR:
913 {
914 (void) SetImageProperty(image,"tiff:photometric","YCBCR");
915 break;
916 }
cristy3ed852e2009-09-05 21:47:34 +0000917 default:
918 {
919 (void) SetImageProperty(image,"tiff:photometric","unknown");
920 break;
921 }
922 }
923 if (image->debug != MagickFalse)
924 {
925 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
926 (unsigned int) width,(unsigned int) height);
927 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
928 interlace);
929 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
930 "Bits per sample: %u",bits_per_sample);
931 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
932 "Min sample value: %u",min_sample_value);
933 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
934 "Max sample value: %u",max_sample_value);
935 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
936 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
937 }
cristybb503372010-05-27 20:51:26 +0000938 image->columns=(size_t) width;
939 image->rows=(size_t) height;
940 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +0000941 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +0000942 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
943 (double) image->depth);
cristyf2687ca2010-06-29 16:32:38 +0000944 lsb_first=1;
945 image->endian=MSBEndian;
946 if ((int) (*(char *) &lsb_first) != 0)
947 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +0000948 if (photometric == PHOTOMETRIC_SEPARATED)
949 image->colorspace=CMYKColorspace;
950 if (photometric == PHOTOMETRIC_CIELAB)
951 image->colorspace=LabColorspace;
952 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
953 &samples_per_pixel);
954 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
955 x_resolution=(float) image->x_resolution;
956 y_resolution=(float) image->y_resolution;
957 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
958 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
959 image->x_resolution=x_resolution;
960 image->y_resolution=y_resolution;
961 x_position=(float) image->page.x/x_resolution;
962 y_position=(float) image->page.y/y_resolution;
963 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
964 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +0000965 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
966 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000967 image->orientation=(OrientationType) orientation;
968 chromaticity=(float *) NULL;
969 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
970 if (chromaticity != (float *) NULL)
971 {
972 image->chromaticity.white_point.x=chromaticity[0];
973 image->chromaticity.white_point.y=chromaticity[1];
974 }
975 chromaticity=(float *) NULL;
976 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
977 if (chromaticity != (float *) NULL)
978 {
979 image->chromaticity.red_primary.x=chromaticity[0];
980 image->chromaticity.red_primary.y=chromaticity[1];
981 image->chromaticity.green_primary.x=chromaticity[2];
982 image->chromaticity.green_primary.y=chromaticity[3];
983 image->chromaticity.blue_primary.x=chromaticity[4];
984 image->chromaticity.blue_primary.y=chromaticity[5];
985 }
986 TIFFGetProperties(tiff,image);
987 option=GetImageOption(image_info,"tiff:exif");
988 if ((option != (const char *) NULL) &&
989 (IsMagickTrue(option) != MagickFalse))
990 TIFFGetEXIFProperties(tiff,image);
991 TIFFGetProfiles(tiff,image);
992 /*
993 Allocate memory for the image and pixel buffer.
994 */
995#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
996 if ((compress_tag != COMPRESSION_NONE) &&
997 (TIFFIsCODECConfigured(compress_tag) == 0))
998 {
999 TIFFClose(tiff);
1000 ThrowReaderException(CoderError,"CompressNotSupported");
1001 }
1002#endif
1003 switch (compress_tag)
1004 {
1005 case COMPRESSION_NONE: image->compression=NoCompression; break;
1006 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1007 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1008 case COMPRESSION_JPEG:
1009 {
1010 image->compression=JPEGCompression;
1011#if defined(JPEG_SUPPORT)
1012 {
1013 char
1014 sampling_factor[MaxTextExtent];
1015
1016 uint16
1017 horizontal,
1018 vertical;
1019
1020 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1021 &horizontal,&vertical);
1022 (void) FormatMagickString(sampling_factor,MaxTextExtent,"%dx%d",
1023 horizontal,vertical);
1024 (void) SetImageProperty(image,"jpeg:sampling-factor",
1025 sampling_factor);
1026 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1027 "Sampling Factors: %s",sampling_factor);
1028 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1029 {
1030 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1031 JPEGCOLORMODE_RGB);
1032 photometric=PHOTOMETRIC_RGB;
1033 }
1034 }
1035#endif
1036 break;
1037 }
1038 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001039#if defined(COMPRESSION_LZMA)
1040 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1041#endif
cristy3ed852e2009-09-05 21:47:34 +00001042 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1043 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1044 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1045 default: image->compression=RLECompression; break;
1046 }
1047 quantum_info=AcquireQuantumInfo(image_info,image);
1048 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001049 {
1050 TIFFClose(tiff);
1051 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1052 }
cristy3ed852e2009-09-05 21:47:34 +00001053 if (sample_format == SAMPLEFORMAT_UINT)
1054 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1055 if (sample_format == SAMPLEFORMAT_INT)
1056 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1057 if (sample_format == SAMPLEFORMAT_IEEEFP)
1058 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1059 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001060 {
1061 TIFFClose(tiff);
1062 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1063 }
cristy3ed852e2009-09-05 21:47:34 +00001064 status=MagickTrue;
1065 switch (photometric)
1066 {
1067 case PHOTOMETRIC_MINISBLACK:
1068 {
1069 quantum_info->min_is_white=MagickFalse;
1070 break;
1071 }
1072 case PHOTOMETRIC_MINISWHITE:
1073 {
1074 quantum_info->min_is_white=MagickTrue;
1075 break;
1076 }
1077 default:
1078 break;
1079 }
cristy891dc792010-03-04 01:47:16 +00001080 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001081 extra_samples=0;
1082 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1083 &sample_info);
1084 if (extra_samples == 0)
1085 {
1086 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1087 image->matte=MagickTrue;
1088 }
1089 else
cristy6c207342010-02-19 17:49:50 +00001090 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001091 {
cristy7cf69862010-03-03 13:57:24 +00001092 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001093 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001094 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001095 }
cristy891dc792010-03-04 01:47:16 +00001096 option=GetImageOption(image_info,"tiff:alpha");
1097 if (option != (const char *) NULL)
1098 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1099 MagickFalse;
1100 if (image->matte != MagickFalse)
1101 (void) SetImageProperty(image,"tiff:alpha",
1102 associated_alpha != MagickFalse ? "associated" : "unassociated");
cristy3ed852e2009-09-05 21:47:34 +00001103 if ((photometric == PHOTOMETRIC_PALETTE) &&
1104 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1105 {
cristybb503372010-05-27 20:51:26 +00001106 size_t
cristy3ed852e2009-09-05 21:47:34 +00001107 colors;
1108
cristybb503372010-05-27 20:51:26 +00001109 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy3ed852e2009-09-05 21:47:34 +00001110 if (AcquireImageColormap(image,colors) == MagickFalse)
1111 {
1112 TIFFClose(tiff);
1113 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1114 }
1115 }
1116 if (units == RESUNIT_INCH)
1117 image->units=PixelsPerInchResolution;
1118 if (units == RESUNIT_CENTIMETER)
1119 image->units=PixelsPerCentimeterResolution;
1120 value=(unsigned short) image->scene;
1121 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1122 image->scene=value;
1123 if (image_info->ping != MagickFalse)
1124 {
1125 if (image_info->number_scenes != 0)
1126 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1127 break;
1128 goto next_tiff_frame;
1129 }
1130 method=ReadGenericMethod;
1131 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1132 {
1133 char
1134 value[MaxTextExtent];
1135
1136 method=ReadStripMethod;
1137 (void) FormatMagickString(value,MaxTextExtent,"%u",
1138 (unsigned int) rows_per_strip);
1139 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1140 }
1141 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1142 method=ReadRGBAMethod;
1143 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1144 method=ReadCMYKAMethod;
1145 if ((photometric != PHOTOMETRIC_RGB) &&
1146 (photometric != PHOTOMETRIC_CIELAB) &&
1147 (photometric != PHOTOMETRIC_SEPARATED))
1148 method=ReadGenericMethod;
1149 if (image->storage_class == PseudoClass)
1150 method=ReadSingleSampleMethod;
1151 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1152 (photometric == PHOTOMETRIC_MINISWHITE))
1153 method=ReadSingleSampleMethod;
1154 if ((photometric != PHOTOMETRIC_SEPARATED) &&
cristybc736662011-03-09 13:39:42 +00001155 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
cristy3ed852e2009-09-05 21:47:34 +00001156 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001157 if (image->compression == JPEGCompression)
1158 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001159 if (TIFFIsTiled(tiff) != MagickFalse)
1160 method=ReadTileMethod;
1161 quantum_type=RGBQuantum;
1162 pixels=GetQuantumPixels(quantum_info);
1163 switch (method)
1164 {
1165 case ReadSingleSampleMethod:
1166 {
1167 /*
1168 Convert TIFF image to PseudoClass MIFF image.
1169 */
1170 if ((image->storage_class == PseudoClass) &&
1171 (photometric == PHOTOMETRIC_PALETTE))
1172 {
cristybb503372010-05-27 20:51:26 +00001173 size_t
cristy3ed852e2009-09-05 21:47:34 +00001174 range;
1175
1176 uint16
1177 *blue_colormap,
1178 *green_colormap,
1179 *red_colormap;
1180
1181 /*
1182 Initialize colormap.
1183 */
1184 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1185 &green_colormap,&blue_colormap);
1186 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001187 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001188 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1189 (blue_colormap[i] >= 256))
1190 {
1191 range=65535;
1192 break;
1193 }
cristybb503372010-05-27 20:51:26 +00001194 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001195 {
cristyce70c172010-01-07 17:15:30 +00001196 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001197 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001198 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001199 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001200 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001201 blue_colormap[i])/range);
1202 }
1203 }
1204 quantum_type=IndexQuantum;
1205 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1206 if (image->matte != MagickFalse)
1207 {
1208 if (image->storage_class != PseudoClass)
1209 {
1210 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1211 GrayAlphaQuantum;
1212 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1213 }
1214 else
1215 {
1216 quantum_type=IndexAlphaQuantum;
1217 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1218 }
1219 }
1220 else
1221 if (image->storage_class != PseudoClass)
1222 {
1223 quantum_type=GrayQuantum;
1224 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1225 }
1226 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1227 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001228 {
1229 TIFFClose(tiff);
1230 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1231 }
cristy3ed852e2009-09-05 21:47:34 +00001232 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001233 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001234 {
1235 int
1236 status;
1237
1238 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001239 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001240
1241 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1242 if (status == -1)
1243 break;
1244 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1245 if (q == (PixelPacket *) NULL)
1246 break;
1247 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1248 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00001249 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00001250 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1251 break;
1252 if (image->previous == (Image *) NULL)
1253 {
cristycee97112010-05-28 00:44:52 +00001254 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1255 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001256 if (status == MagickFalse)
1257 break;
1258 }
1259 }
1260 break;
1261 }
1262 case ReadRGBAMethod:
1263 {
1264 /*
1265 Convert TIFF image to DirectClass MIFF image.
1266 */
1267 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1268 quantum_type=RGBQuantum;
1269 if (image->matte != MagickFalse)
1270 {
1271 quantum_type=RGBAQuantum;
1272 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1273 }
1274 if (image->colorspace == CMYKColorspace)
1275 {
1276 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1277 quantum_type=CMYKQuantum;
1278 if (image->matte != MagickFalse)
1279 {
1280 quantum_type=CMYKAQuantum;
1281 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1282 }
1283 }
1284 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1285 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001286 {
1287 TIFFClose(tiff);
1288 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1289 }
cristy3ed852e2009-09-05 21:47:34 +00001290 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001291 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001292 {
1293 int
1294 status;
1295
1296 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001297 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001298
1299 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1300 if (status == -1)
1301 break;
1302 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1303 if (q == (PixelPacket *) NULL)
1304 break;
cristy0b334892010-03-15 02:26:46 +00001305 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1306 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001307 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1308 break;
1309 if (image->previous == (Image *) NULL)
1310 {
cristycee97112010-05-28 00:44:52 +00001311 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1312 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001313 if (status == MagickFalse)
1314 break;
1315 }
1316 }
1317 break;
1318 }
1319 case ReadCMYKAMethod:
1320 {
1321 /*
1322 Convert TIFF image to DirectClass MIFF image.
1323 */
cristybb503372010-05-27 20:51:26 +00001324 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001325 {
cristybb503372010-05-27 20:51:26 +00001326 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001327 {
cristy6d9f12f2009-11-03 14:50:26 +00001328 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001329 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001330
cristy3ed852e2009-09-05 21:47:34 +00001331 int
1332 status;
1333
1334 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1335 pixels);
1336 if (status == -1)
1337 break;
1338 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1339 if (q == (PixelPacket *) NULL)
1340 break;
1341 if (image->colorspace != CMYKColorspace)
1342 switch (i)
1343 {
1344 case 0: quantum_type=RedQuantum; break;
1345 case 1: quantum_type=GreenQuantum; break;
1346 case 2: quantum_type=BlueQuantum; break;
1347 case 3: quantum_type=AlphaQuantum; break;
1348 default: quantum_type=UndefinedQuantum; break;
1349 }
cristy6d9f12f2009-11-03 14:50:26 +00001350 else
cristy3ed852e2009-09-05 21:47:34 +00001351 switch (i)
1352 {
1353 case 0: quantum_type=CyanQuantum; break;
1354 case 1: quantum_type=MagentaQuantum; break;
1355 case 2: quantum_type=YellowQuantum; break;
1356 case 3: quantum_type=BlackQuantum; break;
1357 case 4: quantum_type=AlphaQuantum; break;
1358 default: quantum_type=UndefinedQuantum; break;
1359 }
1360 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1361 quantum_type,pixels,exception);
1362 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1363 break;
1364 }
1365 if (image->previous == (Image *) NULL)
1366 {
cristycee97112010-05-28 00:44:52 +00001367 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1368 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001369 if (status == MagickFalse)
1370 break;
1371 }
1372 }
1373 break;
1374 }
1375 case ReadStripMethod:
1376 {
1377 register unsigned char
1378 *p;
1379
1380 /*
1381 Convert stripped TIFF image to DirectClass MIFF image.
1382 */
1383 i=0;
1384 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001385 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001386 {
cristybb503372010-05-27 20:51:26 +00001387 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001388 x;
1389
1390 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001391 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001392
1393 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1394 if (q == (PixelPacket *) NULL)
1395 break;
1396 if (i == 0)
1397 {
1398 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1399 break;
cristyeaedf062010-05-29 22:36:02 +00001400 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1401 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001402 }
1403 i--;
cristy3695ef82011-04-13 00:03:42 +00001404 p=((uint32 *) pixels)+image->columns*i;
cristybb503372010-05-27 20:51:26 +00001405 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001406 {
1407 q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
1408 q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
1409 q->blue=ScaleCharToQuantum((unsigned char) (TIFFGetB(*p)));
1410 if (image->matte != MagickFalse)
1411 q->opacity=ScaleCharToQuantum((unsigned char) (TIFFGetA(*p)));
1412 p++;
1413 q++;
1414 }
1415 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1416 break;
1417 if (image->previous == (Image *) NULL)
1418 {
cristycee97112010-05-28 00:44:52 +00001419 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1420 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001421 if (status == MagickFalse)
1422 break;
1423 }
1424 }
1425 break;
1426 }
1427 case ReadTileMethod:
1428 {
1429 register uint32
1430 *p;
1431
1432 uint32
1433 *tile_pixels,
1434 columns,
1435 rows;
1436
cristybb503372010-05-27 20:51:26 +00001437 size_t
cristy3ed852e2009-09-05 21:47:34 +00001438 number_pixels;
1439
1440 /*
1441 Convert tiled TIFF image to DirectClass MIFF image.
1442 */
1443 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1444 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1445 {
1446 TIFFClose(tiff);
1447 ThrowReaderException(CoderError,"ImageIsNotTiled");
1448 }
1449 (void) SetImageStorageClass(image,DirectClass);
1450 number_pixels=columns*rows;
cristyda16f162011-02-19 23:52:17 +00001451 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
cristy3ed852e2009-09-05 21:47:34 +00001452 sizeof(*tile_pixels));
1453 if (tile_pixels == (uint32 *) NULL)
1454 {
1455 TIFFClose(tiff);
1456 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1457 }
cristybb503372010-05-27 20:51:26 +00001458 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001459 {
1460 PixelPacket
1461 *tile;
1462
cristybb503372010-05-27 20:51:26 +00001463 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001464 x;
1465
1466 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001467 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001468
cristybb503372010-05-27 20:51:26 +00001469 size_t
cristy3ed852e2009-09-05 21:47:34 +00001470 columns_remaining,
1471 rows_remaining;
1472
1473 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001474 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001475 rows_remaining=rows;
1476 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1477 exception);
1478 if (tile == (PixelPacket *) NULL)
1479 break;
cristybb503372010-05-27 20:51:26 +00001480 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001481 {
cristybb503372010-05-27 20:51:26 +00001482 size_t
cristy3ed852e2009-09-05 21:47:34 +00001483 column,
1484 row;
1485
1486 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1487 break;
1488 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001489 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001490 columns_remaining=columns;
1491 p=tile_pixels+(rows-rows_remaining)*columns;
1492 q=tile+(image->columns*(rows_remaining-1)+x);
1493 for (row=rows_remaining; row > 0; row--)
1494 {
1495 if (image->matte != MagickFalse)
1496 for (column=columns_remaining; column > 0; column--)
1497 {
1498 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1499 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1500 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1501 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1502 (unsigned char) TIFFGetA(*p)));
1503 q++;
1504 p++;
1505 }
1506 else
1507 for (column=columns_remaining; column > 0; column--)
1508 {
1509 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1510 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1511 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1512 q++;
1513 p++;
1514 }
1515 p+=columns-columns_remaining;
1516 q-=(image->columns+columns_remaining);
1517 }
1518 }
1519 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1520 break;
1521 if (image->previous == (Image *) NULL)
1522 {
cristycee97112010-05-28 00:44:52 +00001523 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1524 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001525 if (status == MagickFalse)
1526 break;
1527 }
1528 }
1529 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1530 break;
1531 }
1532 case ReadGenericMethod:
1533 default:
1534 {
1535 register uint32
1536 *p;
1537
1538 uint32
1539 *pixels;
1540
1541 /*
1542 Convert TIFF image to DirectClass MIFF image.
1543 */
1544 number_pixels=(MagickSizeType) image->columns*image->rows;
1545 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1546 (number_pixels*sizeof(uint32))))
1547 {
1548 TIFFClose(tiff);
1549 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1550 }
1551 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1552 sizeof(uint32));
1553 if (pixels == (uint32 *) NULL)
1554 {
1555 TIFFClose(tiff);
1556 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1557 }
1558 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1559 (uint32) image->rows,(uint32 *) pixels,0);
1560 /*
1561 Convert image to DirectClass pixel packets.
1562 */
1563 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001564 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001565 {
cristybb503372010-05-27 20:51:26 +00001566 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001567 x;
1568
1569 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001570 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001571
1572 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1573 if (q == (PixelPacket *) NULL)
1574 break;
1575 q+=image->columns-1;
cristybb503372010-05-27 20:51:26 +00001576 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001577 {
1578 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1579 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1580 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1581 if (image->matte != MagickFalse)
cristycc768f42010-08-19 16:00:09 +00001582 q->opacity=(Quantum) (QuantumRange-
1583 ScaleCharToQuantum((unsigned char) TIFFGetA(*p)));
cristy3ed852e2009-09-05 21:47:34 +00001584 p--;
1585 q--;
1586 }
1587 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1588 break;
1589 if (image->previous == (Image *) NULL)
1590 {
cristycee97112010-05-28 00:44:52 +00001591 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1592 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001593 if (status == MagickFalse)
1594 break;
1595 }
1596 }
1597 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1598 break;
1599 }
1600 }
1601 SetQuantumImageType(image,quantum_type);
1602 next_tiff_frame:
1603 if ((photometric == PHOTOMETRIC_LOGL) ||
1604 (photometric == PHOTOMETRIC_MINISBLACK) ||
1605 (photometric == PHOTOMETRIC_MINISWHITE))
1606 {
1607 image->type=GrayscaleType;
1608 if (bits_per_sample == 1)
1609 image->type=BilevelType;
1610 }
1611 if (image->storage_class == PseudoClass)
1612 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001613 image->endian=MSBEndian;
1614 if (endian == FILLORDER_LSB2MSB)
1615 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001616 if ((photometric == PHOTOMETRIC_LOGL) ||
1617 (photometric == PHOTOMETRIC_MINISBLACK) ||
1618 (photometric == PHOTOMETRIC_MINISWHITE))
1619 {
1620 image->type=GrayscaleType;
1621 if (bits_per_sample == 1)
1622 image->type=BilevelType;
1623 }
1624 /*
1625 Proceed to next image.
1626 */
1627 if (image_info->number_scenes != 0)
1628 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1629 break;
1630 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1631 if (status == MagickTrue)
1632 {
1633 /*
1634 Allocate next image structure.
1635 */
1636 AcquireNextImage(image_info,image);
1637 if (GetNextImageInList(image) == (Image *) NULL)
1638 {
1639 image=DestroyImageList(image);
1640 return((Image *) NULL);
1641 }
1642 image=SyncNextImageInList(image);
1643 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1644 image->scene);
1645 if (status == MagickFalse)
1646 break;
1647 }
1648 quantum_info=DestroyQuantumInfo(quantum_info);
1649 } while (status == MagickTrue);
1650 (void) TIFFSetWarningHandler(warning_handler);
1651 (void) TIFFSetErrorHandler(error_handler);
1652 TIFFClose(tiff);
1653 return(GetFirstImageInList(image));
1654}
1655#endif
1656
1657/*
1658%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1659% %
1660% %
1661% %
1662% R e g i s t e r T I F F I m a g e %
1663% %
1664% %
1665% %
1666%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1667%
1668% RegisterTIFFImage() adds properties for the TIFF image format to
1669% the list of supported formats. The properties include the image format
1670% tag, a method to read and/or write the format, whether the format
1671% supports the saving of more than one frame to the same file or blob,
1672% whether the format supports native in-memory I/O, and a brief
1673% description of the format.
1674%
1675% The format of the RegisterTIFFImage method is:
1676%
cristybb503372010-05-27 20:51:26 +00001677% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001678%
1679*/
cristybb503372010-05-27 20:51:26 +00001680ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001681{
1682#define TIFFDescription "Tagged Image File Format"
1683
1684 char
1685 version[MaxTextExtent];
1686
1687 MagickInfo
1688 *entry;
cristy6b032822010-06-29 16:52:32 +00001689
cristy18b17442009-10-25 18:36:48 +00001690 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1691 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001692 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001693 if (instantiate_key == MagickFalse)
1694 {
1695 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1696 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1697 instantiate_key=MagickTrue;
1698 }
cristyf84a1932010-01-03 18:00:18 +00001699 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001700 *version='\0';
1701#if defined(TIFF_VERSION)
1702 (void) FormatMagickString(version,MaxTextExtent,"%d",TIFF_VERSION);
1703#endif
1704#if defined(MAGICKCORE_TIFF_DELEGATE)
1705 {
1706 const char
1707 *p;
1708
cristybb503372010-05-27 20:51:26 +00001709 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001710 i;
1711
1712 p=TIFFGetVersion();
1713 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1714 version[i]=(*p++);
1715 version[i]='\0';
1716 }
1717#endif
1718
cristy3d7f8062009-09-24 20:45:35 +00001719 entry=SetMagickInfo("GROUP4");
1720#if defined(MAGICKCORE_TIFF_DELEGATE)
1721 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1722 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1723#endif
1724 entry->raw=MagickTrue;
1725 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001726 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001727 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001728 entry->seekable_stream=MagickTrue;
1729 entry->thread_support=NoThreadSupport;
1730 entry->description=ConstantString("Raw CCITT Group4");
1731 entry->module=ConstantString("TIFF");
1732 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001733 entry=SetMagickInfo("PTIF");
1734#if defined(MAGICKCORE_TIFF_DELEGATE)
1735 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1736 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1737#endif
1738 entry->endian_support=MagickTrue;
1739 entry->seekable_stream=MagickTrue;
1740 entry->thread_support=NoThreadSupport;
1741 entry->description=ConstantString("Pyramid encoded TIFF");
1742 entry->module=ConstantString("TIFF");
1743 (void) RegisterMagickInfo(entry);
1744 entry=SetMagickInfo("TIF");
1745#if defined(MAGICKCORE_TIFF_DELEGATE)
1746 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1747 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1748#endif
1749 entry->endian_support=MagickTrue;
1750 entry->seekable_stream=MagickTrue;
1751 entry->stealth=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("TIFF");
1759#if defined(MAGICKCORE_TIFF_DELEGATE)
1760 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1761 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1762#endif
1763 entry->magick=(IsImageFormatHandler *) IsTIFF;
1764 entry->endian_support=MagickTrue;
1765 entry->seekable_stream=MagickTrue;
1766 entry->thread_support=NoThreadSupport;
1767 entry->description=ConstantString(TIFFDescription);
1768 if (*version != '\0')
1769 entry->version=ConstantString(version);
1770 entry->module=ConstantString("TIFF");
1771 (void) RegisterMagickInfo(entry);
1772 entry=SetMagickInfo("TIFF64");
1773#if defined(TIFF_VERSION_BIG)
1774 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1775 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1776#endif
1777 entry->adjoin=MagickFalse;
1778 entry->endian_support=MagickTrue;
1779 entry->seekable_stream=MagickTrue;
1780 entry->thread_support=NoThreadSupport;
1781 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1782 if (*version != '\0')
1783 entry->version=ConstantString(version);
1784 entry->module=ConstantString("TIFF");
1785 (void) RegisterMagickInfo(entry);
1786 return(MagickImageCoderSignature);
1787}
1788
1789/*
1790%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1791% %
1792% %
1793% %
1794% U n r e g i s t e r T I F F I m a g e %
1795% %
1796% %
1797% %
1798%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1799%
1800% UnregisterTIFFImage() removes format registrations made by the TIFF module
1801% from the list of supported formats.
1802%
1803% The format of the UnregisterTIFFImage method is:
1804%
1805% UnregisterTIFFImage(void)
1806%
1807*/
1808ModuleExport void UnregisterTIFFImage(void)
1809{
cristy3d7f8062009-09-24 20:45:35 +00001810 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001811 (void) UnregisterMagickInfo("PTIF");
1812 (void) UnregisterMagickInfo("TIF");
1813 (void) UnregisterMagickInfo("TIFF");
1814 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001815 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1816 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001817 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001818 if (instantiate_key != MagickFalse)
1819 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1820 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1821 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001822 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001823 DestroySemaphoreInfo(&tiff_semaphore);
1824}
1825
1826#if defined(MAGICKCORE_TIFF_DELEGATE)
1827/*
1828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1829% %
1830% %
1831% %
cristy3d7f8062009-09-24 20:45:35 +00001832% W r i t e G R O U P 4 I m a g e %
1833% %
1834% %
1835% %
1836%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1837%
1838% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1839%
1840% The format of the WriteGROUP4Image method is:
1841%
1842% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1843% Image *image)
1844%
1845% A description of each parameter follows:
1846%
1847% o image_info: the image info.
1848%
1849% o image: The image.
1850%
1851*/
1852static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1853 Image *image)
1854{
1855 char
1856 filename[MaxTextExtent];
1857
1858 FILE
1859 *file;
1860
1861 Image
1862 *huffman_image;
1863
1864 ImageInfo
1865 *write_info;
1866
1867 int
1868 unique_file;
1869
1870 MagickBooleanType
1871 status;
1872
cristybb503372010-05-27 20:51:26 +00001873 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001874 i;
1875
1876 ssize_t
1877 count;
1878
1879 TIFF
1880 *tiff;
1881
cristy94c8fe42009-10-06 01:57:36 +00001882 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001883 *byte_count,
1884 strip_size;
1885
1886 unsigned char
1887 *buffer;
1888
1889 /*
1890 Write image as CCITT Group4 TIFF image to a temporary file.
1891 */
1892 assert(image_info != (const ImageInfo *) NULL);
1893 assert(image_info->signature == MagickSignature);
1894 assert(image != (Image *) NULL);
1895 assert(image->signature == MagickSignature);
1896 if (image->debug != MagickFalse)
1897 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1898 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1899 if (status == MagickFalse)
1900 return(status);
1901 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1902 if (huffman_image == (Image *) NULL)
1903 {
1904 (void) CloseBlob(image);
1905 return(MagickFalse);
1906 }
cristy94c8fe42009-10-06 01:57:36 +00001907 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001908 file=(FILE *) NULL;
1909 unique_file=AcquireUniqueFileResource(filename);
1910 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001911 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001912 if ((unique_file == -1) || (file == (FILE *) NULL))
1913 {
1914 ThrowFileException(&image->exception,FileOpenError,
1915 "UnableToCreateTemporaryFile",filename);
1916 return(MagickFalse);
1917 }
1918 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1919 filename);
1920 (void) SetImageType(huffman_image,BilevelType);
cristy14efd992009-09-26 23:46:03 +00001921 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001922 SetImageInfoFile(write_info,file);
1923 write_info->compression=Group4Compression;
1924 write_info->type=BilevelType;
1925 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1926 status=WriteTIFFImage(write_info,huffman_image);
1927 (void) fflush(file);
1928 write_info=DestroyImageInfo(write_info);
1929 if (status == MagickFalse)
1930 {
1931 InheritException(&image->exception,&huffman_image->exception);
1932 huffman_image=DestroyImage(huffman_image);
1933 (void) fclose(file);
1934 (void) RelinquishUniqueFileResource(filename);
1935 return(MagickFalse);
1936 }
1937 tiff=TIFFOpen(filename,"rb");
1938 if (tiff == (TIFF *) NULL)
1939 {
1940 huffman_image=DestroyImage(huffman_image);
1941 (void) fclose(file);
1942 (void) RelinquishUniqueFileResource(filename);
1943 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1944 image_info->filename);
1945 return(MagickFalse);
1946 }
1947 /*
1948 Allocate raw strip buffer.
1949 */
cristy94c8fe42009-10-06 01:57:36 +00001950 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1951 {
1952 TIFFClose(tiff);
1953 huffman_image=DestroyImage(huffman_image);
1954 (void) fclose(file);
1955 (void) RelinquishUniqueFileResource(filename);
1956 return(MagickFalse);
1957 }
cristy3d7f8062009-09-24 20:45:35 +00001958 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00001959 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001960 if (byte_count[i] > strip_size)
1961 strip_size=byte_count[i];
1962 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1963 sizeof(*buffer));
1964 if (buffer == (unsigned char *) NULL)
1965 {
1966 TIFFClose(tiff);
1967 huffman_image=DestroyImage(huffman_image);
1968 (void) fclose(file);
1969 (void) RelinquishUniqueFileResource(filename);
1970 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1971 image_info->filename);
1972 }
1973 /*
1974 Compress runlength encoded to 2D Huffman pixels.
1975 */
cristybb503372010-05-27 20:51:26 +00001976 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001977 {
cristy94c8fe42009-10-06 01:57:36 +00001978 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00001979 if (WriteBlob(image,(size_t) count,buffer) != count)
1980 status=MagickFalse;
1981 }
1982 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1983 TIFFClose(tiff);
1984 huffman_image=DestroyImage(huffman_image);
1985 (void) fclose(file);
1986 (void) RelinquishUniqueFileResource(filename);
1987 (void) CloseBlob(image);
1988 return(status);
1989}
1990#endif
1991
1992#if defined(MAGICKCORE_TIFF_DELEGATE)
1993/*
1994%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1995% %
1996% %
1997% %
cristy3ed852e2009-09-05 21:47:34 +00001998% W r i t e P T I F I m a g e %
1999% %
2000% %
2001% %
2002%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2003%
2004% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2005% format.
2006%
2007% The format of the WritePTIFImage method is:
2008%
2009% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2010% Image *image)
2011%
2012% A description of each parameter follows:
2013%
2014% o image_info: the image info.
2015%
2016% o image: The image.
2017%
2018*/
2019static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2020 Image *image)
2021{
2022 Image
2023 *images,
2024 *next,
2025 *pyramid_image;
2026
2027 ImageInfo
2028 *write_info;
2029
2030 MagickBooleanType
2031 status;
2032
cristybb503372010-05-27 20:51:26 +00002033 size_t
cristy3ed852e2009-09-05 21:47:34 +00002034 columns,
2035 rows;
2036
2037 /*
2038 Create pyramid-encoded TIFF image.
2039 */
2040 images=NewImageList();
2041 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2042 {
2043 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2044 &image->exception));
2045 columns=next->columns;
2046 rows=next->rows;
2047 while ((columns > 64) && (rows > 64))
2048 {
2049 columns/=2;
2050 rows/=2;
2051 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2052 &image->exception);
2053 AppendImageToList(&images,pyramid_image);
2054 }
2055 }
2056 /*
2057 Write pyramid-encoded TIFF image.
2058 */
cristy58567532009-09-26 01:13:32 +00002059 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002060 *write_info->magick='\0';
2061 write_info->adjoin=MagickTrue;
2062 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2063 images=DestroyImageList(images);
2064 write_info=DestroyImageInfo(write_info);
2065 return(status);
2066}
2067#endif
2068
2069#if defined(MAGICKCORE_TIFF_DELEGATE)
2070/*
2071%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2072% %
2073% %
2074% W r i t e T I F F I m a g e %
2075% %
2076% %
2077% %
2078%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2079%
2080% WriteTIFFImage() writes an image in the Tagged image file format.
2081%
2082% The format of the WriteTIFFImage method is:
2083%
2084% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2085% Image *image)
2086%
2087% A description of each parameter follows:
2088%
2089% o image_info: the image info.
2090%
2091% o image: The image.
2092%
2093*/
2094
2095typedef struct _TIFFInfo
2096{
2097 RectangleInfo
2098 tile_geometry;
2099
2100 unsigned char
2101 *scanline,
2102 *scanlines,
2103 *pixels;
2104} TIFFInfo;
2105
2106static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2107{
2108 assert(tiff_info != (TIFFInfo *) NULL);
2109 if (tiff_info->scanlines != (unsigned char *) NULL)
2110 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2111 tiff_info->scanlines);
2112 if (tiff_info->pixels != (unsigned char *) NULL)
2113 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2114 tiff_info->pixels);
2115}
2116
2117static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2118 TIFFInfo *tiff_info)
2119{
2120 const char
2121 *option;
2122
2123 MagickStatusType
2124 flags;
2125
cristy79e5dad2010-09-16 19:48:33 +00002126 uint32
2127 tile_columns,
2128 tile_rows;
2129
cristy3ed852e2009-09-05 21:47:34 +00002130 assert(tiff_info != (TIFFInfo *) NULL);
2131 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2132 option=GetImageOption(image_info,"tiff:tile-geometry");
2133 if (option == (const char *) NULL)
2134 return(MagickTrue);
2135 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2136 if ((flags & HeightValue) == 0)
2137 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002138 tile_columns=(uint32) tiff_info->tile_geometry.width;
2139 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002140 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2141 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2142 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2143 tiff_info->tile_geometry.width=tile_columns;
2144 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002145 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002146 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002147 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002148 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002149 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2150 (tiff_info->pixels == (unsigned char *) NULL))
2151 {
2152 DestroyTIFFInfo(tiff_info);
2153 return(MagickFalse);
2154 }
2155 return(MagickTrue);
2156}
2157
cristybb503372010-05-27 20:51:26 +00002158static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002159 tsample_t sample,Image *image)
2160{
2161 int32
2162 status;
2163
cristybb503372010-05-27 20:51:26 +00002164 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002165 bytes_per_pixel,
2166 j,
2167 k,
2168 l;
2169
cristybb503372010-05-27 20:51:26 +00002170 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002171 i;
2172
2173 register unsigned char
2174 *p,
2175 *q;
2176
cristybb503372010-05-27 20:51:26 +00002177 size_t
cristy3ed852e2009-09-05 21:47:34 +00002178 number_tiles,
2179 tile_width;
2180
2181 if (TIFFIsTiled(tiff) == 0)
2182 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2183 /*
2184 Fill scanlines to tile height.
2185 */
cristybb503372010-05-27 20:51:26 +00002186 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002187 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2188 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002189 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2190 (tiff_info->tile_geometry.height-1)) && (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002191 return(0);
2192 /*
2193 Write tile to TIFF image.
2194 */
2195 status=0;
cristybb503372010-05-27 20:51:26 +00002196 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002197 tiff_info->tile_geometry.width);
2198 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2199 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002200 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002201 {
cristybb503372010-05-27 20:51:26 +00002202 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002203 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002204 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2205 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002206 {
2207 if (bytes_per_pixel == 0)
2208 {
2209 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2210 tiff_info->tile_geometry.width+k)/8);
2211 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2212 *q++=(*p++);
2213 continue;
2214 }
2215 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2216 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2217 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2218 for (l=0; l < bytes_per_pixel; l++)
2219 *q++=(*p++);
2220 }
cristydaff8092010-04-22 14:50:53 +00002221 if ((i*tiff_info->tile_geometry.width) != image->columns)
2222 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2223 tiff_info->tile_geometry.width),(uint32) ((row/
2224 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2225 sample);
cristy3ed852e2009-09-05 21:47:34 +00002226 if (status < 0)
2227 break;
2228 }
2229 return(status);
2230}
2231
2232static void TIFFSetProfiles(TIFF *tiff,Image *image)
2233{
2234 const char
2235 *name;
2236
2237 const StringInfo
2238 *profile;
2239
2240 if (image->profiles == (void *) NULL)
2241 return;
2242 ResetImageProfileIterator(image);
2243 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2244 {
2245 profile=GetImageProfile(image,name);
2246#if defined(TIFFTAG_XMLPACKET)
2247 if (LocaleCompare(name,"xmp") == 0)
2248 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2249 profile),GetStringInfoDatum(profile));
2250#endif
2251#if defined(TIFFTAG_ICCPROFILE)
2252 if (LocaleCompare(name,"icc") == 0)
2253 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2254 profile),GetStringInfoDatum(profile));
2255#endif
2256 if (LocaleCompare(name,"iptc") == 0)
2257 {
2258 size_t
2259 length;
2260
2261 StringInfo
2262 *iptc_profile;
2263
2264 iptc_profile=CloneStringInfo(profile);
2265 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2266 0x03);
2267 SetStringInfoLength(iptc_profile,length);
2268 if (TIFFIsByteSwapped(tiff))
2269 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002270 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002271 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2272 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2273 iptc_profile=DestroyStringInfo(iptc_profile);
2274 }
2275#if defined(TIFFTAG_PHOTOSHOP)
2276 if (LocaleCompare(name,"8bim") == 0)
2277 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2278 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2279#endif
2280 if (LocaleCompare(name,"tiff:37724") == 0)
2281 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2282 GetStringInfoDatum(profile));
2283 name=GetNextImageProfile(image);
2284 }
2285}
2286
2287static void TIFFSetProperties(TIFF *tiff,Image *image)
2288{
2289 const char
2290 *value;
2291
cristy15d8d212010-09-17 01:56:30 +00002292 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002293 value=GetImageProperty(image,"tiff:hostcomputer");
2294 if (value != (const char *) NULL)
2295 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2296 value=GetImageProperty(image,"tiff:artist");
2297 if (value != (const char *) NULL)
2298 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2299 value=GetImageProperty(image,"tiff:timestamp");
2300 if (value != (const char *) NULL)
2301 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2302 value=GetImageProperty(image,"tiff:make");
2303 if (value != (const char *) NULL)
2304 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2305 value=GetImageProperty(image,"tiff:model");
2306 if (value != (const char *) NULL)
2307 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristy15d8d212010-09-17 01:56:30 +00002308 value=GetImageProperty(image,"tiff:software");
2309 if (value != (const char *) NULL)
2310 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristy3ed852e2009-09-05 21:47:34 +00002311 value=GetImageProperty(image,"tiff:copyright");
2312 if (value != (const char *) NULL)
2313 (void) TIFFSetField(tiff,33432,value);
2314 value=GetImageProperty(image,"kodak-33423");
2315 if (value != (const char *) NULL)
2316 (void) TIFFSetField(tiff,33423,value);
2317 value=GetImageProperty(image,"kodak-36867");
2318 if (value != (const char *) NULL)
2319 (void) TIFFSetField(tiff,36867,value);
2320 value=GetImageProperty(image,"label");
2321 if (value != (const char *) NULL)
2322 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2323 value=GetImageProperty(image,"comment");
2324 if (value != (const char *) NULL)
2325 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2326}
2327
2328static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2329{
2330#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2331 const char
2332 *value;
2333
cristybb503372010-05-27 20:51:26 +00002334 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002335 i;
2336
2337 uint32
2338 offset;
2339
2340 /*
2341 Write EXIF properties.
2342 */
2343 offset=0;
2344 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2345 for (i=0; exif_info[i].tag != 0; i++)
2346 {
2347 value=GetImageProperty(image,exif_info[i].property);
2348 if (value == (const char *) NULL)
2349 continue;
2350 switch (exif_info[i].type)
2351 {
2352 case TIFF_ASCII:
2353 {
2354 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2355 break;
2356 }
2357 case TIFF_SHORT:
2358 {
2359 uint16
2360 shorty;
2361
cristyf2f27272009-12-17 14:48:46 +00002362 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002363 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2364 break;
2365 }
2366 case TIFF_LONG:
2367 {
2368 uint16
cristybb503372010-05-27 20:51:26 +00002369 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002370
cristybb503372010-05-27 20:51:26 +00002371 ssize_ty=(uint16) StringToLong(value);
2372 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002373 break;
2374 }
2375 case TIFF_RATIONAL:
2376 case TIFF_SRATIONAL:
2377 {
2378 float
2379 rational;
2380
cristyf2f27272009-12-17 14:48:46 +00002381 rational=StringToDouble(value);
cristy3ed852e2009-09-05 21:47:34 +00002382 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2383 break;
2384 }
2385 default:
2386 break;
2387 }
2388 }
2389 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2390#else
2391 (void) tiff;
2392 (void) image;
2393#endif
2394}
2395
2396static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2397 Image *image)
2398{
2399#if !defined(TIFFDefaultStripSize)
2400#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2401#endif
2402
2403 const char
2404 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002405 *option;
cristy3ed852e2009-09-05 21:47:34 +00002406
2407 CompressionType
2408 compression;
2409
cristy6b032822010-06-29 16:52:32 +00002410 EndianType
2411 endian_type;
2412
cristybb503372010-05-27 20:51:26 +00002413 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002414 y;
2415
2416 MagickBooleanType
2417 debug,
2418 status;
2419
2420 MagickOffsetType
2421 scene;
2422
2423 QuantumInfo
2424 *quantum_info;
2425
2426 QuantumType
2427 quantum_type;
2428
cristybb503372010-05-27 20:51:26 +00002429 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002430 i;
2431
2432 size_t
cristy5ecaba72011-02-18 02:05:32 +00002433 length,
2434 lsb_first;
cristy3ed852e2009-09-05 21:47:34 +00002435
2436 TIFF
2437 *tiff;
2438
2439 TIFFErrorHandler
2440 error_handler,
2441 warning_handler;
2442
2443 TIFFInfo
2444 tiff_info;
2445
2446 uint16
2447 bits_per_sample,
2448 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002449 endian,
cristy3ed852e2009-09-05 21:47:34 +00002450 photometric;
2451
2452 uint32
2453 rows_per_strip;
2454
2455 unsigned char
2456 *pixels;
2457
cristy3ed852e2009-09-05 21:47:34 +00002458 /*
2459 Open TIFF file.
2460 */
2461 assert(image_info != (const ImageInfo *) NULL);
2462 assert(image_info->signature == MagickSignature);
2463 assert(image != (Image *) NULL);
2464 assert(image->signature == MagickSignature);
2465 if (image->debug != MagickFalse)
2466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2467 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2468 if (status == MagickFalse)
2469 return(status);
2470 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2471 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2472 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002473 endian_type=UndefinedEndian;
2474 option=GetImageOption(image_info,"tiff:endian");
2475 if (option != (const char *) NULL)
2476 {
2477 if (LocaleNCompare(option,"msb",3) == 0)
2478 endian_type=MSBEndian;
2479 if (LocaleNCompare(option,"lsb",3) == 0)
2480 endian_type=LSBEndian;;
2481 }
2482 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002483 {
2484 case LSBEndian: mode="wl"; break;
2485 case MSBEndian: mode="wb"; break;
2486 default: mode="w"; break;
2487 }
2488#if defined(TIFF_VERSION_BIG)
2489 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002490 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002491 {
2492 case LSBEndian: mode="wl8"; break;
2493 case MSBEndian: mode="wb8"; break;
2494 default: mode="w8"; break;
2495 }
2496#endif
2497 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2498 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2499 TIFFUnmapBlob);
2500 if (tiff == (TIFF *) NULL)
2501 {
2502 (void) TIFFSetWarningHandler(warning_handler);
2503 (void) TIFFSetErrorHandler(error_handler);
2504 return(MagickFalse);
2505 }
2506 scene=0;
2507 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +00002508 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +00002509 do
2510 {
2511 /*
2512 Initialize TIFF fields.
2513 */
cristy5f1c1ff2010-12-23 21:38:06 +00002514 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002515 (image_info->type != OptimizeType))
2516 (void) SetImageType(image,image_info->type);
2517 quantum_info=AcquireQuantumInfo(image_info,image);
2518 if (quantum_info == (QuantumInfo *) NULL)
2519 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2520 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2521 (quantum_info->format == UndefinedQuantumFormat) &&
2522 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2523 {
2524 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2525 if (status == MagickFalse)
2526 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2527 }
2528 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2529 (GetPreviousImageInList(image) != (Image *) NULL))
2530 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2531 if ((image->columns != (uint32) image->columns) ||
2532 (image->rows != (uint32) image->rows))
2533 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2534 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2535 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2536 compression=image->compression;
2537 if (image_info->compression != UndefinedCompression)
2538 compression=image_info->compression;
2539 switch (compression)
2540 {
2541 case FaxCompression:
2542 {
2543 compress_tag=COMPRESSION_CCITTFAX3;
2544 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2545 break;
2546 }
2547 case Group4Compression:
2548 {
2549 compress_tag=COMPRESSION_CCITTFAX4;
2550 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2551 break;
2552 }
2553 case JPEGCompression:
2554 {
2555 compress_tag=COMPRESSION_JPEG;
2556 break;
2557 }
cristyfbb0ef02010-12-19 02:32:11 +00002558#if defined(COMPRESSION_LZMA)
2559 case LZMACompression:
2560 {
2561 compress_tag=COMPRESSION_LZMA;
2562 break;
2563 }
2564#endif
cristy3ed852e2009-09-05 21:47:34 +00002565 case LZWCompression:
2566 {
2567 compress_tag=COMPRESSION_LZW;
2568 break;
2569 }
2570 case RLECompression:
2571 {
2572 compress_tag=COMPRESSION_PACKBITS;
2573 break;
2574 }
2575 case ZipCompression:
2576 {
2577 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2578 break;
2579 }
2580 case NoCompression:
2581 default:
2582 {
2583 compress_tag=COMPRESSION_NONE;
2584 break;
2585 }
2586 }
cristy5b675872009-10-21 13:12:00 +00002587#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2588 if ((compress_tag != COMPRESSION_NONE) &&
2589 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002590 {
cristy5b675872009-10-21 13:12:00 +00002591 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2592 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002593 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002594 compress_tag=COMPRESSION_NONE;
2595 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002596 }
2597#else
2598 switch (compress_tag)
2599 {
2600#if defined(CCITT_SUPPORT)
2601 case COMPRESSION_CCITTFAX3:
2602 case COMPRESSION_CCITTFAX4:
2603#endif
2604#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2605 case COMPRESSION_JPEG:
2606#endif
cristyfbb0ef02010-12-19 02:32:11 +00002607#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2608 case COMPRESSION_LZMA:
2609#endif
cristy3ed852e2009-09-05 21:47:34 +00002610#if defined(LZW_SUPPORT)
2611 case COMPRESSION_LZW:
2612#endif
2613#if defined(PACKBITS_SUPPORT)
2614 case COMPRESSION_PACKBITS:
2615#endif
2616#if defined(ZIP_SUPPORT)
2617 case COMPRESSION_ADOBE_DEFLATE:
2618#endif
2619 case COMPRESSION_NONE:
2620 break;
2621 default:
2622 {
2623 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2624 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002625 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002626 compress_tag=COMPRESSION_NONE;
2627 compression=NoCompression;
2628 break;
2629 }
2630 }
2631#endif
2632 switch (compression)
2633 {
2634 case FaxCompression:
2635 case Group4Compression:
2636 {
2637 (void) SetImageType(image,BilevelType);
2638 break;
2639 }
2640 case JPEGCompression:
2641 {
2642 (void) SetImageStorageClass(image,DirectClass);
2643 (void) SetImageDepth(image,8);
2644 break;
2645 }
2646 default:
2647 break;
2648 }
cristy3ed852e2009-09-05 21:47:34 +00002649 if (image->colorspace == CMYKColorspace)
2650 {
2651 photometric=PHOTOMETRIC_SEPARATED;
2652 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2653 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2654 }
2655 else
2656 {
2657 /*
2658 Full color TIFF raster.
2659 */
2660 if (image->colorspace == LabColorspace)
2661 photometric=PHOTOMETRIC_CIELAB;
2662 else
2663 if (image->colorspace == YCbCrColorspace)
2664 {
2665 photometric=PHOTOMETRIC_YCBCR;
2666 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy79e5dad2010-09-16 19:48:33 +00002667 (void) SetImageStorageClass(image,DirectClass);
cristy3ed852e2009-09-05 21:47:34 +00002668 (void) SetImageDepth(image,8);
2669 }
2670 else
2671 {
2672 if (image->colorspace != RGBColorspace)
2673 (void) TransformImageColorspace(image,RGBColorspace);
2674 photometric=PHOTOMETRIC_RGB;
2675 }
2676 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2677 if ((image_info->type != TrueColorType) &&
2678 (image_info->type != TrueColorMatteType))
2679 {
cristy94c8fe42009-10-06 01:57:36 +00002680 if ((image_info->type != PaletteType) &&
2681 (IsGrayImage(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002682 {
cristy94c8fe42009-10-06 01:57:36 +00002683 photometric=(uint16) (quantum_info->min_is_white !=
2684 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2685 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002686 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002687 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy94c8fe42009-10-06 01:57:36 +00002688 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2689 {
2690 status=SetQuantumDepth(image,quantum_info,1);
2691 if (status == MagickFalse)
2692 ThrowWriterException(ResourceLimitError,
2693 "MemoryAllocationFailed");
2694 }
cristy3ed852e2009-09-05 21:47:34 +00002695 }
2696 else
cristy94c8fe42009-10-06 01:57:36 +00002697 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002698 {
cristybb503372010-05-27 20:51:26 +00002699 size_t
cristy94c8fe42009-10-06 01:57:36 +00002700 depth;
2701
2702 /*
2703 Colormapped TIFF raster.
2704 */
cristy3ed852e2009-09-05 21:47:34 +00002705 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002706 photometric=PHOTOMETRIC_PALETTE;
2707 depth=1;
2708 while ((GetQuantumRange(depth)+1) < image->colors)
2709 depth<<=1;
2710 status=SetQuantumDepth(image,quantum_info,depth);
2711 if (status == MagickFalse)
2712 ThrowWriterException(ResourceLimitError,
2713 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002714 }
2715 }
2716 }
cristyf2687ca2010-06-29 16:32:38 +00002717 switch (image->endian)
2718 {
2719 case LSBEndian:
2720 {
2721 endian=FILLORDER_LSB2MSB;
2722 break;
2723 }
2724 case MSBEndian:
2725 {
2726 endian=FILLORDER_MSB2LSB;
2727 break;
2728 }
2729 case UndefinedEndian:
2730 default:
2731 {
2732 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2733 break;
2734 }
2735 }
2736 lsb_first=1;
2737 image->endian=MSBEndian;
2738 if ((int) (*(char *) &lsb_first) != 0)
2739 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002740 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2741 (photometric != PHOTOMETRIC_MINISWHITE))
2742 {
2743 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002744 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002745 }
cristy0accf6e2009-10-01 13:14:28 +00002746 else
cristydaff8092010-04-22 14:50:53 +00002747 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2748 (photometric != PHOTOMETRIC_MINISWHITE))
2749 {
2750 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002751 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002752 }
cristy62e282b2010-06-29 01:27:13 +00002753 option=GetImageProperty(image,"tiff:fill-order");
2754 if (option != (const char *) NULL)
2755 {
2756 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002757 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002758 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002759 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002760 }
cristyf2687ca2010-06-29 16:32:38 +00002761 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2762 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002763 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2764 if (image->matte != MagickFalse)
2765 {
2766 uint16
2767 extra_samples,
2768 sample_info[1],
2769 samples_per_pixel;
2770
2771 /*
2772 TIFF has a matte channel.
2773 */
2774 extra_samples=1;
2775 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2776 option=GetImageProperty(image,"tiff:alpha");
2777 if ((option != (const char *) NULL) &&
2778 (LocaleCompare(option,"associated") == 0))
2779 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2780 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2781 &samples_per_pixel);
2782 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2783 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2784 &sample_info);
2785 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2786 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2787 }
2788 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2789 switch (quantum_info->format)
2790 {
2791 case FloatingPointQuantumFormat:
2792 {
2793 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2794 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2795 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2796 break;
2797 }
2798 case SignedQuantumFormat:
2799 {
2800 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2801 break;
2802 }
2803 case UnsignedQuantumFormat:
2804 {
2805 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2806 break;
2807 }
2808 default:
2809 break;
2810 }
cristy3ed852e2009-09-05 21:47:34 +00002811 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2812 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2813 if (photometric == PHOTOMETRIC_RGB)
2814 if ((image_info->interlace == PlaneInterlace) ||
2815 (image_info->interlace == PartitionInterlace))
2816 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2817 rows_per_strip=1;
2818 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002819 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2820 1);
cristy3ed852e2009-09-05 21:47:34 +00002821 option=GetImageOption(image_info,"tiff:rows-per-strip");
2822 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002823 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002824 switch (compress_tag)
2825 {
2826 case COMPRESSION_JPEG:
2827 {
2828#if defined(JPEG_SUPPORT)
2829 const char
2830 *sampling_factor;
2831
2832 GeometryInfo
2833 geometry_info;
2834
2835 MagickStatusType
2836 flags;
2837
2838 rows_per_strip+=(16-(rows_per_strip % 16));
2839 if (image->quality != 0)
2840 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2841 if (image_info->quality != UndefinedCompressionQuality)
2842 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2843 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2844 if (image->colorspace == RGBColorspace)
2845 {
cristy949bf5b2010-05-08 02:47:03 +00002846 const char
2847 *value;
2848
cristy3ed852e2009-09-05 21:47:34 +00002849 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2850 sampling_factor=(const char *) NULL;
2851 value=GetImageProperty(image,"jpeg:sampling-factor");
2852 if (value != (char *) NULL)
2853 {
2854 sampling_factor=value;
2855 if (image->debug != MagickFalse)
2856 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2857 " Input sampling-factors=%s",sampling_factor);
2858 }
2859 if (image_info->sampling_factor != (char *) NULL)
2860 sampling_factor=image_info->sampling_factor;
2861 if (sampling_factor != (const char *) NULL)
2862 {
2863 flags=ParseGeometry(sampling_factor,&geometry_info);
2864 if ((flags & SigmaValue) == 0)
2865 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002866 if (image->colorspace == YCbCrColorspace)
2867 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2868 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00002869 }
2870 }
2871 if (bits_per_sample == 12)
2872 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2873#endif
2874 break;
2875 }
2876 case COMPRESSION_ADOBE_DEFLATE:
2877 {
cristyf6fe0a12010-05-30 00:44:47 +00002878 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002879 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2880 &bits_per_sample);
2881 if (((photometric == PHOTOMETRIC_RGB) ||
2882 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2883 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002884 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00002885 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2886 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00002887 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00002888 break;
2889 }
2890 case COMPRESSION_CCITTFAX3:
2891 {
2892 /*
2893 Byte-aligned EOL.
2894 */
cristyeaedf062010-05-29 22:36:02 +00002895 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002896 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2897 break;
2898 }
2899 case COMPRESSION_CCITTFAX4:
2900 {
cristyeaedf062010-05-29 22:36:02 +00002901 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002902 break;
2903 }
cristyef8f26b2010-12-19 20:29:16 +00002904#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2905 case COMPRESSION_LZMA:
2906 {
2907 if (((photometric == PHOTOMETRIC_RGB) ||
2908 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2909 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2910 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2911 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
2912 image_info->quality == UndefinedCompressionQuality ? 7 :
2913 MagickMin((ssize_t) image_info->quality/10,9)));
2914 break;
2915 }
2916#endif
cristy3ed852e2009-09-05 21:47:34 +00002917 case COMPRESSION_LZW:
2918 {
2919 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2920 &bits_per_sample);
2921 if (((photometric == PHOTOMETRIC_RGB) ||
2922 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2923 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002924 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00002925 break;
2926 }
2927 default:
2928 break;
2929 }
cristy79e5dad2010-09-16 19:48:33 +00002930 option=GetImageOption(image_info,"tiff:tile-geometry");
2931 if (option == (const char *) NULL)
2932 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy3ed852e2009-09-05 21:47:34 +00002933 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2934 {
2935 unsigned short
2936 units;
2937
2938 /*
2939 Set image resolution.
2940 */
2941 units=RESUNIT_NONE;
2942 if (image->units == PixelsPerInchResolution)
2943 units=RESUNIT_INCH;
2944 if (image->units == PixelsPerCentimeterResolution)
2945 units=RESUNIT_CENTIMETER;
2946 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2947 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2948 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2949 if ((image->page.x != 0) || (image->page.y != 0))
2950 {
2951 /*
2952 Set image position.
2953 */
2954 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2955 image->x_resolution);
2956 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2957 image->y_resolution);
2958 }
2959 }
2960 if (image->chromaticity.white_point.x != 0.0)
2961 {
2962 float
2963 chromaticity[6];
2964
2965 /*
2966 Set image chromaticity.
2967 */
2968 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2969 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2970 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2971 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2972 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2973 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2974 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2975 chromaticity[0]=(float) image->chromaticity.white_point.x;
2976 chromaticity[1]=(float) image->chromaticity.white_point.y;
2977 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2978 }
2979 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2980 {
2981 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2982 if (image->scene != 0)
2983 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
2984 GetImageListLength(image));
2985 }
2986 if (image->orientation != UndefinedOrientation)
2987 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
2988 (void) TIFFSetProfiles(tiff,image);
2989 {
2990 uint16
2991 page,
2992 pages;
2993
2994 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00002995 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00002996 if ((image_info->adjoin != MagickFalse) && (pages > 1))
2997 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2998 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
2999 }
3000 (void) TIFFSetProperties(tiff,image);
3001 if (0)
3002 (void) TIFFSetEXIFProperties(tiff,image);
3003 /*
3004 Write image scanlines.
3005 */
3006 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3007 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3008 pixels=GetQuantumPixels(quantum_info);
3009 tiff_info.scanline=GetQuantumPixels(quantum_info);
3010 switch (photometric)
3011 {
3012 case PHOTOMETRIC_CIELAB:
3013 case PHOTOMETRIC_YCBCR:
3014 case PHOTOMETRIC_RGB:
3015 {
3016 /*
3017 RGB TIFF image.
3018 */
3019 switch (image_info->interlace)
3020 {
3021 case NoInterlace:
3022 default:
3023 {
3024 quantum_type=RGBQuantum;
3025 if (image->matte != MagickFalse)
3026 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003027 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003028 {
3029 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003030 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003031
3032 p=GetVirtualPixels(image,0,y,image->columns,1,
3033 &image->exception);
3034 if (p == (const PixelPacket *) NULL)
3035 break;
3036 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3037 quantum_info,quantum_type,pixels,&image->exception);
cristyda16f162011-02-19 23:52:17 +00003038 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00003039 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3040 break;
3041 if (image->previous == (Image *) NULL)
3042 {
cristy2837bcc2010-08-07 23:57:39 +00003043 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3044 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003045 if (status == MagickFalse)
3046 break;
3047 }
3048 }
3049 break;
3050 }
3051 case PlaneInterlace:
3052 case PartitionInterlace:
3053 {
3054 /*
3055 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3056 */
cristybb503372010-05-27 20:51:26 +00003057 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003058 {
3059 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003060 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003061
3062 p=GetVirtualPixels(image,0,y,image->columns,1,
3063 &image->exception);
3064 if (p == (const PixelPacket *) NULL)
3065 break;
3066 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3067 quantum_info,RedQuantum,pixels,&image->exception);
3068 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3069 break;
3070 }
3071 if (image->previous == (Image *) NULL)
3072 {
3073 status=SetImageProgress(image,SaveImageTag,100,400);
3074 if (status == MagickFalse)
3075 break;
3076 }
cristybb503372010-05-27 20:51:26 +00003077 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003078 {
3079 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003080 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003081
3082 p=GetVirtualPixels(image,0,y,image->columns,1,
3083 &image->exception);
3084 if (p == (const PixelPacket *) NULL)
3085 break;
3086 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3087 quantum_info,GreenQuantum,pixels,&image->exception);
3088 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3089 break;
3090 }
3091 if (image->previous == (Image *) NULL)
3092 {
3093 status=SetImageProgress(image,SaveImageTag,200,400);
3094 if (status == MagickFalse)
3095 break;
3096 }
cristybb503372010-05-27 20:51:26 +00003097 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003098 {
3099 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003100 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003101
3102 p=GetVirtualPixels(image,0,y,image->columns,1,
3103 &image->exception);
3104 if (p == (const PixelPacket *) NULL)
3105 break;
3106 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3107 quantum_info,BlueQuantum,pixels,&image->exception);
3108 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3109 break;
3110 }
3111 if (image->previous == (Image *) NULL)
3112 {
3113 status=SetImageProgress(image,SaveImageTag,300,400);
3114 if (status == MagickFalse)
3115 break;
3116 }
3117 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003118 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003119 {
3120 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003121 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003122
3123 p=GetVirtualPixels(image,0,y,image->columns,1,
3124 &image->exception);
3125 if (p == (const PixelPacket *) NULL)
3126 break;
3127 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3128 quantum_info,AlphaQuantum,pixels,&image->exception);
3129 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3130 break;
3131 }
3132 if (image->previous == (Image *) NULL)
3133 {
3134 status=SetImageProgress(image,SaveImageTag,400,400);
3135 if (status == MagickFalse)
3136 break;
3137 }
3138 break;
3139 }
3140 }
3141 break;
3142 }
3143 case PHOTOMETRIC_SEPARATED:
3144 {
3145 /*
3146 CMYK TIFF image.
3147 */
3148 quantum_type=CMYKQuantum;
3149 if (image->matte != MagickFalse)
3150 quantum_type=CMYKAQuantum;
3151 if (image->colorspace != CMYKColorspace)
3152 (void) TransformImageColorspace(image,CMYKColorspace);
cristybb503372010-05-27 20:51:26 +00003153 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003154 {
3155 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003156 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003157
3158 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3159 if (p == (const PixelPacket *) NULL)
3160 break;
3161 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3162 quantum_info,quantum_type,pixels,&image->exception);
3163 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3164 break;
3165 if (image->previous == (Image *) NULL)
3166 {
cristycee97112010-05-28 00:44:52 +00003167 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3168 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003169 if (status == MagickFalse)
3170 break;
3171 }
3172 }
3173 break;
3174 }
3175 case PHOTOMETRIC_PALETTE:
3176 {
3177 uint16
3178 *blue,
3179 *green,
3180 *red;
3181
3182 /*
3183 Colormapped TIFF image.
3184 */
3185 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3186 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3187 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3188 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3189 (blue == (uint16 *) NULL))
3190 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3191 /*
3192 Initialize TIFF colormap.
3193 */
3194 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3195 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3196 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003197 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003198 {
3199 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3200 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3201 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3202 }
3203 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3204 red=(uint16 *) RelinquishMagickMemory(red);
3205 green=(uint16 *) RelinquishMagickMemory(green);
3206 blue=(uint16 *) RelinquishMagickMemory(blue);
3207 }
3208 default:
3209 {
3210 /*
3211 Convert PseudoClass packets to contiguous grayscale scanlines.
3212 */
3213 quantum_type=IndexQuantum;
3214 if (image->matte != MagickFalse)
3215 {
3216 if (photometric != PHOTOMETRIC_PALETTE)
3217 quantum_type=GrayAlphaQuantum;
3218 else
3219 quantum_type=IndexAlphaQuantum;
3220 }
3221 else
3222 if (photometric != PHOTOMETRIC_PALETTE)
3223 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003224 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003225 {
3226 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003227 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003228
3229 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3230 if (p == (const PixelPacket *) NULL)
3231 break;
3232 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3233 quantum_info,quantum_type,pixels,&image->exception);
3234 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3235 break;
3236 if (image->previous == (Image *) NULL)
3237 {
cristycee97112010-05-28 00:44:52 +00003238 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3239 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003240 if (status == MagickFalse)
3241 break;
3242 }
3243 }
3244 break;
3245 }
3246 }
3247 quantum_info=DestroyQuantumInfo(quantum_info);
3248 DestroyTIFFInfo(&tiff_info);
3249 if (0 && (image_info->verbose == MagickTrue))
3250 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3251 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003252 image->endian=MSBEndian;
3253 if (endian == FILLORDER_LSB2MSB)
3254 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003255 image=SyncNextImageInList(image);
3256 if (image == (Image *) NULL)
3257 break;
3258 status=SetImageProgress(image,SaveImagesTag,scene++,
3259 GetImageListLength(image));
3260 if (status == MagickFalse)
3261 break;
3262 } while (image_info->adjoin != MagickFalse);
3263 (void) TIFFSetWarningHandler(warning_handler);
3264 (void) TIFFSetErrorHandler(error_handler);
3265 TIFFClose(tiff);
3266 return(MagickTrue);
3267}
3268#endif