blob: 3769b669f6c403e8655e36fe880c66c343d0900a [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*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/attribute.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/blob-private.h"
46#include "MagickCore/cache.h"
47#include "MagickCore/color.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colormap.h"
50#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/constitute.h"
53#include "MagickCore/enhance.h"
54#include "MagickCore/exception.h"
55#include "MagickCore/exception-private.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image.h"
58#include "MagickCore/image-private.h"
59#include "MagickCore/list.h"
60#include "MagickCore/log.h"
61#include "MagickCore/magick.h"
62#include "MagickCore/memory_.h"
63#include "MagickCore/module.h"
64#include "MagickCore/monitor.h"
65#include "MagickCore/monitor-private.h"
66#include "MagickCore/option.h"
67#include "MagickCore/pixel-accessor.h"
68#include "MagickCore/property.h"
69#include "MagickCore/quantum.h"
70#include "MagickCore/quantum-private.h"
71#include "MagickCore/profile.h"
72#include "MagickCore/resize.h"
73#include "MagickCore/resource_.h"
74#include "MagickCore/semaphore.h"
75#include "MagickCore/splay-tree.h"
76#include "MagickCore/static.h"
77#include "MagickCore/statistic.h"
78#include "MagickCore/string_.h"
79#include "MagickCore/string-private.h"
80#include "MagickCore/thread_.h"
81#include "MagickCore/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000082#if defined(MAGICKCORE_TIFF_DELEGATE)
83# if defined(MAGICKCORE_HAVE_TIFFCONF_H)
84# include "tiffconf.h"
cristyef8f26b2010-12-19 20:29:16 +000085# endif
cristy3ed852e2009-09-05 21:47:34 +000086# include "tiff.h"
87# include "tiffio.h"
88# if !defined(COMPRESSION_ADOBE_DEFLATE)
89# define COMPRESSION_ADOBE_DEFLATE 8
90# endif
cristyef8f26b2010-12-19 20:29:16 +000091# if !defined(PREDICTOR_HORIZONTAL)
92# define PREDICTOR_HORIZONTAL 2
93# endif
cristy3ed852e2009-09-05 21:47:34 +000094
95/*
96 Typedef declarations.
97*/
98#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
99typedef struct _ExifInfo
100{
101 unsigned int
102 tag,
103 type;
104
105 const char
106 *property;
107} ExifInfo;
108
109static const ExifInfo
110 exif_info[] = {
111 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
112 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
113 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
114 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000115 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy7992e402011-09-16 15:24:22 +0000116 { EXIFTAG_OECF, TIFF_NOTYPE, "exif:OptoelectricConversionFactor" },
117 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, "exif:ExifVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000118 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
119 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000120 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, "exif:ComponentsConfiguration" },
cristy3ed852e2009-09-05 21:47:34 +0000121 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
122 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
123 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
124 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
125 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
126 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
127 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
128 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
129 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
130 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
131 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
cristy7992e402011-09-16 15:24:22 +0000132 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, "exif:SubjectArea" },
133 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, "exif:MakerNote" },
134 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, "exif:UserComment" },
cristy3ed852e2009-09-05 21:47:34 +0000135 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
136 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
137 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000138 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, "exif:FlashpixVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000139 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
140 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
141 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
142 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
143 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
144 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
cristy7992e402011-09-16 15:24:22 +0000145 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, "exif:SpatialFrequencyResponse" },
cristy3ed852e2009-09-05 21:47:34 +0000146 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
147 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
148 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
149 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
150 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
151 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
cristy7992e402011-09-16 15:24:22 +0000152 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, "exif:FileSource" },
153 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, "exif:SceneType" },
154 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, "exif:CFAPattern" },
cristy3ed852e2009-09-05 21:47:34 +0000155 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
156 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
157 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
158 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
159 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
160 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
161 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
162 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
163 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
164 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
cristy7992e402011-09-16 15:24:22 +0000165 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, "exif:DeviceSettingDescription" },
cristy3ed852e2009-09-05 21:47:34 +0000166 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
167 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
168 { 0, 0, (char *) NULL }
169};
170#endif
cristy0805e062011-01-30 02:56:36 +0000171#endif /* MAGICKCORE_TIFF_DELEGATE */
cristy3ed852e2009-09-05 21:47:34 +0000172
173/*
174 Global declarations.
175*/
176static MagickThreadKey
177 tiff_exception;
178
179static SemaphoreInfo
180 *tiff_semaphore = (SemaphoreInfo *) NULL;
181
182static volatile MagickBooleanType
183 instantiate_key = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000184
185/*
186 Forward declarations.
187*/
188#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000189static Image *
190 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
191
cristy3ed852e2009-09-05 21:47:34 +0000192static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +0000193 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
194 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
195 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000196#endif
197
198/*
199%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200% %
201% %
202% %
203% I s T I F F %
204% %
205% %
206% %
207%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208%
209% IsTIFF() returns MagickTrue if the image format type, identified by the
210% magick string, is TIFF.
211%
212% The format of the IsTIFF method is:
213%
214% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
215%
216% A description of each parameter follows:
217%
218% o magick: compare image format pattern against these bytes.
219%
220% o length: Specifies the length of the magick string.
221%
222*/
223static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
224{
225 if (length < 4)
226 return(MagickFalse);
227 if (memcmp(magick,"\115\115\000\052",4) == 0)
228 return(MagickTrue);
229 if (memcmp(magick,"\111\111\052\000",4) == 0)
230 return(MagickTrue);
231#if defined(TIFF_VERSION_BIG)
232 if (length < 8)
233 return(MagickFalse);
234 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
235 return(MagickTrue);
236 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
237 return(MagickTrue);
238#endif
239 return(MagickFalse);
240}
241
242#if defined(MAGICKCORE_TIFF_DELEGATE)
243/*
244%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245% %
246% %
247% %
cristy3d7f8062009-09-24 20:45:35 +0000248% R e a d G R O U P 4 I m a g e %
249% %
250% %
251% %
252%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
253%
254% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
255% allocates the memory necessary for the new Image structure and returns a
256% pointer to the new image.
257%
258% The format of the ReadGROUP4Image method is:
259%
260% Image *ReadGROUP4Image(const ImageInfo *image_info,
261% ExceptionInfo *exception)
262%
263% A description of each parameter follows:
264%
265% o image_info: the image info.
266%
267% o exception: return any errors or warnings in this structure.
268%
269*/
270
cristyeaedf062010-05-29 22:36:02 +0000271static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000272{
273 unsigned char
274 buffer[4];
275
276 buffer[0]=(unsigned char) value;
277 buffer[1]=(unsigned char) (value >> 8);
278 buffer[2]=(unsigned char) (value >> 16);
279 buffer[3]=(unsigned char) (value >> 24);
280 return(fwrite(buffer,1,4,file));
281}
282
283static Image *ReadGROUP4Image(const ImageInfo *image_info,
284 ExceptionInfo *exception)
285{
286 char
287 filename[MaxTextExtent];
288
289 FILE
290 *file;
291
292 Image
293 *image;
294
295 ImageInfo
296 *read_info;
297
298 int
299 c,
300 unique_file;
301
302 MagickBooleanType
303 status;
304
305 size_t
306 length;
307
308 ssize_t
309 offset,
310 strip_offset;
311
312 /*
313 Open image file.
314 */
315 assert(image_info != (const ImageInfo *) NULL);
316 assert(image_info->signature == MagickSignature);
317 if (image_info->debug != MagickFalse)
318 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
319 image_info->filename);
320 assert(exception != (ExceptionInfo *) NULL);
321 assert(exception->signature == MagickSignature);
322 image=AcquireImage(image_info);
323 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
324 if (status == MagickFalse)
325 {
326 image=DestroyImageList(image);
327 return((Image *) NULL);
328 }
329 /*
330 Write raw CCITT Group 4 wrapped as a TIFF image file.
331 */
332 file=(FILE *) NULL;
333 unique_file=AcquireUniqueFileResource(filename);
334 if (unique_file != -1)
335 file=fdopen(unique_file,"wb");
336 if ((unique_file == -1) || (file == (FILE *) NULL))
337 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
338 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
339 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
340 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
341 length=WriteLSBLong(file,image->columns);
342 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
343 length=WriteLSBLong(file,image->rows);
344 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
345 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
346 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
347 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
348 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000349 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000350 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000351 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000352 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
353 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000354 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000355 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
356 offset=(ssize_t) ftell(file)-4;
357 length=fwrite("\032\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("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000360 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000361 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
362 length=fwrite("\000\000\000\000",1,4,file);
cristy94b11832011-09-08 19:46:03 +0000363 length=WriteLSBLong(file,(long) image->x_resolution);
cristy3d7f8062009-09-24 20:45:35 +0000364 length=WriteLSBLong(file,1);
365 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
366 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000367 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000368 length=WriteLSBLong(file,(unsigned int) length);
369 (void) fclose(file);
370 (void) CloseBlob(image);
371 image=DestroyImage(image);
372 /*
373 Read TIFF image.
374 */
cristy14efd992009-09-26 23:46:03 +0000375 read_info=CloneImageInfo((ImageInfo *) NULL);
cristyb51dff52011-05-19 16:55:47 +0000376 (void) FormatLocaleString(read_info->filename,MaxTextExtent,"%s",filename);
cristy3d7f8062009-09-24 20:45:35 +0000377 image=ReadTIFFImage(read_info,exception);
378 read_info=DestroyImageInfo(read_info);
379 if (image != (Image *) NULL)
380 {
381 (void) CopyMagickString(image->filename,image_info->filename,
382 MaxTextExtent);
383 (void) CopyMagickString(image->magick_filename,image_info->filename,
384 MaxTextExtent);
385 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
386 }
387 (void) RelinquishUniqueFileResource(filename);
388 return(image);
389}
390#endif
391
392#if defined(MAGICKCORE_TIFF_DELEGATE)
393/*
394%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395% %
396% %
397% %
cristy3ed852e2009-09-05 21:47:34 +0000398% R e a d T I F F I m a g e %
399% %
400% %
401% %
402%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403%
404% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
405% memory necessary for the new Image structure and returns a pointer to the
406% new image.
407%
408% The format of the ReadTIFFImage method is:
409%
410% Image *ReadTIFFImage(const ImageInfo *image_info,
411% ExceptionInfo *exception)
412%
413% A description of each parameter follows:
414%
415% o image_info: the image info.
416%
417% o exception: return any errors or warnings in this structure.
418%
419*/
420
421static inline size_t MagickMax(const size_t x,const size_t y)
422{
423 if (x > y)
424 return(x);
425 return(y);
426}
427
cristybb503372010-05-27 20:51:26 +0000428static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000429{
430 if (x < y)
431 return(x);
432 return(y);
433}
434
435static MagickBooleanType ReadProfile(Image *image,const char *name,
cristy018f07f2011-09-04 21:15:19 +0000436 unsigned char *datum,ssize_t length,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000437{
438 MagickBooleanType
439 status;
440
cristybb503372010-05-27 20:51:26 +0000441 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000442 i;
443
444 StringInfo
445 *profile;
446
447 if (length < 4)
448 return(MagickFalse);
449 i=0;
450 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
451 {
452 for (i=0; i < (length-4); i+=2)
453 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
454 break;
455 if (i == length)
456 length-=i;
457 else
458 i=0;
459 if (length < 4)
460 return(MagickFalse);
461 }
cristye8f8f382011-09-01 13:32:37 +0000462 profile=BlobToStringInfo(datum+i,(size_t) length);
463 if (profile == (StringInfo *) NULL)
464 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
465 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000466 status=SetImageProfile(image,name,profile);
467 profile=DestroyStringInfo(profile);
468 if (status == MagickFalse)
469 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
470 image->filename);
471 return(MagickTrue);
472}
473
474#if defined(__cplusplus) || defined(c_plusplus)
475extern "C" {
476#endif
477
478static int TIFFCloseBlob(thandle_t image)
479{
480 (void) CloseBlob((Image *) image);
481 return(0);
482}
483
484static void TIFFErrors(const char *module,const char *format,va_list error)
485{
486 char
487 message[MaxTextExtent];
488
489 ExceptionInfo
490 *exception;
491
492#if defined(MAGICKCORE_HAVE_VSNPRINTF)
493 (void) vsnprintf(message,MaxTextExtent,format,error);
494#else
495 (void) vsprintf(message,format,error);
496#endif
497 (void) ConcatenateMagickString(message,".",MaxTextExtent);
498 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
499 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000500 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
501 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000502}
503
cristy018f07f2011-09-04 21:15:19 +0000504static void TIFFGetProfiles(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000505{
506 uint32
507 length;
508
509 unsigned char
510 *profile;
511
512#if defined(TIFFTAG_ICCPROFILE)
513 length=0;
514 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000515 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000516#endif
517#if defined(TIFFTAG_PHOTOSHOP)
518 length=0;
519 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000520 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000521#endif
522#if defined(TIFFTAG_RICHTIFFIPTC)
523 length=0;
524 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
525 {
526 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000527 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy018f07f2011-09-04 21:15:19 +0000528 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000529 }
530#endif
531#if defined(TIFFTAG_XMLPACKET)
532 length=0;
533 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000534 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000535#endif
536 length=0;
537 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000538 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000539}
540
541static void TIFFGetProperties(TIFF *tiff,Image *image)
542{
543 char
544 *text;
545
546 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
547 (void) SetImageProperty(image,"tiff:artist",text);
548 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
549 (void) SetImageProperty(image,"tiff:timestamp",text);
550 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
551 (void) SetImageProperty(image,"tiff:software",text);
552 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
553 (void) SetImageProperty(image,"tiff:hostcomputer",text);
554 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
555 (void) SetImageProperty(image,"tiff:document",text);
556 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
557 (void) SetImageProperty(image,"tiff:make",text);
558 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
559 (void) SetImageProperty(image,"tiff:model",text);
560 if (TIFFGetField(tiff,33432,&text) == 1)
561 (void) SetImageProperty(image,"tiff:copyright",text);
562 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
563 (void) SetImageProperty(image,"label",text);
564 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
565 (void) SetImageProperty(image,"comment",text);
566}
567
568static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
569{
570#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
571 char
572 value[MaxTextExtent];
573
cristybb503372010-05-27 20:51:26 +0000574 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000575 i;
576
577 tdir_t
578 directory;
579
580 uint32
581 offset;
582
cristy7992e402011-09-16 15:24:22 +0000583 void
584 *sans;
585
cristy3ed852e2009-09-05 21:47:34 +0000586 /*
587 Read EXIF properties.
588 */
589 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
590 return;
591 directory=TIFFCurrentDirectory(tiff);
592 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
593 return;
cristy7992e402011-09-16 15:24:22 +0000594 sans=NULL;
cristy3ed852e2009-09-05 21:47:34 +0000595 for (i=0; exif_info[i].tag != 0; i++)
596 {
597 *value='\0';
598 switch (exif_info[i].type)
599 {
600 case TIFF_ASCII:
601 {
602 char
603 *ascii;
604
cristy7992e402011-09-16 15:24:22 +0000605 ascii=(char *) NULL;
606 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans) != 0) &&
607 (ascii != (char *) NULL) && (*ascii != '\0'))
cristy3ed852e2009-09-05 21:47:34 +0000608 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
609 break;
610 }
611 case TIFF_SHORT:
612 {
613 uint16
cristy7992e402011-09-16 15:24:22 +0000614 shorty[2] = { 0, 0};
cristy3ed852e2009-09-05 21:47:34 +0000615
cristy7992e402011-09-16 15:24:22 +0000616 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans) != 0)
617 (void) FormatLocaleString(value,MaxTextExtent,"%d",(int) shorty[0]);
cristy3ed852e2009-09-05 21:47:34 +0000618 break;
619 }
620 case TIFF_LONG:
621 {
622 uint32
cristy7992e402011-09-16 15:24:22 +0000623 longy;
cristy3ed852e2009-09-05 21:47:34 +0000624
cristy7992e402011-09-16 15:24:22 +0000625 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans) != 0)
626 (void) FormatLocaleString(value,MaxTextExtent,"%d",longy);
cristy3ed852e2009-09-05 21:47:34 +0000627 break;
628 }
629 case TIFF_RATIONAL:
630 case TIFF_SRATIONAL:
cristy7992e402011-09-16 15:24:22 +0000631 case TIFF_FLOAT:
632 case TIFF_DOUBLE:
cristy3ed852e2009-09-05 21:47:34 +0000633 {
634 float
cristy7992e402011-09-16 15:24:22 +0000635 rational[16];
cristy3ed852e2009-09-05 21:47:34 +0000636
cristy7992e402011-09-16 15:24:22 +0000637 if (TIFFGetField(tiff,exif_info[i].tag,&rational,&sans) != 0)
638 (void) FormatLocaleString(value,MaxTextExtent,"%g",rational[0]);
cristy3ed852e2009-09-05 21:47:34 +0000639 break;
640 }
641 default:
642 break;
643 }
644 if (*value != '\0')
645 (void) SetImageProperty(image,exif_info[i].property,value);
646 }
647 TIFFSetDirectory(tiff,directory);
648#else
649 (void) tiff;
650 (void) image;
651#endif
652}
653
654static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
655{
656 *base=(tdata_t *) GetBlobStreamData((Image *) image);
657 if (*base != (tdata_t *) NULL)
658 *size=(toff_t) GetBlobSize((Image *) image);
659 if (*base != (tdata_t *) NULL)
660 return(1);
661 return(0);
662}
663
664static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
665{
666 tsize_t
667 count;
668
669 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
670 (unsigned char *) data);
671 return(count);
672}
673
cristybb503372010-05-27 20:51:26 +0000674static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
675 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000676{
677 int32
678 status;
679
680 (void) bits_per_sample;
681 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
682 return(status);
683}
684
685static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
686{
687 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
688}
689
690static toff_t TIFFGetBlobSize(thandle_t image)
691{
692 return((toff_t) GetBlobSize((Image *) image));
693}
694
695static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
696{
697 (void) image;
698 (void) base;
699 (void) size;
700}
701
702static void TIFFWarnings(const char *module,const char *format,va_list warning)
703{
704 char
705 message[MaxTextExtent];
706
707 ExceptionInfo
708 *exception;
709
710#if defined(MAGICKCORE_HAVE_VSNPRINTF)
711 (void) vsnprintf(message,MaxTextExtent,format,warning);
712#else
713 (void) vsprintf(message,format,warning);
714#endif
715 (void) ConcatenateMagickString(message,".",MaxTextExtent);
716 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
717 if (exception != (ExceptionInfo *) NULL)
718 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
719 message,"`%s'",module);
720}
721
722static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
723{
724 tsize_t
725 count;
726
727 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
728 (unsigned char *) data);
729 return(count);
730}
731
732#if defined(__cplusplus) || defined(c_plusplus)
733}
734#endif
735
736static Image *ReadTIFFImage(const ImageInfo *image_info,
737 ExceptionInfo *exception)
738{
739 typedef enum
740 {
741 ReadSingleSampleMethod,
742 ReadRGBAMethod,
743 ReadCMYKAMethod,
744 ReadStripMethod,
745 ReadTileMethod,
746 ReadGenericMethod
747 } TIFFMethodType;
748
749 const char
750 *option;
751
752 float
753 *chromaticity,
754 x_position,
755 y_position,
756 x_resolution,
757 y_resolution;
758
759 Image
760 *image;
761
cristy3ed852e2009-09-05 21:47:34 +0000762 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000763 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000764 debug,
765 status;
766
767 MagickSizeType
768 number_pixels;
769
770 QuantumInfo
771 *quantum_info;
772
773 QuantumType
774 quantum_type;
775
cristybb503372010-05-27 20:51:26 +0000776 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000777 i;
778
779 size_t
780 length,
cristyc6da28e2011-04-28 01:41:35 +0000781 lsb_first,
cristy3ed852e2009-09-05 21:47:34 +0000782 pad;
783
cristyc6da28e2011-04-28 01:41:35 +0000784 ssize_t
785 y;
786
cristy3ed852e2009-09-05 21:47:34 +0000787 TIFF
788 *tiff;
789
790 TIFFErrorHandler
791 error_handler,
792 warning_handler;
793
794 TIFFMethodType
795 method;
796
797 uint16
798 compress_tag,
799 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000800 endian,
cristy3ed852e2009-09-05 21:47:34 +0000801 extra_samples,
802 interlace,
803 max_sample_value,
804 min_sample_value,
805 orientation,
806 pages,
807 photometric,
808 *sample_info,
809 sample_format,
810 samples_per_pixel,
811 units,
812 value;
813
814 uint32
815 height,
816 rows_per_strip,
817 width;
818
819 unsigned char
820 *pixels;
821
cristy3ed852e2009-09-05 21:47:34 +0000822 /*
823 Open image.
824 */
825 assert(image_info != (const ImageInfo *) NULL);
826 assert(image_info->signature == MagickSignature);
827 if (image_info->debug != MagickFalse)
828 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
829 image_info->filename);
830 assert(exception != (ExceptionInfo *) NULL);
831 assert(exception->signature == MagickSignature);
832 image=AcquireImage(image_info);
833 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
834 if (status == MagickFalse)
835 {
836 image=DestroyImageList(image);
837 return((Image *) NULL);
838 }
839 (void) MagickSetThreadValue(tiff_exception,exception);
840 error_handler=TIFFSetErrorHandler(TIFFErrors);
841 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
cristy1630f7f2011-02-18 01:10:02 +0000842 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
cristy3ed852e2009-09-05 21:47:34 +0000843 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
844 TIFFUnmapBlob);
845 if (tiff == (TIFF *) NULL)
846 {
847 (void) TIFFSetWarningHandler(warning_handler);
848 (void) TIFFSetErrorHandler(error_handler);
849 image=DestroyImageList(image);
850 return((Image *) NULL);
851 }
852 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +0000853 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +0000854 if (image_info->number_scenes != 0)
855 {
856 /*
857 Generate blank images for subimage specification (e.g. image.tif[4].
858 */
cristybb503372010-05-27 20:51:26 +0000859 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000860 {
861 (void) TIFFReadDirectory(tiff);
862 AcquireNextImage(image_info,image);
863 if (GetNextImageInList(image) == (Image *) NULL)
864 {
865 image=DestroyImageList(image);
866 return((Image *) NULL);
867 }
868 image=SyncNextImageInList(image);
869 }
870 }
871 do
872 {
873 if (0 && (image_info->verbose != MagickFalse))
874 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristy6b032822010-06-29 16:52:32 +0000875 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
876 "lsb" : "msb");
cristy3ed852e2009-09-05 21:47:34 +0000877 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
878 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
879 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
880 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000881 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000882 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
883 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
884 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
cristyae1319c2011-01-19 20:18:53 +0000885 if (sample_format == SAMPLEFORMAT_IEEEFP)
886 (void) SetImageProperty(image,"quantum:format","floating-point");
cristy3ed852e2009-09-05 21:47:34 +0000887 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
888 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
889 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
890 switch (photometric)
891 {
892 case PHOTOMETRIC_MINISBLACK:
893 {
894 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
895 break;
896 }
897 case PHOTOMETRIC_MINISWHITE:
898 {
899 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
900 break;
901 }
902 case PHOTOMETRIC_PALETTE:
903 {
904 (void) SetImageProperty(image,"tiff:photometric","palette");
905 break;
906 }
907 case PHOTOMETRIC_RGB:
908 {
909 (void) SetImageProperty(image,"tiff:photometric","RGB");
910 break;
911 }
912 case PHOTOMETRIC_CIELAB:
913 {
914 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
915 break;
916 }
917 case PHOTOMETRIC_SEPARATED:
918 {
919 (void) SetImageProperty(image,"tiff:photometric","separated");
920 break;
921 }
cristy79e5dad2010-09-16 19:48:33 +0000922 case PHOTOMETRIC_YCBCR:
923 {
924 (void) SetImageProperty(image,"tiff:photometric","YCBCR");
925 break;
926 }
cristy3ed852e2009-09-05 21:47:34 +0000927 default:
928 {
929 (void) SetImageProperty(image,"tiff:photometric","unknown");
930 break;
931 }
932 }
933 if (image->debug != MagickFalse)
934 {
935 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
936 (unsigned int) width,(unsigned int) height);
937 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
938 interlace);
939 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
940 "Bits per sample: %u",bits_per_sample);
941 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
942 "Min sample value: %u",min_sample_value);
943 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
944 "Max sample value: %u",max_sample_value);
945 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
946 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
947 }
cristybb503372010-05-27 20:51:26 +0000948 image->columns=(size_t) width;
949 image->rows=(size_t) height;
950 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +0000951 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +0000952 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
953 (double) image->depth);
cristyf2687ca2010-06-29 16:32:38 +0000954 lsb_first=1;
955 image->endian=MSBEndian;
956 if ((int) (*(char *) &lsb_first) != 0)
957 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +0000958 if (photometric == PHOTOMETRIC_SEPARATED)
959 image->colorspace=CMYKColorspace;
960 if (photometric == PHOTOMETRIC_CIELAB)
961 image->colorspace=LabColorspace;
962 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
963 &samples_per_pixel);
964 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
965 x_resolution=(float) image->x_resolution;
966 y_resolution=(float) image->y_resolution;
967 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
968 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
969 image->x_resolution=x_resolution;
970 image->y_resolution=y_resolution;
971 x_position=(float) image->page.x/x_resolution;
972 y_position=(float) image->page.y/y_resolution;
973 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
974 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +0000975 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
976 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000977 image->orientation=(OrientationType) orientation;
978 chromaticity=(float *) NULL;
979 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
980 if (chromaticity != (float *) NULL)
981 {
982 image->chromaticity.white_point.x=chromaticity[0];
983 image->chromaticity.white_point.y=chromaticity[1];
984 }
985 chromaticity=(float *) NULL;
986 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
987 if (chromaticity != (float *) NULL)
988 {
989 image->chromaticity.red_primary.x=chromaticity[0];
990 image->chromaticity.red_primary.y=chromaticity[1];
991 image->chromaticity.green_primary.x=chromaticity[2];
992 image->chromaticity.green_primary.y=chromaticity[3];
993 image->chromaticity.blue_primary.x=chromaticity[4];
994 image->chromaticity.blue_primary.y=chromaticity[5];
995 }
996 TIFFGetProperties(tiff,image);
997 option=GetImageOption(image_info,"tiff:exif");
998 if ((option != (const char *) NULL) &&
999 (IsMagickTrue(option) != MagickFalse))
1000 TIFFGetEXIFProperties(tiff,image);
cristy018f07f2011-09-04 21:15:19 +00001001 TIFFGetProfiles(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001002 /*
1003 Allocate memory for the image and pixel buffer.
1004 */
1005#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1006 if ((compress_tag != COMPRESSION_NONE) &&
1007 (TIFFIsCODECConfigured(compress_tag) == 0))
1008 {
1009 TIFFClose(tiff);
1010 ThrowReaderException(CoderError,"CompressNotSupported");
1011 }
1012#endif
1013 switch (compress_tag)
1014 {
1015 case COMPRESSION_NONE: image->compression=NoCompression; break;
1016 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1017 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1018 case COMPRESSION_JPEG:
1019 {
1020 image->compression=JPEGCompression;
1021#if defined(JPEG_SUPPORT)
1022 {
1023 char
1024 sampling_factor[MaxTextExtent];
1025
1026 uint16
1027 horizontal,
1028 vertical;
1029
1030 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1031 &horizontal,&vertical);
cristyb51dff52011-05-19 16:55:47 +00001032 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
cristy3ed852e2009-09-05 21:47:34 +00001033 horizontal,vertical);
1034 (void) SetImageProperty(image,"jpeg:sampling-factor",
1035 sampling_factor);
1036 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1037 "Sampling Factors: %s",sampling_factor);
1038 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1039 {
1040 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1041 JPEGCOLORMODE_RGB);
1042 photometric=PHOTOMETRIC_RGB;
1043 }
1044 }
1045#endif
1046 break;
1047 }
1048 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001049#if defined(COMPRESSION_LZMA)
1050 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1051#endif
cristy3ed852e2009-09-05 21:47:34 +00001052 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1053 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1054 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1055 default: image->compression=RLECompression; break;
1056 }
1057 quantum_info=AcquireQuantumInfo(image_info,image);
1058 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001059 {
1060 TIFFClose(tiff);
1061 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1062 }
cristy3ed852e2009-09-05 21:47:34 +00001063 if (sample_format == SAMPLEFORMAT_UINT)
1064 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1065 if (sample_format == SAMPLEFORMAT_INT)
1066 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1067 if (sample_format == SAMPLEFORMAT_IEEEFP)
1068 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1069 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001070 {
1071 TIFFClose(tiff);
1072 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1073 }
cristy3ed852e2009-09-05 21:47:34 +00001074 status=MagickTrue;
1075 switch (photometric)
1076 {
1077 case PHOTOMETRIC_MINISBLACK:
1078 {
1079 quantum_info->min_is_white=MagickFalse;
1080 break;
1081 }
1082 case PHOTOMETRIC_MINISWHITE:
1083 {
1084 quantum_info->min_is_white=MagickTrue;
1085 break;
1086 }
1087 default:
1088 break;
1089 }
cristy891dc792010-03-04 01:47:16 +00001090 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001091 extra_samples=0;
1092 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1093 &sample_info);
1094 if (extra_samples == 0)
1095 {
1096 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1097 image->matte=MagickTrue;
1098 }
1099 else
cristy6c207342010-02-19 17:49:50 +00001100 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001101 {
cristy7cf69862010-03-03 13:57:24 +00001102 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001103 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001104 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001105 }
cristy891dc792010-03-04 01:47:16 +00001106 option=GetImageOption(image_info,"tiff:alpha");
1107 if (option != (const char *) NULL)
1108 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1109 MagickFalse;
1110 if (image->matte != MagickFalse)
1111 (void) SetImageProperty(image,"tiff:alpha",
1112 associated_alpha != MagickFalse ? "associated" : "unassociated");
cristy3ed852e2009-09-05 21:47:34 +00001113 if ((photometric == PHOTOMETRIC_PALETTE) &&
1114 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1115 {
cristybb503372010-05-27 20:51:26 +00001116 size_t
cristy3ed852e2009-09-05 21:47:34 +00001117 colors;
1118
cristybb503372010-05-27 20:51:26 +00001119 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy018f07f2011-09-04 21:15:19 +00001120 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001121 {
1122 TIFFClose(tiff);
1123 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1124 }
1125 }
1126 if (units == RESUNIT_INCH)
1127 image->units=PixelsPerInchResolution;
1128 if (units == RESUNIT_CENTIMETER)
1129 image->units=PixelsPerCentimeterResolution;
1130 value=(unsigned short) image->scene;
1131 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1132 image->scene=value;
1133 if (image_info->ping != MagickFalse)
1134 {
1135 if (image_info->number_scenes != 0)
1136 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1137 break;
1138 goto next_tiff_frame;
1139 }
1140 method=ReadGenericMethod;
1141 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1142 {
1143 char
1144 value[MaxTextExtent];
1145
1146 method=ReadStripMethod;
cristyb51dff52011-05-19 16:55:47 +00001147 (void) FormatLocaleString(value,MaxTextExtent,"%u",
cristy3ed852e2009-09-05 21:47:34 +00001148 (unsigned int) rows_per_strip);
1149 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1150 }
1151 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1152 method=ReadRGBAMethod;
1153 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1154 method=ReadCMYKAMethod;
1155 if ((photometric != PHOTOMETRIC_RGB) &&
1156 (photometric != PHOTOMETRIC_CIELAB) &&
1157 (photometric != PHOTOMETRIC_SEPARATED))
1158 method=ReadGenericMethod;
1159 if (image->storage_class == PseudoClass)
1160 method=ReadSingleSampleMethod;
1161 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1162 (photometric == PHOTOMETRIC_MINISWHITE))
1163 method=ReadSingleSampleMethod;
1164 if ((photometric != PHOTOMETRIC_SEPARATED) &&
cristybc736662011-03-09 13:39:42 +00001165 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
cristy3ed852e2009-09-05 21:47:34 +00001166 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001167 if (image->compression == JPEGCompression)
1168 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001169 if (TIFFIsTiled(tiff) != MagickFalse)
1170 method=ReadTileMethod;
1171 quantum_type=RGBQuantum;
1172 pixels=GetQuantumPixels(quantum_info);
1173 switch (method)
1174 {
1175 case ReadSingleSampleMethod:
1176 {
1177 /*
1178 Convert TIFF image to PseudoClass MIFF image.
1179 */
1180 if ((image->storage_class == PseudoClass) &&
1181 (photometric == PHOTOMETRIC_PALETTE))
1182 {
cristybb503372010-05-27 20:51:26 +00001183 size_t
cristy3ed852e2009-09-05 21:47:34 +00001184 range;
1185
1186 uint16
1187 *blue_colormap,
1188 *green_colormap,
1189 *red_colormap;
1190
1191 /*
1192 Initialize colormap.
1193 */
1194 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1195 &green_colormap,&blue_colormap);
1196 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001197 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001198 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1199 (blue_colormap[i] >= 256))
1200 {
1201 range=65535;
1202 break;
1203 }
cristybb503372010-05-27 20:51:26 +00001204 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001205 {
cristyce70c172010-01-07 17:15:30 +00001206 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001207 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001208 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001209 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001210 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001211 blue_colormap[i])/range);
1212 }
1213 }
1214 quantum_type=IndexQuantum;
1215 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1216 if (image->matte != MagickFalse)
1217 {
1218 if (image->storage_class != PseudoClass)
1219 {
1220 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1221 GrayAlphaQuantum;
1222 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1223 }
1224 else
1225 {
1226 quantum_type=IndexAlphaQuantum;
1227 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1228 }
1229 }
1230 else
1231 if (image->storage_class != PseudoClass)
1232 {
1233 quantum_type=GrayQuantum;
1234 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1235 }
1236 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1237 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001238 {
1239 TIFFClose(tiff);
1240 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1241 }
cristy3ed852e2009-09-05 21:47:34 +00001242 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001243 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001244 {
1245 int
1246 status;
1247
cristy4c08aed2011-07-01 19:47:50 +00001248 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001249 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001250
1251 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1252 if (status == -1)
1253 break;
1254 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001255 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001256 break;
1257 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1258 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00001259 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00001260 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1261 break;
1262 if (image->previous == (Image *) NULL)
1263 {
cristycee97112010-05-28 00:44:52 +00001264 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1265 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001266 if (status == MagickFalse)
1267 break;
1268 }
1269 }
1270 break;
1271 }
1272 case ReadRGBAMethod:
1273 {
1274 /*
1275 Convert TIFF image to DirectClass MIFF image.
1276 */
1277 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1278 quantum_type=RGBQuantum;
1279 if (image->matte != MagickFalse)
1280 {
1281 quantum_type=RGBAQuantum;
1282 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1283 }
1284 if (image->colorspace == CMYKColorspace)
1285 {
1286 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1287 quantum_type=CMYKQuantum;
1288 if (image->matte != MagickFalse)
1289 {
1290 quantum_type=CMYKAQuantum;
1291 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1292 }
1293 }
1294 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1295 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001296 {
1297 TIFFClose(tiff);
1298 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1299 }
cristy3ed852e2009-09-05 21:47:34 +00001300 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001301 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001302 {
1303 int
1304 status;
1305
cristy4c08aed2011-07-01 19:47:50 +00001306 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001307 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001308
1309 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1310 if (status == -1)
1311 break;
1312 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001313 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001314 break;
cristy0b334892010-03-15 02:26:46 +00001315 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1316 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001317 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1318 break;
1319 if (image->previous == (Image *) NULL)
1320 {
cristycee97112010-05-28 00:44:52 +00001321 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1322 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001323 if (status == MagickFalse)
1324 break;
1325 }
1326 }
1327 break;
1328 }
1329 case ReadCMYKAMethod:
1330 {
1331 /*
1332 Convert TIFF image to DirectClass MIFF image.
1333 */
cristybb503372010-05-27 20:51:26 +00001334 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001335 {
cristybb503372010-05-27 20:51:26 +00001336 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001337 {
cristy4c08aed2011-07-01 19:47:50 +00001338 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001339 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001340
cristy3ed852e2009-09-05 21:47:34 +00001341 int
1342 status;
1343
1344 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1345 pixels);
1346 if (status == -1)
1347 break;
1348 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001349 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001350 break;
1351 if (image->colorspace != CMYKColorspace)
1352 switch (i)
1353 {
1354 case 0: quantum_type=RedQuantum; break;
1355 case 1: quantum_type=GreenQuantum; break;
1356 case 2: quantum_type=BlueQuantum; break;
1357 case 3: quantum_type=AlphaQuantum; break;
1358 default: quantum_type=UndefinedQuantum; break;
1359 }
cristy6d9f12f2009-11-03 14:50:26 +00001360 else
cristy3ed852e2009-09-05 21:47:34 +00001361 switch (i)
1362 {
1363 case 0: quantum_type=CyanQuantum; break;
1364 case 1: quantum_type=MagentaQuantum; break;
1365 case 2: quantum_type=YellowQuantum; break;
1366 case 3: quantum_type=BlackQuantum; break;
1367 case 4: quantum_type=AlphaQuantum; break;
1368 default: quantum_type=UndefinedQuantum; break;
1369 }
1370 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1371 quantum_type,pixels,exception);
1372 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1373 break;
1374 }
1375 if (image->previous == (Image *) NULL)
1376 {
cristycee97112010-05-28 00:44:52 +00001377 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1378 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001379 if (status == MagickFalse)
1380 break;
1381 }
1382 }
1383 break;
1384 }
1385 case ReadStripMethod:
1386 {
1387 register unsigned char
1388 *p;
1389
1390 /*
1391 Convert stripped TIFF image to DirectClass MIFF image.
1392 */
1393 i=0;
1394 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001395 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001396 {
cristybb503372010-05-27 20:51:26 +00001397 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001398 x;
1399
cristy4c08aed2011-07-01 19:47:50 +00001400 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001401 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001402
1403 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001404 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001405 break;
1406 if (i == 0)
1407 {
1408 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1409 break;
cristyeaedf062010-05-29 22:36:02 +00001410 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1411 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001412 }
1413 i--;
cristy7f5d1662011-04-14 12:43:28 +00001414 p=(unsigned char *) (((uint32 *) pixels)+image->columns*i);
cristybb503372010-05-27 20:51:26 +00001415 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001416 {
cristy4c08aed2011-07-01 19:47:50 +00001417 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1418 (TIFFGetR(*p))),q);
1419 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1420 (TIFFGetG(*p))),q);
1421 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1422 (TIFFGetB(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001423 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001424 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1425 (TIFFGetA(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001426 p++;
cristyed231572011-07-14 02:18:59 +00001427 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001428 }
1429 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1430 break;
1431 if (image->previous == (Image *) NULL)
1432 {
cristycee97112010-05-28 00:44:52 +00001433 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1434 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001435 if (status == MagickFalse)
1436 break;
1437 }
1438 }
1439 break;
1440 }
1441 case ReadTileMethod:
1442 {
1443 register uint32
1444 *p;
1445
1446 uint32
1447 *tile_pixels,
1448 columns,
1449 rows;
1450
cristybb503372010-05-27 20:51:26 +00001451 size_t
cristy3ed852e2009-09-05 21:47:34 +00001452 number_pixels;
1453
1454 /*
1455 Convert tiled TIFF image to DirectClass MIFF image.
1456 */
1457 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1458 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1459 {
1460 TIFFClose(tiff);
1461 ThrowReaderException(CoderError,"ImageIsNotTiled");
1462 }
cristy574cc262011-08-05 01:23:58 +00001463 (void) SetImageStorageClass(image,DirectClass,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001464 number_pixels=columns*rows;
cristyda16f162011-02-19 23:52:17 +00001465 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
cristy3ed852e2009-09-05 21:47:34 +00001466 sizeof(*tile_pixels));
1467 if (tile_pixels == (uint32 *) NULL)
1468 {
1469 TIFFClose(tiff);
1470 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1471 }
cristybb503372010-05-27 20:51:26 +00001472 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001473 {
cristybb503372010-05-27 20:51:26 +00001474 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001475 x;
1476
cristy4c08aed2011-07-01 19:47:50 +00001477 register Quantum
1478 *restrict q,
1479 *restrict tile;
cristy3ed852e2009-09-05 21:47:34 +00001480
cristybb503372010-05-27 20:51:26 +00001481 size_t
cristy3ed852e2009-09-05 21:47:34 +00001482 columns_remaining,
1483 rows_remaining;
1484
1485 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001486 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001487 rows_remaining=rows;
1488 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1489 exception);
cristy4c08aed2011-07-01 19:47:50 +00001490 if (tile == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001491 break;
cristybb503372010-05-27 20:51:26 +00001492 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001493 {
cristybb503372010-05-27 20:51:26 +00001494 size_t
cristy3ed852e2009-09-05 21:47:34 +00001495 column,
1496 row;
1497
1498 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1499 break;
1500 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001501 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001502 columns_remaining=columns;
1503 p=tile_pixels+(rows-rows_remaining)*columns;
cristy68473222011-08-17 17:39:19 +00001504 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1505 x);
cristy3ed852e2009-09-05 21:47:34 +00001506 for (row=rows_remaining; row > 0; row--)
1507 {
1508 if (image->matte != MagickFalse)
1509 for (column=columns_remaining; column > 0; column--)
1510 {
cristy4c08aed2011-07-01 19:47:50 +00001511 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1512 TIFFGetR(*p)),q);
1513 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1514 TIFFGetG(*p)),q);
1515 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1516 TIFFGetB(*p)),q);
1517 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1518 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001519 p++;
cristyed231572011-07-14 02:18:59 +00001520 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001521 }
1522 else
1523 for (column=columns_remaining; column > 0; column--)
1524 {
cristy4c08aed2011-07-01 19:47:50 +00001525 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1526 TIFFGetR(*p)),q);
1527 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1528 TIFFGetG(*p)),q);
1529 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1530 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001531 p++;
cristyed231572011-07-14 02:18:59 +00001532 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001533 }
1534 p+=columns-columns_remaining;
cristyed231572011-07-14 02:18:59 +00001535 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
cristy3ed852e2009-09-05 21:47:34 +00001536 }
1537 }
1538 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1539 break;
1540 if (image->previous == (Image *) NULL)
1541 {
cristycee97112010-05-28 00:44:52 +00001542 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1543 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001544 if (status == MagickFalse)
1545 break;
1546 }
1547 }
1548 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1549 break;
1550 }
1551 case ReadGenericMethod:
1552 default:
1553 {
1554 register uint32
1555 *p;
1556
1557 uint32
1558 *pixels;
1559
1560 /*
1561 Convert TIFF image to DirectClass MIFF image.
1562 */
1563 number_pixels=(MagickSizeType) image->columns*image->rows;
1564 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1565 (number_pixels*sizeof(uint32))))
1566 {
1567 TIFFClose(tiff);
1568 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1569 }
1570 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1571 sizeof(uint32));
1572 if (pixels == (uint32 *) NULL)
1573 {
1574 TIFFClose(tiff);
1575 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1576 }
1577 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1578 (uint32) image->rows,(uint32 *) pixels,0);
1579 /*
1580 Convert image to DirectClass pixel packets.
1581 */
1582 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001583 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001584 {
cristybb503372010-05-27 20:51:26 +00001585 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001586 x;
1587
cristy4c08aed2011-07-01 19:47:50 +00001588 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001589 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001590
1591 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001592 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001593 break;
cristyed231572011-07-14 02:18:59 +00001594 q+=GetPixelChannels(image)*(image->columns-1);
cristybb503372010-05-27 20:51:26 +00001595 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001596 {
cristy4c08aed2011-07-01 19:47:50 +00001597 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1598 TIFFGetR(*p)),q);
1599 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1600 TIFFGetG(*p)),q);
1601 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1602 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001603 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001604 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1605 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001606 p--;
cristyed231572011-07-14 02:18:59 +00001607 q-=GetPixelChannels(image);;
cristy3ed852e2009-09-05 21:47:34 +00001608 }
1609 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1610 break;
1611 if (image->previous == (Image *) NULL)
1612 {
cristycee97112010-05-28 00:44:52 +00001613 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1614 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001615 if (status == MagickFalse)
1616 break;
1617 }
1618 }
1619 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1620 break;
1621 }
1622 }
1623 SetQuantumImageType(image,quantum_type);
1624 next_tiff_frame:
1625 if ((photometric == PHOTOMETRIC_LOGL) ||
1626 (photometric == PHOTOMETRIC_MINISBLACK) ||
1627 (photometric == PHOTOMETRIC_MINISWHITE))
1628 {
1629 image->type=GrayscaleType;
1630 if (bits_per_sample == 1)
1631 image->type=BilevelType;
1632 }
1633 if (image->storage_class == PseudoClass)
1634 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001635 image->endian=MSBEndian;
1636 if (endian == FILLORDER_LSB2MSB)
1637 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001638 if ((photometric == PHOTOMETRIC_LOGL) ||
1639 (photometric == PHOTOMETRIC_MINISBLACK) ||
1640 (photometric == PHOTOMETRIC_MINISWHITE))
1641 {
1642 image->type=GrayscaleType;
1643 if (bits_per_sample == 1)
1644 image->type=BilevelType;
1645 }
1646 /*
1647 Proceed to next image.
1648 */
1649 if (image_info->number_scenes != 0)
1650 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1651 break;
1652 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1653 if (status == MagickTrue)
1654 {
1655 /*
1656 Allocate next image structure.
1657 */
1658 AcquireNextImage(image_info,image);
1659 if (GetNextImageInList(image) == (Image *) NULL)
1660 {
1661 image=DestroyImageList(image);
1662 return((Image *) NULL);
1663 }
1664 image=SyncNextImageInList(image);
1665 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1666 image->scene);
1667 if (status == MagickFalse)
1668 break;
1669 }
1670 quantum_info=DestroyQuantumInfo(quantum_info);
1671 } while (status == MagickTrue);
1672 (void) TIFFSetWarningHandler(warning_handler);
1673 (void) TIFFSetErrorHandler(error_handler);
1674 TIFFClose(tiff);
1675 return(GetFirstImageInList(image));
1676}
1677#endif
1678
1679/*
1680%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1681% %
1682% %
1683% %
1684% R e g i s t e r T I F F I m a g e %
1685% %
1686% %
1687% %
1688%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1689%
1690% RegisterTIFFImage() adds properties for the TIFF image format to
1691% the list of supported formats. The properties include the image format
1692% tag, a method to read and/or write the format, whether the format
1693% supports the saving of more than one frame to the same file or blob,
1694% whether the format supports native in-memory I/O, and a brief
1695% description of the format.
1696%
1697% The format of the RegisterTIFFImage method is:
1698%
cristybb503372010-05-27 20:51:26 +00001699% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001700%
1701*/
cristybb503372010-05-27 20:51:26 +00001702ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001703{
1704#define TIFFDescription "Tagged Image File Format"
1705
1706 char
1707 version[MaxTextExtent];
1708
1709 MagickInfo
1710 *entry;
cristy6b032822010-06-29 16:52:32 +00001711
cristy18b17442009-10-25 18:36:48 +00001712 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1713 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001714 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001715 if (instantiate_key == MagickFalse)
1716 {
1717 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1718 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1719 instantiate_key=MagickTrue;
1720 }
cristyf84a1932010-01-03 18:00:18 +00001721 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001722 *version='\0';
1723#if defined(TIFF_VERSION)
cristyb51dff52011-05-19 16:55:47 +00001724 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
cristy3ed852e2009-09-05 21:47:34 +00001725#endif
1726#if defined(MAGICKCORE_TIFF_DELEGATE)
1727 {
1728 const char
1729 *p;
1730
cristybb503372010-05-27 20:51:26 +00001731 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001732 i;
1733
1734 p=TIFFGetVersion();
1735 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1736 version[i]=(*p++);
1737 version[i]='\0';
1738 }
1739#endif
1740
cristy3d7f8062009-09-24 20:45:35 +00001741 entry=SetMagickInfo("GROUP4");
1742#if defined(MAGICKCORE_TIFF_DELEGATE)
1743 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1744 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1745#endif
1746 entry->raw=MagickTrue;
1747 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001748 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001749 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001750 entry->seekable_stream=MagickTrue;
1751 entry->thread_support=NoThreadSupport;
1752 entry->description=ConstantString("Raw CCITT Group4");
1753 entry->module=ConstantString("TIFF");
1754 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001755 entry=SetMagickInfo("PTIF");
1756#if defined(MAGICKCORE_TIFF_DELEGATE)
1757 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1758 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1759#endif
1760 entry->endian_support=MagickTrue;
1761 entry->seekable_stream=MagickTrue;
1762 entry->thread_support=NoThreadSupport;
1763 entry->description=ConstantString("Pyramid encoded TIFF");
1764 entry->module=ConstantString("TIFF");
1765 (void) RegisterMagickInfo(entry);
1766 entry=SetMagickInfo("TIF");
1767#if defined(MAGICKCORE_TIFF_DELEGATE)
1768 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1769 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1770#endif
1771 entry->endian_support=MagickTrue;
1772 entry->seekable_stream=MagickTrue;
1773 entry->stealth=MagickTrue;
1774 entry->thread_support=NoThreadSupport;
1775 entry->description=ConstantString(TIFFDescription);
1776 if (*version != '\0')
1777 entry->version=ConstantString(version);
1778 entry->module=ConstantString("TIFF");
1779 (void) RegisterMagickInfo(entry);
1780 entry=SetMagickInfo("TIFF");
1781#if defined(MAGICKCORE_TIFF_DELEGATE)
1782 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1783 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1784#endif
1785 entry->magick=(IsImageFormatHandler *) IsTIFF;
1786 entry->endian_support=MagickTrue;
1787 entry->seekable_stream=MagickTrue;
1788 entry->thread_support=NoThreadSupport;
1789 entry->description=ConstantString(TIFFDescription);
1790 if (*version != '\0')
1791 entry->version=ConstantString(version);
1792 entry->module=ConstantString("TIFF");
1793 (void) RegisterMagickInfo(entry);
1794 entry=SetMagickInfo("TIFF64");
1795#if defined(TIFF_VERSION_BIG)
1796 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1797 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1798#endif
1799 entry->adjoin=MagickFalse;
1800 entry->endian_support=MagickTrue;
1801 entry->seekable_stream=MagickTrue;
1802 entry->thread_support=NoThreadSupport;
1803 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1804 if (*version != '\0')
1805 entry->version=ConstantString(version);
1806 entry->module=ConstantString("TIFF");
1807 (void) RegisterMagickInfo(entry);
1808 return(MagickImageCoderSignature);
1809}
1810
1811/*
1812%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1813% %
1814% %
1815% %
1816% U n r e g i s t e r T I F F I m a g e %
1817% %
1818% %
1819% %
1820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1821%
1822% UnregisterTIFFImage() removes format registrations made by the TIFF module
1823% from the list of supported formats.
1824%
1825% The format of the UnregisterTIFFImage method is:
1826%
1827% UnregisterTIFFImage(void)
1828%
1829*/
1830ModuleExport void UnregisterTIFFImage(void)
1831{
cristy3d7f8062009-09-24 20:45:35 +00001832 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001833 (void) UnregisterMagickInfo("PTIF");
1834 (void) UnregisterMagickInfo("TIF");
1835 (void) UnregisterMagickInfo("TIFF");
1836 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001837 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1838 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001839 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001840 if (instantiate_key != MagickFalse)
1841 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1842 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1843 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001844 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001845 DestroySemaphoreInfo(&tiff_semaphore);
1846}
1847
1848#if defined(MAGICKCORE_TIFF_DELEGATE)
1849/*
1850%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1851% %
1852% %
1853% %
cristy3d7f8062009-09-24 20:45:35 +00001854% W r i t e G R O U P 4 I m a g e %
1855% %
1856% %
1857% %
1858%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1859%
1860% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1861%
1862% The format of the WriteGROUP4Image method is:
1863%
1864% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001865% Image *image,ExceptionInfo *)
cristy3d7f8062009-09-24 20:45:35 +00001866%
1867% A description of each parameter follows:
1868%
1869% o image_info: the image info.
1870%
1871% o image: The image.
1872%
cristy3a37efd2011-08-28 20:31:03 +00001873% o exception: return any errors or warnings in this structure.
1874%
cristy3d7f8062009-09-24 20:45:35 +00001875*/
1876static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001877 Image *image,ExceptionInfo *exception)
cristy3d7f8062009-09-24 20:45:35 +00001878{
1879 char
1880 filename[MaxTextExtent];
1881
1882 FILE
1883 *file;
1884
1885 Image
1886 *huffman_image;
1887
1888 ImageInfo
1889 *write_info;
1890
1891 int
1892 unique_file;
1893
1894 MagickBooleanType
1895 status;
1896
cristybb503372010-05-27 20:51:26 +00001897 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001898 i;
1899
1900 ssize_t
1901 count;
1902
1903 TIFF
1904 *tiff;
1905
cristy94c8fe42009-10-06 01:57:36 +00001906 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001907 *byte_count,
1908 strip_size;
1909
1910 unsigned char
1911 *buffer;
1912
1913 /*
1914 Write image as CCITT Group4 TIFF image to a temporary file.
1915 */
1916 assert(image_info != (const ImageInfo *) NULL);
1917 assert(image_info->signature == MagickSignature);
1918 assert(image != (Image *) NULL);
1919 assert(image->signature == MagickSignature);
1920 if (image->debug != MagickFalse)
1921 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001922 assert(exception != (ExceptionInfo *) NULL);
1923 assert(exception->signature == MagickSignature);
1924 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3d7f8062009-09-24 20:45:35 +00001925 if (status == MagickFalse)
1926 return(status);
cristy3a37efd2011-08-28 20:31:03 +00001927 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3d7f8062009-09-24 20:45:35 +00001928 if (huffman_image == (Image *) NULL)
1929 {
1930 (void) CloseBlob(image);
1931 return(MagickFalse);
1932 }
cristy94c8fe42009-10-06 01:57:36 +00001933 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001934 file=(FILE *) NULL;
1935 unique_file=AcquireUniqueFileResource(filename);
1936 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001937 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001938 if ((unique_file == -1) || (file == (FILE *) NULL))
1939 {
cristy3a37efd2011-08-28 20:31:03 +00001940 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1941 filename);
cristy3d7f8062009-09-24 20:45:35 +00001942 return(MagickFalse);
1943 }
cristyb51dff52011-05-19 16:55:47 +00001944 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
cristy3d7f8062009-09-24 20:45:35 +00001945 filename);
cristy018f07f2011-09-04 21:15:19 +00001946 (void) SetImageType(huffman_image,BilevelType,exception);
cristy14efd992009-09-26 23:46:03 +00001947 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001948 SetImageInfoFile(write_info,file);
1949 write_info->compression=Group4Compression;
1950 write_info->type=BilevelType;
1951 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
cristy3a37efd2011-08-28 20:31:03 +00001952 status=WriteTIFFImage(write_info,huffman_image,exception);
cristy3d7f8062009-09-24 20:45:35 +00001953 (void) fflush(file);
1954 write_info=DestroyImageInfo(write_info);
1955 if (status == MagickFalse)
1956 {
cristy3d7f8062009-09-24 20:45:35 +00001957 huffman_image=DestroyImage(huffman_image);
1958 (void) fclose(file);
1959 (void) RelinquishUniqueFileResource(filename);
1960 return(MagickFalse);
1961 }
1962 tiff=TIFFOpen(filename,"rb");
1963 if (tiff == (TIFF *) NULL)
1964 {
1965 huffman_image=DestroyImage(huffman_image);
1966 (void) fclose(file);
1967 (void) RelinquishUniqueFileResource(filename);
cristy3a37efd2011-08-28 20:31:03 +00001968 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
cristy3d7f8062009-09-24 20:45:35 +00001969 image_info->filename);
1970 return(MagickFalse);
1971 }
1972 /*
1973 Allocate raw strip buffer.
1974 */
cristy94c8fe42009-10-06 01:57:36 +00001975 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1976 {
1977 TIFFClose(tiff);
1978 huffman_image=DestroyImage(huffman_image);
1979 (void) fclose(file);
1980 (void) RelinquishUniqueFileResource(filename);
1981 return(MagickFalse);
1982 }
cristy3d7f8062009-09-24 20:45:35 +00001983 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00001984 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001985 if (byte_count[i] > strip_size)
1986 strip_size=byte_count[i];
1987 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1988 sizeof(*buffer));
1989 if (buffer == (unsigned char *) NULL)
1990 {
1991 TIFFClose(tiff);
1992 huffman_image=DestroyImage(huffman_image);
1993 (void) fclose(file);
1994 (void) RelinquishUniqueFileResource(filename);
1995 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1996 image_info->filename);
1997 }
1998 /*
1999 Compress runlength encoded to 2D Huffman pixels.
2000 */
cristybb503372010-05-27 20:51:26 +00002001 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002002 {
cristy94c8fe42009-10-06 01:57:36 +00002003 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00002004 if (WriteBlob(image,(size_t) count,buffer) != count)
2005 status=MagickFalse;
2006 }
2007 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2008 TIFFClose(tiff);
2009 huffman_image=DestroyImage(huffman_image);
2010 (void) fclose(file);
2011 (void) RelinquishUniqueFileResource(filename);
2012 (void) CloseBlob(image);
2013 return(status);
2014}
2015#endif
2016
2017#if defined(MAGICKCORE_TIFF_DELEGATE)
2018/*
2019%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2020% %
2021% %
2022% %
cristy3ed852e2009-09-05 21:47:34 +00002023% W r i t e P T I F I m a g e %
2024% %
2025% %
2026% %
2027%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2028%
2029% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2030% format.
2031%
2032% The format of the WritePTIFImage method is:
2033%
2034% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002035% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002036%
2037% A description of each parameter follows:
2038%
2039% o image_info: the image info.
2040%
2041% o image: The image.
2042%
cristy3a37efd2011-08-28 20:31:03 +00002043% o exception: return any errors or warnings in this structure.
2044%
cristy3ed852e2009-09-05 21:47:34 +00002045*/
2046static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002047 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002048{
2049 Image
2050 *images,
2051 *next,
2052 *pyramid_image;
2053
2054 ImageInfo
2055 *write_info;
2056
2057 MagickBooleanType
2058 status;
2059
cristybb503372010-05-27 20:51:26 +00002060 size_t
cristy3ed852e2009-09-05 21:47:34 +00002061 columns,
2062 rows;
2063
2064 /*
2065 Create pyramid-encoded TIFF image.
2066 */
2067 images=NewImageList();
2068 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2069 {
cristy3a37efd2011-08-28 20:31:03 +00002070 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,exception));
cristy3ed852e2009-09-05 21:47:34 +00002071 columns=next->columns;
2072 rows=next->rows;
2073 while ((columns > 64) && (rows > 64))
2074 {
2075 columns/=2;
2076 rows/=2;
2077 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
cristy3a37efd2011-08-28 20:31:03 +00002078 exception);
cristy3ed852e2009-09-05 21:47:34 +00002079 AppendImageToList(&images,pyramid_image);
2080 }
2081 }
2082 /*
2083 Write pyramid-encoded TIFF image.
2084 */
cristy58567532009-09-26 01:13:32 +00002085 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002086 *write_info->magick='\0';
2087 write_info->adjoin=MagickTrue;
cristy3a37efd2011-08-28 20:31:03 +00002088 status=WriteTIFFImage(write_info,GetFirstImageInList(images),exception);
cristy3ed852e2009-09-05 21:47:34 +00002089 images=DestroyImageList(images);
2090 write_info=DestroyImageInfo(write_info);
2091 return(status);
2092}
2093#endif
2094
2095#if defined(MAGICKCORE_TIFF_DELEGATE)
2096/*
2097%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2098% %
2099% %
2100% W r i t e T I F F I m a g e %
2101% %
2102% %
2103% %
2104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2105%
2106% WriteTIFFImage() writes an image in the Tagged image file format.
2107%
2108% The format of the WriteTIFFImage method is:
2109%
2110% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002111% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002112%
2113% A description of each parameter follows:
2114%
2115% o image_info: the image info.
2116%
2117% o image: The image.
2118%
cristy3a37efd2011-08-28 20:31:03 +00002119% o exception: return any errors or warnings in this structure.
2120%
cristy3ed852e2009-09-05 21:47:34 +00002121*/
2122
2123typedef struct _TIFFInfo
2124{
2125 RectangleInfo
2126 tile_geometry;
2127
2128 unsigned char
2129 *scanline,
2130 *scanlines,
2131 *pixels;
2132} TIFFInfo;
2133
2134static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2135{
2136 assert(tiff_info != (TIFFInfo *) NULL);
2137 if (tiff_info->scanlines != (unsigned char *) NULL)
2138 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2139 tiff_info->scanlines);
2140 if (tiff_info->pixels != (unsigned char *) NULL)
2141 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2142 tiff_info->pixels);
2143}
2144
2145static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2146 TIFFInfo *tiff_info)
2147{
2148 const char
2149 *option;
2150
2151 MagickStatusType
2152 flags;
2153
cristy79e5dad2010-09-16 19:48:33 +00002154 uint32
2155 tile_columns,
2156 tile_rows;
2157
cristy3ed852e2009-09-05 21:47:34 +00002158 assert(tiff_info != (TIFFInfo *) NULL);
2159 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2160 option=GetImageOption(image_info,"tiff:tile-geometry");
2161 if (option == (const char *) NULL)
2162 return(MagickTrue);
2163 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2164 if ((flags & HeightValue) == 0)
2165 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002166 tile_columns=(uint32) tiff_info->tile_geometry.width;
2167 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002168 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2169 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2170 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2171 tiff_info->tile_geometry.width=tile_columns;
2172 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002173 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002174 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002175 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002176 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002177 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2178 (tiff_info->pixels == (unsigned char *) NULL))
2179 {
2180 DestroyTIFFInfo(tiff_info);
2181 return(MagickFalse);
2182 }
2183 return(MagickTrue);
2184}
2185
cristybb503372010-05-27 20:51:26 +00002186static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002187 tsample_t sample,Image *image)
2188{
2189 int32
2190 status;
2191
cristybb503372010-05-27 20:51:26 +00002192 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002193 i;
2194
2195 register unsigned char
2196 *p,
2197 *q;
2198
cristybb503372010-05-27 20:51:26 +00002199 size_t
cristy3ed852e2009-09-05 21:47:34 +00002200 number_tiles,
2201 tile_width;
2202
cristyc6da28e2011-04-28 01:41:35 +00002203 ssize_t
2204 bytes_per_pixel,
2205 j,
2206 k,
2207 l;
2208
cristy3ed852e2009-09-05 21:47:34 +00002209 if (TIFFIsTiled(tiff) == 0)
2210 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2211 /*
2212 Fill scanlines to tile height.
2213 */
cristybb503372010-05-27 20:51:26 +00002214 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002215 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2216 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002217 if (((size_t) (row % tiff_info->tile_geometry.height) !=
cristyc6da28e2011-04-28 01:41:35 +00002218 (tiff_info->tile_geometry.height-1)) &&
2219 (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002220 return(0);
2221 /*
2222 Write tile to TIFF image.
2223 */
2224 status=0;
cristybb503372010-05-27 20:51:26 +00002225 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002226 tiff_info->tile_geometry.width);
2227 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2228 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002229 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002230 {
cristybb503372010-05-27 20:51:26 +00002231 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002232 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002233 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2234 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002235 {
2236 if (bytes_per_pixel == 0)
2237 {
2238 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2239 tiff_info->tile_geometry.width+k)/8);
2240 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2241 *q++=(*p++);
2242 continue;
2243 }
2244 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2245 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2246 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2247 for (l=0; l < bytes_per_pixel; l++)
2248 *q++=(*p++);
2249 }
cristydaff8092010-04-22 14:50:53 +00002250 if ((i*tiff_info->tile_geometry.width) != image->columns)
2251 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2252 tiff_info->tile_geometry.width),(uint32) ((row/
2253 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2254 sample);
cristy3ed852e2009-09-05 21:47:34 +00002255 if (status < 0)
2256 break;
2257 }
2258 return(status);
2259}
2260
2261static void TIFFSetProfiles(TIFF *tiff,Image *image)
2262{
2263 const char
2264 *name;
2265
2266 const StringInfo
2267 *profile;
2268
2269 if (image->profiles == (void *) NULL)
2270 return;
2271 ResetImageProfileIterator(image);
2272 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2273 {
2274 profile=GetImageProfile(image,name);
2275#if defined(TIFFTAG_XMLPACKET)
2276 if (LocaleCompare(name,"xmp") == 0)
2277 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2278 profile),GetStringInfoDatum(profile));
2279#endif
2280#if defined(TIFFTAG_ICCPROFILE)
2281 if (LocaleCompare(name,"icc") == 0)
2282 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2283 profile),GetStringInfoDatum(profile));
2284#endif
2285 if (LocaleCompare(name,"iptc") == 0)
2286 {
2287 size_t
2288 length;
2289
2290 StringInfo
2291 *iptc_profile;
2292
2293 iptc_profile=CloneStringInfo(profile);
2294 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2295 0x03);
2296 SetStringInfoLength(iptc_profile,length);
2297 if (TIFFIsByteSwapped(tiff))
2298 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002299 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002300 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2301 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2302 iptc_profile=DestroyStringInfo(iptc_profile);
2303 }
2304#if defined(TIFFTAG_PHOTOSHOP)
2305 if (LocaleCompare(name,"8bim") == 0)
2306 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2307 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2308#endif
2309 if (LocaleCompare(name,"tiff:37724") == 0)
2310 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2311 GetStringInfoDatum(profile));
2312 name=GetNextImageProfile(image);
2313 }
2314}
2315
2316static void TIFFSetProperties(TIFF *tiff,Image *image)
2317{
2318 const char
2319 *value;
2320
cristy15d8d212010-09-17 01:56:30 +00002321 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002322 value=GetImageProperty(image,"tiff:hostcomputer");
2323 if (value != (const char *) NULL)
2324 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2325 value=GetImageProperty(image,"tiff:artist");
2326 if (value != (const char *) NULL)
2327 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2328 value=GetImageProperty(image,"tiff:timestamp");
2329 if (value != (const char *) NULL)
2330 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2331 value=GetImageProperty(image,"tiff:make");
2332 if (value != (const char *) NULL)
2333 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2334 value=GetImageProperty(image,"tiff:model");
2335 if (value != (const char *) NULL)
2336 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristy15d8d212010-09-17 01:56:30 +00002337 value=GetImageProperty(image,"tiff:software");
2338 if (value != (const char *) NULL)
2339 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristy3ed852e2009-09-05 21:47:34 +00002340 value=GetImageProperty(image,"tiff:copyright");
2341 if (value != (const char *) NULL)
2342 (void) TIFFSetField(tiff,33432,value);
2343 value=GetImageProperty(image,"kodak-33423");
2344 if (value != (const char *) NULL)
2345 (void) TIFFSetField(tiff,33423,value);
2346 value=GetImageProperty(image,"kodak-36867");
2347 if (value != (const char *) NULL)
2348 (void) TIFFSetField(tiff,36867,value);
2349 value=GetImageProperty(image,"label");
2350 if (value != (const char *) NULL)
2351 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2352 value=GetImageProperty(image,"comment");
2353 if (value != (const char *) NULL)
2354 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2355}
2356
2357static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2358{
2359#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2360 const char
2361 *value;
2362
cristybb503372010-05-27 20:51:26 +00002363 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002364 i;
2365
2366 uint32
2367 offset;
2368
2369 /*
2370 Write EXIF properties.
2371 */
2372 offset=0;
2373 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2374 for (i=0; exif_info[i].tag != 0; i++)
2375 {
2376 value=GetImageProperty(image,exif_info[i].property);
2377 if (value == (const char *) NULL)
2378 continue;
2379 switch (exif_info[i].type)
2380 {
2381 case TIFF_ASCII:
2382 {
2383 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2384 break;
2385 }
2386 case TIFF_SHORT:
2387 {
2388 uint16
2389 shorty;
2390
cristyf2f27272009-12-17 14:48:46 +00002391 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002392 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2393 break;
2394 }
2395 case TIFF_LONG:
2396 {
2397 uint16
cristybb503372010-05-27 20:51:26 +00002398 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002399
cristybb503372010-05-27 20:51:26 +00002400 ssize_ty=(uint16) StringToLong(value);
2401 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002402 break;
2403 }
2404 case TIFF_RATIONAL:
2405 case TIFF_SRATIONAL:
2406 {
2407 float
2408 rational;
2409
cristyc1acd842011-05-19 23:05:47 +00002410 rational=InterpretLocaleValue(value,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002411 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2412 break;
2413 }
2414 default:
2415 break;
2416 }
2417 }
2418 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2419#else
2420 (void) tiff;
2421 (void) image;
2422#endif
2423}
2424
2425static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002426 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002427{
2428#if !defined(TIFFDefaultStripSize)
2429#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2430#endif
2431
2432 const char
2433 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002434 *option;
cristy3ed852e2009-09-05 21:47:34 +00002435
2436 CompressionType
2437 compression;
2438
cristy6b032822010-06-29 16:52:32 +00002439 EndianType
2440 endian_type;
2441
cristy3ed852e2009-09-05 21:47:34 +00002442 MagickBooleanType
2443 debug,
2444 status;
2445
2446 MagickOffsetType
2447 scene;
2448
2449 QuantumInfo
2450 *quantum_info;
2451
2452 QuantumType
2453 quantum_type;
2454
cristybb503372010-05-27 20:51:26 +00002455 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002456 i;
2457
2458 size_t
cristy5ecaba72011-02-18 02:05:32 +00002459 length,
2460 lsb_first;
cristy3ed852e2009-09-05 21:47:34 +00002461
cristyc6da28e2011-04-28 01:41:35 +00002462 ssize_t
2463 y;
2464
cristy3ed852e2009-09-05 21:47:34 +00002465 TIFF
2466 *tiff;
2467
2468 TIFFErrorHandler
2469 error_handler,
2470 warning_handler;
2471
2472 TIFFInfo
2473 tiff_info;
2474
2475 uint16
2476 bits_per_sample,
2477 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002478 endian,
cristy3ed852e2009-09-05 21:47:34 +00002479 photometric;
2480
2481 uint32
2482 rows_per_strip;
2483
2484 unsigned char
2485 *pixels;
2486
cristy3ed852e2009-09-05 21:47:34 +00002487 /*
2488 Open TIFF file.
2489 */
2490 assert(image_info != (const ImageInfo *) NULL);
2491 assert(image_info->signature == MagickSignature);
2492 assert(image != (Image *) NULL);
2493 assert(image->signature == MagickSignature);
2494 if (image->debug != MagickFalse)
2495 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002496 assert(exception != (ExceptionInfo *) NULL);
2497 assert(exception->signature == MagickSignature);
2498 assert(exception != (ExceptionInfo *) NULL);
2499 assert(exception->signature == MagickSignature);
2500 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00002501 if (status == MagickFalse)
2502 return(status);
cristy3a37efd2011-08-28 20:31:03 +00002503 (void) MagickSetThreadValue(tiff_exception,exception);
cristy3ed852e2009-09-05 21:47:34 +00002504 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2505 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002506 endian_type=UndefinedEndian;
2507 option=GetImageOption(image_info,"tiff:endian");
2508 if (option != (const char *) NULL)
2509 {
2510 if (LocaleNCompare(option,"msb",3) == 0)
2511 endian_type=MSBEndian;
2512 if (LocaleNCompare(option,"lsb",3) == 0)
2513 endian_type=LSBEndian;;
2514 }
2515 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002516 {
2517 case LSBEndian: mode="wl"; break;
2518 case MSBEndian: mode="wb"; break;
2519 default: mode="w"; break;
2520 }
2521#if defined(TIFF_VERSION_BIG)
2522 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002523 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002524 {
2525 case LSBEndian: mode="wl8"; break;
2526 case MSBEndian: mode="wb8"; break;
2527 default: mode="w8"; break;
2528 }
2529#endif
2530 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2531 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2532 TIFFUnmapBlob);
2533 if (tiff == (TIFF *) NULL)
2534 {
2535 (void) TIFFSetWarningHandler(warning_handler);
2536 (void) TIFFSetErrorHandler(error_handler);
2537 return(MagickFalse);
2538 }
2539 scene=0;
2540 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +00002541 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +00002542 do
2543 {
2544 /*
2545 Initialize TIFF fields.
2546 */
cristy5f1c1ff2010-12-23 21:38:06 +00002547 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002548 (image_info->type != OptimizeType))
cristy018f07f2011-09-04 21:15:19 +00002549 (void) SetImageType(image,image_info->type,exception);
cristy3ed852e2009-09-05 21:47:34 +00002550 quantum_info=AcquireQuantumInfo(image_info,image);
2551 if (quantum_info == (QuantumInfo *) NULL)
2552 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2553 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2554 (quantum_info->format == UndefinedQuantumFormat) &&
cristy3a37efd2011-08-28 20:31:03 +00002555 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002556 {
2557 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2558 if (status == MagickFalse)
2559 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2560 }
2561 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2562 (GetPreviousImageInList(image) != (Image *) NULL))
2563 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2564 if ((image->columns != (uint32) image->columns) ||
2565 (image->rows != (uint32) image->rows))
2566 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2567 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2568 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2569 compression=image->compression;
2570 if (image_info->compression != UndefinedCompression)
2571 compression=image_info->compression;
2572 switch (compression)
2573 {
2574 case FaxCompression:
2575 {
2576 compress_tag=COMPRESSION_CCITTFAX3;
2577 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2578 break;
2579 }
2580 case Group4Compression:
2581 {
2582 compress_tag=COMPRESSION_CCITTFAX4;
2583 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2584 break;
2585 }
cristy6d5e20f2011-04-25 13:48:54 +00002586#if defined(COMPRESSION_JBIG)
2587 case JBIG1Compression:
2588 {
2589 compress_tag=COMPRESSION_JBIG;
2590 break;
2591 }
2592#endif
cristy3ed852e2009-09-05 21:47:34 +00002593 case JPEGCompression:
2594 {
2595 compress_tag=COMPRESSION_JPEG;
2596 break;
2597 }
cristyfbb0ef02010-12-19 02:32:11 +00002598#if defined(COMPRESSION_LZMA)
2599 case LZMACompression:
2600 {
2601 compress_tag=COMPRESSION_LZMA;
2602 break;
2603 }
2604#endif
cristy3ed852e2009-09-05 21:47:34 +00002605 case LZWCompression:
2606 {
2607 compress_tag=COMPRESSION_LZW;
2608 break;
2609 }
2610 case RLECompression:
2611 {
2612 compress_tag=COMPRESSION_PACKBITS;
2613 break;
2614 }
2615 case ZipCompression:
2616 {
2617 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2618 break;
2619 }
2620 case NoCompression:
2621 default:
2622 {
2623 compress_tag=COMPRESSION_NONE;
2624 break;
2625 }
2626 }
cristy5b675872009-10-21 13:12:00 +00002627#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2628 if ((compress_tag != COMPRESSION_NONE) &&
2629 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002630 {
cristy3a37efd2011-08-28 20:31:03 +00002631 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2632 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002633 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002634 compress_tag=COMPRESSION_NONE;
2635 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002636 }
2637#else
2638 switch (compress_tag)
2639 {
2640#if defined(CCITT_SUPPORT)
2641 case COMPRESSION_CCITTFAX3:
2642 case COMPRESSION_CCITTFAX4:
2643#endif
2644#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2645 case COMPRESSION_JPEG:
2646#endif
cristyfbb0ef02010-12-19 02:32:11 +00002647#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2648 case COMPRESSION_LZMA:
2649#endif
cristy3ed852e2009-09-05 21:47:34 +00002650#if defined(LZW_SUPPORT)
2651 case COMPRESSION_LZW:
2652#endif
2653#if defined(PACKBITS_SUPPORT)
2654 case COMPRESSION_PACKBITS:
2655#endif
2656#if defined(ZIP_SUPPORT)
2657 case COMPRESSION_ADOBE_DEFLATE:
2658#endif
2659 case COMPRESSION_NONE:
2660 break;
2661 default:
2662 {
cristy3a37efd2011-08-28 20:31:03 +00002663 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2664 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002665 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002666 compress_tag=COMPRESSION_NONE;
2667 compression=NoCompression;
2668 break;
2669 }
2670 }
2671#endif
2672 switch (compression)
2673 {
2674 case FaxCompression:
2675 case Group4Compression:
2676 {
cristy018f07f2011-09-04 21:15:19 +00002677 (void) SetImageType(image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +00002678 break;
2679 }
2680 case JPEGCompression:
2681 {
cristy3a37efd2011-08-28 20:31:03 +00002682 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +00002683 (void) SetImageDepth(image,8);
2684 break;
2685 }
2686 default:
2687 break;
2688 }
cristy3ed852e2009-09-05 21:47:34 +00002689 if (image->colorspace == CMYKColorspace)
2690 {
2691 photometric=PHOTOMETRIC_SEPARATED;
2692 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2693 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2694 }
2695 else
2696 {
2697 /*
2698 Full color TIFF raster.
2699 */
2700 if (image->colorspace == LabColorspace)
2701 photometric=PHOTOMETRIC_CIELAB;
2702 else
2703 if (image->colorspace == YCbCrColorspace)
2704 {
2705 photometric=PHOTOMETRIC_YCBCR;
2706 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy3a37efd2011-08-28 20:31:03 +00002707 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +00002708 (void) SetImageDepth(image,8);
2709 }
2710 else
2711 {
cristy510d06a2011-07-06 23:43:54 +00002712 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002713 (void) TransformImageColorspace(image,RGBColorspace);
2714 photometric=PHOTOMETRIC_RGB;
2715 }
2716 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2717 if ((image_info->type != TrueColorType) &&
2718 (image_info->type != TrueColorMatteType))
2719 {
cristy94c8fe42009-10-06 01:57:36 +00002720 if ((image_info->type != PaletteType) &&
cristy3a37efd2011-08-28 20:31:03 +00002721 (IsImageGray(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002722 {
cristy94c8fe42009-10-06 01:57:36 +00002723 photometric=(uint16) (quantum_info->min_is_white !=
2724 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2725 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002726 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002727 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy3a37efd2011-08-28 20:31:03 +00002728 (IsImageMonochrome(image,exception) != MagickFalse))
cristy94c8fe42009-10-06 01:57:36 +00002729 {
2730 status=SetQuantumDepth(image,quantum_info,1);
2731 if (status == MagickFalse)
2732 ThrowWriterException(ResourceLimitError,
2733 "MemoryAllocationFailed");
2734 }
cristy3ed852e2009-09-05 21:47:34 +00002735 }
2736 else
cristy94c8fe42009-10-06 01:57:36 +00002737 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002738 {
cristybb503372010-05-27 20:51:26 +00002739 size_t
cristy94c8fe42009-10-06 01:57:36 +00002740 depth;
2741
2742 /*
2743 Colormapped TIFF raster.
2744 */
cristy3ed852e2009-09-05 21:47:34 +00002745 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002746 photometric=PHOTOMETRIC_PALETTE;
2747 depth=1;
2748 while ((GetQuantumRange(depth)+1) < image->colors)
2749 depth<<=1;
2750 status=SetQuantumDepth(image,quantum_info,depth);
2751 if (status == MagickFalse)
2752 ThrowWriterException(ResourceLimitError,
2753 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002754 }
2755 }
2756 }
cristyf2687ca2010-06-29 16:32:38 +00002757 switch (image->endian)
2758 {
2759 case LSBEndian:
2760 {
2761 endian=FILLORDER_LSB2MSB;
2762 break;
2763 }
2764 case MSBEndian:
2765 {
2766 endian=FILLORDER_MSB2LSB;
2767 break;
2768 }
2769 case UndefinedEndian:
2770 default:
2771 {
2772 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2773 break;
2774 }
2775 }
2776 lsb_first=1;
2777 image->endian=MSBEndian;
2778 if ((int) (*(char *) &lsb_first) != 0)
2779 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002780 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2781 (photometric != PHOTOMETRIC_MINISWHITE))
2782 {
2783 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002784 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002785 }
cristy0accf6e2009-10-01 13:14:28 +00002786 else
cristydaff8092010-04-22 14:50:53 +00002787 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2788 (photometric != PHOTOMETRIC_MINISWHITE))
2789 {
2790 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002791 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002792 }
cristy62e282b2010-06-29 01:27:13 +00002793 option=GetImageProperty(image,"tiff:fill-order");
2794 if (option != (const char *) NULL)
2795 {
2796 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002797 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002798 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002799 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002800 }
cristyf2687ca2010-06-29 16:32:38 +00002801 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2802 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002803 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2804 if (image->matte != MagickFalse)
2805 {
2806 uint16
2807 extra_samples,
2808 sample_info[1],
2809 samples_per_pixel;
2810
2811 /*
2812 TIFF has a matte channel.
2813 */
2814 extra_samples=1;
2815 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2816 option=GetImageProperty(image,"tiff:alpha");
2817 if ((option != (const char *) NULL) &&
2818 (LocaleCompare(option,"associated") == 0))
2819 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2820 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2821 &samples_per_pixel);
2822 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2823 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2824 &sample_info);
2825 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2826 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2827 }
2828 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2829 switch (quantum_info->format)
2830 {
2831 case FloatingPointQuantumFormat:
2832 {
2833 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2834 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2835 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2836 break;
2837 }
2838 case SignedQuantumFormat:
2839 {
2840 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2841 break;
2842 }
2843 case UnsignedQuantumFormat:
2844 {
2845 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2846 break;
2847 }
2848 default:
2849 break;
2850 }
cristy3ed852e2009-09-05 21:47:34 +00002851 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2852 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2853 if (photometric == PHOTOMETRIC_RGB)
2854 if ((image_info->interlace == PlaneInterlace) ||
2855 (image_info->interlace == PartitionInterlace))
2856 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2857 rows_per_strip=1;
2858 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002859 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2860 1);
cristy3ed852e2009-09-05 21:47:34 +00002861 option=GetImageOption(image_info,"tiff:rows-per-strip");
2862 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002863 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002864 switch (compress_tag)
2865 {
2866 case COMPRESSION_JPEG:
2867 {
2868#if defined(JPEG_SUPPORT)
2869 const char
2870 *sampling_factor;
2871
2872 GeometryInfo
2873 geometry_info;
2874
2875 MagickStatusType
2876 flags;
2877
2878 rows_per_strip+=(16-(rows_per_strip % 16));
2879 if (image->quality != 0)
2880 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2881 if (image_info->quality != UndefinedCompressionQuality)
2882 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2883 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
cristy510d06a2011-07-06 23:43:54 +00002884 if (IsRGBColorspace(image->colorspace) == MagickTrue)
cristy3ed852e2009-09-05 21:47:34 +00002885 {
cristy949bf5b2010-05-08 02:47:03 +00002886 const char
2887 *value;
2888
cristy3ed852e2009-09-05 21:47:34 +00002889 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2890 sampling_factor=(const char *) NULL;
2891 value=GetImageProperty(image,"jpeg:sampling-factor");
2892 if (value != (char *) NULL)
2893 {
2894 sampling_factor=value;
2895 if (image->debug != MagickFalse)
2896 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2897 " Input sampling-factors=%s",sampling_factor);
2898 }
2899 if (image_info->sampling_factor != (char *) NULL)
2900 sampling_factor=image_info->sampling_factor;
2901 if (sampling_factor != (const char *) NULL)
2902 {
2903 flags=ParseGeometry(sampling_factor,&geometry_info);
2904 if ((flags & SigmaValue) == 0)
2905 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002906 if (image->colorspace == YCbCrColorspace)
2907 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2908 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00002909 }
2910 }
2911 if (bits_per_sample == 12)
2912 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2913#endif
2914 break;
2915 }
2916 case COMPRESSION_ADOBE_DEFLATE:
2917 {
cristyf6fe0a12010-05-30 00:44:47 +00002918 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002919 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2920 &bits_per_sample);
2921 if (((photometric == PHOTOMETRIC_RGB) ||
2922 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2923 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002924 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00002925 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2926 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00002927 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00002928 break;
2929 }
2930 case COMPRESSION_CCITTFAX3:
2931 {
2932 /*
2933 Byte-aligned EOL.
2934 */
cristyeaedf062010-05-29 22:36:02 +00002935 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002936 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2937 break;
2938 }
2939 case COMPRESSION_CCITTFAX4:
2940 {
cristyeaedf062010-05-29 22:36:02 +00002941 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002942 break;
2943 }
cristyef8f26b2010-12-19 20:29:16 +00002944#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2945 case COMPRESSION_LZMA:
2946 {
2947 if (((photometric == PHOTOMETRIC_RGB) ||
2948 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2949 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2950 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2951 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
2952 image_info->quality == UndefinedCompressionQuality ? 7 :
2953 MagickMin((ssize_t) image_info->quality/10,9)));
2954 break;
2955 }
2956#endif
cristy3ed852e2009-09-05 21:47:34 +00002957 case COMPRESSION_LZW:
2958 {
2959 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2960 &bits_per_sample);
2961 if (((photometric == PHOTOMETRIC_RGB) ||
2962 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2963 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002964 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00002965 break;
2966 }
2967 default:
2968 break;
2969 }
cristy79e5dad2010-09-16 19:48:33 +00002970 option=GetImageOption(image_info,"tiff:tile-geometry");
2971 if (option == (const char *) NULL)
2972 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy3ed852e2009-09-05 21:47:34 +00002973 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2974 {
2975 unsigned short
2976 units;
2977
2978 /*
2979 Set image resolution.
2980 */
2981 units=RESUNIT_NONE;
2982 if (image->units == PixelsPerInchResolution)
2983 units=RESUNIT_INCH;
2984 if (image->units == PixelsPerCentimeterResolution)
2985 units=RESUNIT_CENTIMETER;
2986 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2987 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2988 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2989 if ((image->page.x != 0) || (image->page.y != 0))
2990 {
2991 /*
2992 Set image position.
2993 */
2994 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2995 image->x_resolution);
2996 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2997 image->y_resolution);
2998 }
2999 }
3000 if (image->chromaticity.white_point.x != 0.0)
3001 {
3002 float
3003 chromaticity[6];
3004
3005 /*
3006 Set image chromaticity.
3007 */
3008 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3009 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3010 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3011 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3012 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3013 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3014 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3015 chromaticity[0]=(float) image->chromaticity.white_point.x;
3016 chromaticity[1]=(float) image->chromaticity.white_point.y;
3017 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3018 }
3019 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3020 {
3021 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3022 if (image->scene != 0)
3023 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3024 GetImageListLength(image));
3025 }
3026 if (image->orientation != UndefinedOrientation)
3027 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3028 (void) TIFFSetProfiles(tiff,image);
3029 {
3030 uint16
3031 page,
3032 pages;
3033
3034 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00003035 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00003036 if ((image_info->adjoin != MagickFalse) && (pages > 1))
3037 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3038 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3039 }
3040 (void) TIFFSetProperties(tiff,image);
3041 if (0)
3042 (void) TIFFSetEXIFProperties(tiff,image);
3043 /*
3044 Write image scanlines.
3045 */
3046 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3047 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3048 pixels=GetQuantumPixels(quantum_info);
3049 tiff_info.scanline=GetQuantumPixels(quantum_info);
3050 switch (photometric)
3051 {
3052 case PHOTOMETRIC_CIELAB:
3053 case PHOTOMETRIC_YCBCR:
3054 case PHOTOMETRIC_RGB:
3055 {
3056 /*
3057 RGB TIFF image.
3058 */
3059 switch (image_info->interlace)
3060 {
3061 case NoInterlace:
3062 default:
3063 {
3064 quantum_type=RGBQuantum;
3065 if (image->matte != MagickFalse)
3066 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003067 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003068 {
cristy4c08aed2011-07-01 19:47:50 +00003069 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003070 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003071
cristy3a37efd2011-08-28 20:31:03 +00003072 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003073 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003074 break;
cristy4c08aed2011-07-01 19:47:50 +00003075 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003076 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00003077 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00003078 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3079 break;
3080 if (image->previous == (Image *) NULL)
3081 {
cristy2837bcc2010-08-07 23:57:39 +00003082 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3083 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003084 if (status == MagickFalse)
3085 break;
3086 }
3087 }
3088 break;
3089 }
3090 case PlaneInterlace:
3091 case PartitionInterlace:
3092 {
3093 /*
3094 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3095 */
cristybb503372010-05-27 20:51:26 +00003096 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003097 {
cristy4c08aed2011-07-01 19:47:50 +00003098 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003099 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003100
cristy3a37efd2011-08-28 20:31:03 +00003101 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003102 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003103 break;
cristy4c08aed2011-07-01 19:47:50 +00003104 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003105 RedQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003106 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3107 break;
3108 }
3109 if (image->previous == (Image *) NULL)
3110 {
3111 status=SetImageProgress(image,SaveImageTag,100,400);
3112 if (status == MagickFalse)
3113 break;
3114 }
cristybb503372010-05-27 20:51:26 +00003115 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003116 {
cristy4c08aed2011-07-01 19:47:50 +00003117 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003118 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003119
cristy3a37efd2011-08-28 20:31:03 +00003120 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003121 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003122 break;
cristy4c08aed2011-07-01 19:47:50 +00003123 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003124 GreenQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003125 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3126 break;
3127 }
3128 if (image->previous == (Image *) NULL)
3129 {
3130 status=SetImageProgress(image,SaveImageTag,200,400);
3131 if (status == MagickFalse)
3132 break;
3133 }
cristybb503372010-05-27 20:51:26 +00003134 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003135 {
cristy4c08aed2011-07-01 19:47:50 +00003136 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003137 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003138
cristy3a37efd2011-08-28 20:31:03 +00003139 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003140 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003141 break;
cristy4c08aed2011-07-01 19:47:50 +00003142 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003143 BlueQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003144 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3145 break;
3146 }
3147 if (image->previous == (Image *) NULL)
3148 {
3149 status=SetImageProgress(image,SaveImageTag,300,400);
3150 if (status == MagickFalse)
3151 break;
3152 }
3153 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003154 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003155 {
cristy4c08aed2011-07-01 19:47:50 +00003156 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003157 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003158
cristy3a37efd2011-08-28 20:31:03 +00003159 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003160 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003161 break;
cristy4c08aed2011-07-01 19:47:50 +00003162 length=ExportQuantumPixels(image,(CacheView *) NULL,
cristy3a37efd2011-08-28 20:31:03 +00003163 quantum_info,AlphaQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003164 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3165 break;
3166 }
3167 if (image->previous == (Image *) NULL)
3168 {
3169 status=SetImageProgress(image,SaveImageTag,400,400);
3170 if (status == MagickFalse)
3171 break;
3172 }
3173 break;
3174 }
3175 }
3176 break;
3177 }
3178 case PHOTOMETRIC_SEPARATED:
3179 {
3180 /*
3181 CMYK TIFF image.
3182 */
3183 quantum_type=CMYKQuantum;
3184 if (image->matte != MagickFalse)
3185 quantum_type=CMYKAQuantum;
3186 if (image->colorspace != CMYKColorspace)
3187 (void) TransformImageColorspace(image,CMYKColorspace);
cristybb503372010-05-27 20:51:26 +00003188 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003189 {
cristy4c08aed2011-07-01 19:47:50 +00003190 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003191 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003192
cristy3a37efd2011-08-28 20:31:03 +00003193 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003194 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003195 break;
cristy4c08aed2011-07-01 19:47:50 +00003196 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003197 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003198 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3199 break;
3200 if (image->previous == (Image *) NULL)
3201 {
cristycee97112010-05-28 00:44:52 +00003202 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3203 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003204 if (status == MagickFalse)
3205 break;
3206 }
3207 }
3208 break;
3209 }
3210 case PHOTOMETRIC_PALETTE:
3211 {
3212 uint16
3213 *blue,
3214 *green,
3215 *red;
3216
3217 /*
3218 Colormapped TIFF image.
3219 */
3220 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3221 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3222 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3223 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3224 (blue == (uint16 *) NULL))
3225 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3226 /*
3227 Initialize TIFF colormap.
3228 */
3229 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3230 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3231 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003232 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003233 {
3234 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3235 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3236 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3237 }
3238 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3239 red=(uint16 *) RelinquishMagickMemory(red);
3240 green=(uint16 *) RelinquishMagickMemory(green);
3241 blue=(uint16 *) RelinquishMagickMemory(blue);
3242 }
3243 default:
3244 {
3245 /*
3246 Convert PseudoClass packets to contiguous grayscale scanlines.
3247 */
3248 quantum_type=IndexQuantum;
3249 if (image->matte != MagickFalse)
3250 {
3251 if (photometric != PHOTOMETRIC_PALETTE)
3252 quantum_type=GrayAlphaQuantum;
3253 else
3254 quantum_type=IndexAlphaQuantum;
3255 }
3256 else
3257 if (photometric != PHOTOMETRIC_PALETTE)
3258 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003259 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003260 {
cristy4c08aed2011-07-01 19:47:50 +00003261 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003262 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003263
cristy3a37efd2011-08-28 20:31:03 +00003264 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003265 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003266 break;
cristy4c08aed2011-07-01 19:47:50 +00003267 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003268 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003269 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3270 break;
3271 if (image->previous == (Image *) NULL)
3272 {
cristycee97112010-05-28 00:44:52 +00003273 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3274 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003275 if (status == MagickFalse)
3276 break;
3277 }
3278 }
3279 break;
3280 }
3281 }
3282 quantum_info=DestroyQuantumInfo(quantum_info);
3283 DestroyTIFFInfo(&tiff_info);
3284 if (0 && (image_info->verbose == MagickTrue))
3285 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3286 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003287 image->endian=MSBEndian;
3288 if (endian == FILLORDER_LSB2MSB)
3289 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003290 image=SyncNextImageInList(image);
3291 if (image == (Image *) NULL)
3292 break;
3293 status=SetImageProgress(image,SaveImagesTag,scene++,
3294 GetImageListLength(image));
3295 if (status == MagickFalse)
3296 break;
3297 } while (image_info->adjoin != MagickFalse);
3298 (void) TIFFSetWarningHandler(warning_handler);
3299 (void) TIFFSetErrorHandler(error_handler);
3300 TIFFClose(tiff);
3301 return(MagickTrue);
3302}
3303#endif