blob: 28fb7fb9b6593c643a1a0b51f9805f79e4db2f5c [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
cristy98ca0f52011-10-08 23:19:10 +000094# if !defined(TIFFTAG_COPYRIGHT)
95# define TIFFTAG_COPYRIGHT 33432
96# endif
97# if !defined(TIFFTAG_OPIIMAGEID)
98# define TIFF_OPIIMAGEID 32781
99# endif
cristy3ed852e2009-09-05 21:47:34 +0000100
101/*
102 Typedef declarations.
103*/
104#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
105typedef struct _ExifInfo
106{
107 unsigned int
108 tag,
109 type;
110
111 const char
112 *property;
113} ExifInfo;
114
115static const ExifInfo
116 exif_info[] = {
117 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
118 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
119 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
120 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000121 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy7992e402011-09-16 15:24:22 +0000122 { EXIFTAG_OECF, TIFF_NOTYPE, "exif:OptoelectricConversionFactor" },
123 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, "exif:ExifVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000124 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
125 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000126 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, "exif:ComponentsConfiguration" },
cristy3ed852e2009-09-05 21:47:34 +0000127 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
128 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
129 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
130 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
131 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
132 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
133 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
134 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
135 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
136 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
137 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
cristy7992e402011-09-16 15:24:22 +0000138 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, "exif:SubjectArea" },
139 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, "exif:MakerNote" },
140 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, "exif:UserComment" },
cristy3ed852e2009-09-05 21:47:34 +0000141 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
142 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
143 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
cristy7992e402011-09-16 15:24:22 +0000144 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, "exif:FlashpixVersion" },
cristy3ed852e2009-09-05 21:47:34 +0000145 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
146 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
147 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
148 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
149 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
150 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
cristy7992e402011-09-16 15:24:22 +0000151 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, "exif:SpatialFrequencyResponse" },
cristy3ed852e2009-09-05 21:47:34 +0000152 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
153 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
154 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
155 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
156 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
157 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
cristy7992e402011-09-16 15:24:22 +0000158 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, "exif:FileSource" },
159 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, "exif:SceneType" },
160 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, "exif:CFAPattern" },
cristy3ed852e2009-09-05 21:47:34 +0000161 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
162 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
163 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
164 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
165 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
166 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
167 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
168 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
169 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
170 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
cristy7992e402011-09-16 15:24:22 +0000171 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, "exif:DeviceSettingDescription" },
cristy3ed852e2009-09-05 21:47:34 +0000172 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
173 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
174 { 0, 0, (char *) NULL }
175};
176#endif
cristy0805e062011-01-30 02:56:36 +0000177#endif /* MAGICKCORE_TIFF_DELEGATE */
cristy3ed852e2009-09-05 21:47:34 +0000178
179/*
180 Global declarations.
181*/
182static MagickThreadKey
183 tiff_exception;
184
185static SemaphoreInfo
186 *tiff_semaphore = (SemaphoreInfo *) NULL;
187
188static volatile MagickBooleanType
189 instantiate_key = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000190
191/*
192 Forward declarations.
193*/
194#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000195static Image *
196 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
197
cristy3ed852e2009-09-05 21:47:34 +0000198static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +0000199 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
200 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
201 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000202#endif
203
204/*
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206% %
207% %
208% %
209% I s T I F F %
210% %
211% %
212% %
213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214%
215% IsTIFF() returns MagickTrue if the image format type, identified by the
216% magick string, is TIFF.
217%
218% The format of the IsTIFF method is:
219%
220% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
221%
222% A description of each parameter follows:
223%
224% o magick: compare image format pattern against these bytes.
225%
226% o length: Specifies the length of the magick string.
227%
228*/
229static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
230{
231 if (length < 4)
232 return(MagickFalse);
233 if (memcmp(magick,"\115\115\000\052",4) == 0)
234 return(MagickTrue);
235 if (memcmp(magick,"\111\111\052\000",4) == 0)
236 return(MagickTrue);
237#if defined(TIFF_VERSION_BIG)
238 if (length < 8)
239 return(MagickFalse);
240 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
241 return(MagickTrue);
242 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
243 return(MagickTrue);
244#endif
245 return(MagickFalse);
246}
247
248#if defined(MAGICKCORE_TIFF_DELEGATE)
249/*
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251% %
252% %
253% %
cristy3d7f8062009-09-24 20:45:35 +0000254% R e a d G R O U P 4 I m a g e %
255% %
256% %
257% %
258%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
259%
260% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
261% allocates the memory necessary for the new Image structure and returns a
262% pointer to the new image.
263%
264% The format of the ReadGROUP4Image method is:
265%
266% Image *ReadGROUP4Image(const ImageInfo *image_info,
267% ExceptionInfo *exception)
268%
269% A description of each parameter follows:
270%
271% o image_info: the image info.
272%
273% o exception: return any errors or warnings in this structure.
274%
275*/
276
cristyeaedf062010-05-29 22:36:02 +0000277static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000278{
279 unsigned char
280 buffer[4];
281
282 buffer[0]=(unsigned char) value;
283 buffer[1]=(unsigned char) (value >> 8);
284 buffer[2]=(unsigned char) (value >> 16);
285 buffer[3]=(unsigned char) (value >> 24);
286 return(fwrite(buffer,1,4,file));
287}
288
289static Image *ReadGROUP4Image(const ImageInfo *image_info,
290 ExceptionInfo *exception)
291{
292 char
293 filename[MaxTextExtent];
294
295 FILE
296 *file;
297
298 Image
299 *image;
300
301 ImageInfo
302 *read_info;
303
304 int
305 c,
306 unique_file;
307
308 MagickBooleanType
309 status;
310
311 size_t
312 length;
313
314 ssize_t
315 offset,
316 strip_offset;
317
318 /*
319 Open image file.
320 */
321 assert(image_info != (const ImageInfo *) NULL);
322 assert(image_info->signature == MagickSignature);
323 if (image_info->debug != MagickFalse)
324 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
325 image_info->filename);
326 assert(exception != (ExceptionInfo *) NULL);
327 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000328 image=AcquireImage(image_info,exception);
cristy3d7f8062009-09-24 20:45:35 +0000329 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
330 if (status == MagickFalse)
331 {
332 image=DestroyImageList(image);
333 return((Image *) NULL);
334 }
335 /*
336 Write raw CCITT Group 4 wrapped as a TIFF image file.
337 */
338 file=(FILE *) NULL;
339 unique_file=AcquireUniqueFileResource(filename);
340 if (unique_file != -1)
341 file=fdopen(unique_file,"wb");
342 if ((unique_file == -1) || (file == (FILE *) NULL))
343 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
344 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
345 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
346 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
347 length=WriteLSBLong(file,image->columns);
348 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
349 length=WriteLSBLong(file,image->rows);
350 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
351 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
352 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
353 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
354 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000355 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000356 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000357 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000358 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
359 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000360 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000361 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
362 offset=(ssize_t) ftell(file)-4;
363 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000364 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000365 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000366 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000367 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
368 length=fwrite("\000\000\000\000",1,4,file);
cristy94b11832011-09-08 19:46:03 +0000369 length=WriteLSBLong(file,(long) image->x_resolution);
cristy3d7f8062009-09-24 20:45:35 +0000370 length=WriteLSBLong(file,1);
371 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
372 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000373 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000374 length=WriteLSBLong(file,(unsigned int) length);
375 (void) fclose(file);
376 (void) CloseBlob(image);
377 image=DestroyImage(image);
378 /*
379 Read TIFF image.
380 */
cristy14efd992009-09-26 23:46:03 +0000381 read_info=CloneImageInfo((ImageInfo *) NULL);
cristyb51dff52011-05-19 16:55:47 +0000382 (void) FormatLocaleString(read_info->filename,MaxTextExtent,"%s",filename);
cristy3d7f8062009-09-24 20:45:35 +0000383 image=ReadTIFFImage(read_info,exception);
384 read_info=DestroyImageInfo(read_info);
385 if (image != (Image *) NULL)
386 {
387 (void) CopyMagickString(image->filename,image_info->filename,
388 MaxTextExtent);
389 (void) CopyMagickString(image->magick_filename,image_info->filename,
390 MaxTextExtent);
391 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
392 }
393 (void) RelinquishUniqueFileResource(filename);
394 return(image);
395}
396#endif
397
398#if defined(MAGICKCORE_TIFF_DELEGATE)
399/*
400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401% %
402% %
403% %
cristy3ed852e2009-09-05 21:47:34 +0000404% R e a d T I F F I m a g e %
405% %
406% %
407% %
408%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409%
410% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
411% memory necessary for the new Image structure and returns a pointer to the
412% new image.
413%
414% The format of the ReadTIFFImage method is:
415%
416% Image *ReadTIFFImage(const ImageInfo *image_info,
417% ExceptionInfo *exception)
418%
419% A description of each parameter follows:
420%
421% o image_info: the image info.
422%
423% o exception: return any errors or warnings in this structure.
424%
425*/
426
427static inline size_t MagickMax(const size_t x,const size_t y)
428{
429 if (x > y)
430 return(x);
431 return(y);
432}
433
cristybb503372010-05-27 20:51:26 +0000434static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000435{
436 if (x < y)
437 return(x);
438 return(y);
439}
440
441static MagickBooleanType ReadProfile(Image *image,const char *name,
cristy018f07f2011-09-04 21:15:19 +0000442 unsigned char *datum,ssize_t length,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000443{
444 MagickBooleanType
445 status;
446
cristybb503372010-05-27 20:51:26 +0000447 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000448 i;
449
450 StringInfo
451 *profile;
452
453 if (length < 4)
454 return(MagickFalse);
455 i=0;
456 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
457 {
458 for (i=0; i < (length-4); i+=2)
459 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
460 break;
461 if (i == length)
462 length-=i;
463 else
464 i=0;
465 if (length < 4)
466 return(MagickFalse);
467 }
cristye8f8f382011-09-01 13:32:37 +0000468 profile=BlobToStringInfo(datum+i,(size_t) length);
469 if (profile == (StringInfo *) NULL)
470 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
471 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000472 status=SetImageProfile(image,name,profile);
473 profile=DestroyStringInfo(profile);
474 if (status == MagickFalse)
475 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
476 image->filename);
477 return(MagickTrue);
478}
479
480#if defined(__cplusplus) || defined(c_plusplus)
481extern "C" {
482#endif
483
484static int TIFFCloseBlob(thandle_t image)
485{
486 (void) CloseBlob((Image *) image);
487 return(0);
488}
489
490static void TIFFErrors(const char *module,const char *format,va_list error)
491{
492 char
493 message[MaxTextExtent];
494
495 ExceptionInfo
496 *exception;
497
498#if defined(MAGICKCORE_HAVE_VSNPRINTF)
499 (void) vsnprintf(message,MaxTextExtent,format,error);
500#else
501 (void) vsprintf(message,format,error);
502#endif
503 (void) ConcatenateMagickString(message,".",MaxTextExtent);
504 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
505 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000506 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
507 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000508}
509
cristy018f07f2011-09-04 21:15:19 +0000510static void TIFFGetProfiles(TIFF *tiff,Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000511{
512 uint32
513 length;
514
515 unsigned char
516 *profile;
517
518#if defined(TIFFTAG_ICCPROFILE)
519 length=0;
520 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000521 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000522#endif
523#if defined(TIFFTAG_PHOTOSHOP)
524 length=0;
525 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000526 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000527#endif
528#if defined(TIFFTAG_RICHTIFFIPTC)
529 length=0;
530 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
531 {
532 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000533 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy018f07f2011-09-04 21:15:19 +0000534 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000535 }
536#endif
537#if defined(TIFFTAG_XMLPACKET)
538 length=0;
539 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000540 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000541#endif
542 length=0;
543 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristy018f07f2011-09-04 21:15:19 +0000544 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
cristy3ed852e2009-09-05 21:47:34 +0000545}
546
547static void TIFFGetProperties(TIFF *tiff,Image *image)
548{
549 char
cristy98ca0f52011-10-08 23:19:10 +0000550 message[MaxTextExtent],
cristy3ed852e2009-09-05 21:47:34 +0000551 *text;
552
cristy98ca0f52011-10-08 23:19:10 +0000553 uint32
554 count;
555
cristy3ed852e2009-09-05 21:47:34 +0000556 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
557 (void) SetImageProperty(image,"tiff:artist",text);
cristy98ca0f52011-10-08 23:19:10 +0000558 if (TIFFGetField(tiff,TIFFTAG_COPYRIGHT,&text) == 1)
559 (void) SetImageProperty(image,"tiff:copyright",text);
cristy3ed852e2009-09-05 21:47:34 +0000560 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
561 (void) SetImageProperty(image,"tiff:timestamp",text);
cristy3ed852e2009-09-05 21:47:34 +0000562 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
563 (void) SetImageProperty(image,"tiff:document",text);
cristy98ca0f52011-10-08 23:19:10 +0000564 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
565 (void) SetImageProperty(image,"tiff:hostcomputer",text);
566 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
567 (void) SetImageProperty(image,"comment",text);
cristy3ed852e2009-09-05 21:47:34 +0000568 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
569 (void) SetImageProperty(image,"tiff:make",text);
570 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
571 (void) SetImageProperty(image,"tiff:model",text);
cristy98ca0f52011-10-08 23:19:10 +0000572 if (TIFFGetField(tiff,TIFFTAG_OPIIMAGEID,&count,&text) == 1)
573 {
574 if (count >= MaxTextExtent)
575 count=MaxTextExtent-1;
576 (void) CopyMagickString(message,text,count+1);
577 (void) SetImageProperty(image,"tiff:image-id",message);
578 }
cristy3ed852e2009-09-05 21:47:34 +0000579 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
580 (void) SetImageProperty(image,"label",text);
cristy98ca0f52011-10-08 23:19:10 +0000581 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
582 (void) SetImageProperty(image,"tiff:software",text);
583 if (TIFFGetField(tiff,33423,&count,&text) == 1)
584 {
585 if (count >= MaxTextExtent)
586 count=MaxTextExtent-1;
587 (void) CopyMagickString(message,text,count+1);
588 (void) SetImageProperty(image,"tiff:kodak-33423",message);
589 }
590 if (TIFFGetField(tiff,36867,&count,&text) == 1)
591 {
592 if (count >= MaxTextExtent)
593 count=MaxTextExtent-1;
594 (void) CopyMagickString(message,text,count+1);
595 (void) SetImageProperty(image,"tiff:kodak-36867",message);
596 }
cristy3ed852e2009-09-05 21:47:34 +0000597}
598
599static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
600{
601#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
602 char
603 value[MaxTextExtent];
604
cristybb503372010-05-27 20:51:26 +0000605 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000606 i;
607
608 tdir_t
609 directory;
610
611 uint32
612 offset;
613
cristy7992e402011-09-16 15:24:22 +0000614 void
615 *sans;
616
cristy3ed852e2009-09-05 21:47:34 +0000617 /*
618 Read EXIF properties.
619 */
620 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
621 return;
622 directory=TIFFCurrentDirectory(tiff);
623 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
624 return;
cristy7992e402011-09-16 15:24:22 +0000625 sans=NULL;
cristy3ed852e2009-09-05 21:47:34 +0000626 for (i=0; exif_info[i].tag != 0; i++)
627 {
628 *value='\0';
629 switch (exif_info[i].type)
630 {
631 case TIFF_ASCII:
632 {
633 char
634 *ascii;
635
cristy7992e402011-09-16 15:24:22 +0000636 ascii=(char *) NULL;
637 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans) != 0) &&
638 (ascii != (char *) NULL) && (*ascii != '\0'))
cristy3ed852e2009-09-05 21:47:34 +0000639 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
640 break;
641 }
642 case TIFF_SHORT:
643 {
644 uint16
cristy7992e402011-09-16 15:24:22 +0000645 shorty[2] = { 0, 0};
cristy3ed852e2009-09-05 21:47:34 +0000646
cristy7992e402011-09-16 15:24:22 +0000647 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans) != 0)
648 (void) FormatLocaleString(value,MaxTextExtent,"%d",(int) shorty[0]);
cristy3ed852e2009-09-05 21:47:34 +0000649 break;
650 }
651 case TIFF_LONG:
652 {
653 uint32
cristy7992e402011-09-16 15:24:22 +0000654 longy;
cristy3ed852e2009-09-05 21:47:34 +0000655
cristy7992e402011-09-16 15:24:22 +0000656 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans) != 0)
657 (void) FormatLocaleString(value,MaxTextExtent,"%d",longy);
cristy3ed852e2009-09-05 21:47:34 +0000658 break;
659 }
660 case TIFF_RATIONAL:
661 case TIFF_SRATIONAL:
cristy7992e402011-09-16 15:24:22 +0000662 case TIFF_FLOAT:
663 case TIFF_DOUBLE:
cristy3ed852e2009-09-05 21:47:34 +0000664 {
665 float
cristy7992e402011-09-16 15:24:22 +0000666 rational[16];
cristy3ed852e2009-09-05 21:47:34 +0000667
cristy7992e402011-09-16 15:24:22 +0000668 if (TIFFGetField(tiff,exif_info[i].tag,&rational,&sans) != 0)
669 (void) FormatLocaleString(value,MaxTextExtent,"%g",rational[0]);
cristy3ed852e2009-09-05 21:47:34 +0000670 break;
671 }
672 default:
673 break;
674 }
675 if (*value != '\0')
676 (void) SetImageProperty(image,exif_info[i].property,value);
677 }
678 TIFFSetDirectory(tiff,directory);
679#else
680 (void) tiff;
681 (void) image;
682#endif
683}
684
685static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
686{
687 *base=(tdata_t *) GetBlobStreamData((Image *) image);
688 if (*base != (tdata_t *) NULL)
689 *size=(toff_t) GetBlobSize((Image *) image);
690 if (*base != (tdata_t *) NULL)
691 return(1);
692 return(0);
693}
694
695static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
696{
697 tsize_t
698 count;
699
700 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
701 (unsigned char *) data);
702 return(count);
703}
704
cristybb503372010-05-27 20:51:26 +0000705static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
706 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000707{
708 int32
709 status;
710
711 (void) bits_per_sample;
712 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
713 return(status);
714}
715
716static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
717{
718 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
719}
720
721static toff_t TIFFGetBlobSize(thandle_t image)
722{
723 return((toff_t) GetBlobSize((Image *) image));
724}
725
726static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
727{
728 (void) image;
729 (void) base;
730 (void) size;
731}
732
733static void TIFFWarnings(const char *module,const char *format,va_list warning)
734{
735 char
736 message[MaxTextExtent];
737
738 ExceptionInfo
739 *exception;
740
741#if defined(MAGICKCORE_HAVE_VSNPRINTF)
742 (void) vsnprintf(message,MaxTextExtent,format,warning);
743#else
744 (void) vsprintf(message,format,warning);
745#endif
746 (void) ConcatenateMagickString(message,".",MaxTextExtent);
747 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
748 if (exception != (ExceptionInfo *) NULL)
749 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
750 message,"`%s'",module);
751}
752
753static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
754{
755 tsize_t
756 count;
757
758 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
759 (unsigned char *) data);
760 return(count);
761}
762
763#if defined(__cplusplus) || defined(c_plusplus)
764}
765#endif
766
767static Image *ReadTIFFImage(const ImageInfo *image_info,
768 ExceptionInfo *exception)
769{
770 typedef enum
771 {
772 ReadSingleSampleMethod,
773 ReadRGBAMethod,
774 ReadCMYKAMethod,
775 ReadStripMethod,
776 ReadTileMethod,
777 ReadGenericMethod
778 } TIFFMethodType;
779
780 const char
781 *option;
782
783 float
784 *chromaticity,
785 x_position,
786 y_position,
787 x_resolution,
788 y_resolution;
789
790 Image
791 *image;
792
cristy3ed852e2009-09-05 21:47:34 +0000793 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000794 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000795 debug,
796 status;
797
798 MagickSizeType
799 number_pixels;
800
801 QuantumInfo
802 *quantum_info;
803
804 QuantumType
805 quantum_type;
806
cristybb503372010-05-27 20:51:26 +0000807 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000808 i;
809
810 size_t
811 length,
cristyc6da28e2011-04-28 01:41:35 +0000812 lsb_first,
cristy3ed852e2009-09-05 21:47:34 +0000813 pad;
814
cristyc6da28e2011-04-28 01:41:35 +0000815 ssize_t
816 y;
817
cristy3ed852e2009-09-05 21:47:34 +0000818 TIFF
819 *tiff;
820
821 TIFFErrorHandler
822 error_handler,
823 warning_handler;
824
825 TIFFMethodType
826 method;
827
828 uint16
829 compress_tag,
830 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000831 endian,
cristy3ed852e2009-09-05 21:47:34 +0000832 extra_samples,
833 interlace,
834 max_sample_value,
835 min_sample_value,
836 orientation,
837 pages,
838 photometric,
839 *sample_info,
840 sample_format,
841 samples_per_pixel,
842 units,
843 value;
844
845 uint32
846 height,
847 rows_per_strip,
848 width;
849
850 unsigned char
851 *pixels;
852
cristy3ed852e2009-09-05 21:47:34 +0000853 /*
854 Open image.
855 */
856 assert(image_info != (const ImageInfo *) NULL);
857 assert(image_info->signature == MagickSignature);
858 if (image_info->debug != MagickFalse)
859 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
860 image_info->filename);
861 assert(exception != (ExceptionInfo *) NULL);
862 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000863 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000864 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
865 if (status == MagickFalse)
866 {
867 image=DestroyImageList(image);
868 return((Image *) NULL);
869 }
870 (void) MagickSetThreadValue(tiff_exception,exception);
871 error_handler=TIFFSetErrorHandler(TIFFErrors);
872 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
cristy1630f7f2011-02-18 01:10:02 +0000873 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
cristy3ed852e2009-09-05 21:47:34 +0000874 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
875 TIFFUnmapBlob);
876 if (tiff == (TIFF *) NULL)
877 {
878 (void) TIFFSetWarningHandler(warning_handler);
879 (void) TIFFSetErrorHandler(error_handler);
880 image=DestroyImageList(image);
881 return((Image *) NULL);
882 }
883 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +0000884 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +0000885 if (image_info->number_scenes != 0)
886 {
887 /*
888 Generate blank images for subimage specification (e.g. image.tif[4].
889 */
cristybb503372010-05-27 20:51:26 +0000890 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000891 {
892 (void) TIFFReadDirectory(tiff);
cristy9950d572011-10-01 18:22:35 +0000893 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000894 if (GetNextImageInList(image) == (Image *) NULL)
895 {
896 image=DestroyImageList(image);
897 return((Image *) NULL);
898 }
899 image=SyncNextImageInList(image);
900 }
901 }
902 do
903 {
904 if (0 && (image_info->verbose != MagickFalse))
905 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristy6b032822010-06-29 16:52:32 +0000906 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
907 "lsb" : "msb");
cristy3ed852e2009-09-05 21:47:34 +0000908 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
909 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
910 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
911 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000912 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000913 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
914 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
915 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
cristyae1319c2011-01-19 20:18:53 +0000916 if (sample_format == SAMPLEFORMAT_IEEEFP)
917 (void) SetImageProperty(image,"quantum:format","floating-point");
cristy3ed852e2009-09-05 21:47:34 +0000918 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
919 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
920 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
921 switch (photometric)
922 {
923 case PHOTOMETRIC_MINISBLACK:
924 {
925 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
926 break;
927 }
928 case PHOTOMETRIC_MINISWHITE:
929 {
930 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
931 break;
932 }
933 case PHOTOMETRIC_PALETTE:
934 {
935 (void) SetImageProperty(image,"tiff:photometric","palette");
936 break;
937 }
938 case PHOTOMETRIC_RGB:
939 {
940 (void) SetImageProperty(image,"tiff:photometric","RGB");
941 break;
942 }
943 case PHOTOMETRIC_CIELAB:
944 {
945 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
946 break;
947 }
948 case PHOTOMETRIC_SEPARATED:
949 {
950 (void) SetImageProperty(image,"tiff:photometric","separated");
951 break;
952 }
cristy79e5dad2010-09-16 19:48:33 +0000953 case PHOTOMETRIC_YCBCR:
954 {
955 (void) SetImageProperty(image,"tiff:photometric","YCBCR");
956 break;
957 }
cristy3ed852e2009-09-05 21:47:34 +0000958 default:
959 {
960 (void) SetImageProperty(image,"tiff:photometric","unknown");
961 break;
962 }
963 }
964 if (image->debug != MagickFalse)
965 {
966 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
967 (unsigned int) width,(unsigned int) height);
968 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
969 interlace);
970 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
971 "Bits per sample: %u",bits_per_sample);
972 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
973 "Min sample value: %u",min_sample_value);
974 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
975 "Max sample value: %u",max_sample_value);
976 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
977 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
978 }
cristybb503372010-05-27 20:51:26 +0000979 image->columns=(size_t) width;
980 image->rows=(size_t) height;
981 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +0000982 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +0000983 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
984 (double) image->depth);
cristyf2687ca2010-06-29 16:32:38 +0000985 lsb_first=1;
986 image->endian=MSBEndian;
987 if ((int) (*(char *) &lsb_first) != 0)
988 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +0000989 if (photometric == PHOTOMETRIC_SEPARATED)
990 image->colorspace=CMYKColorspace;
991 if (photometric == PHOTOMETRIC_CIELAB)
992 image->colorspace=LabColorspace;
993 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
994 &samples_per_pixel);
995 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
996 x_resolution=(float) image->x_resolution;
997 y_resolution=(float) image->y_resolution;
998 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
999 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
1000 image->x_resolution=x_resolution;
1001 image->y_resolution=y_resolution;
1002 x_position=(float) image->page.x/x_resolution;
1003 y_position=(float) image->page.y/y_resolution;
1004 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
1005 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +00001006 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
1007 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +00001008 image->orientation=(OrientationType) orientation;
1009 chromaticity=(float *) NULL;
1010 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
1011 if (chromaticity != (float *) NULL)
1012 {
1013 image->chromaticity.white_point.x=chromaticity[0];
1014 image->chromaticity.white_point.y=chromaticity[1];
1015 }
1016 chromaticity=(float *) NULL;
1017 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
1018 if (chromaticity != (float *) NULL)
1019 {
1020 image->chromaticity.red_primary.x=chromaticity[0];
1021 image->chromaticity.red_primary.y=chromaticity[1];
1022 image->chromaticity.green_primary.x=chromaticity[2];
1023 image->chromaticity.green_primary.y=chromaticity[3];
1024 image->chromaticity.blue_primary.x=chromaticity[4];
1025 image->chromaticity.blue_primary.y=chromaticity[5];
1026 }
1027 TIFFGetProperties(tiff,image);
cristy362980c2011-09-16 23:59:27 +00001028 TIFFGetEXIFProperties(tiff,image);
cristy018f07f2011-09-04 21:15:19 +00001029 TIFFGetProfiles(tiff,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001030 /*
1031 Allocate memory for the image and pixel buffer.
1032 */
1033#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1034 if ((compress_tag != COMPRESSION_NONE) &&
1035 (TIFFIsCODECConfigured(compress_tag) == 0))
1036 {
1037 TIFFClose(tiff);
1038 ThrowReaderException(CoderError,"CompressNotSupported");
1039 }
1040#endif
1041 switch (compress_tag)
1042 {
1043 case COMPRESSION_NONE: image->compression=NoCompression; break;
1044 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1045 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1046 case COMPRESSION_JPEG:
1047 {
1048 image->compression=JPEGCompression;
1049#if defined(JPEG_SUPPORT)
1050 {
1051 char
1052 sampling_factor[MaxTextExtent];
1053
1054 uint16
1055 horizontal,
1056 vertical;
1057
1058 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1059 &horizontal,&vertical);
cristyb51dff52011-05-19 16:55:47 +00001060 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
cristy3ed852e2009-09-05 21:47:34 +00001061 horizontal,vertical);
1062 (void) SetImageProperty(image,"jpeg:sampling-factor",
1063 sampling_factor);
1064 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1065 "Sampling Factors: %s",sampling_factor);
1066 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1067 {
1068 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1069 JPEGCOLORMODE_RGB);
1070 photometric=PHOTOMETRIC_RGB;
1071 }
1072 }
1073#endif
1074 break;
1075 }
1076 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001077#if defined(COMPRESSION_LZMA)
1078 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1079#endif
cristy3ed852e2009-09-05 21:47:34 +00001080 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1081 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1082 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1083 default: image->compression=RLECompression; break;
1084 }
1085 quantum_info=AcquireQuantumInfo(image_info,image);
1086 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001087 {
1088 TIFFClose(tiff);
1089 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1090 }
cristy3ed852e2009-09-05 21:47:34 +00001091 if (sample_format == SAMPLEFORMAT_UINT)
1092 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1093 if (sample_format == SAMPLEFORMAT_INT)
1094 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1095 if (sample_format == SAMPLEFORMAT_IEEEFP)
1096 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1097 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001098 {
1099 TIFFClose(tiff);
1100 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1101 }
cristy3ed852e2009-09-05 21:47:34 +00001102 status=MagickTrue;
1103 switch (photometric)
1104 {
1105 case PHOTOMETRIC_MINISBLACK:
1106 {
1107 quantum_info->min_is_white=MagickFalse;
1108 break;
1109 }
1110 case PHOTOMETRIC_MINISWHITE:
1111 {
1112 quantum_info->min_is_white=MagickTrue;
1113 break;
1114 }
1115 default:
1116 break;
1117 }
cristy891dc792010-03-04 01:47:16 +00001118 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001119 extra_samples=0;
1120 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1121 &sample_info);
1122 if (extra_samples == 0)
1123 {
1124 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1125 image->matte=MagickTrue;
1126 }
1127 else
cristy6c207342010-02-19 17:49:50 +00001128 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001129 {
cristy7cf69862010-03-03 13:57:24 +00001130 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001131 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001132 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001133 }
cristy891dc792010-03-04 01:47:16 +00001134 option=GetImageOption(image_info,"tiff:alpha");
1135 if (option != (const char *) NULL)
1136 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1137 MagickFalse;
1138 if (image->matte != MagickFalse)
1139 (void) SetImageProperty(image,"tiff:alpha",
1140 associated_alpha != MagickFalse ? "associated" : "unassociated");
cristy3ed852e2009-09-05 21:47:34 +00001141 if ((photometric == PHOTOMETRIC_PALETTE) &&
1142 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1143 {
cristybb503372010-05-27 20:51:26 +00001144 size_t
cristy3ed852e2009-09-05 21:47:34 +00001145 colors;
1146
cristybb503372010-05-27 20:51:26 +00001147 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy018f07f2011-09-04 21:15:19 +00001148 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001149 {
1150 TIFFClose(tiff);
1151 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1152 }
1153 }
1154 if (units == RESUNIT_INCH)
1155 image->units=PixelsPerInchResolution;
1156 if (units == RESUNIT_CENTIMETER)
1157 image->units=PixelsPerCentimeterResolution;
1158 value=(unsigned short) image->scene;
1159 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1160 image->scene=value;
1161 if (image_info->ping != MagickFalse)
1162 {
1163 if (image_info->number_scenes != 0)
1164 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1165 break;
1166 goto next_tiff_frame;
1167 }
1168 method=ReadGenericMethod;
1169 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1170 {
1171 char
1172 value[MaxTextExtent];
1173
1174 method=ReadStripMethod;
cristyb51dff52011-05-19 16:55:47 +00001175 (void) FormatLocaleString(value,MaxTextExtent,"%u",
cristy3ed852e2009-09-05 21:47:34 +00001176 (unsigned int) rows_per_strip);
1177 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1178 }
1179 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1180 method=ReadRGBAMethod;
1181 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1182 method=ReadCMYKAMethod;
1183 if ((photometric != PHOTOMETRIC_RGB) &&
1184 (photometric != PHOTOMETRIC_CIELAB) &&
1185 (photometric != PHOTOMETRIC_SEPARATED))
1186 method=ReadGenericMethod;
1187 if (image->storage_class == PseudoClass)
1188 method=ReadSingleSampleMethod;
1189 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1190 (photometric == PHOTOMETRIC_MINISWHITE))
1191 method=ReadSingleSampleMethod;
1192 if ((photometric != PHOTOMETRIC_SEPARATED) &&
cristybc736662011-03-09 13:39:42 +00001193 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
cristy3ed852e2009-09-05 21:47:34 +00001194 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001195 if (image->compression == JPEGCompression)
1196 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001197 if (TIFFIsTiled(tiff) != MagickFalse)
1198 method=ReadTileMethod;
1199 quantum_type=RGBQuantum;
1200 pixels=GetQuantumPixels(quantum_info);
1201 switch (method)
1202 {
1203 case ReadSingleSampleMethod:
1204 {
1205 /*
1206 Convert TIFF image to PseudoClass MIFF image.
1207 */
1208 if ((image->storage_class == PseudoClass) &&
1209 (photometric == PHOTOMETRIC_PALETTE))
1210 {
cristybb503372010-05-27 20:51:26 +00001211 size_t
cristy3ed852e2009-09-05 21:47:34 +00001212 range;
1213
1214 uint16
1215 *blue_colormap,
1216 *green_colormap,
1217 *red_colormap;
1218
1219 /*
1220 Initialize colormap.
1221 */
1222 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1223 &green_colormap,&blue_colormap);
1224 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001225 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001226 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1227 (blue_colormap[i] >= 256))
1228 {
1229 range=65535;
1230 break;
1231 }
cristybb503372010-05-27 20:51:26 +00001232 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001233 {
cristyce70c172010-01-07 17:15:30 +00001234 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001235 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001236 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001237 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001238 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001239 blue_colormap[i])/range);
1240 }
1241 }
1242 quantum_type=IndexQuantum;
1243 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1244 if (image->matte != MagickFalse)
1245 {
1246 if (image->storage_class != PseudoClass)
1247 {
1248 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1249 GrayAlphaQuantum;
1250 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1251 }
1252 else
1253 {
1254 quantum_type=IndexAlphaQuantum;
1255 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1256 }
1257 }
1258 else
1259 if (image->storage_class != PseudoClass)
1260 {
1261 quantum_type=GrayQuantum;
1262 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1263 }
1264 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1265 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001266 {
1267 TIFFClose(tiff);
1268 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1269 }
cristy3ed852e2009-09-05 21:47:34 +00001270 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001271 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001272 {
1273 int
1274 status;
1275
cristy4c08aed2011-07-01 19:47:50 +00001276 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001277 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001278
1279 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1280 if (status == -1)
1281 break;
1282 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001283 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001284 break;
1285 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1286 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00001287 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00001288 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1289 break;
1290 if (image->previous == (Image *) NULL)
1291 {
cristycee97112010-05-28 00:44:52 +00001292 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1293 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001294 if (status == MagickFalse)
1295 break;
1296 }
1297 }
1298 break;
1299 }
1300 case ReadRGBAMethod:
1301 {
1302 /*
1303 Convert TIFF image to DirectClass MIFF image.
1304 */
1305 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1306 quantum_type=RGBQuantum;
1307 if (image->matte != MagickFalse)
1308 {
1309 quantum_type=RGBAQuantum;
1310 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1311 }
1312 if (image->colorspace == CMYKColorspace)
1313 {
1314 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1315 quantum_type=CMYKQuantum;
1316 if (image->matte != MagickFalse)
1317 {
1318 quantum_type=CMYKAQuantum;
1319 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1320 }
1321 }
1322 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1323 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001324 {
1325 TIFFClose(tiff);
1326 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1327 }
cristy3ed852e2009-09-05 21:47:34 +00001328 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001329 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001330 {
1331 int
1332 status;
1333
cristy4c08aed2011-07-01 19:47:50 +00001334 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001335 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001336
1337 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1338 if (status == -1)
1339 break;
1340 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001341 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001342 break;
cristy0b334892010-03-15 02:26:46 +00001343 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1344 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001345 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1346 break;
1347 if (image->previous == (Image *) NULL)
1348 {
cristycee97112010-05-28 00:44:52 +00001349 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1350 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001351 if (status == MagickFalse)
1352 break;
1353 }
1354 }
1355 break;
1356 }
1357 case ReadCMYKAMethod:
1358 {
1359 /*
1360 Convert TIFF image to DirectClass MIFF image.
1361 */
cristybb503372010-05-27 20:51:26 +00001362 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001363 {
cristybb503372010-05-27 20:51:26 +00001364 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001365 {
cristy4c08aed2011-07-01 19:47:50 +00001366 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001367 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001368
cristy3ed852e2009-09-05 21:47:34 +00001369 int
1370 status;
1371
1372 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1373 pixels);
1374 if (status == -1)
1375 break;
1376 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001377 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001378 break;
1379 if (image->colorspace != CMYKColorspace)
1380 switch (i)
1381 {
1382 case 0: quantum_type=RedQuantum; break;
1383 case 1: quantum_type=GreenQuantum; break;
1384 case 2: quantum_type=BlueQuantum; break;
1385 case 3: quantum_type=AlphaQuantum; break;
1386 default: quantum_type=UndefinedQuantum; break;
1387 }
cristy6d9f12f2009-11-03 14:50:26 +00001388 else
cristy3ed852e2009-09-05 21:47:34 +00001389 switch (i)
1390 {
1391 case 0: quantum_type=CyanQuantum; break;
1392 case 1: quantum_type=MagentaQuantum; break;
1393 case 2: quantum_type=YellowQuantum; break;
1394 case 3: quantum_type=BlackQuantum; break;
1395 case 4: quantum_type=AlphaQuantum; break;
1396 default: quantum_type=UndefinedQuantum; break;
1397 }
1398 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1399 quantum_type,pixels,exception);
1400 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1401 break;
1402 }
1403 if (image->previous == (Image *) NULL)
1404 {
cristycee97112010-05-28 00:44:52 +00001405 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1406 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001407 if (status == MagickFalse)
1408 break;
1409 }
1410 }
1411 break;
1412 }
1413 case ReadStripMethod:
1414 {
1415 register unsigned char
1416 *p;
1417
1418 /*
1419 Convert stripped TIFF image to DirectClass MIFF image.
1420 */
1421 i=0;
1422 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001423 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001424 {
cristybb503372010-05-27 20:51:26 +00001425 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001426 x;
1427
cristy4c08aed2011-07-01 19:47:50 +00001428 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001429 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001430
1431 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001432 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001433 break;
1434 if (i == 0)
1435 {
1436 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1437 break;
cristyeaedf062010-05-29 22:36:02 +00001438 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1439 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001440 }
1441 i--;
cristy7f5d1662011-04-14 12:43:28 +00001442 p=(unsigned char *) (((uint32 *) pixels)+image->columns*i);
cristybb503372010-05-27 20:51:26 +00001443 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001444 {
cristy4c08aed2011-07-01 19:47:50 +00001445 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1446 (TIFFGetR(*p))),q);
1447 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1448 (TIFFGetG(*p))),q);
1449 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1450 (TIFFGetB(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001451 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001452 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1453 (TIFFGetA(*p))),q);
cristy3ed852e2009-09-05 21:47:34 +00001454 p++;
cristyed231572011-07-14 02:18:59 +00001455 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001456 }
1457 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1458 break;
1459 if (image->previous == (Image *) NULL)
1460 {
cristycee97112010-05-28 00:44:52 +00001461 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1462 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001463 if (status == MagickFalse)
1464 break;
1465 }
1466 }
1467 break;
1468 }
1469 case ReadTileMethod:
1470 {
1471 register uint32
1472 *p;
1473
1474 uint32
1475 *tile_pixels,
1476 columns,
1477 rows;
1478
cristybb503372010-05-27 20:51:26 +00001479 size_t
cristy3ed852e2009-09-05 21:47:34 +00001480 number_pixels;
1481
1482 /*
1483 Convert tiled TIFF image to DirectClass MIFF image.
1484 */
1485 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1486 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1487 {
1488 TIFFClose(tiff);
1489 ThrowReaderException(CoderError,"ImageIsNotTiled");
1490 }
cristy574cc262011-08-05 01:23:58 +00001491 (void) SetImageStorageClass(image,DirectClass,&image->exception);
cristy3ed852e2009-09-05 21:47:34 +00001492 number_pixels=columns*rows;
cristyda16f162011-02-19 23:52:17 +00001493 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
cristy3ed852e2009-09-05 21:47:34 +00001494 sizeof(*tile_pixels));
1495 if (tile_pixels == (uint32 *) NULL)
1496 {
1497 TIFFClose(tiff);
1498 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1499 }
cristybb503372010-05-27 20:51:26 +00001500 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001501 {
cristybb503372010-05-27 20:51:26 +00001502 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001503 x;
1504
cristy4c08aed2011-07-01 19:47:50 +00001505 register Quantum
1506 *restrict q,
1507 *restrict tile;
cristy3ed852e2009-09-05 21:47:34 +00001508
cristybb503372010-05-27 20:51:26 +00001509 size_t
cristy3ed852e2009-09-05 21:47:34 +00001510 columns_remaining,
1511 rows_remaining;
1512
1513 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001514 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001515 rows_remaining=rows;
1516 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1517 exception);
cristy4c08aed2011-07-01 19:47:50 +00001518 if (tile == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001519 break;
cristybb503372010-05-27 20:51:26 +00001520 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001521 {
cristybb503372010-05-27 20:51:26 +00001522 size_t
cristy3ed852e2009-09-05 21:47:34 +00001523 column,
1524 row;
1525
1526 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1527 break;
1528 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001529 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001530 columns_remaining=columns;
1531 p=tile_pixels+(rows-rows_remaining)*columns;
cristy68473222011-08-17 17:39:19 +00001532 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1533 x);
cristy3ed852e2009-09-05 21:47:34 +00001534 for (row=rows_remaining; row > 0; row--)
1535 {
1536 if (image->matte != MagickFalse)
1537 for (column=columns_remaining; column > 0; column--)
1538 {
cristy4c08aed2011-07-01 19:47:50 +00001539 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1540 TIFFGetR(*p)),q);
1541 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1542 TIFFGetG(*p)),q);
1543 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1544 TIFFGetB(*p)),q);
1545 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1546 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001547 p++;
cristyed231572011-07-14 02:18:59 +00001548 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001549 }
1550 else
1551 for (column=columns_remaining; column > 0; column--)
1552 {
cristy4c08aed2011-07-01 19:47:50 +00001553 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1554 TIFFGetR(*p)),q);
1555 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1556 TIFFGetG(*p)),q);
1557 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1558 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001559 p++;
cristyed231572011-07-14 02:18:59 +00001560 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001561 }
1562 p+=columns-columns_remaining;
cristyed231572011-07-14 02:18:59 +00001563 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
cristy3ed852e2009-09-05 21:47:34 +00001564 }
1565 }
1566 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1567 break;
1568 if (image->previous == (Image *) NULL)
1569 {
cristycee97112010-05-28 00:44:52 +00001570 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1571 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001572 if (status == MagickFalse)
1573 break;
1574 }
1575 }
1576 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1577 break;
1578 }
1579 case ReadGenericMethod:
1580 default:
1581 {
1582 register uint32
1583 *p;
1584
1585 uint32
1586 *pixels;
1587
1588 /*
1589 Convert TIFF image to DirectClass MIFF image.
1590 */
1591 number_pixels=(MagickSizeType) image->columns*image->rows;
1592 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1593 (number_pixels*sizeof(uint32))))
1594 {
1595 TIFFClose(tiff);
1596 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1597 }
1598 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1599 sizeof(uint32));
1600 if (pixels == (uint32 *) NULL)
1601 {
1602 TIFFClose(tiff);
1603 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1604 }
1605 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1606 (uint32) image->rows,(uint32 *) pixels,0);
1607 /*
1608 Convert image to DirectClass pixel packets.
1609 */
1610 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001611 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001612 {
cristybb503372010-05-27 20:51:26 +00001613 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001614 x;
1615
cristy4c08aed2011-07-01 19:47:50 +00001616 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001617 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001618
1619 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001620 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001621 break;
cristyed231572011-07-14 02:18:59 +00001622 q+=GetPixelChannels(image)*(image->columns-1);
cristybb503372010-05-27 20:51:26 +00001623 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001624 {
cristy4c08aed2011-07-01 19:47:50 +00001625 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1626 TIFFGetR(*p)),q);
1627 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1628 TIFFGetG(*p)),q);
1629 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1630 TIFFGetB(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001631 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001632 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1633 TIFFGetA(*p)),q);
cristy3ed852e2009-09-05 21:47:34 +00001634 p--;
cristyed231572011-07-14 02:18:59 +00001635 q-=GetPixelChannels(image);;
cristy3ed852e2009-09-05 21:47:34 +00001636 }
1637 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1638 break;
1639 if (image->previous == (Image *) NULL)
1640 {
cristycee97112010-05-28 00:44:52 +00001641 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1642 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001643 if (status == MagickFalse)
1644 break;
1645 }
1646 }
1647 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1648 break;
1649 }
1650 }
1651 SetQuantumImageType(image,quantum_type);
1652 next_tiff_frame:
1653 if ((photometric == PHOTOMETRIC_LOGL) ||
1654 (photometric == PHOTOMETRIC_MINISBLACK) ||
1655 (photometric == PHOTOMETRIC_MINISWHITE))
1656 {
1657 image->type=GrayscaleType;
1658 if (bits_per_sample == 1)
1659 image->type=BilevelType;
1660 }
1661 if (image->storage_class == PseudoClass)
1662 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001663 image->endian=MSBEndian;
1664 if (endian == FILLORDER_LSB2MSB)
1665 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001666 if ((photometric == PHOTOMETRIC_LOGL) ||
1667 (photometric == PHOTOMETRIC_MINISBLACK) ||
1668 (photometric == PHOTOMETRIC_MINISWHITE))
1669 {
1670 image->type=GrayscaleType;
1671 if (bits_per_sample == 1)
1672 image->type=BilevelType;
1673 }
1674 /*
1675 Proceed to next image.
1676 */
1677 if (image_info->number_scenes != 0)
1678 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1679 break;
1680 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1681 if (status == MagickTrue)
1682 {
1683 /*
1684 Allocate next image structure.
1685 */
cristy9950d572011-10-01 18:22:35 +00001686 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001687 if (GetNextImageInList(image) == (Image *) NULL)
1688 {
1689 image=DestroyImageList(image);
1690 return((Image *) NULL);
1691 }
1692 image=SyncNextImageInList(image);
1693 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1694 image->scene);
1695 if (status == MagickFalse)
1696 break;
1697 }
1698 quantum_info=DestroyQuantumInfo(quantum_info);
1699 } while (status == MagickTrue);
1700 (void) TIFFSetWarningHandler(warning_handler);
1701 (void) TIFFSetErrorHandler(error_handler);
1702 TIFFClose(tiff);
1703 return(GetFirstImageInList(image));
1704}
1705#endif
1706
1707/*
1708%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1709% %
1710% %
1711% %
1712% R e g i s t e r T I F F I m a g e %
1713% %
1714% %
1715% %
1716%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1717%
1718% RegisterTIFFImage() adds properties for the TIFF image format to
1719% the list of supported formats. The properties include the image format
1720% tag, a method to read and/or write the format, whether the format
1721% supports the saving of more than one frame to the same file or blob,
1722% whether the format supports native in-memory I/O, and a brief
1723% description of the format.
1724%
1725% The format of the RegisterTIFFImage method is:
1726%
cristybb503372010-05-27 20:51:26 +00001727% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001728%
1729*/
cristybb503372010-05-27 20:51:26 +00001730ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001731{
1732#define TIFFDescription "Tagged Image File Format"
1733
1734 char
1735 version[MaxTextExtent];
1736
1737 MagickInfo
1738 *entry;
cristy6b032822010-06-29 16:52:32 +00001739
cristy18b17442009-10-25 18:36:48 +00001740 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1741 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001742 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001743 if (instantiate_key == MagickFalse)
1744 {
1745 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1746 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1747 instantiate_key=MagickTrue;
1748 }
cristyf84a1932010-01-03 18:00:18 +00001749 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001750 *version='\0';
1751#if defined(TIFF_VERSION)
cristyb51dff52011-05-19 16:55:47 +00001752 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
cristy3ed852e2009-09-05 21:47:34 +00001753#endif
1754#if defined(MAGICKCORE_TIFF_DELEGATE)
1755 {
1756 const char
1757 *p;
1758
cristybb503372010-05-27 20:51:26 +00001759 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001760 i;
1761
1762 p=TIFFGetVersion();
1763 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1764 version[i]=(*p++);
1765 version[i]='\0';
1766 }
1767#endif
1768
cristy3d7f8062009-09-24 20:45:35 +00001769 entry=SetMagickInfo("GROUP4");
1770#if defined(MAGICKCORE_TIFF_DELEGATE)
1771 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1772 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1773#endif
1774 entry->raw=MagickTrue;
1775 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001776 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001777 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001778 entry->seekable_stream=MagickTrue;
1779 entry->thread_support=NoThreadSupport;
1780 entry->description=ConstantString("Raw CCITT Group4");
1781 entry->module=ConstantString("TIFF");
1782 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001783 entry=SetMagickInfo("PTIF");
1784#if defined(MAGICKCORE_TIFF_DELEGATE)
1785 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1786 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1787#endif
1788 entry->endian_support=MagickTrue;
1789 entry->seekable_stream=MagickTrue;
1790 entry->thread_support=NoThreadSupport;
1791 entry->description=ConstantString("Pyramid encoded TIFF");
1792 entry->module=ConstantString("TIFF");
1793 (void) RegisterMagickInfo(entry);
1794 entry=SetMagickInfo("TIF");
1795#if defined(MAGICKCORE_TIFF_DELEGATE)
1796 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1797 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1798#endif
1799 entry->endian_support=MagickTrue;
1800 entry->seekable_stream=MagickTrue;
1801 entry->stealth=MagickTrue;
1802 entry->thread_support=NoThreadSupport;
1803 entry->description=ConstantString(TIFFDescription);
1804 if (*version != '\0')
1805 entry->version=ConstantString(version);
1806 entry->module=ConstantString("TIFF");
1807 (void) RegisterMagickInfo(entry);
1808 entry=SetMagickInfo("TIFF");
1809#if defined(MAGICKCORE_TIFF_DELEGATE)
1810 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1811 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1812#endif
1813 entry->magick=(IsImageFormatHandler *) IsTIFF;
1814 entry->endian_support=MagickTrue;
1815 entry->seekable_stream=MagickTrue;
1816 entry->thread_support=NoThreadSupport;
1817 entry->description=ConstantString(TIFFDescription);
1818 if (*version != '\0')
1819 entry->version=ConstantString(version);
1820 entry->module=ConstantString("TIFF");
1821 (void) RegisterMagickInfo(entry);
1822 entry=SetMagickInfo("TIFF64");
1823#if defined(TIFF_VERSION_BIG)
1824 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1825 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1826#endif
1827 entry->adjoin=MagickFalse;
1828 entry->endian_support=MagickTrue;
1829 entry->seekable_stream=MagickTrue;
1830 entry->thread_support=NoThreadSupport;
1831 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1832 if (*version != '\0')
1833 entry->version=ConstantString(version);
1834 entry->module=ConstantString("TIFF");
1835 (void) RegisterMagickInfo(entry);
1836 return(MagickImageCoderSignature);
1837}
1838
1839/*
1840%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1841% %
1842% %
1843% %
1844% U n r e g i s t e r T I F F I m a g e %
1845% %
1846% %
1847% %
1848%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1849%
1850% UnregisterTIFFImage() removes format registrations made by the TIFF module
1851% from the list of supported formats.
1852%
1853% The format of the UnregisterTIFFImage method is:
1854%
1855% UnregisterTIFFImage(void)
1856%
1857*/
1858ModuleExport void UnregisterTIFFImage(void)
1859{
cristy3d7f8062009-09-24 20:45:35 +00001860 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001861 (void) UnregisterMagickInfo("PTIF");
1862 (void) UnregisterMagickInfo("TIF");
1863 (void) UnregisterMagickInfo("TIFF");
1864 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001865 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1866 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001867 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001868 if (instantiate_key != MagickFalse)
1869 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1870 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1871 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001872 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001873 DestroySemaphoreInfo(&tiff_semaphore);
1874}
1875
1876#if defined(MAGICKCORE_TIFF_DELEGATE)
1877/*
1878%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1879% %
1880% %
1881% %
cristy3d7f8062009-09-24 20:45:35 +00001882% W r i t e G R O U P 4 I m a g e %
1883% %
1884% %
1885% %
1886%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1887%
1888% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1889%
1890% The format of the WriteGROUP4Image method is:
1891%
1892% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001893% Image *image,ExceptionInfo *)
cristy3d7f8062009-09-24 20:45:35 +00001894%
1895% A description of each parameter follows:
1896%
1897% o image_info: the image info.
1898%
1899% o image: The image.
1900%
cristy3a37efd2011-08-28 20:31:03 +00001901% o exception: return any errors or warnings in this structure.
1902%
cristy3d7f8062009-09-24 20:45:35 +00001903*/
1904static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00001905 Image *image,ExceptionInfo *exception)
cristy3d7f8062009-09-24 20:45:35 +00001906{
1907 char
1908 filename[MaxTextExtent];
1909
1910 FILE
1911 *file;
1912
1913 Image
1914 *huffman_image;
1915
1916 ImageInfo
1917 *write_info;
1918
1919 int
1920 unique_file;
1921
1922 MagickBooleanType
1923 status;
1924
cristybb503372010-05-27 20:51:26 +00001925 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001926 i;
1927
1928 ssize_t
1929 count;
1930
1931 TIFF
1932 *tiff;
1933
cristy94c8fe42009-10-06 01:57:36 +00001934 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001935 *byte_count,
1936 strip_size;
1937
1938 unsigned char
1939 *buffer;
1940
1941 /*
1942 Write image as CCITT Group4 TIFF image to a temporary file.
1943 */
1944 assert(image_info != (const ImageInfo *) NULL);
1945 assert(image_info->signature == MagickSignature);
1946 assert(image != (Image *) NULL);
1947 assert(image->signature == MagickSignature);
1948 if (image->debug != MagickFalse)
1949 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00001950 assert(exception != (ExceptionInfo *) NULL);
1951 assert(exception->signature == MagickSignature);
1952 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3d7f8062009-09-24 20:45:35 +00001953 if (status == MagickFalse)
1954 return(status);
cristy3a37efd2011-08-28 20:31:03 +00001955 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
cristy3d7f8062009-09-24 20:45:35 +00001956 if (huffman_image == (Image *) NULL)
1957 {
1958 (void) CloseBlob(image);
1959 return(MagickFalse);
1960 }
cristy94c8fe42009-10-06 01:57:36 +00001961 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001962 file=(FILE *) NULL;
1963 unique_file=AcquireUniqueFileResource(filename);
1964 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001965 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001966 if ((unique_file == -1) || (file == (FILE *) NULL))
1967 {
cristy3a37efd2011-08-28 20:31:03 +00001968 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1969 filename);
cristy3d7f8062009-09-24 20:45:35 +00001970 return(MagickFalse);
1971 }
cristyb51dff52011-05-19 16:55:47 +00001972 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
cristy3d7f8062009-09-24 20:45:35 +00001973 filename);
cristy018f07f2011-09-04 21:15:19 +00001974 (void) SetImageType(huffman_image,BilevelType,exception);
cristy14efd992009-09-26 23:46:03 +00001975 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001976 SetImageInfoFile(write_info,file);
1977 write_info->compression=Group4Compression;
1978 write_info->type=BilevelType;
1979 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
cristy3a37efd2011-08-28 20:31:03 +00001980 status=WriteTIFFImage(write_info,huffman_image,exception);
cristy3d7f8062009-09-24 20:45:35 +00001981 (void) fflush(file);
1982 write_info=DestroyImageInfo(write_info);
1983 if (status == MagickFalse)
1984 {
cristy3d7f8062009-09-24 20:45:35 +00001985 huffman_image=DestroyImage(huffman_image);
1986 (void) fclose(file);
1987 (void) RelinquishUniqueFileResource(filename);
1988 return(MagickFalse);
1989 }
1990 tiff=TIFFOpen(filename,"rb");
1991 if (tiff == (TIFF *) NULL)
1992 {
1993 huffman_image=DestroyImage(huffman_image);
1994 (void) fclose(file);
1995 (void) RelinquishUniqueFileResource(filename);
cristy3a37efd2011-08-28 20:31:03 +00001996 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
cristy3d7f8062009-09-24 20:45:35 +00001997 image_info->filename);
1998 return(MagickFalse);
1999 }
2000 /*
2001 Allocate raw strip buffer.
2002 */
cristy94c8fe42009-10-06 01:57:36 +00002003 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2004 {
2005 TIFFClose(tiff);
2006 huffman_image=DestroyImage(huffman_image);
2007 (void) fclose(file);
2008 (void) RelinquishUniqueFileResource(filename);
2009 return(MagickFalse);
2010 }
cristy3d7f8062009-09-24 20:45:35 +00002011 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00002012 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002013 if (byte_count[i] > strip_size)
2014 strip_size=byte_count[i];
2015 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2016 sizeof(*buffer));
2017 if (buffer == (unsigned char *) NULL)
2018 {
2019 TIFFClose(tiff);
2020 huffman_image=DestroyImage(huffman_image);
2021 (void) fclose(file);
2022 (void) RelinquishUniqueFileResource(filename);
2023 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2024 image_info->filename);
2025 }
2026 /*
2027 Compress runlength encoded to 2D Huffman pixels.
2028 */
cristybb503372010-05-27 20:51:26 +00002029 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00002030 {
cristy94c8fe42009-10-06 01:57:36 +00002031 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00002032 if (WriteBlob(image,(size_t) count,buffer) != count)
2033 status=MagickFalse;
2034 }
2035 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2036 TIFFClose(tiff);
2037 huffman_image=DestroyImage(huffman_image);
2038 (void) fclose(file);
2039 (void) RelinquishUniqueFileResource(filename);
2040 (void) CloseBlob(image);
2041 return(status);
2042}
2043#endif
2044
2045#if defined(MAGICKCORE_TIFF_DELEGATE)
2046/*
2047%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2048% %
2049% %
2050% %
cristy3ed852e2009-09-05 21:47:34 +00002051% W r i t e P T I F I m a g e %
2052% %
2053% %
2054% %
2055%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2056%
2057% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2058% format.
2059%
2060% The format of the WritePTIFImage method is:
2061%
2062% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002063% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002064%
2065% A description of each parameter follows:
2066%
2067% o image_info: the image info.
2068%
2069% o image: The image.
2070%
cristy3a37efd2011-08-28 20:31:03 +00002071% o exception: return any errors or warnings in this structure.
2072%
cristy3ed852e2009-09-05 21:47:34 +00002073*/
2074static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002075 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002076{
2077 Image
2078 *images,
2079 *next,
2080 *pyramid_image;
2081
2082 ImageInfo
2083 *write_info;
2084
2085 MagickBooleanType
2086 status;
2087
cristybb503372010-05-27 20:51:26 +00002088 size_t
cristy3ed852e2009-09-05 21:47:34 +00002089 columns,
2090 rows;
2091
2092 /*
2093 Create pyramid-encoded TIFF image.
2094 */
2095 images=NewImageList();
2096 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2097 {
cristy3a37efd2011-08-28 20:31:03 +00002098 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,exception));
cristy3ed852e2009-09-05 21:47:34 +00002099 columns=next->columns;
2100 rows=next->rows;
2101 while ((columns > 64) && (rows > 64))
2102 {
2103 columns/=2;
2104 rows/=2;
cristy98ca0f52011-10-08 23:19:10 +00002105 pyramid_image=ResizeImage(next,columns,rows,image->filter,image->blur,
cristy3a37efd2011-08-28 20:31:03 +00002106 exception);
cristy3ed852e2009-09-05 21:47:34 +00002107 AppendImageToList(&images,pyramid_image);
2108 }
2109 }
2110 /*
2111 Write pyramid-encoded TIFF image.
2112 */
cristy58567532009-09-26 01:13:32 +00002113 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002114 *write_info->magick='\0';
2115 write_info->adjoin=MagickTrue;
cristy3a37efd2011-08-28 20:31:03 +00002116 status=WriteTIFFImage(write_info,GetFirstImageInList(images),exception);
cristy3ed852e2009-09-05 21:47:34 +00002117 images=DestroyImageList(images);
2118 write_info=DestroyImageInfo(write_info);
2119 return(status);
2120}
2121#endif
2122
2123#if defined(MAGICKCORE_TIFF_DELEGATE)
2124/*
2125%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2126% %
2127% %
2128% W r i t e T I F F I m a g e %
2129% %
2130% %
2131% %
2132%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2133%
2134% WriteTIFFImage() writes an image in the Tagged image file format.
2135%
2136% The format of the WriteTIFFImage method is:
2137%
2138% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002139% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002140%
2141% A description of each parameter follows:
2142%
2143% o image_info: the image info.
2144%
2145% o image: The image.
2146%
cristy3a37efd2011-08-28 20:31:03 +00002147% o exception: return any errors or warnings in this structure.
2148%
cristy3ed852e2009-09-05 21:47:34 +00002149*/
2150
2151typedef struct _TIFFInfo
2152{
2153 RectangleInfo
2154 tile_geometry;
2155
2156 unsigned char
2157 *scanline,
2158 *scanlines,
2159 *pixels;
2160} TIFFInfo;
2161
2162static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2163{
2164 assert(tiff_info != (TIFFInfo *) NULL);
2165 if (tiff_info->scanlines != (unsigned char *) NULL)
2166 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2167 tiff_info->scanlines);
2168 if (tiff_info->pixels != (unsigned char *) NULL)
2169 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2170 tiff_info->pixels);
2171}
2172
2173static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2174 TIFFInfo *tiff_info)
2175{
2176 const char
2177 *option;
2178
2179 MagickStatusType
2180 flags;
2181
cristy79e5dad2010-09-16 19:48:33 +00002182 uint32
2183 tile_columns,
2184 tile_rows;
2185
cristy3ed852e2009-09-05 21:47:34 +00002186 assert(tiff_info != (TIFFInfo *) NULL);
2187 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2188 option=GetImageOption(image_info,"tiff:tile-geometry");
2189 if (option == (const char *) NULL)
2190 return(MagickTrue);
2191 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2192 if ((flags & HeightValue) == 0)
2193 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002194 tile_columns=(uint32) tiff_info->tile_geometry.width;
2195 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002196 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2197 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2198 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2199 tiff_info->tile_geometry.width=tile_columns;
2200 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002201 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002202 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002203 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002204 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002205 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2206 (tiff_info->pixels == (unsigned char *) NULL))
2207 {
2208 DestroyTIFFInfo(tiff_info);
2209 return(MagickFalse);
2210 }
2211 return(MagickTrue);
2212}
2213
cristybb503372010-05-27 20:51:26 +00002214static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002215 tsample_t sample,Image *image)
2216{
2217 int32
2218 status;
2219
cristybb503372010-05-27 20:51:26 +00002220 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002221 i;
2222
2223 register unsigned char
2224 *p,
2225 *q;
2226
cristybb503372010-05-27 20:51:26 +00002227 size_t
cristy3ed852e2009-09-05 21:47:34 +00002228 number_tiles,
2229 tile_width;
2230
cristyc6da28e2011-04-28 01:41:35 +00002231 ssize_t
2232 bytes_per_pixel,
2233 j,
2234 k,
2235 l;
2236
cristy3ed852e2009-09-05 21:47:34 +00002237 if (TIFFIsTiled(tiff) == 0)
2238 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2239 /*
2240 Fill scanlines to tile height.
2241 */
cristybb503372010-05-27 20:51:26 +00002242 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002243 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2244 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002245 if (((size_t) (row % tiff_info->tile_geometry.height) !=
cristyc6da28e2011-04-28 01:41:35 +00002246 (tiff_info->tile_geometry.height-1)) &&
2247 (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002248 return(0);
2249 /*
2250 Write tile to TIFF image.
2251 */
2252 status=0;
cristybb503372010-05-27 20:51:26 +00002253 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002254 tiff_info->tile_geometry.width);
2255 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2256 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002257 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002258 {
cristybb503372010-05-27 20:51:26 +00002259 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002260 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002261 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2262 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002263 {
2264 if (bytes_per_pixel == 0)
2265 {
2266 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2267 tiff_info->tile_geometry.width+k)/8);
2268 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2269 *q++=(*p++);
2270 continue;
2271 }
2272 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2273 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2274 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2275 for (l=0; l < bytes_per_pixel; l++)
2276 *q++=(*p++);
2277 }
cristydaff8092010-04-22 14:50:53 +00002278 if ((i*tiff_info->tile_geometry.width) != image->columns)
2279 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2280 tiff_info->tile_geometry.width),(uint32) ((row/
2281 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2282 sample);
cristy3ed852e2009-09-05 21:47:34 +00002283 if (status < 0)
2284 break;
2285 }
2286 return(status);
2287}
2288
2289static void TIFFSetProfiles(TIFF *tiff,Image *image)
2290{
2291 const char
2292 *name;
2293
2294 const StringInfo
2295 *profile;
2296
2297 if (image->profiles == (void *) NULL)
2298 return;
2299 ResetImageProfileIterator(image);
2300 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2301 {
2302 profile=GetImageProfile(image,name);
2303#if defined(TIFFTAG_XMLPACKET)
2304 if (LocaleCompare(name,"xmp") == 0)
2305 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2306 profile),GetStringInfoDatum(profile));
2307#endif
2308#if defined(TIFFTAG_ICCPROFILE)
2309 if (LocaleCompare(name,"icc") == 0)
2310 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2311 profile),GetStringInfoDatum(profile));
2312#endif
2313 if (LocaleCompare(name,"iptc") == 0)
2314 {
2315 size_t
2316 length;
2317
2318 StringInfo
2319 *iptc_profile;
2320
2321 iptc_profile=CloneStringInfo(profile);
2322 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2323 0x03);
2324 SetStringInfoLength(iptc_profile,length);
2325 if (TIFFIsByteSwapped(tiff))
2326 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002327 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002328 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2329 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2330 iptc_profile=DestroyStringInfo(iptc_profile);
2331 }
2332#if defined(TIFFTAG_PHOTOSHOP)
2333 if (LocaleCompare(name,"8bim") == 0)
2334 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2335 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2336#endif
2337 if (LocaleCompare(name,"tiff:37724") == 0)
2338 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2339 GetStringInfoDatum(profile));
2340 name=GetNextImageProfile(image);
2341 }
2342}
2343
2344static void TIFFSetProperties(TIFF *tiff,Image *image)
2345{
2346 const char
2347 *value;
2348
cristy15d8d212010-09-17 01:56:30 +00002349 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002350 value=GetImageProperty(image,"tiff:hostcomputer");
2351 if (value != (const char *) NULL)
2352 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2353 value=GetImageProperty(image,"tiff:artist");
2354 if (value != (const char *) NULL)
2355 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2356 value=GetImageProperty(image,"tiff:timestamp");
2357 if (value != (const char *) NULL)
2358 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2359 value=GetImageProperty(image,"tiff:make");
2360 if (value != (const char *) NULL)
2361 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2362 value=GetImageProperty(image,"tiff:model");
2363 if (value != (const char *) NULL)
2364 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristy15d8d212010-09-17 01:56:30 +00002365 value=GetImageProperty(image,"tiff:software");
2366 if (value != (const char *) NULL)
2367 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristy3ed852e2009-09-05 21:47:34 +00002368 value=GetImageProperty(image,"tiff:copyright");
2369 if (value != (const char *) NULL)
cristy98ca0f52011-10-08 23:19:10 +00002370 (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value);
cristy3ed852e2009-09-05 21:47:34 +00002371 value=GetImageProperty(image,"kodak-33423");
2372 if (value != (const char *) NULL)
2373 (void) TIFFSetField(tiff,33423,value);
2374 value=GetImageProperty(image,"kodak-36867");
2375 if (value != (const char *) NULL)
2376 (void) TIFFSetField(tiff,36867,value);
2377 value=GetImageProperty(image,"label");
2378 if (value != (const char *) NULL)
2379 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2380 value=GetImageProperty(image,"comment");
2381 if (value != (const char *) NULL)
2382 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2383}
2384
2385static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2386{
2387#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2388 const char
2389 *value;
2390
cristybb503372010-05-27 20:51:26 +00002391 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002392 i;
2393
2394 uint32
2395 offset;
2396
2397 /*
2398 Write EXIF properties.
2399 */
2400 offset=0;
2401 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2402 for (i=0; exif_info[i].tag != 0; i++)
2403 {
2404 value=GetImageProperty(image,exif_info[i].property);
2405 if (value == (const char *) NULL)
2406 continue;
2407 switch (exif_info[i].type)
2408 {
2409 case TIFF_ASCII:
2410 {
2411 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2412 break;
2413 }
2414 case TIFF_SHORT:
2415 {
2416 uint16
2417 shorty;
2418
cristyf2f27272009-12-17 14:48:46 +00002419 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002420 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2421 break;
2422 }
2423 case TIFF_LONG:
2424 {
2425 uint16
cristybb503372010-05-27 20:51:26 +00002426 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002427
cristybb503372010-05-27 20:51:26 +00002428 ssize_ty=(uint16) StringToLong(value);
2429 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002430 break;
2431 }
2432 case TIFF_RATIONAL:
2433 case TIFF_SRATIONAL:
2434 {
2435 float
2436 rational;
2437
cristyc1acd842011-05-19 23:05:47 +00002438 rational=InterpretLocaleValue(value,(char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +00002439 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2440 break;
2441 }
2442 default:
2443 break;
2444 }
2445 }
2446 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2447#else
2448 (void) tiff;
2449 (void) image;
2450#endif
2451}
2452
2453static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
cristy3a37efd2011-08-28 20:31:03 +00002454 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002455{
2456#if !defined(TIFFDefaultStripSize)
2457#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2458#endif
2459
2460 const char
2461 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002462 *option;
cristy3ed852e2009-09-05 21:47:34 +00002463
2464 CompressionType
2465 compression;
2466
cristy6b032822010-06-29 16:52:32 +00002467 EndianType
2468 endian_type;
2469
cristy3ed852e2009-09-05 21:47:34 +00002470 MagickBooleanType
2471 debug,
2472 status;
2473
2474 MagickOffsetType
2475 scene;
2476
2477 QuantumInfo
2478 *quantum_info;
2479
2480 QuantumType
2481 quantum_type;
2482
cristybb503372010-05-27 20:51:26 +00002483 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002484 i;
2485
2486 size_t
cristy5ecaba72011-02-18 02:05:32 +00002487 length,
2488 lsb_first;
cristy3ed852e2009-09-05 21:47:34 +00002489
cristyc6da28e2011-04-28 01:41:35 +00002490 ssize_t
2491 y;
2492
cristy3ed852e2009-09-05 21:47:34 +00002493 TIFF
2494 *tiff;
2495
2496 TIFFErrorHandler
2497 error_handler,
2498 warning_handler;
2499
2500 TIFFInfo
2501 tiff_info;
2502
2503 uint16
2504 bits_per_sample,
2505 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002506 endian,
cristy3ed852e2009-09-05 21:47:34 +00002507 photometric;
2508
2509 uint32
2510 rows_per_strip;
2511
2512 unsigned char
2513 *pixels;
2514
cristy3ed852e2009-09-05 21:47:34 +00002515 /*
2516 Open TIFF file.
2517 */
2518 assert(image_info != (const ImageInfo *) NULL);
2519 assert(image_info->signature == MagickSignature);
2520 assert(image != (Image *) NULL);
2521 assert(image->signature == MagickSignature);
2522 if (image->debug != MagickFalse)
2523 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +00002524 assert(exception != (ExceptionInfo *) NULL);
2525 assert(exception->signature == MagickSignature);
2526 assert(exception != (ExceptionInfo *) NULL);
2527 assert(exception->signature == MagickSignature);
2528 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +00002529 if (status == MagickFalse)
2530 return(status);
cristy3a37efd2011-08-28 20:31:03 +00002531 (void) MagickSetThreadValue(tiff_exception,exception);
cristy3ed852e2009-09-05 21:47:34 +00002532 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2533 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002534 endian_type=UndefinedEndian;
2535 option=GetImageOption(image_info,"tiff:endian");
2536 if (option != (const char *) NULL)
2537 {
2538 if (LocaleNCompare(option,"msb",3) == 0)
2539 endian_type=MSBEndian;
2540 if (LocaleNCompare(option,"lsb",3) == 0)
2541 endian_type=LSBEndian;;
2542 }
2543 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002544 {
2545 case LSBEndian: mode="wl"; break;
2546 case MSBEndian: mode="wb"; break;
2547 default: mode="w"; break;
2548 }
2549#if defined(TIFF_VERSION_BIG)
2550 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002551 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002552 {
2553 case LSBEndian: mode="wl8"; break;
2554 case MSBEndian: mode="wb8"; break;
2555 default: mode="w8"; break;
2556 }
2557#endif
2558 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2559 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2560 TIFFUnmapBlob);
2561 if (tiff == (TIFF *) NULL)
2562 {
2563 (void) TIFFSetWarningHandler(warning_handler);
2564 (void) TIFFSetErrorHandler(error_handler);
2565 return(MagickFalse);
2566 }
2567 scene=0;
2568 debug=IsEventLogging();
cristyda16f162011-02-19 23:52:17 +00002569 (void) debug;
cristy3ed852e2009-09-05 21:47:34 +00002570 do
2571 {
2572 /*
2573 Initialize TIFF fields.
2574 */
cristy5f1c1ff2010-12-23 21:38:06 +00002575 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002576 (image_info->type != OptimizeType))
cristy018f07f2011-09-04 21:15:19 +00002577 (void) SetImageType(image,image_info->type,exception);
cristy3ed852e2009-09-05 21:47:34 +00002578 quantum_info=AcquireQuantumInfo(image_info,image);
2579 if (quantum_info == (QuantumInfo *) NULL)
2580 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2581 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2582 (quantum_info->format == UndefinedQuantumFormat) &&
cristy3a37efd2011-08-28 20:31:03 +00002583 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002584 {
2585 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2586 if (status == MagickFalse)
2587 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2588 }
2589 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2590 (GetPreviousImageInList(image) != (Image *) NULL))
2591 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2592 if ((image->columns != (uint32) image->columns) ||
2593 (image->rows != (uint32) image->rows))
2594 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2595 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2596 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2597 compression=image->compression;
2598 if (image_info->compression != UndefinedCompression)
2599 compression=image_info->compression;
2600 switch (compression)
2601 {
2602 case FaxCompression:
2603 {
2604 compress_tag=COMPRESSION_CCITTFAX3;
2605 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2606 break;
2607 }
2608 case Group4Compression:
2609 {
2610 compress_tag=COMPRESSION_CCITTFAX4;
2611 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2612 break;
2613 }
cristy6d5e20f2011-04-25 13:48:54 +00002614#if defined(COMPRESSION_JBIG)
2615 case JBIG1Compression:
2616 {
2617 compress_tag=COMPRESSION_JBIG;
2618 break;
2619 }
2620#endif
cristy3ed852e2009-09-05 21:47:34 +00002621 case JPEGCompression:
2622 {
2623 compress_tag=COMPRESSION_JPEG;
2624 break;
2625 }
cristyfbb0ef02010-12-19 02:32:11 +00002626#if defined(COMPRESSION_LZMA)
2627 case LZMACompression:
2628 {
2629 compress_tag=COMPRESSION_LZMA;
2630 break;
2631 }
2632#endif
cristy3ed852e2009-09-05 21:47:34 +00002633 case LZWCompression:
2634 {
2635 compress_tag=COMPRESSION_LZW;
2636 break;
2637 }
2638 case RLECompression:
2639 {
2640 compress_tag=COMPRESSION_PACKBITS;
2641 break;
2642 }
2643 case ZipCompression:
2644 {
2645 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2646 break;
2647 }
2648 case NoCompression:
2649 default:
2650 {
2651 compress_tag=COMPRESSION_NONE;
2652 break;
2653 }
2654 }
cristy5b675872009-10-21 13:12:00 +00002655#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2656 if ((compress_tag != COMPRESSION_NONE) &&
2657 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002658 {
cristy3a37efd2011-08-28 20:31:03 +00002659 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2660 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002661 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002662 compress_tag=COMPRESSION_NONE;
2663 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002664 }
2665#else
2666 switch (compress_tag)
2667 {
2668#if defined(CCITT_SUPPORT)
2669 case COMPRESSION_CCITTFAX3:
2670 case COMPRESSION_CCITTFAX4:
2671#endif
2672#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2673 case COMPRESSION_JPEG:
2674#endif
cristyfbb0ef02010-12-19 02:32:11 +00002675#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2676 case COMPRESSION_LZMA:
2677#endif
cristy3ed852e2009-09-05 21:47:34 +00002678#if defined(LZW_SUPPORT)
2679 case COMPRESSION_LZW:
2680#endif
2681#if defined(PACKBITS_SUPPORT)
2682 case COMPRESSION_PACKBITS:
2683#endif
2684#if defined(ZIP_SUPPORT)
2685 case COMPRESSION_ADOBE_DEFLATE:
2686#endif
2687 case COMPRESSION_NONE:
2688 break;
2689 default:
2690 {
cristy3a37efd2011-08-28 20:31:03 +00002691 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2692 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002693 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002694 compress_tag=COMPRESSION_NONE;
2695 compression=NoCompression;
2696 break;
2697 }
2698 }
2699#endif
2700 switch (compression)
2701 {
2702 case FaxCompression:
2703 case Group4Compression:
2704 {
cristy018f07f2011-09-04 21:15:19 +00002705 (void) SetImageType(image,BilevelType,exception);
cristy3ed852e2009-09-05 21:47:34 +00002706 break;
2707 }
2708 case JPEGCompression:
2709 {
cristy3a37efd2011-08-28 20:31:03 +00002710 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +00002711 (void) SetImageDepth(image,8);
2712 break;
2713 }
2714 default:
2715 break;
2716 }
cristy3ed852e2009-09-05 21:47:34 +00002717 if (image->colorspace == CMYKColorspace)
2718 {
2719 photometric=PHOTOMETRIC_SEPARATED;
2720 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2721 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2722 }
2723 else
2724 {
2725 /*
2726 Full color TIFF raster.
2727 */
2728 if (image->colorspace == LabColorspace)
2729 photometric=PHOTOMETRIC_CIELAB;
2730 else
2731 if (image->colorspace == YCbCrColorspace)
2732 {
2733 photometric=PHOTOMETRIC_YCBCR;
2734 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy3a37efd2011-08-28 20:31:03 +00002735 (void) SetImageStorageClass(image,DirectClass,exception);
cristy3ed852e2009-09-05 21:47:34 +00002736 (void) SetImageDepth(image,8);
2737 }
2738 else
2739 {
cristy510d06a2011-07-06 23:43:54 +00002740 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00002741 (void) TransformImageColorspace(image,RGBColorspace);
2742 photometric=PHOTOMETRIC_RGB;
2743 }
2744 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2745 if ((image_info->type != TrueColorType) &&
2746 (image_info->type != TrueColorMatteType))
2747 {
cristy94c8fe42009-10-06 01:57:36 +00002748 if ((image_info->type != PaletteType) &&
cristy3a37efd2011-08-28 20:31:03 +00002749 (IsImageGray(image,exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002750 {
cristy94c8fe42009-10-06 01:57:36 +00002751 photometric=(uint16) (quantum_info->min_is_white !=
2752 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2753 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002754 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002755 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy3a37efd2011-08-28 20:31:03 +00002756 (IsImageMonochrome(image,exception) != MagickFalse))
cristy94c8fe42009-10-06 01:57:36 +00002757 {
2758 status=SetQuantumDepth(image,quantum_info,1);
2759 if (status == MagickFalse)
2760 ThrowWriterException(ResourceLimitError,
2761 "MemoryAllocationFailed");
2762 }
cristy3ed852e2009-09-05 21:47:34 +00002763 }
2764 else
cristy94c8fe42009-10-06 01:57:36 +00002765 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002766 {
cristybb503372010-05-27 20:51:26 +00002767 size_t
cristy94c8fe42009-10-06 01:57:36 +00002768 depth;
2769
2770 /*
2771 Colormapped TIFF raster.
2772 */
cristy3ed852e2009-09-05 21:47:34 +00002773 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002774 photometric=PHOTOMETRIC_PALETTE;
2775 depth=1;
2776 while ((GetQuantumRange(depth)+1) < image->colors)
2777 depth<<=1;
2778 status=SetQuantumDepth(image,quantum_info,depth);
2779 if (status == MagickFalse)
2780 ThrowWriterException(ResourceLimitError,
2781 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002782 }
2783 }
2784 }
cristyf2687ca2010-06-29 16:32:38 +00002785 switch (image->endian)
2786 {
2787 case LSBEndian:
2788 {
2789 endian=FILLORDER_LSB2MSB;
2790 break;
2791 }
2792 case MSBEndian:
2793 {
2794 endian=FILLORDER_MSB2LSB;
2795 break;
2796 }
2797 case UndefinedEndian:
2798 default:
2799 {
2800 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2801 break;
2802 }
2803 }
2804 lsb_first=1;
2805 image->endian=MSBEndian;
2806 if ((int) (*(char *) &lsb_first) != 0)
2807 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002808 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2809 (photometric != PHOTOMETRIC_MINISWHITE))
2810 {
2811 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002812 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002813 }
cristy0accf6e2009-10-01 13:14:28 +00002814 else
cristydaff8092010-04-22 14:50:53 +00002815 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2816 (photometric != PHOTOMETRIC_MINISWHITE))
2817 {
2818 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002819 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002820 }
cristy62e282b2010-06-29 01:27:13 +00002821 option=GetImageProperty(image,"tiff:fill-order");
2822 if (option != (const char *) NULL)
2823 {
2824 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002825 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002826 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002827 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002828 }
cristyf2687ca2010-06-29 16:32:38 +00002829 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2830 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002831 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2832 if (image->matte != MagickFalse)
2833 {
2834 uint16
2835 extra_samples,
2836 sample_info[1],
2837 samples_per_pixel;
2838
2839 /*
2840 TIFF has a matte channel.
2841 */
2842 extra_samples=1;
2843 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2844 option=GetImageProperty(image,"tiff:alpha");
2845 if ((option != (const char *) NULL) &&
2846 (LocaleCompare(option,"associated") == 0))
2847 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2848 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2849 &samples_per_pixel);
2850 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2851 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2852 &sample_info);
2853 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2854 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2855 }
2856 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2857 switch (quantum_info->format)
2858 {
2859 case FloatingPointQuantumFormat:
2860 {
2861 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2862 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2863 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2864 break;
2865 }
2866 case SignedQuantumFormat:
2867 {
2868 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2869 break;
2870 }
2871 case UnsignedQuantumFormat:
2872 {
2873 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2874 break;
2875 }
2876 default:
2877 break;
2878 }
cristy3ed852e2009-09-05 21:47:34 +00002879 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2880 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2881 if (photometric == PHOTOMETRIC_RGB)
2882 if ((image_info->interlace == PlaneInterlace) ||
2883 (image_info->interlace == PartitionInterlace))
2884 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2885 rows_per_strip=1;
2886 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002887 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2888 1);
cristy3ed852e2009-09-05 21:47:34 +00002889 option=GetImageOption(image_info,"tiff:rows-per-strip");
2890 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002891 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002892 switch (compress_tag)
2893 {
2894 case COMPRESSION_JPEG:
2895 {
2896#if defined(JPEG_SUPPORT)
2897 const char
2898 *sampling_factor;
2899
2900 GeometryInfo
2901 geometry_info;
2902
2903 MagickStatusType
2904 flags;
2905
2906 rows_per_strip+=(16-(rows_per_strip % 16));
2907 if (image->quality != 0)
2908 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2909 if (image_info->quality != UndefinedCompressionQuality)
2910 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2911 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
cristy510d06a2011-07-06 23:43:54 +00002912 if (IsRGBColorspace(image->colorspace) == MagickTrue)
cristy3ed852e2009-09-05 21:47:34 +00002913 {
cristy949bf5b2010-05-08 02:47:03 +00002914 const char
2915 *value;
2916
cristy3ed852e2009-09-05 21:47:34 +00002917 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2918 sampling_factor=(const char *) NULL;
2919 value=GetImageProperty(image,"jpeg:sampling-factor");
2920 if (value != (char *) NULL)
2921 {
2922 sampling_factor=value;
2923 if (image->debug != MagickFalse)
2924 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2925 " Input sampling-factors=%s",sampling_factor);
2926 }
2927 if (image_info->sampling_factor != (char *) NULL)
2928 sampling_factor=image_info->sampling_factor;
2929 if (sampling_factor != (const char *) NULL)
2930 {
2931 flags=ParseGeometry(sampling_factor,&geometry_info);
2932 if ((flags & SigmaValue) == 0)
2933 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002934 if (image->colorspace == YCbCrColorspace)
2935 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2936 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00002937 }
2938 }
2939 if (bits_per_sample == 12)
2940 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2941#endif
2942 break;
2943 }
2944 case COMPRESSION_ADOBE_DEFLATE:
2945 {
cristyf6fe0a12010-05-30 00:44:47 +00002946 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002947 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2948 &bits_per_sample);
2949 if (((photometric == PHOTOMETRIC_RGB) ||
2950 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2951 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002952 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00002953 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2954 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00002955 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00002956 break;
2957 }
2958 case COMPRESSION_CCITTFAX3:
2959 {
2960 /*
2961 Byte-aligned EOL.
2962 */
cristyeaedf062010-05-29 22:36:02 +00002963 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002964 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2965 break;
2966 }
2967 case COMPRESSION_CCITTFAX4:
2968 {
cristyeaedf062010-05-29 22:36:02 +00002969 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002970 break;
2971 }
cristyef8f26b2010-12-19 20:29:16 +00002972#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2973 case COMPRESSION_LZMA:
2974 {
2975 if (((photometric == PHOTOMETRIC_RGB) ||
2976 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2977 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2978 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2979 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
2980 image_info->quality == UndefinedCompressionQuality ? 7 :
2981 MagickMin((ssize_t) image_info->quality/10,9)));
2982 break;
2983 }
2984#endif
cristy3ed852e2009-09-05 21:47:34 +00002985 case COMPRESSION_LZW:
2986 {
2987 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2988 &bits_per_sample);
2989 if (((photometric == PHOTOMETRIC_RGB) ||
2990 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2991 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002992 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00002993 break;
2994 }
2995 default:
2996 break;
2997 }
cristy79e5dad2010-09-16 19:48:33 +00002998 option=GetImageOption(image_info,"tiff:tile-geometry");
2999 if (option == (const char *) NULL)
3000 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy3ed852e2009-09-05 21:47:34 +00003001 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
3002 {
3003 unsigned short
3004 units;
3005
3006 /*
3007 Set image resolution.
3008 */
3009 units=RESUNIT_NONE;
3010 if (image->units == PixelsPerInchResolution)
3011 units=RESUNIT_INCH;
3012 if (image->units == PixelsPerCentimeterResolution)
3013 units=RESUNIT_CENTIMETER;
3014 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
3015 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
3016 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
3017 if ((image->page.x != 0) || (image->page.y != 0))
3018 {
3019 /*
3020 Set image position.
3021 */
3022 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
3023 image->x_resolution);
3024 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
3025 image->y_resolution);
3026 }
3027 }
3028 if (image->chromaticity.white_point.x != 0.0)
3029 {
3030 float
3031 chromaticity[6];
3032
3033 /*
3034 Set image chromaticity.
3035 */
3036 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3037 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3038 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3039 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3040 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3041 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3042 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3043 chromaticity[0]=(float) image->chromaticity.white_point.x;
3044 chromaticity[1]=(float) image->chromaticity.white_point.y;
3045 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3046 }
3047 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3048 {
3049 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3050 if (image->scene != 0)
3051 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3052 GetImageListLength(image));
3053 }
3054 if (image->orientation != UndefinedOrientation)
3055 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3056 (void) TIFFSetProfiles(tiff,image);
3057 {
3058 uint16
3059 page,
3060 pages;
3061
3062 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00003063 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00003064 if ((image_info->adjoin != MagickFalse) && (pages > 1))
3065 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3066 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3067 }
3068 (void) TIFFSetProperties(tiff,image);
3069 if (0)
3070 (void) TIFFSetEXIFProperties(tiff,image);
3071 /*
3072 Write image scanlines.
3073 */
3074 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3075 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3076 pixels=GetQuantumPixels(quantum_info);
3077 tiff_info.scanline=GetQuantumPixels(quantum_info);
3078 switch (photometric)
3079 {
3080 case PHOTOMETRIC_CIELAB:
3081 case PHOTOMETRIC_YCBCR:
3082 case PHOTOMETRIC_RGB:
3083 {
3084 /*
3085 RGB TIFF image.
3086 */
3087 switch (image_info->interlace)
3088 {
3089 case NoInterlace:
3090 default:
3091 {
3092 quantum_type=RGBQuantum;
3093 if (image->matte != MagickFalse)
3094 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003095 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003096 {
cristy4c08aed2011-07-01 19:47:50 +00003097 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003098 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003099
cristy3a37efd2011-08-28 20:31:03 +00003100 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003101 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003102 break;
cristy4c08aed2011-07-01 19:47:50 +00003103 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003104 quantum_type,pixels,exception);
cristyda16f162011-02-19 23:52:17 +00003105 (void) length;
cristy3ed852e2009-09-05 21:47:34 +00003106 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3107 break;
3108 if (image->previous == (Image *) NULL)
3109 {
cristy2837bcc2010-08-07 23:57:39 +00003110 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3111 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003112 if (status == MagickFalse)
3113 break;
3114 }
3115 }
3116 break;
3117 }
3118 case PlaneInterlace:
3119 case PartitionInterlace:
3120 {
3121 /*
3122 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3123 */
cristybb503372010-05-27 20:51:26 +00003124 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003125 {
cristy4c08aed2011-07-01 19:47:50 +00003126 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003127 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003128
cristy3a37efd2011-08-28 20:31:03 +00003129 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003130 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003131 break;
cristy4c08aed2011-07-01 19:47:50 +00003132 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003133 RedQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003134 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3135 break;
3136 }
3137 if (image->previous == (Image *) NULL)
3138 {
3139 status=SetImageProgress(image,SaveImageTag,100,400);
3140 if (status == MagickFalse)
3141 break;
3142 }
cristybb503372010-05-27 20:51:26 +00003143 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003144 {
cristy4c08aed2011-07-01 19:47:50 +00003145 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003146 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003147
cristy3a37efd2011-08-28 20:31:03 +00003148 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003149 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003150 break;
cristy4c08aed2011-07-01 19:47:50 +00003151 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003152 GreenQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003153 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3154 break;
3155 }
3156 if (image->previous == (Image *) NULL)
3157 {
3158 status=SetImageProgress(image,SaveImageTag,200,400);
3159 if (status == MagickFalse)
3160 break;
3161 }
cristybb503372010-05-27 20:51:26 +00003162 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003163 {
cristy4c08aed2011-07-01 19:47:50 +00003164 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003165 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003166
cristy3a37efd2011-08-28 20:31:03 +00003167 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003168 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003169 break;
cristy4c08aed2011-07-01 19:47:50 +00003170 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003171 BlueQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003172 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3173 break;
3174 }
3175 if (image->previous == (Image *) NULL)
3176 {
3177 status=SetImageProgress(image,SaveImageTag,300,400);
3178 if (status == MagickFalse)
3179 break;
3180 }
3181 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003182 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003183 {
cristy4c08aed2011-07-01 19:47:50 +00003184 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003185 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003186
cristy3a37efd2011-08-28 20:31:03 +00003187 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003188 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003189 break;
cristy4c08aed2011-07-01 19:47:50 +00003190 length=ExportQuantumPixels(image,(CacheView *) NULL,
cristy3a37efd2011-08-28 20:31:03 +00003191 quantum_info,AlphaQuantum,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003192 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3193 break;
3194 }
3195 if (image->previous == (Image *) NULL)
3196 {
3197 status=SetImageProgress(image,SaveImageTag,400,400);
3198 if (status == MagickFalse)
3199 break;
3200 }
3201 break;
3202 }
3203 }
3204 break;
3205 }
3206 case PHOTOMETRIC_SEPARATED:
3207 {
3208 /*
3209 CMYK TIFF image.
3210 */
3211 quantum_type=CMYKQuantum;
3212 if (image->matte != MagickFalse)
3213 quantum_type=CMYKAQuantum;
3214 if (image->colorspace != CMYKColorspace)
3215 (void) TransformImageColorspace(image,CMYKColorspace);
cristybb503372010-05-27 20:51:26 +00003216 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003217 {
cristy4c08aed2011-07-01 19:47:50 +00003218 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003219 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003220
cristy3a37efd2011-08-28 20:31:03 +00003221 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003222 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003223 break;
cristy4c08aed2011-07-01 19:47:50 +00003224 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003225 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003226 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3227 break;
3228 if (image->previous == (Image *) NULL)
3229 {
cristycee97112010-05-28 00:44:52 +00003230 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3231 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003232 if (status == MagickFalse)
3233 break;
3234 }
3235 }
3236 break;
3237 }
3238 case PHOTOMETRIC_PALETTE:
3239 {
3240 uint16
3241 *blue,
3242 *green,
3243 *red;
3244
3245 /*
3246 Colormapped TIFF image.
3247 */
3248 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3249 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3250 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3251 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3252 (blue == (uint16 *) NULL))
3253 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3254 /*
3255 Initialize TIFF colormap.
3256 */
3257 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3258 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3259 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003260 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003261 {
3262 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3263 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3264 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3265 }
3266 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3267 red=(uint16 *) RelinquishMagickMemory(red);
3268 green=(uint16 *) RelinquishMagickMemory(green);
3269 blue=(uint16 *) RelinquishMagickMemory(blue);
3270 }
3271 default:
3272 {
3273 /*
3274 Convert PseudoClass packets to contiguous grayscale scanlines.
3275 */
3276 quantum_type=IndexQuantum;
3277 if (image->matte != MagickFalse)
3278 {
3279 if (photometric != PHOTOMETRIC_PALETTE)
3280 quantum_type=GrayAlphaQuantum;
3281 else
3282 quantum_type=IndexAlphaQuantum;
3283 }
3284 else
3285 if (photometric != PHOTOMETRIC_PALETTE)
3286 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003287 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003288 {
cristy4c08aed2011-07-01 19:47:50 +00003289 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00003290 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003291
cristy3a37efd2011-08-28 20:31:03 +00003292 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00003293 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003294 break;
cristy4c08aed2011-07-01 19:47:50 +00003295 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
cristy3a37efd2011-08-28 20:31:03 +00003296 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00003297 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3298 break;
3299 if (image->previous == (Image *) NULL)
3300 {
cristycee97112010-05-28 00:44:52 +00003301 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3302 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003303 if (status == MagickFalse)
3304 break;
3305 }
3306 }
3307 break;
3308 }
3309 }
3310 quantum_info=DestroyQuantumInfo(quantum_info);
3311 DestroyTIFFInfo(&tiff_info);
3312 if (0 && (image_info->verbose == MagickTrue))
3313 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3314 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003315 image->endian=MSBEndian;
3316 if (endian == FILLORDER_LSB2MSB)
3317 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003318 image=SyncNextImageInList(image);
3319 if (image == (Image *) NULL)
3320 break;
3321 status=SetImageProgress(image,SaveImagesTag,scene++,
3322 GetImageListLength(image));
3323 if (status == MagickFalse)
3324 break;
3325 } while (image_info->adjoin != MagickFalse);
3326 (void) TIFFSetWarningHandler(warning_handler);
3327 (void) TIFFSetErrorHandler(error_handler);
3328 TIFFClose(tiff);
3329 return(MagickTrue);
3330}
3331#endif