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