blob: f51d93e96ef04329128742c76d8a2f6706452cff [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% TTTTT IIIII FFFFF FFFFF %
7% T I F F %
8% T I FFF FFF %
9% T I F F %
10% T IIIII F F %
11% %
12% %
13% Read/Write TIFF Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "magick/studio.h"
cristy5a2ca482009-10-14 18:24:56 +000043#include "magick/attribute.h"
cristy3ed852e2009-09-05 21:47:34 +000044#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/cache.h"
47#include "magick/color.h"
48#include "magick/color-private.h"
cristyf4cf3072010-04-25 20:07:02 +000049#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000050#include "magick/colorspace.h"
51#include "magick/constitute.h"
52#include "magick/enhance.h"
53#include "magick/exception.h"
54#include "magick/exception-private.h"
55#include "magick/geometry.h"
56#include "magick/image.h"
57#include "magick/image-private.h"
58#include "magick/list.h"
59#include "magick/log.h"
60#include "magick/magick.h"
61#include "magick/memory_.h"
62#include "magick/module.h"
63#include "magick/monitor.h"
64#include "magick/monitor-private.h"
65#include "magick/option.h"
66#include "magick/property.h"
67#include "magick/quantum.h"
68#include "magick/quantum-private.h"
69#include "magick/profile.h"
70#include "magick/resize.h"
cristy3d7f8062009-09-24 20:45:35 +000071#include "magick/resource_.h"
cristy3ed852e2009-09-05 21:47:34 +000072#include "magick/semaphore.h"
73#include "magick/splay-tree.h"
74#include "magick/static.h"
75#include "magick/statistic.h"
76#include "magick/string_.h"
cristyf2f27272009-12-17 14:48:46 +000077#include "magick/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000078#include "magick/thread_.h"
79#include "magick/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000080#if defined(MAGICKCORE_TIFF_DELEGATE)
81# if defined(MAGICKCORE_HAVE_TIFFCONF_H)
82# include "tiffconf.h"
83#endif
84# include "tiff.h"
85# include "tiffio.h"
86# if !defined(COMPRESSION_ADOBE_DEFLATE)
87# define COMPRESSION_ADOBE_DEFLATE 8
88# endif
89
90/*
91 Typedef declarations.
92*/
93#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
94typedef struct _ExifInfo
95{
96 unsigned int
97 tag,
98 type;
99
100 const char
101 *property;
102} ExifInfo;
103
104static const ExifInfo
105 exif_info[] = {
106 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
107 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
108 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
109 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000110 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy3ed852e2009-09-05 21:47:34 +0000111 { EXIFTAG_OECF, TIFF_UNDEFINED, "exif:OptoelectricConversionFactor" },
112 { EXIFTAG_EXIFVERSION, TIFF_UNDEFINED, "exif:ExifVersion" },
113 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
114 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
115 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_UNDEFINED, "exif:ComponentsConfiguration" },
116 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
117 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
118 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
119 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
120 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
121 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
122 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
123 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
124 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
125 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
126 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
127 /* { EXIFTAG_SUBJECTAREA, TIFF_SHORT, "exif:SubjectArea" }, */
128 { EXIFTAG_MAKERNOTE, TIFF_UNDEFINED, "exif:MakerNote" },
129 { EXIFTAG_USERCOMMENT, TIFF_UNDEFINED, "exif:UserComment" },
130 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
131 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
132 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
133 { EXIFTAG_FLASHPIXVERSION, TIFF_UNDEFINED, "exif:FlashpixVersion" },
134 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
135 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
136 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
137 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
138 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
139 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
140 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_UNDEFINED, "exif:SpatialFrequencyResponse" },
141 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
142 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
143 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
144 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
145 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
146 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
147 { EXIFTAG_FILESOURCE, TIFF_UNDEFINED, "exif:FileSource" },
148 { EXIFTAG_SCENETYPE, TIFF_UNDEFINED, "exif:SceneType" },
149 { EXIFTAG_CFAPATTERN, TIFF_UNDEFINED, "exif:CFAPattern" },
150 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
151 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
152 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
153 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
154 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
155 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
156 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
157 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
158 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
159 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
160 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_UNDEFINED, "exif:DeviceSettingDescription" },
161 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
162 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
163 { 0, 0, (char *) NULL }
164};
165#endif
166
167/*
168 Global declarations.
169*/
170static MagickThreadKey
171 tiff_exception;
172
173static SemaphoreInfo
174 *tiff_semaphore = (SemaphoreInfo *) NULL;
175
176static volatile MagickBooleanType
177 instantiate_key = MagickFalse;
178#endif
179
180/*
181 Forward declarations.
182*/
183#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000184static Image *
185 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
186
cristy3ed852e2009-09-05 21:47:34 +0000187static MagickBooleanType
cristy3d7f8062009-09-24 20:45:35 +0000188 WriteGROUP4Image(const ImageInfo *,Image *),
cristy3ed852e2009-09-05 21:47:34 +0000189 WritePTIFImage(const ImageInfo *,Image *),
190 WriteTIFFImage(const ImageInfo *,Image *);
191#endif
192
193/*
194%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
195% %
196% %
197% %
198% I s T I F F %
199% %
200% %
201% %
202%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
203%
204% IsTIFF() returns MagickTrue if the image format type, identified by the
205% magick string, is TIFF.
206%
207% The format of the IsTIFF method is:
208%
209% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
210%
211% A description of each parameter follows:
212%
213% o magick: compare image format pattern against these bytes.
214%
215% o length: Specifies the length of the magick string.
216%
217*/
218static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
219{
220 if (length < 4)
221 return(MagickFalse);
222 if (memcmp(magick,"\115\115\000\052",4) == 0)
223 return(MagickTrue);
224 if (memcmp(magick,"\111\111\052\000",4) == 0)
225 return(MagickTrue);
226#if defined(TIFF_VERSION_BIG)
227 if (length < 8)
228 return(MagickFalse);
229 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
230 return(MagickTrue);
231 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
232 return(MagickTrue);
233#endif
234 return(MagickFalse);
235}
236
237#if defined(MAGICKCORE_TIFF_DELEGATE)
238/*
239%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
240% %
241% %
242% %
cristy3d7f8062009-09-24 20:45:35 +0000243% R e a d G R O U P 4 I m a g e %
244% %
245% %
246% %
247%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248%
249% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
250% allocates the memory necessary for the new Image structure and returns a
251% pointer to the new image.
252%
253% The format of the ReadGROUP4Image method is:
254%
255% Image *ReadGROUP4Image(const ImageInfo *image_info,
256% ExceptionInfo *exception)
257%
258% A description of each parameter follows:
259%
260% o image_info: the image info.
261%
262% o exception: return any errors or warnings in this structure.
263%
264*/
265
cristyeaedf062010-05-29 22:36:02 +0000266static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000267{
268 unsigned char
269 buffer[4];
270
271 buffer[0]=(unsigned char) value;
272 buffer[1]=(unsigned char) (value >> 8);
273 buffer[2]=(unsigned char) (value >> 16);
274 buffer[3]=(unsigned char) (value >> 24);
275 return(fwrite(buffer,1,4,file));
276}
277
278static Image *ReadGROUP4Image(const ImageInfo *image_info,
279 ExceptionInfo *exception)
280{
281 char
282 filename[MaxTextExtent];
283
284 FILE
285 *file;
286
287 Image
288 *image;
289
290 ImageInfo
291 *read_info;
292
293 int
294 c,
295 unique_file;
296
297 MagickBooleanType
298 status;
299
300 size_t
301 length;
302
303 ssize_t
304 offset,
305 strip_offset;
306
307 /*
308 Open image file.
309 */
310 assert(image_info != (const ImageInfo *) NULL);
311 assert(image_info->signature == MagickSignature);
312 if (image_info->debug != MagickFalse)
313 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
314 image_info->filename);
315 assert(exception != (ExceptionInfo *) NULL);
316 assert(exception->signature == MagickSignature);
317 image=AcquireImage(image_info);
318 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
319 if (status == MagickFalse)
320 {
321 image=DestroyImageList(image);
322 return((Image *) NULL);
323 }
324 /*
325 Write raw CCITT Group 4 wrapped as a TIFF image file.
326 */
327 file=(FILE *) NULL;
328 unique_file=AcquireUniqueFileResource(filename);
329 if (unique_file != -1)
330 file=fdopen(unique_file,"wb");
331 if ((unique_file == -1) || (file == (FILE *) NULL))
332 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
333 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
334 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
335 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
336 length=WriteLSBLong(file,image->columns);
337 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
338 length=WriteLSBLong(file,image->rows);
339 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
340 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
341 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
342 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
343 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000344 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000345 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000346 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000347 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
348 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000349 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000350 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
351 offset=(ssize_t) ftell(file)-4;
352 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000353 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000354 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000355 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000356 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
357 length=fwrite("\000\000\000\000",1,4,file);
358 length=WriteLSBLong(file,image->x_resolution);
359 length=WriteLSBLong(file,1);
360 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
361 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000362 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000363 length=WriteLSBLong(file,(unsigned int) length);
364 (void) fclose(file);
365 (void) CloseBlob(image);
366 image=DestroyImage(image);
367 /*
368 Read TIFF image.
369 */
cristy14efd992009-09-26 23:46:03 +0000370 read_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +0000371 (void) FormatMagickString(read_info->filename,MaxTextExtent,"%.1024s",
372 filename);
373 image=ReadTIFFImage(read_info,exception);
374 read_info=DestroyImageInfo(read_info);
375 if (image != (Image *) NULL)
376 {
377 (void) CopyMagickString(image->filename,image_info->filename,
378 MaxTextExtent);
379 (void) CopyMagickString(image->magick_filename,image_info->filename,
380 MaxTextExtent);
381 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
382 }
383 (void) RelinquishUniqueFileResource(filename);
384 return(image);
385}
386#endif
387
388#if defined(MAGICKCORE_TIFF_DELEGATE)
389/*
390%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391% %
392% %
393% %
cristy3ed852e2009-09-05 21:47:34 +0000394% R e a d T I F F I m a g e %
395% %
396% %
397% %
398%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399%
400% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
401% memory necessary for the new Image structure and returns a pointer to the
402% new image.
403%
404% The format of the ReadTIFFImage method is:
405%
406% Image *ReadTIFFImage(const ImageInfo *image_info,
407% ExceptionInfo *exception)
408%
409% A description of each parameter follows:
410%
411% o image_info: the image info.
412%
413% o exception: return any errors or warnings in this structure.
414%
415*/
416
417static inline size_t MagickMax(const size_t x,const size_t y)
418{
419 if (x > y)
420 return(x);
421 return(y);
422}
423
cristybb503372010-05-27 20:51:26 +0000424static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000425{
426 if (x < y)
427 return(x);
428 return(y);
429}
430
431static MagickBooleanType ReadProfile(Image *image,const char *name,
cristybb503372010-05-27 20:51:26 +0000432 unsigned char *datum,ssize_t length)
cristy3ed852e2009-09-05 21:47:34 +0000433{
434 MagickBooleanType
435 status;
436
cristybb503372010-05-27 20:51:26 +0000437 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000438 i;
439
440 StringInfo
441 *profile;
442
443 if (length < 4)
444 return(MagickFalse);
445 i=0;
446 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
447 {
448 for (i=0; i < (length-4); i+=2)
449 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
450 break;
451 if (i == length)
452 length-=i;
453 else
454 i=0;
455 if (length < 4)
456 return(MagickFalse);
457 }
458 profile=AcquireStringInfo((size_t) length);
459 SetStringInfoDatum(profile,datum+i);
460 status=SetImageProfile(image,name,profile);
461 profile=DestroyStringInfo(profile);
462 if (status == MagickFalse)
463 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
464 image->filename);
465 return(MagickTrue);
466}
467
468#if defined(__cplusplus) || defined(c_plusplus)
469extern "C" {
470#endif
471
472static int TIFFCloseBlob(thandle_t image)
473{
474 (void) CloseBlob((Image *) image);
475 return(0);
476}
477
478static void TIFFErrors(const char *module,const char *format,va_list error)
479{
480 char
481 message[MaxTextExtent];
482
483 ExceptionInfo
484 *exception;
485
486#if defined(MAGICKCORE_HAVE_VSNPRINTF)
487 (void) vsnprintf(message,MaxTextExtent,format,error);
488#else
489 (void) vsprintf(message,format,error);
490#endif
491 (void) ConcatenateMagickString(message,".",MaxTextExtent);
492 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
493 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000494 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
495 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000496}
497
498static void TIFFGetProfiles(TIFF *tiff,Image *image)
499{
500 uint32
501 length;
502
503 unsigned char
504 *profile;
505
506#if defined(TIFFTAG_ICCPROFILE)
507 length=0;
508 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000509 (void) ReadProfile(image,"icc",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000510#endif
511#if defined(TIFFTAG_PHOTOSHOP)
512 length=0;
513 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000514 (void) ReadProfile(image,"8bim",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000515#endif
516#if defined(TIFFTAG_RICHTIFFIPTC)
517 length=0;
518 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
519 {
520 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000521 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000522 (void) ReadProfile(image,"iptc",profile,4L*length);
523 }
524#endif
525#if defined(TIFFTAG_XMLPACKET)
526 length=0;
527 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000528 (void) ReadProfile(image,"xmp",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000529#endif
530 length=0;
531 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000532 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000533}
534
535static void TIFFGetProperties(TIFF *tiff,Image *image)
536{
537 char
538 *text;
539
540 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
541 (void) SetImageProperty(image,"tiff:artist",text);
542 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
543 (void) SetImageProperty(image,"tiff:timestamp",text);
544 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
545 (void) SetImageProperty(image,"tiff:software",text);
546 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
547 (void) SetImageProperty(image,"tiff:hostcomputer",text);
548 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
549 (void) SetImageProperty(image,"tiff:document",text);
550 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
551 (void) SetImageProperty(image,"tiff:make",text);
552 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
553 (void) SetImageProperty(image,"tiff:model",text);
554 if (TIFFGetField(tiff,33432,&text) == 1)
555 (void) SetImageProperty(image,"tiff:copyright",text);
556 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
557 (void) SetImageProperty(image,"label",text);
558 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
559 (void) SetImageProperty(image,"comment",text);
560}
561
562static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
563{
564#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
565 char
566 value[MaxTextExtent];
567
cristybb503372010-05-27 20:51:26 +0000568 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000569 i;
570
571 tdir_t
572 directory;
573
574 uint32
575 offset;
576
577 /*
578 Read EXIF properties.
579 */
580 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
581 return;
582 directory=TIFFCurrentDirectory(tiff);
583 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
584 return;
585 for (i=0; exif_info[i].tag != 0; i++)
586 {
587 *value='\0';
588 switch (exif_info[i].type)
589 {
590 case TIFF_ASCII:
591 {
592 char
593 *ascii;
594
595 if (TIFFGetField(tiff,exif_info[i].tag,&ascii) != 0)
596 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
597 break;
598 }
599 case TIFF_SHORT:
600 {
601 uint16
602 shorty;
603
604 if (TIFFGetField(tiff,exif_info[i].tag,&shorty) != 0)
605 (void) FormatMagickString(value,MaxTextExtent,"%d",shorty);
606 break;
607 }
608 case TIFF_LONG:
609 {
610 uint32
cristybb503372010-05-27 20:51:26 +0000611 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +0000612
cristybb503372010-05-27 20:51:26 +0000613 if (TIFFGetField(tiff,exif_info[i].tag,&ssize_ty) != 0)
614 (void) FormatMagickString(value,MaxTextExtent,"%d",ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +0000615 break;
616 }
617 case TIFF_RATIONAL:
618 case TIFF_SRATIONAL:
619 {
620 float
621 rational;
622
623 if (TIFFGetField(tiff,exif_info[i].tag,&rational) != 0)
cristye7f51092010-01-17 00:39:37 +0000624 (void) FormatMagickString(value,MaxTextExtent,"%g",rational);
cristy3ed852e2009-09-05 21:47:34 +0000625 break;
626 }
627 default:
628 break;
629 }
630 if (*value != '\0')
631 (void) SetImageProperty(image,exif_info[i].property,value);
632 }
633 TIFFSetDirectory(tiff,directory);
634#else
635 (void) tiff;
636 (void) image;
637#endif
638}
639
640static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
641{
642 *base=(tdata_t *) GetBlobStreamData((Image *) image);
643 if (*base != (tdata_t *) NULL)
644 *size=(toff_t) GetBlobSize((Image *) image);
645 if (*base != (tdata_t *) NULL)
646 return(1);
647 return(0);
648}
649
650static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
651{
652 tsize_t
653 count;
654
655 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
656 (unsigned char *) data);
657 return(count);
658}
659
cristybb503372010-05-27 20:51:26 +0000660static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
661 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000662{
663 int32
664 status;
665
666 (void) bits_per_sample;
667 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
668 return(status);
669}
670
671static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
672{
673 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
674}
675
676static toff_t TIFFGetBlobSize(thandle_t image)
677{
678 return((toff_t) GetBlobSize((Image *) image));
679}
680
681static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
682{
683 (void) image;
684 (void) base;
685 (void) size;
686}
687
688static void TIFFWarnings(const char *module,const char *format,va_list warning)
689{
690 char
691 message[MaxTextExtent];
692
693 ExceptionInfo
694 *exception;
695
696#if defined(MAGICKCORE_HAVE_VSNPRINTF)
697 (void) vsnprintf(message,MaxTextExtent,format,warning);
698#else
699 (void) vsprintf(message,format,warning);
700#endif
701 (void) ConcatenateMagickString(message,".",MaxTextExtent);
702 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
703 if (exception != (ExceptionInfo *) NULL)
704 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
705 message,"`%s'",module);
706}
707
708static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
709{
710 tsize_t
711 count;
712
713 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
714 (unsigned char *) data);
715 return(count);
716}
717
718#if defined(__cplusplus) || defined(c_plusplus)
719}
720#endif
721
722static Image *ReadTIFFImage(const ImageInfo *image_info,
723 ExceptionInfo *exception)
724{
725 typedef enum
726 {
727 ReadSingleSampleMethod,
728 ReadRGBAMethod,
729 ReadCMYKAMethod,
730 ReadStripMethod,
731 ReadTileMethod,
732 ReadGenericMethod
733 } TIFFMethodType;
734
735 const char
736 *option;
737
738 float
739 *chromaticity,
740 x_position,
741 y_position,
742 x_resolution,
743 y_resolution;
744
745 Image
746 *image;
747
cristybb503372010-05-27 20:51:26 +0000748 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000749 y;
750
751 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000752 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000753 debug,
754 status;
755
756 MagickSizeType
757 number_pixels;
758
759 QuantumInfo
760 *quantum_info;
761
762 QuantumType
763 quantum_type;
764
cristybb503372010-05-27 20:51:26 +0000765 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000766 i;
767
768 size_t
769 length,
770 pad;
771
772 TIFF
773 *tiff;
774
775 TIFFErrorHandler
776 error_handler,
777 warning_handler;
778
779 TIFFMethodType
780 method;
781
782 uint16
783 compress_tag,
784 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000785 endian,
cristy3ed852e2009-09-05 21:47:34 +0000786 extra_samples,
787 interlace,
788 max_sample_value,
789 min_sample_value,
790 orientation,
791 pages,
792 photometric,
793 *sample_info,
794 sample_format,
795 samples_per_pixel,
796 units,
797 value;
798
799 uint32
800 height,
801 rows_per_strip,
802 width;
803
804 unsigned char
805 *pixels;
806
cristyf2687ca2010-06-29 16:32:38 +0000807 size_t
808 lsb_first;
809
cristy3ed852e2009-09-05 21:47:34 +0000810 /*
811 Open image.
812 */
813 assert(image_info != (const ImageInfo *) NULL);
814 assert(image_info->signature == MagickSignature);
815 if (image_info->debug != MagickFalse)
816 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
817 image_info->filename);
818 assert(exception != (ExceptionInfo *) NULL);
819 assert(exception->signature == MagickSignature);
820 image=AcquireImage(image_info);
821 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
822 if (status == MagickFalse)
823 {
824 image=DestroyImageList(image);
825 return((Image *) NULL);
826 }
827 (void) MagickSetThreadValue(tiff_exception,exception);
828 error_handler=TIFFSetErrorHandler(TIFFErrors);
829 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
830 tiff=TIFFClientOpen(image->filename,"r",(thandle_t) image,TIFFReadBlob,
831 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
832 TIFFUnmapBlob);
833 if (tiff == (TIFF *) NULL)
834 {
835 (void) TIFFSetWarningHandler(warning_handler);
836 (void) TIFFSetErrorHandler(error_handler);
837 image=DestroyImageList(image);
838 return((Image *) NULL);
839 }
840 debug=IsEventLogging();
841 if (image_info->number_scenes != 0)
842 {
843 /*
844 Generate blank images for subimage specification (e.g. image.tif[4].
845 */
cristybb503372010-05-27 20:51:26 +0000846 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000847 {
848 (void) TIFFReadDirectory(tiff);
849 AcquireNextImage(image_info,image);
850 if (GetNextImageInList(image) == (Image *) NULL)
851 {
852 image=DestroyImageList(image);
853 return((Image *) NULL);
854 }
855 image=SyncNextImageInList(image);
856 }
857 }
858 do
859 {
860 if (0 && (image_info->verbose != MagickFalse))
861 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristy6b032822010-06-29 16:52:32 +0000862 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
863 "lsb" : "msb");
cristy3ed852e2009-09-05 21:47:34 +0000864 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
865 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
866 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
867 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
872 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
873 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
874 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
875 switch (photometric)
876 {
877 case PHOTOMETRIC_MINISBLACK:
878 {
879 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
880 break;
881 }
882 case PHOTOMETRIC_MINISWHITE:
883 {
884 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
885 break;
886 }
887 case PHOTOMETRIC_PALETTE:
888 {
889 (void) SetImageProperty(image,"tiff:photometric","palette");
890 break;
891 }
892 case PHOTOMETRIC_RGB:
893 {
894 (void) SetImageProperty(image,"tiff:photometric","RGB");
895 break;
896 }
897 case PHOTOMETRIC_CIELAB:
898 {
899 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
900 break;
901 }
902 case PHOTOMETRIC_SEPARATED:
903 {
904 (void) SetImageProperty(image,"tiff:photometric","separated");
905 break;
906 }
cristy79e5dad2010-09-16 19:48:33 +0000907 case PHOTOMETRIC_YCBCR:
908 {
909 (void) SetImageProperty(image,"tiff:photometric","YCBCR");
910 break;
911 }
cristy3ed852e2009-09-05 21:47:34 +0000912 default:
913 {
914 (void) SetImageProperty(image,"tiff:photometric","unknown");
915 break;
916 }
917 }
918 if (image->debug != MagickFalse)
919 {
920 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
921 (unsigned int) width,(unsigned int) height);
922 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
923 interlace);
924 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
925 "Bits per sample: %u",bits_per_sample);
926 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
927 "Min sample value: %u",min_sample_value);
928 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
929 "Max sample value: %u",max_sample_value);
930 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
931 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
932 }
cristybb503372010-05-27 20:51:26 +0000933 image->columns=(size_t) width;
934 image->rows=(size_t) height;
935 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +0000936 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +0000937 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
938 (double) image->depth);
cristyf2687ca2010-06-29 16:32:38 +0000939 lsb_first=1;
940 image->endian=MSBEndian;
941 if ((int) (*(char *) &lsb_first) != 0)
942 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +0000943 if (photometric == PHOTOMETRIC_SEPARATED)
944 image->colorspace=CMYKColorspace;
945 if (photometric == PHOTOMETRIC_CIELAB)
946 image->colorspace=LabColorspace;
947 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
948 &samples_per_pixel);
949 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
950 x_resolution=(float) image->x_resolution;
951 y_resolution=(float) image->y_resolution;
952 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
953 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
954 image->x_resolution=x_resolution;
955 image->y_resolution=y_resolution;
956 x_position=(float) image->page.x/x_resolution;
957 y_position=(float) image->page.y/y_resolution;
958 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
959 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +0000960 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
961 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000962 image->orientation=(OrientationType) orientation;
963 chromaticity=(float *) NULL;
964 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
965 if (chromaticity != (float *) NULL)
966 {
967 image->chromaticity.white_point.x=chromaticity[0];
968 image->chromaticity.white_point.y=chromaticity[1];
969 }
970 chromaticity=(float *) NULL;
971 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
972 if (chromaticity != (float *) NULL)
973 {
974 image->chromaticity.red_primary.x=chromaticity[0];
975 image->chromaticity.red_primary.y=chromaticity[1];
976 image->chromaticity.green_primary.x=chromaticity[2];
977 image->chromaticity.green_primary.y=chromaticity[3];
978 image->chromaticity.blue_primary.x=chromaticity[4];
979 image->chromaticity.blue_primary.y=chromaticity[5];
980 }
981 TIFFGetProperties(tiff,image);
982 option=GetImageOption(image_info,"tiff:exif");
983 if ((option != (const char *) NULL) &&
984 (IsMagickTrue(option) != MagickFalse))
985 TIFFGetEXIFProperties(tiff,image);
986 TIFFGetProfiles(tiff,image);
987 /*
988 Allocate memory for the image and pixel buffer.
989 */
990#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
991 if ((compress_tag != COMPRESSION_NONE) &&
992 (TIFFIsCODECConfigured(compress_tag) == 0))
993 {
994 TIFFClose(tiff);
995 ThrowReaderException(CoderError,"CompressNotSupported");
996 }
997#endif
998 switch (compress_tag)
999 {
1000 case COMPRESSION_NONE: image->compression=NoCompression; break;
1001 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1002 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1003 case COMPRESSION_JPEG:
1004 {
1005 image->compression=JPEGCompression;
1006#if defined(JPEG_SUPPORT)
1007 {
1008 char
1009 sampling_factor[MaxTextExtent];
1010
1011 uint16
1012 horizontal,
1013 vertical;
1014
1015 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1016 &horizontal,&vertical);
1017 (void) FormatMagickString(sampling_factor,MaxTextExtent,"%dx%d",
1018 horizontal,vertical);
1019 (void) SetImageProperty(image,"jpeg:sampling-factor",
1020 sampling_factor);
1021 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1022 "Sampling Factors: %s",sampling_factor);
1023 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1024 {
1025 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1026 JPEGCOLORMODE_RGB);
1027 photometric=PHOTOMETRIC_RGB;
1028 }
1029 }
1030#endif
1031 break;
1032 }
1033 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1034 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1035 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1036 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1037 default: image->compression=RLECompression; break;
1038 }
1039 quantum_info=AcquireQuantumInfo(image_info,image);
1040 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001041 {
1042 TIFFClose(tiff);
1043 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1044 }
cristy3ed852e2009-09-05 21:47:34 +00001045 if (sample_format == SAMPLEFORMAT_UINT)
1046 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1047 if (sample_format == SAMPLEFORMAT_INT)
1048 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1049 if (sample_format == SAMPLEFORMAT_IEEEFP)
1050 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1051 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001052 {
1053 TIFFClose(tiff);
1054 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1055 }
cristy3ed852e2009-09-05 21:47:34 +00001056 status=MagickTrue;
1057 switch (photometric)
1058 {
1059 case PHOTOMETRIC_MINISBLACK:
1060 {
1061 quantum_info->min_is_white=MagickFalse;
1062 break;
1063 }
1064 case PHOTOMETRIC_MINISWHITE:
1065 {
1066 quantum_info->min_is_white=MagickTrue;
1067 break;
1068 }
1069 default:
1070 break;
1071 }
cristy891dc792010-03-04 01:47:16 +00001072 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001073 extra_samples=0;
1074 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1075 &sample_info);
1076 if (extra_samples == 0)
1077 {
1078 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1079 image->matte=MagickTrue;
1080 }
1081 else
cristy6c207342010-02-19 17:49:50 +00001082 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001083 {
cristy7cf69862010-03-03 13:57:24 +00001084 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001085 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001086 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001087 }
cristy891dc792010-03-04 01:47:16 +00001088 option=GetImageOption(image_info,"tiff:alpha");
1089 if (option != (const char *) NULL)
1090 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1091 MagickFalse;
1092 if (image->matte != MagickFalse)
1093 (void) SetImageProperty(image,"tiff:alpha",
1094 associated_alpha != MagickFalse ? "associated" : "unassociated");
cristy3ed852e2009-09-05 21:47:34 +00001095 if ((photometric == PHOTOMETRIC_PALETTE) &&
1096 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1097 {
cristybb503372010-05-27 20:51:26 +00001098 size_t
cristy3ed852e2009-09-05 21:47:34 +00001099 colors;
1100
cristybb503372010-05-27 20:51:26 +00001101 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy3ed852e2009-09-05 21:47:34 +00001102 if (AcquireImageColormap(image,colors) == MagickFalse)
1103 {
1104 TIFFClose(tiff);
1105 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1106 }
1107 }
1108 if (units == RESUNIT_INCH)
1109 image->units=PixelsPerInchResolution;
1110 if (units == RESUNIT_CENTIMETER)
1111 image->units=PixelsPerCentimeterResolution;
1112 value=(unsigned short) image->scene;
1113 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1114 image->scene=value;
1115 if (image_info->ping != MagickFalse)
1116 {
1117 if (image_info->number_scenes != 0)
1118 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1119 break;
1120 goto next_tiff_frame;
1121 }
1122 method=ReadGenericMethod;
1123 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1124 {
1125 char
1126 value[MaxTextExtent];
1127
1128 method=ReadStripMethod;
1129 (void) FormatMagickString(value,MaxTextExtent,"%u",
1130 (unsigned int) rows_per_strip);
1131 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1132 }
1133 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1134 method=ReadRGBAMethod;
1135 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1136 method=ReadCMYKAMethod;
1137 if ((photometric != PHOTOMETRIC_RGB) &&
1138 (photometric != PHOTOMETRIC_CIELAB) &&
1139 (photometric != PHOTOMETRIC_SEPARATED))
1140 method=ReadGenericMethod;
1141 if (image->storage_class == PseudoClass)
1142 method=ReadSingleSampleMethod;
1143 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1144 (photometric == PHOTOMETRIC_MINISWHITE))
1145 method=ReadSingleSampleMethod;
1146 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1147 (interlace == PLANARCONFIG_SEPARATE))
1148 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001149 if (image->compression == JPEGCompression)
1150 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001151 if (TIFFIsTiled(tiff) != MagickFalse)
1152 method=ReadTileMethod;
1153 quantum_type=RGBQuantum;
1154 pixels=GetQuantumPixels(quantum_info);
1155 switch (method)
1156 {
1157 case ReadSingleSampleMethod:
1158 {
1159 /*
1160 Convert TIFF image to PseudoClass MIFF image.
1161 */
1162 if ((image->storage_class == PseudoClass) &&
1163 (photometric == PHOTOMETRIC_PALETTE))
1164 {
cristybb503372010-05-27 20:51:26 +00001165 size_t
cristy3ed852e2009-09-05 21:47:34 +00001166 range;
1167
1168 uint16
1169 *blue_colormap,
1170 *green_colormap,
1171 *red_colormap;
1172
1173 /*
1174 Initialize colormap.
1175 */
1176 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1177 &green_colormap,&blue_colormap);
1178 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001179 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001180 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1181 (blue_colormap[i] >= 256))
1182 {
1183 range=65535;
1184 break;
1185 }
cristybb503372010-05-27 20:51:26 +00001186 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001187 {
cristyce70c172010-01-07 17:15:30 +00001188 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001189 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001190 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001191 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001192 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001193 blue_colormap[i])/range);
1194 }
1195 }
1196 quantum_type=IndexQuantum;
1197 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1198 if (image->matte != MagickFalse)
1199 {
1200 if (image->storage_class != PseudoClass)
1201 {
1202 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1203 GrayAlphaQuantum;
1204 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1205 }
1206 else
1207 {
1208 quantum_type=IndexAlphaQuantum;
1209 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1210 }
1211 }
1212 else
1213 if (image->storage_class != PseudoClass)
1214 {
1215 quantum_type=GrayQuantum;
1216 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1217 }
1218 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1219 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001220 {
1221 TIFFClose(tiff);
1222 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1223 }
cristy3ed852e2009-09-05 21:47:34 +00001224 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001225 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001226 {
1227 int
1228 status;
1229
1230 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001231 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001232
1233 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1234 if (status == -1)
1235 break;
1236 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1237 if (q == (PixelPacket *) NULL)
1238 break;
1239 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1240 quantum_type,pixels,exception);
1241 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1242 break;
1243 if (image->previous == (Image *) NULL)
1244 {
cristycee97112010-05-28 00:44:52 +00001245 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1246 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001247 if (status == MagickFalse)
1248 break;
1249 }
1250 }
1251 break;
1252 }
1253 case ReadRGBAMethod:
1254 {
1255 /*
1256 Convert TIFF image to DirectClass MIFF image.
1257 */
1258 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1259 quantum_type=RGBQuantum;
1260 if (image->matte != MagickFalse)
1261 {
1262 quantum_type=RGBAQuantum;
1263 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1264 }
1265 if (image->colorspace == CMYKColorspace)
1266 {
1267 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1268 quantum_type=CMYKQuantum;
1269 if (image->matte != MagickFalse)
1270 {
1271 quantum_type=CMYKAQuantum;
1272 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1273 }
1274 }
1275 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1276 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001277 {
1278 TIFFClose(tiff);
1279 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1280 }
cristy3ed852e2009-09-05 21:47:34 +00001281 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001282 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001283 {
1284 int
1285 status;
1286
1287 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001288 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001289
1290 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1291 if (status == -1)
1292 break;
1293 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1294 if (q == (PixelPacket *) NULL)
1295 break;
cristy0b334892010-03-15 02:26:46 +00001296 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1297 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001298 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1299 break;
1300 if (image->previous == (Image *) NULL)
1301 {
cristycee97112010-05-28 00:44:52 +00001302 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1303 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001304 if (status == MagickFalse)
1305 break;
1306 }
1307 }
1308 break;
1309 }
1310 case ReadCMYKAMethod:
1311 {
1312 /*
1313 Convert TIFF image to DirectClass MIFF image.
1314 */
cristybb503372010-05-27 20:51:26 +00001315 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001316 {
cristybb503372010-05-27 20:51:26 +00001317 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001318 {
cristy6d9f12f2009-11-03 14:50:26 +00001319 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001320 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001321
cristy3ed852e2009-09-05 21:47:34 +00001322 int
1323 status;
1324
1325 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1326 pixels);
1327 if (status == -1)
1328 break;
1329 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1330 if (q == (PixelPacket *) NULL)
1331 break;
1332 if (image->colorspace != CMYKColorspace)
1333 switch (i)
1334 {
1335 case 0: quantum_type=RedQuantum; break;
1336 case 1: quantum_type=GreenQuantum; break;
1337 case 2: quantum_type=BlueQuantum; break;
1338 case 3: quantum_type=AlphaQuantum; break;
1339 default: quantum_type=UndefinedQuantum; break;
1340 }
cristy6d9f12f2009-11-03 14:50:26 +00001341 else
cristy3ed852e2009-09-05 21:47:34 +00001342 switch (i)
1343 {
1344 case 0: quantum_type=CyanQuantum; break;
1345 case 1: quantum_type=MagentaQuantum; break;
1346 case 2: quantum_type=YellowQuantum; break;
1347 case 3: quantum_type=BlackQuantum; break;
1348 case 4: quantum_type=AlphaQuantum; break;
1349 default: quantum_type=UndefinedQuantum; break;
1350 }
1351 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1352 quantum_type,pixels,exception);
1353 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1354 break;
1355 }
1356 if (image->previous == (Image *) NULL)
1357 {
cristycee97112010-05-28 00:44:52 +00001358 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1359 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001360 if (status == MagickFalse)
1361 break;
1362 }
1363 }
1364 break;
1365 }
1366 case ReadStripMethod:
1367 {
1368 register unsigned char
1369 *p;
1370
1371 /*
1372 Convert stripped TIFF image to DirectClass MIFF image.
1373 */
1374 i=0;
1375 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001376 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001377 {
cristybb503372010-05-27 20:51:26 +00001378 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001379 x;
1380
1381 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001382 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001383
1384 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1385 if (q == (PixelPacket *) NULL)
1386 break;
1387 if (i == 0)
1388 {
1389 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1390 break;
cristyeaedf062010-05-29 22:36:02 +00001391 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1392 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001393 }
1394 i--;
1395 p=pixels+image->columns*i;
cristybb503372010-05-27 20:51:26 +00001396 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001397 {
1398 q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
1399 q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
1400 q->blue=ScaleCharToQuantum((unsigned char) (TIFFGetB(*p)));
1401 if (image->matte != MagickFalse)
1402 q->opacity=ScaleCharToQuantum((unsigned char) (TIFFGetA(*p)));
1403 p++;
1404 q++;
1405 }
1406 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1407 break;
1408 if (image->previous == (Image *) NULL)
1409 {
cristycee97112010-05-28 00:44:52 +00001410 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1411 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001412 if (status == MagickFalse)
1413 break;
1414 }
1415 }
1416 break;
1417 }
1418 case ReadTileMethod:
1419 {
1420 register uint32
1421 *p;
1422
1423 uint32
1424 *tile_pixels,
1425 columns,
1426 rows;
1427
cristybb503372010-05-27 20:51:26 +00001428 size_t
cristy3ed852e2009-09-05 21:47:34 +00001429 number_pixels;
1430
1431 /*
1432 Convert tiled TIFF image to DirectClass MIFF image.
1433 */
1434 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1435 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1436 {
1437 TIFFClose(tiff);
1438 ThrowReaderException(CoderError,"ImageIsNotTiled");
1439 }
1440 (void) SetImageStorageClass(image,DirectClass);
1441 number_pixels=columns*rows;
1442 tile_pixels=(uint32 *) AcquireQuantumMemory((size_t) columns*rows,
1443 sizeof(*tile_pixels));
1444 if (tile_pixels == (uint32 *) NULL)
1445 {
1446 TIFFClose(tiff);
1447 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1448 }
cristybb503372010-05-27 20:51:26 +00001449 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001450 {
1451 PixelPacket
1452 *tile;
1453
cristybb503372010-05-27 20:51:26 +00001454 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001455 x;
1456
1457 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001458 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001459
cristybb503372010-05-27 20:51:26 +00001460 size_t
cristy3ed852e2009-09-05 21:47:34 +00001461 columns_remaining,
1462 rows_remaining;
1463
1464 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001465 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001466 rows_remaining=rows;
1467 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1468 exception);
1469 if (tile == (PixelPacket *) NULL)
1470 break;
cristybb503372010-05-27 20:51:26 +00001471 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001472 {
cristybb503372010-05-27 20:51:26 +00001473 size_t
cristy3ed852e2009-09-05 21:47:34 +00001474 column,
1475 row;
1476
1477 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1478 break;
1479 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001480 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001481 columns_remaining=columns;
1482 p=tile_pixels+(rows-rows_remaining)*columns;
1483 q=tile+(image->columns*(rows_remaining-1)+x);
1484 for (row=rows_remaining; row > 0; row--)
1485 {
1486 if (image->matte != MagickFalse)
1487 for (column=columns_remaining; column > 0; column--)
1488 {
1489 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1490 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1491 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1492 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1493 (unsigned char) TIFFGetA(*p)));
1494 q++;
1495 p++;
1496 }
1497 else
1498 for (column=columns_remaining; column > 0; column--)
1499 {
1500 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1501 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1502 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1503 q++;
1504 p++;
1505 }
1506 p+=columns-columns_remaining;
1507 q-=(image->columns+columns_remaining);
1508 }
1509 }
1510 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1511 break;
1512 if (image->previous == (Image *) NULL)
1513 {
cristycee97112010-05-28 00:44:52 +00001514 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1515 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001516 if (status == MagickFalse)
1517 break;
1518 }
1519 }
1520 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1521 break;
1522 }
1523 case ReadGenericMethod:
1524 default:
1525 {
1526 register uint32
1527 *p;
1528
1529 uint32
1530 *pixels;
1531
1532 /*
1533 Convert TIFF image to DirectClass MIFF image.
1534 */
1535 number_pixels=(MagickSizeType) image->columns*image->rows;
1536 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1537 (number_pixels*sizeof(uint32))))
1538 {
1539 TIFFClose(tiff);
1540 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1541 }
1542 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1543 sizeof(uint32));
1544 if (pixels == (uint32 *) NULL)
1545 {
1546 TIFFClose(tiff);
1547 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1548 }
1549 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1550 (uint32) image->rows,(uint32 *) pixels,0);
1551 /*
1552 Convert image to DirectClass pixel packets.
1553 */
1554 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001555 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001556 {
cristybb503372010-05-27 20:51:26 +00001557 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001558 x;
1559
1560 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001561 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001562
1563 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1564 if (q == (PixelPacket *) NULL)
1565 break;
1566 q+=image->columns-1;
cristybb503372010-05-27 20:51:26 +00001567 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001568 {
1569 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1570 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1571 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1572 if (image->matte != MagickFalse)
cristycc768f42010-08-19 16:00:09 +00001573 q->opacity=(Quantum) (QuantumRange-
1574 ScaleCharToQuantum((unsigned char) TIFFGetA(*p)));
cristy3ed852e2009-09-05 21:47:34 +00001575 p--;
1576 q--;
1577 }
1578 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1579 break;
1580 if (image->previous == (Image *) NULL)
1581 {
cristycee97112010-05-28 00:44:52 +00001582 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1583 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001584 if (status == MagickFalse)
1585 break;
1586 }
1587 }
1588 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1589 break;
1590 }
1591 }
1592 SetQuantumImageType(image,quantum_type);
1593 next_tiff_frame:
1594 if ((photometric == PHOTOMETRIC_LOGL) ||
1595 (photometric == PHOTOMETRIC_MINISBLACK) ||
1596 (photometric == PHOTOMETRIC_MINISWHITE))
1597 {
1598 image->type=GrayscaleType;
1599 if (bits_per_sample == 1)
1600 image->type=BilevelType;
1601 }
1602 if (image->storage_class == PseudoClass)
1603 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001604 image->endian=MSBEndian;
1605 if (endian == FILLORDER_LSB2MSB)
1606 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001607 if ((photometric == PHOTOMETRIC_LOGL) ||
1608 (photometric == PHOTOMETRIC_MINISBLACK) ||
1609 (photometric == PHOTOMETRIC_MINISWHITE))
1610 {
1611 image->type=GrayscaleType;
1612 if (bits_per_sample == 1)
1613 image->type=BilevelType;
1614 }
1615 /*
1616 Proceed to next image.
1617 */
1618 if (image_info->number_scenes != 0)
1619 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1620 break;
1621 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1622 if (status == MagickTrue)
1623 {
1624 /*
1625 Allocate next image structure.
1626 */
1627 AcquireNextImage(image_info,image);
1628 if (GetNextImageInList(image) == (Image *) NULL)
1629 {
1630 image=DestroyImageList(image);
1631 return((Image *) NULL);
1632 }
1633 image=SyncNextImageInList(image);
1634 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1635 image->scene);
1636 if (status == MagickFalse)
1637 break;
1638 }
1639 quantum_info=DestroyQuantumInfo(quantum_info);
1640 } while (status == MagickTrue);
1641 (void) TIFFSetWarningHandler(warning_handler);
1642 (void) TIFFSetErrorHandler(error_handler);
1643 TIFFClose(tiff);
1644 return(GetFirstImageInList(image));
1645}
1646#endif
1647
1648/*
1649%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1650% %
1651% %
1652% %
1653% R e g i s t e r T I F F I m a g e %
1654% %
1655% %
1656% %
1657%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1658%
1659% RegisterTIFFImage() adds properties for the TIFF image format to
1660% the list of supported formats. The properties include the image format
1661% tag, a method to read and/or write the format, whether the format
1662% supports the saving of more than one frame to the same file or blob,
1663% whether the format supports native in-memory I/O, and a brief
1664% description of the format.
1665%
1666% The format of the RegisterTIFFImage method is:
1667%
cristybb503372010-05-27 20:51:26 +00001668% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001669%
1670*/
cristybb503372010-05-27 20:51:26 +00001671ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001672{
1673#define TIFFDescription "Tagged Image File Format"
1674
1675 char
1676 version[MaxTextExtent];
1677
1678 MagickInfo
1679 *entry;
cristy6b032822010-06-29 16:52:32 +00001680
cristy18b17442009-10-25 18:36:48 +00001681 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1682 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001683 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001684 if (instantiate_key == MagickFalse)
1685 {
1686 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1687 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1688 instantiate_key=MagickTrue;
1689 }
cristyf84a1932010-01-03 18:00:18 +00001690 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001691 *version='\0';
1692#if defined(TIFF_VERSION)
1693 (void) FormatMagickString(version,MaxTextExtent,"%d",TIFF_VERSION);
1694#endif
1695#if defined(MAGICKCORE_TIFF_DELEGATE)
1696 {
1697 const char
1698 *p;
1699
cristybb503372010-05-27 20:51:26 +00001700 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001701 i;
1702
1703 p=TIFFGetVersion();
1704 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1705 version[i]=(*p++);
1706 version[i]='\0';
1707 }
1708#endif
1709
cristy3d7f8062009-09-24 20:45:35 +00001710 entry=SetMagickInfo("GROUP4");
1711#if defined(MAGICKCORE_TIFF_DELEGATE)
1712 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1713 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1714#endif
1715 entry->raw=MagickTrue;
1716 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001717 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001718 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001719 entry->seekable_stream=MagickTrue;
1720 entry->thread_support=NoThreadSupport;
1721 entry->description=ConstantString("Raw CCITT Group4");
1722 entry->module=ConstantString("TIFF");
1723 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001724 entry=SetMagickInfo("PTIF");
1725#if defined(MAGICKCORE_TIFF_DELEGATE)
1726 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1727 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1728#endif
1729 entry->endian_support=MagickTrue;
1730 entry->seekable_stream=MagickTrue;
1731 entry->thread_support=NoThreadSupport;
1732 entry->description=ConstantString("Pyramid encoded TIFF");
1733 entry->module=ConstantString("TIFF");
1734 (void) RegisterMagickInfo(entry);
1735 entry=SetMagickInfo("TIF");
1736#if defined(MAGICKCORE_TIFF_DELEGATE)
1737 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1738 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1739#endif
1740 entry->endian_support=MagickTrue;
1741 entry->seekable_stream=MagickTrue;
1742 entry->stealth=MagickTrue;
1743 entry->thread_support=NoThreadSupport;
1744 entry->description=ConstantString(TIFFDescription);
1745 if (*version != '\0')
1746 entry->version=ConstantString(version);
1747 entry->module=ConstantString("TIFF");
1748 (void) RegisterMagickInfo(entry);
1749 entry=SetMagickInfo("TIFF");
1750#if defined(MAGICKCORE_TIFF_DELEGATE)
1751 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1752 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1753#endif
1754 entry->magick=(IsImageFormatHandler *) IsTIFF;
1755 entry->endian_support=MagickTrue;
1756 entry->seekable_stream=MagickTrue;
1757 entry->thread_support=NoThreadSupport;
1758 entry->description=ConstantString(TIFFDescription);
1759 if (*version != '\0')
1760 entry->version=ConstantString(version);
1761 entry->module=ConstantString("TIFF");
1762 (void) RegisterMagickInfo(entry);
1763 entry=SetMagickInfo("TIFF64");
1764#if defined(TIFF_VERSION_BIG)
1765 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1766 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1767#endif
1768 entry->adjoin=MagickFalse;
1769 entry->endian_support=MagickTrue;
1770 entry->seekable_stream=MagickTrue;
1771 entry->thread_support=NoThreadSupport;
1772 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1773 if (*version != '\0')
1774 entry->version=ConstantString(version);
1775 entry->module=ConstantString("TIFF");
1776 (void) RegisterMagickInfo(entry);
1777 return(MagickImageCoderSignature);
1778}
1779
1780/*
1781%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1782% %
1783% %
1784% %
1785% U n r e g i s t e r T I F F I m a g e %
1786% %
1787% %
1788% %
1789%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1790%
1791% UnregisterTIFFImage() removes format registrations made by the TIFF module
1792% from the list of supported formats.
1793%
1794% The format of the UnregisterTIFFImage method is:
1795%
1796% UnregisterTIFFImage(void)
1797%
1798*/
1799ModuleExport void UnregisterTIFFImage(void)
1800{
cristy3d7f8062009-09-24 20:45:35 +00001801 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001802 (void) UnregisterMagickInfo("PTIF");
1803 (void) UnregisterMagickInfo("TIF");
1804 (void) UnregisterMagickInfo("TIFF");
1805 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001806 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1807 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001808 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001809 if (instantiate_key != MagickFalse)
1810 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1811 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1812 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001813 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001814 DestroySemaphoreInfo(&tiff_semaphore);
1815}
1816
1817#if defined(MAGICKCORE_TIFF_DELEGATE)
1818/*
1819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1820% %
1821% %
1822% %
cristy3d7f8062009-09-24 20:45:35 +00001823% W r i t e G R O U P 4 I m a g e %
1824% %
1825% %
1826% %
1827%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1828%
1829% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1830%
1831% The format of the WriteGROUP4Image method is:
1832%
1833% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1834% Image *image)
1835%
1836% A description of each parameter follows:
1837%
1838% o image_info: the image info.
1839%
1840% o image: The image.
1841%
1842*/
1843static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1844 Image *image)
1845{
1846 char
1847 filename[MaxTextExtent];
1848
1849 FILE
1850 *file;
1851
1852 Image
1853 *huffman_image;
1854
1855 ImageInfo
1856 *write_info;
1857
1858 int
1859 unique_file;
1860
1861 MagickBooleanType
1862 status;
1863
cristybb503372010-05-27 20:51:26 +00001864 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001865 i;
1866
1867 ssize_t
1868 count;
1869
1870 TIFF
1871 *tiff;
1872
cristy94c8fe42009-10-06 01:57:36 +00001873 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001874 *byte_count,
1875 strip_size;
1876
1877 unsigned char
1878 *buffer;
1879
1880 /*
1881 Write image as CCITT Group4 TIFF image to a temporary file.
1882 */
1883 assert(image_info != (const ImageInfo *) NULL);
1884 assert(image_info->signature == MagickSignature);
1885 assert(image != (Image *) NULL);
1886 assert(image->signature == MagickSignature);
1887 if (image->debug != MagickFalse)
1888 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1889 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1890 if (status == MagickFalse)
1891 return(status);
1892 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1893 if (huffman_image == (Image *) NULL)
1894 {
1895 (void) CloseBlob(image);
1896 return(MagickFalse);
1897 }
cristy94c8fe42009-10-06 01:57:36 +00001898 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001899 file=(FILE *) NULL;
1900 unique_file=AcquireUniqueFileResource(filename);
1901 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001902 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001903 if ((unique_file == -1) || (file == (FILE *) NULL))
1904 {
1905 ThrowFileException(&image->exception,FileOpenError,
1906 "UnableToCreateTemporaryFile",filename);
1907 return(MagickFalse);
1908 }
1909 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1910 filename);
1911 (void) SetImageType(huffman_image,BilevelType);
cristy14efd992009-09-26 23:46:03 +00001912 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001913 SetImageInfoFile(write_info,file);
1914 write_info->compression=Group4Compression;
1915 write_info->type=BilevelType;
1916 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1917 status=WriteTIFFImage(write_info,huffman_image);
1918 (void) fflush(file);
1919 write_info=DestroyImageInfo(write_info);
1920 if (status == MagickFalse)
1921 {
1922 InheritException(&image->exception,&huffman_image->exception);
1923 huffman_image=DestroyImage(huffman_image);
1924 (void) fclose(file);
1925 (void) RelinquishUniqueFileResource(filename);
1926 return(MagickFalse);
1927 }
1928 tiff=TIFFOpen(filename,"rb");
1929 if (tiff == (TIFF *) NULL)
1930 {
1931 huffman_image=DestroyImage(huffman_image);
1932 (void) fclose(file);
1933 (void) RelinquishUniqueFileResource(filename);
1934 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1935 image_info->filename);
1936 return(MagickFalse);
1937 }
1938 /*
1939 Allocate raw strip buffer.
1940 */
cristy94c8fe42009-10-06 01:57:36 +00001941 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1942 {
1943 TIFFClose(tiff);
1944 huffman_image=DestroyImage(huffman_image);
1945 (void) fclose(file);
1946 (void) RelinquishUniqueFileResource(filename);
1947 return(MagickFalse);
1948 }
cristy3d7f8062009-09-24 20:45:35 +00001949 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00001950 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001951 if (byte_count[i] > strip_size)
1952 strip_size=byte_count[i];
1953 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1954 sizeof(*buffer));
1955 if (buffer == (unsigned char *) NULL)
1956 {
1957 TIFFClose(tiff);
1958 huffman_image=DestroyImage(huffman_image);
1959 (void) fclose(file);
1960 (void) RelinquishUniqueFileResource(filename);
1961 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1962 image_info->filename);
1963 }
1964 /*
1965 Compress runlength encoded to 2D Huffman pixels.
1966 */
cristybb503372010-05-27 20:51:26 +00001967 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001968 {
cristy94c8fe42009-10-06 01:57:36 +00001969 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00001970 if (WriteBlob(image,(size_t) count,buffer) != count)
1971 status=MagickFalse;
1972 }
1973 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1974 TIFFClose(tiff);
1975 huffman_image=DestroyImage(huffman_image);
1976 (void) fclose(file);
1977 (void) RelinquishUniqueFileResource(filename);
1978 (void) CloseBlob(image);
1979 return(status);
1980}
1981#endif
1982
1983#if defined(MAGICKCORE_TIFF_DELEGATE)
1984/*
1985%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1986% %
1987% %
1988% %
cristy3ed852e2009-09-05 21:47:34 +00001989% W r i t e P T I F I m a g e %
1990% %
1991% %
1992% %
1993%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1994%
1995% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
1996% format.
1997%
1998% The format of the WritePTIFImage method is:
1999%
2000% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2001% Image *image)
2002%
2003% A description of each parameter follows:
2004%
2005% o image_info: the image info.
2006%
2007% o image: The image.
2008%
2009*/
2010static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2011 Image *image)
2012{
2013 Image
2014 *images,
2015 *next,
2016 *pyramid_image;
2017
2018 ImageInfo
2019 *write_info;
2020
2021 MagickBooleanType
2022 status;
2023
cristybb503372010-05-27 20:51:26 +00002024 size_t
cristy3ed852e2009-09-05 21:47:34 +00002025 columns,
2026 rows;
2027
2028 /*
2029 Create pyramid-encoded TIFF image.
2030 */
2031 images=NewImageList();
2032 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2033 {
2034 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2035 &image->exception));
2036 columns=next->columns;
2037 rows=next->rows;
2038 while ((columns > 64) && (rows > 64))
2039 {
2040 columns/=2;
2041 rows/=2;
2042 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2043 &image->exception);
2044 AppendImageToList(&images,pyramid_image);
2045 }
2046 }
2047 /*
2048 Write pyramid-encoded TIFF image.
2049 */
cristy58567532009-09-26 01:13:32 +00002050 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002051 *write_info->magick='\0';
2052 write_info->adjoin=MagickTrue;
2053 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2054 images=DestroyImageList(images);
2055 write_info=DestroyImageInfo(write_info);
2056 return(status);
2057}
2058#endif
2059
2060#if defined(MAGICKCORE_TIFF_DELEGATE)
2061/*
2062%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2063% %
2064% %
2065% W r i t e T I F F I m a g e %
2066% %
2067% %
2068% %
2069%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2070%
2071% WriteTIFFImage() writes an image in the Tagged image file format.
2072%
2073% The format of the WriteTIFFImage method is:
2074%
2075% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2076% Image *image)
2077%
2078% A description of each parameter follows:
2079%
2080% o image_info: the image info.
2081%
2082% o image: The image.
2083%
2084*/
2085
2086typedef struct _TIFFInfo
2087{
2088 RectangleInfo
2089 tile_geometry;
2090
2091 unsigned char
2092 *scanline,
2093 *scanlines,
2094 *pixels;
2095} TIFFInfo;
2096
2097static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2098{
2099 assert(tiff_info != (TIFFInfo *) NULL);
2100 if (tiff_info->scanlines != (unsigned char *) NULL)
2101 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2102 tiff_info->scanlines);
2103 if (tiff_info->pixels != (unsigned char *) NULL)
2104 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2105 tiff_info->pixels);
2106}
2107
2108static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2109 TIFFInfo *tiff_info)
2110{
2111 const char
2112 *option;
2113
2114 MagickStatusType
2115 flags;
2116
cristy79e5dad2010-09-16 19:48:33 +00002117 uint32
2118 tile_columns,
2119 tile_rows;
2120
cristy3ed852e2009-09-05 21:47:34 +00002121 assert(tiff_info != (TIFFInfo *) NULL);
2122 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2123 option=GetImageOption(image_info,"tiff:tile-geometry");
2124 if (option == (const char *) NULL)
2125 return(MagickTrue);
2126 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2127 if ((flags & HeightValue) == 0)
2128 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002129 tile_columns=(uint32) tiff_info->tile_geometry.width;
2130 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002131 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2132 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2133 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2134 tiff_info->tile_geometry.width=tile_columns;
2135 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002136 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002137 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002138 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002139 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002140 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2141 (tiff_info->pixels == (unsigned char *) NULL))
2142 {
2143 DestroyTIFFInfo(tiff_info);
2144 return(MagickFalse);
2145 }
2146 return(MagickTrue);
2147}
2148
cristybb503372010-05-27 20:51:26 +00002149static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002150 tsample_t sample,Image *image)
2151{
2152 int32
2153 status;
2154
cristybb503372010-05-27 20:51:26 +00002155 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002156 bytes_per_pixel,
2157 j,
2158 k,
2159 l;
2160
cristybb503372010-05-27 20:51:26 +00002161 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002162 i;
2163
2164 register unsigned char
2165 *p,
2166 *q;
2167
cristybb503372010-05-27 20:51:26 +00002168 size_t
cristy3ed852e2009-09-05 21:47:34 +00002169 number_tiles,
2170 tile_width;
2171
2172 if (TIFFIsTiled(tiff) == 0)
2173 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2174 /*
2175 Fill scanlines to tile height.
2176 */
cristybb503372010-05-27 20:51:26 +00002177 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002178 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2179 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002180 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2181 (tiff_info->tile_geometry.height-1)) && (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002182 return(0);
2183 /*
2184 Write tile to TIFF image.
2185 */
2186 status=0;
cristybb503372010-05-27 20:51:26 +00002187 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002188 tiff_info->tile_geometry.width);
2189 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2190 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002191 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002192 {
cristybb503372010-05-27 20:51:26 +00002193 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002194 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002195 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2196 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002197 {
2198 if (bytes_per_pixel == 0)
2199 {
2200 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2201 tiff_info->tile_geometry.width+k)/8);
2202 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2203 *q++=(*p++);
2204 continue;
2205 }
2206 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2207 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2208 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2209 for (l=0; l < bytes_per_pixel; l++)
2210 *q++=(*p++);
2211 }
cristydaff8092010-04-22 14:50:53 +00002212 if ((i*tiff_info->tile_geometry.width) != image->columns)
2213 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2214 tiff_info->tile_geometry.width),(uint32) ((row/
2215 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2216 sample);
cristy3ed852e2009-09-05 21:47:34 +00002217 if (status < 0)
2218 break;
2219 }
2220 return(status);
2221}
2222
2223static void TIFFSetProfiles(TIFF *tiff,Image *image)
2224{
2225 const char
2226 *name;
2227
2228 const StringInfo
2229 *profile;
2230
2231 if (image->profiles == (void *) NULL)
2232 return;
2233 ResetImageProfileIterator(image);
2234 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2235 {
2236 profile=GetImageProfile(image,name);
2237#if defined(TIFFTAG_XMLPACKET)
2238 if (LocaleCompare(name,"xmp") == 0)
2239 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2240 profile),GetStringInfoDatum(profile));
2241#endif
2242#if defined(TIFFTAG_ICCPROFILE)
2243 if (LocaleCompare(name,"icc") == 0)
2244 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2245 profile),GetStringInfoDatum(profile));
2246#endif
2247 if (LocaleCompare(name,"iptc") == 0)
2248 {
2249 size_t
2250 length;
2251
2252 StringInfo
2253 *iptc_profile;
2254
2255 iptc_profile=CloneStringInfo(profile);
2256 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2257 0x03);
2258 SetStringInfoLength(iptc_profile,length);
2259 if (TIFFIsByteSwapped(tiff))
2260 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002261 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002262 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2263 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2264 iptc_profile=DestroyStringInfo(iptc_profile);
2265 }
2266#if defined(TIFFTAG_PHOTOSHOP)
2267 if (LocaleCompare(name,"8bim") == 0)
2268 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2269 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2270#endif
2271 if (LocaleCompare(name,"tiff:37724") == 0)
2272 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2273 GetStringInfoDatum(profile));
2274 name=GetNextImageProfile(image);
2275 }
2276}
2277
2278static void TIFFSetProperties(TIFF *tiff,Image *image)
2279{
2280 const char
2281 *value;
2282
cristy15d8d212010-09-17 01:56:30 +00002283 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002284 value=GetImageProperty(image,"tiff:hostcomputer");
2285 if (value != (const char *) NULL)
2286 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2287 value=GetImageProperty(image,"tiff:artist");
2288 if (value != (const char *) NULL)
2289 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2290 value=GetImageProperty(image,"tiff:timestamp");
2291 if (value != (const char *) NULL)
2292 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2293 value=GetImageProperty(image,"tiff:make");
2294 if (value != (const char *) NULL)
2295 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2296 value=GetImageProperty(image,"tiff:model");
2297 if (value != (const char *) NULL)
2298 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristy15d8d212010-09-17 01:56:30 +00002299 value=GetImageProperty(image,"tiff:software");
2300 if (value != (const char *) NULL)
2301 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristy3ed852e2009-09-05 21:47:34 +00002302 value=GetImageProperty(image,"tiff:copyright");
2303 if (value != (const char *) NULL)
2304 (void) TIFFSetField(tiff,33432,value);
2305 value=GetImageProperty(image,"kodak-33423");
2306 if (value != (const char *) NULL)
2307 (void) TIFFSetField(tiff,33423,value);
2308 value=GetImageProperty(image,"kodak-36867");
2309 if (value != (const char *) NULL)
2310 (void) TIFFSetField(tiff,36867,value);
2311 value=GetImageProperty(image,"label");
2312 if (value != (const char *) NULL)
2313 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2314 value=GetImageProperty(image,"comment");
2315 if (value != (const char *) NULL)
2316 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2317}
2318
2319static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2320{
2321#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2322 const char
2323 *value;
2324
cristybb503372010-05-27 20:51:26 +00002325 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002326 i;
2327
2328 uint32
2329 offset;
2330
2331 /*
2332 Write EXIF properties.
2333 */
2334 offset=0;
2335 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2336 for (i=0; exif_info[i].tag != 0; i++)
2337 {
2338 value=GetImageProperty(image,exif_info[i].property);
2339 if (value == (const char *) NULL)
2340 continue;
2341 switch (exif_info[i].type)
2342 {
2343 case TIFF_ASCII:
2344 {
2345 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2346 break;
2347 }
2348 case TIFF_SHORT:
2349 {
2350 uint16
2351 shorty;
2352
cristyf2f27272009-12-17 14:48:46 +00002353 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002354 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2355 break;
2356 }
2357 case TIFF_LONG:
2358 {
2359 uint16
cristybb503372010-05-27 20:51:26 +00002360 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002361
cristybb503372010-05-27 20:51:26 +00002362 ssize_ty=(uint16) StringToLong(value);
2363 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002364 break;
2365 }
2366 case TIFF_RATIONAL:
2367 case TIFF_SRATIONAL:
2368 {
2369 float
2370 rational;
2371
cristyf2f27272009-12-17 14:48:46 +00002372 rational=StringToDouble(value);
cristy3ed852e2009-09-05 21:47:34 +00002373 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2374 break;
2375 }
2376 default:
2377 break;
2378 }
2379 }
2380 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2381#else
2382 (void) tiff;
2383 (void) image;
2384#endif
2385}
2386
2387static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2388 Image *image)
2389{
2390#if !defined(TIFFDefaultStripSize)
2391#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2392#endif
2393
2394 const char
2395 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002396 *option;
cristy3ed852e2009-09-05 21:47:34 +00002397
2398 CompressionType
2399 compression;
2400
cristy6b032822010-06-29 16:52:32 +00002401 EndianType
2402 endian_type;
2403
cristybb503372010-05-27 20:51:26 +00002404 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002405 y;
2406
2407 MagickBooleanType
2408 debug,
2409 status;
2410
2411 MagickOffsetType
2412 scene;
2413
2414 QuantumInfo
2415 *quantum_info;
2416
2417 QuantumType
2418 quantum_type;
2419
cristybb503372010-05-27 20:51:26 +00002420 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002421 i;
2422
2423 size_t
2424 length;
2425
2426 TIFF
2427 *tiff;
2428
2429 TIFFErrorHandler
2430 error_handler,
2431 warning_handler;
2432
2433 TIFFInfo
2434 tiff_info;
2435
2436 uint16
2437 bits_per_sample,
2438 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002439 endian,
cristy3ed852e2009-09-05 21:47:34 +00002440 photometric;
2441
2442 uint32
2443 rows_per_strip;
2444
2445 unsigned char
2446 *pixels;
2447
cristyf2687ca2010-06-29 16:32:38 +00002448 size_t
2449 lsb_first;
2450
cristy3ed852e2009-09-05 21:47:34 +00002451 /*
2452 Open TIFF file.
2453 */
2454 assert(image_info != (const ImageInfo *) NULL);
2455 assert(image_info->signature == MagickSignature);
2456 assert(image != (Image *) NULL);
2457 assert(image->signature == MagickSignature);
2458 if (image->debug != MagickFalse)
2459 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2460 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2461 if (status == MagickFalse)
2462 return(status);
2463 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2464 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2465 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002466 endian_type=UndefinedEndian;
2467 option=GetImageOption(image_info,"tiff:endian");
2468 if (option != (const char *) NULL)
2469 {
2470 if (LocaleNCompare(option,"msb",3) == 0)
2471 endian_type=MSBEndian;
2472 if (LocaleNCompare(option,"lsb",3) == 0)
2473 endian_type=LSBEndian;;
2474 }
2475 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002476 {
2477 case LSBEndian: mode="wl"; break;
2478 case MSBEndian: mode="wb"; break;
2479 default: mode="w"; break;
2480 }
2481#if defined(TIFF_VERSION_BIG)
2482 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002483 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002484 {
2485 case LSBEndian: mode="wl8"; break;
2486 case MSBEndian: mode="wb8"; break;
2487 default: mode="w8"; break;
2488 }
2489#endif
2490 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2491 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2492 TIFFUnmapBlob);
2493 if (tiff == (TIFF *) NULL)
2494 {
2495 (void) TIFFSetWarningHandler(warning_handler);
2496 (void) TIFFSetErrorHandler(error_handler);
2497 return(MagickFalse);
2498 }
2499 scene=0;
2500 debug=IsEventLogging();
2501 do
2502 {
2503 /*
2504 Initialize TIFF fields.
2505 */
2506 if ((image_info->type != UndefinedType) &&
2507 (image_info->type != OptimizeType))
2508 (void) SetImageType(image,image_info->type);
2509 quantum_info=AcquireQuantumInfo(image_info,image);
2510 if (quantum_info == (QuantumInfo *) NULL)
2511 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2512 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2513 (quantum_info->format == UndefinedQuantumFormat) &&
2514 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2515 {
2516 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2517 if (status == MagickFalse)
2518 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2519 }
2520 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2521 (GetPreviousImageInList(image) != (Image *) NULL))
2522 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2523 if ((image->columns != (uint32) image->columns) ||
2524 (image->rows != (uint32) image->rows))
2525 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2526 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2527 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2528 compression=image->compression;
2529 if (image_info->compression != UndefinedCompression)
2530 compression=image_info->compression;
2531 switch (compression)
2532 {
2533 case FaxCompression:
2534 {
2535 compress_tag=COMPRESSION_CCITTFAX3;
2536 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2537 break;
2538 }
2539 case Group4Compression:
2540 {
2541 compress_tag=COMPRESSION_CCITTFAX4;
2542 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2543 break;
2544 }
2545 case JPEGCompression:
2546 {
2547 compress_tag=COMPRESSION_JPEG;
2548 break;
2549 }
2550 case LZWCompression:
2551 {
2552 compress_tag=COMPRESSION_LZW;
2553 break;
2554 }
2555 case RLECompression:
2556 {
2557 compress_tag=COMPRESSION_PACKBITS;
2558 break;
2559 }
2560 case ZipCompression:
2561 {
2562 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2563 break;
2564 }
2565 case NoCompression:
2566 default:
2567 {
2568 compress_tag=COMPRESSION_NONE;
2569 break;
2570 }
2571 }
cristy5b675872009-10-21 13:12:00 +00002572#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2573 if ((compress_tag != COMPRESSION_NONE) &&
2574 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002575 {
cristy5b675872009-10-21 13:12:00 +00002576 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2577 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002578 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002579 compress_tag=COMPRESSION_NONE;
2580 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002581 }
2582#else
2583 switch (compress_tag)
2584 {
2585#if defined(CCITT_SUPPORT)
2586 case COMPRESSION_CCITTFAX3:
2587 case COMPRESSION_CCITTFAX4:
2588#endif
2589#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2590 case COMPRESSION_JPEG:
2591#endif
2592#if defined(LZW_SUPPORT)
2593 case COMPRESSION_LZW:
2594#endif
2595#if defined(PACKBITS_SUPPORT)
2596 case COMPRESSION_PACKBITS:
2597#endif
2598#if defined(ZIP_SUPPORT)
2599 case COMPRESSION_ADOBE_DEFLATE:
2600#endif
2601 case COMPRESSION_NONE:
2602 break;
2603 default:
2604 {
2605 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2606 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002607 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002608 compress_tag=COMPRESSION_NONE;
2609 compression=NoCompression;
2610 break;
2611 }
2612 }
2613#endif
2614 switch (compression)
2615 {
2616 case FaxCompression:
2617 case Group4Compression:
2618 {
2619 (void) SetImageType(image,BilevelType);
2620 break;
2621 }
2622 case JPEGCompression:
2623 {
2624 (void) SetImageStorageClass(image,DirectClass);
2625 (void) SetImageDepth(image,8);
2626 break;
2627 }
2628 default:
2629 break;
2630 }
cristy3ed852e2009-09-05 21:47:34 +00002631 if (image->colorspace == CMYKColorspace)
2632 {
2633 photometric=PHOTOMETRIC_SEPARATED;
2634 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2635 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2636 }
2637 else
2638 {
2639 /*
2640 Full color TIFF raster.
2641 */
2642 if (image->colorspace == LabColorspace)
2643 photometric=PHOTOMETRIC_CIELAB;
2644 else
2645 if (image->colorspace == YCbCrColorspace)
2646 {
2647 photometric=PHOTOMETRIC_YCBCR;
2648 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy79e5dad2010-09-16 19:48:33 +00002649 (void) SetImageStorageClass(image,DirectClass);
cristy3ed852e2009-09-05 21:47:34 +00002650 (void) SetImageDepth(image,8);
2651 }
2652 else
2653 {
2654 if (image->colorspace != RGBColorspace)
2655 (void) TransformImageColorspace(image,RGBColorspace);
2656 photometric=PHOTOMETRIC_RGB;
2657 }
2658 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2659 if ((image_info->type != TrueColorType) &&
2660 (image_info->type != TrueColorMatteType))
2661 {
cristy94c8fe42009-10-06 01:57:36 +00002662 if ((image_info->type != PaletteType) &&
2663 (IsGrayImage(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002664 {
cristy94c8fe42009-10-06 01:57:36 +00002665 photometric=(uint16) (quantum_info->min_is_white !=
2666 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2667 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002668 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002669 if ((image_info->depth == 0) &&
2670 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2671 {
2672 status=SetQuantumDepth(image,quantum_info,1);
2673 if (status == MagickFalse)
2674 ThrowWriterException(ResourceLimitError,
2675 "MemoryAllocationFailed");
2676 }
cristy3ed852e2009-09-05 21:47:34 +00002677 }
2678 else
cristy94c8fe42009-10-06 01:57:36 +00002679 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002680 {
cristybb503372010-05-27 20:51:26 +00002681 size_t
cristy94c8fe42009-10-06 01:57:36 +00002682 depth;
2683
2684 /*
2685 Colormapped TIFF raster.
2686 */
cristy3ed852e2009-09-05 21:47:34 +00002687 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002688 photometric=PHOTOMETRIC_PALETTE;
2689 depth=1;
2690 while ((GetQuantumRange(depth)+1) < image->colors)
2691 depth<<=1;
2692 status=SetQuantumDepth(image,quantum_info,depth);
2693 if (status == MagickFalse)
2694 ThrowWriterException(ResourceLimitError,
2695 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002696 }
2697 }
2698 }
cristyf2687ca2010-06-29 16:32:38 +00002699 switch (image->endian)
2700 {
2701 case LSBEndian:
2702 {
2703 endian=FILLORDER_LSB2MSB;
2704 break;
2705 }
2706 case MSBEndian:
2707 {
2708 endian=FILLORDER_MSB2LSB;
2709 break;
2710 }
2711 case UndefinedEndian:
2712 default:
2713 {
2714 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2715 break;
2716 }
2717 }
2718 lsb_first=1;
2719 image->endian=MSBEndian;
2720 if ((int) (*(char *) &lsb_first) != 0)
2721 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002722 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2723 (photometric != PHOTOMETRIC_MINISWHITE))
2724 {
2725 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002726 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002727 }
cristy0accf6e2009-10-01 13:14:28 +00002728 else
cristydaff8092010-04-22 14:50:53 +00002729 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2730 (photometric != PHOTOMETRIC_MINISWHITE))
2731 {
2732 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002733 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002734 }
cristy62e282b2010-06-29 01:27:13 +00002735 option=GetImageProperty(image,"tiff:fill-order");
2736 if (option != (const char *) NULL)
2737 {
2738 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002739 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002740 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002741 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002742 }
cristyf2687ca2010-06-29 16:32:38 +00002743 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2744 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002745 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2746 if (image->matte != MagickFalse)
2747 {
2748 uint16
2749 extra_samples,
2750 sample_info[1],
2751 samples_per_pixel;
2752
2753 /*
2754 TIFF has a matte channel.
2755 */
2756 extra_samples=1;
2757 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2758 option=GetImageProperty(image,"tiff:alpha");
2759 if ((option != (const char *) NULL) &&
2760 (LocaleCompare(option,"associated") == 0))
2761 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2762 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2763 &samples_per_pixel);
2764 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2765 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2766 &sample_info);
2767 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2768 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2769 }
2770 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2771 switch (quantum_info->format)
2772 {
2773 case FloatingPointQuantumFormat:
2774 {
2775 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2776 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2777 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2778 break;
2779 }
2780 case SignedQuantumFormat:
2781 {
2782 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2783 break;
2784 }
2785 case UnsignedQuantumFormat:
2786 {
2787 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2788 break;
2789 }
2790 default:
2791 break;
2792 }
cristy3ed852e2009-09-05 21:47:34 +00002793 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2794 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2795 if (photometric == PHOTOMETRIC_RGB)
2796 if ((image_info->interlace == PlaneInterlace) ||
2797 (image_info->interlace == PartitionInterlace))
2798 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2799 rows_per_strip=1;
2800 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002801 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2802 1);
cristy3ed852e2009-09-05 21:47:34 +00002803 option=GetImageOption(image_info,"tiff:rows-per-strip");
2804 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002805 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002806 switch (compress_tag)
2807 {
2808 case COMPRESSION_JPEG:
2809 {
2810#if defined(JPEG_SUPPORT)
2811 const char
2812 *sampling_factor;
2813
2814 GeometryInfo
2815 geometry_info;
2816
2817 MagickStatusType
2818 flags;
2819
2820 rows_per_strip+=(16-(rows_per_strip % 16));
2821 if (image->quality != 0)
2822 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2823 if (image_info->quality != UndefinedCompressionQuality)
2824 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2825 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2826 if (image->colorspace == RGBColorspace)
2827 {
cristy949bf5b2010-05-08 02:47:03 +00002828 const char
2829 *value;
2830
cristy3ed852e2009-09-05 21:47:34 +00002831 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2832 sampling_factor=(const char *) NULL;
2833 value=GetImageProperty(image,"jpeg:sampling-factor");
2834 if (value != (char *) NULL)
2835 {
2836 sampling_factor=value;
2837 if (image->debug != MagickFalse)
2838 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2839 " Input sampling-factors=%s",sampling_factor);
2840 }
2841 if (image_info->sampling_factor != (char *) NULL)
2842 sampling_factor=image_info->sampling_factor;
2843 if (sampling_factor != (const char *) NULL)
2844 {
2845 flags=ParseGeometry(sampling_factor,&geometry_info);
2846 if ((flags & SigmaValue) == 0)
2847 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002848 if (image->colorspace == YCbCrColorspace)
2849 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2850 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00002851 }
2852 }
2853 if (bits_per_sample == 12)
2854 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2855#endif
2856 break;
2857 }
2858 case COMPRESSION_ADOBE_DEFLATE:
2859 {
cristyf6fe0a12010-05-30 00:44:47 +00002860 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002861 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2862 &bits_per_sample);
2863 if (((photometric == PHOTOMETRIC_RGB) ||
2864 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2865 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2866 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
cristyf6fe0a12010-05-30 00:44:47 +00002867 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2868 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00002869 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00002870 break;
2871 }
2872 case COMPRESSION_CCITTFAX3:
2873 {
2874 /*
2875 Byte-aligned EOL.
2876 */
cristyeaedf062010-05-29 22:36:02 +00002877 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002878 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2879 break;
2880 }
2881 case COMPRESSION_CCITTFAX4:
2882 {
cristyeaedf062010-05-29 22:36:02 +00002883 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002884 break;
2885 }
2886 case COMPRESSION_LZW:
2887 {
2888 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2889 &bits_per_sample);
2890 if (((photometric == PHOTOMETRIC_RGB) ||
2891 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2892 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2893 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
2894 break;
2895 }
2896 default:
2897 break;
2898 }
cristy79e5dad2010-09-16 19:48:33 +00002899 option=GetImageOption(image_info,"tiff:tile-geometry");
2900 if (option == (const char *) NULL)
2901 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy3ed852e2009-09-05 21:47:34 +00002902 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2903 {
2904 unsigned short
2905 units;
2906
2907 /*
2908 Set image resolution.
2909 */
2910 units=RESUNIT_NONE;
2911 if (image->units == PixelsPerInchResolution)
2912 units=RESUNIT_INCH;
2913 if (image->units == PixelsPerCentimeterResolution)
2914 units=RESUNIT_CENTIMETER;
2915 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2916 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2917 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2918 if ((image->page.x != 0) || (image->page.y != 0))
2919 {
2920 /*
2921 Set image position.
2922 */
2923 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2924 image->x_resolution);
2925 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2926 image->y_resolution);
2927 }
2928 }
2929 if (image->chromaticity.white_point.x != 0.0)
2930 {
2931 float
2932 chromaticity[6];
2933
2934 /*
2935 Set image chromaticity.
2936 */
2937 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2938 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2939 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2940 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2941 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2942 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2943 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2944 chromaticity[0]=(float) image->chromaticity.white_point.x;
2945 chromaticity[1]=(float) image->chromaticity.white_point.y;
2946 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2947 }
2948 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2949 {
2950 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2951 if (image->scene != 0)
2952 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
2953 GetImageListLength(image));
2954 }
2955 if (image->orientation != UndefinedOrientation)
2956 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
2957 (void) TIFFSetProfiles(tiff,image);
2958 {
2959 uint16
2960 page,
2961 pages;
2962
2963 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00002964 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00002965 if ((image_info->adjoin != MagickFalse) && (pages > 1))
2966 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2967 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
2968 }
2969 (void) TIFFSetProperties(tiff,image);
2970 if (0)
2971 (void) TIFFSetEXIFProperties(tiff,image);
2972 /*
2973 Write image scanlines.
2974 */
2975 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
2976 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2977 pixels=GetQuantumPixels(quantum_info);
2978 tiff_info.scanline=GetQuantumPixels(quantum_info);
2979 switch (photometric)
2980 {
2981 case PHOTOMETRIC_CIELAB:
2982 case PHOTOMETRIC_YCBCR:
2983 case PHOTOMETRIC_RGB:
2984 {
2985 /*
2986 RGB TIFF image.
2987 */
2988 switch (image_info->interlace)
2989 {
2990 case NoInterlace:
2991 default:
2992 {
2993 quantum_type=RGBQuantum;
2994 if (image->matte != MagickFalse)
2995 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00002996 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002997 {
2998 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002999 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003000
3001 p=GetVirtualPixels(image,0,y,image->columns,1,
3002 &image->exception);
3003 if (p == (const PixelPacket *) NULL)
3004 break;
3005 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3006 quantum_info,quantum_type,pixels,&image->exception);
3007 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3008 break;
3009 if (image->previous == (Image *) NULL)
3010 {
cristy2837bcc2010-08-07 23:57:39 +00003011 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3012 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003013 if (status == MagickFalse)
3014 break;
3015 }
3016 }
3017 break;
3018 }
3019 case PlaneInterlace:
3020 case PartitionInterlace:
3021 {
3022 /*
3023 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3024 */
cristybb503372010-05-27 20:51:26 +00003025 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003026 {
3027 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003028 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003029
3030 p=GetVirtualPixels(image,0,y,image->columns,1,
3031 &image->exception);
3032 if (p == (const PixelPacket *) NULL)
3033 break;
3034 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3035 quantum_info,RedQuantum,pixels,&image->exception);
3036 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3037 break;
3038 }
3039 if (image->previous == (Image *) NULL)
3040 {
3041 status=SetImageProgress(image,SaveImageTag,100,400);
3042 if (status == MagickFalse)
3043 break;
3044 }
cristybb503372010-05-27 20:51:26 +00003045 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003046 {
3047 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003048 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003049
3050 p=GetVirtualPixels(image,0,y,image->columns,1,
3051 &image->exception);
3052 if (p == (const PixelPacket *) NULL)
3053 break;
3054 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3055 quantum_info,GreenQuantum,pixels,&image->exception);
3056 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3057 break;
3058 }
3059 if (image->previous == (Image *) NULL)
3060 {
3061 status=SetImageProgress(image,SaveImageTag,200,400);
3062 if (status == MagickFalse)
3063 break;
3064 }
cristybb503372010-05-27 20:51:26 +00003065 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003066 {
3067 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003068 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003069
3070 p=GetVirtualPixels(image,0,y,image->columns,1,
3071 &image->exception);
3072 if (p == (const PixelPacket *) NULL)
3073 break;
3074 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3075 quantum_info,BlueQuantum,pixels,&image->exception);
3076 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3077 break;
3078 }
3079 if (image->previous == (Image *) NULL)
3080 {
3081 status=SetImageProgress(image,SaveImageTag,300,400);
3082 if (status == MagickFalse)
3083 break;
3084 }
3085 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003086 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003087 {
3088 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003089 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003090
3091 p=GetVirtualPixels(image,0,y,image->columns,1,
3092 &image->exception);
3093 if (p == (const PixelPacket *) NULL)
3094 break;
3095 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3096 quantum_info,AlphaQuantum,pixels,&image->exception);
3097 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3098 break;
3099 }
3100 if (image->previous == (Image *) NULL)
3101 {
3102 status=SetImageProgress(image,SaveImageTag,400,400);
3103 if (status == MagickFalse)
3104 break;
3105 }
3106 break;
3107 }
3108 }
3109 break;
3110 }
3111 case PHOTOMETRIC_SEPARATED:
3112 {
3113 /*
3114 CMYK TIFF image.
3115 */
3116 quantum_type=CMYKQuantum;
3117 if (image->matte != MagickFalse)
3118 quantum_type=CMYKAQuantum;
3119 if (image->colorspace != CMYKColorspace)
3120 (void) TransformImageColorspace(image,CMYKColorspace);
cristybb503372010-05-27 20:51:26 +00003121 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003122 {
3123 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003124 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003125
3126 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3127 if (p == (const PixelPacket *) NULL)
3128 break;
3129 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3130 quantum_info,quantum_type,pixels,&image->exception);
3131 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3132 break;
3133 if (image->previous == (Image *) NULL)
3134 {
cristycee97112010-05-28 00:44:52 +00003135 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3136 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003137 if (status == MagickFalse)
3138 break;
3139 }
3140 }
3141 break;
3142 }
3143 case PHOTOMETRIC_PALETTE:
3144 {
3145 uint16
3146 *blue,
3147 *green,
3148 *red;
3149
3150 /*
3151 Colormapped TIFF image.
3152 */
3153 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3154 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3155 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3156 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3157 (blue == (uint16 *) NULL))
3158 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3159 /*
3160 Initialize TIFF colormap.
3161 */
3162 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3163 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3164 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003165 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003166 {
3167 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3168 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3169 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3170 }
3171 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3172 red=(uint16 *) RelinquishMagickMemory(red);
3173 green=(uint16 *) RelinquishMagickMemory(green);
3174 blue=(uint16 *) RelinquishMagickMemory(blue);
3175 }
3176 default:
3177 {
3178 /*
3179 Convert PseudoClass packets to contiguous grayscale scanlines.
3180 */
3181 quantum_type=IndexQuantum;
3182 if (image->matte != MagickFalse)
3183 {
3184 if (photometric != PHOTOMETRIC_PALETTE)
3185 quantum_type=GrayAlphaQuantum;
3186 else
3187 quantum_type=IndexAlphaQuantum;
3188 }
3189 else
3190 if (photometric != PHOTOMETRIC_PALETTE)
3191 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003192 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003193 {
3194 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003195 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003196
3197 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3198 if (p == (const PixelPacket *) NULL)
3199 break;
3200 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3201 quantum_info,quantum_type,pixels,&image->exception);
3202 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3203 break;
3204 if (image->previous == (Image *) NULL)
3205 {
cristycee97112010-05-28 00:44:52 +00003206 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3207 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003208 if (status == MagickFalse)
3209 break;
3210 }
3211 }
3212 break;
3213 }
3214 }
3215 quantum_info=DestroyQuantumInfo(quantum_info);
3216 DestroyTIFFInfo(&tiff_info);
3217 if (0 && (image_info->verbose == MagickTrue))
3218 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3219 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003220 image->endian=MSBEndian;
3221 if (endian == FILLORDER_LSB2MSB)
3222 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003223 image=SyncNextImageInList(image);
3224 if (image == (Image *) NULL)
3225 break;
3226 status=SetImageProgress(image,SaveImagesTag,scene++,
3227 GetImageListLength(image));
3228 if (status == MagickFalse)
3229 break;
3230 } while (image_info->adjoin != MagickFalse);
3231 (void) TIFFSetWarningHandler(warning_handler);
3232 (void) TIFFSetErrorHandler(error_handler);
3233 TIFFClose(tiff);
3234 return(MagickTrue);
3235}
3236#endif