blob: d6b6ce39ca6c7d1dc285de1ea0106961eb41efc0 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% TTTTT IIIII FFFFF FFFFF %
7% T I F F %
8% T I FFF FFF %
9% T I F F %
10% T IIIII F F %
11% %
12% %
13% Read/Write TIFF Image Format %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "magick/studio.h"
cristy5a2ca482009-10-14 18:24:56 +000043#include "magick/attribute.h"
cristy3ed852e2009-09-05 21:47:34 +000044#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/cache.h"
47#include "magick/color.h"
48#include "magick/color-private.h"
49#include "magick/colorspace.h"
50#include "magick/constitute.h"
51#include "magick/enhance.h"
52#include "magick/exception.h"
53#include "magick/exception-private.h"
54#include "magick/geometry.h"
55#include "magick/image.h"
56#include "magick/image-private.h"
57#include "magick/list.h"
58#include "magick/log.h"
59#include "magick/magick.h"
60#include "magick/memory_.h"
61#include "magick/module.h"
62#include "magick/monitor.h"
63#include "magick/monitor-private.h"
64#include "magick/option.h"
65#include "magick/property.h"
66#include "magick/quantum.h"
67#include "magick/quantum-private.h"
68#include "magick/profile.h"
69#include "magick/resize.h"
cristy3d7f8062009-09-24 20:45:35 +000070#include "magick/resource_.h"
cristy3ed852e2009-09-05 21:47:34 +000071#include "magick/semaphore.h"
72#include "magick/splay-tree.h"
73#include "magick/static.h"
74#include "magick/statistic.h"
75#include "magick/string_.h"
cristyf2f27272009-12-17 14:48:46 +000076#include "magick/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000077#include "magick/thread_.h"
78#include "magick/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000079#if defined(MAGICKCORE_TIFF_DELEGATE)
80# if defined(MAGICKCORE_HAVE_TIFFCONF_H)
81# include "tiffconf.h"
82#endif
83# include "tiff.h"
84# include "tiffio.h"
85# if !defined(COMPRESSION_ADOBE_DEFLATE)
86# define COMPRESSION_ADOBE_DEFLATE 8
87# endif
88
89/*
90 Typedef declarations.
91*/
92#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
93typedef struct _ExifInfo
94{
95 unsigned int
96 tag,
97 type;
98
99 const char
100 *property;
101} ExifInfo;
102
103static const ExifInfo
104 exif_info[] = {
105 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
106 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
107 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
108 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000109 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy3ed852e2009-09-05 21:47:34 +0000110 { EXIFTAG_OECF, TIFF_UNDEFINED, "exif:OptoelectricConversionFactor" },
111 { EXIFTAG_EXIFVERSION, TIFF_UNDEFINED, "exif:ExifVersion" },
112 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
113 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
114 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_UNDEFINED, "exif:ComponentsConfiguration" },
115 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
116 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
117 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
118 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
119 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
120 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
121 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
122 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
123 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
124 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
125 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
126 /* { EXIFTAG_SUBJECTAREA, TIFF_SHORT, "exif:SubjectArea" }, */
127 { EXIFTAG_MAKERNOTE, TIFF_UNDEFINED, "exif:MakerNote" },
128 { EXIFTAG_USERCOMMENT, TIFF_UNDEFINED, "exif:UserComment" },
129 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
130 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
131 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
132 { EXIFTAG_FLASHPIXVERSION, TIFF_UNDEFINED, "exif:FlashpixVersion" },
133 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
134 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
135 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
136 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
137 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
138 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
139 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_UNDEFINED, "exif:SpatialFrequencyResponse" },
140 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
141 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
142 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
143 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
144 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
145 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
146 { EXIFTAG_FILESOURCE, TIFF_UNDEFINED, "exif:FileSource" },
147 { EXIFTAG_SCENETYPE, TIFF_UNDEFINED, "exif:SceneType" },
148 { EXIFTAG_CFAPATTERN, TIFF_UNDEFINED, "exif:CFAPattern" },
149 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
150 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
151 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
152 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
153 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
154 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
155 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
156 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
157 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
158 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
159 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_UNDEFINED, "exif:DeviceSettingDescription" },
160 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
161 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
162 { 0, 0, (char *) NULL }
163};
164#endif
165
166/*
167 Global declarations.
168*/
169static MagickThreadKey
170 tiff_exception;
171
172static SemaphoreInfo
173 *tiff_semaphore = (SemaphoreInfo *) NULL;
174
175static volatile MagickBooleanType
176 instantiate_key = MagickFalse;
177#endif
178
179/*
180 Forward declarations.
181*/
182#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000183static Image *
184 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
185
cristy3ed852e2009-09-05 21:47:34 +0000186static MagickBooleanType
cristy3d7f8062009-09-24 20:45:35 +0000187 WriteGROUP4Image(const ImageInfo *,Image *),
cristy3ed852e2009-09-05 21:47:34 +0000188 WritePTIFImage(const ImageInfo *,Image *),
189 WriteTIFFImage(const ImageInfo *,Image *);
190#endif
191
192/*
193%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194% %
195% %
196% %
197% I s T I F F %
198% %
199% %
200% %
201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202%
203% IsTIFF() returns MagickTrue if the image format type, identified by the
204% magick string, is TIFF.
205%
206% The format of the IsTIFF method is:
207%
208% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
209%
210% A description of each parameter follows:
211%
212% o magick: compare image format pattern against these bytes.
213%
214% o length: Specifies the length of the magick string.
215%
216*/
217static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
218{
219 if (length < 4)
220 return(MagickFalse);
221 if (memcmp(magick,"\115\115\000\052",4) == 0)
222 return(MagickTrue);
223 if (memcmp(magick,"\111\111\052\000",4) == 0)
224 return(MagickTrue);
225#if defined(TIFF_VERSION_BIG)
226 if (length < 8)
227 return(MagickFalse);
228 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
229 return(MagickTrue);
230 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
231 return(MagickTrue);
232#endif
233 return(MagickFalse);
234}
235
236#if defined(MAGICKCORE_TIFF_DELEGATE)
237/*
238%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239% %
240% %
241% %
cristy3d7f8062009-09-24 20:45:35 +0000242% R e a d G R O U P 4 I m a g e %
243% %
244% %
245% %
246%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247%
248% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
249% allocates the memory necessary for the new Image structure and returns a
250% pointer to the new image.
251%
252% The format of the ReadGROUP4Image method is:
253%
254% Image *ReadGROUP4Image(const ImageInfo *image_info,
255% ExceptionInfo *exception)
256%
257% A description of each parameter follows:
258%
259% o image_info: the image info.
260%
261% o exception: return any errors or warnings in this structure.
262%
263*/
264
265static inline size_t WriteLSBLong(FILE *file,const unsigned int value)
266{
267 unsigned char
268 buffer[4];
269
270 buffer[0]=(unsigned char) value;
271 buffer[1]=(unsigned char) (value >> 8);
272 buffer[2]=(unsigned char) (value >> 16);
273 buffer[3]=(unsigned char) (value >> 24);
274 return(fwrite(buffer,1,4,file));
275}
276
277static Image *ReadGROUP4Image(const ImageInfo *image_info,
278 ExceptionInfo *exception)
279{
280 char
281 filename[MaxTextExtent];
282
283 FILE
284 *file;
285
286 Image
287 *image;
288
289 ImageInfo
290 *read_info;
291
292 int
293 c,
294 unique_file;
295
296 MagickBooleanType
297 status;
298
299 size_t
300 length;
301
302 ssize_t
303 offset,
304 strip_offset;
305
306 /*
307 Open image file.
308 */
309 assert(image_info != (const ImageInfo *) NULL);
310 assert(image_info->signature == MagickSignature);
311 if (image_info->debug != MagickFalse)
312 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
313 image_info->filename);
314 assert(exception != (ExceptionInfo *) NULL);
315 assert(exception->signature == MagickSignature);
316 image=AcquireImage(image_info);
317 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
318 if (status == MagickFalse)
319 {
320 image=DestroyImageList(image);
321 return((Image *) NULL);
322 }
323 /*
324 Write raw CCITT Group 4 wrapped as a TIFF image file.
325 */
326 file=(FILE *) NULL;
327 unique_file=AcquireUniqueFileResource(filename);
328 if (unique_file != -1)
329 file=fdopen(unique_file,"wb");
330 if ((unique_file == -1) || (file == (FILE *) NULL))
331 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
332 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
333 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
334 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
335 length=WriteLSBLong(file,image->columns);
336 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
337 length=WriteLSBLong(file,image->rows);
338 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
339 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
340 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
341 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
342 strip_offset=10+(12*14)+4+8;
343 length=WriteLSBLong(file,(unsigned long) strip_offset);
344 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristy2d6ccc32009-09-25 03:18:25 +0000345 length=WriteLSBLong(file,(unsigned long) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000346 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
347 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000348 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000349 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
350 offset=(ssize_t) ftell(file)-4;
351 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
352 length=WriteLSBLong(file,(unsigned long) (strip_offset-8));
353 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
354 length=WriteLSBLong(file,(unsigned long) (strip_offset-8));
355 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
356 length=fwrite("\000\000\000\000",1,4,file);
357 length=WriteLSBLong(file,image->x_resolution);
358 length=WriteLSBLong(file,1);
359 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
360 (void) fputc(c,file);
361 offset=(ssize_t) fseek(file,(long) offset,SEEK_SET);
362 length=WriteLSBLong(file,(unsigned int) length);
363 (void) fclose(file);
364 (void) CloseBlob(image);
365 image=DestroyImage(image);
366 /*
367 Read TIFF image.
368 */
cristy14efd992009-09-26 23:46:03 +0000369 read_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +0000370 (void) FormatMagickString(read_info->filename,MaxTextExtent,"%.1024s",
371 filename);
372 image=ReadTIFFImage(read_info,exception);
373 read_info=DestroyImageInfo(read_info);
374 if (image != (Image *) NULL)
375 {
376 (void) CopyMagickString(image->filename,image_info->filename,
377 MaxTextExtent);
378 (void) CopyMagickString(image->magick_filename,image_info->filename,
379 MaxTextExtent);
380 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
381 }
382 (void) RelinquishUniqueFileResource(filename);
383 return(image);
384}
385#endif
386
387#if defined(MAGICKCORE_TIFF_DELEGATE)
388/*
389%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390% %
391% %
392% %
cristy3ed852e2009-09-05 21:47:34 +0000393% R e a d T I F F I m a g e %
394% %
395% %
396% %
397%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398%
399% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
400% memory necessary for the new Image structure and returns a pointer to the
401% new image.
402%
403% The format of the ReadTIFFImage method is:
404%
405% Image *ReadTIFFImage(const ImageInfo *image_info,
406% ExceptionInfo *exception)
407%
408% A description of each parameter follows:
409%
410% o image_info: the image info.
411%
412% o exception: return any errors or warnings in this structure.
413%
414*/
415
416static inline size_t MagickMax(const size_t x,const size_t y)
417{
418 if (x > y)
419 return(x);
420 return(y);
421}
422
423static inline long MagickMin(const long x,const long y)
424{
425 if (x < y)
426 return(x);
427 return(y);
428}
429
430static MagickBooleanType ReadProfile(Image *image,const char *name,
431 unsigned char *datum,long length)
432{
433 MagickBooleanType
434 status;
435
436 register long
437 i;
438
439 StringInfo
440 *profile;
441
442 if (length < 4)
443 return(MagickFalse);
444 i=0;
445 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
446 {
447 for (i=0; i < (length-4); i+=2)
448 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
449 break;
450 if (i == length)
451 length-=i;
452 else
453 i=0;
454 if (length < 4)
455 return(MagickFalse);
456 }
457 profile=AcquireStringInfo((size_t) length);
458 SetStringInfoDatum(profile,datum+i);
459 status=SetImageProfile(image,name,profile);
460 profile=DestroyStringInfo(profile);
461 if (status == MagickFalse)
462 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
463 image->filename);
464 return(MagickTrue);
465}
466
467#if defined(__cplusplus) || defined(c_plusplus)
468extern "C" {
469#endif
470
471static int TIFFCloseBlob(thandle_t image)
472{
473 (void) CloseBlob((Image *) image);
474 return(0);
475}
476
477static void TIFFErrors(const char *module,const char *format,va_list error)
478{
479 char
480 message[MaxTextExtent];
481
482 ExceptionInfo
483 *exception;
484
485#if defined(MAGICKCORE_HAVE_VSNPRINTF)
486 (void) vsnprintf(message,MaxTextExtent,format,error);
487#else
488 (void) vsprintf(message,format,error);
489#endif
490 (void) ConcatenateMagickString(message,".",MaxTextExtent);
491 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
492 if (exception != (ExceptionInfo *) NULL)
493 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
494 message,"`%s'",module);
495}
496
497static void TIFFGetProfiles(TIFF *tiff,Image *image)
498{
499 uint32
500 length;
501
502 unsigned char
503 *profile;
504
505#if defined(TIFFTAG_ICCPROFILE)
506 length=0;
507 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
508 (void) ReadProfile(image,"icc",profile,(long) length);
509#endif
510#if defined(TIFFTAG_PHOTOSHOP)
511 length=0;
512 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
513 (void) ReadProfile(image,"8bim",profile,(long) length);
514#endif
515#if defined(TIFFTAG_RICHTIFFIPTC)
516 length=0;
517 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
518 {
519 if (TIFFIsByteSwapped(tiff) != 0)
520 TIFFSwabArrayOfLong((uint32 *) profile,(unsigned long) length);
521 (void) ReadProfile(image,"iptc",profile,4L*length);
522 }
523#endif
524#if defined(TIFFTAG_XMLPACKET)
525 length=0;
526 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
527 (void) ReadProfile(image,"xmp",profile,(long) length);
528#endif
529 length=0;
530 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
531 (void) ReadProfile(image,"tiff:37724",profile,(long) length);
532}
533
534static void TIFFGetProperties(TIFF *tiff,Image *image)
535{
536 char
537 *text;
538
539 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
540 (void) SetImageProperty(image,"tiff:artist",text);
541 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
542 (void) SetImageProperty(image,"tiff:timestamp",text);
543 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
544 (void) SetImageProperty(image,"tiff:software",text);
545 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
546 (void) SetImageProperty(image,"tiff:hostcomputer",text);
547 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
548 (void) SetImageProperty(image,"tiff:document",text);
549 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
550 (void) SetImageProperty(image,"tiff:make",text);
551 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
552 (void) SetImageProperty(image,"tiff:model",text);
553 if (TIFFGetField(tiff,33432,&text) == 1)
554 (void) SetImageProperty(image,"tiff:copyright",text);
555 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
556 (void) SetImageProperty(image,"label",text);
557 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
558 (void) SetImageProperty(image,"comment",text);
559}
560
561static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
562{
563#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
564 char
565 value[MaxTextExtent];
566
567 register long
568 i;
569
570 tdir_t
571 directory;
572
573 uint32
574 offset;
575
576 /*
577 Read EXIF properties.
578 */
579 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
580 return;
581 directory=TIFFCurrentDirectory(tiff);
582 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
583 return;
584 for (i=0; exif_info[i].tag != 0; i++)
585 {
586 *value='\0';
587 switch (exif_info[i].type)
588 {
589 case TIFF_ASCII:
590 {
591 char
592 *ascii;
593
594 if (TIFFGetField(tiff,exif_info[i].tag,&ascii) != 0)
595 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
596 break;
597 }
598 case TIFF_SHORT:
599 {
600 uint16
601 shorty;
602
603 if (TIFFGetField(tiff,exif_info[i].tag,&shorty) != 0)
604 (void) FormatMagickString(value,MaxTextExtent,"%d",shorty);
605 break;
606 }
607 case TIFF_LONG:
608 {
609 uint32
610 longy;
611
612 if (TIFFGetField(tiff,exif_info[i].tag,&longy) != 0)
613 (void) FormatMagickString(value,MaxTextExtent,"%d",longy);
614 break;
615 }
616 case TIFF_RATIONAL:
617 case TIFF_SRATIONAL:
618 {
619 float
620 rational;
621
622 if (TIFFGetField(tiff,exif_info[i].tag,&rational) != 0)
cristye7f51092010-01-17 00:39:37 +0000623 (void) FormatMagickString(value,MaxTextExtent,"%g",rational);
cristy3ed852e2009-09-05 21:47:34 +0000624 break;
625 }
626 default:
627 break;
628 }
629 if (*value != '\0')
630 (void) SetImageProperty(image,exif_info[i].property,value);
631 }
632 TIFFSetDirectory(tiff,directory);
633#else
634 (void) tiff;
635 (void) image;
636#endif
637}
638
639static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
640{
641 *base=(tdata_t *) GetBlobStreamData((Image *) image);
642 if (*base != (tdata_t *) NULL)
643 *size=(toff_t) GetBlobSize((Image *) image);
644 if (*base != (tdata_t *) NULL)
645 return(1);
646 return(0);
647}
648
649static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
650{
651 tsize_t
652 count;
653
654 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
655 (unsigned char *) data);
656 return(count);
657}
658
659static int32 TIFFReadPixels(TIFF *tiff,unsigned long bits_per_sample,
660 tsample_t sample,long row,tdata_t scanline)
661{
662 int32
663 status;
664
665 (void) bits_per_sample;
666 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
667 return(status);
668}
669
670static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
671{
672 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
673}
674
675static toff_t TIFFGetBlobSize(thandle_t image)
676{
677 return((toff_t) GetBlobSize((Image *) image));
678}
679
680static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
681{
682 (void) image;
683 (void) base;
684 (void) size;
685}
686
687static void TIFFWarnings(const char *module,const char *format,va_list warning)
688{
689 char
690 message[MaxTextExtent];
691
692 ExceptionInfo
693 *exception;
694
695#if defined(MAGICKCORE_HAVE_VSNPRINTF)
696 (void) vsnprintf(message,MaxTextExtent,format,warning);
697#else
698 (void) vsprintf(message,format,warning);
699#endif
700 (void) ConcatenateMagickString(message,".",MaxTextExtent);
701 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
702 if (exception != (ExceptionInfo *) NULL)
703 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
704 message,"`%s'",module);
705}
706
707static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
708{
709 tsize_t
710 count;
711
712 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
713 (unsigned char *) data);
714 return(count);
715}
716
717#if defined(__cplusplus) || defined(c_plusplus)
718}
719#endif
720
721static Image *ReadTIFFImage(const ImageInfo *image_info,
722 ExceptionInfo *exception)
723{
724 typedef enum
725 {
726 ReadSingleSampleMethod,
727 ReadRGBAMethod,
728 ReadCMYKAMethod,
729 ReadStripMethod,
730 ReadTileMethod,
731 ReadGenericMethod
732 } TIFFMethodType;
733
734 const char
735 *option;
736
737 float
738 *chromaticity,
739 x_position,
740 y_position,
741 x_resolution,
742 y_resolution;
743
744 Image
745 *image;
746
747 long
748 y;
749
750 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000751 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000752 debug,
753 status;
754
755 MagickSizeType
756 number_pixels;
757
758 QuantumInfo
759 *quantum_info;
760
761 QuantumType
762 quantum_type;
763
764 register long
765 i;
766
767 size_t
768 length,
769 pad;
770
771 TIFF
772 *tiff;
773
774 TIFFErrorHandler
775 error_handler,
776 warning_handler;
777
778 TIFFMethodType
779 method;
780
781 uint16
782 compress_tag,
783 bits_per_sample,
784 endian,
785 extra_samples,
786 interlace,
787 max_sample_value,
788 min_sample_value,
789 orientation,
790 pages,
791 photometric,
792 *sample_info,
793 sample_format,
794 samples_per_pixel,
795 units,
796 value;
797
798 uint32
799 height,
800 rows_per_strip,
801 width;
802
803 unsigned char
804 *pixels;
805
806 unsigned long
807 lsb_first;
808
809 /*
810 Open image.
811 */
812 assert(image_info != (const ImageInfo *) NULL);
813 assert(image_info->signature == MagickSignature);
814 if (image_info->debug != MagickFalse)
815 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
816 image_info->filename);
817 assert(exception != (ExceptionInfo *) NULL);
818 assert(exception->signature == MagickSignature);
819 image=AcquireImage(image_info);
820 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
821 if (status == MagickFalse)
822 {
823 image=DestroyImageList(image);
824 return((Image *) NULL);
825 }
826 (void) MagickSetThreadValue(tiff_exception,exception);
827 error_handler=TIFFSetErrorHandler(TIFFErrors);
828 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
829 tiff=TIFFClientOpen(image->filename,"r",(thandle_t) image,TIFFReadBlob,
830 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
831 TIFFUnmapBlob);
832 if (tiff == (TIFF *) NULL)
833 {
834 (void) TIFFSetWarningHandler(warning_handler);
835 (void) TIFFSetErrorHandler(error_handler);
836 image=DestroyImageList(image);
837 return((Image *) NULL);
838 }
839 debug=IsEventLogging();
840 if (image_info->number_scenes != 0)
841 {
842 /*
843 Generate blank images for subimage specification (e.g. image.tif[4].
844 */
845 for (i=0; i < (long) image_info->scene; i++)
846 {
847 (void) TIFFReadDirectory(tiff);
848 AcquireNextImage(image_info,image);
849 if (GetNextImageInList(image) == (Image *) NULL)
850 {
851 image=DestroyImageList(image);
852 return((Image *) NULL);
853 }
854 image=SyncNextImageInList(image);
855 }
856 }
857 do
858 {
859 if (0 && (image_info->verbose != MagickFalse))
860 TIFFPrintDirectory(tiff,stdout,MagickFalse);
861 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
862 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
863 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
864 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
865 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
866 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
867 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
872 switch (photometric)
873 {
874 case PHOTOMETRIC_MINISBLACK:
875 {
876 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
877 break;
878 }
879 case PHOTOMETRIC_MINISWHITE:
880 {
881 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
882 break;
883 }
884 case PHOTOMETRIC_PALETTE:
885 {
886 (void) SetImageProperty(image,"tiff:photometric","palette");
887 break;
888 }
889 case PHOTOMETRIC_RGB:
890 {
891 (void) SetImageProperty(image,"tiff:photometric","RGB");
892 break;
893 }
894 case PHOTOMETRIC_CIELAB:
895 {
896 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
897 break;
898 }
899 case PHOTOMETRIC_SEPARATED:
900 {
901 (void) SetImageProperty(image,"tiff:photometric","separated");
902 break;
903 }
904 default:
905 {
906 (void) SetImageProperty(image,"tiff:photometric","unknown");
907 break;
908 }
909 }
910 if (image->debug != MagickFalse)
911 {
912 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
913 (unsigned int) width,(unsigned int) height);
914 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
915 interlace);
916 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
917 "Bits per sample: %u",bits_per_sample);
918 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
919 "Min sample value: %u",min_sample_value);
920 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
921 "Max sample value: %u",max_sample_value);
922 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
923 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
924 }
925 image->columns=(unsigned long) width;
926 image->rows=(unsigned long) height;
927 image->depth=(unsigned long) bits_per_sample;
928 if (image->debug != MagickFalse)
929 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %lu",
930 image->depth);
931 lsb_first=1;
932 image->endian=MSBEndian;
933 if ((int) (*(char *) &lsb_first) != 0)
934 image->endian=LSBEndian;
935 if (photometric == PHOTOMETRIC_SEPARATED)
936 image->colorspace=CMYKColorspace;
937 if (photometric == PHOTOMETRIC_CIELAB)
938 image->colorspace=LabColorspace;
939 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
940 &samples_per_pixel);
941 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
942 x_resolution=(float) image->x_resolution;
943 y_resolution=(float) image->y_resolution;
944 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
945 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
946 image->x_resolution=x_resolution;
947 image->y_resolution=y_resolution;
948 x_position=(float) image->page.x/x_resolution;
949 y_position=(float) image->page.y/y_resolution;
950 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
951 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristy06609ee2010-03-17 20:21:27 +0000952 image->page.x=(long) ceil(x_position*x_resolution-0.5);
953 image->page.y=(long) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000954 image->orientation=(OrientationType) orientation;
955 chromaticity=(float *) NULL;
956 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
957 if (chromaticity != (float *) NULL)
958 {
959 image->chromaticity.white_point.x=chromaticity[0];
960 image->chromaticity.white_point.y=chromaticity[1];
961 }
962 chromaticity=(float *) NULL;
963 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
964 if (chromaticity != (float *) NULL)
965 {
966 image->chromaticity.red_primary.x=chromaticity[0];
967 image->chromaticity.red_primary.y=chromaticity[1];
968 image->chromaticity.green_primary.x=chromaticity[2];
969 image->chromaticity.green_primary.y=chromaticity[3];
970 image->chromaticity.blue_primary.x=chromaticity[4];
971 image->chromaticity.blue_primary.y=chromaticity[5];
972 }
973 TIFFGetProperties(tiff,image);
974 option=GetImageOption(image_info,"tiff:exif");
975 if ((option != (const char *) NULL) &&
976 (IsMagickTrue(option) != MagickFalse))
977 TIFFGetEXIFProperties(tiff,image);
978 TIFFGetProfiles(tiff,image);
979 /*
980 Allocate memory for the image and pixel buffer.
981 */
982#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
983 if ((compress_tag != COMPRESSION_NONE) &&
984 (TIFFIsCODECConfigured(compress_tag) == 0))
985 {
986 TIFFClose(tiff);
987 ThrowReaderException(CoderError,"CompressNotSupported");
988 }
989#endif
990 switch (compress_tag)
991 {
992 case COMPRESSION_NONE: image->compression=NoCompression; break;
993 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
994 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
995 case COMPRESSION_JPEG:
996 {
997 image->compression=JPEGCompression;
998#if defined(JPEG_SUPPORT)
999 {
1000 char
1001 sampling_factor[MaxTextExtent];
1002
1003 uint16
1004 horizontal,
1005 vertical;
1006
1007 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1008 &horizontal,&vertical);
1009 (void) FormatMagickString(sampling_factor,MaxTextExtent,"%dx%d",
1010 horizontal,vertical);
1011 (void) SetImageProperty(image,"jpeg:sampling-factor",
1012 sampling_factor);
1013 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1014 "Sampling Factors: %s",sampling_factor);
1015 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1016 {
1017 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1018 JPEGCOLORMODE_RGB);
1019 photometric=PHOTOMETRIC_RGB;
1020 }
1021 }
1022#endif
1023 break;
1024 }
1025 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1026 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1027 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1028 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1029 default: image->compression=RLECompression; break;
1030 }
1031 quantum_info=AcquireQuantumInfo(image_info,image);
1032 if (quantum_info == (QuantumInfo *) NULL)
1033 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1034 if (sample_format == SAMPLEFORMAT_UINT)
1035 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1036 if (sample_format == SAMPLEFORMAT_INT)
1037 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1038 if (sample_format == SAMPLEFORMAT_IEEEFP)
1039 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1040 if (status == MagickFalse)
1041 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1042 status=MagickTrue;
1043 switch (photometric)
1044 {
1045 case PHOTOMETRIC_MINISBLACK:
1046 {
1047 quantum_info->min_is_white=MagickFalse;
1048 break;
1049 }
1050 case PHOTOMETRIC_MINISWHITE:
1051 {
1052 quantum_info->min_is_white=MagickTrue;
1053 break;
1054 }
1055 default:
1056 break;
1057 }
cristy891dc792010-03-04 01:47:16 +00001058 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001059 extra_samples=0;
1060 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1061 &sample_info);
1062 if (extra_samples == 0)
1063 {
1064 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1065 image->matte=MagickTrue;
1066 }
1067 else
cristy6c207342010-02-19 17:49:50 +00001068 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001069 {
cristy7cf69862010-03-03 13:57:24 +00001070 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001071 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001072 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001073 }
cristy891dc792010-03-04 01:47:16 +00001074 option=GetImageOption(image_info,"tiff:alpha");
1075 if (option != (const char *) NULL)
1076 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1077 MagickFalse;
1078 if (image->matte != MagickFalse)
1079 (void) SetImageProperty(image,"tiff:alpha",
1080 associated_alpha != MagickFalse ? "associated" : "unassociated");
cristy3ed852e2009-09-05 21:47:34 +00001081 if ((photometric == PHOTOMETRIC_PALETTE) &&
1082 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1083 {
1084 unsigned long
1085 colors;
1086
1087 colors=(unsigned long) GetQuantumRange(bits_per_sample)+1;
1088 if (AcquireImageColormap(image,colors) == MagickFalse)
1089 {
1090 TIFFClose(tiff);
1091 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1092 }
1093 }
1094 if (units == RESUNIT_INCH)
1095 image->units=PixelsPerInchResolution;
1096 if (units == RESUNIT_CENTIMETER)
1097 image->units=PixelsPerCentimeterResolution;
1098 value=(unsigned short) image->scene;
1099 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1100 image->scene=value;
1101 if (image_info->ping != MagickFalse)
1102 {
1103 if (image_info->number_scenes != 0)
1104 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1105 break;
1106 goto next_tiff_frame;
1107 }
1108 method=ReadGenericMethod;
1109 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1110 {
1111 char
1112 value[MaxTextExtent];
1113
1114 method=ReadStripMethod;
1115 (void) FormatMagickString(value,MaxTextExtent,"%u",
1116 (unsigned int) rows_per_strip);
1117 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1118 }
1119 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1120 method=ReadRGBAMethod;
1121 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1122 method=ReadCMYKAMethod;
1123 if ((photometric != PHOTOMETRIC_RGB) &&
1124 (photometric != PHOTOMETRIC_CIELAB) &&
1125 (photometric != PHOTOMETRIC_SEPARATED))
1126 method=ReadGenericMethod;
1127 if (image->storage_class == PseudoClass)
1128 method=ReadSingleSampleMethod;
1129 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1130 (photometric == PHOTOMETRIC_MINISWHITE))
1131 method=ReadSingleSampleMethod;
1132 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1133 (interlace == PLANARCONFIG_SEPARATE))
1134 method=ReadGenericMethod;
1135 if (TIFFIsTiled(tiff) != MagickFalse)
1136 method=ReadTileMethod;
1137 quantum_type=RGBQuantum;
1138 pixels=GetQuantumPixels(quantum_info);
1139 switch (method)
1140 {
1141 case ReadSingleSampleMethod:
1142 {
1143 /*
1144 Convert TIFF image to PseudoClass MIFF image.
1145 */
1146 if ((image->storage_class == PseudoClass) &&
1147 (photometric == PHOTOMETRIC_PALETTE))
1148 {
1149 unsigned long
1150 range;
1151
1152 uint16
1153 *blue_colormap,
1154 *green_colormap,
1155 *red_colormap;
1156
1157 /*
1158 Initialize colormap.
1159 */
1160 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1161 &green_colormap,&blue_colormap);
1162 range=255; /* might be old style 8-bit colormap */
1163 for (i=0; i < (long) image->colors; i++)
1164 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1165 (blue_colormap[i] >= 256))
1166 {
1167 range=65535;
1168 break;
1169 }
1170 for (i=0; i < (long) image->colors; i++)
1171 {
cristyce70c172010-01-07 17:15:30 +00001172 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001173 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001174 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001175 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001176 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001177 blue_colormap[i])/range);
1178 }
1179 }
1180 quantum_type=IndexQuantum;
1181 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1182 if (image->matte != MagickFalse)
1183 {
1184 if (image->storage_class != PseudoClass)
1185 {
1186 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1187 GrayAlphaQuantum;
1188 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1189 }
1190 else
1191 {
1192 quantum_type=IndexAlphaQuantum;
1193 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1194 }
1195 }
1196 else
1197 if (image->storage_class != PseudoClass)
1198 {
1199 quantum_type=GrayQuantum;
1200 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1201 }
1202 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1203 if (status == MagickFalse)
1204 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1205 pixels=GetQuantumPixels(quantum_info);
1206 for (y=0; y < (long) image->rows; y++)
1207 {
1208 int
1209 status;
1210
1211 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001212 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001213
1214 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1215 if (status == -1)
1216 break;
1217 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1218 if (q == (PixelPacket *) NULL)
1219 break;
1220 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1221 quantum_type,pixels,exception);
1222 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1223 break;
1224 if (image->previous == (Image *) NULL)
1225 {
1226 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1227 if (status == MagickFalse)
1228 break;
1229 }
1230 }
1231 break;
1232 }
1233 case ReadRGBAMethod:
1234 {
1235 /*
1236 Convert TIFF image to DirectClass MIFF image.
1237 */
1238 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1239 quantum_type=RGBQuantum;
1240 if (image->matte != MagickFalse)
1241 {
1242 quantum_type=RGBAQuantum;
1243 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1244 }
1245 if (image->colorspace == CMYKColorspace)
1246 {
1247 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1248 quantum_type=CMYKQuantum;
1249 if (image->matte != MagickFalse)
1250 {
1251 quantum_type=CMYKAQuantum;
1252 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1253 }
1254 }
1255 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1256 if (status == MagickFalse)
1257 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1258 pixels=GetQuantumPixels(quantum_info);
1259 for (y=0; y < (long) image->rows; y++)
1260 {
1261 int
1262 status;
1263
1264 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001265 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001266
1267 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1268 if (status == -1)
1269 break;
1270 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1271 if (q == (PixelPacket *) NULL)
1272 break;
cristy0b334892010-03-15 02:26:46 +00001273 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1274 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001275 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1276 break;
1277 if (image->previous == (Image *) NULL)
1278 {
1279 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1280 if (status == MagickFalse)
1281 break;
1282 }
1283 }
1284 break;
1285 }
1286 case ReadCMYKAMethod:
1287 {
1288 /*
1289 Convert TIFF image to DirectClass MIFF image.
1290 */
cristy6d9f12f2009-11-03 14:50:26 +00001291 for (i=0; i < (long) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001292 {
cristy6d9f12f2009-11-03 14:50:26 +00001293 for (y=0; y < (long) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001294 {
cristy6d9f12f2009-11-03 14:50:26 +00001295 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001296 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001297
cristy3ed852e2009-09-05 21:47:34 +00001298 int
1299 status;
1300
1301 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1302 pixels);
1303 if (status == -1)
1304 break;
1305 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1306 if (q == (PixelPacket *) NULL)
1307 break;
1308 if (image->colorspace != CMYKColorspace)
1309 switch (i)
1310 {
1311 case 0: quantum_type=RedQuantum; break;
1312 case 1: quantum_type=GreenQuantum; break;
1313 case 2: quantum_type=BlueQuantum; break;
1314 case 3: quantum_type=AlphaQuantum; break;
1315 default: quantum_type=UndefinedQuantum; break;
1316 }
cristy6d9f12f2009-11-03 14:50:26 +00001317 else
cristy3ed852e2009-09-05 21:47:34 +00001318 switch (i)
1319 {
1320 case 0: quantum_type=CyanQuantum; break;
1321 case 1: quantum_type=MagentaQuantum; break;
1322 case 2: quantum_type=YellowQuantum; break;
1323 case 3: quantum_type=BlackQuantum; break;
1324 case 4: quantum_type=AlphaQuantum; break;
1325 default: quantum_type=UndefinedQuantum; break;
1326 }
1327 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1328 quantum_type,pixels,exception);
1329 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1330 break;
1331 }
1332 if (image->previous == (Image *) NULL)
1333 {
1334 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1335 if (status == MagickFalse)
1336 break;
1337 }
1338 }
1339 break;
1340 }
1341 case ReadStripMethod:
1342 {
1343 register unsigned char
1344 *p;
1345
1346 /*
1347 Convert stripped TIFF image to DirectClass MIFF image.
1348 */
1349 i=0;
1350 p=(unsigned char *) NULL;
1351 for (y=0; y < (long) image->rows; y++)
1352 {
1353 register long
1354 x;
1355
1356 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001357 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001358
1359 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1360 if (q == (PixelPacket *) NULL)
1361 break;
1362 if (i == 0)
1363 {
1364 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1365 break;
1366 i=(long) MagickMin((long) rows_per_strip,(long) image->rows-y);
1367 }
1368 i--;
1369 p=pixels+image->columns*i;
1370 for (x=0; x < (long) image->columns; x++)
1371 {
1372 q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
1373 q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
1374 q->blue=ScaleCharToQuantum((unsigned char) (TIFFGetB(*p)));
1375 if (image->matte != MagickFalse)
1376 q->opacity=ScaleCharToQuantum((unsigned char) (TIFFGetA(*p)));
1377 p++;
1378 q++;
1379 }
1380 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1381 break;
1382 if (image->previous == (Image *) NULL)
1383 {
1384 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1385 if (status == MagickFalse)
1386 break;
1387 }
1388 }
1389 break;
1390 }
1391 case ReadTileMethod:
1392 {
1393 register uint32
1394 *p;
1395
1396 uint32
1397 *tile_pixels,
1398 columns,
1399 rows;
1400
1401 unsigned long
1402 number_pixels;
1403
1404 /*
1405 Convert tiled TIFF image to DirectClass MIFF image.
1406 */
1407 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1408 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1409 {
1410 TIFFClose(tiff);
1411 ThrowReaderException(CoderError,"ImageIsNotTiled");
1412 }
1413 (void) SetImageStorageClass(image,DirectClass);
1414 number_pixels=columns*rows;
1415 tile_pixels=(uint32 *) AcquireQuantumMemory((size_t) columns*rows,
1416 sizeof(*tile_pixels));
1417 if (tile_pixels == (uint32 *) NULL)
1418 {
1419 TIFFClose(tiff);
1420 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1421 }
1422 for (y=0; y < (long) image->rows; y+=rows)
1423 {
1424 PixelPacket
1425 *tile;
1426
1427 register long
1428 x;
1429
1430 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001431 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001432
1433 unsigned long
1434 columns_remaining,
1435 rows_remaining;
1436
1437 rows_remaining=image->rows-y;
1438 if ((long) (y+rows) < (long) image->rows)
1439 rows_remaining=rows;
1440 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1441 exception);
1442 if (tile == (PixelPacket *) NULL)
1443 break;
1444 for (x=0; x < (long) image->columns; x+=columns)
1445 {
1446 unsigned long
1447 column,
1448 row;
1449
1450 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1451 break;
1452 columns_remaining=image->columns-x;
1453 if ((long) (x+columns) < (long) image->columns)
1454 columns_remaining=columns;
1455 p=tile_pixels+(rows-rows_remaining)*columns;
1456 q=tile+(image->columns*(rows_remaining-1)+x);
1457 for (row=rows_remaining; row > 0; row--)
1458 {
1459 if (image->matte != MagickFalse)
1460 for (column=columns_remaining; column > 0; column--)
1461 {
1462 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1463 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1464 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1465 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1466 (unsigned char) TIFFGetA(*p)));
1467 q++;
1468 p++;
1469 }
1470 else
1471 for (column=columns_remaining; column > 0; column--)
1472 {
1473 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1474 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1475 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1476 q++;
1477 p++;
1478 }
1479 p+=columns-columns_remaining;
1480 q-=(image->columns+columns_remaining);
1481 }
1482 }
1483 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1484 break;
1485 if (image->previous == (Image *) NULL)
1486 {
1487 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1488 if (status == MagickFalse)
1489 break;
1490 }
1491 }
1492 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1493 break;
1494 }
1495 case ReadGenericMethod:
1496 default:
1497 {
1498 register uint32
1499 *p;
1500
1501 uint32
1502 *pixels;
1503
1504 /*
1505 Convert TIFF image to DirectClass MIFF image.
1506 */
1507 number_pixels=(MagickSizeType) image->columns*image->rows;
1508 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1509 (number_pixels*sizeof(uint32))))
1510 {
1511 TIFFClose(tiff);
1512 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1513 }
1514 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1515 sizeof(uint32));
1516 if (pixels == (uint32 *) NULL)
1517 {
1518 TIFFClose(tiff);
1519 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1520 }
1521 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1522 (uint32) image->rows,(uint32 *) pixels,0);
1523 /*
1524 Convert image to DirectClass pixel packets.
1525 */
1526 p=pixels+number_pixels-1;
1527 for (y=0; y < (long) image->rows; y++)
1528 {
1529 register long
1530 x;
1531
1532 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001533 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001534
1535 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1536 if (q == (PixelPacket *) NULL)
1537 break;
1538 q+=image->columns-1;
1539 for (x=0; x < (long) image->columns; x++)
1540 {
1541 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1542 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1543 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1544 if (image->matte != MagickFalse)
1545 q->opacity=ScaleCharToQuantum((unsigned char) TIFFGetA(*p));
1546 p--;
1547 q--;
1548 }
1549 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1550 break;
1551 if (image->previous == (Image *) NULL)
1552 {
1553 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1554 if (status == MagickFalse)
1555 break;
1556 }
1557 }
1558 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1559 break;
1560 }
1561 }
1562 SetQuantumImageType(image,quantum_type);
1563 next_tiff_frame:
1564 if ((photometric == PHOTOMETRIC_LOGL) ||
1565 (photometric == PHOTOMETRIC_MINISBLACK) ||
1566 (photometric == PHOTOMETRIC_MINISWHITE))
1567 {
1568 image->type=GrayscaleType;
1569 if (bits_per_sample == 1)
1570 image->type=BilevelType;
1571 }
1572 if (image->storage_class == PseudoClass)
1573 image->depth=GetImageDepth(image,exception);
1574 image->endian=MSBEndian;
1575 if (endian == FILLORDER_LSB2MSB)
1576 image->endian=LSBEndian;
1577 if ((photometric == PHOTOMETRIC_LOGL) ||
1578 (photometric == PHOTOMETRIC_MINISBLACK) ||
1579 (photometric == PHOTOMETRIC_MINISWHITE))
1580 {
1581 image->type=GrayscaleType;
1582 if (bits_per_sample == 1)
1583 image->type=BilevelType;
1584 }
1585 /*
1586 Proceed to next image.
1587 */
1588 if (image_info->number_scenes != 0)
1589 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1590 break;
1591 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1592 if (status == MagickTrue)
1593 {
1594 /*
1595 Allocate next image structure.
1596 */
1597 AcquireNextImage(image_info,image);
1598 if (GetNextImageInList(image) == (Image *) NULL)
1599 {
1600 image=DestroyImageList(image);
1601 return((Image *) NULL);
1602 }
1603 image=SyncNextImageInList(image);
1604 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1605 image->scene);
1606 if (status == MagickFalse)
1607 break;
1608 }
1609 quantum_info=DestroyQuantumInfo(quantum_info);
1610 } while (status == MagickTrue);
1611 (void) TIFFSetWarningHandler(warning_handler);
1612 (void) TIFFSetErrorHandler(error_handler);
1613 TIFFClose(tiff);
1614 return(GetFirstImageInList(image));
1615}
1616#endif
1617
1618/*
1619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1620% %
1621% %
1622% %
1623% R e g i s t e r T I F F I m a g e %
1624% %
1625% %
1626% %
1627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1628%
1629% RegisterTIFFImage() adds properties for the TIFF image format to
1630% the list of supported formats. The properties include the image format
1631% tag, a method to read and/or write the format, whether the format
1632% supports the saving of more than one frame to the same file or blob,
1633% whether the format supports native in-memory I/O, and a brief
1634% description of the format.
1635%
1636% The format of the RegisterTIFFImage method is:
1637%
1638% unsigned long RegisterTIFFImage(void)
1639%
1640*/
1641ModuleExport unsigned long RegisterTIFFImage(void)
1642{
1643#define TIFFDescription "Tagged Image File Format"
1644
1645 char
1646 version[MaxTextExtent];
1647
1648 MagickInfo
1649 *entry;
cristy891dc792010-03-04 01:47:16 +00001650
cristy18b17442009-10-25 18:36:48 +00001651 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1652 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001653 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001654 if (instantiate_key == MagickFalse)
1655 {
1656 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1657 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1658 instantiate_key=MagickTrue;
1659 }
cristyf84a1932010-01-03 18:00:18 +00001660 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001661 *version='\0';
1662#if defined(TIFF_VERSION)
1663 (void) FormatMagickString(version,MaxTextExtent,"%d",TIFF_VERSION);
1664#endif
1665#if defined(MAGICKCORE_TIFF_DELEGATE)
1666 {
1667 const char
1668 *p;
1669
1670 register long int
1671 i;
1672
1673 p=TIFFGetVersion();
1674 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1675 version[i]=(*p++);
1676 version[i]='\0';
1677 }
1678#endif
1679
cristy3d7f8062009-09-24 20:45:35 +00001680 entry=SetMagickInfo("GROUP4");
1681#if defined(MAGICKCORE_TIFF_DELEGATE)
1682 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1683 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1684#endif
1685 entry->raw=MagickTrue;
1686 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001687 entry->adjoin=MagickFalse;
cristy3d7f8062009-09-24 20:45:35 +00001688 entry->seekable_stream=MagickTrue;
1689 entry->thread_support=NoThreadSupport;
1690 entry->description=ConstantString("Raw CCITT Group4");
1691 entry->module=ConstantString("TIFF");
1692 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001693 entry=SetMagickInfo("PTIF");
1694#if defined(MAGICKCORE_TIFF_DELEGATE)
1695 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1696 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1697#endif
1698 entry->endian_support=MagickTrue;
1699 entry->seekable_stream=MagickTrue;
1700 entry->thread_support=NoThreadSupport;
1701 entry->description=ConstantString("Pyramid encoded TIFF");
1702 entry->module=ConstantString("TIFF");
1703 (void) RegisterMagickInfo(entry);
1704 entry=SetMagickInfo("TIF");
1705#if defined(MAGICKCORE_TIFF_DELEGATE)
1706 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1707 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1708#endif
1709 entry->endian_support=MagickTrue;
1710 entry->seekable_stream=MagickTrue;
1711 entry->stealth=MagickTrue;
1712 entry->thread_support=NoThreadSupport;
1713 entry->description=ConstantString(TIFFDescription);
1714 if (*version != '\0')
1715 entry->version=ConstantString(version);
1716 entry->module=ConstantString("TIFF");
1717 (void) RegisterMagickInfo(entry);
1718 entry=SetMagickInfo("TIFF");
1719#if defined(MAGICKCORE_TIFF_DELEGATE)
1720 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1721 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1722#endif
1723 entry->magick=(IsImageFormatHandler *) IsTIFF;
1724 entry->endian_support=MagickTrue;
1725 entry->seekable_stream=MagickTrue;
1726 entry->thread_support=NoThreadSupport;
1727 entry->description=ConstantString(TIFFDescription);
1728 if (*version != '\0')
1729 entry->version=ConstantString(version);
1730 entry->module=ConstantString("TIFF");
1731 (void) RegisterMagickInfo(entry);
1732 entry=SetMagickInfo("TIFF64");
1733#if defined(TIFF_VERSION_BIG)
1734 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1735 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1736#endif
1737 entry->adjoin=MagickFalse;
1738 entry->endian_support=MagickTrue;
1739 entry->seekable_stream=MagickTrue;
1740 entry->thread_support=NoThreadSupport;
1741 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1742 if (*version != '\0')
1743 entry->version=ConstantString(version);
1744 entry->module=ConstantString("TIFF");
1745 (void) RegisterMagickInfo(entry);
1746 return(MagickImageCoderSignature);
1747}
1748
1749/*
1750%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1751% %
1752% %
1753% %
1754% U n r e g i s t e r T I F F I m a g e %
1755% %
1756% %
1757% %
1758%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1759%
1760% UnregisterTIFFImage() removes format registrations made by the TIFF module
1761% from the list of supported formats.
1762%
1763% The format of the UnregisterTIFFImage method is:
1764%
1765% UnregisterTIFFImage(void)
1766%
1767*/
1768ModuleExport void UnregisterTIFFImage(void)
1769{
cristy3d7f8062009-09-24 20:45:35 +00001770 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001771 (void) UnregisterMagickInfo("PTIF");
1772 (void) UnregisterMagickInfo("TIF");
1773 (void) UnregisterMagickInfo("TIFF");
1774 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001775 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1776 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001777 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001778 if (instantiate_key != MagickFalse)
1779 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1780 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1781 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001782 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001783 DestroySemaphoreInfo(&tiff_semaphore);
1784}
1785
1786#if defined(MAGICKCORE_TIFF_DELEGATE)
1787/*
1788%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1789% %
1790% %
1791% %
cristy3d7f8062009-09-24 20:45:35 +00001792% W r i t e G R O U P 4 I m a g e %
1793% %
1794% %
1795% %
1796%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1797%
1798% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1799%
1800% The format of the WriteGROUP4Image method is:
1801%
1802% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1803% Image *image)
1804%
1805% A description of each parameter follows:
1806%
1807% o image_info: the image info.
1808%
1809% o image: The image.
1810%
1811*/
1812static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1813 Image *image)
1814{
1815 char
1816 filename[MaxTextExtent];
1817
1818 FILE
1819 *file;
1820
1821 Image
1822 *huffman_image;
1823
1824 ImageInfo
1825 *write_info;
1826
1827 int
1828 unique_file;
1829
1830 MagickBooleanType
1831 status;
1832
1833 register long
1834 i;
1835
1836 ssize_t
1837 count;
1838
1839 TIFF
1840 *tiff;
1841
cristy94c8fe42009-10-06 01:57:36 +00001842 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001843 *byte_count,
1844 strip_size;
1845
1846 unsigned char
1847 *buffer;
1848
1849 /*
1850 Write image as CCITT Group4 TIFF image to a temporary file.
1851 */
1852 assert(image_info != (const ImageInfo *) NULL);
1853 assert(image_info->signature == MagickSignature);
1854 assert(image != (Image *) NULL);
1855 assert(image->signature == MagickSignature);
1856 if (image->debug != MagickFalse)
1857 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1858 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1859 if (status == MagickFalse)
1860 return(status);
1861 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1862 if (huffman_image == (Image *) NULL)
1863 {
1864 (void) CloseBlob(image);
1865 return(MagickFalse);
1866 }
cristy94c8fe42009-10-06 01:57:36 +00001867 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001868 file=(FILE *) NULL;
1869 unique_file=AcquireUniqueFileResource(filename);
1870 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001871 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001872 if ((unique_file == -1) || (file == (FILE *) NULL))
1873 {
1874 ThrowFileException(&image->exception,FileOpenError,
1875 "UnableToCreateTemporaryFile",filename);
1876 return(MagickFalse);
1877 }
1878 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1879 filename);
1880 (void) SetImageType(huffman_image,BilevelType);
cristy14efd992009-09-26 23:46:03 +00001881 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001882 SetImageInfoFile(write_info,file);
1883 write_info->compression=Group4Compression;
1884 write_info->type=BilevelType;
1885 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1886 status=WriteTIFFImage(write_info,huffman_image);
1887 (void) fflush(file);
1888 write_info=DestroyImageInfo(write_info);
1889 if (status == MagickFalse)
1890 {
1891 InheritException(&image->exception,&huffman_image->exception);
1892 huffman_image=DestroyImage(huffman_image);
1893 (void) fclose(file);
1894 (void) RelinquishUniqueFileResource(filename);
1895 return(MagickFalse);
1896 }
1897 tiff=TIFFOpen(filename,"rb");
1898 if (tiff == (TIFF *) NULL)
1899 {
1900 huffman_image=DestroyImage(huffman_image);
1901 (void) fclose(file);
1902 (void) RelinquishUniqueFileResource(filename);
1903 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1904 image_info->filename);
1905 return(MagickFalse);
1906 }
1907 /*
1908 Allocate raw strip buffer.
1909 */
cristy94c8fe42009-10-06 01:57:36 +00001910 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1911 {
1912 TIFFClose(tiff);
1913 huffman_image=DestroyImage(huffman_image);
1914 (void) fclose(file);
1915 (void) RelinquishUniqueFileResource(filename);
1916 return(MagickFalse);
1917 }
cristy3d7f8062009-09-24 20:45:35 +00001918 strip_size=byte_count[0];
1919 for (i=1; i < (long) TIFFNumberOfStrips(tiff); i++)
1920 if (byte_count[i] > strip_size)
1921 strip_size=byte_count[i];
1922 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1923 sizeof(*buffer));
1924 if (buffer == (unsigned char *) NULL)
1925 {
1926 TIFFClose(tiff);
1927 huffman_image=DestroyImage(huffman_image);
1928 (void) fclose(file);
1929 (void) RelinquishUniqueFileResource(filename);
1930 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1931 image_info->filename);
1932 }
1933 /*
1934 Compress runlength encoded to 2D Huffman pixels.
1935 */
cristy3d7f8062009-09-24 20:45:35 +00001936 for (i=0; i < (long) TIFFNumberOfStrips(tiff); i++)
1937 {
cristy94c8fe42009-10-06 01:57:36 +00001938 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00001939 if (WriteBlob(image,(size_t) count,buffer) != count)
1940 status=MagickFalse;
1941 }
1942 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1943 TIFFClose(tiff);
1944 huffman_image=DestroyImage(huffman_image);
1945 (void) fclose(file);
1946 (void) RelinquishUniqueFileResource(filename);
1947 (void) CloseBlob(image);
1948 return(status);
1949}
1950#endif
1951
1952#if defined(MAGICKCORE_TIFF_DELEGATE)
1953/*
1954%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1955% %
1956% %
1957% %
cristy3ed852e2009-09-05 21:47:34 +00001958% W r i t e P T I F I m a g e %
1959% %
1960% %
1961% %
1962%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1963%
1964% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
1965% format.
1966%
1967% The format of the WritePTIFImage method is:
1968%
1969% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
1970% Image *image)
1971%
1972% A description of each parameter follows:
1973%
1974% o image_info: the image info.
1975%
1976% o image: The image.
1977%
1978*/
1979static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
1980 Image *image)
1981{
1982 Image
1983 *images,
1984 *next,
1985 *pyramid_image;
1986
1987 ImageInfo
1988 *write_info;
1989
1990 MagickBooleanType
1991 status;
1992
1993 unsigned long
1994 columns,
1995 rows;
1996
1997 /*
1998 Create pyramid-encoded TIFF image.
1999 */
2000 images=NewImageList();
2001 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2002 {
2003 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2004 &image->exception));
2005 columns=next->columns;
2006 rows=next->rows;
2007 while ((columns > 64) && (rows > 64))
2008 {
2009 columns/=2;
2010 rows/=2;
2011 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2012 &image->exception);
2013 AppendImageToList(&images,pyramid_image);
2014 }
2015 }
2016 /*
2017 Write pyramid-encoded TIFF image.
2018 */
cristy58567532009-09-26 01:13:32 +00002019 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002020 *write_info->magick='\0';
2021 write_info->adjoin=MagickTrue;
2022 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2023 images=DestroyImageList(images);
2024 write_info=DestroyImageInfo(write_info);
2025 return(status);
2026}
2027#endif
2028
2029#if defined(MAGICKCORE_TIFF_DELEGATE)
2030/*
2031%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2032% %
2033% %
2034% W r i t e T I F F I m a g e %
2035% %
2036% %
2037% %
2038%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2039%
2040% WriteTIFFImage() writes an image in the Tagged image file format.
2041%
2042% The format of the WriteTIFFImage method is:
2043%
2044% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2045% Image *image)
2046%
2047% A description of each parameter follows:
2048%
2049% o image_info: the image info.
2050%
2051% o image: The image.
2052%
2053*/
2054
2055typedef struct _TIFFInfo
2056{
2057 RectangleInfo
2058 tile_geometry;
2059
2060 unsigned char
2061 *scanline,
2062 *scanlines,
2063 *pixels;
2064} TIFFInfo;
2065
2066static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2067{
2068 assert(tiff_info != (TIFFInfo *) NULL);
2069 if (tiff_info->scanlines != (unsigned char *) NULL)
2070 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2071 tiff_info->scanlines);
2072 if (tiff_info->pixels != (unsigned char *) NULL)
2073 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2074 tiff_info->pixels);
2075}
2076
2077static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2078 TIFFInfo *tiff_info)
2079{
2080 const char
2081 *option;
2082
2083 MagickStatusType
2084 flags;
2085
2086 assert(tiff_info != (TIFFInfo *) NULL);
2087 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2088 option=GetImageOption(image_info,"tiff:tile-geometry");
2089 if (option == (const char *) NULL)
2090 return(MagickTrue);
2091 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2092 if ((flags & HeightValue) == 0)
2093 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2094 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,(uint32)
2095 tiff_info->tile_geometry.width);
2096 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,(uint32)
2097 tiff_info->tile_geometry.height);
2098 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2099 tiff_info->tile_geometry.height*TIFFScanlineSize(tiff),
2100 sizeof(*tiff_info->scanlines));
2101 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2102 TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2103 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2104 (tiff_info->pixels == (unsigned char *) NULL))
2105 {
2106 DestroyTIFFInfo(tiff_info);
2107 return(MagickFalse);
2108 }
2109 return(MagickTrue);
2110}
2111
2112static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,long row,
2113 tsample_t sample,Image *image)
2114{
2115 int32
2116 status;
2117
2118 long
2119 bytes_per_pixel,
2120 j,
2121 k,
2122 l;
2123
2124 register long
2125 i;
2126
2127 register unsigned char
2128 *p,
2129 *q;
2130
2131 unsigned long
2132 number_tiles,
2133 tile_width;
2134
2135 if (TIFFIsTiled(tiff) == 0)
2136 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2137 /*
2138 Fill scanlines to tile height.
2139 */
2140 i=(long) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2141 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2142 (size_t) TIFFScanlineSize(tiff));
2143 if (((unsigned long) (row % tiff_info->tile_geometry.height) !=
2144 (tiff_info->tile_geometry.height-1)) && (row != (long) (image->rows-1)))
2145 return(0);
2146 /*
2147 Write tile to TIFF image.
2148 */
2149 status=0;
2150 bytes_per_pixel=TIFFTileSize(tiff)/(long) (tiff_info->tile_geometry.height*
2151 tiff_info->tile_geometry.width);
2152 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2153 tiff_info->tile_geometry.width;
2154 for (i=0; i < (long) number_tiles; i++)
2155 {
2156 tile_width=(i == (long) (number_tiles-1)) ? image->columns-(i*
2157 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2158 for (j=0; j < (long) ((row % tiff_info->tile_geometry.height)+1); j++)
2159 for (k=0; k < (long) tile_width; k++)
2160 {
2161 if (bytes_per_pixel == 0)
2162 {
2163 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2164 tiff_info->tile_geometry.width+k)/8);
2165 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2166 *q++=(*p++);
2167 continue;
2168 }
2169 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2170 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2171 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2172 for (l=0; l < bytes_per_pixel; l++)
2173 *q++=(*p++);
2174 }
2175 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2176 tiff_info->tile_geometry.width),(uint32) ((row/
2177 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2178 sample);
2179 if (status < 0)
2180 break;
2181 }
2182 return(status);
2183}
2184
2185static void TIFFSetProfiles(TIFF *tiff,Image *image)
2186{
2187 const char
2188 *name;
2189
2190 const StringInfo
2191 *profile;
2192
2193 if (image->profiles == (void *) NULL)
2194 return;
2195 ResetImageProfileIterator(image);
2196 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2197 {
2198 profile=GetImageProfile(image,name);
2199#if defined(TIFFTAG_XMLPACKET)
2200 if (LocaleCompare(name,"xmp") == 0)
2201 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2202 profile),GetStringInfoDatum(profile));
2203#endif
2204#if defined(TIFFTAG_ICCPROFILE)
2205 if (LocaleCompare(name,"icc") == 0)
2206 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2207 profile),GetStringInfoDatum(profile));
2208#endif
2209 if (LocaleCompare(name,"iptc") == 0)
2210 {
2211 size_t
2212 length;
2213
2214 StringInfo
2215 *iptc_profile;
2216
2217 iptc_profile=CloneStringInfo(profile);
2218 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2219 0x03);
2220 SetStringInfoLength(iptc_profile,length);
2221 if (TIFFIsByteSwapped(tiff))
2222 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2223 (unsigned long) (length/4));
2224 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2225 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2226 iptc_profile=DestroyStringInfo(iptc_profile);
2227 }
2228#if defined(TIFFTAG_PHOTOSHOP)
2229 if (LocaleCompare(name,"8bim") == 0)
2230 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2231 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2232#endif
2233 if (LocaleCompare(name,"tiff:37724") == 0)
2234 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2235 GetStringInfoDatum(profile));
2236 name=GetNextImageProfile(image);
2237 }
2238}
2239
2240static void TIFFSetProperties(TIFF *tiff,Image *image)
2241{
2242 const char
2243 *value;
2244
2245 value=GetImageProperty(image,"tiff:hostcomputer");
2246 if (value != (const char *) NULL)
2247 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2248 value=GetImageProperty(image,"tiff:artist");
2249 if (value != (const char *) NULL)
2250 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2251 value=GetImageProperty(image,"tiff:timestamp");
2252 if (value != (const char *) NULL)
2253 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2254 value=GetImageProperty(image,"tiff:make");
2255 if (value != (const char *) NULL)
2256 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2257 value=GetImageProperty(image,"tiff:model");
2258 if (value != (const char *) NULL)
2259 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2260 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,
2261 GetMagickVersion((unsigned long *) NULL));
2262 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
2263 value=GetImageProperty(image,"tiff:copyright");
2264 if (value != (const char *) NULL)
2265 (void) TIFFSetField(tiff,33432,value);
2266 value=GetImageProperty(image,"kodak-33423");
2267 if (value != (const char *) NULL)
2268 (void) TIFFSetField(tiff,33423,value);
2269 value=GetImageProperty(image,"kodak-36867");
2270 if (value != (const char *) NULL)
2271 (void) TIFFSetField(tiff,36867,value);
2272 value=GetImageProperty(image,"label");
2273 if (value != (const char *) NULL)
2274 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2275 value=GetImageProperty(image,"comment");
2276 if (value != (const char *) NULL)
2277 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2278}
2279
2280static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2281{
2282#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2283 const char
2284 *value;
2285
2286 register long
2287 i;
2288
2289 uint32
2290 offset;
2291
2292 /*
2293 Write EXIF properties.
2294 */
2295 offset=0;
2296 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2297 for (i=0; exif_info[i].tag != 0; i++)
2298 {
2299 value=GetImageProperty(image,exif_info[i].property);
2300 if (value == (const char *) NULL)
2301 continue;
2302 switch (exif_info[i].type)
2303 {
2304 case TIFF_ASCII:
2305 {
2306 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2307 break;
2308 }
2309 case TIFF_SHORT:
2310 {
2311 uint16
2312 shorty;
2313
cristyf2f27272009-12-17 14:48:46 +00002314 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002315 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2316 break;
2317 }
2318 case TIFF_LONG:
2319 {
2320 uint16
2321 longy;
2322
cristyf2f27272009-12-17 14:48:46 +00002323 longy=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002324 (void) TIFFSetField(tiff,exif_info[i].tag,longy);
2325 break;
2326 }
2327 case TIFF_RATIONAL:
2328 case TIFF_SRATIONAL:
2329 {
2330 float
2331 rational;
2332
cristyf2f27272009-12-17 14:48:46 +00002333 rational=StringToDouble(value);
cristy3ed852e2009-09-05 21:47:34 +00002334 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2335 break;
2336 }
2337 default:
2338 break;
2339 }
2340 }
2341 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2342#else
2343 (void) tiff;
2344 (void) image;
2345#endif
2346}
2347
2348static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2349 Image *image)
2350{
2351#if !defined(TIFFDefaultStripSize)
2352#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2353#endif
2354
2355 const char
2356 *mode,
cristy5f0f48d2010-03-27 14:51:16 +00002357 *option,
cristy3ed852e2009-09-05 21:47:34 +00002358 *value;
2359
2360 CompressionType
2361 compression;
2362
2363 long
2364 y;
2365
2366 MagickBooleanType
2367 debug,
2368 status;
2369
2370 MagickOffsetType
2371 scene;
2372
2373 QuantumInfo
2374 *quantum_info;
2375
2376 QuantumType
2377 quantum_type;
2378
2379 register long
2380 i;
2381
2382 size_t
2383 length;
2384
2385 TIFF
2386 *tiff;
2387
2388 TIFFErrorHandler
2389 error_handler,
2390 warning_handler;
2391
2392 TIFFInfo
2393 tiff_info;
2394
2395 uint16
2396 bits_per_sample,
2397 compress_tag,
2398 endian,
2399 photometric;
2400
2401 uint32
2402 rows_per_strip;
2403
2404 unsigned char
2405 *pixels;
2406
2407 unsigned long
2408 lsb_first;
2409
2410 /*
2411 Open TIFF file.
2412 */
2413 assert(image_info != (const ImageInfo *) NULL);
2414 assert(image_info->signature == MagickSignature);
2415 assert(image != (Image *) NULL);
2416 assert(image->signature == MagickSignature);
2417 if (image->debug != MagickFalse)
2418 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2419 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2420 if (status == MagickFalse)
2421 return(status);
2422 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2423 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2424 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
2425 switch (image_info->endian)
2426 {
2427 case LSBEndian: mode="wl"; break;
2428 case MSBEndian: mode="wb"; break;
2429 default: mode="w"; break;
2430 }
2431#if defined(TIFF_VERSION_BIG)
2432 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
2433 switch (image_info->endian)
2434 {
2435 case LSBEndian: mode="wl8"; break;
2436 case MSBEndian: mode="wb8"; break;
2437 default: mode="w8"; break;
2438 }
2439#endif
2440 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2441 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2442 TIFFUnmapBlob);
2443 if (tiff == (TIFF *) NULL)
2444 {
2445 (void) TIFFSetWarningHandler(warning_handler);
2446 (void) TIFFSetErrorHandler(error_handler);
2447 return(MagickFalse);
2448 }
2449 scene=0;
2450 debug=IsEventLogging();
2451 do
2452 {
2453 /*
2454 Initialize TIFF fields.
2455 */
2456 if ((image_info->type != UndefinedType) &&
2457 (image_info->type != OptimizeType))
2458 (void) SetImageType(image,image_info->type);
2459 quantum_info=AcquireQuantumInfo(image_info,image);
2460 if (quantum_info == (QuantumInfo *) NULL)
2461 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2462 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2463 (quantum_info->format == UndefinedQuantumFormat) &&
2464 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2465 {
2466 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2467 if (status == MagickFalse)
2468 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2469 }
2470 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2471 (GetPreviousImageInList(image) != (Image *) NULL))
2472 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2473 if ((image->columns != (uint32) image->columns) ||
2474 (image->rows != (uint32) image->rows))
2475 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2476 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2477 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2478 compression=image->compression;
2479 if (image_info->compression != UndefinedCompression)
2480 compression=image_info->compression;
2481 switch (compression)
2482 {
2483 case FaxCompression:
2484 {
2485 compress_tag=COMPRESSION_CCITTFAX3;
2486 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2487 break;
2488 }
2489 case Group4Compression:
2490 {
2491 compress_tag=COMPRESSION_CCITTFAX4;
2492 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2493 break;
2494 }
2495 case JPEGCompression:
2496 {
2497 compress_tag=COMPRESSION_JPEG;
2498 break;
2499 }
2500 case LZWCompression:
2501 {
2502 compress_tag=COMPRESSION_LZW;
2503 break;
2504 }
2505 case RLECompression:
2506 {
2507 compress_tag=COMPRESSION_PACKBITS;
2508 break;
2509 }
2510 case ZipCompression:
2511 {
2512 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2513 break;
2514 }
2515 case NoCompression:
2516 default:
2517 {
2518 compress_tag=COMPRESSION_NONE;
2519 break;
2520 }
2521 }
cristy5b675872009-10-21 13:12:00 +00002522#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2523 if ((compress_tag != COMPRESSION_NONE) &&
2524 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002525 {
cristy5b675872009-10-21 13:12:00 +00002526 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2527 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
2528 MagickCompressOptions,(long) compression));
2529 compress_tag=COMPRESSION_NONE;
2530 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002531 }
2532#else
2533 switch (compress_tag)
2534 {
2535#if defined(CCITT_SUPPORT)
2536 case COMPRESSION_CCITTFAX3:
2537 case COMPRESSION_CCITTFAX4:
2538#endif
2539#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2540 case COMPRESSION_JPEG:
2541#endif
2542#if defined(LZW_SUPPORT)
2543 case COMPRESSION_LZW:
2544#endif
2545#if defined(PACKBITS_SUPPORT)
2546 case COMPRESSION_PACKBITS:
2547#endif
2548#if defined(ZIP_SUPPORT)
2549 case COMPRESSION_ADOBE_DEFLATE:
2550#endif
2551 case COMPRESSION_NONE:
2552 break;
2553 default:
2554 {
2555 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2556 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
2557 MagickCompressOptions,(long) compression));
2558 compress_tag=COMPRESSION_NONE;
2559 compression=NoCompression;
2560 break;
2561 }
2562 }
2563#endif
2564 switch (compression)
2565 {
2566 case FaxCompression:
2567 case Group4Compression:
2568 {
2569 (void) SetImageType(image,BilevelType);
2570 break;
2571 }
2572 case JPEGCompression:
2573 {
2574 (void) SetImageStorageClass(image,DirectClass);
2575 (void) SetImageDepth(image,8);
2576 break;
2577 }
2578 default:
2579 break;
2580 }
cristy3ed852e2009-09-05 21:47:34 +00002581 if (image->colorspace == CMYKColorspace)
2582 {
2583 photometric=PHOTOMETRIC_SEPARATED;
2584 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2585 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2586 }
2587 else
2588 {
2589 /*
2590 Full color TIFF raster.
2591 */
2592 if (image->colorspace == LabColorspace)
2593 photometric=PHOTOMETRIC_CIELAB;
2594 else
2595 if (image->colorspace == YCbCrColorspace)
2596 {
2597 photometric=PHOTOMETRIC_YCBCR;
2598 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
2599 (void) SetImageDepth(image,8);
2600 }
2601 else
2602 {
2603 if (image->colorspace != RGBColorspace)
2604 (void) TransformImageColorspace(image,RGBColorspace);
2605 photometric=PHOTOMETRIC_RGB;
2606 }
2607 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2608 if ((image_info->type != TrueColorType) &&
2609 (image_info->type != TrueColorMatteType))
2610 {
cristy94c8fe42009-10-06 01:57:36 +00002611 if ((image_info->type != PaletteType) &&
2612 (IsGrayImage(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002613 {
cristy94c8fe42009-10-06 01:57:36 +00002614 photometric=(uint16) (quantum_info->min_is_white !=
2615 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2616 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002617 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002618 if ((image_info->depth == 0) &&
2619 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2620 {
2621 status=SetQuantumDepth(image,quantum_info,1);
2622 if (status == MagickFalse)
2623 ThrowWriterException(ResourceLimitError,
2624 "MemoryAllocationFailed");
2625 }
cristy3ed852e2009-09-05 21:47:34 +00002626 }
2627 else
cristy94c8fe42009-10-06 01:57:36 +00002628 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002629 {
cristy94c8fe42009-10-06 01:57:36 +00002630 unsigned long
2631 depth;
2632
2633 /*
2634 Colormapped TIFF raster.
2635 */
cristy3ed852e2009-09-05 21:47:34 +00002636 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002637 photometric=PHOTOMETRIC_PALETTE;
2638 depth=1;
2639 while ((GetQuantumRange(depth)+1) < image->colors)
2640 depth<<=1;
2641 status=SetQuantumDepth(image,quantum_info,depth);
2642 if (status == MagickFalse)
2643 ThrowWriterException(ResourceLimitError,
2644 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002645 }
2646 }
2647 }
cristy0accf6e2009-10-01 13:14:28 +00002648 switch (image->endian)
2649 {
2650 case LSBEndian:
2651 {
2652 endian=FILLORDER_LSB2MSB;
2653 break;
2654 }
2655 case MSBEndian:
2656 {
2657 endian=FILLORDER_MSB2LSB;
2658 break;
2659 }
2660 case UndefinedEndian:
2661 default:
2662 {
2663 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2664 break;
2665 }
2666 }
2667 lsb_first=1;
2668 image->endian=MSBEndian;
2669 if ((int) (*(char *) &lsb_first) != 0)
2670 image->endian=LSBEndian;
2671 if ((compress_tag == COMPRESSION_JPEG) && (photometric != PHOTOMETRIC_RGB))
2672 compress_tag=COMPRESSION_NONE;
2673 else
2674 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2675 (photometric != PHOTOMETRIC_MINISWHITE))
2676 {
2677 compress_tag=COMPRESSION_NONE;
2678 endian=FILLORDER_MSB2LSB;
2679 }
2680 else
2681 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2682 (photometric != PHOTOMETRIC_MINISWHITE))
2683 {
2684 compress_tag=COMPRESSION_NONE;
2685 endian=FILLORDER_MSB2LSB;
2686 }
2687 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2688 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002689 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2690 if (image->matte != MagickFalse)
2691 {
2692 uint16
2693 extra_samples,
2694 sample_info[1],
2695 samples_per_pixel;
2696
2697 /*
2698 TIFF has a matte channel.
2699 */
2700 extra_samples=1;
2701 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2702 option=GetImageProperty(image,"tiff:alpha");
2703 if ((option != (const char *) NULL) &&
2704 (LocaleCompare(option,"associated") == 0))
2705 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2706 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2707 &samples_per_pixel);
2708 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2709 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2710 &sample_info);
2711 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2712 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2713 }
2714 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2715 switch (quantum_info->format)
2716 {
2717 case FloatingPointQuantumFormat:
2718 {
2719 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2720 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2721 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2722 break;
2723 }
2724 case SignedQuantumFormat:
2725 {
2726 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2727 break;
2728 }
2729 case UnsignedQuantumFormat:
2730 {
2731 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2732 break;
2733 }
2734 default:
2735 break;
2736 }
cristy3ed852e2009-09-05 21:47:34 +00002737 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2738 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2739 if (photometric == PHOTOMETRIC_RGB)
2740 if ((image_info->interlace == PlaneInterlace) ||
2741 (image_info->interlace == PartitionInterlace))
2742 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2743 rows_per_strip=1;
2744 if (TIFFScanlineSize(tiff) != 0)
2745 rows_per_strip=(unsigned long) MagickMax((size_t)
cristy5f0f48d2010-03-27 14:51:16 +00002746 TIFFDefaultStripSize(tiff,0),1);
cristy3ed852e2009-09-05 21:47:34 +00002747 option=GetImageOption(image_info,"tiff:rows-per-strip");
2748 if (option != (const char *) NULL)
2749 rows_per_strip=(unsigned long) strtol(option,(char **) NULL,10);
2750 switch (compress_tag)
2751 {
2752 case COMPRESSION_JPEG:
2753 {
2754#if defined(JPEG_SUPPORT)
2755 const char
2756 *sampling_factor;
2757
2758 GeometryInfo
2759 geometry_info;
2760
2761 MagickStatusType
2762 flags;
2763
2764 rows_per_strip+=(16-(rows_per_strip % 16));
2765 if (image->quality != 0)
2766 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2767 if (image_info->quality != UndefinedCompressionQuality)
2768 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2769 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2770 if (image->colorspace == RGBColorspace)
2771 {
2772 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2773 sampling_factor=(const char *) NULL;
2774 value=GetImageProperty(image,"jpeg:sampling-factor");
2775 if (value != (char *) NULL)
2776 {
2777 sampling_factor=value;
2778 if (image->debug != MagickFalse)
2779 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2780 " Input sampling-factors=%s",sampling_factor);
2781 }
2782 if (image_info->sampling_factor != (char *) NULL)
2783 sampling_factor=image_info->sampling_factor;
2784 if (sampling_factor != (const char *) NULL)
2785 {
2786 flags=ParseGeometry(sampling_factor,&geometry_info);
2787 if ((flags & SigmaValue) == 0)
2788 geometry_info.sigma=geometry_info.rho;
2789 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2790 geometry_info.rho,(uint16) geometry_info.sigma);
2791 }
2792 }
2793 if (bits_per_sample == 12)
2794 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2795#endif
2796 break;
2797 }
2798 case COMPRESSION_ADOBE_DEFLATE:
2799 {
2800 rows_per_strip=image->rows;
2801 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2802 &bits_per_sample);
2803 if (((photometric == PHOTOMETRIC_RGB) ||
2804 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2805 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2806 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
2807 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,image_info->quality ==
2808 UndefinedCompressionQuality ? 7 : MagickMin(1L*image_info->quality/10,
2809 9));
2810 break;
2811 }
2812 case COMPRESSION_CCITTFAX3:
2813 {
2814 /*
2815 Byte-aligned EOL.
2816 */
2817 rows_per_strip=image->rows;
2818 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2819 break;
2820 }
2821 case COMPRESSION_CCITTFAX4:
2822 {
2823 rows_per_strip=image->rows;
2824 break;
2825 }
2826 case COMPRESSION_LZW:
2827 {
2828 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2829 &bits_per_sample);
2830 if (((photometric == PHOTOMETRIC_RGB) ||
2831 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2832 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2833 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
2834 break;
2835 }
2836 default:
2837 break;
2838 }
2839 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
2840 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2841 {
2842 unsigned short
2843 units;
2844
2845 /*
2846 Set image resolution.
2847 */
2848 units=RESUNIT_NONE;
2849 if (image->units == PixelsPerInchResolution)
2850 units=RESUNIT_INCH;
2851 if (image->units == PixelsPerCentimeterResolution)
2852 units=RESUNIT_CENTIMETER;
2853 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2854 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2855 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2856 if ((image->page.x != 0) || (image->page.y != 0))
2857 {
2858 /*
2859 Set image position.
2860 */
2861 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2862 image->x_resolution);
2863 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2864 image->y_resolution);
2865 }
2866 }
2867 if (image->chromaticity.white_point.x != 0.0)
2868 {
2869 float
2870 chromaticity[6];
2871
2872 /*
2873 Set image chromaticity.
2874 */
2875 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2876 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2877 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2878 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2879 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2880 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2881 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2882 chromaticity[0]=(float) image->chromaticity.white_point.x;
2883 chromaticity[1]=(float) image->chromaticity.white_point.y;
2884 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2885 }
2886 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2887 {
2888 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2889 if (image->scene != 0)
2890 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
2891 GetImageListLength(image));
2892 }
2893 if (image->orientation != UndefinedOrientation)
2894 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
2895 (void) TIFFSetProfiles(tiff,image);
2896 {
2897 uint16
2898 page,
2899 pages;
2900
2901 page=(uint16) scene;
2902 pages=GetImageListLength(image);
2903 if ((image_info->adjoin != MagickFalse) && (pages > 1))
2904 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2905 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
2906 }
2907 (void) TIFFSetProperties(tiff,image);
2908 if (0)
2909 (void) TIFFSetEXIFProperties(tiff,image);
2910 /*
2911 Write image scanlines.
2912 */
2913 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
2914 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2915 pixels=GetQuantumPixels(quantum_info);
2916 tiff_info.scanline=GetQuantumPixels(quantum_info);
2917 switch (photometric)
2918 {
2919 case PHOTOMETRIC_CIELAB:
2920 case PHOTOMETRIC_YCBCR:
2921 case PHOTOMETRIC_RGB:
2922 {
2923 /*
2924 RGB TIFF image.
2925 */
2926 switch (image_info->interlace)
2927 {
2928 case NoInterlace:
2929 default:
2930 {
2931 quantum_type=RGBQuantum;
2932 if (image->matte != MagickFalse)
2933 quantum_type=RGBAQuantum;
2934 for (y=0; y < (long) image->rows; y++)
2935 {
2936 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002937 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002938
2939 p=GetVirtualPixels(image,0,y,image->columns,1,
2940 &image->exception);
2941 if (p == (const PixelPacket *) NULL)
2942 break;
2943 length=ExportQuantumPixels(image,(const CacheView *) NULL,
2944 quantum_info,quantum_type,pixels,&image->exception);
2945 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
2946 break;
2947 if (image->previous == (Image *) NULL)
2948 {
2949 status=SetImageProgress(image,SaveImageTag,y,image->rows);
2950 if (status == MagickFalse)
2951 break;
2952 }
2953 }
2954 break;
2955 }
2956 case PlaneInterlace:
2957 case PartitionInterlace:
2958 {
2959 /*
2960 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
2961 */
2962 for (y=0; y < (long) image->rows; y++)
2963 {
2964 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002965 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002966
2967 p=GetVirtualPixels(image,0,y,image->columns,1,
2968 &image->exception);
2969 if (p == (const PixelPacket *) NULL)
2970 break;
2971 length=ExportQuantumPixels(image,(const CacheView *) NULL,
2972 quantum_info,RedQuantum,pixels,&image->exception);
2973 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
2974 break;
2975 }
2976 if (image->previous == (Image *) NULL)
2977 {
2978 status=SetImageProgress(image,SaveImageTag,100,400);
2979 if (status == MagickFalse)
2980 break;
2981 }
2982 for (y=0; y < (long) image->rows; y++)
2983 {
2984 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002985 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002986
2987 p=GetVirtualPixels(image,0,y,image->columns,1,
2988 &image->exception);
2989 if (p == (const PixelPacket *) NULL)
2990 break;
2991 length=ExportQuantumPixels(image,(const CacheView *) NULL,
2992 quantum_info,GreenQuantum,pixels,&image->exception);
2993 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
2994 break;
2995 }
2996 if (image->previous == (Image *) NULL)
2997 {
2998 status=SetImageProgress(image,SaveImageTag,200,400);
2999 if (status == MagickFalse)
3000 break;
3001 }
3002 for (y=0; y < (long) image->rows; y++)
3003 {
3004 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003005 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003006
3007 p=GetVirtualPixels(image,0,y,image->columns,1,
3008 &image->exception);
3009 if (p == (const PixelPacket *) NULL)
3010 break;
3011 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3012 quantum_info,BlueQuantum,pixels,&image->exception);
3013 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3014 break;
3015 }
3016 if (image->previous == (Image *) NULL)
3017 {
3018 status=SetImageProgress(image,SaveImageTag,300,400);
3019 if (status == MagickFalse)
3020 break;
3021 }
3022 if (image->matte != MagickFalse)
3023 for (y=0; y < (long) image->rows; y++)
3024 {
3025 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003026 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003027
3028 p=GetVirtualPixels(image,0,y,image->columns,1,
3029 &image->exception);
3030 if (p == (const PixelPacket *) NULL)
3031 break;
3032 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3033 quantum_info,AlphaQuantum,pixels,&image->exception);
3034 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3035 break;
3036 }
3037 if (image->previous == (Image *) NULL)
3038 {
3039 status=SetImageProgress(image,SaveImageTag,400,400);
3040 if (status == MagickFalse)
3041 break;
3042 }
3043 break;
3044 }
3045 }
3046 break;
3047 }
3048 case PHOTOMETRIC_SEPARATED:
3049 {
3050 /*
3051 CMYK TIFF image.
3052 */
3053 quantum_type=CMYKQuantum;
3054 if (image->matte != MagickFalse)
3055 quantum_type=CMYKAQuantum;
3056 if (image->colorspace != CMYKColorspace)
3057 (void) TransformImageColorspace(image,CMYKColorspace);
3058 for (y=0; y < (long) image->rows; y++)
3059 {
3060 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003061 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003062
3063 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3064 if (p == (const PixelPacket *) NULL)
3065 break;
3066 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3067 quantum_info,quantum_type,pixels,&image->exception);
3068 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3069 break;
3070 if (image->previous == (Image *) NULL)
3071 {
3072 status=SetImageProgress(image,SaveImageTag,y,image->rows);
3073 if (status == MagickFalse)
3074 break;
3075 }
3076 }
3077 break;
3078 }
3079 case PHOTOMETRIC_PALETTE:
3080 {
3081 uint16
3082 *blue,
3083 *green,
3084 *red;
3085
3086 /*
3087 Colormapped TIFF image.
3088 */
3089 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3090 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3091 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3092 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3093 (blue == (uint16 *) NULL))
3094 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3095 /*
3096 Initialize TIFF colormap.
3097 */
3098 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3099 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3100 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3101 for (i=0; i < (long) image->colors; i++)
3102 {
3103 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3104 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3105 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3106 }
3107 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3108 red=(uint16 *) RelinquishMagickMemory(red);
3109 green=(uint16 *) RelinquishMagickMemory(green);
3110 blue=(uint16 *) RelinquishMagickMemory(blue);
3111 }
3112 default:
3113 {
3114 /*
3115 Convert PseudoClass packets to contiguous grayscale scanlines.
3116 */
3117 quantum_type=IndexQuantum;
3118 if (image->matte != MagickFalse)
3119 {
3120 if (photometric != PHOTOMETRIC_PALETTE)
3121 quantum_type=GrayAlphaQuantum;
3122 else
3123 quantum_type=IndexAlphaQuantum;
3124 }
3125 else
3126 if (photometric != PHOTOMETRIC_PALETTE)
3127 quantum_type=GrayQuantum;
3128 for (y=0; y < (long) image->rows; y++)
3129 {
3130 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003131 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003132
3133 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3134 if (p == (const PixelPacket *) NULL)
3135 break;
3136 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3137 quantum_info,quantum_type,pixels,&image->exception);
3138 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3139 break;
3140 if (image->previous == (Image *) NULL)
3141 {
3142 status=SetImageProgress(image,SaveImageTag,y,image->rows);
3143 if (status == MagickFalse)
3144 break;
3145 }
3146 }
3147 break;
3148 }
3149 }
3150 quantum_info=DestroyQuantumInfo(quantum_info);
3151 DestroyTIFFInfo(&tiff_info);
3152 if (0 && (image_info->verbose == MagickTrue))
3153 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3154 (void) TIFFWriteDirectory(tiff);
3155 image->endian=MSBEndian;
3156 if (endian == FILLORDER_LSB2MSB)
3157 image->endian=LSBEndian;
3158 image=SyncNextImageInList(image);
3159 if (image == (Image *) NULL)
3160 break;
3161 status=SetImageProgress(image,SaveImagesTag,scene++,
3162 GetImageListLength(image));
3163 if (status == MagickFalse)
3164 break;
3165 } while (image_info->adjoin != MagickFalse);
3166 (void) TIFFSetWarningHandler(warning_handler);
3167 (void) TIFFSetErrorHandler(error_handler);
3168 TIFFClose(tiff);
3169 return(MagickTrue);
3170}
3171#endif