blob: 02916f81dc360963bdb5f9e9dd6c8f4b2de8f7d3 [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);
cristyb51dff52011-05-19 16:55:47 +0000374 (void) FormatLocaleString(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)
cristyb51dff52011-05-19 16:55:47 +0000607 (void) FormatLocaleString(value,MaxTextExtent,"%d",shorty);
cristy3ed852e2009-09-05 21:47:34 +0000608 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)
cristyb51dff52011-05-19 16:55:47 +0000616 (void) FormatLocaleString(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)
cristyb51dff52011-05-19 16:55:47 +0000626 (void) FormatLocaleString(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
cristy3ed852e2009-09-05 21:47:34 +0000750 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000751 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000752 debug,
753 status;
754
755 MagickSizeType
756 number_pixels;
757
758 QuantumInfo
759 *quantum_info;
760
761 QuantumType
762 quantum_type;
763
cristybb503372010-05-27 20:51:26 +0000764 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000765 i;
766
767 size_t
768 length,
cristyc6da28e2011-04-28 01:41:35 +0000769 lsb_first,
cristy3ed852e2009-09-05 21:47:34 +0000770 pad;
771
cristyc6da28e2011-04-28 01:41:35 +0000772 ssize_t
773 y;
774
cristy3ed852e2009-09-05 21:47:34 +0000775 TIFF
776 *tiff;
777
778 TIFFErrorHandler
779 error_handler,
780 warning_handler;
781
782 TIFFMethodType
783 method;
784
785 uint16
786 compress_tag,
787 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000788 endian,
cristy3ed852e2009-09-05 21:47:34 +0000789 extra_samples,
790 interlace,
791 max_sample_value,
792 min_sample_value,
793 orientation,
794 pages,
795 photometric,
796 *sample_info,
797 sample_format,
798 samples_per_pixel,
799 units,
800 value;
801
802 uint32
803 height,
804 rows_per_strip,
805 width;
806
807 unsigned char
808 *pixels;
809
cristy3ed852e2009-09-05 21:47:34 +0000810 /*
811 Open image.
812 */
813 assert(image_info != (const ImageInfo *) NULL);
814 assert(image_info->signature == MagickSignature);
815 if (image_info->debug != MagickFalse)
816 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
817 image_info->filename);
818 assert(exception != (ExceptionInfo *) NULL);
819 assert(exception->signature == MagickSignature);
820 image=AcquireImage(image_info);
821 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
822 if (status == MagickFalse)
823 {
824 image=DestroyImageList(image);
825 return((Image *) NULL);
826 }
827 (void) MagickSetThreadValue(tiff_exception,exception);
828 error_handler=TIFFSetErrorHandler(TIFFErrors);
829 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
cristy1630f7f2011-02-18 01:10:02 +0000830 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
cristy3ed852e2009-09-05 21:47:34 +0000831 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
832 TIFFUnmapBlob);
833 if (tiff == (TIFF *) NULL)
834 {
835 (void) TIFFSetWarningHandler(warning_handler);
836 (void) TIFFSetErrorHandler(error_handler);
837 image=DestroyImageList(image);
838 return((Image *) NULL);
839 }
840 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +0000841 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +0000842 if (image_info->number_scenes != 0)
843 {
844 /*
845 Generate blank images for subimage specification (e.g. image.tif[4].
846 */
cristybb503372010-05-27 20:51:26 +0000847 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000848 {
849 (void) TIFFReadDirectory(tiff);
850 AcquireNextImage(image_info,image);
851 if (GetNextImageInList(image) == (Image *) NULL)
852 {
853 image=DestroyImageList(image);
854 return((Image *) NULL);
855 }
856 image=SyncNextImageInList(image);
857 }
858 }
859 do
860 {
861 if (0 && (image_info->verbose != MagickFalse))
862 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristy6b032822010-06-29 16:52:32 +0000863 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
864 "lsb" : "msb");
cristy3ed852e2009-09-05 21:47:34 +0000865 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
866 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
867 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
872 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
cristyae1319c2011-01-19 20:18:53 +0000873 if (sample_format == SAMPLEFORMAT_IEEEFP)
874 (void) SetImageProperty(image,"quantum:format","floating-point");
cristy3ed852e2009-09-05 21:47:34 +0000875 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
876 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
877 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
878 switch (photometric)
879 {
880 case PHOTOMETRIC_MINISBLACK:
881 {
882 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
883 break;
884 }
885 case PHOTOMETRIC_MINISWHITE:
886 {
887 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
888 break;
889 }
890 case PHOTOMETRIC_PALETTE:
891 {
892 (void) SetImageProperty(image,"tiff:photometric","palette");
893 break;
894 }
895 case PHOTOMETRIC_RGB:
896 {
897 (void) SetImageProperty(image,"tiff:photometric","RGB");
898 break;
899 }
900 case PHOTOMETRIC_CIELAB:
901 {
902 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
903 break;
904 }
905 case PHOTOMETRIC_SEPARATED:
906 {
907 (void) SetImageProperty(image,"tiff:photometric","separated");
908 break;
909 }
cristy79e5dad2010-09-16 19:48:33 +0000910 case PHOTOMETRIC_YCBCR:
911 {
912 (void) SetImageProperty(image,"tiff:photometric","YCBCR");
913 break;
914 }
cristy3ed852e2009-09-05 21:47:34 +0000915 default:
916 {
917 (void) SetImageProperty(image,"tiff:photometric","unknown");
918 break;
919 }
920 }
921 if (image->debug != MagickFalse)
922 {
923 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
924 (unsigned int) width,(unsigned int) height);
925 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
926 interlace);
927 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
928 "Bits per sample: %u",bits_per_sample);
929 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
930 "Min sample value: %u",min_sample_value);
931 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
932 "Max sample value: %u",max_sample_value);
933 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
934 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
935 }
cristybb503372010-05-27 20:51:26 +0000936 image->columns=(size_t) width;
937 image->rows=(size_t) height;
938 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +0000939 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +0000940 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
941 (double) image->depth);
cristyf2687ca2010-06-29 16:32:38 +0000942 lsb_first=1;
943 image->endian=MSBEndian;
944 if ((int) (*(char *) &lsb_first) != 0)
945 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +0000946 if (photometric == PHOTOMETRIC_SEPARATED)
947 image->colorspace=CMYKColorspace;
948 if (photometric == PHOTOMETRIC_CIELAB)
949 image->colorspace=LabColorspace;
950 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
951 &samples_per_pixel);
952 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
953 x_resolution=(float) image->x_resolution;
954 y_resolution=(float) image->y_resolution;
955 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
956 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
957 image->x_resolution=x_resolution;
958 image->y_resolution=y_resolution;
959 x_position=(float) image->page.x/x_resolution;
960 y_position=(float) image->page.y/y_resolution;
961 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
962 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +0000963 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
964 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000965 image->orientation=(OrientationType) orientation;
966 chromaticity=(float *) NULL;
967 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
968 if (chromaticity != (float *) NULL)
969 {
970 image->chromaticity.white_point.x=chromaticity[0];
971 image->chromaticity.white_point.y=chromaticity[1];
972 }
973 chromaticity=(float *) NULL;
974 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
975 if (chromaticity != (float *) NULL)
976 {
977 image->chromaticity.red_primary.x=chromaticity[0];
978 image->chromaticity.red_primary.y=chromaticity[1];
979 image->chromaticity.green_primary.x=chromaticity[2];
980 image->chromaticity.green_primary.y=chromaticity[3];
981 image->chromaticity.blue_primary.x=chromaticity[4];
982 image->chromaticity.blue_primary.y=chromaticity[5];
983 }
984 TIFFGetProperties(tiff,image);
985 option=GetImageOption(image_info,"tiff:exif");
986 if ((option != (const char *) NULL) &&
987 (IsMagickTrue(option) != MagickFalse))
988 TIFFGetEXIFProperties(tiff,image);
989 TIFFGetProfiles(tiff,image);
990 /*
991 Allocate memory for the image and pixel buffer.
992 */
993#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
994 if ((compress_tag != COMPRESSION_NONE) &&
995 (TIFFIsCODECConfigured(compress_tag) == 0))
996 {
997 TIFFClose(tiff);
998 ThrowReaderException(CoderError,"CompressNotSupported");
999 }
1000#endif
1001 switch (compress_tag)
1002 {
1003 case COMPRESSION_NONE: image->compression=NoCompression; break;
1004 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1005 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1006 case COMPRESSION_JPEG:
1007 {
1008 image->compression=JPEGCompression;
1009#if defined(JPEG_SUPPORT)
1010 {
1011 char
1012 sampling_factor[MaxTextExtent];
1013
1014 uint16
1015 horizontal,
1016 vertical;
1017
1018 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1019 &horizontal,&vertical);
cristyb51dff52011-05-19 16:55:47 +00001020 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
cristy3ed852e2009-09-05 21:47:34 +00001021 horizontal,vertical);
1022 (void) SetImageProperty(image,"jpeg:sampling-factor",
1023 sampling_factor);
1024 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1025 "Sampling Factors: %s",sampling_factor);
1026 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1027 {
1028 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1029 JPEGCOLORMODE_RGB);
1030 photometric=PHOTOMETRIC_RGB;
1031 }
1032 }
1033#endif
1034 break;
1035 }
1036 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001037#if defined(COMPRESSION_LZMA)
1038 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1039#endif
cristy3ed852e2009-09-05 21:47:34 +00001040 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1041 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1042 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1043 default: image->compression=RLECompression; break;
1044 }
1045 quantum_info=AcquireQuantumInfo(image_info,image);
1046 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001047 {
1048 TIFFClose(tiff);
1049 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1050 }
cristy3ed852e2009-09-05 21:47:34 +00001051 if (sample_format == SAMPLEFORMAT_UINT)
1052 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1053 if (sample_format == SAMPLEFORMAT_INT)
1054 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1055 if (sample_format == SAMPLEFORMAT_IEEEFP)
1056 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1057 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001058 {
1059 TIFFClose(tiff);
1060 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1061 }
cristy3ed852e2009-09-05 21:47:34 +00001062 status=MagickTrue;
1063 switch (photometric)
1064 {
1065 case PHOTOMETRIC_MINISBLACK:
1066 {
1067 quantum_info->min_is_white=MagickFalse;
1068 break;
1069 }
1070 case PHOTOMETRIC_MINISWHITE:
1071 {
1072 quantum_info->min_is_white=MagickTrue;
1073 break;
1074 }
1075 default:
1076 break;
1077 }
cristy891dc792010-03-04 01:47:16 +00001078 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001079 extra_samples=0;
1080 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1081 &sample_info);
1082 if (extra_samples == 0)
1083 {
1084 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1085 image->matte=MagickTrue;
1086 }
1087 else
cristy6c207342010-02-19 17:49:50 +00001088 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001089 {
cristy7cf69862010-03-03 13:57:24 +00001090 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001091 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001092 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001093 }
cristy891dc792010-03-04 01:47:16 +00001094 option=GetImageOption(image_info,"tiff:alpha");
1095 if (option != (const char *) NULL)
1096 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1097 MagickFalse;
1098 if (image->matte != MagickFalse)
1099 (void) SetImageProperty(image,"tiff:alpha",
1100 associated_alpha != MagickFalse ? "associated" : "unassociated");
cristy3ed852e2009-09-05 21:47:34 +00001101 if ((photometric == PHOTOMETRIC_PALETTE) &&
1102 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1103 {
cristybb503372010-05-27 20:51:26 +00001104 size_t
cristy3ed852e2009-09-05 21:47:34 +00001105 colors;
1106
cristybb503372010-05-27 20:51:26 +00001107 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy3ed852e2009-09-05 21:47:34 +00001108 if (AcquireImageColormap(image,colors) == MagickFalse)
1109 {
1110 TIFFClose(tiff);
1111 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1112 }
1113 }
1114 if (units == RESUNIT_INCH)
1115 image->units=PixelsPerInchResolution;
1116 if (units == RESUNIT_CENTIMETER)
1117 image->units=PixelsPerCentimeterResolution;
1118 value=(unsigned short) image->scene;
1119 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1120 image->scene=value;
1121 if (image_info->ping != MagickFalse)
1122 {
1123 if (image_info->number_scenes != 0)
1124 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1125 break;
1126 goto next_tiff_frame;
1127 }
1128 method=ReadGenericMethod;
1129 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1130 {
1131 char
1132 value[MaxTextExtent];
1133
1134 method=ReadStripMethod;
cristyb51dff52011-05-19 16:55:47 +00001135 (void) FormatLocaleString(value,MaxTextExtent,"%u",
cristy3ed852e2009-09-05 21:47:34 +00001136 (unsigned int) rows_per_strip);
1137 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1138 }
1139 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1140 method=ReadRGBAMethod;
1141 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1142 method=ReadCMYKAMethod;
1143 if ((photometric != PHOTOMETRIC_RGB) &&
1144 (photometric != PHOTOMETRIC_CIELAB) &&
1145 (photometric != PHOTOMETRIC_SEPARATED))
1146 method=ReadGenericMethod;
1147 if (image->storage_class == PseudoClass)
1148 method=ReadSingleSampleMethod;
1149 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1150 (photometric == PHOTOMETRIC_MINISWHITE))
1151 method=ReadSingleSampleMethod;
1152 if ((photometric != PHOTOMETRIC_SEPARATED) &&
cristybc736662011-03-09 13:39:42 +00001153 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
cristy3ed852e2009-09-05 21:47:34 +00001154 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001155 if (image->compression == JPEGCompression)
1156 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001157 if (TIFFIsTiled(tiff) != MagickFalse)
1158 method=ReadTileMethod;
1159 quantum_type=RGBQuantum;
1160 pixels=GetQuantumPixels(quantum_info);
1161 switch (method)
1162 {
1163 case ReadSingleSampleMethod:
1164 {
1165 /*
1166 Convert TIFF image to PseudoClass MIFF image.
1167 */
1168 if ((image->storage_class == PseudoClass) &&
1169 (photometric == PHOTOMETRIC_PALETTE))
1170 {
cristybb503372010-05-27 20:51:26 +00001171 size_t
cristy3ed852e2009-09-05 21:47:34 +00001172 range;
1173
1174 uint16
1175 *blue_colormap,
1176 *green_colormap,
1177 *red_colormap;
1178
1179 /*
1180 Initialize colormap.
1181 */
1182 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1183 &green_colormap,&blue_colormap);
1184 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001185 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001186 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1187 (blue_colormap[i] >= 256))
1188 {
1189 range=65535;
1190 break;
1191 }
cristybb503372010-05-27 20:51:26 +00001192 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001193 {
cristyce70c172010-01-07 17:15:30 +00001194 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001195 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001196 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001197 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001198 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001199 blue_colormap[i])/range);
1200 }
1201 }
1202 quantum_type=IndexQuantum;
1203 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1204 if (image->matte != MagickFalse)
1205 {
1206 if (image->storage_class != PseudoClass)
1207 {
1208 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1209 GrayAlphaQuantum;
1210 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1211 }
1212 else
1213 {
1214 quantum_type=IndexAlphaQuantum;
1215 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1216 }
1217 }
1218 else
1219 if (image->storage_class != PseudoClass)
1220 {
1221 quantum_type=GrayQuantum;
1222 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1223 }
1224 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1225 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001226 {
1227 TIFFClose(tiff);
1228 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1229 }
cristy3ed852e2009-09-05 21:47:34 +00001230 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001231 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001232 {
1233 int
1234 status;
1235
1236 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001237 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001238
1239 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1240 if (status == -1)
1241 break;
1242 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1243 if (q == (PixelPacket *) NULL)
1244 break;
1245 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1246 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00001247 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00001248 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1249 break;
1250 if (image->previous == (Image *) NULL)
1251 {
cristycee97112010-05-28 00:44:52 +00001252 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1253 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001254 if (status == MagickFalse)
1255 break;
1256 }
1257 }
1258 break;
1259 }
1260 case ReadRGBAMethod:
1261 {
1262 /*
1263 Convert TIFF image to DirectClass MIFF image.
1264 */
1265 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1266 quantum_type=RGBQuantum;
1267 if (image->matte != MagickFalse)
1268 {
1269 quantum_type=RGBAQuantum;
1270 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1271 }
1272 if (image->colorspace == CMYKColorspace)
1273 {
1274 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1275 quantum_type=CMYKQuantum;
1276 if (image->matte != MagickFalse)
1277 {
1278 quantum_type=CMYKAQuantum;
1279 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1280 }
1281 }
1282 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1283 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001284 {
1285 TIFFClose(tiff);
1286 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1287 }
cristy3ed852e2009-09-05 21:47:34 +00001288 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001289 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001290 {
1291 int
1292 status;
1293
1294 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001295 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001296
1297 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1298 if (status == -1)
1299 break;
1300 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1301 if (q == (PixelPacket *) NULL)
1302 break;
cristy0b334892010-03-15 02:26:46 +00001303 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1304 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001305 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1306 break;
1307 if (image->previous == (Image *) NULL)
1308 {
cristycee97112010-05-28 00:44:52 +00001309 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1310 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001311 if (status == MagickFalse)
1312 break;
1313 }
1314 }
1315 break;
1316 }
1317 case ReadCMYKAMethod:
1318 {
1319 /*
1320 Convert TIFF image to DirectClass MIFF image.
1321 */
cristybb503372010-05-27 20:51:26 +00001322 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001323 {
cristybb503372010-05-27 20:51:26 +00001324 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001325 {
cristy6d9f12f2009-11-03 14:50:26 +00001326 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001327 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001328
cristy3ed852e2009-09-05 21:47:34 +00001329 int
1330 status;
1331
1332 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1333 pixels);
1334 if (status == -1)
1335 break;
1336 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1337 if (q == (PixelPacket *) NULL)
1338 break;
1339 if (image->colorspace != CMYKColorspace)
1340 switch (i)
1341 {
1342 case 0: quantum_type=RedQuantum; break;
1343 case 1: quantum_type=GreenQuantum; break;
1344 case 2: quantum_type=BlueQuantum; break;
1345 case 3: quantum_type=AlphaQuantum; break;
1346 default: quantum_type=UndefinedQuantum; break;
1347 }
cristy6d9f12f2009-11-03 14:50:26 +00001348 else
cristy3ed852e2009-09-05 21:47:34 +00001349 switch (i)
1350 {
1351 case 0: quantum_type=CyanQuantum; break;
1352 case 1: quantum_type=MagentaQuantum; break;
1353 case 2: quantum_type=YellowQuantum; break;
1354 case 3: quantum_type=BlackQuantum; break;
1355 case 4: quantum_type=AlphaQuantum; break;
1356 default: quantum_type=UndefinedQuantum; break;
1357 }
1358 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1359 quantum_type,pixels,exception);
1360 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1361 break;
1362 }
1363 if (image->previous == (Image *) NULL)
1364 {
cristycee97112010-05-28 00:44:52 +00001365 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1366 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001367 if (status == MagickFalse)
1368 break;
1369 }
1370 }
1371 break;
1372 }
1373 case ReadStripMethod:
1374 {
1375 register unsigned char
1376 *p;
1377
1378 /*
1379 Convert stripped TIFF image to DirectClass MIFF image.
1380 */
1381 i=0;
1382 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001383 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001384 {
cristybb503372010-05-27 20:51:26 +00001385 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001386 x;
1387
1388 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001389 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001390
1391 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1392 if (q == (PixelPacket *) NULL)
1393 break;
1394 if (i == 0)
1395 {
1396 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1397 break;
cristyeaedf062010-05-29 22:36:02 +00001398 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1399 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001400 }
1401 i--;
cristy7f5d1662011-04-14 12:43:28 +00001402 p=(unsigned char *) (((uint32 *) pixels)+image->columns*i);
cristybb503372010-05-27 20:51:26 +00001403 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001404 {
cristyc6da28e2011-04-28 01:41:35 +00001405 SetRedPixelComponent(q,ScaleCharToQuantum((unsigned char)
1406 (TIFFGetR(*p))));
1407 SetGreenPixelComponent(q,ScaleCharToQuantum((unsigned char)
1408 (TIFFGetG(*p))));
1409 SetBluePixelComponent(q,ScaleCharToQuantum((unsigned char)
1410 (TIFFGetB(*p))));
cristy3ed852e2009-09-05 21:47:34 +00001411 if (image->matte != MagickFalse)
cristyc6da28e2011-04-28 01:41:35 +00001412 SetOpacityPixelComponent(q,ScaleCharToQuantum((unsigned char)
1413 (TIFFGetA(*p))));
cristy3ed852e2009-09-05 21:47:34 +00001414 p++;
1415 q++;
1416 }
1417 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1418 break;
1419 if (image->previous == (Image *) NULL)
1420 {
cristycee97112010-05-28 00:44:52 +00001421 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1422 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001423 if (status == MagickFalse)
1424 break;
1425 }
1426 }
1427 break;
1428 }
1429 case ReadTileMethod:
1430 {
1431 register uint32
1432 *p;
1433
1434 uint32
1435 *tile_pixels,
1436 columns,
1437 rows;
1438
cristybb503372010-05-27 20:51:26 +00001439 size_t
cristy3ed852e2009-09-05 21:47:34 +00001440 number_pixels;
1441
1442 /*
1443 Convert tiled TIFF image to DirectClass MIFF image.
1444 */
1445 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1446 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1447 {
1448 TIFFClose(tiff);
1449 ThrowReaderException(CoderError,"ImageIsNotTiled");
1450 }
1451 (void) SetImageStorageClass(image,DirectClass);
1452 number_pixels=columns*rows;
cristyda16f162011-02-19 23:52:17 +00001453 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
cristy3ed852e2009-09-05 21:47:34 +00001454 sizeof(*tile_pixels));
1455 if (tile_pixels == (uint32 *) NULL)
1456 {
1457 TIFFClose(tiff);
1458 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1459 }
cristybb503372010-05-27 20:51:26 +00001460 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001461 {
1462 PixelPacket
1463 *tile;
1464
cristybb503372010-05-27 20:51:26 +00001465 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001466 x;
1467
1468 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001469 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001470
cristybb503372010-05-27 20:51:26 +00001471 size_t
cristy3ed852e2009-09-05 21:47:34 +00001472 columns_remaining,
1473 rows_remaining;
1474
1475 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001476 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001477 rows_remaining=rows;
1478 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1479 exception);
1480 if (tile == (PixelPacket *) NULL)
1481 break;
cristybb503372010-05-27 20:51:26 +00001482 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001483 {
cristybb503372010-05-27 20:51:26 +00001484 size_t
cristy3ed852e2009-09-05 21:47:34 +00001485 column,
1486 row;
1487
1488 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1489 break;
1490 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001491 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001492 columns_remaining=columns;
1493 p=tile_pixels+(rows-rows_remaining)*columns;
1494 q=tile+(image->columns*(rows_remaining-1)+x);
1495 for (row=rows_remaining; row > 0; row--)
1496 {
1497 if (image->matte != MagickFalse)
1498 for (column=columns_remaining; column > 0; column--)
1499 {
cristyc6da28e2011-04-28 01:41:35 +00001500 SetRedPixelComponent(q,ScaleCharToQuantum((unsigned char)
1501 TIFFGetR(*p)));
1502 SetGreenPixelComponent(q,ScaleCharToQuantum((unsigned char)
1503 TIFFGetG(*p)));
1504 SetBluePixelComponent(q,ScaleCharToQuantum((unsigned char)
1505 TIFFGetB(*p)));
1506 SetOpacityPixelComponent(q,QuantumRange-ScaleCharToQuantum(
cristy3ed852e2009-09-05 21:47:34 +00001507 (unsigned char) TIFFGetA(*p)));
1508 q++;
1509 p++;
1510 }
1511 else
1512 for (column=columns_remaining; column > 0; column--)
1513 {
cristyc6da28e2011-04-28 01:41:35 +00001514 SetRedPixelComponent(q,ScaleCharToQuantum((unsigned char)
1515 TIFFGetR(*p)));
1516 SetGreenPixelComponent(q,ScaleCharToQuantum((unsigned char)
1517 TIFFGetG(*p)));
1518 SetBluePixelComponent(q,ScaleCharToQuantum((unsigned char)
1519 TIFFGetB(*p)));
cristy3ed852e2009-09-05 21:47:34 +00001520 q++;
1521 p++;
1522 }
1523 p+=columns-columns_remaining;
1524 q-=(image->columns+columns_remaining);
1525 }
1526 }
1527 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1528 break;
1529 if (image->previous == (Image *) NULL)
1530 {
cristycee97112010-05-28 00:44:52 +00001531 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1532 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001533 if (status == MagickFalse)
1534 break;
1535 }
1536 }
1537 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1538 break;
1539 }
1540 case ReadGenericMethod:
1541 default:
1542 {
1543 register uint32
1544 *p;
1545
1546 uint32
1547 *pixels;
1548
1549 /*
1550 Convert TIFF image to DirectClass MIFF image.
1551 */
1552 number_pixels=(MagickSizeType) image->columns*image->rows;
1553 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1554 (number_pixels*sizeof(uint32))))
1555 {
1556 TIFFClose(tiff);
1557 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1558 }
1559 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1560 sizeof(uint32));
1561 if (pixels == (uint32 *) NULL)
1562 {
1563 TIFFClose(tiff);
1564 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1565 }
1566 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1567 (uint32) image->rows,(uint32 *) pixels,0);
1568 /*
1569 Convert image to DirectClass pixel packets.
1570 */
1571 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001572 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001573 {
cristybb503372010-05-27 20:51:26 +00001574 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001575 x;
1576
1577 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001578 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001579
1580 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1581 if (q == (PixelPacket *) NULL)
1582 break;
1583 q+=image->columns-1;
cristybb503372010-05-27 20:51:26 +00001584 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001585 {
cristyc6da28e2011-04-28 01:41:35 +00001586 SetRedPixelComponent(q,ScaleCharToQuantum((unsigned char)
1587 TIFFGetR(*p)));
1588 SetBluePixelComponent(q,ScaleCharToQuantum((unsigned char)
1589 TIFFGetG(*p)));
1590 SetGreenPixelComponent(q,ScaleCharToQuantum((unsigned char)
1591 TIFFGetB(*p)));
cristy3ed852e2009-09-05 21:47:34 +00001592 if (image->matte != MagickFalse)
cristyc6da28e2011-04-28 01:41:35 +00001593 SetOpacityPixelComponent(q,QuantumRange-ScaleCharToQuantum(
1594 (unsigned char) TIFFGetA(*p)));
cristy3ed852e2009-09-05 21:47:34 +00001595 p--;
1596 q--;
1597 }
1598 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1599 break;
1600 if (image->previous == (Image *) NULL)
1601 {
cristycee97112010-05-28 00:44:52 +00001602 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1603 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001604 if (status == MagickFalse)
1605 break;
1606 }
1607 }
1608 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1609 break;
1610 }
1611 }
1612 SetQuantumImageType(image,quantum_type);
1613 next_tiff_frame:
1614 if ((photometric == PHOTOMETRIC_LOGL) ||
1615 (photometric == PHOTOMETRIC_MINISBLACK) ||
1616 (photometric == PHOTOMETRIC_MINISWHITE))
1617 {
1618 image->type=GrayscaleType;
1619 if (bits_per_sample == 1)
1620 image->type=BilevelType;
1621 }
1622 if (image->storage_class == PseudoClass)
1623 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001624 image->endian=MSBEndian;
1625 if (endian == FILLORDER_LSB2MSB)
1626 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001627 if ((photometric == PHOTOMETRIC_LOGL) ||
1628 (photometric == PHOTOMETRIC_MINISBLACK) ||
1629 (photometric == PHOTOMETRIC_MINISWHITE))
1630 {
1631 image->type=GrayscaleType;
1632 if (bits_per_sample == 1)
1633 image->type=BilevelType;
1634 }
1635 /*
1636 Proceed to next image.
1637 */
1638 if (image_info->number_scenes != 0)
1639 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1640 break;
1641 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1642 if (status == MagickTrue)
1643 {
1644 /*
1645 Allocate next image structure.
1646 */
1647 AcquireNextImage(image_info,image);
1648 if (GetNextImageInList(image) == (Image *) NULL)
1649 {
1650 image=DestroyImageList(image);
1651 return((Image *) NULL);
1652 }
1653 image=SyncNextImageInList(image);
1654 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1655 image->scene);
1656 if (status == MagickFalse)
1657 break;
1658 }
1659 quantum_info=DestroyQuantumInfo(quantum_info);
1660 } while (status == MagickTrue);
1661 (void) TIFFSetWarningHandler(warning_handler);
1662 (void) TIFFSetErrorHandler(error_handler);
1663 TIFFClose(tiff);
1664 return(GetFirstImageInList(image));
1665}
1666#endif
1667
1668/*
1669%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1670% %
1671% %
1672% %
1673% R e g i s t e r T I F F I m a g e %
1674% %
1675% %
1676% %
1677%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1678%
1679% RegisterTIFFImage() adds properties for the TIFF image format to
1680% the list of supported formats. The properties include the image format
1681% tag, a method to read and/or write the format, whether the format
1682% supports the saving of more than one frame to the same file or blob,
1683% whether the format supports native in-memory I/O, and a brief
1684% description of the format.
1685%
1686% The format of the RegisterTIFFImage method is:
1687%
cristybb503372010-05-27 20:51:26 +00001688% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001689%
1690*/
cristybb503372010-05-27 20:51:26 +00001691ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001692{
1693#define TIFFDescription "Tagged Image File Format"
1694
1695 char
1696 version[MaxTextExtent];
1697
1698 MagickInfo
1699 *entry;
cristy6b032822010-06-29 16:52:32 +00001700
cristy18b17442009-10-25 18:36:48 +00001701 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1702 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001703 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001704 if (instantiate_key == MagickFalse)
1705 {
1706 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1707 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1708 instantiate_key=MagickTrue;
1709 }
cristyf84a1932010-01-03 18:00:18 +00001710 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001711 *version='\0';
1712#if defined(TIFF_VERSION)
cristyb51dff52011-05-19 16:55:47 +00001713 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
cristy3ed852e2009-09-05 21:47:34 +00001714#endif
1715#if defined(MAGICKCORE_TIFF_DELEGATE)
1716 {
1717 const char
1718 *p;
1719
cristybb503372010-05-27 20:51:26 +00001720 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001721 i;
1722
1723 p=TIFFGetVersion();
1724 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1725 version[i]=(*p++);
1726 version[i]='\0';
1727 }
1728#endif
1729
cristy3d7f8062009-09-24 20:45:35 +00001730 entry=SetMagickInfo("GROUP4");
1731#if defined(MAGICKCORE_TIFF_DELEGATE)
1732 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1733 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1734#endif
1735 entry->raw=MagickTrue;
1736 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001737 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001738 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001739 entry->seekable_stream=MagickTrue;
1740 entry->thread_support=NoThreadSupport;
1741 entry->description=ConstantString("Raw CCITT Group4");
1742 entry->module=ConstantString("TIFF");
1743 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001744 entry=SetMagickInfo("PTIF");
1745#if defined(MAGICKCORE_TIFF_DELEGATE)
1746 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1747 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1748#endif
1749 entry->endian_support=MagickTrue;
1750 entry->seekable_stream=MagickTrue;
1751 entry->thread_support=NoThreadSupport;
1752 entry->description=ConstantString("Pyramid encoded TIFF");
1753 entry->module=ConstantString("TIFF");
1754 (void) RegisterMagickInfo(entry);
1755 entry=SetMagickInfo("TIF");
1756#if defined(MAGICKCORE_TIFF_DELEGATE)
1757 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1758 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1759#endif
1760 entry->endian_support=MagickTrue;
1761 entry->seekable_stream=MagickTrue;
1762 entry->stealth=MagickTrue;
1763 entry->thread_support=NoThreadSupport;
1764 entry->description=ConstantString(TIFFDescription);
1765 if (*version != '\0')
1766 entry->version=ConstantString(version);
1767 entry->module=ConstantString("TIFF");
1768 (void) RegisterMagickInfo(entry);
1769 entry=SetMagickInfo("TIFF");
1770#if defined(MAGICKCORE_TIFF_DELEGATE)
1771 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1772 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1773#endif
1774 entry->magick=(IsImageFormatHandler *) IsTIFF;
1775 entry->endian_support=MagickTrue;
1776 entry->seekable_stream=MagickTrue;
1777 entry->thread_support=NoThreadSupport;
1778 entry->description=ConstantString(TIFFDescription);
1779 if (*version != '\0')
1780 entry->version=ConstantString(version);
1781 entry->module=ConstantString("TIFF");
1782 (void) RegisterMagickInfo(entry);
1783 entry=SetMagickInfo("TIFF64");
1784#if defined(TIFF_VERSION_BIG)
1785 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1786 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1787#endif
1788 entry->adjoin=MagickFalse;
1789 entry->endian_support=MagickTrue;
1790 entry->seekable_stream=MagickTrue;
1791 entry->thread_support=NoThreadSupport;
1792 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1793 if (*version != '\0')
1794 entry->version=ConstantString(version);
1795 entry->module=ConstantString("TIFF");
1796 (void) RegisterMagickInfo(entry);
1797 return(MagickImageCoderSignature);
1798}
1799
1800/*
1801%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1802% %
1803% %
1804% %
1805% U n r e g i s t e r T I F F I m a g e %
1806% %
1807% %
1808% %
1809%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1810%
1811% UnregisterTIFFImage() removes format registrations made by the TIFF module
1812% from the list of supported formats.
1813%
1814% The format of the UnregisterTIFFImage method is:
1815%
1816% UnregisterTIFFImage(void)
1817%
1818*/
1819ModuleExport void UnregisterTIFFImage(void)
1820{
cristy3d7f8062009-09-24 20:45:35 +00001821 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001822 (void) UnregisterMagickInfo("PTIF");
1823 (void) UnregisterMagickInfo("TIF");
1824 (void) UnregisterMagickInfo("TIFF");
1825 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001826 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1827 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001828 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001829 if (instantiate_key != MagickFalse)
1830 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1831 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1832 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001833 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001834 DestroySemaphoreInfo(&tiff_semaphore);
1835}
1836
1837#if defined(MAGICKCORE_TIFF_DELEGATE)
1838/*
1839%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1840% %
1841% %
1842% %
cristy3d7f8062009-09-24 20:45:35 +00001843% W r i t e G R O U P 4 I m a g e %
1844% %
1845% %
1846% %
1847%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1848%
1849% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1850%
1851% The format of the WriteGROUP4Image method is:
1852%
1853% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1854% Image *image)
1855%
1856% A description of each parameter follows:
1857%
1858% o image_info: the image info.
1859%
1860% o image: The image.
1861%
1862*/
1863static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1864 Image *image)
1865{
1866 char
1867 filename[MaxTextExtent];
1868
1869 FILE
1870 *file;
1871
1872 Image
1873 *huffman_image;
1874
1875 ImageInfo
1876 *write_info;
1877
1878 int
1879 unique_file;
1880
1881 MagickBooleanType
1882 status;
1883
cristybb503372010-05-27 20:51:26 +00001884 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001885 i;
1886
1887 ssize_t
1888 count;
1889
1890 TIFF
1891 *tiff;
1892
cristy94c8fe42009-10-06 01:57:36 +00001893 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001894 *byte_count,
1895 strip_size;
1896
1897 unsigned char
1898 *buffer;
1899
1900 /*
1901 Write image as CCITT Group4 TIFF image to a temporary file.
1902 */
1903 assert(image_info != (const ImageInfo *) NULL);
1904 assert(image_info->signature == MagickSignature);
1905 assert(image != (Image *) NULL);
1906 assert(image->signature == MagickSignature);
1907 if (image->debug != MagickFalse)
1908 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1909 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1910 if (status == MagickFalse)
1911 return(status);
1912 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1913 if (huffman_image == (Image *) NULL)
1914 {
1915 (void) CloseBlob(image);
1916 return(MagickFalse);
1917 }
cristy94c8fe42009-10-06 01:57:36 +00001918 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001919 file=(FILE *) NULL;
1920 unique_file=AcquireUniqueFileResource(filename);
1921 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001922 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001923 if ((unique_file == -1) || (file == (FILE *) NULL))
1924 {
1925 ThrowFileException(&image->exception,FileOpenError,
1926 "UnableToCreateTemporaryFile",filename);
1927 return(MagickFalse);
1928 }
cristyb51dff52011-05-19 16:55:47 +00001929 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
cristy3d7f8062009-09-24 20:45:35 +00001930 filename);
1931 (void) SetImageType(huffman_image,BilevelType);
cristy14efd992009-09-26 23:46:03 +00001932 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001933 SetImageInfoFile(write_info,file);
1934 write_info->compression=Group4Compression;
1935 write_info->type=BilevelType;
1936 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1937 status=WriteTIFFImage(write_info,huffman_image);
1938 (void) fflush(file);
1939 write_info=DestroyImageInfo(write_info);
1940 if (status == MagickFalse)
1941 {
1942 InheritException(&image->exception,&huffman_image->exception);
1943 huffman_image=DestroyImage(huffman_image);
1944 (void) fclose(file);
1945 (void) RelinquishUniqueFileResource(filename);
1946 return(MagickFalse);
1947 }
1948 tiff=TIFFOpen(filename,"rb");
1949 if (tiff == (TIFF *) NULL)
1950 {
1951 huffman_image=DestroyImage(huffman_image);
1952 (void) fclose(file);
1953 (void) RelinquishUniqueFileResource(filename);
1954 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1955 image_info->filename);
1956 return(MagickFalse);
1957 }
1958 /*
1959 Allocate raw strip buffer.
1960 */
cristy94c8fe42009-10-06 01:57:36 +00001961 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1962 {
1963 TIFFClose(tiff);
1964 huffman_image=DestroyImage(huffman_image);
1965 (void) fclose(file);
1966 (void) RelinquishUniqueFileResource(filename);
1967 return(MagickFalse);
1968 }
cristy3d7f8062009-09-24 20:45:35 +00001969 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00001970 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001971 if (byte_count[i] > strip_size)
1972 strip_size=byte_count[i];
1973 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1974 sizeof(*buffer));
1975 if (buffer == (unsigned char *) NULL)
1976 {
1977 TIFFClose(tiff);
1978 huffman_image=DestroyImage(huffman_image);
1979 (void) fclose(file);
1980 (void) RelinquishUniqueFileResource(filename);
1981 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1982 image_info->filename);
1983 }
1984 /*
1985 Compress runlength encoded to 2D Huffman pixels.
1986 */
cristybb503372010-05-27 20:51:26 +00001987 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001988 {
cristy94c8fe42009-10-06 01:57:36 +00001989 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00001990 if (WriteBlob(image,(size_t) count,buffer) != count)
1991 status=MagickFalse;
1992 }
1993 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1994 TIFFClose(tiff);
1995 huffman_image=DestroyImage(huffman_image);
1996 (void) fclose(file);
1997 (void) RelinquishUniqueFileResource(filename);
1998 (void) CloseBlob(image);
1999 return(status);
2000}
2001#endif
2002
2003#if defined(MAGICKCORE_TIFF_DELEGATE)
2004/*
2005%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2006% %
2007% %
2008% %
cristy3ed852e2009-09-05 21:47:34 +00002009% W r i t e P T I F I m a g e %
2010% %
2011% %
2012% %
2013%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2014%
2015% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2016% format.
2017%
2018% The format of the WritePTIFImage method is:
2019%
2020% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2021% Image *image)
2022%
2023% A description of each parameter follows:
2024%
2025% o image_info: the image info.
2026%
2027% o image: The image.
2028%
2029*/
2030static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2031 Image *image)
2032{
2033 Image
2034 *images,
2035 *next,
2036 *pyramid_image;
2037
2038 ImageInfo
2039 *write_info;
2040
2041 MagickBooleanType
2042 status;
2043
cristybb503372010-05-27 20:51:26 +00002044 size_t
cristy3ed852e2009-09-05 21:47:34 +00002045 columns,
2046 rows;
2047
2048 /*
2049 Create pyramid-encoded TIFF image.
2050 */
2051 images=NewImageList();
2052 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2053 {
2054 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2055 &image->exception));
2056 columns=next->columns;
2057 rows=next->rows;
2058 while ((columns > 64) && (rows > 64))
2059 {
2060 columns/=2;
2061 rows/=2;
2062 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2063 &image->exception);
2064 AppendImageToList(&images,pyramid_image);
2065 }
2066 }
2067 /*
2068 Write pyramid-encoded TIFF image.
2069 */
cristy58567532009-09-26 01:13:32 +00002070 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002071 *write_info->magick='\0';
2072 write_info->adjoin=MagickTrue;
2073 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2074 images=DestroyImageList(images);
2075 write_info=DestroyImageInfo(write_info);
2076 return(status);
2077}
2078#endif
2079
2080#if defined(MAGICKCORE_TIFF_DELEGATE)
2081/*
2082%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2083% %
2084% %
2085% W r i t e T I F F I m a g e %
2086% %
2087% %
2088% %
2089%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2090%
2091% WriteTIFFImage() writes an image in the Tagged image file format.
2092%
2093% The format of the WriteTIFFImage method is:
2094%
2095% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2096% Image *image)
2097%
2098% A description of each parameter follows:
2099%
2100% o image_info: the image info.
2101%
2102% o image: The image.
2103%
2104*/
2105
2106typedef struct _TIFFInfo
2107{
2108 RectangleInfo
2109 tile_geometry;
2110
2111 unsigned char
2112 *scanline,
2113 *scanlines,
2114 *pixels;
2115} TIFFInfo;
2116
2117static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2118{
2119 assert(tiff_info != (TIFFInfo *) NULL);
2120 if (tiff_info->scanlines != (unsigned char *) NULL)
2121 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2122 tiff_info->scanlines);
2123 if (tiff_info->pixels != (unsigned char *) NULL)
2124 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2125 tiff_info->pixels);
2126}
2127
2128static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2129 TIFFInfo *tiff_info)
2130{
2131 const char
2132 *option;
2133
2134 MagickStatusType
2135 flags;
2136
cristy79e5dad2010-09-16 19:48:33 +00002137 uint32
2138 tile_columns,
2139 tile_rows;
2140
cristy3ed852e2009-09-05 21:47:34 +00002141 assert(tiff_info != (TIFFInfo *) NULL);
2142 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2143 option=GetImageOption(image_info,"tiff:tile-geometry");
2144 if (option == (const char *) NULL)
2145 return(MagickTrue);
2146 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2147 if ((flags & HeightValue) == 0)
2148 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002149 tile_columns=(uint32) tiff_info->tile_geometry.width;
2150 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002151 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2152 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2153 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2154 tiff_info->tile_geometry.width=tile_columns;
2155 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002156 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002157 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002158 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002159 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002160 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2161 (tiff_info->pixels == (unsigned char *) NULL))
2162 {
2163 DestroyTIFFInfo(tiff_info);
2164 return(MagickFalse);
2165 }
2166 return(MagickTrue);
2167}
2168
cristybb503372010-05-27 20:51:26 +00002169static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002170 tsample_t sample,Image *image)
2171{
2172 int32
2173 status;
2174
cristybb503372010-05-27 20:51:26 +00002175 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002176 i;
2177
2178 register unsigned char
2179 *p,
2180 *q;
2181
cristybb503372010-05-27 20:51:26 +00002182 size_t
cristy3ed852e2009-09-05 21:47:34 +00002183 number_tiles,
2184 tile_width;
2185
cristyc6da28e2011-04-28 01:41:35 +00002186 ssize_t
2187 bytes_per_pixel,
2188 j,
2189 k,
2190 l;
2191
cristy3ed852e2009-09-05 21:47:34 +00002192 if (TIFFIsTiled(tiff) == 0)
2193 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2194 /*
2195 Fill scanlines to tile height.
2196 */
cristybb503372010-05-27 20:51:26 +00002197 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002198 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2199 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002200 if (((size_t) (row % tiff_info->tile_geometry.height) !=
cristyc6da28e2011-04-28 01:41:35 +00002201 (tiff_info->tile_geometry.height-1)) &&
2202 (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002203 return(0);
2204 /*
2205 Write tile to TIFF image.
2206 */
2207 status=0;
cristybb503372010-05-27 20:51:26 +00002208 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002209 tiff_info->tile_geometry.width);
2210 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2211 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002212 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002213 {
cristybb503372010-05-27 20:51:26 +00002214 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002215 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002216 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2217 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002218 {
2219 if (bytes_per_pixel == 0)
2220 {
2221 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2222 tiff_info->tile_geometry.width+k)/8);
2223 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2224 *q++=(*p++);
2225 continue;
2226 }
2227 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2228 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2229 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2230 for (l=0; l < bytes_per_pixel; l++)
2231 *q++=(*p++);
2232 }
cristydaff8092010-04-22 14:50:53 +00002233 if ((i*tiff_info->tile_geometry.width) != image->columns)
2234 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2235 tiff_info->tile_geometry.width),(uint32) ((row/
2236 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2237 sample);
cristy3ed852e2009-09-05 21:47:34 +00002238 if (status < 0)
2239 break;
2240 }
2241 return(status);
2242}
2243
2244static void TIFFSetProfiles(TIFF *tiff,Image *image)
2245{
2246 const char
2247 *name;
2248
2249 const StringInfo
2250 *profile;
2251
2252 if (image->profiles == (void *) NULL)
2253 return;
2254 ResetImageProfileIterator(image);
2255 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2256 {
2257 profile=GetImageProfile(image,name);
2258#if defined(TIFFTAG_XMLPACKET)
2259 if (LocaleCompare(name,"xmp") == 0)
2260 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2261 profile),GetStringInfoDatum(profile));
2262#endif
2263#if defined(TIFFTAG_ICCPROFILE)
2264 if (LocaleCompare(name,"icc") == 0)
2265 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2266 profile),GetStringInfoDatum(profile));
2267#endif
2268 if (LocaleCompare(name,"iptc") == 0)
2269 {
2270 size_t
2271 length;
2272
2273 StringInfo
2274 *iptc_profile;
2275
2276 iptc_profile=CloneStringInfo(profile);
2277 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2278 0x03);
2279 SetStringInfoLength(iptc_profile,length);
2280 if (TIFFIsByteSwapped(tiff))
2281 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002282 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002283 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2284 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2285 iptc_profile=DestroyStringInfo(iptc_profile);
2286 }
2287#if defined(TIFFTAG_PHOTOSHOP)
2288 if (LocaleCompare(name,"8bim") == 0)
2289 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2290 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2291#endif
2292 if (LocaleCompare(name,"tiff:37724") == 0)
2293 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2294 GetStringInfoDatum(profile));
2295 name=GetNextImageProfile(image);
2296 }
2297}
2298
2299static void TIFFSetProperties(TIFF *tiff,Image *image)
2300{
2301 const char
2302 *value;
2303
cristy15d8d212010-09-17 01:56:30 +00002304 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002305 value=GetImageProperty(image,"tiff:hostcomputer");
2306 if (value != (const char *) NULL)
2307 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2308 value=GetImageProperty(image,"tiff:artist");
2309 if (value != (const char *) NULL)
2310 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2311 value=GetImageProperty(image,"tiff:timestamp");
2312 if (value != (const char *) NULL)
2313 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2314 value=GetImageProperty(image,"tiff:make");
2315 if (value != (const char *) NULL)
2316 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2317 value=GetImageProperty(image,"tiff:model");
2318 if (value != (const char *) NULL)
2319 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristy15d8d212010-09-17 01:56:30 +00002320 value=GetImageProperty(image,"tiff:software");
2321 if (value != (const char *) NULL)
2322 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristy3ed852e2009-09-05 21:47:34 +00002323 value=GetImageProperty(image,"tiff:copyright");
2324 if (value != (const char *) NULL)
2325 (void) TIFFSetField(tiff,33432,value);
2326 value=GetImageProperty(image,"kodak-33423");
2327 if (value != (const char *) NULL)
2328 (void) TIFFSetField(tiff,33423,value);
2329 value=GetImageProperty(image,"kodak-36867");
2330 if (value != (const char *) NULL)
2331 (void) TIFFSetField(tiff,36867,value);
2332 value=GetImageProperty(image,"label");
2333 if (value != (const char *) NULL)
2334 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2335 value=GetImageProperty(image,"comment");
2336 if (value != (const char *) NULL)
2337 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2338}
2339
2340static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2341{
2342#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2343 const char
2344 *value;
2345
cristybb503372010-05-27 20:51:26 +00002346 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002347 i;
2348
2349 uint32
2350 offset;
2351
2352 /*
2353 Write EXIF properties.
2354 */
2355 offset=0;
2356 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2357 for (i=0; exif_info[i].tag != 0; i++)
2358 {
2359 value=GetImageProperty(image,exif_info[i].property);
2360 if (value == (const char *) NULL)
2361 continue;
2362 switch (exif_info[i].type)
2363 {
2364 case TIFF_ASCII:
2365 {
2366 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2367 break;
2368 }
2369 case TIFF_SHORT:
2370 {
2371 uint16
2372 shorty;
2373
cristyf2f27272009-12-17 14:48:46 +00002374 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002375 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2376 break;
2377 }
2378 case TIFF_LONG:
2379 {
2380 uint16
cristybb503372010-05-27 20:51:26 +00002381 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002382
cristybb503372010-05-27 20:51:26 +00002383 ssize_ty=(uint16) StringToLong(value);
2384 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002385 break;
2386 }
2387 case TIFF_RATIONAL:
2388 case TIFF_SRATIONAL:
2389 {
2390 float
2391 rational;
2392
cristyc1acd842011-05-19 23:05:47 +00002393 rational=InterpretLocaleValue(value,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002394 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2395 break;
2396 }
2397 default:
2398 break;
2399 }
2400 }
2401 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2402#else
2403 (void) tiff;
2404 (void) image;
2405#endif
2406}
2407
2408static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2409 Image *image)
2410{
2411#if !defined(TIFFDefaultStripSize)
2412#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2413#endif
2414
2415 const char
2416 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002417 *option;
cristy3ed852e2009-09-05 21:47:34 +00002418
2419 CompressionType
2420 compression;
2421
cristy6b032822010-06-29 16:52:32 +00002422 EndianType
2423 endian_type;
2424
cristy3ed852e2009-09-05 21:47:34 +00002425 MagickBooleanType
2426 debug,
2427 status;
2428
2429 MagickOffsetType
2430 scene;
2431
2432 QuantumInfo
2433 *quantum_info;
2434
2435 QuantumType
2436 quantum_type;
2437
cristybb503372010-05-27 20:51:26 +00002438 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002439 i;
2440
2441 size_t
cristy5ecaba72011-02-18 02:05:32 +00002442 length,
2443 lsb_first;
cristy3ed852e2009-09-05 21:47:34 +00002444
cristyc6da28e2011-04-28 01:41:35 +00002445 ssize_t
2446 y;
2447
cristy3ed852e2009-09-05 21:47:34 +00002448 TIFF
2449 *tiff;
2450
2451 TIFFErrorHandler
2452 error_handler,
2453 warning_handler;
2454
2455 TIFFInfo
2456 tiff_info;
2457
2458 uint16
2459 bits_per_sample,
2460 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002461 endian,
cristy3ed852e2009-09-05 21:47:34 +00002462 photometric;
2463
2464 uint32
2465 rows_per_strip;
2466
2467 unsigned char
2468 *pixels;
2469
cristy3ed852e2009-09-05 21:47:34 +00002470 /*
2471 Open TIFF file.
2472 */
2473 assert(image_info != (const ImageInfo *) NULL);
2474 assert(image_info->signature == MagickSignature);
2475 assert(image != (Image *) NULL);
2476 assert(image->signature == MagickSignature);
2477 if (image->debug != MagickFalse)
2478 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2479 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2480 if (status == MagickFalse)
2481 return(status);
2482 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2483 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2484 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002485 endian_type=UndefinedEndian;
2486 option=GetImageOption(image_info,"tiff:endian");
2487 if (option != (const char *) NULL)
2488 {
2489 if (LocaleNCompare(option,"msb",3) == 0)
2490 endian_type=MSBEndian;
2491 if (LocaleNCompare(option,"lsb",3) == 0)
2492 endian_type=LSBEndian;;
2493 }
2494 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002495 {
2496 case LSBEndian: mode="wl"; break;
2497 case MSBEndian: mode="wb"; break;
2498 default: mode="w"; break;
2499 }
2500#if defined(TIFF_VERSION_BIG)
2501 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002502 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002503 {
2504 case LSBEndian: mode="wl8"; break;
2505 case MSBEndian: mode="wb8"; break;
2506 default: mode="w8"; break;
2507 }
2508#endif
2509 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2510 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2511 TIFFUnmapBlob);
2512 if (tiff == (TIFF *) NULL)
2513 {
2514 (void) TIFFSetWarningHandler(warning_handler);
2515 (void) TIFFSetErrorHandler(error_handler);
2516 return(MagickFalse);
2517 }
2518 scene=0;
2519 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +00002520 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +00002521 do
2522 {
2523 /*
2524 Initialize TIFF fields.
2525 */
cristy5f1c1ff2010-12-23 21:38:06 +00002526 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002527 (image_info->type != OptimizeType))
2528 (void) SetImageType(image,image_info->type);
2529 quantum_info=AcquireQuantumInfo(image_info,image);
2530 if (quantum_info == (QuantumInfo *) NULL)
2531 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2532 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2533 (quantum_info->format == UndefinedQuantumFormat) &&
2534 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2535 {
2536 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2537 if (status == MagickFalse)
2538 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2539 }
2540 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2541 (GetPreviousImageInList(image) != (Image *) NULL))
2542 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2543 if ((image->columns != (uint32) image->columns) ||
2544 (image->rows != (uint32) image->rows))
2545 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2546 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2547 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2548 compression=image->compression;
2549 if (image_info->compression != UndefinedCompression)
2550 compression=image_info->compression;
2551 switch (compression)
2552 {
2553 case FaxCompression:
2554 {
2555 compress_tag=COMPRESSION_CCITTFAX3;
2556 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2557 break;
2558 }
2559 case Group4Compression:
2560 {
2561 compress_tag=COMPRESSION_CCITTFAX4;
2562 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2563 break;
2564 }
cristy6d5e20f2011-04-25 13:48:54 +00002565#if defined(COMPRESSION_JBIG)
2566 case JBIG1Compression:
2567 {
2568 compress_tag=COMPRESSION_JBIG;
2569 break;
2570 }
2571#endif
cristy3ed852e2009-09-05 21:47:34 +00002572 case JPEGCompression:
2573 {
2574 compress_tag=COMPRESSION_JPEG;
2575 break;
2576 }
cristyfbb0ef02010-12-19 02:32:11 +00002577#if defined(COMPRESSION_LZMA)
2578 case LZMACompression:
2579 {
2580 compress_tag=COMPRESSION_LZMA;
2581 break;
2582 }
2583#endif
cristy3ed852e2009-09-05 21:47:34 +00002584 case LZWCompression:
2585 {
2586 compress_tag=COMPRESSION_LZW;
2587 break;
2588 }
2589 case RLECompression:
2590 {
2591 compress_tag=COMPRESSION_PACKBITS;
2592 break;
2593 }
2594 case ZipCompression:
2595 {
2596 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2597 break;
2598 }
2599 case NoCompression:
2600 default:
2601 {
2602 compress_tag=COMPRESSION_NONE;
2603 break;
2604 }
2605 }
cristy5b675872009-10-21 13:12:00 +00002606#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2607 if ((compress_tag != COMPRESSION_NONE) &&
2608 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002609 {
cristy5b675872009-10-21 13:12:00 +00002610 (void) ThrowMagickException(&image->exception,GetMagickModule(),
cristy042ee782011-04-22 18:48:30 +00002611 CoderError,"CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002612 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002613 compress_tag=COMPRESSION_NONE;
2614 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002615 }
2616#else
2617 switch (compress_tag)
2618 {
2619#if defined(CCITT_SUPPORT)
2620 case COMPRESSION_CCITTFAX3:
2621 case COMPRESSION_CCITTFAX4:
2622#endif
2623#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2624 case COMPRESSION_JPEG:
2625#endif
cristyfbb0ef02010-12-19 02:32:11 +00002626#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2627 case COMPRESSION_LZMA:
2628#endif
cristy3ed852e2009-09-05 21:47:34 +00002629#if defined(LZW_SUPPORT)
2630 case COMPRESSION_LZW:
2631#endif
2632#if defined(PACKBITS_SUPPORT)
2633 case COMPRESSION_PACKBITS:
2634#endif
2635#if defined(ZIP_SUPPORT)
2636 case COMPRESSION_ADOBE_DEFLATE:
2637#endif
2638 case COMPRESSION_NONE:
2639 break;
2640 default:
2641 {
2642 (void) ThrowMagickException(&image->exception,GetMagickModule(),
cristy042ee782011-04-22 18:48:30 +00002643 CoderError,"CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002644 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002645 compress_tag=COMPRESSION_NONE;
2646 compression=NoCompression;
2647 break;
2648 }
2649 }
2650#endif
2651 switch (compression)
2652 {
2653 case FaxCompression:
2654 case Group4Compression:
2655 {
2656 (void) SetImageType(image,BilevelType);
2657 break;
2658 }
2659 case JPEGCompression:
2660 {
2661 (void) SetImageStorageClass(image,DirectClass);
2662 (void) SetImageDepth(image,8);
2663 break;
2664 }
2665 default:
2666 break;
2667 }
cristy3ed852e2009-09-05 21:47:34 +00002668 if (image->colorspace == CMYKColorspace)
2669 {
2670 photometric=PHOTOMETRIC_SEPARATED;
2671 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2672 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2673 }
2674 else
2675 {
2676 /*
2677 Full color TIFF raster.
2678 */
2679 if (image->colorspace == LabColorspace)
2680 photometric=PHOTOMETRIC_CIELAB;
2681 else
2682 if (image->colorspace == YCbCrColorspace)
2683 {
2684 photometric=PHOTOMETRIC_YCBCR;
2685 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy79e5dad2010-09-16 19:48:33 +00002686 (void) SetImageStorageClass(image,DirectClass);
cristy3ed852e2009-09-05 21:47:34 +00002687 (void) SetImageDepth(image,8);
2688 }
2689 else
2690 {
2691 if (image->colorspace != RGBColorspace)
2692 (void) TransformImageColorspace(image,RGBColorspace);
2693 photometric=PHOTOMETRIC_RGB;
2694 }
2695 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2696 if ((image_info->type != TrueColorType) &&
2697 (image_info->type != TrueColorMatteType))
2698 {
cristy94c8fe42009-10-06 01:57:36 +00002699 if ((image_info->type != PaletteType) &&
2700 (IsGrayImage(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002701 {
cristy94c8fe42009-10-06 01:57:36 +00002702 photometric=(uint16) (quantum_info->min_is_white !=
2703 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2704 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002705 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002706 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy94c8fe42009-10-06 01:57:36 +00002707 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2708 {
2709 status=SetQuantumDepth(image,quantum_info,1);
2710 if (status == MagickFalse)
2711 ThrowWriterException(ResourceLimitError,
2712 "MemoryAllocationFailed");
2713 }
cristy3ed852e2009-09-05 21:47:34 +00002714 }
2715 else
cristy94c8fe42009-10-06 01:57:36 +00002716 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002717 {
cristybb503372010-05-27 20:51:26 +00002718 size_t
cristy94c8fe42009-10-06 01:57:36 +00002719 depth;
2720
2721 /*
2722 Colormapped TIFF raster.
2723 */
cristy3ed852e2009-09-05 21:47:34 +00002724 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002725 photometric=PHOTOMETRIC_PALETTE;
2726 depth=1;
2727 while ((GetQuantumRange(depth)+1) < image->colors)
2728 depth<<=1;
2729 status=SetQuantumDepth(image,quantum_info,depth);
2730 if (status == MagickFalse)
2731 ThrowWriterException(ResourceLimitError,
2732 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002733 }
2734 }
2735 }
cristyf2687ca2010-06-29 16:32:38 +00002736 switch (image->endian)
2737 {
2738 case LSBEndian:
2739 {
2740 endian=FILLORDER_LSB2MSB;
2741 break;
2742 }
2743 case MSBEndian:
2744 {
2745 endian=FILLORDER_MSB2LSB;
2746 break;
2747 }
2748 case UndefinedEndian:
2749 default:
2750 {
2751 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2752 break;
2753 }
2754 }
2755 lsb_first=1;
2756 image->endian=MSBEndian;
2757 if ((int) (*(char *) &lsb_first) != 0)
2758 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002759 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2760 (photometric != PHOTOMETRIC_MINISWHITE))
2761 {
2762 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002763 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002764 }
cristy0accf6e2009-10-01 13:14:28 +00002765 else
cristydaff8092010-04-22 14:50:53 +00002766 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2767 (photometric != PHOTOMETRIC_MINISWHITE))
2768 {
2769 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002770 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002771 }
cristy62e282b2010-06-29 01:27:13 +00002772 option=GetImageProperty(image,"tiff:fill-order");
2773 if (option != (const char *) NULL)
2774 {
2775 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002776 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002777 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002778 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002779 }
cristyf2687ca2010-06-29 16:32:38 +00002780 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2781 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002782 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2783 if (image->matte != MagickFalse)
2784 {
2785 uint16
2786 extra_samples,
2787 sample_info[1],
2788 samples_per_pixel;
2789
2790 /*
2791 TIFF has a matte channel.
2792 */
2793 extra_samples=1;
2794 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2795 option=GetImageProperty(image,"tiff:alpha");
2796 if ((option != (const char *) NULL) &&
2797 (LocaleCompare(option,"associated") == 0))
2798 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2799 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2800 &samples_per_pixel);
2801 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2802 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2803 &sample_info);
2804 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2805 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2806 }
2807 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2808 switch (quantum_info->format)
2809 {
2810 case FloatingPointQuantumFormat:
2811 {
2812 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2813 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2814 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2815 break;
2816 }
2817 case SignedQuantumFormat:
2818 {
2819 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2820 break;
2821 }
2822 case UnsignedQuantumFormat:
2823 {
2824 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2825 break;
2826 }
2827 default:
2828 break;
2829 }
cristy3ed852e2009-09-05 21:47:34 +00002830 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2831 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2832 if (photometric == PHOTOMETRIC_RGB)
2833 if ((image_info->interlace == PlaneInterlace) ||
2834 (image_info->interlace == PartitionInterlace))
2835 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2836 rows_per_strip=1;
2837 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002838 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2839 1);
cristy3ed852e2009-09-05 21:47:34 +00002840 option=GetImageOption(image_info,"tiff:rows-per-strip");
2841 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002842 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002843 switch (compress_tag)
2844 {
2845 case COMPRESSION_JPEG:
2846 {
2847#if defined(JPEG_SUPPORT)
2848 const char
2849 *sampling_factor;
2850
2851 GeometryInfo
2852 geometry_info;
2853
2854 MagickStatusType
2855 flags;
2856
2857 rows_per_strip+=(16-(rows_per_strip % 16));
2858 if (image->quality != 0)
2859 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2860 if (image_info->quality != UndefinedCompressionQuality)
2861 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2862 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2863 if (image->colorspace == RGBColorspace)
2864 {
cristy949bf5b2010-05-08 02:47:03 +00002865 const char
2866 *value;
2867
cristy3ed852e2009-09-05 21:47:34 +00002868 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2869 sampling_factor=(const char *) NULL;
2870 value=GetImageProperty(image,"jpeg:sampling-factor");
2871 if (value != (char *) NULL)
2872 {
2873 sampling_factor=value;
2874 if (image->debug != MagickFalse)
2875 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2876 " Input sampling-factors=%s",sampling_factor);
2877 }
2878 if (image_info->sampling_factor != (char *) NULL)
2879 sampling_factor=image_info->sampling_factor;
2880 if (sampling_factor != (const char *) NULL)
2881 {
2882 flags=ParseGeometry(sampling_factor,&geometry_info);
2883 if ((flags & SigmaValue) == 0)
2884 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002885 if (image->colorspace == YCbCrColorspace)
2886 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2887 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00002888 }
2889 }
2890 if (bits_per_sample == 12)
2891 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2892#endif
2893 break;
2894 }
2895 case COMPRESSION_ADOBE_DEFLATE:
2896 {
cristyf6fe0a12010-05-30 00:44:47 +00002897 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002898 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2899 &bits_per_sample);
2900 if (((photometric == PHOTOMETRIC_RGB) ||
2901 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2902 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002903 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00002904 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2905 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00002906 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00002907 break;
2908 }
2909 case COMPRESSION_CCITTFAX3:
2910 {
2911 /*
2912 Byte-aligned EOL.
2913 */
cristyeaedf062010-05-29 22:36:02 +00002914 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002915 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2916 break;
2917 }
2918 case COMPRESSION_CCITTFAX4:
2919 {
cristyeaedf062010-05-29 22:36:02 +00002920 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002921 break;
2922 }
cristyef8f26b2010-12-19 20:29:16 +00002923#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2924 case COMPRESSION_LZMA:
2925 {
2926 if (((photometric == PHOTOMETRIC_RGB) ||
2927 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2928 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2929 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2930 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
2931 image_info->quality == UndefinedCompressionQuality ? 7 :
2932 MagickMin((ssize_t) image_info->quality/10,9)));
2933 break;
2934 }
2935#endif
cristy3ed852e2009-09-05 21:47:34 +00002936 case COMPRESSION_LZW:
2937 {
2938 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2939 &bits_per_sample);
2940 if (((photometric == PHOTOMETRIC_RGB) ||
2941 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2942 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002943 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00002944 break;
2945 }
2946 default:
2947 break;
2948 }
cristy79e5dad2010-09-16 19:48:33 +00002949 option=GetImageOption(image_info,"tiff:tile-geometry");
2950 if (option == (const char *) NULL)
2951 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy3ed852e2009-09-05 21:47:34 +00002952 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2953 {
2954 unsigned short
2955 units;
2956
2957 /*
2958 Set image resolution.
2959 */
2960 units=RESUNIT_NONE;
2961 if (image->units == PixelsPerInchResolution)
2962 units=RESUNIT_INCH;
2963 if (image->units == PixelsPerCentimeterResolution)
2964 units=RESUNIT_CENTIMETER;
2965 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2966 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2967 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2968 if ((image->page.x != 0) || (image->page.y != 0))
2969 {
2970 /*
2971 Set image position.
2972 */
2973 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2974 image->x_resolution);
2975 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2976 image->y_resolution);
2977 }
2978 }
2979 if (image->chromaticity.white_point.x != 0.0)
2980 {
2981 float
2982 chromaticity[6];
2983
2984 /*
2985 Set image chromaticity.
2986 */
2987 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2988 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2989 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2990 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2991 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2992 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2993 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2994 chromaticity[0]=(float) image->chromaticity.white_point.x;
2995 chromaticity[1]=(float) image->chromaticity.white_point.y;
2996 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2997 }
2998 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2999 {
3000 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3001 if (image->scene != 0)
3002 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3003 GetImageListLength(image));
3004 }
3005 if (image->orientation != UndefinedOrientation)
3006 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3007 (void) TIFFSetProfiles(tiff,image);
3008 {
3009 uint16
3010 page,
3011 pages;
3012
3013 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00003014 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00003015 if ((image_info->adjoin != MagickFalse) && (pages > 1))
3016 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3017 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3018 }
3019 (void) TIFFSetProperties(tiff,image);
3020 if (0)
3021 (void) TIFFSetEXIFProperties(tiff,image);
3022 /*
3023 Write image scanlines.
3024 */
3025 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3026 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3027 pixels=GetQuantumPixels(quantum_info);
3028 tiff_info.scanline=GetQuantumPixels(quantum_info);
3029 switch (photometric)
3030 {
3031 case PHOTOMETRIC_CIELAB:
3032 case PHOTOMETRIC_YCBCR:
3033 case PHOTOMETRIC_RGB:
3034 {
3035 /*
3036 RGB TIFF image.
3037 */
3038 switch (image_info->interlace)
3039 {
3040 case NoInterlace:
3041 default:
3042 {
3043 quantum_type=RGBQuantum;
3044 if (image->matte != MagickFalse)
3045 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003046 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003047 {
3048 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003049 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003050
3051 p=GetVirtualPixels(image,0,y,image->columns,1,
3052 &image->exception);
3053 if (p == (const PixelPacket *) NULL)
3054 break;
3055 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3056 quantum_info,quantum_type,pixels,&image->exception);
cristyda16f162011-02-19 23:52:17 +00003057 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00003058 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3059 break;
3060 if (image->previous == (Image *) NULL)
3061 {
cristy2837bcc2010-08-07 23:57:39 +00003062 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3063 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003064 if (status == MagickFalse)
3065 break;
3066 }
3067 }
3068 break;
3069 }
3070 case PlaneInterlace:
3071 case PartitionInterlace:
3072 {
3073 /*
3074 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3075 */
cristybb503372010-05-27 20:51:26 +00003076 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003077 {
3078 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003079 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003080
3081 p=GetVirtualPixels(image,0,y,image->columns,1,
3082 &image->exception);
3083 if (p == (const PixelPacket *) NULL)
3084 break;
3085 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3086 quantum_info,RedQuantum,pixels,&image->exception);
3087 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3088 break;
3089 }
3090 if (image->previous == (Image *) NULL)
3091 {
3092 status=SetImageProgress(image,SaveImageTag,100,400);
3093 if (status == MagickFalse)
3094 break;
3095 }
cristybb503372010-05-27 20:51:26 +00003096 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003097 {
3098 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003099 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003100
3101 p=GetVirtualPixels(image,0,y,image->columns,1,
3102 &image->exception);
3103 if (p == (const PixelPacket *) NULL)
3104 break;
3105 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3106 quantum_info,GreenQuantum,pixels,&image->exception);
3107 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3108 break;
3109 }
3110 if (image->previous == (Image *) NULL)
3111 {
3112 status=SetImageProgress(image,SaveImageTag,200,400);
3113 if (status == MagickFalse)
3114 break;
3115 }
cristybb503372010-05-27 20:51:26 +00003116 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003117 {
3118 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003119 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003120
3121 p=GetVirtualPixels(image,0,y,image->columns,1,
3122 &image->exception);
3123 if (p == (const PixelPacket *) NULL)
3124 break;
3125 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3126 quantum_info,BlueQuantum,pixels,&image->exception);
3127 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3128 break;
3129 }
3130 if (image->previous == (Image *) NULL)
3131 {
3132 status=SetImageProgress(image,SaveImageTag,300,400);
3133 if (status == MagickFalse)
3134 break;
3135 }
3136 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003137 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003138 {
3139 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003140 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003141
3142 p=GetVirtualPixels(image,0,y,image->columns,1,
3143 &image->exception);
3144 if (p == (const PixelPacket *) NULL)
3145 break;
3146 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3147 quantum_info,AlphaQuantum,pixels,&image->exception);
3148 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3149 break;
3150 }
3151 if (image->previous == (Image *) NULL)
3152 {
3153 status=SetImageProgress(image,SaveImageTag,400,400);
3154 if (status == MagickFalse)
3155 break;
3156 }
3157 break;
3158 }
3159 }
3160 break;
3161 }
3162 case PHOTOMETRIC_SEPARATED:
3163 {
3164 /*
3165 CMYK TIFF image.
3166 */
3167 quantum_type=CMYKQuantum;
3168 if (image->matte != MagickFalse)
3169 quantum_type=CMYKAQuantum;
3170 if (image->colorspace != CMYKColorspace)
3171 (void) TransformImageColorspace(image,CMYKColorspace);
cristybb503372010-05-27 20:51:26 +00003172 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003173 {
3174 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003175 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003176
3177 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3178 if (p == (const PixelPacket *) NULL)
3179 break;
3180 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3181 quantum_info,quantum_type,pixels,&image->exception);
3182 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3183 break;
3184 if (image->previous == (Image *) NULL)
3185 {
cristycee97112010-05-28 00:44:52 +00003186 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3187 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003188 if (status == MagickFalse)
3189 break;
3190 }
3191 }
3192 break;
3193 }
3194 case PHOTOMETRIC_PALETTE:
3195 {
3196 uint16
3197 *blue,
3198 *green,
3199 *red;
3200
3201 /*
3202 Colormapped TIFF image.
3203 */
3204 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3205 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3206 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3207 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3208 (blue == (uint16 *) NULL))
3209 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3210 /*
3211 Initialize TIFF colormap.
3212 */
3213 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3214 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3215 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003216 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003217 {
3218 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3219 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3220 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3221 }
3222 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3223 red=(uint16 *) RelinquishMagickMemory(red);
3224 green=(uint16 *) RelinquishMagickMemory(green);
3225 blue=(uint16 *) RelinquishMagickMemory(blue);
3226 }
3227 default:
3228 {
3229 /*
3230 Convert PseudoClass packets to contiguous grayscale scanlines.
3231 */
3232 quantum_type=IndexQuantum;
3233 if (image->matte != MagickFalse)
3234 {
3235 if (photometric != PHOTOMETRIC_PALETTE)
3236 quantum_type=GrayAlphaQuantum;
3237 else
3238 quantum_type=IndexAlphaQuantum;
3239 }
3240 else
3241 if (photometric != PHOTOMETRIC_PALETTE)
3242 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003243 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003244 {
3245 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003246 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003247
3248 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3249 if (p == (const PixelPacket *) NULL)
3250 break;
3251 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3252 quantum_info,quantum_type,pixels,&image->exception);
3253 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3254 break;
3255 if (image->previous == (Image *) NULL)
3256 {
cristycee97112010-05-28 00:44:52 +00003257 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3258 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003259 if (status == MagickFalse)
3260 break;
3261 }
3262 }
3263 break;
3264 }
3265 }
3266 quantum_info=DestroyQuantumInfo(quantum_info);
3267 DestroyTIFFInfo(&tiff_info);
3268 if (0 && (image_info->verbose == MagickTrue))
3269 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3270 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003271 image->endian=MSBEndian;
3272 if (endian == FILLORDER_LSB2MSB)
3273 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003274 image=SyncNextImageInList(image);
3275 if (image == (Image *) NULL)
3276 break;
3277 status=SetImageProgress(image,SaveImagesTag,scene++,
3278 GetImageListLength(image));
3279 if (status == MagickFalse)
3280 break;
3281 } while (image_info->adjoin != MagickFalse);
3282 (void) TIFFSetWarningHandler(warning_handler);
3283 (void) TIFFSetErrorHandler(error_handler);
3284 TIFFClose(tiff);
3285 return(MagickTrue);
3286}
3287#endif