blob: 2271695c77e4cfd0eb77d5802114366f6df5739c [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% TTTTT IIIII FFFFF FFFFF %
7% T I F F %
8% T I FFF FFF %
9% T I F F %
10% T IIIII F F %
11% %
12% %
13% Read/Write TIFF Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "magick/studio.h"
cristy5a2ca482009-10-14 18:24:56 +000043#include "magick/attribute.h"
cristy3ed852e2009-09-05 21:47:34 +000044#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/cache.h"
47#include "magick/color.h"
48#include "magick/color-private.h"
cristyf4cf3072010-04-25 20:07:02 +000049#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000050#include "magick/colorspace.h"
51#include "magick/constitute.h"
52#include "magick/enhance.h"
53#include "magick/exception.h"
54#include "magick/exception-private.h"
55#include "magick/geometry.h"
56#include "magick/image.h"
57#include "magick/image-private.h"
58#include "magick/list.h"
59#include "magick/log.h"
60#include "magick/magick.h"
61#include "magick/memory_.h"
62#include "magick/module.h"
63#include "magick/monitor.h"
64#include "magick/monitor-private.h"
65#include "magick/option.h"
66#include "magick/property.h"
67#include "magick/quantum.h"
68#include "magick/quantum-private.h"
69#include "magick/profile.h"
70#include "magick/resize.h"
cristy3d7f8062009-09-24 20:45:35 +000071#include "magick/resource_.h"
cristy3ed852e2009-09-05 21:47:34 +000072#include "magick/semaphore.h"
73#include "magick/splay-tree.h"
74#include "magick/static.h"
75#include "magick/statistic.h"
76#include "magick/string_.h"
cristyf2f27272009-12-17 14:48:46 +000077#include "magick/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000078#include "magick/thread_.h"
79#include "magick/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000080#if defined(MAGICKCORE_TIFF_DELEGATE)
81# if defined(MAGICKCORE_HAVE_TIFFCONF_H)
82# include "tiffconf.h"
cristyef8f26b2010-12-19 20:29:16 +000083# endif
cristy3ed852e2009-09-05 21:47:34 +000084# include "tiff.h"
85# include "tiffio.h"
86# if !defined(COMPRESSION_ADOBE_DEFLATE)
87# define COMPRESSION_ADOBE_DEFLATE 8
88# endif
cristyef8f26b2010-12-19 20:29:16 +000089# if !defined(PREDICTOR_HORIZONTAL)
90# define PREDICTOR_HORIZONTAL 2
91# endif
cristy3ed852e2009-09-05 21:47:34 +000092
93/*
94 Typedef declarations.
95*/
96#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
97typedef struct _ExifInfo
98{
99 unsigned int
100 tag,
101 type;
102
103 const char
104 *property;
105} ExifInfo;
106
107static const ExifInfo
108 exif_info[] = {
109 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
110 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
111 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
112 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000113 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy3ed852e2009-09-05 21:47:34 +0000114 { EXIFTAG_OECF, TIFF_UNDEFINED, "exif:OptoelectricConversionFactor" },
115 { EXIFTAG_EXIFVERSION, TIFF_UNDEFINED, "exif:ExifVersion" },
116 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
117 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
118 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_UNDEFINED, "exif:ComponentsConfiguration" },
119 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
120 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
121 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
122 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
123 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
124 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
125 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
126 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
127 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
128 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
129 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
130 /* { EXIFTAG_SUBJECTAREA, TIFF_SHORT, "exif:SubjectArea" }, */
131 { EXIFTAG_MAKERNOTE, TIFF_UNDEFINED, "exif:MakerNote" },
132 { EXIFTAG_USERCOMMENT, TIFF_UNDEFINED, "exif:UserComment" },
133 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
134 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
135 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
136 { EXIFTAG_FLASHPIXVERSION, TIFF_UNDEFINED, "exif:FlashpixVersion" },
137 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
138 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
139 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
140 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
141 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
142 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
143 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_UNDEFINED, "exif:SpatialFrequencyResponse" },
144 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
145 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
146 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
147 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
148 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
149 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
150 { EXIFTAG_FILESOURCE, TIFF_UNDEFINED, "exif:FileSource" },
151 { EXIFTAG_SCENETYPE, TIFF_UNDEFINED, "exif:SceneType" },
152 { EXIFTAG_CFAPATTERN, TIFF_UNDEFINED, "exif:CFAPattern" },
153 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
154 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
155 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
156 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
157 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
158 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
159 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
160 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
161 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
162 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
163 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_UNDEFINED, "exif:DeviceSettingDescription" },
164 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
165 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
166 { 0, 0, (char *) NULL }
167};
168#endif
169
170/*
171 Global declarations.
172*/
173static MagickThreadKey
174 tiff_exception;
175
176static SemaphoreInfo
177 *tiff_semaphore = (SemaphoreInfo *) NULL;
178
179static volatile MagickBooleanType
180 instantiate_key = MagickFalse;
181#endif
182
183/*
184 Forward declarations.
185*/
186#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000187static Image *
188 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
189
cristy3ed852e2009-09-05 21:47:34 +0000190static MagickBooleanType
cristy3d7f8062009-09-24 20:45:35 +0000191 WriteGROUP4Image(const ImageInfo *,Image *),
cristy3ed852e2009-09-05 21:47:34 +0000192 WritePTIFImage(const ImageInfo *,Image *),
193 WriteTIFFImage(const ImageInfo *,Image *);
194#endif
195
196/*
197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198% %
199% %
200% %
201% I s T I F F %
202% %
203% %
204% %
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206%
207% IsTIFF() returns MagickTrue if the image format type, identified by the
208% magick string, is TIFF.
209%
210% The format of the IsTIFF method is:
211%
212% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
213%
214% A description of each parameter follows:
215%
216% o magick: compare image format pattern against these bytes.
217%
218% o length: Specifies the length of the magick string.
219%
220*/
221static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
222{
223 if (length < 4)
224 return(MagickFalse);
225 if (memcmp(magick,"\115\115\000\052",4) == 0)
226 return(MagickTrue);
227 if (memcmp(magick,"\111\111\052\000",4) == 0)
228 return(MagickTrue);
229#if defined(TIFF_VERSION_BIG)
230 if (length < 8)
231 return(MagickFalse);
232 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
233 return(MagickTrue);
234 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
235 return(MagickTrue);
236#endif
237 return(MagickFalse);
238}
239
240#if defined(MAGICKCORE_TIFF_DELEGATE)
241/*
242%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243% %
244% %
245% %
cristy3d7f8062009-09-24 20:45:35 +0000246% R e a d G R O U P 4 I m a g e %
247% %
248% %
249% %
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251%
252% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
253% allocates the memory necessary for the new Image structure and returns a
254% pointer to the new image.
255%
256% The format of the ReadGROUP4Image method is:
257%
258% Image *ReadGROUP4Image(const ImageInfo *image_info,
259% ExceptionInfo *exception)
260%
261% A description of each parameter follows:
262%
263% o image_info: the image info.
264%
265% o exception: return any errors or warnings in this structure.
266%
267*/
268
cristyeaedf062010-05-29 22:36:02 +0000269static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000270{
271 unsigned char
272 buffer[4];
273
274 buffer[0]=(unsigned char) value;
275 buffer[1]=(unsigned char) (value >> 8);
276 buffer[2]=(unsigned char) (value >> 16);
277 buffer[3]=(unsigned char) (value >> 24);
278 return(fwrite(buffer,1,4,file));
279}
280
281static Image *ReadGROUP4Image(const ImageInfo *image_info,
282 ExceptionInfo *exception)
283{
284 char
285 filename[MaxTextExtent];
286
287 FILE
288 *file;
289
290 Image
291 *image;
292
293 ImageInfo
294 *read_info;
295
296 int
297 c,
298 unique_file;
299
300 MagickBooleanType
301 status;
302
303 size_t
304 length;
305
306 ssize_t
307 offset,
308 strip_offset;
309
310 /*
311 Open image file.
312 */
313 assert(image_info != (const ImageInfo *) NULL);
314 assert(image_info->signature == MagickSignature);
315 if (image_info->debug != MagickFalse)
316 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
317 image_info->filename);
318 assert(exception != (ExceptionInfo *) NULL);
319 assert(exception->signature == MagickSignature);
320 image=AcquireImage(image_info);
321 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
322 if (status == MagickFalse)
323 {
324 image=DestroyImageList(image);
325 return((Image *) NULL);
326 }
327 /*
328 Write raw CCITT Group 4 wrapped as a TIFF image file.
329 */
330 file=(FILE *) NULL;
331 unique_file=AcquireUniqueFileResource(filename);
332 if (unique_file != -1)
333 file=fdopen(unique_file,"wb");
334 if ((unique_file == -1) || (file == (FILE *) NULL))
335 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
336 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
337 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
338 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
339 length=WriteLSBLong(file,image->columns);
340 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
341 length=WriteLSBLong(file,image->rows);
342 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
343 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
344 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
345 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
346 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000347 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000348 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000349 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000350 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
351 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000352 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000353 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
354 offset=(ssize_t) ftell(file)-4;
355 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000356 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000357 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000358 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000359 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
360 length=fwrite("\000\000\000\000",1,4,file);
361 length=WriteLSBLong(file,image->x_resolution);
362 length=WriteLSBLong(file,1);
363 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
364 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000365 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000366 length=WriteLSBLong(file,(unsigned int) length);
367 (void) fclose(file);
368 (void) CloseBlob(image);
369 image=DestroyImage(image);
370 /*
371 Read TIFF image.
372 */
cristy14efd992009-09-26 23:46:03 +0000373 read_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +0000374 (void) FormatMagickString(read_info->filename,MaxTextExtent,"%.1024s",
375 filename);
376 image=ReadTIFFImage(read_info,exception);
377 read_info=DestroyImageInfo(read_info);
378 if (image != (Image *) NULL)
379 {
380 (void) CopyMagickString(image->filename,image_info->filename,
381 MaxTextExtent);
382 (void) CopyMagickString(image->magick_filename,image_info->filename,
383 MaxTextExtent);
384 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
385 }
386 (void) RelinquishUniqueFileResource(filename);
387 return(image);
388}
389#endif
390
391#if defined(MAGICKCORE_TIFF_DELEGATE)
392/*
393%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394% %
395% %
396% %
cristy3ed852e2009-09-05 21:47:34 +0000397% R e a d T I F F I m a g e %
398% %
399% %
400% %
401%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402%
403% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
404% memory necessary for the new Image structure and returns a pointer to the
405% new image.
406%
407% The format of the ReadTIFFImage method is:
408%
409% Image *ReadTIFFImage(const ImageInfo *image_info,
410% ExceptionInfo *exception)
411%
412% A description of each parameter follows:
413%
414% o image_info: the image info.
415%
416% o exception: return any errors or warnings in this structure.
417%
418*/
419
420static inline size_t MagickMax(const size_t x,const size_t y)
421{
422 if (x > y)
423 return(x);
424 return(y);
425}
426
cristybb503372010-05-27 20:51:26 +0000427static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000428{
429 if (x < y)
430 return(x);
431 return(y);
432}
433
434static MagickBooleanType ReadProfile(Image *image,const char *name,
cristybb503372010-05-27 20:51:26 +0000435 unsigned char *datum,ssize_t length)
cristy3ed852e2009-09-05 21:47:34 +0000436{
437 MagickBooleanType
438 status;
439
cristybb503372010-05-27 20:51:26 +0000440 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000441 i;
442
443 StringInfo
444 *profile;
445
446 if (length < 4)
447 return(MagickFalse);
448 i=0;
449 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
450 {
451 for (i=0; i < (length-4); i+=2)
452 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
453 break;
454 if (i == length)
455 length-=i;
456 else
457 i=0;
458 if (length < 4)
459 return(MagickFalse);
460 }
461 profile=AcquireStringInfo((size_t) length);
462 SetStringInfoDatum(profile,datum+i);
463 status=SetImageProfile(image,name,profile);
464 profile=DestroyStringInfo(profile);
465 if (status == MagickFalse)
466 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
467 image->filename);
468 return(MagickTrue);
469}
470
471#if defined(__cplusplus) || defined(c_plusplus)
472extern "C" {
473#endif
474
475static int TIFFCloseBlob(thandle_t image)
476{
477 (void) CloseBlob((Image *) image);
478 return(0);
479}
480
481static void TIFFErrors(const char *module,const char *format,va_list error)
482{
483 char
484 message[MaxTextExtent];
485
486 ExceptionInfo
487 *exception;
488
489#if defined(MAGICKCORE_HAVE_VSNPRINTF)
490 (void) vsnprintf(message,MaxTextExtent,format,error);
491#else
492 (void) vsprintf(message,format,error);
493#endif
494 (void) ConcatenateMagickString(message,".",MaxTextExtent);
495 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
496 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000497 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
498 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000499}
500
501static void TIFFGetProfiles(TIFF *tiff,Image *image)
502{
503 uint32
504 length;
505
506 unsigned char
507 *profile;
508
509#if defined(TIFFTAG_ICCPROFILE)
510 length=0;
511 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000512 (void) ReadProfile(image,"icc",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000513#endif
514#if defined(TIFFTAG_PHOTOSHOP)
515 length=0;
516 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000517 (void) ReadProfile(image,"8bim",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000518#endif
519#if defined(TIFFTAG_RICHTIFFIPTC)
520 length=0;
521 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
522 {
523 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000524 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000525 (void) ReadProfile(image,"iptc",profile,4L*length);
526 }
527#endif
528#if defined(TIFFTAG_XMLPACKET)
529 length=0;
530 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000531 (void) ReadProfile(image,"xmp",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000532#endif
533 length=0;
534 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000535 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000536}
537
538static void TIFFGetProperties(TIFF *tiff,Image *image)
539{
540 char
541 *text;
542
543 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
544 (void) SetImageProperty(image,"tiff:artist",text);
545 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
546 (void) SetImageProperty(image,"tiff:timestamp",text);
547 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
548 (void) SetImageProperty(image,"tiff:software",text);
549 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
550 (void) SetImageProperty(image,"tiff:hostcomputer",text);
551 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
552 (void) SetImageProperty(image,"tiff:document",text);
553 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
554 (void) SetImageProperty(image,"tiff:make",text);
555 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
556 (void) SetImageProperty(image,"tiff:model",text);
557 if (TIFFGetField(tiff,33432,&text) == 1)
558 (void) SetImageProperty(image,"tiff:copyright",text);
559 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
560 (void) SetImageProperty(image,"label",text);
561 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
562 (void) SetImageProperty(image,"comment",text);
563}
564
565static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
566{
567#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
568 char
569 value[MaxTextExtent];
570
cristybb503372010-05-27 20:51:26 +0000571 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000572 i;
573
574 tdir_t
575 directory;
576
577 uint32
578 offset;
579
580 /*
581 Read EXIF properties.
582 */
583 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
584 return;
585 directory=TIFFCurrentDirectory(tiff);
586 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
587 return;
588 for (i=0; exif_info[i].tag != 0; i++)
589 {
590 *value='\0';
591 switch (exif_info[i].type)
592 {
593 case TIFF_ASCII:
594 {
595 char
596 *ascii;
597
598 if (TIFFGetField(tiff,exif_info[i].tag,&ascii) != 0)
599 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
600 break;
601 }
602 case TIFF_SHORT:
603 {
604 uint16
605 shorty;
606
607 if (TIFFGetField(tiff,exif_info[i].tag,&shorty) != 0)
608 (void) FormatMagickString(value,MaxTextExtent,"%d",shorty);
609 break;
610 }
611 case TIFF_LONG:
612 {
613 uint32
cristybb503372010-05-27 20:51:26 +0000614 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +0000615
cristybb503372010-05-27 20:51:26 +0000616 if (TIFFGetField(tiff,exif_info[i].tag,&ssize_ty) != 0)
617 (void) FormatMagickString(value,MaxTextExtent,"%d",ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +0000618 break;
619 }
620 case TIFF_RATIONAL:
621 case TIFF_SRATIONAL:
622 {
623 float
624 rational;
625
626 if (TIFFGetField(tiff,exif_info[i].tag,&rational) != 0)
cristye7f51092010-01-17 00:39:37 +0000627 (void) FormatMagickString(value,MaxTextExtent,"%g",rational);
cristy3ed852e2009-09-05 21:47:34 +0000628 break;
629 }
630 default:
631 break;
632 }
633 if (*value != '\0')
634 (void) SetImageProperty(image,exif_info[i].property,value);
635 }
636 TIFFSetDirectory(tiff,directory);
637#else
638 (void) tiff;
639 (void) image;
640#endif
641}
642
643static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
644{
645 *base=(tdata_t *) GetBlobStreamData((Image *) image);
646 if (*base != (tdata_t *) NULL)
647 *size=(toff_t) GetBlobSize((Image *) image);
648 if (*base != (tdata_t *) NULL)
649 return(1);
650 return(0);
651}
652
653static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
654{
655 tsize_t
656 count;
657
658 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
659 (unsigned char *) data);
660 return(count);
661}
662
cristybb503372010-05-27 20:51:26 +0000663static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
664 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000665{
666 int32
667 status;
668
669 (void) bits_per_sample;
670 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
671 return(status);
672}
673
674static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
675{
676 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
677}
678
679static toff_t TIFFGetBlobSize(thandle_t image)
680{
681 return((toff_t) GetBlobSize((Image *) image));
682}
683
684static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
685{
686 (void) image;
687 (void) base;
688 (void) size;
689}
690
691static void TIFFWarnings(const char *module,const char *format,va_list warning)
692{
693 char
694 message[MaxTextExtent];
695
696 ExceptionInfo
697 *exception;
698
699#if defined(MAGICKCORE_HAVE_VSNPRINTF)
700 (void) vsnprintf(message,MaxTextExtent,format,warning);
701#else
702 (void) vsprintf(message,format,warning);
703#endif
704 (void) ConcatenateMagickString(message,".",MaxTextExtent);
705 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
706 if (exception != (ExceptionInfo *) NULL)
707 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
708 message,"`%s'",module);
709}
710
711static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
712{
713 tsize_t
714 count;
715
716 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
717 (unsigned char *) data);
718 return(count);
719}
720
721#if defined(__cplusplus) || defined(c_plusplus)
722}
723#endif
724
725static Image *ReadTIFFImage(const ImageInfo *image_info,
726 ExceptionInfo *exception)
727{
728 typedef enum
729 {
730 ReadSingleSampleMethod,
731 ReadRGBAMethod,
732 ReadCMYKAMethod,
733 ReadStripMethod,
734 ReadTileMethod,
735 ReadGenericMethod
736 } TIFFMethodType;
737
738 const char
739 *option;
740
741 float
742 *chromaticity,
743 x_position,
744 y_position,
745 x_resolution,
746 y_resolution;
747
748 Image
749 *image;
750
cristybb503372010-05-27 20:51:26 +0000751 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000752 y;
753
754 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000755 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000756 debug,
757 status;
758
759 MagickSizeType
760 number_pixels;
761
762 QuantumInfo
763 *quantum_info;
764
765 QuantumType
766 quantum_type;
767
cristybb503372010-05-27 20:51:26 +0000768 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000769 i;
770
771 size_t
772 length,
773 pad;
774
775 TIFF
776 *tiff;
777
778 TIFFErrorHandler
779 error_handler,
780 warning_handler;
781
782 TIFFMethodType
783 method;
784
785 uint16
786 compress_tag,
787 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000788 endian,
cristy3ed852e2009-09-05 21:47:34 +0000789 extra_samples,
790 interlace,
791 max_sample_value,
792 min_sample_value,
793 orientation,
794 pages,
795 photometric,
796 *sample_info,
797 sample_format,
798 samples_per_pixel,
799 units,
800 value;
801
802 uint32
803 height,
804 rows_per_strip,
805 width;
806
807 unsigned char
808 *pixels;
809
cristyf2687ca2010-06-29 16:32:38 +0000810 size_t
811 lsb_first;
812
cristy3ed852e2009-09-05 21:47:34 +0000813 /*
814 Open image.
815 */
816 assert(image_info != (const ImageInfo *) NULL);
817 assert(image_info->signature == MagickSignature);
818 if (image_info->debug != MagickFalse)
819 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
820 image_info->filename);
821 assert(exception != (ExceptionInfo *) NULL);
822 assert(exception->signature == MagickSignature);
823 image=AcquireImage(image_info);
824 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
825 if (status == MagickFalse)
826 {
827 image=DestroyImageList(image);
828 return((Image *) NULL);
829 }
830 (void) MagickSetThreadValue(tiff_exception,exception);
831 error_handler=TIFFSetErrorHandler(TIFFErrors);
832 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
833 tiff=TIFFClientOpen(image->filename,"r",(thandle_t) image,TIFFReadBlob,
834 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
835 TIFFUnmapBlob);
836 if (tiff == (TIFF *) NULL)
837 {
838 (void) TIFFSetWarningHandler(warning_handler);
839 (void) TIFFSetErrorHandler(error_handler);
840 image=DestroyImageList(image);
841 return((Image *) NULL);
842 }
843 debug=IsEventLogging();
844 if (image_info->number_scenes != 0)
845 {
846 /*
847 Generate blank images for subimage specification (e.g. image.tif[4].
848 */
cristybb503372010-05-27 20:51:26 +0000849 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000850 {
851 (void) TIFFReadDirectory(tiff);
852 AcquireNextImage(image_info,image);
853 if (GetNextImageInList(image) == (Image *) NULL)
854 {
855 image=DestroyImageList(image);
856 return((Image *) NULL);
857 }
858 image=SyncNextImageInList(image);
859 }
860 }
861 do
862 {
863 if (0 && (image_info->verbose != MagickFalse))
864 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristy6b032822010-06-29 16:52:32 +0000865 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
866 "lsb" : "msb");
cristy3ed852e2009-09-05 21:47:34 +0000867 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000872 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
873 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
874 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
875 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
876 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
877 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
878 switch (photometric)
879 {
880 case PHOTOMETRIC_MINISBLACK:
881 {
882 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
883 break;
884 }
885 case PHOTOMETRIC_MINISWHITE:
886 {
887 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
888 break;
889 }
890 case PHOTOMETRIC_PALETTE:
891 {
892 (void) SetImageProperty(image,"tiff:photometric","palette");
893 break;
894 }
895 case PHOTOMETRIC_RGB:
896 {
897 (void) SetImageProperty(image,"tiff:photometric","RGB");
898 break;
899 }
900 case PHOTOMETRIC_CIELAB:
901 {
902 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
903 break;
904 }
905 case PHOTOMETRIC_SEPARATED:
906 {
907 (void) SetImageProperty(image,"tiff:photometric","separated");
908 break;
909 }
cristy79e5dad2010-09-16 19:48:33 +0000910 case PHOTOMETRIC_YCBCR:
911 {
912 (void) SetImageProperty(image,"tiff:photometric","YCBCR");
913 break;
914 }
cristy3ed852e2009-09-05 21:47:34 +0000915 default:
916 {
917 (void) SetImageProperty(image,"tiff:photometric","unknown");
918 break;
919 }
920 }
921 if (image->debug != MagickFalse)
922 {
923 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
924 (unsigned int) width,(unsigned int) height);
925 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
926 interlace);
927 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
928 "Bits per sample: %u",bits_per_sample);
929 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
930 "Min sample value: %u",min_sample_value);
931 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
932 "Max sample value: %u",max_sample_value);
933 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
934 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
935 }
cristybb503372010-05-27 20:51:26 +0000936 image->columns=(size_t) width;
937 image->rows=(size_t) height;
938 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +0000939 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +0000940 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
941 (double) image->depth);
cristyf2687ca2010-06-29 16:32:38 +0000942 lsb_first=1;
943 image->endian=MSBEndian;
944 if ((int) (*(char *) &lsb_first) != 0)
945 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +0000946 if (photometric == PHOTOMETRIC_SEPARATED)
947 image->colorspace=CMYKColorspace;
948 if (photometric == PHOTOMETRIC_CIELAB)
949 image->colorspace=LabColorspace;
950 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
951 &samples_per_pixel);
952 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
953 x_resolution=(float) image->x_resolution;
954 y_resolution=(float) image->y_resolution;
955 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
956 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
957 image->x_resolution=x_resolution;
958 image->y_resolution=y_resolution;
959 x_position=(float) image->page.x/x_resolution;
960 y_position=(float) image->page.y/y_resolution;
961 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
962 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +0000963 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
964 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000965 image->orientation=(OrientationType) orientation;
966 chromaticity=(float *) NULL;
967 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
968 if (chromaticity != (float *) NULL)
969 {
970 image->chromaticity.white_point.x=chromaticity[0];
971 image->chromaticity.white_point.y=chromaticity[1];
972 }
973 chromaticity=(float *) NULL;
974 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
975 if (chromaticity != (float *) NULL)
976 {
977 image->chromaticity.red_primary.x=chromaticity[0];
978 image->chromaticity.red_primary.y=chromaticity[1];
979 image->chromaticity.green_primary.x=chromaticity[2];
980 image->chromaticity.green_primary.y=chromaticity[3];
981 image->chromaticity.blue_primary.x=chromaticity[4];
982 image->chromaticity.blue_primary.y=chromaticity[5];
983 }
984 TIFFGetProperties(tiff,image);
985 option=GetImageOption(image_info,"tiff:exif");
986 if ((option != (const char *) NULL) &&
987 (IsMagickTrue(option) != MagickFalse))
988 TIFFGetEXIFProperties(tiff,image);
989 TIFFGetProfiles(tiff,image);
990 /*
991 Allocate memory for the image and pixel buffer.
992 */
993#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
994 if ((compress_tag != COMPRESSION_NONE) &&
995 (TIFFIsCODECConfigured(compress_tag) == 0))
996 {
997 TIFFClose(tiff);
998 ThrowReaderException(CoderError,"CompressNotSupported");
999 }
1000#endif
1001 switch (compress_tag)
1002 {
1003 case COMPRESSION_NONE: image->compression=NoCompression; break;
1004 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1005 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1006 case COMPRESSION_JPEG:
1007 {
1008 image->compression=JPEGCompression;
1009#if defined(JPEG_SUPPORT)
1010 {
1011 char
1012 sampling_factor[MaxTextExtent];
1013
1014 uint16
1015 horizontal,
1016 vertical;
1017
1018 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1019 &horizontal,&vertical);
1020 (void) FormatMagickString(sampling_factor,MaxTextExtent,"%dx%d",
1021 horizontal,vertical);
1022 (void) SetImageProperty(image,"jpeg:sampling-factor",
1023 sampling_factor);
1024 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1025 "Sampling Factors: %s",sampling_factor);
1026 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1027 {
1028 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1029 JPEGCOLORMODE_RGB);
1030 photometric=PHOTOMETRIC_RGB;
1031 }
1032 }
1033#endif
1034 break;
1035 }
1036 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001037#if defined(COMPRESSION_LZMA)
1038 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1039#endif
cristy3ed852e2009-09-05 21:47:34 +00001040 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1041 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1042 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1043 default: image->compression=RLECompression; break;
1044 }
1045 quantum_info=AcquireQuantumInfo(image_info,image);
1046 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001047 {
1048 TIFFClose(tiff);
1049 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1050 }
cristy3ed852e2009-09-05 21:47:34 +00001051 if (sample_format == SAMPLEFORMAT_UINT)
1052 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1053 if (sample_format == SAMPLEFORMAT_INT)
1054 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1055 if (sample_format == SAMPLEFORMAT_IEEEFP)
1056 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1057 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001058 {
1059 TIFFClose(tiff);
1060 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1061 }
cristy3ed852e2009-09-05 21:47:34 +00001062 status=MagickTrue;
1063 switch (photometric)
1064 {
1065 case PHOTOMETRIC_MINISBLACK:
1066 {
1067 quantum_info->min_is_white=MagickFalse;
1068 break;
1069 }
1070 case PHOTOMETRIC_MINISWHITE:
1071 {
1072 quantum_info->min_is_white=MagickTrue;
1073 break;
1074 }
1075 default:
1076 break;
1077 }
cristy891dc792010-03-04 01:47:16 +00001078 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001079 extra_samples=0;
1080 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1081 &sample_info);
1082 if (extra_samples == 0)
1083 {
1084 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1085 image->matte=MagickTrue;
1086 }
1087 else
cristy6c207342010-02-19 17:49:50 +00001088 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001089 {
cristy7cf69862010-03-03 13:57:24 +00001090 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001091 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001092 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001093 }
cristy891dc792010-03-04 01:47:16 +00001094 option=GetImageOption(image_info,"tiff:alpha");
1095 if (option != (const char *) NULL)
1096 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1097 MagickFalse;
1098 if (image->matte != MagickFalse)
1099 (void) SetImageProperty(image,"tiff:alpha",
1100 associated_alpha != MagickFalse ? "associated" : "unassociated");
cristy3ed852e2009-09-05 21:47:34 +00001101 if ((photometric == PHOTOMETRIC_PALETTE) &&
1102 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1103 {
cristybb503372010-05-27 20:51:26 +00001104 size_t
cristy3ed852e2009-09-05 21:47:34 +00001105 colors;
1106
cristybb503372010-05-27 20:51:26 +00001107 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy3ed852e2009-09-05 21:47:34 +00001108 if (AcquireImageColormap(image,colors) == MagickFalse)
1109 {
1110 TIFFClose(tiff);
1111 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1112 }
1113 }
1114 if (units == RESUNIT_INCH)
1115 image->units=PixelsPerInchResolution;
1116 if (units == RESUNIT_CENTIMETER)
1117 image->units=PixelsPerCentimeterResolution;
1118 value=(unsigned short) image->scene;
1119 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1120 image->scene=value;
1121 if (image_info->ping != MagickFalse)
1122 {
1123 if (image_info->number_scenes != 0)
1124 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1125 break;
1126 goto next_tiff_frame;
1127 }
1128 method=ReadGenericMethod;
1129 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1130 {
1131 char
1132 value[MaxTextExtent];
1133
1134 method=ReadStripMethod;
1135 (void) FormatMagickString(value,MaxTextExtent,"%u",
1136 (unsigned int) rows_per_strip);
1137 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1138 }
1139 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1140 method=ReadRGBAMethod;
1141 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1142 method=ReadCMYKAMethod;
1143 if ((photometric != PHOTOMETRIC_RGB) &&
1144 (photometric != PHOTOMETRIC_CIELAB) &&
1145 (photometric != PHOTOMETRIC_SEPARATED))
1146 method=ReadGenericMethod;
1147 if (image->storage_class == PseudoClass)
1148 method=ReadSingleSampleMethod;
1149 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1150 (photometric == PHOTOMETRIC_MINISWHITE))
1151 method=ReadSingleSampleMethod;
1152 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1153 (interlace == PLANARCONFIG_SEPARATE))
1154 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001155 if (image->compression == JPEGCompression)
1156 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001157 if (TIFFIsTiled(tiff) != MagickFalse)
1158 method=ReadTileMethod;
1159 quantum_type=RGBQuantum;
1160 pixels=GetQuantumPixels(quantum_info);
1161 switch (method)
1162 {
1163 case ReadSingleSampleMethod:
1164 {
1165 /*
1166 Convert TIFF image to PseudoClass MIFF image.
1167 */
1168 if ((image->storage_class == PseudoClass) &&
1169 (photometric == PHOTOMETRIC_PALETTE))
1170 {
cristybb503372010-05-27 20:51:26 +00001171 size_t
cristy3ed852e2009-09-05 21:47:34 +00001172 range;
1173
1174 uint16
1175 *blue_colormap,
1176 *green_colormap,
1177 *red_colormap;
1178
1179 /*
1180 Initialize colormap.
1181 */
1182 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1183 &green_colormap,&blue_colormap);
1184 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001185 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001186 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1187 (blue_colormap[i] >= 256))
1188 {
1189 range=65535;
1190 break;
1191 }
cristybb503372010-05-27 20:51:26 +00001192 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001193 {
cristyce70c172010-01-07 17:15:30 +00001194 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001195 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001196 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001197 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001198 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001199 blue_colormap[i])/range);
1200 }
1201 }
1202 quantum_type=IndexQuantum;
1203 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1204 if (image->matte != MagickFalse)
1205 {
1206 if (image->storage_class != PseudoClass)
1207 {
1208 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1209 GrayAlphaQuantum;
1210 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1211 }
1212 else
1213 {
1214 quantum_type=IndexAlphaQuantum;
1215 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1216 }
1217 }
1218 else
1219 if (image->storage_class != PseudoClass)
1220 {
1221 quantum_type=GrayQuantum;
1222 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1223 }
1224 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1225 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001226 {
1227 TIFFClose(tiff);
1228 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1229 }
cristy3ed852e2009-09-05 21:47:34 +00001230 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001231 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001232 {
1233 int
1234 status;
1235
1236 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001237 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001238
1239 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1240 if (status == -1)
1241 break;
1242 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1243 if (q == (PixelPacket *) NULL)
1244 break;
1245 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1246 quantum_type,pixels,exception);
1247 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1248 break;
1249 if (image->previous == (Image *) NULL)
1250 {
cristycee97112010-05-28 00:44:52 +00001251 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1252 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001253 if (status == MagickFalse)
1254 break;
1255 }
1256 }
1257 break;
1258 }
1259 case ReadRGBAMethod:
1260 {
1261 /*
1262 Convert TIFF image to DirectClass MIFF image.
1263 */
1264 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1265 quantum_type=RGBQuantum;
1266 if (image->matte != MagickFalse)
1267 {
1268 quantum_type=RGBAQuantum;
1269 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1270 }
1271 if (image->colorspace == CMYKColorspace)
1272 {
1273 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1274 quantum_type=CMYKQuantum;
1275 if (image->matte != MagickFalse)
1276 {
1277 quantum_type=CMYKAQuantum;
1278 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1279 }
1280 }
1281 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1282 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001283 {
1284 TIFFClose(tiff);
1285 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1286 }
cristy3ed852e2009-09-05 21:47:34 +00001287 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001288 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001289 {
1290 int
1291 status;
1292
1293 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001294 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001295
1296 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1297 if (status == -1)
1298 break;
1299 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1300 if (q == (PixelPacket *) NULL)
1301 break;
cristy0b334892010-03-15 02:26:46 +00001302 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1303 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001304 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1305 break;
1306 if (image->previous == (Image *) NULL)
1307 {
cristycee97112010-05-28 00:44:52 +00001308 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1309 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001310 if (status == MagickFalse)
1311 break;
1312 }
1313 }
1314 break;
1315 }
1316 case ReadCMYKAMethod:
1317 {
1318 /*
1319 Convert TIFF image to DirectClass MIFF image.
1320 */
cristybb503372010-05-27 20:51:26 +00001321 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001322 {
cristybb503372010-05-27 20:51:26 +00001323 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001324 {
cristy6d9f12f2009-11-03 14:50:26 +00001325 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001326 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001327
cristy3ed852e2009-09-05 21:47:34 +00001328 int
1329 status;
1330
1331 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1332 pixels);
1333 if (status == -1)
1334 break;
1335 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1336 if (q == (PixelPacket *) NULL)
1337 break;
1338 if (image->colorspace != CMYKColorspace)
1339 switch (i)
1340 {
1341 case 0: quantum_type=RedQuantum; break;
1342 case 1: quantum_type=GreenQuantum; break;
1343 case 2: quantum_type=BlueQuantum; break;
1344 case 3: quantum_type=AlphaQuantum; break;
1345 default: quantum_type=UndefinedQuantum; break;
1346 }
cristy6d9f12f2009-11-03 14:50:26 +00001347 else
cristy3ed852e2009-09-05 21:47:34 +00001348 switch (i)
1349 {
1350 case 0: quantum_type=CyanQuantum; break;
1351 case 1: quantum_type=MagentaQuantum; break;
1352 case 2: quantum_type=YellowQuantum; break;
1353 case 3: quantum_type=BlackQuantum; break;
1354 case 4: quantum_type=AlphaQuantum; break;
1355 default: quantum_type=UndefinedQuantum; break;
1356 }
1357 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1358 quantum_type,pixels,exception);
1359 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1360 break;
1361 }
1362 if (image->previous == (Image *) NULL)
1363 {
cristycee97112010-05-28 00:44:52 +00001364 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1365 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001366 if (status == MagickFalse)
1367 break;
1368 }
1369 }
1370 break;
1371 }
1372 case ReadStripMethod:
1373 {
1374 register unsigned char
1375 *p;
1376
1377 /*
1378 Convert stripped TIFF image to DirectClass MIFF image.
1379 */
1380 i=0;
1381 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001382 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001383 {
cristybb503372010-05-27 20:51:26 +00001384 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001385 x;
1386
1387 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001388 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001389
1390 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1391 if (q == (PixelPacket *) NULL)
1392 break;
1393 if (i == 0)
1394 {
1395 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1396 break;
cristyeaedf062010-05-29 22:36:02 +00001397 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1398 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001399 }
1400 i--;
1401 p=pixels+image->columns*i;
cristybb503372010-05-27 20:51:26 +00001402 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001403 {
1404 q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
1405 q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
1406 q->blue=ScaleCharToQuantum((unsigned char) (TIFFGetB(*p)));
1407 if (image->matte != MagickFalse)
1408 q->opacity=ScaleCharToQuantum((unsigned char) (TIFFGetA(*p)));
1409 p++;
1410 q++;
1411 }
1412 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1413 break;
1414 if (image->previous == (Image *) NULL)
1415 {
cristycee97112010-05-28 00:44:52 +00001416 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1417 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001418 if (status == MagickFalse)
1419 break;
1420 }
1421 }
1422 break;
1423 }
1424 case ReadTileMethod:
1425 {
1426 register uint32
1427 *p;
1428
1429 uint32
1430 *tile_pixels,
1431 columns,
1432 rows;
1433
cristybb503372010-05-27 20:51:26 +00001434 size_t
cristy3ed852e2009-09-05 21:47:34 +00001435 number_pixels;
1436
1437 /*
1438 Convert tiled TIFF image to DirectClass MIFF image.
1439 */
1440 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1441 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1442 {
1443 TIFFClose(tiff);
1444 ThrowReaderException(CoderError,"ImageIsNotTiled");
1445 }
1446 (void) SetImageStorageClass(image,DirectClass);
1447 number_pixels=columns*rows;
1448 tile_pixels=(uint32 *) AcquireQuantumMemory((size_t) columns*rows,
1449 sizeof(*tile_pixels));
1450 if (tile_pixels == (uint32 *) NULL)
1451 {
1452 TIFFClose(tiff);
1453 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1454 }
cristybb503372010-05-27 20:51:26 +00001455 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001456 {
1457 PixelPacket
1458 *tile;
1459
cristybb503372010-05-27 20:51:26 +00001460 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001461 x;
1462
1463 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001464 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001465
cristybb503372010-05-27 20:51:26 +00001466 size_t
cristy3ed852e2009-09-05 21:47:34 +00001467 columns_remaining,
1468 rows_remaining;
1469
1470 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001471 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001472 rows_remaining=rows;
1473 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1474 exception);
1475 if (tile == (PixelPacket *) NULL)
1476 break;
cristybb503372010-05-27 20:51:26 +00001477 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001478 {
cristybb503372010-05-27 20:51:26 +00001479 size_t
cristy3ed852e2009-09-05 21:47:34 +00001480 column,
1481 row;
1482
1483 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1484 break;
1485 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001486 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001487 columns_remaining=columns;
1488 p=tile_pixels+(rows-rows_remaining)*columns;
1489 q=tile+(image->columns*(rows_remaining-1)+x);
1490 for (row=rows_remaining; row > 0; row--)
1491 {
1492 if (image->matte != MagickFalse)
1493 for (column=columns_remaining; column > 0; column--)
1494 {
1495 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1496 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1497 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1498 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1499 (unsigned char) TIFFGetA(*p)));
1500 q++;
1501 p++;
1502 }
1503 else
1504 for (column=columns_remaining; column > 0; column--)
1505 {
1506 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1507 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1508 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1509 q++;
1510 p++;
1511 }
1512 p+=columns-columns_remaining;
1513 q-=(image->columns+columns_remaining);
1514 }
1515 }
1516 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1517 break;
1518 if (image->previous == (Image *) NULL)
1519 {
cristycee97112010-05-28 00:44:52 +00001520 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1521 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001522 if (status == MagickFalse)
1523 break;
1524 }
1525 }
1526 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1527 break;
1528 }
1529 case ReadGenericMethod:
1530 default:
1531 {
1532 register uint32
1533 *p;
1534
1535 uint32
1536 *pixels;
1537
1538 /*
1539 Convert TIFF image to DirectClass MIFF image.
1540 */
1541 number_pixels=(MagickSizeType) image->columns*image->rows;
1542 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1543 (number_pixels*sizeof(uint32))))
1544 {
1545 TIFFClose(tiff);
1546 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1547 }
1548 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1549 sizeof(uint32));
1550 if (pixels == (uint32 *) NULL)
1551 {
1552 TIFFClose(tiff);
1553 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1554 }
1555 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1556 (uint32) image->rows,(uint32 *) pixels,0);
1557 /*
1558 Convert image to DirectClass pixel packets.
1559 */
1560 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001561 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001562 {
cristybb503372010-05-27 20:51:26 +00001563 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001564 x;
1565
1566 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001567 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001568
1569 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1570 if (q == (PixelPacket *) NULL)
1571 break;
1572 q+=image->columns-1;
cristybb503372010-05-27 20:51:26 +00001573 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001574 {
1575 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1576 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1577 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1578 if (image->matte != MagickFalse)
cristycc768f42010-08-19 16:00:09 +00001579 q->opacity=(Quantum) (QuantumRange-
1580 ScaleCharToQuantum((unsigned char) TIFFGetA(*p)));
cristy3ed852e2009-09-05 21:47:34 +00001581 p--;
1582 q--;
1583 }
1584 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1585 break;
1586 if (image->previous == (Image *) NULL)
1587 {
cristycee97112010-05-28 00:44:52 +00001588 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1589 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001590 if (status == MagickFalse)
1591 break;
1592 }
1593 }
1594 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1595 break;
1596 }
1597 }
1598 SetQuantumImageType(image,quantum_type);
1599 next_tiff_frame:
1600 if ((photometric == PHOTOMETRIC_LOGL) ||
1601 (photometric == PHOTOMETRIC_MINISBLACK) ||
1602 (photometric == PHOTOMETRIC_MINISWHITE))
1603 {
1604 image->type=GrayscaleType;
1605 if (bits_per_sample == 1)
1606 image->type=BilevelType;
1607 }
1608 if (image->storage_class == PseudoClass)
1609 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001610 image->endian=MSBEndian;
1611 if (endian == FILLORDER_LSB2MSB)
1612 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001613 if ((photometric == PHOTOMETRIC_LOGL) ||
1614 (photometric == PHOTOMETRIC_MINISBLACK) ||
1615 (photometric == PHOTOMETRIC_MINISWHITE))
1616 {
1617 image->type=GrayscaleType;
1618 if (bits_per_sample == 1)
1619 image->type=BilevelType;
1620 }
1621 /*
1622 Proceed to next image.
1623 */
1624 if (image_info->number_scenes != 0)
1625 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1626 break;
1627 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1628 if (status == MagickTrue)
1629 {
1630 /*
1631 Allocate next image structure.
1632 */
1633 AcquireNextImage(image_info,image);
1634 if (GetNextImageInList(image) == (Image *) NULL)
1635 {
1636 image=DestroyImageList(image);
1637 return((Image *) NULL);
1638 }
1639 image=SyncNextImageInList(image);
1640 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1641 image->scene);
1642 if (status == MagickFalse)
1643 break;
1644 }
1645 quantum_info=DestroyQuantumInfo(quantum_info);
1646 } while (status == MagickTrue);
1647 (void) TIFFSetWarningHandler(warning_handler);
1648 (void) TIFFSetErrorHandler(error_handler);
1649 TIFFClose(tiff);
1650 return(GetFirstImageInList(image));
1651}
1652#endif
1653
1654/*
1655%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1656% %
1657% %
1658% %
1659% R e g i s t e r T I F F I m a g e %
1660% %
1661% %
1662% %
1663%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1664%
1665% RegisterTIFFImage() adds properties for the TIFF image format to
1666% the list of supported formats. The properties include the image format
1667% tag, a method to read and/or write the format, whether the format
1668% supports the saving of more than one frame to the same file or blob,
1669% whether the format supports native in-memory I/O, and a brief
1670% description of the format.
1671%
1672% The format of the RegisterTIFFImage method is:
1673%
cristybb503372010-05-27 20:51:26 +00001674% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001675%
1676*/
cristybb503372010-05-27 20:51:26 +00001677ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001678{
1679#define TIFFDescription "Tagged Image File Format"
1680
1681 char
1682 version[MaxTextExtent];
1683
1684 MagickInfo
1685 *entry;
cristy6b032822010-06-29 16:52:32 +00001686
cristy18b17442009-10-25 18:36:48 +00001687 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1688 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001689 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001690 if (instantiate_key == MagickFalse)
1691 {
1692 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1693 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1694 instantiate_key=MagickTrue;
1695 }
cristyf84a1932010-01-03 18:00:18 +00001696 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001697 *version='\0';
1698#if defined(TIFF_VERSION)
1699 (void) FormatMagickString(version,MaxTextExtent,"%d",TIFF_VERSION);
1700#endif
1701#if defined(MAGICKCORE_TIFF_DELEGATE)
1702 {
1703 const char
1704 *p;
1705
cristybb503372010-05-27 20:51:26 +00001706 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001707 i;
1708
1709 p=TIFFGetVersion();
1710 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1711 version[i]=(*p++);
1712 version[i]='\0';
1713 }
1714#endif
1715
cristy3d7f8062009-09-24 20:45:35 +00001716 entry=SetMagickInfo("GROUP4");
1717#if defined(MAGICKCORE_TIFF_DELEGATE)
1718 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1719 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1720#endif
1721 entry->raw=MagickTrue;
1722 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001723 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001724 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001725 entry->seekable_stream=MagickTrue;
1726 entry->thread_support=NoThreadSupport;
1727 entry->description=ConstantString("Raw CCITT Group4");
1728 entry->module=ConstantString("TIFF");
1729 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001730 entry=SetMagickInfo("PTIF");
1731#if defined(MAGICKCORE_TIFF_DELEGATE)
1732 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1733 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1734#endif
1735 entry->endian_support=MagickTrue;
1736 entry->seekable_stream=MagickTrue;
1737 entry->thread_support=NoThreadSupport;
1738 entry->description=ConstantString("Pyramid encoded TIFF");
1739 entry->module=ConstantString("TIFF");
1740 (void) RegisterMagickInfo(entry);
1741 entry=SetMagickInfo("TIF");
1742#if defined(MAGICKCORE_TIFF_DELEGATE)
1743 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1744 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1745#endif
1746 entry->endian_support=MagickTrue;
1747 entry->seekable_stream=MagickTrue;
1748 entry->stealth=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("TIFF");
1756#if defined(MAGICKCORE_TIFF_DELEGATE)
1757 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1758 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1759#endif
1760 entry->magick=(IsImageFormatHandler *) IsTIFF;
1761 entry->endian_support=MagickTrue;
1762 entry->seekable_stream=MagickTrue;
1763 entry->thread_support=NoThreadSupport;
1764 entry->description=ConstantString(TIFFDescription);
1765 if (*version != '\0')
1766 entry->version=ConstantString(version);
1767 entry->module=ConstantString("TIFF");
1768 (void) RegisterMagickInfo(entry);
1769 entry=SetMagickInfo("TIFF64");
1770#if defined(TIFF_VERSION_BIG)
1771 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1772 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1773#endif
1774 entry->adjoin=MagickFalse;
1775 entry->endian_support=MagickTrue;
1776 entry->seekable_stream=MagickTrue;
1777 entry->thread_support=NoThreadSupport;
1778 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1779 if (*version != '\0')
1780 entry->version=ConstantString(version);
1781 entry->module=ConstantString("TIFF");
1782 (void) RegisterMagickInfo(entry);
1783 return(MagickImageCoderSignature);
1784}
1785
1786/*
1787%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1788% %
1789% %
1790% %
1791% U n r e g i s t e r T I F F I m a g e %
1792% %
1793% %
1794% %
1795%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1796%
1797% UnregisterTIFFImage() removes format registrations made by the TIFF module
1798% from the list of supported formats.
1799%
1800% The format of the UnregisterTIFFImage method is:
1801%
1802% UnregisterTIFFImage(void)
1803%
1804*/
1805ModuleExport void UnregisterTIFFImage(void)
1806{
cristy3d7f8062009-09-24 20:45:35 +00001807 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001808 (void) UnregisterMagickInfo("PTIF");
1809 (void) UnregisterMagickInfo("TIF");
1810 (void) UnregisterMagickInfo("TIFF");
1811 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001812 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1813 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001814 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001815 if (instantiate_key != MagickFalse)
1816 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1817 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1818 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001819 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001820 DestroySemaphoreInfo(&tiff_semaphore);
1821}
1822
1823#if defined(MAGICKCORE_TIFF_DELEGATE)
1824/*
1825%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1826% %
1827% %
1828% %
cristy3d7f8062009-09-24 20:45:35 +00001829% W r i t e G R O U P 4 I m a g e %
1830% %
1831% %
1832% %
1833%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1834%
1835% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1836%
1837% The format of the WriteGROUP4Image method is:
1838%
1839% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1840% Image *image)
1841%
1842% A description of each parameter follows:
1843%
1844% o image_info: the image info.
1845%
1846% o image: The image.
1847%
1848*/
1849static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1850 Image *image)
1851{
1852 char
1853 filename[MaxTextExtent];
1854
1855 FILE
1856 *file;
1857
1858 Image
1859 *huffman_image;
1860
1861 ImageInfo
1862 *write_info;
1863
1864 int
1865 unique_file;
1866
1867 MagickBooleanType
1868 status;
1869
cristybb503372010-05-27 20:51:26 +00001870 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001871 i;
1872
1873 ssize_t
1874 count;
1875
1876 TIFF
1877 *tiff;
1878
cristy94c8fe42009-10-06 01:57:36 +00001879 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001880 *byte_count,
1881 strip_size;
1882
1883 unsigned char
1884 *buffer;
1885
1886 /*
1887 Write image as CCITT Group4 TIFF image to a temporary file.
1888 */
1889 assert(image_info != (const ImageInfo *) NULL);
1890 assert(image_info->signature == MagickSignature);
1891 assert(image != (Image *) NULL);
1892 assert(image->signature == MagickSignature);
1893 if (image->debug != MagickFalse)
1894 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1895 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1896 if (status == MagickFalse)
1897 return(status);
1898 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1899 if (huffman_image == (Image *) NULL)
1900 {
1901 (void) CloseBlob(image);
1902 return(MagickFalse);
1903 }
cristy94c8fe42009-10-06 01:57:36 +00001904 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001905 file=(FILE *) NULL;
1906 unique_file=AcquireUniqueFileResource(filename);
1907 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001908 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001909 if ((unique_file == -1) || (file == (FILE *) NULL))
1910 {
1911 ThrowFileException(&image->exception,FileOpenError,
1912 "UnableToCreateTemporaryFile",filename);
1913 return(MagickFalse);
1914 }
1915 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1916 filename);
1917 (void) SetImageType(huffman_image,BilevelType);
cristy14efd992009-09-26 23:46:03 +00001918 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001919 SetImageInfoFile(write_info,file);
1920 write_info->compression=Group4Compression;
1921 write_info->type=BilevelType;
1922 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1923 status=WriteTIFFImage(write_info,huffman_image);
1924 (void) fflush(file);
1925 write_info=DestroyImageInfo(write_info);
1926 if (status == MagickFalse)
1927 {
1928 InheritException(&image->exception,&huffman_image->exception);
1929 huffman_image=DestroyImage(huffman_image);
1930 (void) fclose(file);
1931 (void) RelinquishUniqueFileResource(filename);
1932 return(MagickFalse);
1933 }
1934 tiff=TIFFOpen(filename,"rb");
1935 if (tiff == (TIFF *) NULL)
1936 {
1937 huffman_image=DestroyImage(huffman_image);
1938 (void) fclose(file);
1939 (void) RelinquishUniqueFileResource(filename);
1940 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1941 image_info->filename);
1942 return(MagickFalse);
1943 }
1944 /*
1945 Allocate raw strip buffer.
1946 */
cristy94c8fe42009-10-06 01:57:36 +00001947 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1948 {
1949 TIFFClose(tiff);
1950 huffman_image=DestroyImage(huffman_image);
1951 (void) fclose(file);
1952 (void) RelinquishUniqueFileResource(filename);
1953 return(MagickFalse);
1954 }
cristy3d7f8062009-09-24 20:45:35 +00001955 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00001956 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001957 if (byte_count[i] > strip_size)
1958 strip_size=byte_count[i];
1959 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1960 sizeof(*buffer));
1961 if (buffer == (unsigned char *) NULL)
1962 {
1963 TIFFClose(tiff);
1964 huffman_image=DestroyImage(huffman_image);
1965 (void) fclose(file);
1966 (void) RelinquishUniqueFileResource(filename);
1967 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1968 image_info->filename);
1969 }
1970 /*
1971 Compress runlength encoded to 2D Huffman pixels.
1972 */
cristybb503372010-05-27 20:51:26 +00001973 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001974 {
cristy94c8fe42009-10-06 01:57:36 +00001975 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00001976 if (WriteBlob(image,(size_t) count,buffer) != count)
1977 status=MagickFalse;
1978 }
1979 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1980 TIFFClose(tiff);
1981 huffman_image=DestroyImage(huffman_image);
1982 (void) fclose(file);
1983 (void) RelinquishUniqueFileResource(filename);
1984 (void) CloseBlob(image);
1985 return(status);
1986}
1987#endif
1988
1989#if defined(MAGICKCORE_TIFF_DELEGATE)
1990/*
1991%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1992% %
1993% %
1994% %
cristy3ed852e2009-09-05 21:47:34 +00001995% W r i t e P T I F I m a g e %
1996% %
1997% %
1998% %
1999%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2000%
2001% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2002% format.
2003%
2004% The format of the WritePTIFImage method is:
2005%
2006% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2007% Image *image)
2008%
2009% A description of each parameter follows:
2010%
2011% o image_info: the image info.
2012%
2013% o image: The image.
2014%
2015*/
2016static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2017 Image *image)
2018{
2019 Image
2020 *images,
2021 *next,
2022 *pyramid_image;
2023
2024 ImageInfo
2025 *write_info;
2026
2027 MagickBooleanType
2028 status;
2029
cristybb503372010-05-27 20:51:26 +00002030 size_t
cristy3ed852e2009-09-05 21:47:34 +00002031 columns,
2032 rows;
2033
2034 /*
2035 Create pyramid-encoded TIFF image.
2036 */
2037 images=NewImageList();
2038 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2039 {
2040 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2041 &image->exception));
2042 columns=next->columns;
2043 rows=next->rows;
2044 while ((columns > 64) && (rows > 64))
2045 {
2046 columns/=2;
2047 rows/=2;
2048 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2049 &image->exception);
2050 AppendImageToList(&images,pyramid_image);
2051 }
2052 }
2053 /*
2054 Write pyramid-encoded TIFF image.
2055 */
cristy58567532009-09-26 01:13:32 +00002056 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002057 *write_info->magick='\0';
2058 write_info->adjoin=MagickTrue;
2059 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2060 images=DestroyImageList(images);
2061 write_info=DestroyImageInfo(write_info);
2062 return(status);
2063}
2064#endif
2065
2066#if defined(MAGICKCORE_TIFF_DELEGATE)
2067/*
2068%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2069% %
2070% %
2071% W r i t e T I F F I m a g e %
2072% %
2073% %
2074% %
2075%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2076%
2077% WriteTIFFImage() writes an image in the Tagged image file format.
2078%
2079% The format of the WriteTIFFImage method is:
2080%
2081% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2082% Image *image)
2083%
2084% A description of each parameter follows:
2085%
2086% o image_info: the image info.
2087%
2088% o image: The image.
2089%
2090*/
2091
2092typedef struct _TIFFInfo
2093{
2094 RectangleInfo
2095 tile_geometry;
2096
2097 unsigned char
2098 *scanline,
2099 *scanlines,
2100 *pixels;
2101} TIFFInfo;
2102
2103static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2104{
2105 assert(tiff_info != (TIFFInfo *) NULL);
2106 if (tiff_info->scanlines != (unsigned char *) NULL)
2107 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2108 tiff_info->scanlines);
2109 if (tiff_info->pixels != (unsigned char *) NULL)
2110 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2111 tiff_info->pixels);
2112}
2113
2114static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2115 TIFFInfo *tiff_info)
2116{
2117 const char
2118 *option;
2119
2120 MagickStatusType
2121 flags;
2122
cristy79e5dad2010-09-16 19:48:33 +00002123 uint32
2124 tile_columns,
2125 tile_rows;
2126
cristy3ed852e2009-09-05 21:47:34 +00002127 assert(tiff_info != (TIFFInfo *) NULL);
2128 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2129 option=GetImageOption(image_info,"tiff:tile-geometry");
2130 if (option == (const char *) NULL)
2131 return(MagickTrue);
2132 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2133 if ((flags & HeightValue) == 0)
2134 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002135 tile_columns=(uint32) tiff_info->tile_geometry.width;
2136 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002137 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2138 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2139 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2140 tiff_info->tile_geometry.width=tile_columns;
2141 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002142 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002143 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002144 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002145 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002146 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2147 (tiff_info->pixels == (unsigned char *) NULL))
2148 {
2149 DestroyTIFFInfo(tiff_info);
2150 return(MagickFalse);
2151 }
2152 return(MagickTrue);
2153}
2154
cristybb503372010-05-27 20:51:26 +00002155static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002156 tsample_t sample,Image *image)
2157{
2158 int32
2159 status;
2160
cristybb503372010-05-27 20:51:26 +00002161 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002162 bytes_per_pixel,
2163 j,
2164 k,
2165 l;
2166
cristybb503372010-05-27 20:51:26 +00002167 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002168 i;
2169
2170 register unsigned char
2171 *p,
2172 *q;
2173
cristybb503372010-05-27 20:51:26 +00002174 size_t
cristy3ed852e2009-09-05 21:47:34 +00002175 number_tiles,
2176 tile_width;
2177
2178 if (TIFFIsTiled(tiff) == 0)
2179 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2180 /*
2181 Fill scanlines to tile height.
2182 */
cristybb503372010-05-27 20:51:26 +00002183 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002184 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2185 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002186 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2187 (tiff_info->tile_geometry.height-1)) && (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002188 return(0);
2189 /*
2190 Write tile to TIFF image.
2191 */
2192 status=0;
cristybb503372010-05-27 20:51:26 +00002193 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002194 tiff_info->tile_geometry.width);
2195 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2196 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002197 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002198 {
cristybb503372010-05-27 20:51:26 +00002199 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002200 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002201 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2202 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002203 {
2204 if (bytes_per_pixel == 0)
2205 {
2206 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2207 tiff_info->tile_geometry.width+k)/8);
2208 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2209 *q++=(*p++);
2210 continue;
2211 }
2212 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2213 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2214 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2215 for (l=0; l < bytes_per_pixel; l++)
2216 *q++=(*p++);
2217 }
cristydaff8092010-04-22 14:50:53 +00002218 if ((i*tiff_info->tile_geometry.width) != image->columns)
2219 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2220 tiff_info->tile_geometry.width),(uint32) ((row/
2221 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2222 sample);
cristy3ed852e2009-09-05 21:47:34 +00002223 if (status < 0)
2224 break;
2225 }
2226 return(status);
2227}
2228
2229static void TIFFSetProfiles(TIFF *tiff,Image *image)
2230{
2231 const char
2232 *name;
2233
2234 const StringInfo
2235 *profile;
2236
2237 if (image->profiles == (void *) NULL)
2238 return;
2239 ResetImageProfileIterator(image);
2240 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2241 {
2242 profile=GetImageProfile(image,name);
2243#if defined(TIFFTAG_XMLPACKET)
2244 if (LocaleCompare(name,"xmp") == 0)
2245 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2246 profile),GetStringInfoDatum(profile));
2247#endif
2248#if defined(TIFFTAG_ICCPROFILE)
2249 if (LocaleCompare(name,"icc") == 0)
2250 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2251 profile),GetStringInfoDatum(profile));
2252#endif
2253 if (LocaleCompare(name,"iptc") == 0)
2254 {
2255 size_t
2256 length;
2257
2258 StringInfo
2259 *iptc_profile;
2260
2261 iptc_profile=CloneStringInfo(profile);
2262 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2263 0x03);
2264 SetStringInfoLength(iptc_profile,length);
2265 if (TIFFIsByteSwapped(tiff))
2266 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002267 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002268 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2269 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2270 iptc_profile=DestroyStringInfo(iptc_profile);
2271 }
2272#if defined(TIFFTAG_PHOTOSHOP)
2273 if (LocaleCompare(name,"8bim") == 0)
2274 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2275 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2276#endif
2277 if (LocaleCompare(name,"tiff:37724") == 0)
2278 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2279 GetStringInfoDatum(profile));
2280 name=GetNextImageProfile(image);
2281 }
2282}
2283
2284static void TIFFSetProperties(TIFF *tiff,Image *image)
2285{
2286 const char
2287 *value;
2288
cristy15d8d212010-09-17 01:56:30 +00002289 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002290 value=GetImageProperty(image,"tiff:hostcomputer");
2291 if (value != (const char *) NULL)
2292 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2293 value=GetImageProperty(image,"tiff:artist");
2294 if (value != (const char *) NULL)
2295 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2296 value=GetImageProperty(image,"tiff:timestamp");
2297 if (value != (const char *) NULL)
2298 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2299 value=GetImageProperty(image,"tiff:make");
2300 if (value != (const char *) NULL)
2301 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2302 value=GetImageProperty(image,"tiff:model");
2303 if (value != (const char *) NULL)
2304 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristy15d8d212010-09-17 01:56:30 +00002305 value=GetImageProperty(image,"tiff:software");
2306 if (value != (const char *) NULL)
2307 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristy3ed852e2009-09-05 21:47:34 +00002308 value=GetImageProperty(image,"tiff:copyright");
2309 if (value != (const char *) NULL)
2310 (void) TIFFSetField(tiff,33432,value);
2311 value=GetImageProperty(image,"kodak-33423");
2312 if (value != (const char *) NULL)
2313 (void) TIFFSetField(tiff,33423,value);
2314 value=GetImageProperty(image,"kodak-36867");
2315 if (value != (const char *) NULL)
2316 (void) TIFFSetField(tiff,36867,value);
2317 value=GetImageProperty(image,"label");
2318 if (value != (const char *) NULL)
2319 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2320 value=GetImageProperty(image,"comment");
2321 if (value != (const char *) NULL)
2322 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2323}
2324
2325static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2326{
2327#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2328 const char
2329 *value;
2330
cristybb503372010-05-27 20:51:26 +00002331 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002332 i;
2333
2334 uint32
2335 offset;
2336
2337 /*
2338 Write EXIF properties.
2339 */
2340 offset=0;
2341 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2342 for (i=0; exif_info[i].tag != 0; i++)
2343 {
2344 value=GetImageProperty(image,exif_info[i].property);
2345 if (value == (const char *) NULL)
2346 continue;
2347 switch (exif_info[i].type)
2348 {
2349 case TIFF_ASCII:
2350 {
2351 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2352 break;
2353 }
2354 case TIFF_SHORT:
2355 {
2356 uint16
2357 shorty;
2358
cristyf2f27272009-12-17 14:48:46 +00002359 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002360 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2361 break;
2362 }
2363 case TIFF_LONG:
2364 {
2365 uint16
cristybb503372010-05-27 20:51:26 +00002366 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002367
cristybb503372010-05-27 20:51:26 +00002368 ssize_ty=(uint16) StringToLong(value);
2369 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002370 break;
2371 }
2372 case TIFF_RATIONAL:
2373 case TIFF_SRATIONAL:
2374 {
2375 float
2376 rational;
2377
cristyf2f27272009-12-17 14:48:46 +00002378 rational=StringToDouble(value);
cristy3ed852e2009-09-05 21:47:34 +00002379 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2380 break;
2381 }
2382 default:
2383 break;
2384 }
2385 }
2386 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2387#else
2388 (void) tiff;
2389 (void) image;
2390#endif
2391}
2392
2393static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2394 Image *image)
2395{
2396#if !defined(TIFFDefaultStripSize)
2397#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2398#endif
2399
2400 const char
2401 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002402 *option;
cristy3ed852e2009-09-05 21:47:34 +00002403
2404 CompressionType
2405 compression;
2406
cristy6b032822010-06-29 16:52:32 +00002407 EndianType
2408 endian_type;
2409
cristybb503372010-05-27 20:51:26 +00002410 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002411 y;
2412
2413 MagickBooleanType
2414 debug,
2415 status;
2416
2417 MagickOffsetType
2418 scene;
2419
2420 QuantumInfo
2421 *quantum_info;
2422
2423 QuantumType
2424 quantum_type;
2425
cristybb503372010-05-27 20:51:26 +00002426 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002427 i;
2428
2429 size_t
2430 length;
2431
2432 TIFF
2433 *tiff;
2434
2435 TIFFErrorHandler
2436 error_handler,
2437 warning_handler;
2438
2439 TIFFInfo
2440 tiff_info;
2441
2442 uint16
2443 bits_per_sample,
2444 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002445 endian,
cristy3ed852e2009-09-05 21:47:34 +00002446 photometric;
2447
2448 uint32
2449 rows_per_strip;
2450
2451 unsigned char
2452 *pixels;
2453
cristyf2687ca2010-06-29 16:32:38 +00002454 size_t
2455 lsb_first;
2456
cristy3ed852e2009-09-05 21:47:34 +00002457 /*
2458 Open TIFF file.
2459 */
2460 assert(image_info != (const ImageInfo *) NULL);
2461 assert(image_info->signature == MagickSignature);
2462 assert(image != (Image *) NULL);
2463 assert(image->signature == MagickSignature);
2464 if (image->debug != MagickFalse)
2465 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2466 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2467 if (status == MagickFalse)
2468 return(status);
2469 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2470 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2471 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002472 endian_type=UndefinedEndian;
2473 option=GetImageOption(image_info,"tiff:endian");
2474 if (option != (const char *) NULL)
2475 {
2476 if (LocaleNCompare(option,"msb",3) == 0)
2477 endian_type=MSBEndian;
2478 if (LocaleNCompare(option,"lsb",3) == 0)
2479 endian_type=LSBEndian;;
2480 }
2481 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002482 {
2483 case LSBEndian: mode="wl"; break;
2484 case MSBEndian: mode="wb"; break;
2485 default: mode="w"; break;
2486 }
2487#if defined(TIFF_VERSION_BIG)
2488 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002489 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002490 {
2491 case LSBEndian: mode="wl8"; break;
2492 case MSBEndian: mode="wb8"; break;
2493 default: mode="w8"; break;
2494 }
2495#endif
2496 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2497 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2498 TIFFUnmapBlob);
2499 if (tiff == (TIFF *) NULL)
2500 {
2501 (void) TIFFSetWarningHandler(warning_handler);
2502 (void) TIFFSetErrorHandler(error_handler);
2503 return(MagickFalse);
2504 }
2505 scene=0;
2506 debug=IsEventLogging();
2507 do
2508 {
2509 /*
2510 Initialize TIFF fields.
2511 */
cristy5f1c1ff2010-12-23 21:38:06 +00002512 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002513 (image_info->type != OptimizeType))
2514 (void) SetImageType(image,image_info->type);
2515 quantum_info=AcquireQuantumInfo(image_info,image);
2516 if (quantum_info == (QuantumInfo *) NULL)
2517 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2518 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2519 (quantum_info->format == UndefinedQuantumFormat) &&
2520 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2521 {
2522 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2523 if (status == MagickFalse)
2524 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2525 }
2526 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2527 (GetPreviousImageInList(image) != (Image *) NULL))
2528 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2529 if ((image->columns != (uint32) image->columns) ||
2530 (image->rows != (uint32) image->rows))
2531 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2532 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2533 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2534 compression=image->compression;
2535 if (image_info->compression != UndefinedCompression)
2536 compression=image_info->compression;
2537 switch (compression)
2538 {
2539 case FaxCompression:
2540 {
2541 compress_tag=COMPRESSION_CCITTFAX3;
2542 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2543 break;
2544 }
2545 case Group4Compression:
2546 {
2547 compress_tag=COMPRESSION_CCITTFAX4;
2548 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2549 break;
2550 }
2551 case JPEGCompression:
2552 {
2553 compress_tag=COMPRESSION_JPEG;
2554 break;
2555 }
cristyfbb0ef02010-12-19 02:32:11 +00002556#if defined(COMPRESSION_LZMA)
2557 case LZMACompression:
2558 {
2559 compress_tag=COMPRESSION_LZMA;
2560 break;
2561 }
2562#endif
cristy3ed852e2009-09-05 21:47:34 +00002563 case LZWCompression:
2564 {
2565 compress_tag=COMPRESSION_LZW;
2566 break;
2567 }
2568 case RLECompression:
2569 {
2570 compress_tag=COMPRESSION_PACKBITS;
2571 break;
2572 }
2573 case ZipCompression:
2574 {
2575 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2576 break;
2577 }
2578 case NoCompression:
2579 default:
2580 {
2581 compress_tag=COMPRESSION_NONE;
2582 break;
2583 }
2584 }
cristy5b675872009-10-21 13:12:00 +00002585#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2586 if ((compress_tag != COMPRESSION_NONE) &&
2587 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002588 {
cristy5b675872009-10-21 13:12:00 +00002589 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2590 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002591 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002592 compress_tag=COMPRESSION_NONE;
2593 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002594 }
2595#else
2596 switch (compress_tag)
2597 {
2598#if defined(CCITT_SUPPORT)
2599 case COMPRESSION_CCITTFAX3:
2600 case COMPRESSION_CCITTFAX4:
2601#endif
2602#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2603 case COMPRESSION_JPEG:
2604#endif
cristyfbb0ef02010-12-19 02:32:11 +00002605#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2606 case COMPRESSION_LZMA:
2607#endif
cristy3ed852e2009-09-05 21:47:34 +00002608#if defined(LZW_SUPPORT)
2609 case COMPRESSION_LZW:
2610#endif
2611#if defined(PACKBITS_SUPPORT)
2612 case COMPRESSION_PACKBITS:
2613#endif
2614#if defined(ZIP_SUPPORT)
2615 case COMPRESSION_ADOBE_DEFLATE:
2616#endif
2617 case COMPRESSION_NONE:
2618 break;
2619 default:
2620 {
2621 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2622 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002623 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002624 compress_tag=COMPRESSION_NONE;
2625 compression=NoCompression;
2626 break;
2627 }
2628 }
2629#endif
2630 switch (compression)
2631 {
2632 case FaxCompression:
2633 case Group4Compression:
2634 {
2635 (void) SetImageType(image,BilevelType);
2636 break;
2637 }
2638 case JPEGCompression:
2639 {
2640 (void) SetImageStorageClass(image,DirectClass);
2641 (void) SetImageDepth(image,8);
2642 break;
2643 }
2644 default:
2645 break;
2646 }
cristy3ed852e2009-09-05 21:47:34 +00002647 if (image->colorspace == CMYKColorspace)
2648 {
2649 photometric=PHOTOMETRIC_SEPARATED;
2650 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2651 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2652 }
2653 else
2654 {
2655 /*
2656 Full color TIFF raster.
2657 */
2658 if (image->colorspace == LabColorspace)
2659 photometric=PHOTOMETRIC_CIELAB;
2660 else
2661 if (image->colorspace == YCbCrColorspace)
2662 {
2663 photometric=PHOTOMETRIC_YCBCR;
2664 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy79e5dad2010-09-16 19:48:33 +00002665 (void) SetImageStorageClass(image,DirectClass);
cristy3ed852e2009-09-05 21:47:34 +00002666 (void) SetImageDepth(image,8);
2667 }
2668 else
2669 {
2670 if (image->colorspace != RGBColorspace)
2671 (void) TransformImageColorspace(image,RGBColorspace);
2672 photometric=PHOTOMETRIC_RGB;
2673 }
2674 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2675 if ((image_info->type != TrueColorType) &&
2676 (image_info->type != TrueColorMatteType))
2677 {
cristy94c8fe42009-10-06 01:57:36 +00002678 if ((image_info->type != PaletteType) &&
2679 (IsGrayImage(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002680 {
cristy94c8fe42009-10-06 01:57:36 +00002681 photometric=(uint16) (quantum_info->min_is_white !=
2682 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2683 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002684 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002685 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy94c8fe42009-10-06 01:57:36 +00002686 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2687 {
2688 status=SetQuantumDepth(image,quantum_info,1);
2689 if (status == MagickFalse)
2690 ThrowWriterException(ResourceLimitError,
2691 "MemoryAllocationFailed");
2692 }
cristy3ed852e2009-09-05 21:47:34 +00002693 }
2694 else
cristy94c8fe42009-10-06 01:57:36 +00002695 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002696 {
cristybb503372010-05-27 20:51:26 +00002697 size_t
cristy94c8fe42009-10-06 01:57:36 +00002698 depth;
2699
2700 /*
2701 Colormapped TIFF raster.
2702 */
cristy3ed852e2009-09-05 21:47:34 +00002703 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002704 photometric=PHOTOMETRIC_PALETTE;
2705 depth=1;
2706 while ((GetQuantumRange(depth)+1) < image->colors)
2707 depth<<=1;
2708 status=SetQuantumDepth(image,quantum_info,depth);
2709 if (status == MagickFalse)
2710 ThrowWriterException(ResourceLimitError,
2711 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002712 }
2713 }
2714 }
cristyf2687ca2010-06-29 16:32:38 +00002715 switch (image->endian)
2716 {
2717 case LSBEndian:
2718 {
2719 endian=FILLORDER_LSB2MSB;
2720 break;
2721 }
2722 case MSBEndian:
2723 {
2724 endian=FILLORDER_MSB2LSB;
2725 break;
2726 }
2727 case UndefinedEndian:
2728 default:
2729 {
2730 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2731 break;
2732 }
2733 }
2734 lsb_first=1;
2735 image->endian=MSBEndian;
2736 if ((int) (*(char *) &lsb_first) != 0)
2737 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002738 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2739 (photometric != PHOTOMETRIC_MINISWHITE))
2740 {
2741 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002742 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002743 }
cristy0accf6e2009-10-01 13:14:28 +00002744 else
cristydaff8092010-04-22 14:50:53 +00002745 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2746 (photometric != PHOTOMETRIC_MINISWHITE))
2747 {
2748 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002749 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002750 }
cristy62e282b2010-06-29 01:27:13 +00002751 option=GetImageProperty(image,"tiff:fill-order");
2752 if (option != (const char *) NULL)
2753 {
2754 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002755 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002756 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002757 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002758 }
cristyf2687ca2010-06-29 16:32:38 +00002759 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2760 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002761 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2762 if (image->matte != MagickFalse)
2763 {
2764 uint16
2765 extra_samples,
2766 sample_info[1],
2767 samples_per_pixel;
2768
2769 /*
2770 TIFF has a matte channel.
2771 */
2772 extra_samples=1;
2773 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2774 option=GetImageProperty(image,"tiff:alpha");
2775 if ((option != (const char *) NULL) &&
2776 (LocaleCompare(option,"associated") == 0))
2777 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2778 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2779 &samples_per_pixel);
2780 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2781 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2782 &sample_info);
2783 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2784 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2785 }
2786 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2787 switch (quantum_info->format)
2788 {
2789 case FloatingPointQuantumFormat:
2790 {
2791 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2792 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2793 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2794 break;
2795 }
2796 case SignedQuantumFormat:
2797 {
2798 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2799 break;
2800 }
2801 case UnsignedQuantumFormat:
2802 {
2803 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2804 break;
2805 }
2806 default:
2807 break;
2808 }
cristy3ed852e2009-09-05 21:47:34 +00002809 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2810 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2811 if (photometric == PHOTOMETRIC_RGB)
2812 if ((image_info->interlace == PlaneInterlace) ||
2813 (image_info->interlace == PartitionInterlace))
2814 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2815 rows_per_strip=1;
2816 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002817 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2818 1);
cristy3ed852e2009-09-05 21:47:34 +00002819 option=GetImageOption(image_info,"tiff:rows-per-strip");
2820 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002821 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002822 switch (compress_tag)
2823 {
2824 case COMPRESSION_JPEG:
2825 {
2826#if defined(JPEG_SUPPORT)
2827 const char
2828 *sampling_factor;
2829
2830 GeometryInfo
2831 geometry_info;
2832
2833 MagickStatusType
2834 flags;
2835
2836 rows_per_strip+=(16-(rows_per_strip % 16));
2837 if (image->quality != 0)
2838 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2839 if (image_info->quality != UndefinedCompressionQuality)
2840 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2841 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2842 if (image->colorspace == RGBColorspace)
2843 {
cristy949bf5b2010-05-08 02:47:03 +00002844 const char
2845 *value;
2846
cristy3ed852e2009-09-05 21:47:34 +00002847 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2848 sampling_factor=(const char *) NULL;
2849 value=GetImageProperty(image,"jpeg:sampling-factor");
2850 if (value != (char *) NULL)
2851 {
2852 sampling_factor=value;
2853 if (image->debug != MagickFalse)
2854 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2855 " Input sampling-factors=%s",sampling_factor);
2856 }
2857 if (image_info->sampling_factor != (char *) NULL)
2858 sampling_factor=image_info->sampling_factor;
2859 if (sampling_factor != (const char *) NULL)
2860 {
2861 flags=ParseGeometry(sampling_factor,&geometry_info);
2862 if ((flags & SigmaValue) == 0)
2863 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002864 if (image->colorspace == YCbCrColorspace)
2865 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2866 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00002867 }
2868 }
2869 if (bits_per_sample == 12)
2870 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2871#endif
2872 break;
2873 }
2874 case COMPRESSION_ADOBE_DEFLATE:
2875 {
cristyf6fe0a12010-05-30 00:44:47 +00002876 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002877 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2878 &bits_per_sample);
2879 if (((photometric == PHOTOMETRIC_RGB) ||
2880 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2881 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002882 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00002883 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2884 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00002885 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00002886 break;
2887 }
2888 case COMPRESSION_CCITTFAX3:
2889 {
2890 /*
2891 Byte-aligned EOL.
2892 */
cristyeaedf062010-05-29 22:36:02 +00002893 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002894 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2895 break;
2896 }
2897 case COMPRESSION_CCITTFAX4:
2898 {
cristyeaedf062010-05-29 22:36:02 +00002899 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002900 break;
2901 }
cristyef8f26b2010-12-19 20:29:16 +00002902#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2903 case COMPRESSION_LZMA:
2904 {
2905 if (((photometric == PHOTOMETRIC_RGB) ||
2906 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2907 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2908 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2909 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
2910 image_info->quality == UndefinedCompressionQuality ? 7 :
2911 MagickMin((ssize_t) image_info->quality/10,9)));
2912 break;
2913 }
2914#endif
cristy3ed852e2009-09-05 21:47:34 +00002915 case COMPRESSION_LZW:
2916 {
2917 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2918 &bits_per_sample);
2919 if (((photometric == PHOTOMETRIC_RGB) ||
2920 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2921 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002922 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00002923 break;
2924 }
2925 default:
2926 break;
2927 }
cristy79e5dad2010-09-16 19:48:33 +00002928 option=GetImageOption(image_info,"tiff:tile-geometry");
2929 if (option == (const char *) NULL)
2930 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy3ed852e2009-09-05 21:47:34 +00002931 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2932 {
2933 unsigned short
2934 units;
2935
2936 /*
2937 Set image resolution.
2938 */
2939 units=RESUNIT_NONE;
2940 if (image->units == PixelsPerInchResolution)
2941 units=RESUNIT_INCH;
2942 if (image->units == PixelsPerCentimeterResolution)
2943 units=RESUNIT_CENTIMETER;
2944 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2945 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2946 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2947 if ((image->page.x != 0) || (image->page.y != 0))
2948 {
2949 /*
2950 Set image position.
2951 */
2952 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2953 image->x_resolution);
2954 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2955 image->y_resolution);
2956 }
2957 }
2958 if (image->chromaticity.white_point.x != 0.0)
2959 {
2960 float
2961 chromaticity[6];
2962
2963 /*
2964 Set image chromaticity.
2965 */
2966 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2967 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2968 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2969 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2970 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2971 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2972 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2973 chromaticity[0]=(float) image->chromaticity.white_point.x;
2974 chromaticity[1]=(float) image->chromaticity.white_point.y;
2975 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2976 }
2977 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2978 {
2979 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2980 if (image->scene != 0)
2981 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
2982 GetImageListLength(image));
2983 }
2984 if (image->orientation != UndefinedOrientation)
2985 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
2986 (void) TIFFSetProfiles(tiff,image);
2987 {
2988 uint16
2989 page,
2990 pages;
2991
2992 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00002993 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00002994 if ((image_info->adjoin != MagickFalse) && (pages > 1))
2995 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2996 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
2997 }
2998 (void) TIFFSetProperties(tiff,image);
2999 if (0)
3000 (void) TIFFSetEXIFProperties(tiff,image);
3001 /*
3002 Write image scanlines.
3003 */
3004 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3005 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3006 pixels=GetQuantumPixels(quantum_info);
3007 tiff_info.scanline=GetQuantumPixels(quantum_info);
3008 switch (photometric)
3009 {
3010 case PHOTOMETRIC_CIELAB:
3011 case PHOTOMETRIC_YCBCR:
3012 case PHOTOMETRIC_RGB:
3013 {
3014 /*
3015 RGB TIFF image.
3016 */
3017 switch (image_info->interlace)
3018 {
3019 case NoInterlace:
3020 default:
3021 {
3022 quantum_type=RGBQuantum;
3023 if (image->matte != MagickFalse)
3024 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003025 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003026 {
3027 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003028 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003029
3030 p=GetVirtualPixels(image,0,y,image->columns,1,
3031 &image->exception);
3032 if (p == (const PixelPacket *) NULL)
3033 break;
3034 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3035 quantum_info,quantum_type,pixels,&image->exception);
3036 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3037 break;
3038 if (image->previous == (Image *) NULL)
3039 {
cristy2837bcc2010-08-07 23:57:39 +00003040 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3041 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003042 if (status == MagickFalse)
3043 break;
3044 }
3045 }
3046 break;
3047 }
3048 case PlaneInterlace:
3049 case PartitionInterlace:
3050 {
3051 /*
3052 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3053 */
cristybb503372010-05-27 20:51:26 +00003054 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003055 {
3056 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003057 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003058
3059 p=GetVirtualPixels(image,0,y,image->columns,1,
3060 &image->exception);
3061 if (p == (const PixelPacket *) NULL)
3062 break;
3063 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3064 quantum_info,RedQuantum,pixels,&image->exception);
3065 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3066 break;
3067 }
3068 if (image->previous == (Image *) NULL)
3069 {
3070 status=SetImageProgress(image,SaveImageTag,100,400);
3071 if (status == MagickFalse)
3072 break;
3073 }
cristybb503372010-05-27 20:51:26 +00003074 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003075 {
3076 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003077 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003078
3079 p=GetVirtualPixels(image,0,y,image->columns,1,
3080 &image->exception);
3081 if (p == (const PixelPacket *) NULL)
3082 break;
3083 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3084 quantum_info,GreenQuantum,pixels,&image->exception);
3085 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3086 break;
3087 }
3088 if (image->previous == (Image *) NULL)
3089 {
3090 status=SetImageProgress(image,SaveImageTag,200,400);
3091 if (status == MagickFalse)
3092 break;
3093 }
cristybb503372010-05-27 20:51:26 +00003094 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003095 {
3096 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003097 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003098
3099 p=GetVirtualPixels(image,0,y,image->columns,1,
3100 &image->exception);
3101 if (p == (const PixelPacket *) NULL)
3102 break;
3103 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3104 quantum_info,BlueQuantum,pixels,&image->exception);
3105 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3106 break;
3107 }
3108 if (image->previous == (Image *) NULL)
3109 {
3110 status=SetImageProgress(image,SaveImageTag,300,400);
3111 if (status == MagickFalse)
3112 break;
3113 }
3114 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003115 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003116 {
3117 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003118 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003119
3120 p=GetVirtualPixels(image,0,y,image->columns,1,
3121 &image->exception);
3122 if (p == (const PixelPacket *) NULL)
3123 break;
3124 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3125 quantum_info,AlphaQuantum,pixels,&image->exception);
3126 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3127 break;
3128 }
3129 if (image->previous == (Image *) NULL)
3130 {
3131 status=SetImageProgress(image,SaveImageTag,400,400);
3132 if (status == MagickFalse)
3133 break;
3134 }
3135 break;
3136 }
3137 }
3138 break;
3139 }
3140 case PHOTOMETRIC_SEPARATED:
3141 {
3142 /*
3143 CMYK TIFF image.
3144 */
3145 quantum_type=CMYKQuantum;
3146 if (image->matte != MagickFalse)
3147 quantum_type=CMYKAQuantum;
3148 if (image->colorspace != CMYKColorspace)
3149 (void) TransformImageColorspace(image,CMYKColorspace);
cristybb503372010-05-27 20:51:26 +00003150 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003151 {
3152 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003153 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003154
3155 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3156 if (p == (const PixelPacket *) NULL)
3157 break;
3158 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3159 quantum_info,quantum_type,pixels,&image->exception);
3160 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3161 break;
3162 if (image->previous == (Image *) NULL)
3163 {
cristycee97112010-05-28 00:44:52 +00003164 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3165 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003166 if (status == MagickFalse)
3167 break;
3168 }
3169 }
3170 break;
3171 }
3172 case PHOTOMETRIC_PALETTE:
3173 {
3174 uint16
3175 *blue,
3176 *green,
3177 *red;
3178
3179 /*
3180 Colormapped TIFF image.
3181 */
3182 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3183 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3184 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3185 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3186 (blue == (uint16 *) NULL))
3187 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3188 /*
3189 Initialize TIFF colormap.
3190 */
3191 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3192 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3193 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003194 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003195 {
3196 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3197 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3198 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3199 }
3200 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3201 red=(uint16 *) RelinquishMagickMemory(red);
3202 green=(uint16 *) RelinquishMagickMemory(green);
3203 blue=(uint16 *) RelinquishMagickMemory(blue);
3204 }
3205 default:
3206 {
3207 /*
3208 Convert PseudoClass packets to contiguous grayscale scanlines.
3209 */
3210 quantum_type=IndexQuantum;
3211 if (image->matte != MagickFalse)
3212 {
3213 if (photometric != PHOTOMETRIC_PALETTE)
3214 quantum_type=GrayAlphaQuantum;
3215 else
3216 quantum_type=IndexAlphaQuantum;
3217 }
3218 else
3219 if (photometric != PHOTOMETRIC_PALETTE)
3220 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003221 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003222 {
3223 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003224 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003225
3226 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3227 if (p == (const PixelPacket *) NULL)
3228 break;
3229 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3230 quantum_info,quantum_type,pixels,&image->exception);
3231 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3232 break;
3233 if (image->previous == (Image *) NULL)
3234 {
cristycee97112010-05-28 00:44:52 +00003235 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3236 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003237 if (status == MagickFalse)
3238 break;
3239 }
3240 }
3241 break;
3242 }
3243 }
3244 quantum_info=DestroyQuantumInfo(quantum_info);
3245 DestroyTIFFInfo(&tiff_info);
3246 if (0 && (image_info->verbose == MagickTrue))
3247 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3248 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003249 image->endian=MSBEndian;
3250 if (endian == FILLORDER_LSB2MSB)
3251 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003252 image=SyncNextImageInList(image);
3253 if (image == (Image *) NULL)
3254 break;
3255 status=SetImageProgress(image,SaveImagesTag,scene++,
3256 GetImageListLength(image));
3257 if (status == MagickFalse)
3258 break;
3259 } while (image_info->adjoin != MagickFalse);
3260 (void) TIFFSetWarningHandler(warning_handler);
3261 (void) TIFFSetErrorHandler(error_handler);
3262 TIFFClose(tiff);
3263 return(MagickTrue);
3264}
3265#endif