blob: bb8ed46ea62ae882fa34f98c91a238b2a8560583 [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;
1141 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1142 (interlace == PLANARCONFIG_SEPARATE))
1143 method=ReadGenericMethod;
1144 if (TIFFIsTiled(tiff) != MagickFalse)
1145 method=ReadTileMethod;
1146 quantum_type=RGBQuantum;
1147 pixels=GetQuantumPixels(quantum_info);
1148 switch (method)
1149 {
1150 case ReadSingleSampleMethod:
1151 {
1152 /*
1153 Convert TIFF image to PseudoClass MIFF image.
1154 */
1155 if ((image->storage_class == PseudoClass) &&
1156 (photometric == PHOTOMETRIC_PALETTE))
1157 {
cristybb503372010-05-27 20:51:26 +00001158 size_t
cristy3ed852e2009-09-05 21:47:34 +00001159 range;
1160
1161 uint16
1162 *blue_colormap,
1163 *green_colormap,
1164 *red_colormap;
1165
1166 /*
1167 Initialize colormap.
1168 */
1169 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1170 &green_colormap,&blue_colormap);
1171 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001172 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001173 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1174 (blue_colormap[i] >= 256))
1175 {
1176 range=65535;
1177 break;
1178 }
cristybb503372010-05-27 20:51:26 +00001179 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001180 {
cristyce70c172010-01-07 17:15:30 +00001181 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001182 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001183 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001184 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001185 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001186 blue_colormap[i])/range);
1187 }
1188 }
1189 quantum_type=IndexQuantum;
1190 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1191 if (image->matte != MagickFalse)
1192 {
1193 if (image->storage_class != PseudoClass)
1194 {
1195 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1196 GrayAlphaQuantum;
1197 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1198 }
1199 else
1200 {
1201 quantum_type=IndexAlphaQuantum;
1202 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1203 }
1204 }
1205 else
1206 if (image->storage_class != PseudoClass)
1207 {
1208 quantum_type=GrayQuantum;
1209 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1210 }
1211 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1212 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001213 {
1214 TIFFClose(tiff);
1215 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1216 }
cristy3ed852e2009-09-05 21:47:34 +00001217 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001218 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001219 {
1220 int
1221 status;
1222
1223 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001224 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001225
1226 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1227 if (status == -1)
1228 break;
1229 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1230 if (q == (PixelPacket *) NULL)
1231 break;
1232 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1233 quantum_type,pixels,exception);
1234 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1235 break;
1236 if (image->previous == (Image *) NULL)
1237 {
cristycee97112010-05-28 00:44:52 +00001238 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1239 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001240 if (status == MagickFalse)
1241 break;
1242 }
1243 }
1244 break;
1245 }
1246 case ReadRGBAMethod:
1247 {
1248 /*
1249 Convert TIFF image to DirectClass MIFF image.
1250 */
1251 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1252 quantum_type=RGBQuantum;
1253 if (image->matte != MagickFalse)
1254 {
1255 quantum_type=RGBAQuantum;
1256 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1257 }
1258 if (image->colorspace == CMYKColorspace)
1259 {
1260 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1261 quantum_type=CMYKQuantum;
1262 if (image->matte != MagickFalse)
1263 {
1264 quantum_type=CMYKAQuantum;
1265 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1266 }
1267 }
1268 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1269 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001270 {
1271 TIFFClose(tiff);
1272 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1273 }
cristy3ed852e2009-09-05 21:47:34 +00001274 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001275 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001276 {
1277 int
1278 status;
1279
1280 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001281 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001282
1283 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1284 if (status == -1)
1285 break;
1286 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1287 if (q == (PixelPacket *) NULL)
1288 break;
cristy0b334892010-03-15 02:26:46 +00001289 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1290 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001291 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1292 break;
1293 if (image->previous == (Image *) NULL)
1294 {
cristycee97112010-05-28 00:44:52 +00001295 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1296 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001297 if (status == MagickFalse)
1298 break;
1299 }
1300 }
1301 break;
1302 }
1303 case ReadCMYKAMethod:
1304 {
1305 /*
1306 Convert TIFF image to DirectClass MIFF image.
1307 */
cristybb503372010-05-27 20:51:26 +00001308 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001309 {
cristybb503372010-05-27 20:51:26 +00001310 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001311 {
cristy6d9f12f2009-11-03 14:50:26 +00001312 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001313 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001314
cristy3ed852e2009-09-05 21:47:34 +00001315 int
1316 status;
1317
1318 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1319 pixels);
1320 if (status == -1)
1321 break;
1322 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1323 if (q == (PixelPacket *) NULL)
1324 break;
1325 if (image->colorspace != CMYKColorspace)
1326 switch (i)
1327 {
1328 case 0: quantum_type=RedQuantum; break;
1329 case 1: quantum_type=GreenQuantum; break;
1330 case 2: quantum_type=BlueQuantum; break;
1331 case 3: quantum_type=AlphaQuantum; break;
1332 default: quantum_type=UndefinedQuantum; break;
1333 }
cristy6d9f12f2009-11-03 14:50:26 +00001334 else
cristy3ed852e2009-09-05 21:47:34 +00001335 switch (i)
1336 {
1337 case 0: quantum_type=CyanQuantum; break;
1338 case 1: quantum_type=MagentaQuantum; break;
1339 case 2: quantum_type=YellowQuantum; break;
1340 case 3: quantum_type=BlackQuantum; break;
1341 case 4: quantum_type=AlphaQuantum; break;
1342 default: quantum_type=UndefinedQuantum; break;
1343 }
1344 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1345 quantum_type,pixels,exception);
1346 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1347 break;
1348 }
1349 if (image->previous == (Image *) NULL)
1350 {
cristycee97112010-05-28 00:44:52 +00001351 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1352 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001353 if (status == MagickFalse)
1354 break;
1355 }
1356 }
1357 break;
1358 }
1359 case ReadStripMethod:
1360 {
1361 register unsigned char
1362 *p;
1363
1364 /*
1365 Convert stripped TIFF image to DirectClass MIFF image.
1366 */
1367 i=0;
1368 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001369 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001370 {
cristybb503372010-05-27 20:51:26 +00001371 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001372 x;
1373
1374 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001375 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001376
1377 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1378 if (q == (PixelPacket *) NULL)
1379 break;
1380 if (i == 0)
1381 {
1382 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1383 break;
cristyeaedf062010-05-29 22:36:02 +00001384 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1385 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001386 }
1387 i--;
1388 p=pixels+image->columns*i;
cristybb503372010-05-27 20:51:26 +00001389 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001390 {
1391 q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
1392 q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
1393 q->blue=ScaleCharToQuantum((unsigned char) (TIFFGetB(*p)));
1394 if (image->matte != MagickFalse)
1395 q->opacity=ScaleCharToQuantum((unsigned char) (TIFFGetA(*p)));
1396 p++;
1397 q++;
1398 }
1399 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1400 break;
1401 if (image->previous == (Image *) NULL)
1402 {
cristycee97112010-05-28 00:44:52 +00001403 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1404 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001405 if (status == MagickFalse)
1406 break;
1407 }
1408 }
1409 break;
1410 }
1411 case ReadTileMethod:
1412 {
1413 register uint32
1414 *p;
1415
1416 uint32
1417 *tile_pixels,
1418 columns,
1419 rows;
1420
cristybb503372010-05-27 20:51:26 +00001421 size_t
cristy3ed852e2009-09-05 21:47:34 +00001422 number_pixels;
1423
1424 /*
1425 Convert tiled TIFF image to DirectClass MIFF image.
1426 */
1427 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1428 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1429 {
1430 TIFFClose(tiff);
1431 ThrowReaderException(CoderError,"ImageIsNotTiled");
1432 }
1433 (void) SetImageStorageClass(image,DirectClass);
1434 number_pixels=columns*rows;
1435 tile_pixels=(uint32 *) AcquireQuantumMemory((size_t) columns*rows,
1436 sizeof(*tile_pixels));
1437 if (tile_pixels == (uint32 *) NULL)
1438 {
1439 TIFFClose(tiff);
1440 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1441 }
cristybb503372010-05-27 20:51:26 +00001442 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001443 {
1444 PixelPacket
1445 *tile;
1446
cristybb503372010-05-27 20:51:26 +00001447 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001448 x;
1449
1450 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001451 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001452
cristybb503372010-05-27 20:51:26 +00001453 size_t
cristy3ed852e2009-09-05 21:47:34 +00001454 columns_remaining,
1455 rows_remaining;
1456
1457 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001458 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001459 rows_remaining=rows;
1460 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1461 exception);
1462 if (tile == (PixelPacket *) NULL)
1463 break;
cristybb503372010-05-27 20:51:26 +00001464 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001465 {
cristybb503372010-05-27 20:51:26 +00001466 size_t
cristy3ed852e2009-09-05 21:47:34 +00001467 column,
1468 row;
1469
1470 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1471 break;
1472 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001473 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001474 columns_remaining=columns;
1475 p=tile_pixels+(rows-rows_remaining)*columns;
1476 q=tile+(image->columns*(rows_remaining-1)+x);
1477 for (row=rows_remaining; row > 0; row--)
1478 {
1479 if (image->matte != MagickFalse)
1480 for (column=columns_remaining; column > 0; column--)
1481 {
1482 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1483 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1484 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1485 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1486 (unsigned char) TIFFGetA(*p)));
1487 q++;
1488 p++;
1489 }
1490 else
1491 for (column=columns_remaining; column > 0; column--)
1492 {
1493 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1494 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1495 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1496 q++;
1497 p++;
1498 }
1499 p+=columns-columns_remaining;
1500 q-=(image->columns+columns_remaining);
1501 }
1502 }
1503 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1504 break;
1505 if (image->previous == (Image *) NULL)
1506 {
cristycee97112010-05-28 00:44:52 +00001507 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1508 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001509 if (status == MagickFalse)
1510 break;
1511 }
1512 }
1513 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1514 break;
1515 }
1516 case ReadGenericMethod:
1517 default:
1518 {
1519 register uint32
1520 *p;
1521
1522 uint32
1523 *pixels;
1524
1525 /*
1526 Convert TIFF image to DirectClass MIFF image.
1527 */
1528 number_pixels=(MagickSizeType) image->columns*image->rows;
1529 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1530 (number_pixels*sizeof(uint32))))
1531 {
1532 TIFFClose(tiff);
1533 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1534 }
1535 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1536 sizeof(uint32));
1537 if (pixels == (uint32 *) NULL)
1538 {
1539 TIFFClose(tiff);
1540 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1541 }
1542 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1543 (uint32) image->rows,(uint32 *) pixels,0);
1544 /*
1545 Convert image to DirectClass pixel packets.
1546 */
1547 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001548 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001549 {
cristybb503372010-05-27 20:51:26 +00001550 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001551 x;
1552
1553 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001554 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001555
1556 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1557 if (q == (PixelPacket *) NULL)
1558 break;
1559 q+=image->columns-1;
cristybb503372010-05-27 20:51:26 +00001560 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001561 {
1562 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1563 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1564 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1565 if (image->matte != MagickFalse)
1566 q->opacity=ScaleCharToQuantum((unsigned char) TIFFGetA(*p));
1567 p--;
1568 q--;
1569 }
1570 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1571 break;
1572 if (image->previous == (Image *) NULL)
1573 {
cristycee97112010-05-28 00:44:52 +00001574 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1575 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001576 if (status == MagickFalse)
1577 break;
1578 }
1579 }
1580 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1581 break;
1582 }
1583 }
1584 SetQuantumImageType(image,quantum_type);
1585 next_tiff_frame:
1586 if ((photometric == PHOTOMETRIC_LOGL) ||
1587 (photometric == PHOTOMETRIC_MINISBLACK) ||
1588 (photometric == PHOTOMETRIC_MINISWHITE))
1589 {
1590 image->type=GrayscaleType;
1591 if (bits_per_sample == 1)
1592 image->type=BilevelType;
1593 }
1594 if (image->storage_class == PseudoClass)
1595 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001596 image->endian=MSBEndian;
1597 if (endian == FILLORDER_LSB2MSB)
1598 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001599 if ((photometric == PHOTOMETRIC_LOGL) ||
1600 (photometric == PHOTOMETRIC_MINISBLACK) ||
1601 (photometric == PHOTOMETRIC_MINISWHITE))
1602 {
1603 image->type=GrayscaleType;
1604 if (bits_per_sample == 1)
1605 image->type=BilevelType;
1606 }
1607 /*
1608 Proceed to next image.
1609 */
1610 if (image_info->number_scenes != 0)
1611 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1612 break;
1613 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1614 if (status == MagickTrue)
1615 {
1616 /*
1617 Allocate next image structure.
1618 */
1619 AcquireNextImage(image_info,image);
1620 if (GetNextImageInList(image) == (Image *) NULL)
1621 {
1622 image=DestroyImageList(image);
1623 return((Image *) NULL);
1624 }
1625 image=SyncNextImageInList(image);
1626 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1627 image->scene);
1628 if (status == MagickFalse)
1629 break;
1630 }
1631 quantum_info=DestroyQuantumInfo(quantum_info);
1632 } while (status == MagickTrue);
1633 (void) TIFFSetWarningHandler(warning_handler);
1634 (void) TIFFSetErrorHandler(error_handler);
1635 TIFFClose(tiff);
1636 return(GetFirstImageInList(image));
1637}
1638#endif
1639
1640/*
1641%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1642% %
1643% %
1644% %
1645% R e g i s t e r T I F F I m a g e %
1646% %
1647% %
1648% %
1649%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1650%
1651% RegisterTIFFImage() adds properties for the TIFF image format to
1652% the list of supported formats. The properties include the image format
1653% tag, a method to read and/or write the format, whether the format
1654% supports the saving of more than one frame to the same file or blob,
1655% whether the format supports native in-memory I/O, and a brief
1656% description of the format.
1657%
1658% The format of the RegisterTIFFImage method is:
1659%
cristybb503372010-05-27 20:51:26 +00001660% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001661%
1662*/
cristybb503372010-05-27 20:51:26 +00001663ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001664{
1665#define TIFFDescription "Tagged Image File Format"
1666
1667 char
1668 version[MaxTextExtent];
1669
1670 MagickInfo
1671 *entry;
cristy6b032822010-06-29 16:52:32 +00001672
cristy18b17442009-10-25 18:36:48 +00001673 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1674 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001675 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001676 if (instantiate_key == MagickFalse)
1677 {
1678 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1679 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1680 instantiate_key=MagickTrue;
1681 }
cristyf84a1932010-01-03 18:00:18 +00001682 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001683 *version='\0';
1684#if defined(TIFF_VERSION)
1685 (void) FormatMagickString(version,MaxTextExtent,"%d",TIFF_VERSION);
1686#endif
1687#if defined(MAGICKCORE_TIFF_DELEGATE)
1688 {
1689 const char
1690 *p;
1691
cristybb503372010-05-27 20:51:26 +00001692 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001693 i;
1694
1695 p=TIFFGetVersion();
1696 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1697 version[i]=(*p++);
1698 version[i]='\0';
1699 }
1700#endif
1701
cristy3d7f8062009-09-24 20:45:35 +00001702 entry=SetMagickInfo("GROUP4");
1703#if defined(MAGICKCORE_TIFF_DELEGATE)
1704 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1705 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1706#endif
1707 entry->raw=MagickTrue;
1708 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001709 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001710 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001711 entry->seekable_stream=MagickTrue;
1712 entry->thread_support=NoThreadSupport;
1713 entry->description=ConstantString("Raw CCITT Group4");
1714 entry->module=ConstantString("TIFF");
1715 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001716 entry=SetMagickInfo("PTIF");
1717#if defined(MAGICKCORE_TIFF_DELEGATE)
1718 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1719 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1720#endif
1721 entry->endian_support=MagickTrue;
1722 entry->seekable_stream=MagickTrue;
1723 entry->thread_support=NoThreadSupport;
1724 entry->description=ConstantString("Pyramid encoded TIFF");
1725 entry->module=ConstantString("TIFF");
1726 (void) RegisterMagickInfo(entry);
1727 entry=SetMagickInfo("TIF");
1728#if defined(MAGICKCORE_TIFF_DELEGATE)
1729 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1730 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1731#endif
1732 entry->endian_support=MagickTrue;
1733 entry->seekable_stream=MagickTrue;
1734 entry->stealth=MagickTrue;
1735 entry->thread_support=NoThreadSupport;
1736 entry->description=ConstantString(TIFFDescription);
1737 if (*version != '\0')
1738 entry->version=ConstantString(version);
1739 entry->module=ConstantString("TIFF");
1740 (void) RegisterMagickInfo(entry);
1741 entry=SetMagickInfo("TIFF");
1742#if defined(MAGICKCORE_TIFF_DELEGATE)
1743 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1744 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1745#endif
1746 entry->magick=(IsImageFormatHandler *) IsTIFF;
1747 entry->endian_support=MagickTrue;
1748 entry->seekable_stream=MagickTrue;
1749 entry->thread_support=NoThreadSupport;
1750 entry->description=ConstantString(TIFFDescription);
1751 if (*version != '\0')
1752 entry->version=ConstantString(version);
1753 entry->module=ConstantString("TIFF");
1754 (void) RegisterMagickInfo(entry);
1755 entry=SetMagickInfo("TIFF64");
1756#if defined(TIFF_VERSION_BIG)
1757 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1758 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1759#endif
1760 entry->adjoin=MagickFalse;
1761 entry->endian_support=MagickTrue;
1762 entry->seekable_stream=MagickTrue;
1763 entry->thread_support=NoThreadSupport;
1764 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1765 if (*version != '\0')
1766 entry->version=ConstantString(version);
1767 entry->module=ConstantString("TIFF");
1768 (void) RegisterMagickInfo(entry);
1769 return(MagickImageCoderSignature);
1770}
1771
1772/*
1773%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1774% %
1775% %
1776% %
1777% U n r e g i s t e r T I F F I m a g e %
1778% %
1779% %
1780% %
1781%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1782%
1783% UnregisterTIFFImage() removes format registrations made by the TIFF module
1784% from the list of supported formats.
1785%
1786% The format of the UnregisterTIFFImage method is:
1787%
1788% UnregisterTIFFImage(void)
1789%
1790*/
1791ModuleExport void UnregisterTIFFImage(void)
1792{
cristy3d7f8062009-09-24 20:45:35 +00001793 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001794 (void) UnregisterMagickInfo("PTIF");
1795 (void) UnregisterMagickInfo("TIF");
1796 (void) UnregisterMagickInfo("TIFF");
1797 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001798 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1799 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001800 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001801 if (instantiate_key != MagickFalse)
1802 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1803 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1804 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001805 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001806 DestroySemaphoreInfo(&tiff_semaphore);
1807}
1808
1809#if defined(MAGICKCORE_TIFF_DELEGATE)
1810/*
1811%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1812% %
1813% %
1814% %
cristy3d7f8062009-09-24 20:45:35 +00001815% W r i t e G R O U P 4 I m a g e %
1816% %
1817% %
1818% %
1819%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1820%
1821% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1822%
1823% The format of the WriteGROUP4Image method is:
1824%
1825% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1826% Image *image)
1827%
1828% A description of each parameter follows:
1829%
1830% o image_info: the image info.
1831%
1832% o image: The image.
1833%
1834*/
1835static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1836 Image *image)
1837{
1838 char
1839 filename[MaxTextExtent];
1840
1841 FILE
1842 *file;
1843
1844 Image
1845 *huffman_image;
1846
1847 ImageInfo
1848 *write_info;
1849
1850 int
1851 unique_file;
1852
1853 MagickBooleanType
1854 status;
1855
cristybb503372010-05-27 20:51:26 +00001856 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001857 i;
1858
1859 ssize_t
1860 count;
1861
1862 TIFF
1863 *tiff;
1864
cristy94c8fe42009-10-06 01:57:36 +00001865 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001866 *byte_count,
1867 strip_size;
1868
1869 unsigned char
1870 *buffer;
1871
1872 /*
1873 Write image as CCITT Group4 TIFF image to a temporary file.
1874 */
1875 assert(image_info != (const ImageInfo *) NULL);
1876 assert(image_info->signature == MagickSignature);
1877 assert(image != (Image *) NULL);
1878 assert(image->signature == MagickSignature);
1879 if (image->debug != MagickFalse)
1880 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1881 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1882 if (status == MagickFalse)
1883 return(status);
1884 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1885 if (huffman_image == (Image *) NULL)
1886 {
1887 (void) CloseBlob(image);
1888 return(MagickFalse);
1889 }
cristy94c8fe42009-10-06 01:57:36 +00001890 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001891 file=(FILE *) NULL;
1892 unique_file=AcquireUniqueFileResource(filename);
1893 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001894 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001895 if ((unique_file == -1) || (file == (FILE *) NULL))
1896 {
1897 ThrowFileException(&image->exception,FileOpenError,
1898 "UnableToCreateTemporaryFile",filename);
1899 return(MagickFalse);
1900 }
1901 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1902 filename);
1903 (void) SetImageType(huffman_image,BilevelType);
cristy14efd992009-09-26 23:46:03 +00001904 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001905 SetImageInfoFile(write_info,file);
1906 write_info->compression=Group4Compression;
1907 write_info->type=BilevelType;
1908 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1909 status=WriteTIFFImage(write_info,huffman_image);
1910 (void) fflush(file);
1911 write_info=DestroyImageInfo(write_info);
1912 if (status == MagickFalse)
1913 {
1914 InheritException(&image->exception,&huffman_image->exception);
1915 huffman_image=DestroyImage(huffman_image);
1916 (void) fclose(file);
1917 (void) RelinquishUniqueFileResource(filename);
1918 return(MagickFalse);
1919 }
1920 tiff=TIFFOpen(filename,"rb");
1921 if (tiff == (TIFF *) NULL)
1922 {
1923 huffman_image=DestroyImage(huffman_image);
1924 (void) fclose(file);
1925 (void) RelinquishUniqueFileResource(filename);
1926 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1927 image_info->filename);
1928 return(MagickFalse);
1929 }
1930 /*
1931 Allocate raw strip buffer.
1932 */
cristy94c8fe42009-10-06 01:57:36 +00001933 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1934 {
1935 TIFFClose(tiff);
1936 huffman_image=DestroyImage(huffman_image);
1937 (void) fclose(file);
1938 (void) RelinquishUniqueFileResource(filename);
1939 return(MagickFalse);
1940 }
cristy3d7f8062009-09-24 20:45:35 +00001941 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00001942 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001943 if (byte_count[i] > strip_size)
1944 strip_size=byte_count[i];
1945 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1946 sizeof(*buffer));
1947 if (buffer == (unsigned char *) NULL)
1948 {
1949 TIFFClose(tiff);
1950 huffman_image=DestroyImage(huffman_image);
1951 (void) fclose(file);
1952 (void) RelinquishUniqueFileResource(filename);
1953 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1954 image_info->filename);
1955 }
1956 /*
1957 Compress runlength encoded to 2D Huffman pixels.
1958 */
cristybb503372010-05-27 20:51:26 +00001959 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001960 {
cristy94c8fe42009-10-06 01:57:36 +00001961 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00001962 if (WriteBlob(image,(size_t) count,buffer) != count)
1963 status=MagickFalse;
1964 }
1965 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1966 TIFFClose(tiff);
1967 huffman_image=DestroyImage(huffman_image);
1968 (void) fclose(file);
1969 (void) RelinquishUniqueFileResource(filename);
1970 (void) CloseBlob(image);
1971 return(status);
1972}
1973#endif
1974
1975#if defined(MAGICKCORE_TIFF_DELEGATE)
1976/*
1977%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1978% %
1979% %
1980% %
cristy3ed852e2009-09-05 21:47:34 +00001981% W r i t e P T I F I m a g e %
1982% %
1983% %
1984% %
1985%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1986%
1987% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
1988% format.
1989%
1990% The format of the WritePTIFImage method is:
1991%
1992% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
1993% Image *image)
1994%
1995% A description of each parameter follows:
1996%
1997% o image_info: the image info.
1998%
1999% o image: The image.
2000%
2001*/
2002static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2003 Image *image)
2004{
2005 Image
2006 *images,
2007 *next,
2008 *pyramid_image;
2009
2010 ImageInfo
2011 *write_info;
2012
2013 MagickBooleanType
2014 status;
2015
cristybb503372010-05-27 20:51:26 +00002016 size_t
cristy3ed852e2009-09-05 21:47:34 +00002017 columns,
2018 rows;
2019
2020 /*
2021 Create pyramid-encoded TIFF image.
2022 */
2023 images=NewImageList();
2024 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2025 {
2026 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2027 &image->exception));
2028 columns=next->columns;
2029 rows=next->rows;
2030 while ((columns > 64) && (rows > 64))
2031 {
2032 columns/=2;
2033 rows/=2;
2034 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2035 &image->exception);
2036 AppendImageToList(&images,pyramid_image);
2037 }
2038 }
2039 /*
2040 Write pyramid-encoded TIFF image.
2041 */
cristy58567532009-09-26 01:13:32 +00002042 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002043 *write_info->magick='\0';
2044 write_info->adjoin=MagickTrue;
2045 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2046 images=DestroyImageList(images);
2047 write_info=DestroyImageInfo(write_info);
2048 return(status);
2049}
2050#endif
2051
2052#if defined(MAGICKCORE_TIFF_DELEGATE)
2053/*
2054%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2055% %
2056% %
2057% W r i t e T I F F I m a g e %
2058% %
2059% %
2060% %
2061%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2062%
2063% WriteTIFFImage() writes an image in the Tagged image file format.
2064%
2065% The format of the WriteTIFFImage method is:
2066%
2067% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2068% Image *image)
2069%
2070% A description of each parameter follows:
2071%
2072% o image_info: the image info.
2073%
2074% o image: The image.
2075%
2076*/
2077
2078typedef struct _TIFFInfo
2079{
2080 RectangleInfo
2081 tile_geometry;
2082
2083 unsigned char
2084 *scanline,
2085 *scanlines,
2086 *pixels;
2087} TIFFInfo;
2088
2089static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2090{
2091 assert(tiff_info != (TIFFInfo *) NULL);
2092 if (tiff_info->scanlines != (unsigned char *) NULL)
2093 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2094 tiff_info->scanlines);
2095 if (tiff_info->pixels != (unsigned char *) NULL)
2096 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2097 tiff_info->pixels);
2098}
2099
2100static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2101 TIFFInfo *tiff_info)
2102{
2103 const char
2104 *option;
2105
2106 MagickStatusType
2107 flags;
2108
2109 assert(tiff_info != (TIFFInfo *) NULL);
2110 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2111 option=GetImageOption(image_info,"tiff:tile-geometry");
2112 if (option == (const char *) NULL)
2113 return(MagickTrue);
2114 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2115 if ((flags & HeightValue) == 0)
2116 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2117 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,(uint32)
2118 tiff_info->tile_geometry.width);
2119 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,(uint32)
2120 tiff_info->tile_geometry.height);
2121 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2122 tiff_info->tile_geometry.height*TIFFScanlineSize(tiff),
2123 sizeof(*tiff_info->scanlines));
2124 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2125 TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2126 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2127 (tiff_info->pixels == (unsigned char *) NULL))
2128 {
2129 DestroyTIFFInfo(tiff_info);
2130 return(MagickFalse);
2131 }
2132 return(MagickTrue);
2133}
2134
cristybb503372010-05-27 20:51:26 +00002135static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002136 tsample_t sample,Image *image)
2137{
2138 int32
2139 status;
2140
cristybb503372010-05-27 20:51:26 +00002141 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002142 bytes_per_pixel,
2143 j,
2144 k,
2145 l;
2146
cristybb503372010-05-27 20:51:26 +00002147 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002148 i;
2149
2150 register unsigned char
2151 *p,
2152 *q;
2153
cristybb503372010-05-27 20:51:26 +00002154 size_t
cristy3ed852e2009-09-05 21:47:34 +00002155 number_tiles,
2156 tile_width;
2157
2158 if (TIFFIsTiled(tiff) == 0)
2159 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2160 /*
2161 Fill scanlines to tile height.
2162 */
cristybb503372010-05-27 20:51:26 +00002163 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002164 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2165 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002166 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2167 (tiff_info->tile_geometry.height-1)) && (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002168 return(0);
2169 /*
2170 Write tile to TIFF image.
2171 */
2172 status=0;
cristybb503372010-05-27 20:51:26 +00002173 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002174 tiff_info->tile_geometry.width);
2175 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2176 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002177 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002178 {
cristybb503372010-05-27 20:51:26 +00002179 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002180 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002181 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2182 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002183 {
2184 if (bytes_per_pixel == 0)
2185 {
2186 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2187 tiff_info->tile_geometry.width+k)/8);
2188 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2189 *q++=(*p++);
2190 continue;
2191 }
2192 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2193 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2194 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2195 for (l=0; l < bytes_per_pixel; l++)
2196 *q++=(*p++);
2197 }
cristydaff8092010-04-22 14:50:53 +00002198 if ((i*tiff_info->tile_geometry.width) != image->columns)
2199 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2200 tiff_info->tile_geometry.width),(uint32) ((row/
2201 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2202 sample);
cristy3ed852e2009-09-05 21:47:34 +00002203 if (status < 0)
2204 break;
2205 }
2206 return(status);
2207}
2208
2209static void TIFFSetProfiles(TIFF *tiff,Image *image)
2210{
2211 const char
2212 *name;
2213
2214 const StringInfo
2215 *profile;
2216
2217 if (image->profiles == (void *) NULL)
2218 return;
2219 ResetImageProfileIterator(image);
2220 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2221 {
2222 profile=GetImageProfile(image,name);
2223#if defined(TIFFTAG_XMLPACKET)
2224 if (LocaleCompare(name,"xmp") == 0)
2225 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2226 profile),GetStringInfoDatum(profile));
2227#endif
2228#if defined(TIFFTAG_ICCPROFILE)
2229 if (LocaleCompare(name,"icc") == 0)
2230 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2231 profile),GetStringInfoDatum(profile));
2232#endif
2233 if (LocaleCompare(name,"iptc") == 0)
2234 {
2235 size_t
2236 length;
2237
2238 StringInfo
2239 *iptc_profile;
2240
2241 iptc_profile=CloneStringInfo(profile);
2242 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2243 0x03);
2244 SetStringInfoLength(iptc_profile,length);
2245 if (TIFFIsByteSwapped(tiff))
2246 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002247 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002248 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2249 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2250 iptc_profile=DestroyStringInfo(iptc_profile);
2251 }
2252#if defined(TIFFTAG_PHOTOSHOP)
2253 if (LocaleCompare(name,"8bim") == 0)
2254 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2255 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2256#endif
2257 if (LocaleCompare(name,"tiff:37724") == 0)
2258 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2259 GetStringInfoDatum(profile));
2260 name=GetNextImageProfile(image);
2261 }
2262}
2263
2264static void TIFFSetProperties(TIFF *tiff,Image *image)
2265{
2266 const char
2267 *value;
2268
2269 value=GetImageProperty(image,"tiff:hostcomputer");
2270 if (value != (const char *) NULL)
2271 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2272 value=GetImageProperty(image,"tiff:artist");
2273 if (value != (const char *) NULL)
2274 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2275 value=GetImageProperty(image,"tiff:timestamp");
2276 if (value != (const char *) NULL)
2277 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2278 value=GetImageProperty(image,"tiff:make");
2279 if (value != (const char *) NULL)
2280 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2281 value=GetImageProperty(image,"tiff:model");
2282 if (value != (const char *) NULL)
2283 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2284 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,
cristybb503372010-05-27 20:51:26 +00002285 GetMagickVersion((size_t *) NULL));
cristy3ed852e2009-09-05 21:47:34 +00002286 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
2287 value=GetImageProperty(image,"tiff:copyright");
2288 if (value != (const char *) NULL)
2289 (void) TIFFSetField(tiff,33432,value);
2290 value=GetImageProperty(image,"kodak-33423");
2291 if (value != (const char *) NULL)
2292 (void) TIFFSetField(tiff,33423,value);
2293 value=GetImageProperty(image,"kodak-36867");
2294 if (value != (const char *) NULL)
2295 (void) TIFFSetField(tiff,36867,value);
2296 value=GetImageProperty(image,"label");
2297 if (value != (const char *) NULL)
2298 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2299 value=GetImageProperty(image,"comment");
2300 if (value != (const char *) NULL)
2301 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2302}
2303
2304static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2305{
2306#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2307 const char
2308 *value;
2309
cristybb503372010-05-27 20:51:26 +00002310 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002311 i;
2312
2313 uint32
2314 offset;
2315
2316 /*
2317 Write EXIF properties.
2318 */
2319 offset=0;
2320 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2321 for (i=0; exif_info[i].tag != 0; i++)
2322 {
2323 value=GetImageProperty(image,exif_info[i].property);
2324 if (value == (const char *) NULL)
2325 continue;
2326 switch (exif_info[i].type)
2327 {
2328 case TIFF_ASCII:
2329 {
2330 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2331 break;
2332 }
2333 case TIFF_SHORT:
2334 {
2335 uint16
2336 shorty;
2337
cristyf2f27272009-12-17 14:48:46 +00002338 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002339 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2340 break;
2341 }
2342 case TIFF_LONG:
2343 {
2344 uint16
cristybb503372010-05-27 20:51:26 +00002345 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002346
cristybb503372010-05-27 20:51:26 +00002347 ssize_ty=(uint16) StringToLong(value);
2348 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002349 break;
2350 }
2351 case TIFF_RATIONAL:
2352 case TIFF_SRATIONAL:
2353 {
2354 float
2355 rational;
2356
cristyf2f27272009-12-17 14:48:46 +00002357 rational=StringToDouble(value);
cristy3ed852e2009-09-05 21:47:34 +00002358 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2359 break;
2360 }
2361 default:
2362 break;
2363 }
2364 }
2365 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2366#else
2367 (void) tiff;
2368 (void) image;
2369#endif
2370}
2371
2372static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2373 Image *image)
2374{
2375#if !defined(TIFFDefaultStripSize)
2376#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2377#endif
2378
2379 const char
2380 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002381 *option;
cristy3ed852e2009-09-05 21:47:34 +00002382
2383 CompressionType
2384 compression;
2385
cristy6b032822010-06-29 16:52:32 +00002386 EndianType
2387 endian_type;
2388
cristybb503372010-05-27 20:51:26 +00002389 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002390 y;
2391
2392 MagickBooleanType
2393 debug,
2394 status;
2395
2396 MagickOffsetType
2397 scene;
2398
2399 QuantumInfo
2400 *quantum_info;
2401
2402 QuantumType
2403 quantum_type;
2404
cristybb503372010-05-27 20:51:26 +00002405 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002406 i;
2407
2408 size_t
2409 length;
2410
2411 TIFF
2412 *tiff;
2413
2414 TIFFErrorHandler
2415 error_handler,
2416 warning_handler;
2417
2418 TIFFInfo
2419 tiff_info;
2420
2421 uint16
2422 bits_per_sample,
2423 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002424 endian,
cristy3ed852e2009-09-05 21:47:34 +00002425 photometric;
2426
2427 uint32
2428 rows_per_strip;
2429
2430 unsigned char
2431 *pixels;
2432
cristyf2687ca2010-06-29 16:32:38 +00002433 size_t
2434 lsb_first;
2435
cristy3ed852e2009-09-05 21:47:34 +00002436 /*
2437 Open TIFF file.
2438 */
2439 assert(image_info != (const ImageInfo *) NULL);
2440 assert(image_info->signature == MagickSignature);
2441 assert(image != (Image *) NULL);
2442 assert(image->signature == MagickSignature);
2443 if (image->debug != MagickFalse)
2444 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2445 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2446 if (status == MagickFalse)
2447 return(status);
2448 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2449 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2450 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002451 endian_type=UndefinedEndian;
2452 option=GetImageOption(image_info,"tiff:endian");
2453 if (option != (const char *) NULL)
2454 {
2455 if (LocaleNCompare(option,"msb",3) == 0)
2456 endian_type=MSBEndian;
2457 if (LocaleNCompare(option,"lsb",3) == 0)
2458 endian_type=LSBEndian;;
2459 }
2460 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002461 {
2462 case LSBEndian: mode="wl"; break;
2463 case MSBEndian: mode="wb"; break;
2464 default: mode="w"; break;
2465 }
2466#if defined(TIFF_VERSION_BIG)
2467 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002468 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002469 {
2470 case LSBEndian: mode="wl8"; break;
2471 case MSBEndian: mode="wb8"; break;
2472 default: mode="w8"; break;
2473 }
2474#endif
2475 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2476 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2477 TIFFUnmapBlob);
2478 if (tiff == (TIFF *) NULL)
2479 {
2480 (void) TIFFSetWarningHandler(warning_handler);
2481 (void) TIFFSetErrorHandler(error_handler);
2482 return(MagickFalse);
2483 }
2484 scene=0;
2485 debug=IsEventLogging();
2486 do
2487 {
2488 /*
2489 Initialize TIFF fields.
2490 */
2491 if ((image_info->type != UndefinedType) &&
2492 (image_info->type != OptimizeType))
2493 (void) SetImageType(image,image_info->type);
2494 quantum_info=AcquireQuantumInfo(image_info,image);
2495 if (quantum_info == (QuantumInfo *) NULL)
2496 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2497 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2498 (quantum_info->format == UndefinedQuantumFormat) &&
2499 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2500 {
2501 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2502 if (status == MagickFalse)
2503 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2504 }
2505 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2506 (GetPreviousImageInList(image) != (Image *) NULL))
2507 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2508 if ((image->columns != (uint32) image->columns) ||
2509 (image->rows != (uint32) image->rows))
2510 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2511 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2512 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2513 compression=image->compression;
2514 if (image_info->compression != UndefinedCompression)
2515 compression=image_info->compression;
2516 switch (compression)
2517 {
2518 case FaxCompression:
2519 {
2520 compress_tag=COMPRESSION_CCITTFAX3;
2521 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2522 break;
2523 }
2524 case Group4Compression:
2525 {
2526 compress_tag=COMPRESSION_CCITTFAX4;
2527 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2528 break;
2529 }
2530 case JPEGCompression:
2531 {
2532 compress_tag=COMPRESSION_JPEG;
2533 break;
2534 }
2535 case LZWCompression:
2536 {
2537 compress_tag=COMPRESSION_LZW;
2538 break;
2539 }
2540 case RLECompression:
2541 {
2542 compress_tag=COMPRESSION_PACKBITS;
2543 break;
2544 }
2545 case ZipCompression:
2546 {
2547 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2548 break;
2549 }
2550 case NoCompression:
2551 default:
2552 {
2553 compress_tag=COMPRESSION_NONE;
2554 break;
2555 }
2556 }
cristy5b675872009-10-21 13:12:00 +00002557#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2558 if ((compress_tag != COMPRESSION_NONE) &&
2559 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002560 {
cristy5b675872009-10-21 13:12:00 +00002561 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2562 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002563 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002564 compress_tag=COMPRESSION_NONE;
2565 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002566 }
2567#else
2568 switch (compress_tag)
2569 {
2570#if defined(CCITT_SUPPORT)
2571 case COMPRESSION_CCITTFAX3:
2572 case COMPRESSION_CCITTFAX4:
2573#endif
2574#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2575 case COMPRESSION_JPEG:
2576#endif
2577#if defined(LZW_SUPPORT)
2578 case COMPRESSION_LZW:
2579#endif
2580#if defined(PACKBITS_SUPPORT)
2581 case COMPRESSION_PACKBITS:
2582#endif
2583#if defined(ZIP_SUPPORT)
2584 case COMPRESSION_ADOBE_DEFLATE:
2585#endif
2586 case COMPRESSION_NONE:
2587 break;
2588 default:
2589 {
2590 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2591 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002592 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002593 compress_tag=COMPRESSION_NONE;
2594 compression=NoCompression;
2595 break;
2596 }
2597 }
2598#endif
2599 switch (compression)
2600 {
2601 case FaxCompression:
2602 case Group4Compression:
2603 {
2604 (void) SetImageType(image,BilevelType);
2605 break;
2606 }
2607 case JPEGCompression:
2608 {
2609 (void) SetImageStorageClass(image,DirectClass);
2610 (void) SetImageDepth(image,8);
2611 break;
2612 }
2613 default:
2614 break;
2615 }
cristy3ed852e2009-09-05 21:47:34 +00002616 if (image->colorspace == CMYKColorspace)
2617 {
2618 photometric=PHOTOMETRIC_SEPARATED;
2619 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2620 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2621 }
2622 else
2623 {
2624 /*
2625 Full color TIFF raster.
2626 */
2627 if (image->colorspace == LabColorspace)
2628 photometric=PHOTOMETRIC_CIELAB;
2629 else
2630 if (image->colorspace == YCbCrColorspace)
2631 {
2632 photometric=PHOTOMETRIC_YCBCR;
2633 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
2634 (void) SetImageDepth(image,8);
2635 }
2636 else
2637 {
2638 if (image->colorspace != RGBColorspace)
2639 (void) TransformImageColorspace(image,RGBColorspace);
2640 photometric=PHOTOMETRIC_RGB;
2641 }
2642 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2643 if ((image_info->type != TrueColorType) &&
2644 (image_info->type != TrueColorMatteType))
2645 {
cristy94c8fe42009-10-06 01:57:36 +00002646 if ((image_info->type != PaletteType) &&
2647 (IsGrayImage(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002648 {
cristy94c8fe42009-10-06 01:57:36 +00002649 photometric=(uint16) (quantum_info->min_is_white !=
2650 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2651 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002652 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002653 if ((image_info->depth == 0) &&
2654 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2655 {
2656 status=SetQuantumDepth(image,quantum_info,1);
2657 if (status == MagickFalse)
2658 ThrowWriterException(ResourceLimitError,
2659 "MemoryAllocationFailed");
2660 }
cristy3ed852e2009-09-05 21:47:34 +00002661 }
2662 else
cristy94c8fe42009-10-06 01:57:36 +00002663 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002664 {
cristybb503372010-05-27 20:51:26 +00002665 size_t
cristy94c8fe42009-10-06 01:57:36 +00002666 depth;
2667
2668 /*
2669 Colormapped TIFF raster.
2670 */
cristy3ed852e2009-09-05 21:47:34 +00002671 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002672 photometric=PHOTOMETRIC_PALETTE;
2673 depth=1;
2674 while ((GetQuantumRange(depth)+1) < image->colors)
2675 depth<<=1;
2676 status=SetQuantumDepth(image,quantum_info,depth);
2677 if (status == MagickFalse)
2678 ThrowWriterException(ResourceLimitError,
2679 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002680 }
2681 }
2682 }
cristyf2687ca2010-06-29 16:32:38 +00002683 switch (image->endian)
2684 {
2685 case LSBEndian:
2686 {
2687 endian=FILLORDER_LSB2MSB;
2688 break;
2689 }
2690 case MSBEndian:
2691 {
2692 endian=FILLORDER_MSB2LSB;
2693 break;
2694 }
2695 case UndefinedEndian:
2696 default:
2697 {
2698 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2699 break;
2700 }
2701 }
2702 lsb_first=1;
2703 image->endian=MSBEndian;
2704 if ((int) (*(char *) &lsb_first) != 0)
2705 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002706 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2707 (photometric != PHOTOMETRIC_MINISWHITE))
2708 {
2709 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002710 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002711 }
cristy0accf6e2009-10-01 13:14:28 +00002712 else
cristydaff8092010-04-22 14:50:53 +00002713 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2714 (photometric != PHOTOMETRIC_MINISWHITE))
2715 {
2716 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002717 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002718 }
cristy62e282b2010-06-29 01:27:13 +00002719 option=GetImageProperty(image,"tiff:fill-order");
2720 if (option != (const char *) NULL)
2721 {
2722 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002723 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002724 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002725 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002726 }
cristyf2687ca2010-06-29 16:32:38 +00002727 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2728 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002729 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2730 if (image->matte != MagickFalse)
2731 {
2732 uint16
2733 extra_samples,
2734 sample_info[1],
2735 samples_per_pixel;
2736
2737 /*
2738 TIFF has a matte channel.
2739 */
2740 extra_samples=1;
2741 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2742 option=GetImageProperty(image,"tiff:alpha");
2743 if ((option != (const char *) NULL) &&
2744 (LocaleCompare(option,"associated") == 0))
2745 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2746 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2747 &samples_per_pixel);
2748 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2749 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2750 &sample_info);
2751 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2752 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2753 }
2754 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2755 switch (quantum_info->format)
2756 {
2757 case FloatingPointQuantumFormat:
2758 {
2759 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2760 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2761 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2762 break;
2763 }
2764 case SignedQuantumFormat:
2765 {
2766 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2767 break;
2768 }
2769 case UnsignedQuantumFormat:
2770 {
2771 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2772 break;
2773 }
2774 default:
2775 break;
2776 }
cristy3ed852e2009-09-05 21:47:34 +00002777 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2778 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2779 if (photometric == PHOTOMETRIC_RGB)
2780 if ((image_info->interlace == PlaneInterlace) ||
2781 (image_info->interlace == PartitionInterlace))
2782 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2783 rows_per_strip=1;
2784 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002785 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2786 1);
cristy3ed852e2009-09-05 21:47:34 +00002787 option=GetImageOption(image_info,"tiff:rows-per-strip");
2788 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002789 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002790 switch (compress_tag)
2791 {
2792 case COMPRESSION_JPEG:
2793 {
2794#if defined(JPEG_SUPPORT)
2795 const char
2796 *sampling_factor;
2797
2798 GeometryInfo
2799 geometry_info;
2800
2801 MagickStatusType
2802 flags;
2803
2804 rows_per_strip+=(16-(rows_per_strip % 16));
2805 if (image->quality != 0)
2806 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2807 if (image_info->quality != UndefinedCompressionQuality)
2808 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2809 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2810 if (image->colorspace == RGBColorspace)
2811 {
cristy949bf5b2010-05-08 02:47:03 +00002812 const char
2813 *value;
2814
cristy3ed852e2009-09-05 21:47:34 +00002815 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2816 sampling_factor=(const char *) NULL;
2817 value=GetImageProperty(image,"jpeg:sampling-factor");
2818 if (value != (char *) NULL)
2819 {
2820 sampling_factor=value;
2821 if (image->debug != MagickFalse)
2822 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2823 " Input sampling-factors=%s",sampling_factor);
2824 }
2825 if (image_info->sampling_factor != (char *) NULL)
2826 sampling_factor=image_info->sampling_factor;
2827 if (sampling_factor != (const char *) NULL)
2828 {
2829 flags=ParseGeometry(sampling_factor,&geometry_info);
2830 if ((flags & SigmaValue) == 0)
2831 geometry_info.sigma=geometry_info.rho;
2832 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2833 geometry_info.rho,(uint16) geometry_info.sigma);
2834 }
2835 }
2836 if (bits_per_sample == 12)
2837 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2838#endif
2839 break;
2840 }
2841 case COMPRESSION_ADOBE_DEFLATE:
2842 {
cristyf6fe0a12010-05-30 00:44:47 +00002843 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002844 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2845 &bits_per_sample);
2846 if (((photometric == PHOTOMETRIC_RGB) ||
2847 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2848 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2849 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
cristyf6fe0a12010-05-30 00:44:47 +00002850 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2851 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00002852 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00002853 break;
2854 }
2855 case COMPRESSION_CCITTFAX3:
2856 {
2857 /*
2858 Byte-aligned EOL.
2859 */
cristyeaedf062010-05-29 22:36:02 +00002860 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002861 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2862 break;
2863 }
2864 case COMPRESSION_CCITTFAX4:
2865 {
cristyeaedf062010-05-29 22:36:02 +00002866 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002867 break;
2868 }
2869 case COMPRESSION_LZW:
2870 {
2871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2872 &bits_per_sample);
2873 if (((photometric == PHOTOMETRIC_RGB) ||
2874 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2875 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2876 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
2877 break;
2878 }
2879 default:
2880 break;
2881 }
2882 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
2883 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2884 {
2885 unsigned short
2886 units;
2887
2888 /*
2889 Set image resolution.
2890 */
2891 units=RESUNIT_NONE;
2892 if (image->units == PixelsPerInchResolution)
2893 units=RESUNIT_INCH;
2894 if (image->units == PixelsPerCentimeterResolution)
2895 units=RESUNIT_CENTIMETER;
2896 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2897 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2898 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2899 if ((image->page.x != 0) || (image->page.y != 0))
2900 {
2901 /*
2902 Set image position.
2903 */
2904 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2905 image->x_resolution);
2906 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2907 image->y_resolution);
2908 }
2909 }
2910 if (image->chromaticity.white_point.x != 0.0)
2911 {
2912 float
2913 chromaticity[6];
2914
2915 /*
2916 Set image chromaticity.
2917 */
2918 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2919 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2920 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2921 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2922 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2923 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2924 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2925 chromaticity[0]=(float) image->chromaticity.white_point.x;
2926 chromaticity[1]=(float) image->chromaticity.white_point.y;
2927 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2928 }
2929 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2930 {
2931 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2932 if (image->scene != 0)
2933 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
2934 GetImageListLength(image));
2935 }
2936 if (image->orientation != UndefinedOrientation)
2937 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
2938 (void) TIFFSetProfiles(tiff,image);
2939 {
2940 uint16
2941 page,
2942 pages;
2943
2944 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00002945 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00002946 if ((image_info->adjoin != MagickFalse) && (pages > 1))
2947 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2948 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
2949 }
2950 (void) TIFFSetProperties(tiff,image);
2951 if (0)
2952 (void) TIFFSetEXIFProperties(tiff,image);
2953 /*
2954 Write image scanlines.
2955 */
2956 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
2957 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2958 pixels=GetQuantumPixels(quantum_info);
2959 tiff_info.scanline=GetQuantumPixels(quantum_info);
2960 switch (photometric)
2961 {
2962 case PHOTOMETRIC_CIELAB:
2963 case PHOTOMETRIC_YCBCR:
2964 case PHOTOMETRIC_RGB:
2965 {
2966 /*
2967 RGB TIFF image.
2968 */
2969 switch (image_info->interlace)
2970 {
2971 case NoInterlace:
2972 default:
2973 {
2974 quantum_type=RGBQuantum;
2975 if (image->matte != MagickFalse)
2976 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00002977 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002978 {
2979 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002980 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002981
2982 p=GetVirtualPixels(image,0,y,image->columns,1,
2983 &image->exception);
2984 if (p == (const PixelPacket *) NULL)
2985 break;
2986 length=ExportQuantumPixels(image,(const CacheView *) NULL,
2987 quantum_info,quantum_type,pixels,&image->exception);
2988 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
2989 break;
2990 if (image->previous == (Image *) NULL)
2991 {
cristy2837bcc2010-08-07 23:57:39 +00002992 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
2993 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00002994 if (status == MagickFalse)
2995 break;
2996 }
2997 }
2998 break;
2999 }
3000 case PlaneInterlace:
3001 case PartitionInterlace:
3002 {
3003 /*
3004 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3005 */
cristybb503372010-05-27 20:51:26 +00003006 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003007 {
3008 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003009 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003010
3011 p=GetVirtualPixels(image,0,y,image->columns,1,
3012 &image->exception);
3013 if (p == (const PixelPacket *) NULL)
3014 break;
3015 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3016 quantum_info,RedQuantum,pixels,&image->exception);
3017 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3018 break;
3019 }
3020 if (image->previous == (Image *) NULL)
3021 {
3022 status=SetImageProgress(image,SaveImageTag,100,400);
3023 if (status == MagickFalse)
3024 break;
3025 }
cristybb503372010-05-27 20:51:26 +00003026 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003027 {
3028 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003029 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003030
3031 p=GetVirtualPixels(image,0,y,image->columns,1,
3032 &image->exception);
3033 if (p == (const PixelPacket *) NULL)
3034 break;
3035 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3036 quantum_info,GreenQuantum,pixels,&image->exception);
3037 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3038 break;
3039 }
3040 if (image->previous == (Image *) NULL)
3041 {
3042 status=SetImageProgress(image,SaveImageTag,200,400);
3043 if (status == MagickFalse)
3044 break;
3045 }
cristybb503372010-05-27 20:51:26 +00003046 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003047 {
3048 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003049 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003050
3051 p=GetVirtualPixels(image,0,y,image->columns,1,
3052 &image->exception);
3053 if (p == (const PixelPacket *) NULL)
3054 break;
3055 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3056 quantum_info,BlueQuantum,pixels,&image->exception);
3057 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3058 break;
3059 }
3060 if (image->previous == (Image *) NULL)
3061 {
3062 status=SetImageProgress(image,SaveImageTag,300,400);
3063 if (status == MagickFalse)
3064 break;
3065 }
3066 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003067 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003068 {
3069 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003070 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003071
3072 p=GetVirtualPixels(image,0,y,image->columns,1,
3073 &image->exception);
3074 if (p == (const PixelPacket *) NULL)
3075 break;
3076 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3077 quantum_info,AlphaQuantum,pixels,&image->exception);
3078 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3079 break;
3080 }
3081 if (image->previous == (Image *) NULL)
3082 {
3083 status=SetImageProgress(image,SaveImageTag,400,400);
3084 if (status == MagickFalse)
3085 break;
3086 }
3087 break;
3088 }
3089 }
3090 break;
3091 }
3092 case PHOTOMETRIC_SEPARATED:
3093 {
3094 /*
3095 CMYK TIFF image.
3096 */
3097 quantum_type=CMYKQuantum;
3098 if (image->matte != MagickFalse)
3099 quantum_type=CMYKAQuantum;
3100 if (image->colorspace != CMYKColorspace)
3101 (void) TransformImageColorspace(image,CMYKColorspace);
cristybb503372010-05-27 20:51:26 +00003102 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003103 {
3104 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003105 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003106
3107 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3108 if (p == (const PixelPacket *) NULL)
3109 break;
3110 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3111 quantum_info,quantum_type,pixels,&image->exception);
3112 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3113 break;
3114 if (image->previous == (Image *) NULL)
3115 {
cristycee97112010-05-28 00:44:52 +00003116 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3117 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003118 if (status == MagickFalse)
3119 break;
3120 }
3121 }
3122 break;
3123 }
3124 case PHOTOMETRIC_PALETTE:
3125 {
3126 uint16
3127 *blue,
3128 *green,
3129 *red;
3130
3131 /*
3132 Colormapped TIFF image.
3133 */
3134 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3135 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3136 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3137 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3138 (blue == (uint16 *) NULL))
3139 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3140 /*
3141 Initialize TIFF colormap.
3142 */
3143 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3144 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3145 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003146 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003147 {
3148 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3149 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3150 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3151 }
3152 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3153 red=(uint16 *) RelinquishMagickMemory(red);
3154 green=(uint16 *) RelinquishMagickMemory(green);
3155 blue=(uint16 *) RelinquishMagickMemory(blue);
3156 }
3157 default:
3158 {
3159 /*
3160 Convert PseudoClass packets to contiguous grayscale scanlines.
3161 */
3162 quantum_type=IndexQuantum;
3163 if (image->matte != MagickFalse)
3164 {
3165 if (photometric != PHOTOMETRIC_PALETTE)
3166 quantum_type=GrayAlphaQuantum;
3167 else
3168 quantum_type=IndexAlphaQuantum;
3169 }
3170 else
3171 if (photometric != PHOTOMETRIC_PALETTE)
3172 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003173 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003174 {
3175 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003176 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003177
3178 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3179 if (p == (const PixelPacket *) NULL)
3180 break;
3181 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3182 quantum_info,quantum_type,pixels,&image->exception);
3183 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3184 break;
3185 if (image->previous == (Image *) NULL)
3186 {
cristycee97112010-05-28 00:44:52 +00003187 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3188 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003189 if (status == MagickFalse)
3190 break;
3191 }
3192 }
3193 break;
3194 }
3195 }
3196 quantum_info=DestroyQuantumInfo(quantum_info);
3197 DestroyTIFFInfo(&tiff_info);
3198 if (0 && (image_info->verbose == MagickTrue))
3199 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3200 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003201 image->endian=MSBEndian;
3202 if (endian == FILLORDER_LSB2MSB)
3203 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003204 image=SyncNextImageInList(image);
3205 if (image == (Image *) NULL)
3206 break;
3207 status=SetImageProgress(image,SaveImagesTag,scene++,
3208 GetImageListLength(image));
3209 if (status == MagickFalse)
3210 break;
3211 } while (image_info->adjoin != MagickFalse);
3212 (void) TIFFSetWarningHandler(warning_handler);
3213 (void) TIFFSetErrorHandler(error_handler);
3214 TIFFClose(tiff);
3215 return(MagickTrue);
3216}
3217#endif