blob: 4cdcd2770e16090cfa84da3e40e809114dd25122 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% TTTTT IIIII FFFFF FFFFF %
7% T I F F %
8% T I FFF FFF %
9% T I F F %
10% T IIIII F F %
11% %
12% %
13% Read/Write TIFF Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "magick/studio.h"
cristy5a2ca482009-10-14 18:24:56 +000043#include "magick/attribute.h"
cristy3ed852e2009-09-05 21:47:34 +000044#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/cache.h"
47#include "magick/color.h"
48#include "magick/color-private.h"
cristyf4cf3072010-04-25 20:07:02 +000049#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000050#include "magick/colorspace.h"
51#include "magick/constitute.h"
52#include "magick/enhance.h"
53#include "magick/exception.h"
54#include "magick/exception-private.h"
55#include "magick/geometry.h"
56#include "magick/image.h"
57#include "magick/image-private.h"
58#include "magick/list.h"
59#include "magick/log.h"
60#include "magick/magick.h"
61#include "magick/memory_.h"
62#include "magick/module.h"
63#include "magick/monitor.h"
64#include "magick/monitor-private.h"
65#include "magick/option.h"
66#include "magick/property.h"
67#include "magick/quantum.h"
68#include "magick/quantum-private.h"
69#include "magick/profile.h"
70#include "magick/resize.h"
cristy3d7f8062009-09-24 20:45:35 +000071#include "magick/resource_.h"
cristy3ed852e2009-09-05 21:47:34 +000072#include "magick/semaphore.h"
73#include "magick/splay-tree.h"
74#include "magick/static.h"
75#include "magick/statistic.h"
76#include "magick/string_.h"
cristyf2f27272009-12-17 14:48:46 +000077#include "magick/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000078#include "magick/thread_.h"
79#include "magick/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000080#if defined(MAGICKCORE_TIFF_DELEGATE)
81# if defined(MAGICKCORE_HAVE_TIFFCONF_H)
82# include "tiffconf.h"
cristyef8f26b2010-12-19 20:29:16 +000083# endif
cristy3ed852e2009-09-05 21:47:34 +000084# include "tiff.h"
85# include "tiffio.h"
86# if !defined(COMPRESSION_ADOBE_DEFLATE)
87# define COMPRESSION_ADOBE_DEFLATE 8
88# endif
cristyef8f26b2010-12-19 20:29:16 +000089# if !defined(PREDICTOR_HORIZONTAL)
90# define PREDICTOR_HORIZONTAL 2
91# endif
cristy3ed852e2009-09-05 21:47:34 +000092
93/*
94 Typedef declarations.
95*/
96#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
97typedef struct _ExifInfo
98{
99 unsigned int
100 tag,
101 type;
102
103 const char
104 *property;
105} ExifInfo;
106
107static const ExifInfo
108 exif_info[] = {
109 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
110 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
111 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
112 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000113 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy3ed852e2009-09-05 21:47:34 +0000114 { EXIFTAG_OECF, TIFF_UNDEFINED, "exif:OptoelectricConversionFactor" },
115 { EXIFTAG_EXIFVERSION, TIFF_UNDEFINED, "exif:ExifVersion" },
116 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
117 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
118 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_UNDEFINED, "exif:ComponentsConfiguration" },
119 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
120 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
121 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
122 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
123 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
124 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
125 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
126 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
127 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
128 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
129 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
130 /* { EXIFTAG_SUBJECTAREA, TIFF_SHORT, "exif:SubjectArea" }, */
131 { EXIFTAG_MAKERNOTE, TIFF_UNDEFINED, "exif:MakerNote" },
132 { EXIFTAG_USERCOMMENT, TIFF_UNDEFINED, "exif:UserComment" },
133 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
134 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
135 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
136 { EXIFTAG_FLASHPIXVERSION, TIFF_UNDEFINED, "exif:FlashpixVersion" },
137 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
138 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
139 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
140 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
141 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
142 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
143 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_UNDEFINED, "exif:SpatialFrequencyResponse" },
144 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
145 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
146 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
147 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
148 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
149 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
150 { EXIFTAG_FILESOURCE, TIFF_UNDEFINED, "exif:FileSource" },
151 { EXIFTAG_SCENETYPE, TIFF_UNDEFINED, "exif:SceneType" },
152 { EXIFTAG_CFAPATTERN, TIFF_UNDEFINED, "exif:CFAPattern" },
153 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
154 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
155 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
156 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
157 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
158 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
159 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
160 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
161 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
162 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
163 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_UNDEFINED, "exif:DeviceSettingDescription" },
164 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
165 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
166 { 0, 0, (char *) NULL }
167};
168#endif
cristy0805e062011-01-30 02:56:36 +0000169#endif /* MAGICKCORE_TIFF_DELEGATE */
cristy3ed852e2009-09-05 21:47:34 +0000170
171/*
172 Global declarations.
173*/
174static MagickThreadKey
175 tiff_exception;
176
177static SemaphoreInfo
178 *tiff_semaphore = (SemaphoreInfo *) NULL;
179
180static volatile MagickBooleanType
181 instantiate_key = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000182
183/*
184 Forward declarations.
185*/
186#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000187static Image *
188 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
189
cristy3ed852e2009-09-05 21:47:34 +0000190static MagickBooleanType
cristy3d7f8062009-09-24 20:45:35 +0000191 WriteGROUP4Image(const ImageInfo *,Image *),
cristy3ed852e2009-09-05 21:47:34 +0000192 WritePTIFImage(const ImageInfo *,Image *),
193 WriteTIFFImage(const ImageInfo *,Image *);
194#endif
195
196/*
197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198% %
199% %
200% %
201% I s T I F F %
202% %
203% %
204% %
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206%
207% IsTIFF() returns MagickTrue if the image format type, identified by the
208% magick string, is TIFF.
209%
210% The format of the IsTIFF method is:
211%
212% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
213%
214% A description of each parameter follows:
215%
216% o magick: compare image format pattern against these bytes.
217%
218% o length: Specifies the length of the magick string.
219%
220*/
221static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
222{
223 if (length < 4)
224 return(MagickFalse);
225 if (memcmp(magick,"\115\115\000\052",4) == 0)
226 return(MagickTrue);
227 if (memcmp(magick,"\111\111\052\000",4) == 0)
228 return(MagickTrue);
229#if defined(TIFF_VERSION_BIG)
230 if (length < 8)
231 return(MagickFalse);
232 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
233 return(MagickTrue);
234 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
235 return(MagickTrue);
236#endif
237 return(MagickFalse);
238}
239
240#if defined(MAGICKCORE_TIFF_DELEGATE)
241/*
242%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243% %
244% %
245% %
cristy3d7f8062009-09-24 20:45:35 +0000246% R e a d G R O U P 4 I m a g e %
247% %
248% %
249% %
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251%
252% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
253% allocates the memory necessary for the new Image structure and returns a
254% pointer to the new image.
255%
256% The format of the ReadGROUP4Image method is:
257%
258% Image *ReadGROUP4Image(const ImageInfo *image_info,
259% ExceptionInfo *exception)
260%
261% A description of each parameter follows:
262%
263% o image_info: the image info.
264%
265% o exception: return any errors or warnings in this structure.
266%
267*/
268
cristyeaedf062010-05-29 22:36:02 +0000269static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000270{
271 unsigned char
272 buffer[4];
273
274 buffer[0]=(unsigned char) value;
275 buffer[1]=(unsigned char) (value >> 8);
276 buffer[2]=(unsigned char) (value >> 16);
277 buffer[3]=(unsigned char) (value >> 24);
278 return(fwrite(buffer,1,4,file));
279}
280
281static Image *ReadGROUP4Image(const ImageInfo *image_info,
282 ExceptionInfo *exception)
283{
284 char
285 filename[MaxTextExtent];
286
287 FILE
288 *file;
289
290 Image
291 *image;
292
293 ImageInfo
294 *read_info;
295
296 int
297 c,
298 unique_file;
299
300 MagickBooleanType
301 status;
302
303 size_t
304 length;
305
306 ssize_t
307 offset,
308 strip_offset;
309
310 /*
311 Open image file.
312 */
313 assert(image_info != (const ImageInfo *) NULL);
314 assert(image_info->signature == MagickSignature);
315 if (image_info->debug != MagickFalse)
316 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
317 image_info->filename);
318 assert(exception != (ExceptionInfo *) NULL);
319 assert(exception->signature == MagickSignature);
320 image=AcquireImage(image_info);
321 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
322 if (status == MagickFalse)
323 {
324 image=DestroyImageList(image);
325 return((Image *) NULL);
326 }
327 /*
328 Write raw CCITT Group 4 wrapped as a TIFF image file.
329 */
330 file=(FILE *) NULL;
331 unique_file=AcquireUniqueFileResource(filename);
332 if (unique_file != -1)
333 file=fdopen(unique_file,"wb");
334 if ((unique_file == -1) || (file == (FILE *) NULL))
335 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
336 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
337 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
338 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
339 length=WriteLSBLong(file,image->columns);
340 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
341 length=WriteLSBLong(file,image->rows);
342 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
343 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
344 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
345 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
346 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000347 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000348 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000349 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000350 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
351 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000352 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000353 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
354 offset=(ssize_t) ftell(file)-4;
355 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000356 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000357 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000358 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000359 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
360 length=fwrite("\000\000\000\000",1,4,file);
361 length=WriteLSBLong(file,image->x_resolution);
362 length=WriteLSBLong(file,1);
363 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
364 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000365 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000366 length=WriteLSBLong(file,(unsigned int) length);
367 (void) fclose(file);
368 (void) CloseBlob(image);
369 image=DestroyImage(image);
370 /*
371 Read TIFF image.
372 */
cristy14efd992009-09-26 23:46:03 +0000373 read_info=CloneImageInfo((ImageInfo *) NULL);
cristy53a8bc92011-01-10 19:15:17 +0000374 (void) FormatMagickString(read_info->filename,MaxTextExtent,"%s",filename);
cristy3d7f8062009-09-24 20:45:35 +0000375 image=ReadTIFFImage(read_info,exception);
376 read_info=DestroyImageInfo(read_info);
377 if (image != (Image *) NULL)
378 {
379 (void) CopyMagickString(image->filename,image_info->filename,
380 MaxTextExtent);
381 (void) CopyMagickString(image->magick_filename,image_info->filename,
382 MaxTextExtent);
383 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
384 }
385 (void) RelinquishUniqueFileResource(filename);
386 return(image);
387}
388#endif
389
390#if defined(MAGICKCORE_TIFF_DELEGATE)
391/*
392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393% %
394% %
395% %
cristy3ed852e2009-09-05 21:47:34 +0000396% R e a d T I F F I m a g e %
397% %
398% %
399% %
400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401%
402% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
403% memory necessary for the new Image structure and returns a pointer to the
404% new image.
405%
406% The format of the ReadTIFFImage method is:
407%
408% Image *ReadTIFFImage(const ImageInfo *image_info,
409% ExceptionInfo *exception)
410%
411% A description of each parameter follows:
412%
413% o image_info: the image info.
414%
415% o exception: return any errors or warnings in this structure.
416%
417*/
418
419static inline size_t MagickMax(const size_t x,const size_t y)
420{
421 if (x > y)
422 return(x);
423 return(y);
424}
425
cristybb503372010-05-27 20:51:26 +0000426static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000427{
428 if (x < y)
429 return(x);
430 return(y);
431}
432
433static MagickBooleanType ReadProfile(Image *image,const char *name,
cristybb503372010-05-27 20:51:26 +0000434 unsigned char *datum,ssize_t length)
cristy3ed852e2009-09-05 21:47:34 +0000435{
436 MagickBooleanType
437 status;
438
cristybb503372010-05-27 20:51:26 +0000439 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000440 i;
441
442 StringInfo
443 *profile;
444
445 if (length < 4)
446 return(MagickFalse);
447 i=0;
448 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
449 {
450 for (i=0; i < (length-4); i+=2)
451 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
452 break;
453 if (i == length)
454 length-=i;
455 else
456 i=0;
457 if (length < 4)
458 return(MagickFalse);
459 }
460 profile=AcquireStringInfo((size_t) length);
461 SetStringInfoDatum(profile,datum+i);
462 status=SetImageProfile(image,name,profile);
463 profile=DestroyStringInfo(profile);
464 if (status == MagickFalse)
465 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
466 image->filename);
467 return(MagickTrue);
468}
469
470#if defined(__cplusplus) || defined(c_plusplus)
471extern "C" {
472#endif
473
474static int TIFFCloseBlob(thandle_t image)
475{
476 (void) CloseBlob((Image *) image);
477 return(0);
478}
479
480static void TIFFErrors(const char *module,const char *format,va_list error)
481{
482 char
483 message[MaxTextExtent];
484
485 ExceptionInfo
486 *exception;
487
488#if defined(MAGICKCORE_HAVE_VSNPRINTF)
489 (void) vsnprintf(message,MaxTextExtent,format,error);
490#else
491 (void) vsprintf(message,format,error);
492#endif
493 (void) ConcatenateMagickString(message,".",MaxTextExtent);
494 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
495 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000496 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
497 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000498}
499
500static void TIFFGetProfiles(TIFF *tiff,Image *image)
501{
502 uint32
503 length;
504
505 unsigned char
506 *profile;
507
508#if defined(TIFFTAG_ICCPROFILE)
509 length=0;
510 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000511 (void) ReadProfile(image,"icc",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000512#endif
513#if defined(TIFFTAG_PHOTOSHOP)
514 length=0;
515 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000516 (void) ReadProfile(image,"8bim",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000517#endif
518#if defined(TIFFTAG_RICHTIFFIPTC)
519 length=0;
520 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
521 {
522 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000523 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000524 (void) ReadProfile(image,"iptc",profile,4L*length);
525 }
526#endif
527#if defined(TIFFTAG_XMLPACKET)
528 length=0;
529 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000530 (void) ReadProfile(image,"xmp",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000531#endif
532 length=0;
533 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000534 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000535}
536
537static void TIFFGetProperties(TIFF *tiff,Image *image)
538{
539 char
540 *text;
541
542 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
543 (void) SetImageProperty(image,"tiff:artist",text);
544 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
545 (void) SetImageProperty(image,"tiff:timestamp",text);
546 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
547 (void) SetImageProperty(image,"tiff:software",text);
548 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
549 (void) SetImageProperty(image,"tiff:hostcomputer",text);
550 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
551 (void) SetImageProperty(image,"tiff:document",text);
552 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
553 (void) SetImageProperty(image,"tiff:make",text);
554 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
555 (void) SetImageProperty(image,"tiff:model",text);
556 if (TIFFGetField(tiff,33432,&text) == 1)
557 (void) SetImageProperty(image,"tiff:copyright",text);
558 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
559 (void) SetImageProperty(image,"label",text);
560 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
561 (void) SetImageProperty(image,"comment",text);
562}
563
564static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
565{
566#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
567 char
568 value[MaxTextExtent];
569
cristybb503372010-05-27 20:51:26 +0000570 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000571 i;
572
573 tdir_t
574 directory;
575
576 uint32
577 offset;
578
579 /*
580 Read EXIF properties.
581 */
582 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
583 return;
584 directory=TIFFCurrentDirectory(tiff);
585 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
586 return;
587 for (i=0; exif_info[i].tag != 0; i++)
588 {
589 *value='\0';
590 switch (exif_info[i].type)
591 {
592 case TIFF_ASCII:
593 {
594 char
595 *ascii;
596
597 if (TIFFGetField(tiff,exif_info[i].tag,&ascii) != 0)
598 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
599 break;
600 }
601 case TIFF_SHORT:
602 {
603 uint16
604 shorty;
605
606 if (TIFFGetField(tiff,exif_info[i].tag,&shorty) != 0)
607 (void) FormatMagickString(value,MaxTextExtent,"%d",shorty);
608 break;
609 }
610 case TIFF_LONG:
611 {
612 uint32
cristybb503372010-05-27 20:51:26 +0000613 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +0000614
cristybb503372010-05-27 20:51:26 +0000615 if (TIFFGetField(tiff,exif_info[i].tag,&ssize_ty) != 0)
616 (void) FormatMagickString(value,MaxTextExtent,"%d",ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +0000617 break;
618 }
619 case TIFF_RATIONAL:
620 case TIFF_SRATIONAL:
621 {
622 float
623 rational;
624
625 if (TIFFGetField(tiff,exif_info[i].tag,&rational) != 0)
cristye7f51092010-01-17 00:39:37 +0000626 (void) FormatMagickString(value,MaxTextExtent,"%g",rational);
cristy3ed852e2009-09-05 21:47:34 +0000627 break;
628 }
629 default:
630 break;
631 }
632 if (*value != '\0')
633 (void) SetImageProperty(image,exif_info[i].property,value);
634 }
635 TIFFSetDirectory(tiff,directory);
636#else
637 (void) tiff;
638 (void) image;
639#endif
640}
641
642static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
643{
644 *base=(tdata_t *) GetBlobStreamData((Image *) image);
645 if (*base != (tdata_t *) NULL)
646 *size=(toff_t) GetBlobSize((Image *) image);
647 if (*base != (tdata_t *) NULL)
648 return(1);
649 return(0);
650}
651
652static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
653{
654 tsize_t
655 count;
656
657 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
658 (unsigned char *) data);
659 return(count);
660}
661
cristybb503372010-05-27 20:51:26 +0000662static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
663 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000664{
665 int32
666 status;
667
668 (void) bits_per_sample;
669 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
670 return(status);
671}
672
673static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
674{
675 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
676}
677
678static toff_t TIFFGetBlobSize(thandle_t image)
679{
680 return((toff_t) GetBlobSize((Image *) image));
681}
682
683static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
684{
685 (void) image;
686 (void) base;
687 (void) size;
688}
689
690static void TIFFWarnings(const char *module,const char *format,va_list warning)
691{
692 char
693 message[MaxTextExtent];
694
695 ExceptionInfo
696 *exception;
697
698#if defined(MAGICKCORE_HAVE_VSNPRINTF)
699 (void) vsnprintf(message,MaxTextExtent,format,warning);
700#else
701 (void) vsprintf(message,format,warning);
702#endif
703 (void) ConcatenateMagickString(message,".",MaxTextExtent);
704 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
705 if (exception != (ExceptionInfo *) NULL)
706 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
707 message,"`%s'",module);
708}
709
710static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
711{
712 tsize_t
713 count;
714
715 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
716 (unsigned char *) data);
717 return(count);
718}
719
720#if defined(__cplusplus) || defined(c_plusplus)
721}
722#endif
723
724static Image *ReadTIFFImage(const ImageInfo *image_info,
725 ExceptionInfo *exception)
726{
727 typedef enum
728 {
729 ReadSingleSampleMethod,
730 ReadRGBAMethod,
731 ReadCMYKAMethod,
732 ReadStripMethod,
733 ReadTileMethod,
734 ReadGenericMethod
735 } TIFFMethodType;
736
737 const char
738 *option;
739
740 float
741 *chromaticity,
742 x_position,
743 y_position,
744 x_resolution,
745 y_resolution;
746
747 Image
748 *image;
749
cristybb503372010-05-27 20:51:26 +0000750 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000751 y;
752
753 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000754 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000755 debug,
756 status;
757
758 MagickSizeType
759 number_pixels;
760
761 QuantumInfo
762 *quantum_info;
763
764 QuantumType
765 quantum_type;
766
cristybb503372010-05-27 20:51:26 +0000767 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000768 i;
769
770 size_t
771 length,
772 pad;
773
774 TIFF
775 *tiff;
776
777 TIFFErrorHandler
778 error_handler,
779 warning_handler;
780
781 TIFFMethodType
782 method;
783
784 uint16
785 compress_tag,
786 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000787 endian,
cristy3ed852e2009-09-05 21:47:34 +0000788 extra_samples,
789 interlace,
790 max_sample_value,
791 min_sample_value,
792 orientation,
793 pages,
794 photometric,
795 *sample_info,
796 sample_format,
797 samples_per_pixel,
798 units,
799 value;
800
801 uint32
802 height,
803 rows_per_strip,
804 width;
805
806 unsigned char
807 *pixels;
808
cristyf2687ca2010-06-29 16:32:38 +0000809 size_t
810 lsb_first;
811
cristy3ed852e2009-09-05 21:47:34 +0000812 /*
813 Open image.
814 */
815 assert(image_info != (const ImageInfo *) NULL);
816 assert(image_info->signature == MagickSignature);
817 if (image_info->debug != MagickFalse)
818 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
819 image_info->filename);
820 assert(exception != (ExceptionInfo *) NULL);
821 assert(exception->signature == MagickSignature);
822 image=AcquireImage(image_info);
823 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
824 if (status == MagickFalse)
825 {
826 image=DestroyImageList(image);
827 return((Image *) NULL);
828 }
829 (void) MagickSetThreadValue(tiff_exception,exception);
830 error_handler=TIFFSetErrorHandler(TIFFErrors);
831 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
cristy1630f7f2011-02-18 01:10:02 +0000832 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
cristy3ed852e2009-09-05 21:47:34 +0000833 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
834 TIFFUnmapBlob);
835 if (tiff == (TIFF *) NULL)
836 {
837 (void) TIFFSetWarningHandler(warning_handler);
838 (void) TIFFSetErrorHandler(error_handler);
839 image=DestroyImageList(image);
840 return((Image *) NULL);
841 }
842 debug=IsEventLogging();
843 if (image_info->number_scenes != 0)
844 {
845 /*
846 Generate blank images for subimage specification (e.g. image.tif[4].
847 */
cristybb503372010-05-27 20:51:26 +0000848 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000849 {
850 (void) TIFFReadDirectory(tiff);
851 AcquireNextImage(image_info,image);
852 if (GetNextImageInList(image) == (Image *) NULL)
853 {
854 image=DestroyImageList(image);
855 return((Image *) NULL);
856 }
857 image=SyncNextImageInList(image);
858 }
859 }
860 do
861 {
862 if (0 && (image_info->verbose != MagickFalse))
863 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristy6b032822010-06-29 16:52:32 +0000864 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
865 "lsb" : "msb");
cristy3ed852e2009-09-05 21:47:34 +0000866 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
867 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
872 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
873 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
cristyae1319c2011-01-19 20:18:53 +0000874 if (sample_format == SAMPLEFORMAT_IEEEFP)
875 (void) SetImageProperty(image,"quantum:format","floating-point");
cristy3ed852e2009-09-05 21:47:34 +0000876 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
877 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
878 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
879 switch (photometric)
880 {
881 case PHOTOMETRIC_MINISBLACK:
882 {
883 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
884 break;
885 }
886 case PHOTOMETRIC_MINISWHITE:
887 {
888 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
889 break;
890 }
891 case PHOTOMETRIC_PALETTE:
892 {
893 (void) SetImageProperty(image,"tiff:photometric","palette");
894 break;
895 }
896 case PHOTOMETRIC_RGB:
897 {
898 (void) SetImageProperty(image,"tiff:photometric","RGB");
899 break;
900 }
901 case PHOTOMETRIC_CIELAB:
902 {
903 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
904 break;
905 }
906 case PHOTOMETRIC_SEPARATED:
907 {
908 (void) SetImageProperty(image,"tiff:photometric","separated");
909 break;
910 }
cristy79e5dad2010-09-16 19:48:33 +0000911 case PHOTOMETRIC_YCBCR:
912 {
913 (void) SetImageProperty(image,"tiff:photometric","YCBCR");
914 break;
915 }
cristy3ed852e2009-09-05 21:47:34 +0000916 default:
917 {
918 (void) SetImageProperty(image,"tiff:photometric","unknown");
919 break;
920 }
921 }
922 if (image->debug != MagickFalse)
923 {
924 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
925 (unsigned int) width,(unsigned int) height);
926 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
927 interlace);
928 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
929 "Bits per sample: %u",bits_per_sample);
930 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
931 "Min sample value: %u",min_sample_value);
932 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
933 "Max sample value: %u",max_sample_value);
934 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
935 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
936 }
cristybb503372010-05-27 20:51:26 +0000937 image->columns=(size_t) width;
938 image->rows=(size_t) height;
939 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +0000940 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +0000941 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
942 (double) image->depth);
cristyf2687ca2010-06-29 16:32:38 +0000943 lsb_first=1;
944 image->endian=MSBEndian;
945 if ((int) (*(char *) &lsb_first) != 0)
946 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +0000947 if (photometric == PHOTOMETRIC_SEPARATED)
948 image->colorspace=CMYKColorspace;
949 if (photometric == PHOTOMETRIC_CIELAB)
950 image->colorspace=LabColorspace;
951 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
952 &samples_per_pixel);
953 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
954 x_resolution=(float) image->x_resolution;
955 y_resolution=(float) image->y_resolution;
956 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
957 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
958 image->x_resolution=x_resolution;
959 image->y_resolution=y_resolution;
960 x_position=(float) image->page.x/x_resolution;
961 y_position=(float) image->page.y/y_resolution;
962 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
963 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +0000964 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
965 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000966 image->orientation=(OrientationType) orientation;
967 chromaticity=(float *) NULL;
968 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
969 if (chromaticity != (float *) NULL)
970 {
971 image->chromaticity.white_point.x=chromaticity[0];
972 image->chromaticity.white_point.y=chromaticity[1];
973 }
974 chromaticity=(float *) NULL;
975 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
976 if (chromaticity != (float *) NULL)
977 {
978 image->chromaticity.red_primary.x=chromaticity[0];
979 image->chromaticity.red_primary.y=chromaticity[1];
980 image->chromaticity.green_primary.x=chromaticity[2];
981 image->chromaticity.green_primary.y=chromaticity[3];
982 image->chromaticity.blue_primary.x=chromaticity[4];
983 image->chromaticity.blue_primary.y=chromaticity[5];
984 }
985 TIFFGetProperties(tiff,image);
986 option=GetImageOption(image_info,"tiff:exif");
987 if ((option != (const char *) NULL) &&
988 (IsMagickTrue(option) != MagickFalse))
989 TIFFGetEXIFProperties(tiff,image);
990 TIFFGetProfiles(tiff,image);
991 /*
992 Allocate memory for the image and pixel buffer.
993 */
994#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
995 if ((compress_tag != COMPRESSION_NONE) &&
996 (TIFFIsCODECConfigured(compress_tag) == 0))
997 {
998 TIFFClose(tiff);
999 ThrowReaderException(CoderError,"CompressNotSupported");
1000 }
1001#endif
1002 switch (compress_tag)
1003 {
1004 case COMPRESSION_NONE: image->compression=NoCompression; break;
1005 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1006 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1007 case COMPRESSION_JPEG:
1008 {
1009 image->compression=JPEGCompression;
1010#if defined(JPEG_SUPPORT)
1011 {
1012 char
1013 sampling_factor[MaxTextExtent];
1014
1015 uint16
1016 horizontal,
1017 vertical;
1018
1019 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1020 &horizontal,&vertical);
1021 (void) FormatMagickString(sampling_factor,MaxTextExtent,"%dx%d",
1022 horizontal,vertical);
1023 (void) SetImageProperty(image,"jpeg:sampling-factor",
1024 sampling_factor);
1025 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1026 "Sampling Factors: %s",sampling_factor);
1027 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1028 {
1029 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1030 JPEGCOLORMODE_RGB);
1031 photometric=PHOTOMETRIC_RGB;
1032 }
1033 }
1034#endif
1035 break;
1036 }
1037 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001038#if defined(COMPRESSION_LZMA)
1039 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1040#endif
cristy3ed852e2009-09-05 21:47:34 +00001041 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1042 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1043 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1044 default: image->compression=RLECompression; break;
1045 }
1046 quantum_info=AcquireQuantumInfo(image_info,image);
1047 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001048 {
1049 TIFFClose(tiff);
1050 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1051 }
cristy3ed852e2009-09-05 21:47:34 +00001052 if (sample_format == SAMPLEFORMAT_UINT)
1053 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1054 if (sample_format == SAMPLEFORMAT_INT)
1055 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1056 if (sample_format == SAMPLEFORMAT_IEEEFP)
1057 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1058 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001059 {
1060 TIFFClose(tiff);
1061 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1062 }
cristy3ed852e2009-09-05 21:47:34 +00001063 status=MagickTrue;
1064 switch (photometric)
1065 {
1066 case PHOTOMETRIC_MINISBLACK:
1067 {
1068 quantum_info->min_is_white=MagickFalse;
1069 break;
1070 }
1071 case PHOTOMETRIC_MINISWHITE:
1072 {
1073 quantum_info->min_is_white=MagickTrue;
1074 break;
1075 }
1076 default:
1077 break;
1078 }
cristy891dc792010-03-04 01:47:16 +00001079 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001080 extra_samples=0;
1081 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1082 &sample_info);
1083 if (extra_samples == 0)
1084 {
1085 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1086 image->matte=MagickTrue;
1087 }
1088 else
cristy6c207342010-02-19 17:49:50 +00001089 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001090 {
cristy7cf69862010-03-03 13:57:24 +00001091 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001092 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001093 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001094 }
cristy891dc792010-03-04 01:47:16 +00001095 option=GetImageOption(image_info,"tiff:alpha");
1096 if (option != (const char *) NULL)
1097 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1098 MagickFalse;
1099 if (image->matte != MagickFalse)
1100 (void) SetImageProperty(image,"tiff:alpha",
1101 associated_alpha != MagickFalse ? "associated" : "unassociated");
cristy3ed852e2009-09-05 21:47:34 +00001102 if ((photometric == PHOTOMETRIC_PALETTE) &&
1103 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1104 {
cristybb503372010-05-27 20:51:26 +00001105 size_t
cristy3ed852e2009-09-05 21:47:34 +00001106 colors;
1107
cristybb503372010-05-27 20:51:26 +00001108 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy3ed852e2009-09-05 21:47:34 +00001109 if (AcquireImageColormap(image,colors) == MagickFalse)
1110 {
1111 TIFFClose(tiff);
1112 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1113 }
1114 }
1115 if (units == RESUNIT_INCH)
1116 image->units=PixelsPerInchResolution;
1117 if (units == RESUNIT_CENTIMETER)
1118 image->units=PixelsPerCentimeterResolution;
1119 value=(unsigned short) image->scene;
1120 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1121 image->scene=value;
1122 if (image_info->ping != MagickFalse)
1123 {
1124 if (image_info->number_scenes != 0)
1125 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1126 break;
1127 goto next_tiff_frame;
1128 }
1129 method=ReadGenericMethod;
1130 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1131 {
1132 char
1133 value[MaxTextExtent];
1134
1135 method=ReadStripMethod;
1136 (void) FormatMagickString(value,MaxTextExtent,"%u",
1137 (unsigned int) rows_per_strip);
1138 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1139 }
1140 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1141 method=ReadRGBAMethod;
1142 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1143 method=ReadCMYKAMethod;
1144 if ((photometric != PHOTOMETRIC_RGB) &&
1145 (photometric != PHOTOMETRIC_CIELAB) &&
1146 (photometric != PHOTOMETRIC_SEPARATED))
1147 method=ReadGenericMethod;
1148 if (image->storage_class == PseudoClass)
1149 method=ReadSingleSampleMethod;
1150 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1151 (photometric == PHOTOMETRIC_MINISWHITE))
1152 method=ReadSingleSampleMethod;
1153 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1154 (interlace == PLANARCONFIG_SEPARATE))
1155 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001156 if (image->compression == JPEGCompression)
1157 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001158 if (TIFFIsTiled(tiff) != MagickFalse)
1159 method=ReadTileMethod;
1160 quantum_type=RGBQuantum;
1161 pixels=GetQuantumPixels(quantum_info);
1162 switch (method)
1163 {
1164 case ReadSingleSampleMethod:
1165 {
1166 /*
1167 Convert TIFF image to PseudoClass MIFF image.
1168 */
1169 if ((image->storage_class == PseudoClass) &&
1170 (photometric == PHOTOMETRIC_PALETTE))
1171 {
cristybb503372010-05-27 20:51:26 +00001172 size_t
cristy3ed852e2009-09-05 21:47:34 +00001173 range;
1174
1175 uint16
1176 *blue_colormap,
1177 *green_colormap,
1178 *red_colormap;
1179
1180 /*
1181 Initialize colormap.
1182 */
1183 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1184 &green_colormap,&blue_colormap);
1185 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001186 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001187 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1188 (blue_colormap[i] >= 256))
1189 {
1190 range=65535;
1191 break;
1192 }
cristybb503372010-05-27 20:51:26 +00001193 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001194 {
cristyce70c172010-01-07 17:15:30 +00001195 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001196 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001197 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001198 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001199 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001200 blue_colormap[i])/range);
1201 }
1202 }
1203 quantum_type=IndexQuantum;
1204 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1205 if (image->matte != MagickFalse)
1206 {
1207 if (image->storage_class != PseudoClass)
1208 {
1209 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1210 GrayAlphaQuantum;
1211 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1212 }
1213 else
1214 {
1215 quantum_type=IndexAlphaQuantum;
1216 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1217 }
1218 }
1219 else
1220 if (image->storage_class != PseudoClass)
1221 {
1222 quantum_type=GrayQuantum;
1223 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1224 }
1225 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1226 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001227 {
1228 TIFFClose(tiff);
1229 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1230 }
cristy3ed852e2009-09-05 21:47:34 +00001231 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001232 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001233 {
1234 int
1235 status;
1236
1237 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001238 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001239
1240 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1241 if (status == -1)
1242 break;
1243 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1244 if (q == (PixelPacket *) NULL)
1245 break;
1246 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1247 quantum_type,pixels,exception);
1248 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--;
1402 p=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 {
1405 q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
1406 q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
1407 q->blue=ScaleCharToQuantum((unsigned char) (TIFFGetB(*p)));
1408 if (image->matte != MagickFalse)
1409 q->opacity=ScaleCharToQuantum((unsigned char) (TIFFGetA(*p)));
1410 p++;
1411 q++;
1412 }
1413 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1414 break;
1415 if (image->previous == (Image *) NULL)
1416 {
cristycee97112010-05-28 00:44:52 +00001417 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1418 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001419 if (status == MagickFalse)
1420 break;
1421 }
1422 }
1423 break;
1424 }
1425 case ReadTileMethod:
1426 {
1427 register uint32
1428 *p;
1429
1430 uint32
1431 *tile_pixels,
1432 columns,
1433 rows;
1434
cristybb503372010-05-27 20:51:26 +00001435 size_t
cristy3ed852e2009-09-05 21:47:34 +00001436 number_pixels;
1437
1438 /*
1439 Convert tiled TIFF image to DirectClass MIFF image.
1440 */
1441 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1442 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1443 {
1444 TIFFClose(tiff);
1445 ThrowReaderException(CoderError,"ImageIsNotTiled");
1446 }
1447 (void) SetImageStorageClass(image,DirectClass);
1448 number_pixels=columns*rows;
1449 tile_pixels=(uint32 *) AcquireQuantumMemory((size_t) columns*rows,
1450 sizeof(*tile_pixels));
1451 if (tile_pixels == (uint32 *) NULL)
1452 {
1453 TIFFClose(tiff);
1454 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1455 }
cristybb503372010-05-27 20:51:26 +00001456 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001457 {
1458 PixelPacket
1459 *tile;
1460
cristybb503372010-05-27 20:51:26 +00001461 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001462 x;
1463
1464 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001465 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001466
cristybb503372010-05-27 20:51:26 +00001467 size_t
cristy3ed852e2009-09-05 21:47:34 +00001468 columns_remaining,
1469 rows_remaining;
1470
1471 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001472 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001473 rows_remaining=rows;
1474 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1475 exception);
1476 if (tile == (PixelPacket *) NULL)
1477 break;
cristybb503372010-05-27 20:51:26 +00001478 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001479 {
cristybb503372010-05-27 20:51:26 +00001480 size_t
cristy3ed852e2009-09-05 21:47:34 +00001481 column,
1482 row;
1483
1484 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1485 break;
1486 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001487 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001488 columns_remaining=columns;
1489 p=tile_pixels+(rows-rows_remaining)*columns;
1490 q=tile+(image->columns*(rows_remaining-1)+x);
1491 for (row=rows_remaining; row > 0; row--)
1492 {
1493 if (image->matte != MagickFalse)
1494 for (column=columns_remaining; column > 0; column--)
1495 {
1496 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1497 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1498 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1499 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1500 (unsigned char) TIFFGetA(*p)));
1501 q++;
1502 p++;
1503 }
1504 else
1505 for (column=columns_remaining; column > 0; column--)
1506 {
1507 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1508 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1509 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1510 q++;
1511 p++;
1512 }
1513 p+=columns-columns_remaining;
1514 q-=(image->columns+columns_remaining);
1515 }
1516 }
1517 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1518 break;
1519 if (image->previous == (Image *) NULL)
1520 {
cristycee97112010-05-28 00:44:52 +00001521 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1522 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001523 if (status == MagickFalse)
1524 break;
1525 }
1526 }
1527 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1528 break;
1529 }
1530 case ReadGenericMethod:
1531 default:
1532 {
1533 register uint32
1534 *p;
1535
1536 uint32
1537 *pixels;
1538
1539 /*
1540 Convert TIFF image to DirectClass MIFF image.
1541 */
1542 number_pixels=(MagickSizeType) image->columns*image->rows;
1543 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1544 (number_pixels*sizeof(uint32))))
1545 {
1546 TIFFClose(tiff);
1547 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1548 }
1549 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1550 sizeof(uint32));
1551 if (pixels == (uint32 *) NULL)
1552 {
1553 TIFFClose(tiff);
1554 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1555 }
1556 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1557 (uint32) image->rows,(uint32 *) pixels,0);
1558 /*
1559 Convert image to DirectClass pixel packets.
1560 */
1561 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001562 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001563 {
cristybb503372010-05-27 20:51:26 +00001564 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001565 x;
1566
1567 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001568 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001569
1570 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1571 if (q == (PixelPacket *) NULL)
1572 break;
1573 q+=image->columns-1;
cristybb503372010-05-27 20:51:26 +00001574 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001575 {
1576 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1577 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1578 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1579 if (image->matte != MagickFalse)
cristycc768f42010-08-19 16:00:09 +00001580 q->opacity=(Quantum) (QuantumRange-
1581 ScaleCharToQuantum((unsigned char) TIFFGetA(*p)));
cristy3ed852e2009-09-05 21:47:34 +00001582 p--;
1583 q--;
1584 }
1585 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1586 break;
1587 if (image->previous == (Image *) NULL)
1588 {
cristycee97112010-05-28 00:44:52 +00001589 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1590 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001591 if (status == MagickFalse)
1592 break;
1593 }
1594 }
1595 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1596 break;
1597 }
1598 }
1599 SetQuantumImageType(image,quantum_type);
1600 next_tiff_frame:
1601 if ((photometric == PHOTOMETRIC_LOGL) ||
1602 (photometric == PHOTOMETRIC_MINISBLACK) ||
1603 (photometric == PHOTOMETRIC_MINISWHITE))
1604 {
1605 image->type=GrayscaleType;
1606 if (bits_per_sample == 1)
1607 image->type=BilevelType;
1608 }
1609 if (image->storage_class == PseudoClass)
1610 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001611 image->endian=MSBEndian;
1612 if (endian == FILLORDER_LSB2MSB)
1613 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001614 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 /*
1623 Proceed to next image.
1624 */
1625 if (image_info->number_scenes != 0)
1626 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1627 break;
1628 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1629 if (status == MagickTrue)
1630 {
1631 /*
1632 Allocate next image structure.
1633 */
1634 AcquireNextImage(image_info,image);
1635 if (GetNextImageInList(image) == (Image *) NULL)
1636 {
1637 image=DestroyImageList(image);
1638 return((Image *) NULL);
1639 }
1640 image=SyncNextImageInList(image);
1641 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1642 image->scene);
1643 if (status == MagickFalse)
1644 break;
1645 }
1646 quantum_info=DestroyQuantumInfo(quantum_info);
1647 } while (status == MagickTrue);
1648 (void) TIFFSetWarningHandler(warning_handler);
1649 (void) TIFFSetErrorHandler(error_handler);
1650 TIFFClose(tiff);
1651 return(GetFirstImageInList(image));
1652}
1653#endif
1654
1655/*
1656%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1657% %
1658% %
1659% %
1660% R e g i s t e r T I F F I m a g e %
1661% %
1662% %
1663% %
1664%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1665%
1666% RegisterTIFFImage() adds properties for the TIFF image format to
1667% the list of supported formats. The properties include the image format
1668% tag, a method to read and/or write the format, whether the format
1669% supports the saving of more than one frame to the same file or blob,
1670% whether the format supports native in-memory I/O, and a brief
1671% description of the format.
1672%
1673% The format of the RegisterTIFFImage method is:
1674%
cristybb503372010-05-27 20:51:26 +00001675% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001676%
1677*/
cristybb503372010-05-27 20:51:26 +00001678ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001679{
1680#define TIFFDescription "Tagged Image File Format"
1681
1682 char
1683 version[MaxTextExtent];
1684
1685 MagickInfo
1686 *entry;
cristy6b032822010-06-29 16:52:32 +00001687
cristy18b17442009-10-25 18:36:48 +00001688 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1689 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001690 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001691 if (instantiate_key == MagickFalse)
1692 {
1693 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1694 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1695 instantiate_key=MagickTrue;
1696 }
cristyf84a1932010-01-03 18:00:18 +00001697 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001698 *version='\0';
1699#if defined(TIFF_VERSION)
1700 (void) FormatMagickString(version,MaxTextExtent,"%d",TIFF_VERSION);
1701#endif
1702#if defined(MAGICKCORE_TIFF_DELEGATE)
1703 {
1704 const char
1705 *p;
1706
cristybb503372010-05-27 20:51:26 +00001707 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001708 i;
1709
1710 p=TIFFGetVersion();
1711 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1712 version[i]=(*p++);
1713 version[i]='\0';
1714 }
1715#endif
1716
cristy3d7f8062009-09-24 20:45:35 +00001717 entry=SetMagickInfo("GROUP4");
1718#if defined(MAGICKCORE_TIFF_DELEGATE)
1719 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1720 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1721#endif
1722 entry->raw=MagickTrue;
1723 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001724 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001725 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001726 entry->seekable_stream=MagickTrue;
1727 entry->thread_support=NoThreadSupport;
1728 entry->description=ConstantString("Raw CCITT Group4");
1729 entry->module=ConstantString("TIFF");
1730 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001731 entry=SetMagickInfo("PTIF");
1732#if defined(MAGICKCORE_TIFF_DELEGATE)
1733 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1734 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1735#endif
1736 entry->endian_support=MagickTrue;
1737 entry->seekable_stream=MagickTrue;
1738 entry->thread_support=NoThreadSupport;
1739 entry->description=ConstantString("Pyramid encoded TIFF");
1740 entry->module=ConstantString("TIFF");
1741 (void) RegisterMagickInfo(entry);
1742 entry=SetMagickInfo("TIF");
1743#if defined(MAGICKCORE_TIFF_DELEGATE)
1744 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1745 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1746#endif
1747 entry->endian_support=MagickTrue;
1748 entry->seekable_stream=MagickTrue;
1749 entry->stealth=MagickTrue;
1750 entry->thread_support=NoThreadSupport;
1751 entry->description=ConstantString(TIFFDescription);
1752 if (*version != '\0')
1753 entry->version=ConstantString(version);
1754 entry->module=ConstantString("TIFF");
1755 (void) RegisterMagickInfo(entry);
1756 entry=SetMagickInfo("TIFF");
1757#if defined(MAGICKCORE_TIFF_DELEGATE)
1758 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1759 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1760#endif
1761 entry->magick=(IsImageFormatHandler *) IsTIFF;
1762 entry->endian_support=MagickTrue;
1763 entry->seekable_stream=MagickTrue;
1764 entry->thread_support=NoThreadSupport;
1765 entry->description=ConstantString(TIFFDescription);
1766 if (*version != '\0')
1767 entry->version=ConstantString(version);
1768 entry->module=ConstantString("TIFF");
1769 (void) RegisterMagickInfo(entry);
1770 entry=SetMagickInfo("TIFF64");
1771#if defined(TIFF_VERSION_BIG)
1772 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1773 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1774#endif
1775 entry->adjoin=MagickFalse;
1776 entry->endian_support=MagickTrue;
1777 entry->seekable_stream=MagickTrue;
1778 entry->thread_support=NoThreadSupport;
1779 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1780 if (*version != '\0')
1781 entry->version=ConstantString(version);
1782 entry->module=ConstantString("TIFF");
1783 (void) RegisterMagickInfo(entry);
1784 return(MagickImageCoderSignature);
1785}
1786
1787/*
1788%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1789% %
1790% %
1791% %
1792% U n r e g i s t e r T I F F I m a g e %
1793% %
1794% %
1795% %
1796%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1797%
1798% UnregisterTIFFImage() removes format registrations made by the TIFF module
1799% from the list of supported formats.
1800%
1801% The format of the UnregisterTIFFImage method is:
1802%
1803% UnregisterTIFFImage(void)
1804%
1805*/
1806ModuleExport void UnregisterTIFFImage(void)
1807{
cristy3d7f8062009-09-24 20:45:35 +00001808 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001809 (void) UnregisterMagickInfo("PTIF");
1810 (void) UnregisterMagickInfo("TIF");
1811 (void) UnregisterMagickInfo("TIFF");
1812 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001813 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1814 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001815 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001816 if (instantiate_key != MagickFalse)
1817 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1818 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1819 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001820 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001821 DestroySemaphoreInfo(&tiff_semaphore);
1822}
1823
1824#if defined(MAGICKCORE_TIFF_DELEGATE)
1825/*
1826%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1827% %
1828% %
1829% %
cristy3d7f8062009-09-24 20:45:35 +00001830% W r i t e G R O U P 4 I m a g e %
1831% %
1832% %
1833% %
1834%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1835%
1836% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1837%
1838% The format of the WriteGROUP4Image method is:
1839%
1840% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1841% Image *image)
1842%
1843% A description of each parameter follows:
1844%
1845% o image_info: the image info.
1846%
1847% o image: The image.
1848%
1849*/
1850static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1851 Image *image)
1852{
1853 char
1854 filename[MaxTextExtent];
1855
1856 FILE
1857 *file;
1858
1859 Image
1860 *huffman_image;
1861
1862 ImageInfo
1863 *write_info;
1864
1865 int
1866 unique_file;
1867
1868 MagickBooleanType
1869 status;
1870
cristybb503372010-05-27 20:51:26 +00001871 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001872 i;
1873
1874 ssize_t
1875 count;
1876
1877 TIFF
1878 *tiff;
1879
cristy94c8fe42009-10-06 01:57:36 +00001880 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001881 *byte_count,
1882 strip_size;
1883
1884 unsigned char
1885 *buffer;
1886
1887 /*
1888 Write image as CCITT Group4 TIFF image to a temporary file.
1889 */
1890 assert(image_info != (const ImageInfo *) NULL);
1891 assert(image_info->signature == MagickSignature);
1892 assert(image != (Image *) NULL);
1893 assert(image->signature == MagickSignature);
1894 if (image->debug != MagickFalse)
1895 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1896 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1897 if (status == MagickFalse)
1898 return(status);
1899 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1900 if (huffman_image == (Image *) NULL)
1901 {
1902 (void) CloseBlob(image);
1903 return(MagickFalse);
1904 }
cristy94c8fe42009-10-06 01:57:36 +00001905 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001906 file=(FILE *) NULL;
1907 unique_file=AcquireUniqueFileResource(filename);
1908 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001909 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001910 if ((unique_file == -1) || (file == (FILE *) NULL))
1911 {
1912 ThrowFileException(&image->exception,FileOpenError,
1913 "UnableToCreateTemporaryFile",filename);
1914 return(MagickFalse);
1915 }
1916 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1917 filename);
1918 (void) SetImageType(huffman_image,BilevelType);
cristy14efd992009-09-26 23:46:03 +00001919 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001920 SetImageInfoFile(write_info,file);
1921 write_info->compression=Group4Compression;
1922 write_info->type=BilevelType;
1923 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1924 status=WriteTIFFImage(write_info,huffman_image);
1925 (void) fflush(file);
1926 write_info=DestroyImageInfo(write_info);
1927 if (status == MagickFalse)
1928 {
1929 InheritException(&image->exception,&huffman_image->exception);
1930 huffman_image=DestroyImage(huffman_image);
1931 (void) fclose(file);
1932 (void) RelinquishUniqueFileResource(filename);
1933 return(MagickFalse);
1934 }
1935 tiff=TIFFOpen(filename,"rb");
1936 if (tiff == (TIFF *) NULL)
1937 {
1938 huffman_image=DestroyImage(huffman_image);
1939 (void) fclose(file);
1940 (void) RelinquishUniqueFileResource(filename);
1941 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1942 image_info->filename);
1943 return(MagickFalse);
1944 }
1945 /*
1946 Allocate raw strip buffer.
1947 */
cristy94c8fe42009-10-06 01:57:36 +00001948 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1949 {
1950 TIFFClose(tiff);
1951 huffman_image=DestroyImage(huffman_image);
1952 (void) fclose(file);
1953 (void) RelinquishUniqueFileResource(filename);
1954 return(MagickFalse);
1955 }
cristy3d7f8062009-09-24 20:45:35 +00001956 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00001957 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001958 if (byte_count[i] > strip_size)
1959 strip_size=byte_count[i];
1960 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1961 sizeof(*buffer));
1962 if (buffer == (unsigned char *) NULL)
1963 {
1964 TIFFClose(tiff);
1965 huffman_image=DestroyImage(huffman_image);
1966 (void) fclose(file);
1967 (void) RelinquishUniqueFileResource(filename);
1968 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1969 image_info->filename);
1970 }
1971 /*
1972 Compress runlength encoded to 2D Huffman pixels.
1973 */
cristybb503372010-05-27 20:51:26 +00001974 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001975 {
cristy94c8fe42009-10-06 01:57:36 +00001976 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00001977 if (WriteBlob(image,(size_t) count,buffer) != count)
1978 status=MagickFalse;
1979 }
1980 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1981 TIFFClose(tiff);
1982 huffman_image=DestroyImage(huffman_image);
1983 (void) fclose(file);
1984 (void) RelinquishUniqueFileResource(filename);
1985 (void) CloseBlob(image);
1986 return(status);
1987}
1988#endif
1989
1990#if defined(MAGICKCORE_TIFF_DELEGATE)
1991/*
1992%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1993% %
1994% %
1995% %
cristy3ed852e2009-09-05 21:47:34 +00001996% W r i t e P T I F I m a g e %
1997% %
1998% %
1999% %
2000%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2001%
2002% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2003% format.
2004%
2005% The format of the WritePTIFImage method is:
2006%
2007% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2008% Image *image)
2009%
2010% A description of each parameter follows:
2011%
2012% o image_info: the image info.
2013%
2014% o image: The image.
2015%
2016*/
2017static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2018 Image *image)
2019{
2020 Image
2021 *images,
2022 *next,
2023 *pyramid_image;
2024
2025 ImageInfo
2026 *write_info;
2027
2028 MagickBooleanType
2029 status;
2030
cristybb503372010-05-27 20:51:26 +00002031 size_t
cristy3ed852e2009-09-05 21:47:34 +00002032 columns,
2033 rows;
2034
2035 /*
2036 Create pyramid-encoded TIFF image.
2037 */
2038 images=NewImageList();
2039 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2040 {
2041 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2042 &image->exception));
2043 columns=next->columns;
2044 rows=next->rows;
2045 while ((columns > 64) && (rows > 64))
2046 {
2047 columns/=2;
2048 rows/=2;
2049 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2050 &image->exception);
2051 AppendImageToList(&images,pyramid_image);
2052 }
2053 }
2054 /*
2055 Write pyramid-encoded TIFF image.
2056 */
cristy58567532009-09-26 01:13:32 +00002057 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002058 *write_info->magick='\0';
2059 write_info->adjoin=MagickTrue;
2060 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2061 images=DestroyImageList(images);
2062 write_info=DestroyImageInfo(write_info);
2063 return(status);
2064}
2065#endif
2066
2067#if defined(MAGICKCORE_TIFF_DELEGATE)
2068/*
2069%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2070% %
2071% %
2072% W r i t e T I F F I m a g e %
2073% %
2074% %
2075% %
2076%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2077%
2078% WriteTIFFImage() writes an image in the Tagged image file format.
2079%
2080% The format of the WriteTIFFImage method is:
2081%
2082% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2083% Image *image)
2084%
2085% A description of each parameter follows:
2086%
2087% o image_info: the image info.
2088%
2089% o image: The image.
2090%
2091*/
2092
2093typedef struct _TIFFInfo
2094{
2095 RectangleInfo
2096 tile_geometry;
2097
2098 unsigned char
2099 *scanline,
2100 *scanlines,
2101 *pixels;
2102} TIFFInfo;
2103
2104static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2105{
2106 assert(tiff_info != (TIFFInfo *) NULL);
2107 if (tiff_info->scanlines != (unsigned char *) NULL)
2108 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2109 tiff_info->scanlines);
2110 if (tiff_info->pixels != (unsigned char *) NULL)
2111 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2112 tiff_info->pixels);
2113}
2114
2115static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2116 TIFFInfo *tiff_info)
2117{
2118 const char
2119 *option;
2120
2121 MagickStatusType
2122 flags;
2123
cristy79e5dad2010-09-16 19:48:33 +00002124 uint32
2125 tile_columns,
2126 tile_rows;
2127
cristy3ed852e2009-09-05 21:47:34 +00002128 assert(tiff_info != (TIFFInfo *) NULL);
2129 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2130 option=GetImageOption(image_info,"tiff:tile-geometry");
2131 if (option == (const char *) NULL)
2132 return(MagickTrue);
2133 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2134 if ((flags & HeightValue) == 0)
2135 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002136 tile_columns=(uint32) tiff_info->tile_geometry.width;
2137 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002138 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2139 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2140 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2141 tiff_info->tile_geometry.width=tile_columns;
2142 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002143 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002144 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002145 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002146 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002147 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2148 (tiff_info->pixels == (unsigned char *) NULL))
2149 {
2150 DestroyTIFFInfo(tiff_info);
2151 return(MagickFalse);
2152 }
2153 return(MagickTrue);
2154}
2155
cristybb503372010-05-27 20:51:26 +00002156static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002157 tsample_t sample,Image *image)
2158{
2159 int32
2160 status;
2161
cristybb503372010-05-27 20:51:26 +00002162 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002163 bytes_per_pixel,
2164 j,
2165 k,
2166 l;
2167
cristybb503372010-05-27 20:51:26 +00002168 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002169 i;
2170
2171 register unsigned char
2172 *p,
2173 *q;
2174
cristybb503372010-05-27 20:51:26 +00002175 size_t
cristy3ed852e2009-09-05 21:47:34 +00002176 number_tiles,
2177 tile_width;
2178
2179 if (TIFFIsTiled(tiff) == 0)
2180 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2181 /*
2182 Fill scanlines to tile height.
2183 */
cristybb503372010-05-27 20:51:26 +00002184 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002185 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2186 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002187 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2188 (tiff_info->tile_geometry.height-1)) && (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002189 return(0);
2190 /*
2191 Write tile to TIFF image.
2192 */
2193 status=0;
cristybb503372010-05-27 20:51:26 +00002194 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002195 tiff_info->tile_geometry.width);
2196 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2197 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002198 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002199 {
cristybb503372010-05-27 20:51:26 +00002200 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002201 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002202 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2203 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002204 {
2205 if (bytes_per_pixel == 0)
2206 {
2207 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2208 tiff_info->tile_geometry.width+k)/8);
2209 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2210 *q++=(*p++);
2211 continue;
2212 }
2213 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2214 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2215 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2216 for (l=0; l < bytes_per_pixel; l++)
2217 *q++=(*p++);
2218 }
cristydaff8092010-04-22 14:50:53 +00002219 if ((i*tiff_info->tile_geometry.width) != image->columns)
2220 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2221 tiff_info->tile_geometry.width),(uint32) ((row/
2222 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2223 sample);
cristy3ed852e2009-09-05 21:47:34 +00002224 if (status < 0)
2225 break;
2226 }
2227 return(status);
2228}
2229
2230static void TIFFSetProfiles(TIFF *tiff,Image *image)
2231{
2232 const char
2233 *name;
2234
2235 const StringInfo
2236 *profile;
2237
2238 if (image->profiles == (void *) NULL)
2239 return;
2240 ResetImageProfileIterator(image);
2241 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2242 {
2243 profile=GetImageProfile(image,name);
2244#if defined(TIFFTAG_XMLPACKET)
2245 if (LocaleCompare(name,"xmp") == 0)
2246 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2247 profile),GetStringInfoDatum(profile));
2248#endif
2249#if defined(TIFFTAG_ICCPROFILE)
2250 if (LocaleCompare(name,"icc") == 0)
2251 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2252 profile),GetStringInfoDatum(profile));
2253#endif
2254 if (LocaleCompare(name,"iptc") == 0)
2255 {
2256 size_t
2257 length;
2258
2259 StringInfo
2260 *iptc_profile;
2261
2262 iptc_profile=CloneStringInfo(profile);
2263 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2264 0x03);
2265 SetStringInfoLength(iptc_profile,length);
2266 if (TIFFIsByteSwapped(tiff))
2267 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002268 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002269 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2270 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2271 iptc_profile=DestroyStringInfo(iptc_profile);
2272 }
2273#if defined(TIFFTAG_PHOTOSHOP)
2274 if (LocaleCompare(name,"8bim") == 0)
2275 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2276 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2277#endif
2278 if (LocaleCompare(name,"tiff:37724") == 0)
2279 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2280 GetStringInfoDatum(profile));
2281 name=GetNextImageProfile(image);
2282 }
2283}
2284
2285static void TIFFSetProperties(TIFF *tiff,Image *image)
2286{
2287 const char
2288 *value;
2289
cristy15d8d212010-09-17 01:56:30 +00002290 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002291 value=GetImageProperty(image,"tiff:hostcomputer");
2292 if (value != (const char *) NULL)
2293 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2294 value=GetImageProperty(image,"tiff:artist");
2295 if (value != (const char *) NULL)
2296 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2297 value=GetImageProperty(image,"tiff:timestamp");
2298 if (value != (const char *) NULL)
2299 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2300 value=GetImageProperty(image,"tiff:make");
2301 if (value != (const char *) NULL)
2302 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2303 value=GetImageProperty(image,"tiff:model");
2304 if (value != (const char *) NULL)
2305 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristy15d8d212010-09-17 01:56:30 +00002306 value=GetImageProperty(image,"tiff:software");
2307 if (value != (const char *) NULL)
2308 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristy3ed852e2009-09-05 21:47:34 +00002309 value=GetImageProperty(image,"tiff:copyright");
2310 if (value != (const char *) NULL)
2311 (void) TIFFSetField(tiff,33432,value);
2312 value=GetImageProperty(image,"kodak-33423");
2313 if (value != (const char *) NULL)
2314 (void) TIFFSetField(tiff,33423,value);
2315 value=GetImageProperty(image,"kodak-36867");
2316 if (value != (const char *) NULL)
2317 (void) TIFFSetField(tiff,36867,value);
2318 value=GetImageProperty(image,"label");
2319 if (value != (const char *) NULL)
2320 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2321 value=GetImageProperty(image,"comment");
2322 if (value != (const char *) NULL)
2323 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2324}
2325
2326static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2327{
2328#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2329 const char
2330 *value;
2331
cristybb503372010-05-27 20:51:26 +00002332 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002333 i;
2334
2335 uint32
2336 offset;
2337
2338 /*
2339 Write EXIF properties.
2340 */
2341 offset=0;
2342 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2343 for (i=0; exif_info[i].tag != 0; i++)
2344 {
2345 value=GetImageProperty(image,exif_info[i].property);
2346 if (value == (const char *) NULL)
2347 continue;
2348 switch (exif_info[i].type)
2349 {
2350 case TIFF_ASCII:
2351 {
2352 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2353 break;
2354 }
2355 case TIFF_SHORT:
2356 {
2357 uint16
2358 shorty;
2359
cristyf2f27272009-12-17 14:48:46 +00002360 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002361 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2362 break;
2363 }
2364 case TIFF_LONG:
2365 {
2366 uint16
cristybb503372010-05-27 20:51:26 +00002367 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002368
cristybb503372010-05-27 20:51:26 +00002369 ssize_ty=(uint16) StringToLong(value);
2370 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002371 break;
2372 }
2373 case TIFF_RATIONAL:
2374 case TIFF_SRATIONAL:
2375 {
2376 float
2377 rational;
2378
cristyf2f27272009-12-17 14:48:46 +00002379 rational=StringToDouble(value);
cristy3ed852e2009-09-05 21:47:34 +00002380 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2381 break;
2382 }
2383 default:
2384 break;
2385 }
2386 }
2387 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2388#else
2389 (void) tiff;
2390 (void) image;
2391#endif
2392}
2393
2394static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2395 Image *image)
2396{
2397#if !defined(TIFFDefaultStripSize)
2398#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2399#endif
2400
2401 const char
2402 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002403 *option;
cristy3ed852e2009-09-05 21:47:34 +00002404
2405 CompressionType
2406 compression;
2407
cristy6b032822010-06-29 16:52:32 +00002408 EndianType
2409 endian_type;
2410
cristybb503372010-05-27 20:51:26 +00002411 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002412 y;
2413
2414 MagickBooleanType
2415 debug,
2416 status;
2417
2418 MagickOffsetType
2419 scene;
2420
2421 QuantumInfo
2422 *quantum_info;
2423
2424 QuantumType
2425 quantum_type;
2426
cristybb503372010-05-27 20:51:26 +00002427 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002428 i;
2429
2430 size_t
cristy5ecaba72011-02-18 02:05:32 +00002431 length,
2432 lsb_first;
cristy3ed852e2009-09-05 21:47:34 +00002433
2434 TIFF
2435 *tiff;
2436
2437 TIFFErrorHandler
2438 error_handler,
2439 warning_handler;
2440
2441 TIFFInfo
2442 tiff_info;
2443
2444 uint16
2445 bits_per_sample,
2446 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002447 endian,
cristy3ed852e2009-09-05 21:47:34 +00002448 photometric;
2449
2450 uint32
2451 rows_per_strip;
2452
2453 unsigned char
2454 *pixels;
2455
cristy3ed852e2009-09-05 21:47:34 +00002456 /*
2457 Open TIFF file.
2458 */
2459 assert(image_info != (const ImageInfo *) NULL);
2460 assert(image_info->signature == MagickSignature);
2461 assert(image != (Image *) NULL);
2462 assert(image->signature == MagickSignature);
2463 if (image->debug != MagickFalse)
2464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2465 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2466 if (status == MagickFalse)
2467 return(status);
2468 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2469 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2470 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002471 endian_type=UndefinedEndian;
2472 option=GetImageOption(image_info,"tiff:endian");
2473 if (option != (const char *) NULL)
2474 {
2475 if (LocaleNCompare(option,"msb",3) == 0)
2476 endian_type=MSBEndian;
2477 if (LocaleNCompare(option,"lsb",3) == 0)
2478 endian_type=LSBEndian;;
2479 }
2480 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002481 {
2482 case LSBEndian: mode="wl"; break;
2483 case MSBEndian: mode="wb"; break;
2484 default: mode="w"; break;
2485 }
2486#if defined(TIFF_VERSION_BIG)
2487 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002488 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002489 {
2490 case LSBEndian: mode="wl8"; break;
2491 case MSBEndian: mode="wb8"; break;
2492 default: mode="w8"; break;
2493 }
2494#endif
2495 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2496 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2497 TIFFUnmapBlob);
2498 if (tiff == (TIFF *) NULL)
2499 {
2500 (void) TIFFSetWarningHandler(warning_handler);
2501 (void) TIFFSetErrorHandler(error_handler);
2502 return(MagickFalse);
2503 }
2504 scene=0;
2505 debug=IsEventLogging();
2506 do
2507 {
2508 /*
2509 Initialize TIFF fields.
2510 */
cristy5f1c1ff2010-12-23 21:38:06 +00002511 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002512 (image_info->type != OptimizeType))
2513 (void) SetImageType(image,image_info->type);
2514 quantum_info=AcquireQuantumInfo(image_info,image);
2515 if (quantum_info == (QuantumInfo *) NULL)
2516 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2517 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2518 (quantum_info->format == UndefinedQuantumFormat) &&
2519 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2520 {
2521 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2522 if (status == MagickFalse)
2523 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2524 }
2525 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2526 (GetPreviousImageInList(image) != (Image *) NULL))
2527 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2528 if ((image->columns != (uint32) image->columns) ||
2529 (image->rows != (uint32) image->rows))
2530 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2531 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2532 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2533 compression=image->compression;
2534 if (image_info->compression != UndefinedCompression)
2535 compression=image_info->compression;
2536 switch (compression)
2537 {
2538 case FaxCompression:
2539 {
2540 compress_tag=COMPRESSION_CCITTFAX3;
2541 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2542 break;
2543 }
2544 case Group4Compression:
2545 {
2546 compress_tag=COMPRESSION_CCITTFAX4;
2547 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2548 break;
2549 }
2550 case JPEGCompression:
2551 {
2552 compress_tag=COMPRESSION_JPEG;
2553 break;
2554 }
cristyfbb0ef02010-12-19 02:32:11 +00002555#if defined(COMPRESSION_LZMA)
2556 case LZMACompression:
2557 {
2558 compress_tag=COMPRESSION_LZMA;
2559 break;
2560 }
2561#endif
cristy3ed852e2009-09-05 21:47:34 +00002562 case LZWCompression:
2563 {
2564 compress_tag=COMPRESSION_LZW;
2565 break;
2566 }
2567 case RLECompression:
2568 {
2569 compress_tag=COMPRESSION_PACKBITS;
2570 break;
2571 }
2572 case ZipCompression:
2573 {
2574 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2575 break;
2576 }
2577 case NoCompression:
2578 default:
2579 {
2580 compress_tag=COMPRESSION_NONE;
2581 break;
2582 }
2583 }
cristy5b675872009-10-21 13:12:00 +00002584#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2585 if ((compress_tag != COMPRESSION_NONE) &&
2586 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002587 {
cristy5b675872009-10-21 13:12:00 +00002588 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2589 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002590 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002591 compress_tag=COMPRESSION_NONE;
2592 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002593 }
2594#else
2595 switch (compress_tag)
2596 {
2597#if defined(CCITT_SUPPORT)
2598 case COMPRESSION_CCITTFAX3:
2599 case COMPRESSION_CCITTFAX4:
2600#endif
2601#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2602 case COMPRESSION_JPEG:
2603#endif
cristyfbb0ef02010-12-19 02:32:11 +00002604#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2605 case COMPRESSION_LZMA:
2606#endif
cristy3ed852e2009-09-05 21:47:34 +00002607#if defined(LZW_SUPPORT)
2608 case COMPRESSION_LZW:
2609#endif
2610#if defined(PACKBITS_SUPPORT)
2611 case COMPRESSION_PACKBITS:
2612#endif
2613#if defined(ZIP_SUPPORT)
2614 case COMPRESSION_ADOBE_DEFLATE:
2615#endif
2616 case COMPRESSION_NONE:
2617 break;
2618 default:
2619 {
2620 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2621 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002622 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002623 compress_tag=COMPRESSION_NONE;
2624 compression=NoCompression;
2625 break;
2626 }
2627 }
2628#endif
2629 switch (compression)
2630 {
2631 case FaxCompression:
2632 case Group4Compression:
2633 {
2634 (void) SetImageType(image,BilevelType);
2635 break;
2636 }
2637 case JPEGCompression:
2638 {
2639 (void) SetImageStorageClass(image,DirectClass);
2640 (void) SetImageDepth(image,8);
2641 break;
2642 }
2643 default:
2644 break;
2645 }
cristy3ed852e2009-09-05 21:47:34 +00002646 if (image->colorspace == CMYKColorspace)
2647 {
2648 photometric=PHOTOMETRIC_SEPARATED;
2649 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2650 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2651 }
2652 else
2653 {
2654 /*
2655 Full color TIFF raster.
2656 */
2657 if (image->colorspace == LabColorspace)
2658 photometric=PHOTOMETRIC_CIELAB;
2659 else
2660 if (image->colorspace == YCbCrColorspace)
2661 {
2662 photometric=PHOTOMETRIC_YCBCR;
2663 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy79e5dad2010-09-16 19:48:33 +00002664 (void) SetImageStorageClass(image,DirectClass);
cristy3ed852e2009-09-05 21:47:34 +00002665 (void) SetImageDepth(image,8);
2666 }
2667 else
2668 {
2669 if (image->colorspace != RGBColorspace)
2670 (void) TransformImageColorspace(image,RGBColorspace);
2671 photometric=PHOTOMETRIC_RGB;
2672 }
2673 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2674 if ((image_info->type != TrueColorType) &&
2675 (image_info->type != TrueColorMatteType))
2676 {
cristy94c8fe42009-10-06 01:57:36 +00002677 if ((image_info->type != PaletteType) &&
2678 (IsGrayImage(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002679 {
cristy94c8fe42009-10-06 01:57:36 +00002680 photometric=(uint16) (quantum_info->min_is_white !=
2681 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2682 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002683 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002684 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy94c8fe42009-10-06 01:57:36 +00002685 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2686 {
2687 status=SetQuantumDepth(image,quantum_info,1);
2688 if (status == MagickFalse)
2689 ThrowWriterException(ResourceLimitError,
2690 "MemoryAllocationFailed");
2691 }
cristy3ed852e2009-09-05 21:47:34 +00002692 }
2693 else
cristy94c8fe42009-10-06 01:57:36 +00002694 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002695 {
cristybb503372010-05-27 20:51:26 +00002696 size_t
cristy94c8fe42009-10-06 01:57:36 +00002697 depth;
2698
2699 /*
2700 Colormapped TIFF raster.
2701 */
cristy3ed852e2009-09-05 21:47:34 +00002702 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002703 photometric=PHOTOMETRIC_PALETTE;
2704 depth=1;
2705 while ((GetQuantumRange(depth)+1) < image->colors)
2706 depth<<=1;
2707 status=SetQuantumDepth(image,quantum_info,depth);
2708 if (status == MagickFalse)
2709 ThrowWriterException(ResourceLimitError,
2710 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002711 }
2712 }
2713 }
cristyf2687ca2010-06-29 16:32:38 +00002714 switch (image->endian)
2715 {
2716 case LSBEndian:
2717 {
2718 endian=FILLORDER_LSB2MSB;
2719 break;
2720 }
2721 case MSBEndian:
2722 {
2723 endian=FILLORDER_MSB2LSB;
2724 break;
2725 }
2726 case UndefinedEndian:
2727 default:
2728 {
2729 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2730 break;
2731 }
2732 }
2733 lsb_first=1;
2734 image->endian=MSBEndian;
2735 if ((int) (*(char *) &lsb_first) != 0)
2736 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002737 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2738 (photometric != PHOTOMETRIC_MINISWHITE))
2739 {
2740 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002741 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002742 }
cristy0accf6e2009-10-01 13:14:28 +00002743 else
cristydaff8092010-04-22 14:50:53 +00002744 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2745 (photometric != PHOTOMETRIC_MINISWHITE))
2746 {
2747 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002748 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002749 }
cristy62e282b2010-06-29 01:27:13 +00002750 option=GetImageProperty(image,"tiff:fill-order");
2751 if (option != (const char *) NULL)
2752 {
2753 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002754 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002755 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002756 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002757 }
cristyf2687ca2010-06-29 16:32:38 +00002758 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2759 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002760 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2761 if (image->matte != MagickFalse)
2762 {
2763 uint16
2764 extra_samples,
2765 sample_info[1],
2766 samples_per_pixel;
2767
2768 /*
2769 TIFF has a matte channel.
2770 */
2771 extra_samples=1;
2772 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2773 option=GetImageProperty(image,"tiff:alpha");
2774 if ((option != (const char *) NULL) &&
2775 (LocaleCompare(option,"associated") == 0))
2776 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2777 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2778 &samples_per_pixel);
2779 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2780 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2781 &sample_info);
2782 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2783 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2784 }
2785 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2786 switch (quantum_info->format)
2787 {
2788 case FloatingPointQuantumFormat:
2789 {
2790 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2791 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2792 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2793 break;
2794 }
2795 case SignedQuantumFormat:
2796 {
2797 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2798 break;
2799 }
2800 case UnsignedQuantumFormat:
2801 {
2802 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2803 break;
2804 }
2805 default:
2806 break;
2807 }
cristy3ed852e2009-09-05 21:47:34 +00002808 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2809 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2810 if (photometric == PHOTOMETRIC_RGB)
2811 if ((image_info->interlace == PlaneInterlace) ||
2812 (image_info->interlace == PartitionInterlace))
2813 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2814 rows_per_strip=1;
2815 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002816 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2817 1);
cristy3ed852e2009-09-05 21:47:34 +00002818 option=GetImageOption(image_info,"tiff:rows-per-strip");
2819 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002820 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002821 switch (compress_tag)
2822 {
2823 case COMPRESSION_JPEG:
2824 {
2825#if defined(JPEG_SUPPORT)
2826 const char
2827 *sampling_factor;
2828
2829 GeometryInfo
2830 geometry_info;
2831
2832 MagickStatusType
2833 flags;
2834
2835 rows_per_strip+=(16-(rows_per_strip % 16));
2836 if (image->quality != 0)
2837 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2838 if (image_info->quality != UndefinedCompressionQuality)
2839 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2840 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2841 if (image->colorspace == RGBColorspace)
2842 {
cristy949bf5b2010-05-08 02:47:03 +00002843 const char
2844 *value;
2845
cristy3ed852e2009-09-05 21:47:34 +00002846 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2847 sampling_factor=(const char *) NULL;
2848 value=GetImageProperty(image,"jpeg:sampling-factor");
2849 if (value != (char *) NULL)
2850 {
2851 sampling_factor=value;
2852 if (image->debug != MagickFalse)
2853 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2854 " Input sampling-factors=%s",sampling_factor);
2855 }
2856 if (image_info->sampling_factor != (char *) NULL)
2857 sampling_factor=image_info->sampling_factor;
2858 if (sampling_factor != (const char *) NULL)
2859 {
2860 flags=ParseGeometry(sampling_factor,&geometry_info);
2861 if ((flags & SigmaValue) == 0)
2862 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002863 if (image->colorspace == YCbCrColorspace)
2864 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2865 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00002866 }
2867 }
2868 if (bits_per_sample == 12)
2869 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2870#endif
2871 break;
2872 }
2873 case COMPRESSION_ADOBE_DEFLATE:
2874 {
cristyf6fe0a12010-05-30 00:44:47 +00002875 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002876 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2877 &bits_per_sample);
2878 if (((photometric == PHOTOMETRIC_RGB) ||
2879 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2880 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002881 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00002882 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2883 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00002884 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00002885 break;
2886 }
2887 case COMPRESSION_CCITTFAX3:
2888 {
2889 /*
2890 Byte-aligned EOL.
2891 */
cristyeaedf062010-05-29 22:36:02 +00002892 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002893 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2894 break;
2895 }
2896 case COMPRESSION_CCITTFAX4:
2897 {
cristyeaedf062010-05-29 22:36:02 +00002898 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002899 break;
2900 }
cristyef8f26b2010-12-19 20:29:16 +00002901#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2902 case COMPRESSION_LZMA:
2903 {
2904 if (((photometric == PHOTOMETRIC_RGB) ||
2905 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2906 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2907 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2908 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
2909 image_info->quality == UndefinedCompressionQuality ? 7 :
2910 MagickMin((ssize_t) image_info->quality/10,9)));
2911 break;
2912 }
2913#endif
cristy3ed852e2009-09-05 21:47:34 +00002914 case COMPRESSION_LZW:
2915 {
2916 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2917 &bits_per_sample);
2918 if (((photometric == PHOTOMETRIC_RGB) ||
2919 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2920 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002921 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00002922 break;
2923 }
2924 default:
2925 break;
2926 }
cristy79e5dad2010-09-16 19:48:33 +00002927 option=GetImageOption(image_info,"tiff:tile-geometry");
2928 if (option == (const char *) NULL)
2929 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy3ed852e2009-09-05 21:47:34 +00002930 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2931 {
2932 unsigned short
2933 units;
2934
2935 /*
2936 Set image resolution.
2937 */
2938 units=RESUNIT_NONE;
2939 if (image->units == PixelsPerInchResolution)
2940 units=RESUNIT_INCH;
2941 if (image->units == PixelsPerCentimeterResolution)
2942 units=RESUNIT_CENTIMETER;
2943 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2944 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2945 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2946 if ((image->page.x != 0) || (image->page.y != 0))
2947 {
2948 /*
2949 Set image position.
2950 */
2951 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2952 image->x_resolution);
2953 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2954 image->y_resolution);
2955 }
2956 }
2957 if (image->chromaticity.white_point.x != 0.0)
2958 {
2959 float
2960 chromaticity[6];
2961
2962 /*
2963 Set image chromaticity.
2964 */
2965 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2966 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2967 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2968 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2969 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2970 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2971 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2972 chromaticity[0]=(float) image->chromaticity.white_point.x;
2973 chromaticity[1]=(float) image->chromaticity.white_point.y;
2974 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2975 }
2976 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2977 {
2978 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2979 if (image->scene != 0)
2980 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
2981 GetImageListLength(image));
2982 }
2983 if (image->orientation != UndefinedOrientation)
2984 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
2985 (void) TIFFSetProfiles(tiff,image);
2986 {
2987 uint16
2988 page,
2989 pages;
2990
2991 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00002992 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00002993 if ((image_info->adjoin != MagickFalse) && (pages > 1))
2994 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2995 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
2996 }
2997 (void) TIFFSetProperties(tiff,image);
2998 if (0)
2999 (void) TIFFSetEXIFProperties(tiff,image);
3000 /*
3001 Write image scanlines.
3002 */
3003 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3004 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3005 pixels=GetQuantumPixels(quantum_info);
3006 tiff_info.scanline=GetQuantumPixels(quantum_info);
3007 switch (photometric)
3008 {
3009 case PHOTOMETRIC_CIELAB:
3010 case PHOTOMETRIC_YCBCR:
3011 case PHOTOMETRIC_RGB:
3012 {
3013 /*
3014 RGB TIFF image.
3015 */
3016 switch (image_info->interlace)
3017 {
3018 case NoInterlace:
3019 default:
3020 {
3021 quantum_type=RGBQuantum;
3022 if (image->matte != MagickFalse)
3023 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003024 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003025 {
3026 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003027 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003028
3029 p=GetVirtualPixels(image,0,y,image->columns,1,
3030 &image->exception);
3031 if (p == (const PixelPacket *) NULL)
3032 break;
3033 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3034 quantum_info,quantum_type,pixels,&image->exception);
3035 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3036 break;
3037 if (image->previous == (Image *) NULL)
3038 {
cristy2837bcc2010-08-07 23:57:39 +00003039 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3040 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003041 if (status == MagickFalse)
3042 break;
3043 }
3044 }
3045 break;
3046 }
3047 case PlaneInterlace:
3048 case PartitionInterlace:
3049 {
3050 /*
3051 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3052 */
cristybb503372010-05-27 20:51:26 +00003053 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003054 {
3055 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003056 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003057
3058 p=GetVirtualPixels(image,0,y,image->columns,1,
3059 &image->exception);
3060 if (p == (const PixelPacket *) NULL)
3061 break;
3062 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3063 quantum_info,RedQuantum,pixels,&image->exception);
3064 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3065 break;
3066 }
3067 if (image->previous == (Image *) NULL)
3068 {
3069 status=SetImageProgress(image,SaveImageTag,100,400);
3070 if (status == MagickFalse)
3071 break;
3072 }
cristybb503372010-05-27 20:51:26 +00003073 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003074 {
3075 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003076 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003077
3078 p=GetVirtualPixels(image,0,y,image->columns,1,
3079 &image->exception);
3080 if (p == (const PixelPacket *) NULL)
3081 break;
3082 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3083 quantum_info,GreenQuantum,pixels,&image->exception);
3084 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3085 break;
3086 }
3087 if (image->previous == (Image *) NULL)
3088 {
3089 status=SetImageProgress(image,SaveImageTag,200,400);
3090 if (status == MagickFalse)
3091 break;
3092 }
cristybb503372010-05-27 20:51:26 +00003093 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003094 {
3095 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003096 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003097
3098 p=GetVirtualPixels(image,0,y,image->columns,1,
3099 &image->exception);
3100 if (p == (const PixelPacket *) NULL)
3101 break;
3102 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3103 quantum_info,BlueQuantum,pixels,&image->exception);
3104 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3105 break;
3106 }
3107 if (image->previous == (Image *) NULL)
3108 {
3109 status=SetImageProgress(image,SaveImageTag,300,400);
3110 if (status == MagickFalse)
3111 break;
3112 }
3113 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003114 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003115 {
3116 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003117 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003118
3119 p=GetVirtualPixels(image,0,y,image->columns,1,
3120 &image->exception);
3121 if (p == (const PixelPacket *) NULL)
3122 break;
3123 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3124 quantum_info,AlphaQuantum,pixels,&image->exception);
3125 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3126 break;
3127 }
3128 if (image->previous == (Image *) NULL)
3129 {
3130 status=SetImageProgress(image,SaveImageTag,400,400);
3131 if (status == MagickFalse)
3132 break;
3133 }
3134 break;
3135 }
3136 }
3137 break;
3138 }
3139 case PHOTOMETRIC_SEPARATED:
3140 {
3141 /*
3142 CMYK TIFF image.
3143 */
3144 quantum_type=CMYKQuantum;
3145 if (image->matte != MagickFalse)
3146 quantum_type=CMYKAQuantum;
3147 if (image->colorspace != CMYKColorspace)
3148 (void) TransformImageColorspace(image,CMYKColorspace);
cristybb503372010-05-27 20:51:26 +00003149 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003150 {
3151 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003152 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003153
3154 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3155 if (p == (const PixelPacket *) NULL)
3156 break;
3157 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3158 quantum_info,quantum_type,pixels,&image->exception);
3159 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3160 break;
3161 if (image->previous == (Image *) NULL)
3162 {
cristycee97112010-05-28 00:44:52 +00003163 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3164 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003165 if (status == MagickFalse)
3166 break;
3167 }
3168 }
3169 break;
3170 }
3171 case PHOTOMETRIC_PALETTE:
3172 {
3173 uint16
3174 *blue,
3175 *green,
3176 *red;
3177
3178 /*
3179 Colormapped TIFF image.
3180 */
3181 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3182 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3183 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3184 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3185 (blue == (uint16 *) NULL))
3186 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3187 /*
3188 Initialize TIFF colormap.
3189 */
3190 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3191 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3192 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003193 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003194 {
3195 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3196 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3197 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3198 }
3199 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3200 red=(uint16 *) RelinquishMagickMemory(red);
3201 green=(uint16 *) RelinquishMagickMemory(green);
3202 blue=(uint16 *) RelinquishMagickMemory(blue);
3203 }
3204 default:
3205 {
3206 /*
3207 Convert PseudoClass packets to contiguous grayscale scanlines.
3208 */
3209 quantum_type=IndexQuantum;
3210 if (image->matte != MagickFalse)
3211 {
3212 if (photometric != PHOTOMETRIC_PALETTE)
3213 quantum_type=GrayAlphaQuantum;
3214 else
3215 quantum_type=IndexAlphaQuantum;
3216 }
3217 else
3218 if (photometric != PHOTOMETRIC_PALETTE)
3219 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003220 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003221 {
3222 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003223 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003224
3225 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3226 if (p == (const PixelPacket *) NULL)
3227 break;
3228 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3229 quantum_info,quantum_type,pixels,&image->exception);
3230 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3231 break;
3232 if (image->previous == (Image *) NULL)
3233 {
cristycee97112010-05-28 00:44:52 +00003234 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3235 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003236 if (status == MagickFalse)
3237 break;
3238 }
3239 }
3240 break;
3241 }
3242 }
3243 quantum_info=DestroyQuantumInfo(quantum_info);
3244 DestroyTIFFInfo(&tiff_info);
3245 if (0 && (image_info->verbose == MagickTrue))
3246 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3247 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003248 image->endian=MSBEndian;
3249 if (endian == FILLORDER_LSB2MSB)
3250 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003251 image=SyncNextImageInList(image);
3252 if (image == (Image *) NULL)
3253 break;
3254 status=SetImageProgress(image,SaveImagesTag,scene++,
3255 GetImageListLength(image));
3256 if (status == MagickFalse)
3257 break;
3258 } while (image_info->adjoin != MagickFalse);
3259 (void) TIFFSetWarningHandler(warning_handler);
3260 (void) TIFFSetErrorHandler(error_handler);
3261 TIFFClose(tiff);
3262 return(MagickTrue);
3263}
3264#endif