blob: b3c92d3a004bca49f1723766b552ebfa1452b774 [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
cristy3ed852e2009-09-05 21:47:34 +0000751 debug,
752 status;
753
754 MagickSizeType
755 number_pixels;
756
757 QuantumInfo
758 *quantum_info;
759
760 QuantumType
761 quantum_type;
762
763 register long
764 i;
765
766 size_t
767 length,
768 pad;
769
770 TIFF
771 *tiff;
772
773 TIFFErrorHandler
774 error_handler,
775 warning_handler;
776
777 TIFFMethodType
778 method;
779
780 uint16
781 compress_tag,
782 bits_per_sample,
783 endian,
784 extra_samples,
785 interlace,
786 max_sample_value,
787 min_sample_value,
788 orientation,
789 pages,
790 photometric,
791 *sample_info,
792 sample_format,
793 samples_per_pixel,
794 units,
795 value;
796
797 uint32
798 height,
799 rows_per_strip,
800 width;
801
802 unsigned char
803 *pixels;
804
805 unsigned long
806 lsb_first;
807
808 /*
809 Open image.
810 */
811 assert(image_info != (const ImageInfo *) NULL);
812 assert(image_info->signature == MagickSignature);
813 if (image_info->debug != MagickFalse)
814 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
815 image_info->filename);
816 assert(exception != (ExceptionInfo *) NULL);
817 assert(exception->signature == MagickSignature);
818 image=AcquireImage(image_info);
819 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
820 if (status == MagickFalse)
821 {
822 image=DestroyImageList(image);
823 return((Image *) NULL);
824 }
825 (void) MagickSetThreadValue(tiff_exception,exception);
826 error_handler=TIFFSetErrorHandler(TIFFErrors);
827 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
828 tiff=TIFFClientOpen(image->filename,"r",(thandle_t) image,TIFFReadBlob,
829 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
830 TIFFUnmapBlob);
831 if (tiff == (TIFF *) NULL)
832 {
833 (void) TIFFSetWarningHandler(warning_handler);
834 (void) TIFFSetErrorHandler(error_handler);
835 image=DestroyImageList(image);
836 return((Image *) NULL);
837 }
838 debug=IsEventLogging();
839 if (image_info->number_scenes != 0)
840 {
841 /*
842 Generate blank images for subimage specification (e.g. image.tif[4].
843 */
844 for (i=0; i < (long) image_info->scene; i++)
845 {
846 (void) TIFFReadDirectory(tiff);
847 AcquireNextImage(image_info,image);
848 if (GetNextImageInList(image) == (Image *) NULL)
849 {
850 image=DestroyImageList(image);
851 return((Image *) NULL);
852 }
853 image=SyncNextImageInList(image);
854 }
855 }
856 do
857 {
858 if (0 && (image_info->verbose != MagickFalse))
859 TIFFPrintDirectory(tiff,stdout,MagickFalse);
860 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
861 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
862 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
863 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
864 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
865 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
866 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
867 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
871 switch (photometric)
872 {
873 case PHOTOMETRIC_MINISBLACK:
874 {
875 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
876 break;
877 }
878 case PHOTOMETRIC_MINISWHITE:
879 {
880 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
881 break;
882 }
883 case PHOTOMETRIC_PALETTE:
884 {
885 (void) SetImageProperty(image,"tiff:photometric","palette");
886 break;
887 }
888 case PHOTOMETRIC_RGB:
889 {
890 (void) SetImageProperty(image,"tiff:photometric","RGB");
891 break;
892 }
893 case PHOTOMETRIC_CIELAB:
894 {
895 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
896 break;
897 }
898 case PHOTOMETRIC_SEPARATED:
899 {
900 (void) SetImageProperty(image,"tiff:photometric","separated");
901 break;
902 }
903 default:
904 {
905 (void) SetImageProperty(image,"tiff:photometric","unknown");
906 break;
907 }
908 }
909 if (image->debug != MagickFalse)
910 {
911 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
912 (unsigned int) width,(unsigned int) height);
913 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
914 interlace);
915 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
916 "Bits per sample: %u",bits_per_sample);
917 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
918 "Min sample value: %u",min_sample_value);
919 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
920 "Max sample value: %u",max_sample_value);
921 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
922 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
923 }
924 image->columns=(unsigned long) width;
925 image->rows=(unsigned long) height;
926 image->depth=(unsigned long) bits_per_sample;
927 if (image->debug != MagickFalse)
928 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %lu",
929 image->depth);
930 lsb_first=1;
931 image->endian=MSBEndian;
932 if ((int) (*(char *) &lsb_first) != 0)
933 image->endian=LSBEndian;
934 if (photometric == PHOTOMETRIC_SEPARATED)
935 image->colorspace=CMYKColorspace;
936 if (photometric == PHOTOMETRIC_CIELAB)
937 image->colorspace=LabColorspace;
938 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
939 &samples_per_pixel);
940 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
941 x_resolution=(float) image->x_resolution;
942 y_resolution=(float) image->y_resolution;
943 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
944 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
945 image->x_resolution=x_resolution;
946 image->y_resolution=y_resolution;
947 x_position=(float) image->page.x/x_resolution;
948 y_position=(float) image->page.y/y_resolution;
949 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
950 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
951 image->page.x=(long) (x_position*x_resolution+0.5);
952 image->page.y=(long) (y_position*y_resolution+0.5);
953 image->orientation=(OrientationType) orientation;
954 chromaticity=(float *) NULL;
955 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
956 if (chromaticity != (float *) NULL)
957 {
958 image->chromaticity.white_point.x=chromaticity[0];
959 image->chromaticity.white_point.y=chromaticity[1];
960 }
961 chromaticity=(float *) NULL;
962 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
963 if (chromaticity != (float *) NULL)
964 {
965 image->chromaticity.red_primary.x=chromaticity[0];
966 image->chromaticity.red_primary.y=chromaticity[1];
967 image->chromaticity.green_primary.x=chromaticity[2];
968 image->chromaticity.green_primary.y=chromaticity[3];
969 image->chromaticity.blue_primary.x=chromaticity[4];
970 image->chromaticity.blue_primary.y=chromaticity[5];
971 }
972 TIFFGetProperties(tiff,image);
973 option=GetImageOption(image_info,"tiff:exif");
974 if ((option != (const char *) NULL) &&
975 (IsMagickTrue(option) != MagickFalse))
976 TIFFGetEXIFProperties(tiff,image);
977 TIFFGetProfiles(tiff,image);
978 /*
979 Allocate memory for the image and pixel buffer.
980 */
981#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
982 if ((compress_tag != COMPRESSION_NONE) &&
983 (TIFFIsCODECConfigured(compress_tag) == 0))
984 {
985 TIFFClose(tiff);
986 ThrowReaderException(CoderError,"CompressNotSupported");
987 }
988#endif
989 switch (compress_tag)
990 {
991 case COMPRESSION_NONE: image->compression=NoCompression; break;
992 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
993 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
994 case COMPRESSION_JPEG:
995 {
996 image->compression=JPEGCompression;
997#if defined(JPEG_SUPPORT)
998 {
999 char
1000 sampling_factor[MaxTextExtent];
1001
1002 uint16
1003 horizontal,
1004 vertical;
1005
1006 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1007 &horizontal,&vertical);
1008 (void) FormatMagickString(sampling_factor,MaxTextExtent,"%dx%d",
1009 horizontal,vertical);
1010 (void) SetImageProperty(image,"jpeg:sampling-factor",
1011 sampling_factor);
1012 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1013 "Sampling Factors: %s",sampling_factor);
1014 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1015 {
1016 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1017 JPEGCOLORMODE_RGB);
1018 photometric=PHOTOMETRIC_RGB;
1019 }
1020 }
1021#endif
1022 break;
1023 }
1024 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1025 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1026 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1027 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1028 default: image->compression=RLECompression; break;
1029 }
1030 quantum_info=AcquireQuantumInfo(image_info,image);
1031 if (quantum_info == (QuantumInfo *) NULL)
1032 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1033 if (sample_format == SAMPLEFORMAT_UINT)
1034 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1035 if (sample_format == SAMPLEFORMAT_INT)
1036 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1037 if (sample_format == SAMPLEFORMAT_IEEEFP)
1038 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1039 if (status == MagickFalse)
1040 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1041 status=MagickTrue;
1042 switch (photometric)
1043 {
1044 case PHOTOMETRIC_MINISBLACK:
1045 {
1046 quantum_info->min_is_white=MagickFalse;
1047 break;
1048 }
1049 case PHOTOMETRIC_MINISWHITE:
1050 {
1051 quantum_info->min_is_white=MagickTrue;
1052 break;
1053 }
1054 default:
1055 break;
1056 }
cristy3ed852e2009-09-05 21:47:34 +00001057 extra_samples=0;
1058 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1059 &sample_info);
1060 if (extra_samples == 0)
1061 {
1062 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1063 image->matte=MagickTrue;
1064 }
1065 else
cristy6c207342010-02-19 17:49:50 +00001066 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001067 {
cristy7cf69862010-03-03 13:57:24 +00001068 image->matte=MagickTrue;
1069 image->alpha_channel=UndefinedAlphaChannel;
1070 (void) SetImageProperty(image,"tiff:alpha","undefined");
cristy6c207342010-02-19 17:49:50 +00001071 if (sample_info[i] == EXTRASAMPLE_UNASSALPHA)
cristy3ed852e2009-09-05 21:47:34 +00001072 {
cristy7cf69862010-03-03 13:57:24 +00001073 image->alpha_channel=UnassociatedAlphaChannel;
1074 (void) SetImageProperty(image,"tiff:alpha","unassociated");
cristy3ed852e2009-09-05 21:47:34 +00001075 }
cristy6c207342010-02-19 17:49:50 +00001076 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy3ed852e2009-09-05 21:47:34 +00001077 {
cristy7cf69862010-03-03 13:57:24 +00001078 image->alpha_channel=AssociatedAlphaChannel;
cristy3ed852e2009-09-05 21:47:34 +00001079 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy7cf69862010-03-03 13:57:24 +00001080 (void) SetImageProperty(image,"tiff:alpha","associated");
cristy3ed852e2009-09-05 21:47:34 +00001081 }
1082 }
cristy3ed852e2009-09-05 21:47:34 +00001083 if ((photometric == PHOTOMETRIC_PALETTE) &&
1084 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1085 {
1086 unsigned long
1087 colors;
1088
1089 colors=(unsigned long) GetQuantumRange(bits_per_sample)+1;
1090 if (AcquireImageColormap(image,colors) == MagickFalse)
1091 {
1092 TIFFClose(tiff);
1093 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1094 }
1095 }
1096 if (units == RESUNIT_INCH)
1097 image->units=PixelsPerInchResolution;
1098 if (units == RESUNIT_CENTIMETER)
1099 image->units=PixelsPerCentimeterResolution;
1100 value=(unsigned short) image->scene;
1101 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1102 image->scene=value;
1103 if (image_info->ping != MagickFalse)
1104 {
1105 if (image_info->number_scenes != 0)
1106 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1107 break;
1108 goto next_tiff_frame;
1109 }
1110 method=ReadGenericMethod;
1111 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1112 {
1113 char
1114 value[MaxTextExtent];
1115
1116 method=ReadStripMethod;
1117 (void) FormatMagickString(value,MaxTextExtent,"%u",
1118 (unsigned int) rows_per_strip);
1119 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1120 }
1121 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1122 method=ReadRGBAMethod;
1123 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1124 method=ReadCMYKAMethod;
1125 if ((photometric != PHOTOMETRIC_RGB) &&
1126 (photometric != PHOTOMETRIC_CIELAB) &&
1127 (photometric != PHOTOMETRIC_SEPARATED))
1128 method=ReadGenericMethod;
1129 if (image->storage_class == PseudoClass)
1130 method=ReadSingleSampleMethod;
1131 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1132 (photometric == PHOTOMETRIC_MINISWHITE))
1133 method=ReadSingleSampleMethod;
1134 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1135 (interlace == PLANARCONFIG_SEPARATE))
1136 method=ReadGenericMethod;
1137 if (TIFFIsTiled(tiff) != MagickFalse)
1138 method=ReadTileMethod;
1139 quantum_type=RGBQuantum;
1140 pixels=GetQuantumPixels(quantum_info);
1141 switch (method)
1142 {
1143 case ReadSingleSampleMethod:
1144 {
1145 /*
1146 Convert TIFF image to PseudoClass MIFF image.
1147 */
1148 if ((image->storage_class == PseudoClass) &&
1149 (photometric == PHOTOMETRIC_PALETTE))
1150 {
1151 unsigned long
1152 range;
1153
1154 uint16
1155 *blue_colormap,
1156 *green_colormap,
1157 *red_colormap;
1158
1159 /*
1160 Initialize colormap.
1161 */
1162 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1163 &green_colormap,&blue_colormap);
1164 range=255; /* might be old style 8-bit colormap */
1165 for (i=0; i < (long) image->colors; i++)
1166 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1167 (blue_colormap[i] >= 256))
1168 {
1169 range=65535;
1170 break;
1171 }
1172 for (i=0; i < (long) image->colors; i++)
1173 {
cristyce70c172010-01-07 17:15:30 +00001174 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001175 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001176 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001177 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001178 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001179 blue_colormap[i])/range);
1180 }
1181 }
1182 quantum_type=IndexQuantum;
1183 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1184 if (image->matte != MagickFalse)
1185 {
1186 if (image->storage_class != PseudoClass)
1187 {
1188 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1189 GrayAlphaQuantum;
1190 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1191 }
1192 else
1193 {
1194 quantum_type=IndexAlphaQuantum;
1195 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1196 }
1197 }
1198 else
1199 if (image->storage_class != PseudoClass)
1200 {
1201 quantum_type=GrayQuantum;
1202 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1203 }
1204 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1205 if (status == MagickFalse)
1206 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1207 pixels=GetQuantumPixels(quantum_info);
1208 for (y=0; y < (long) image->rows; y++)
1209 {
1210 int
1211 status;
1212
1213 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001214 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001215
1216 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1217 if (status == -1)
1218 break;
1219 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1220 if (q == (PixelPacket *) NULL)
1221 break;
1222 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1223 quantum_type,pixels,exception);
1224 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1225 break;
1226 if (image->previous == (Image *) NULL)
1227 {
1228 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1229 if (status == MagickFalse)
1230 break;
1231 }
1232 }
1233 break;
1234 }
1235 case ReadRGBAMethod:
1236 {
1237 /*
1238 Convert TIFF image to DirectClass MIFF image.
1239 */
1240 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1241 quantum_type=RGBQuantum;
1242 if (image->matte != MagickFalse)
1243 {
1244 quantum_type=RGBAQuantum;
1245 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1246 }
1247 if (image->colorspace == CMYKColorspace)
1248 {
1249 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1250 quantum_type=CMYKQuantum;
1251 if (image->matte != MagickFalse)
1252 {
1253 quantum_type=CMYKAQuantum;
1254 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1255 }
1256 }
1257 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1258 if (status == MagickFalse)
1259 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1260 pixels=GetQuantumPixels(quantum_info);
1261 for (y=0; y < (long) image->rows; y++)
1262 {
1263 int
1264 status;
1265
1266 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001267 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001268
1269 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1270 if (status == -1)
1271 break;
1272 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1273 if (q == (PixelPacket *) NULL)
1274 break;
1275 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1276 quantum_type,pixels,exception);
1277 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1278 break;
1279 if (image->previous == (Image *) NULL)
1280 {
1281 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1282 if (status == MagickFalse)
1283 break;
1284 }
1285 }
1286 break;
1287 }
1288 case ReadCMYKAMethod:
1289 {
1290 /*
1291 Convert TIFF image to DirectClass MIFF image.
1292 */
cristy6d9f12f2009-11-03 14:50:26 +00001293 for (i=0; i < (long) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001294 {
cristy6d9f12f2009-11-03 14:50:26 +00001295 for (y=0; y < (long) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001296 {
cristy6d9f12f2009-11-03 14:50:26 +00001297 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001298 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001299
cristy3ed852e2009-09-05 21:47:34 +00001300 int
1301 status;
1302
1303 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1304 pixels);
1305 if (status == -1)
1306 break;
1307 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1308 if (q == (PixelPacket *) NULL)
1309 break;
1310 if (image->colorspace != CMYKColorspace)
1311 switch (i)
1312 {
1313 case 0: quantum_type=RedQuantum; break;
1314 case 1: quantum_type=GreenQuantum; break;
1315 case 2: quantum_type=BlueQuantum; break;
1316 case 3: quantum_type=AlphaQuantum; break;
1317 default: quantum_type=UndefinedQuantum; break;
1318 }
cristy6d9f12f2009-11-03 14:50:26 +00001319 else
cristy3ed852e2009-09-05 21:47:34 +00001320 switch (i)
1321 {
1322 case 0: quantum_type=CyanQuantum; break;
1323 case 1: quantum_type=MagentaQuantum; break;
1324 case 2: quantum_type=YellowQuantum; break;
1325 case 3: quantum_type=BlackQuantum; break;
1326 case 4: quantum_type=AlphaQuantum; break;
1327 default: quantum_type=UndefinedQuantum; break;
1328 }
1329 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1330 quantum_type,pixels,exception);
1331 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1332 break;
1333 }
1334 if (image->previous == (Image *) NULL)
1335 {
1336 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1337 if (status == MagickFalse)
1338 break;
1339 }
1340 }
1341 break;
1342 }
1343 case ReadStripMethod:
1344 {
1345 register unsigned char
1346 *p;
1347
1348 /*
1349 Convert stripped TIFF image to DirectClass MIFF image.
1350 */
1351 i=0;
1352 p=(unsigned char *) NULL;
1353 for (y=0; y < (long) image->rows; y++)
1354 {
1355 register long
1356 x;
1357
1358 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001359 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001360
1361 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1362 if (q == (PixelPacket *) NULL)
1363 break;
1364 if (i == 0)
1365 {
1366 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1367 break;
1368 i=(long) MagickMin((long) rows_per_strip,(long) image->rows-y);
1369 }
1370 i--;
1371 p=pixels+image->columns*i;
1372 for (x=0; x < (long) image->columns; x++)
1373 {
1374 q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
1375 q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
1376 q->blue=ScaleCharToQuantum((unsigned char) (TIFFGetB(*p)));
1377 if (image->matte != MagickFalse)
1378 q->opacity=ScaleCharToQuantum((unsigned char) (TIFFGetA(*p)));
1379 p++;
1380 q++;
1381 }
1382 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1383 break;
1384 if (image->previous == (Image *) NULL)
1385 {
1386 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1387 if (status == MagickFalse)
1388 break;
1389 }
1390 }
1391 break;
1392 }
1393 case ReadTileMethod:
1394 {
1395 register uint32
1396 *p;
1397
1398 uint32
1399 *tile_pixels,
1400 columns,
1401 rows;
1402
1403 unsigned long
1404 number_pixels;
1405
1406 /*
1407 Convert tiled TIFF image to DirectClass MIFF image.
1408 */
1409 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1410 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1411 {
1412 TIFFClose(tiff);
1413 ThrowReaderException(CoderError,"ImageIsNotTiled");
1414 }
1415 (void) SetImageStorageClass(image,DirectClass);
1416 number_pixels=columns*rows;
1417 tile_pixels=(uint32 *) AcquireQuantumMemory((size_t) columns*rows,
1418 sizeof(*tile_pixels));
1419 if (tile_pixels == (uint32 *) NULL)
1420 {
1421 TIFFClose(tiff);
1422 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1423 }
1424 for (y=0; y < (long) image->rows; y+=rows)
1425 {
1426 PixelPacket
1427 *tile;
1428
1429 register long
1430 x;
1431
1432 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001433 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001434
1435 unsigned long
1436 columns_remaining,
1437 rows_remaining;
1438
1439 rows_remaining=image->rows-y;
1440 if ((long) (y+rows) < (long) image->rows)
1441 rows_remaining=rows;
1442 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1443 exception);
1444 if (tile == (PixelPacket *) NULL)
1445 break;
1446 for (x=0; x < (long) image->columns; x+=columns)
1447 {
1448 unsigned long
1449 column,
1450 row;
1451
1452 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1453 break;
1454 columns_remaining=image->columns-x;
1455 if ((long) (x+columns) < (long) image->columns)
1456 columns_remaining=columns;
1457 p=tile_pixels+(rows-rows_remaining)*columns;
1458 q=tile+(image->columns*(rows_remaining-1)+x);
1459 for (row=rows_remaining; row > 0; row--)
1460 {
1461 if (image->matte != MagickFalse)
1462 for (column=columns_remaining; column > 0; column--)
1463 {
1464 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1465 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1466 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1467 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1468 (unsigned char) TIFFGetA(*p)));
1469 q++;
1470 p++;
1471 }
1472 else
1473 for (column=columns_remaining; column > 0; column--)
1474 {
1475 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1476 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1477 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1478 q++;
1479 p++;
1480 }
1481 p+=columns-columns_remaining;
1482 q-=(image->columns+columns_remaining);
1483 }
1484 }
1485 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1486 break;
1487 if (image->previous == (Image *) NULL)
1488 {
1489 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1490 if (status == MagickFalse)
1491 break;
1492 }
1493 }
1494 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1495 break;
1496 }
1497 case ReadGenericMethod:
1498 default:
1499 {
1500 register uint32
1501 *p;
1502
1503 uint32
1504 *pixels;
1505
1506 /*
1507 Convert TIFF image to DirectClass MIFF image.
1508 */
1509 number_pixels=(MagickSizeType) image->columns*image->rows;
1510 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1511 (number_pixels*sizeof(uint32))))
1512 {
1513 TIFFClose(tiff);
1514 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1515 }
1516 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1517 sizeof(uint32));
1518 if (pixels == (uint32 *) NULL)
1519 {
1520 TIFFClose(tiff);
1521 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1522 }
1523 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1524 (uint32) image->rows,(uint32 *) pixels,0);
1525 /*
1526 Convert image to DirectClass pixel packets.
1527 */
1528 p=pixels+number_pixels-1;
1529 for (y=0; y < (long) image->rows; y++)
1530 {
1531 register long
1532 x;
1533
1534 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001535 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001536
1537 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1538 if (q == (PixelPacket *) NULL)
1539 break;
1540 q+=image->columns-1;
1541 for (x=0; x < (long) image->columns; x++)
1542 {
1543 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1544 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1545 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1546 if (image->matte != MagickFalse)
1547 q->opacity=ScaleCharToQuantum((unsigned char) TIFFGetA(*p));
1548 p--;
1549 q--;
1550 }
1551 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1552 break;
1553 if (image->previous == (Image *) NULL)
1554 {
1555 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1556 if (status == MagickFalse)
1557 break;
1558 }
1559 }
1560 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1561 break;
1562 }
1563 }
1564 SetQuantumImageType(image,quantum_type);
1565 next_tiff_frame:
1566 if ((photometric == PHOTOMETRIC_LOGL) ||
1567 (photometric == PHOTOMETRIC_MINISBLACK) ||
1568 (photometric == PHOTOMETRIC_MINISWHITE))
1569 {
1570 image->type=GrayscaleType;
1571 if (bits_per_sample == 1)
1572 image->type=BilevelType;
1573 }
1574 if (image->storage_class == PseudoClass)
1575 image->depth=GetImageDepth(image,exception);
1576 image->endian=MSBEndian;
1577 if (endian == FILLORDER_LSB2MSB)
1578 image->endian=LSBEndian;
1579 if ((photometric == PHOTOMETRIC_LOGL) ||
1580 (photometric == PHOTOMETRIC_MINISBLACK) ||
1581 (photometric == PHOTOMETRIC_MINISWHITE))
1582 {
1583 image->type=GrayscaleType;
1584 if (bits_per_sample == 1)
1585 image->type=BilevelType;
1586 }
1587 /*
1588 Proceed to next image.
1589 */
1590 if (image_info->number_scenes != 0)
1591 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1592 break;
1593 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1594 if (status == MagickTrue)
1595 {
1596 /*
1597 Allocate next image structure.
1598 */
1599 AcquireNextImage(image_info,image);
1600 if (GetNextImageInList(image) == (Image *) NULL)
1601 {
1602 image=DestroyImageList(image);
1603 return((Image *) NULL);
1604 }
1605 image=SyncNextImageInList(image);
1606 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1607 image->scene);
1608 if (status == MagickFalse)
1609 break;
1610 }
1611 quantum_info=DestroyQuantumInfo(quantum_info);
1612 } while (status == MagickTrue);
1613 (void) TIFFSetWarningHandler(warning_handler);
1614 (void) TIFFSetErrorHandler(error_handler);
1615 TIFFClose(tiff);
1616 return(GetFirstImageInList(image));
1617}
1618#endif
1619
1620/*
1621%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1622% %
1623% %
1624% %
1625% R e g i s t e r T I F F I m a g e %
1626% %
1627% %
1628% %
1629%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1630%
1631% RegisterTIFFImage() adds properties for the TIFF image format to
1632% the list of supported formats. The properties include the image format
1633% tag, a method to read and/or write the format, whether the format
1634% supports the saving of more than one frame to the same file or blob,
1635% whether the format supports native in-memory I/O, and a brief
1636% description of the format.
1637%
1638% The format of the RegisterTIFFImage method is:
1639%
1640% unsigned long RegisterTIFFImage(void)
1641%
1642*/
1643ModuleExport unsigned long RegisterTIFFImage(void)
1644{
1645#define TIFFDescription "Tagged Image File Format"
1646
1647 char
1648 version[MaxTextExtent];
1649
1650 MagickInfo
1651 *entry;
cristy7cf69862010-03-03 13:57:24 +00001652
cristy18b17442009-10-25 18:36:48 +00001653 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1654 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001655 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001656 if (instantiate_key == MagickFalse)
1657 {
1658 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1659 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1660 instantiate_key=MagickTrue;
1661 }
cristyf84a1932010-01-03 18:00:18 +00001662 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001663 *version='\0';
1664#if defined(TIFF_VERSION)
1665 (void) FormatMagickString(version,MaxTextExtent,"%d",TIFF_VERSION);
1666#endif
1667#if defined(MAGICKCORE_TIFF_DELEGATE)
1668 {
1669 const char
1670 *p;
1671
1672 register long int
1673 i;
1674
1675 p=TIFFGetVersion();
1676 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1677 version[i]=(*p++);
1678 version[i]='\0';
1679 }
1680#endif
1681
cristy3d7f8062009-09-24 20:45:35 +00001682 entry=SetMagickInfo("GROUP4");
1683#if defined(MAGICKCORE_TIFF_DELEGATE)
1684 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1685 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1686#endif
1687 entry->raw=MagickTrue;
1688 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001689 entry->adjoin=MagickFalse;
cristy3d7f8062009-09-24 20:45:35 +00001690 entry->seekable_stream=MagickTrue;
1691 entry->thread_support=NoThreadSupport;
1692 entry->description=ConstantString("Raw CCITT Group4");
1693 entry->module=ConstantString("TIFF");
1694 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001695 entry=SetMagickInfo("PTIF");
1696#if defined(MAGICKCORE_TIFF_DELEGATE)
1697 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1698 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1699#endif
1700 entry->endian_support=MagickTrue;
1701 entry->seekable_stream=MagickTrue;
1702 entry->thread_support=NoThreadSupport;
1703 entry->description=ConstantString("Pyramid encoded TIFF");
1704 entry->module=ConstantString("TIFF");
1705 (void) RegisterMagickInfo(entry);
1706 entry=SetMagickInfo("TIF");
1707#if defined(MAGICKCORE_TIFF_DELEGATE)
1708 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1709 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1710#endif
1711 entry->endian_support=MagickTrue;
1712 entry->seekable_stream=MagickTrue;
1713 entry->stealth=MagickTrue;
1714 entry->thread_support=NoThreadSupport;
1715 entry->description=ConstantString(TIFFDescription);
1716 if (*version != '\0')
1717 entry->version=ConstantString(version);
1718 entry->module=ConstantString("TIFF");
1719 (void) RegisterMagickInfo(entry);
1720 entry=SetMagickInfo("TIFF");
1721#if defined(MAGICKCORE_TIFF_DELEGATE)
1722 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1723 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1724#endif
1725 entry->magick=(IsImageFormatHandler *) IsTIFF;
1726 entry->endian_support=MagickTrue;
1727 entry->seekable_stream=MagickTrue;
1728 entry->thread_support=NoThreadSupport;
1729 entry->description=ConstantString(TIFFDescription);
1730 if (*version != '\0')
1731 entry->version=ConstantString(version);
1732 entry->module=ConstantString("TIFF");
1733 (void) RegisterMagickInfo(entry);
1734 entry=SetMagickInfo("TIFF64");
1735#if defined(TIFF_VERSION_BIG)
1736 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1737 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1738#endif
1739 entry->adjoin=MagickFalse;
1740 entry->endian_support=MagickTrue;
1741 entry->seekable_stream=MagickTrue;
1742 entry->thread_support=NoThreadSupport;
1743 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1744 if (*version != '\0')
1745 entry->version=ConstantString(version);
1746 entry->module=ConstantString("TIFF");
1747 (void) RegisterMagickInfo(entry);
1748 return(MagickImageCoderSignature);
1749}
1750
1751/*
1752%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1753% %
1754% %
1755% %
1756% U n r e g i s t e r T I F F I m a g e %
1757% %
1758% %
1759% %
1760%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1761%
1762% UnregisterTIFFImage() removes format registrations made by the TIFF module
1763% from the list of supported formats.
1764%
1765% The format of the UnregisterTIFFImage method is:
1766%
1767% UnregisterTIFFImage(void)
1768%
1769*/
1770ModuleExport void UnregisterTIFFImage(void)
1771{
cristy3d7f8062009-09-24 20:45:35 +00001772 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001773 (void) UnregisterMagickInfo("PTIF");
1774 (void) UnregisterMagickInfo("TIF");
1775 (void) UnregisterMagickInfo("TIFF");
1776 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001777 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1778 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001779 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001780 if (instantiate_key != MagickFalse)
1781 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1782 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1783 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001784 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001785 DestroySemaphoreInfo(&tiff_semaphore);
1786}
1787
1788#if defined(MAGICKCORE_TIFF_DELEGATE)
1789/*
1790%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1791% %
1792% %
1793% %
cristy3d7f8062009-09-24 20:45:35 +00001794% W r i t e G R O U P 4 I m a g e %
1795% %
1796% %
1797% %
1798%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1799%
1800% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1801%
1802% The format of the WriteGROUP4Image method is:
1803%
1804% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1805% Image *image)
1806%
1807% A description of each parameter follows:
1808%
1809% o image_info: the image info.
1810%
1811% o image: The image.
1812%
1813*/
1814static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1815 Image *image)
1816{
1817 char
1818 filename[MaxTextExtent];
1819
1820 FILE
1821 *file;
1822
1823 Image
1824 *huffman_image;
1825
1826 ImageInfo
1827 *write_info;
1828
1829 int
1830 unique_file;
1831
1832 MagickBooleanType
1833 status;
1834
1835 register long
1836 i;
1837
1838 ssize_t
1839 count;
1840
1841 TIFF
1842 *tiff;
1843
cristy94c8fe42009-10-06 01:57:36 +00001844 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001845 *byte_count,
1846 strip_size;
1847
1848 unsigned char
1849 *buffer;
1850
1851 /*
1852 Write image as CCITT Group4 TIFF image to a temporary file.
1853 */
1854 assert(image_info != (const ImageInfo *) NULL);
1855 assert(image_info->signature == MagickSignature);
1856 assert(image != (Image *) NULL);
1857 assert(image->signature == MagickSignature);
1858 if (image->debug != MagickFalse)
1859 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1860 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1861 if (status == MagickFalse)
1862 return(status);
1863 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1864 if (huffman_image == (Image *) NULL)
1865 {
1866 (void) CloseBlob(image);
1867 return(MagickFalse);
1868 }
cristy94c8fe42009-10-06 01:57:36 +00001869 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001870 file=(FILE *) NULL;
1871 unique_file=AcquireUniqueFileResource(filename);
1872 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001873 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001874 if ((unique_file == -1) || (file == (FILE *) NULL))
1875 {
1876 ThrowFileException(&image->exception,FileOpenError,
1877 "UnableToCreateTemporaryFile",filename);
1878 return(MagickFalse);
1879 }
1880 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1881 filename);
1882 (void) SetImageType(huffman_image,BilevelType);
cristy14efd992009-09-26 23:46:03 +00001883 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001884 SetImageInfoFile(write_info,file);
1885 write_info->compression=Group4Compression;
1886 write_info->type=BilevelType;
1887 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1888 status=WriteTIFFImage(write_info,huffman_image);
1889 (void) fflush(file);
1890 write_info=DestroyImageInfo(write_info);
1891 if (status == MagickFalse)
1892 {
1893 InheritException(&image->exception,&huffman_image->exception);
1894 huffman_image=DestroyImage(huffman_image);
1895 (void) fclose(file);
1896 (void) RelinquishUniqueFileResource(filename);
1897 return(MagickFalse);
1898 }
1899 tiff=TIFFOpen(filename,"rb");
1900 if (tiff == (TIFF *) NULL)
1901 {
1902 huffman_image=DestroyImage(huffman_image);
1903 (void) fclose(file);
1904 (void) RelinquishUniqueFileResource(filename);
1905 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1906 image_info->filename);
1907 return(MagickFalse);
1908 }
1909 /*
1910 Allocate raw strip buffer.
1911 */
cristy94c8fe42009-10-06 01:57:36 +00001912 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1913 {
1914 TIFFClose(tiff);
1915 huffman_image=DestroyImage(huffman_image);
1916 (void) fclose(file);
1917 (void) RelinquishUniqueFileResource(filename);
1918 return(MagickFalse);
1919 }
cristy3d7f8062009-09-24 20:45:35 +00001920 strip_size=byte_count[0];
1921 for (i=1; i < (long) TIFFNumberOfStrips(tiff); i++)
1922 if (byte_count[i] > strip_size)
1923 strip_size=byte_count[i];
1924 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1925 sizeof(*buffer));
1926 if (buffer == (unsigned char *) NULL)
1927 {
1928 TIFFClose(tiff);
1929 huffman_image=DestroyImage(huffman_image);
1930 (void) fclose(file);
1931 (void) RelinquishUniqueFileResource(filename);
1932 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1933 image_info->filename);
1934 }
1935 /*
1936 Compress runlength encoded to 2D Huffman pixels.
1937 */
cristy3d7f8062009-09-24 20:45:35 +00001938 for (i=0; i < (long) TIFFNumberOfStrips(tiff); i++)
1939 {
cristy94c8fe42009-10-06 01:57:36 +00001940 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00001941 if (WriteBlob(image,(size_t) count,buffer) != count)
1942 status=MagickFalse;
1943 }
1944 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1945 TIFFClose(tiff);
1946 huffman_image=DestroyImage(huffman_image);
1947 (void) fclose(file);
1948 (void) RelinquishUniqueFileResource(filename);
1949 (void) CloseBlob(image);
1950 return(status);
1951}
1952#endif
1953
1954#if defined(MAGICKCORE_TIFF_DELEGATE)
1955/*
1956%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1957% %
1958% %
1959% %
cristy3ed852e2009-09-05 21:47:34 +00001960% W r i t e P T I F I m a g e %
1961% %
1962% %
1963% %
1964%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1965%
1966% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
1967% format.
1968%
1969% The format of the WritePTIFImage method is:
1970%
1971% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
1972% Image *image)
1973%
1974% A description of each parameter follows:
1975%
1976% o image_info: the image info.
1977%
1978% o image: The image.
1979%
1980*/
1981static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
1982 Image *image)
1983{
1984 Image
1985 *images,
1986 *next,
1987 *pyramid_image;
1988
1989 ImageInfo
1990 *write_info;
1991
1992 MagickBooleanType
1993 status;
1994
1995 unsigned long
1996 columns,
1997 rows;
1998
1999 /*
2000 Create pyramid-encoded TIFF image.
2001 */
2002 images=NewImageList();
2003 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2004 {
2005 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2006 &image->exception));
2007 columns=next->columns;
2008 rows=next->rows;
2009 while ((columns > 64) && (rows > 64))
2010 {
2011 columns/=2;
2012 rows/=2;
2013 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2014 &image->exception);
2015 AppendImageToList(&images,pyramid_image);
2016 }
2017 }
2018 /*
2019 Write pyramid-encoded TIFF image.
2020 */
cristy58567532009-09-26 01:13:32 +00002021 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002022 *write_info->magick='\0';
2023 write_info->adjoin=MagickTrue;
2024 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2025 images=DestroyImageList(images);
2026 write_info=DestroyImageInfo(write_info);
2027 return(status);
2028}
2029#endif
2030
2031#if defined(MAGICKCORE_TIFF_DELEGATE)
2032/*
2033%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2034% %
2035% %
2036% W r i t e T I F F I m a g e %
2037% %
2038% %
2039% %
2040%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2041%
2042% WriteTIFFImage() writes an image in the Tagged image file format.
2043%
2044% The format of the WriteTIFFImage method is:
2045%
2046% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2047% Image *image)
2048%
2049% A description of each parameter follows:
2050%
2051% o image_info: the image info.
2052%
2053% o image: The image.
2054%
2055*/
2056
2057typedef struct _TIFFInfo
2058{
2059 RectangleInfo
2060 tile_geometry;
2061
2062 unsigned char
2063 *scanline,
2064 *scanlines,
2065 *pixels;
2066} TIFFInfo;
2067
2068static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2069{
2070 assert(tiff_info != (TIFFInfo *) NULL);
2071 if (tiff_info->scanlines != (unsigned char *) NULL)
2072 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2073 tiff_info->scanlines);
2074 if (tiff_info->pixels != (unsigned char *) NULL)
2075 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2076 tiff_info->pixels);
2077}
2078
2079static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2080 TIFFInfo *tiff_info)
2081{
2082 const char
2083 *option;
2084
2085 MagickStatusType
2086 flags;
2087
2088 assert(tiff_info != (TIFFInfo *) NULL);
2089 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2090 option=GetImageOption(image_info,"tiff:tile-geometry");
2091 if (option == (const char *) NULL)
2092 return(MagickTrue);
2093 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2094 if ((flags & HeightValue) == 0)
2095 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2096 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,(uint32)
2097 tiff_info->tile_geometry.width);
2098 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,(uint32)
2099 tiff_info->tile_geometry.height);
2100 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2101 tiff_info->tile_geometry.height*TIFFScanlineSize(tiff),
2102 sizeof(*tiff_info->scanlines));
2103 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2104 TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2105 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2106 (tiff_info->pixels == (unsigned char *) NULL))
2107 {
2108 DestroyTIFFInfo(tiff_info);
2109 return(MagickFalse);
2110 }
2111 return(MagickTrue);
2112}
2113
2114static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,long row,
2115 tsample_t sample,Image *image)
2116{
2117 int32
2118 status;
2119
2120 long
2121 bytes_per_pixel,
2122 j,
2123 k,
2124 l;
2125
2126 register long
2127 i;
2128
2129 register unsigned char
2130 *p,
2131 *q;
2132
2133 unsigned long
2134 number_tiles,
2135 tile_width;
2136
2137 if (TIFFIsTiled(tiff) == 0)
2138 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2139 /*
2140 Fill scanlines to tile height.
2141 */
2142 i=(long) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2143 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2144 (size_t) TIFFScanlineSize(tiff));
2145 if (((unsigned long) (row % tiff_info->tile_geometry.height) !=
2146 (tiff_info->tile_geometry.height-1)) && (row != (long) (image->rows-1)))
2147 return(0);
2148 /*
2149 Write tile to TIFF image.
2150 */
2151 status=0;
2152 bytes_per_pixel=TIFFTileSize(tiff)/(long) (tiff_info->tile_geometry.height*
2153 tiff_info->tile_geometry.width);
2154 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2155 tiff_info->tile_geometry.width;
2156 for (i=0; i < (long) number_tiles; i++)
2157 {
2158 tile_width=(i == (long) (number_tiles-1)) ? image->columns-(i*
2159 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2160 for (j=0; j < (long) ((row % tiff_info->tile_geometry.height)+1); j++)
2161 for (k=0; k < (long) tile_width; k++)
2162 {
2163 if (bytes_per_pixel == 0)
2164 {
2165 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2166 tiff_info->tile_geometry.width+k)/8);
2167 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2168 *q++=(*p++);
2169 continue;
2170 }
2171 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2172 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2173 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2174 for (l=0; l < bytes_per_pixel; l++)
2175 *q++=(*p++);
2176 }
2177 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2178 tiff_info->tile_geometry.width),(uint32) ((row/
2179 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2180 sample);
2181 if (status < 0)
2182 break;
2183 }
2184 return(status);
2185}
2186
2187static void TIFFSetProfiles(TIFF *tiff,Image *image)
2188{
2189 const char
2190 *name;
2191
2192 const StringInfo
2193 *profile;
2194
2195 if (image->profiles == (void *) NULL)
2196 return;
2197 ResetImageProfileIterator(image);
2198 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2199 {
2200 profile=GetImageProfile(image,name);
2201#if defined(TIFFTAG_XMLPACKET)
2202 if (LocaleCompare(name,"xmp") == 0)
2203 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2204 profile),GetStringInfoDatum(profile));
2205#endif
2206#if defined(TIFFTAG_ICCPROFILE)
2207 if (LocaleCompare(name,"icc") == 0)
2208 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2209 profile),GetStringInfoDatum(profile));
2210#endif
2211 if (LocaleCompare(name,"iptc") == 0)
2212 {
2213 size_t
2214 length;
2215
2216 StringInfo
2217 *iptc_profile;
2218
2219 iptc_profile=CloneStringInfo(profile);
2220 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2221 0x03);
2222 SetStringInfoLength(iptc_profile,length);
2223 if (TIFFIsByteSwapped(tiff))
2224 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2225 (unsigned long) (length/4));
2226 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2227 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2228 iptc_profile=DestroyStringInfo(iptc_profile);
2229 }
2230#if defined(TIFFTAG_PHOTOSHOP)
2231 if (LocaleCompare(name,"8bim") == 0)
2232 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2233 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2234#endif
2235 if (LocaleCompare(name,"tiff:37724") == 0)
2236 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2237 GetStringInfoDatum(profile));
2238 name=GetNextImageProfile(image);
2239 }
2240}
2241
2242static void TIFFSetProperties(TIFF *tiff,Image *image)
2243{
2244 const char
2245 *value;
2246
2247 value=GetImageProperty(image,"tiff:hostcomputer");
2248 if (value != (const char *) NULL)
2249 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2250 value=GetImageProperty(image,"tiff:artist");
2251 if (value != (const char *) NULL)
2252 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2253 value=GetImageProperty(image,"tiff:timestamp");
2254 if (value != (const char *) NULL)
2255 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2256 value=GetImageProperty(image,"tiff:make");
2257 if (value != (const char *) NULL)
2258 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2259 value=GetImageProperty(image,"tiff:model");
2260 if (value != (const char *) NULL)
2261 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2262 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,
2263 GetMagickVersion((unsigned long *) NULL));
2264 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
2265 value=GetImageProperty(image,"tiff:copyright");
2266 if (value != (const char *) NULL)
2267 (void) TIFFSetField(tiff,33432,value);
2268 value=GetImageProperty(image,"kodak-33423");
2269 if (value != (const char *) NULL)
2270 (void) TIFFSetField(tiff,33423,value);
2271 value=GetImageProperty(image,"kodak-36867");
2272 if (value != (const char *) NULL)
2273 (void) TIFFSetField(tiff,36867,value);
2274 value=GetImageProperty(image,"label");
2275 if (value != (const char *) NULL)
2276 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2277 value=GetImageProperty(image,"comment");
2278 if (value != (const char *) NULL)
2279 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2280}
2281
2282static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2283{
2284#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2285 const char
2286 *value;
2287
2288 register long
2289 i;
2290
2291 uint32
2292 offset;
2293
2294 /*
2295 Write EXIF properties.
2296 */
2297 offset=0;
2298 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2299 for (i=0; exif_info[i].tag != 0; i++)
2300 {
2301 value=GetImageProperty(image,exif_info[i].property);
2302 if (value == (const char *) NULL)
2303 continue;
2304 switch (exif_info[i].type)
2305 {
2306 case TIFF_ASCII:
2307 {
2308 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2309 break;
2310 }
2311 case TIFF_SHORT:
2312 {
2313 uint16
2314 shorty;
2315
cristyf2f27272009-12-17 14:48:46 +00002316 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002317 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2318 break;
2319 }
2320 case TIFF_LONG:
2321 {
2322 uint16
2323 longy;
2324
cristyf2f27272009-12-17 14:48:46 +00002325 longy=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002326 (void) TIFFSetField(tiff,exif_info[i].tag,longy);
2327 break;
2328 }
2329 case TIFF_RATIONAL:
2330 case TIFF_SRATIONAL:
2331 {
2332 float
2333 rational;
2334
cristyf2f27272009-12-17 14:48:46 +00002335 rational=StringToDouble(value);
cristy3ed852e2009-09-05 21:47:34 +00002336 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2337 break;
2338 }
2339 default:
2340 break;
2341 }
2342 }
2343 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2344#else
2345 (void) tiff;
2346 (void) image;
2347#endif
2348}
2349
2350static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2351 Image *image)
2352{
2353#if !defined(TIFFDefaultStripSize)
2354#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2355#endif
2356
2357 const char
2358 *mode,
2359 *option;
2360
2361 const char
2362 *value;
2363
2364 CompressionType
2365 compression;
2366
2367 long
2368 y;
2369
2370 MagickBooleanType
2371 debug,
2372 status;
2373
2374 MagickOffsetType
2375 scene;
2376
2377 QuantumInfo
2378 *quantum_info;
2379
2380 QuantumType
2381 quantum_type;
2382
2383 register long
2384 i;
2385
2386 size_t
2387 length;
2388
2389 TIFF
2390 *tiff;
2391
2392 TIFFErrorHandler
2393 error_handler,
2394 warning_handler;
2395
2396 TIFFInfo
2397 tiff_info;
2398
2399 uint16
2400 bits_per_sample,
2401 compress_tag,
2402 endian,
2403 photometric;
2404
2405 uint32
2406 rows_per_strip;
2407
2408 unsigned char
2409 *pixels;
2410
2411 unsigned long
2412 lsb_first;
2413
2414 /*
2415 Open TIFF file.
2416 */
2417 assert(image_info != (const ImageInfo *) NULL);
2418 assert(image_info->signature == MagickSignature);
2419 assert(image != (Image *) NULL);
2420 assert(image->signature == MagickSignature);
2421 if (image->debug != MagickFalse)
2422 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2423 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2424 if (status == MagickFalse)
2425 return(status);
2426 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2427 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2428 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
2429 switch (image_info->endian)
2430 {
2431 case LSBEndian: mode="wl"; break;
2432 case MSBEndian: mode="wb"; break;
2433 default: mode="w"; break;
2434 }
2435#if defined(TIFF_VERSION_BIG)
2436 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
2437 switch (image_info->endian)
2438 {
2439 case LSBEndian: mode="wl8"; break;
2440 case MSBEndian: mode="wb8"; break;
2441 default: mode="w8"; break;
2442 }
2443#endif
2444 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2445 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2446 TIFFUnmapBlob);
2447 if (tiff == (TIFF *) NULL)
2448 {
2449 (void) TIFFSetWarningHandler(warning_handler);
2450 (void) TIFFSetErrorHandler(error_handler);
2451 return(MagickFalse);
2452 }
2453 scene=0;
2454 debug=IsEventLogging();
2455 do
2456 {
2457 /*
2458 Initialize TIFF fields.
2459 */
2460 if ((image_info->type != UndefinedType) &&
2461 (image_info->type != OptimizeType))
2462 (void) SetImageType(image,image_info->type);
2463 quantum_info=AcquireQuantumInfo(image_info,image);
2464 if (quantum_info == (QuantumInfo *) NULL)
2465 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2466 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2467 (quantum_info->format == UndefinedQuantumFormat) &&
2468 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2469 {
2470 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2471 if (status == MagickFalse)
2472 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2473 }
2474 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2475 (GetPreviousImageInList(image) != (Image *) NULL))
2476 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2477 if ((image->columns != (uint32) image->columns) ||
2478 (image->rows != (uint32) image->rows))
2479 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2480 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2481 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2482 compression=image->compression;
2483 if (image_info->compression != UndefinedCompression)
2484 compression=image_info->compression;
2485 switch (compression)
2486 {
2487 case FaxCompression:
2488 {
2489 compress_tag=COMPRESSION_CCITTFAX3;
2490 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2491 break;
2492 }
2493 case Group4Compression:
2494 {
2495 compress_tag=COMPRESSION_CCITTFAX4;
2496 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2497 break;
2498 }
2499 case JPEGCompression:
2500 {
2501 compress_tag=COMPRESSION_JPEG;
2502 break;
2503 }
2504 case LZWCompression:
2505 {
2506 compress_tag=COMPRESSION_LZW;
2507 break;
2508 }
2509 case RLECompression:
2510 {
2511 compress_tag=COMPRESSION_PACKBITS;
2512 break;
2513 }
2514 case ZipCompression:
2515 {
2516 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2517 break;
2518 }
2519 case NoCompression:
2520 default:
2521 {
2522 compress_tag=COMPRESSION_NONE;
2523 break;
2524 }
2525 }
cristy5b675872009-10-21 13:12:00 +00002526#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2527 if ((compress_tag != COMPRESSION_NONE) &&
2528 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002529 {
cristy5b675872009-10-21 13:12:00 +00002530 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2531 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
2532 MagickCompressOptions,(long) compression));
2533 compress_tag=COMPRESSION_NONE;
2534 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002535 }
2536#else
2537 switch (compress_tag)
2538 {
2539#if defined(CCITT_SUPPORT)
2540 case COMPRESSION_CCITTFAX3:
2541 case COMPRESSION_CCITTFAX4:
2542#endif
2543#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2544 case COMPRESSION_JPEG:
2545#endif
2546#if defined(LZW_SUPPORT)
2547 case COMPRESSION_LZW:
2548#endif
2549#if defined(PACKBITS_SUPPORT)
2550 case COMPRESSION_PACKBITS:
2551#endif
2552#if defined(ZIP_SUPPORT)
2553 case COMPRESSION_ADOBE_DEFLATE:
2554#endif
2555 case COMPRESSION_NONE:
2556 break;
2557 default:
2558 {
2559 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2560 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
2561 MagickCompressOptions,(long) compression));
2562 compress_tag=COMPRESSION_NONE;
2563 compression=NoCompression;
2564 break;
2565 }
2566 }
2567#endif
2568 switch (compression)
2569 {
2570 case FaxCompression:
2571 case Group4Compression:
2572 {
2573 (void) SetImageType(image,BilevelType);
2574 break;
2575 }
2576 case JPEGCompression:
2577 {
2578 (void) SetImageStorageClass(image,DirectClass);
2579 (void) SetImageDepth(image,8);
2580 break;
2581 }
2582 default:
2583 break;
2584 }
cristy3ed852e2009-09-05 21:47:34 +00002585 if (image->colorspace == CMYKColorspace)
2586 {
2587 photometric=PHOTOMETRIC_SEPARATED;
2588 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2589 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2590 }
2591 else
2592 {
2593 /*
2594 Full color TIFF raster.
2595 */
2596 if (image->colorspace == LabColorspace)
2597 photometric=PHOTOMETRIC_CIELAB;
2598 else
2599 if (image->colorspace == YCbCrColorspace)
2600 {
2601 photometric=PHOTOMETRIC_YCBCR;
2602 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
2603 (void) SetImageDepth(image,8);
2604 }
2605 else
2606 {
2607 if (image->colorspace != RGBColorspace)
2608 (void) TransformImageColorspace(image,RGBColorspace);
2609 photometric=PHOTOMETRIC_RGB;
2610 }
2611 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2612 if ((image_info->type != TrueColorType) &&
2613 (image_info->type != TrueColorMatteType))
2614 {
cristy94c8fe42009-10-06 01:57:36 +00002615 if ((image_info->type != PaletteType) &&
2616 (IsGrayImage(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002617 {
cristy94c8fe42009-10-06 01:57:36 +00002618 photometric=(uint16) (quantum_info->min_is_white !=
2619 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2620 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002621 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002622 if ((image_info->depth == 0) &&
2623 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2624 {
2625 status=SetQuantumDepth(image,quantum_info,1);
2626 if (status == MagickFalse)
2627 ThrowWriterException(ResourceLimitError,
2628 "MemoryAllocationFailed");
2629 }
cristy3ed852e2009-09-05 21:47:34 +00002630 }
2631 else
cristy94c8fe42009-10-06 01:57:36 +00002632 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002633 {
cristy94c8fe42009-10-06 01:57:36 +00002634 unsigned long
2635 depth;
2636
2637 /*
2638 Colormapped TIFF raster.
2639 */
cristy3ed852e2009-09-05 21:47:34 +00002640 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002641 photometric=PHOTOMETRIC_PALETTE;
2642 depth=1;
2643 while ((GetQuantumRange(depth)+1) < image->colors)
2644 depth<<=1;
2645 status=SetQuantumDepth(image,quantum_info,depth);
2646 if (status == MagickFalse)
2647 ThrowWriterException(ResourceLimitError,
2648 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002649 }
2650 }
2651 }
cristy0accf6e2009-10-01 13:14:28 +00002652 switch (image->endian)
2653 {
2654 case LSBEndian:
2655 {
2656 endian=FILLORDER_LSB2MSB;
2657 break;
2658 }
2659 case MSBEndian:
2660 {
2661 endian=FILLORDER_MSB2LSB;
2662 break;
2663 }
2664 case UndefinedEndian:
2665 default:
2666 {
2667 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2668 break;
2669 }
2670 }
2671 lsb_first=1;
2672 image->endian=MSBEndian;
2673 if ((int) (*(char *) &lsb_first) != 0)
2674 image->endian=LSBEndian;
2675 if ((compress_tag == COMPRESSION_JPEG) && (photometric != PHOTOMETRIC_RGB))
2676 compress_tag=COMPRESSION_NONE;
2677 else
2678 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2679 (photometric != PHOTOMETRIC_MINISWHITE))
2680 {
2681 compress_tag=COMPRESSION_NONE;
2682 endian=FILLORDER_MSB2LSB;
2683 }
2684 else
2685 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2686 (photometric != PHOTOMETRIC_MINISWHITE))
2687 {
2688 compress_tag=COMPRESSION_NONE;
2689 endian=FILLORDER_MSB2LSB;
2690 }
2691 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2692 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002693 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2694 if (image->matte != MagickFalse)
2695 {
2696 uint16
2697 extra_samples,
2698 sample_info[1],
2699 samples_per_pixel;
2700
2701 /*
2702 TIFF has a matte channel.
2703 */
2704 extra_samples=1;
2705 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2706 option=GetImageProperty(image,"tiff:alpha");
2707 if ((option != (const char *) NULL) &&
2708 (LocaleCompare(option,"associated") == 0))
2709 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2710 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2711 &samples_per_pixel);
2712 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2713 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2714 &sample_info);
2715 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2716 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2717 }
2718 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2719 switch (quantum_info->format)
2720 {
2721 case FloatingPointQuantumFormat:
2722 {
2723 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2724 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2725 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2726 break;
2727 }
2728 case SignedQuantumFormat:
2729 {
2730 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2731 break;
2732 }
2733 case UnsignedQuantumFormat:
2734 {
2735 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2736 break;
2737 }
2738 default:
2739 break;
2740 }
cristy3ed852e2009-09-05 21:47:34 +00002741 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2742 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2743 if (photometric == PHOTOMETRIC_RGB)
2744 if ((image_info->interlace == PlaneInterlace) ||
2745 (image_info->interlace == PartitionInterlace))
2746 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2747 rows_per_strip=1;
2748 if (TIFFScanlineSize(tiff) != 0)
2749 rows_per_strip=(unsigned long) MagickMax((size_t)
2750 TIFFDefaultStripSize(tiff,-1),1);
2751 option=GetImageOption(image_info,"tiff:rows-per-strip");
2752 if (option != (const char *) NULL)
2753 rows_per_strip=(unsigned long) strtol(option,(char **) NULL,10);
2754 switch (compress_tag)
2755 {
2756 case COMPRESSION_JPEG:
2757 {
2758#if defined(JPEG_SUPPORT)
2759 const char
2760 *sampling_factor;
2761
2762 GeometryInfo
2763 geometry_info;
2764
2765 MagickStatusType
2766 flags;
2767
2768 rows_per_strip+=(16-(rows_per_strip % 16));
2769 if (image->quality != 0)
2770 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2771 if (image_info->quality != UndefinedCompressionQuality)
2772 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2773 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2774 if (image->colorspace == RGBColorspace)
2775 {
2776 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2777 sampling_factor=(const char *) NULL;
2778 value=GetImageProperty(image,"jpeg:sampling-factor");
2779 if (value != (char *) NULL)
2780 {
2781 sampling_factor=value;
2782 if (image->debug != MagickFalse)
2783 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2784 " Input sampling-factors=%s",sampling_factor);
2785 }
2786 if (image_info->sampling_factor != (char *) NULL)
2787 sampling_factor=image_info->sampling_factor;
2788 if (sampling_factor != (const char *) NULL)
2789 {
2790 flags=ParseGeometry(sampling_factor,&geometry_info);
2791 if ((flags & SigmaValue) == 0)
2792 geometry_info.sigma=geometry_info.rho;
2793 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2794 geometry_info.rho,(uint16) geometry_info.sigma);
2795 }
2796 }
2797 if (bits_per_sample == 12)
2798 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2799#endif
2800 break;
2801 }
2802 case COMPRESSION_ADOBE_DEFLATE:
2803 {
2804 rows_per_strip=image->rows;
2805 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2806 &bits_per_sample);
2807 if (((photometric == PHOTOMETRIC_RGB) ||
2808 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2809 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2810 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
2811 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,image_info->quality ==
2812 UndefinedCompressionQuality ? 7 : MagickMin(1L*image_info->quality/10,
2813 9));
2814 break;
2815 }
2816 case COMPRESSION_CCITTFAX3:
2817 {
2818 /*
2819 Byte-aligned EOL.
2820 */
2821 rows_per_strip=image->rows;
2822 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2823 break;
2824 }
2825 case COMPRESSION_CCITTFAX4:
2826 {
2827 rows_per_strip=image->rows;
2828 break;
2829 }
2830 case COMPRESSION_LZW:
2831 {
2832 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2833 &bits_per_sample);
2834 if (((photometric == PHOTOMETRIC_RGB) ||
2835 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2836 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2837 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
2838 break;
2839 }
2840 default:
2841 break;
2842 }
2843 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
2844 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2845 {
2846 unsigned short
2847 units;
2848
2849 /*
2850 Set image resolution.
2851 */
2852 units=RESUNIT_NONE;
2853 if (image->units == PixelsPerInchResolution)
2854 units=RESUNIT_INCH;
2855 if (image->units == PixelsPerCentimeterResolution)
2856 units=RESUNIT_CENTIMETER;
2857 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2858 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2859 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2860 if ((image->page.x != 0) || (image->page.y != 0))
2861 {
2862 /*
2863 Set image position.
2864 */
2865 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2866 image->x_resolution);
2867 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2868 image->y_resolution);
2869 }
2870 }
2871 if (image->chromaticity.white_point.x != 0.0)
2872 {
2873 float
2874 chromaticity[6];
2875
2876 /*
2877 Set image chromaticity.
2878 */
2879 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2880 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2881 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2882 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2883 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2884 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2885 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2886 chromaticity[0]=(float) image->chromaticity.white_point.x;
2887 chromaticity[1]=(float) image->chromaticity.white_point.y;
2888 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2889 }
2890 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2891 {
2892 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2893 if (image->scene != 0)
2894 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
2895 GetImageListLength(image));
2896 }
2897 if (image->orientation != UndefinedOrientation)
2898 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
2899 (void) TIFFSetProfiles(tiff,image);
2900 {
2901 uint16
2902 page,
2903 pages;
2904
2905 page=(uint16) scene;
2906 pages=GetImageListLength(image);
2907 if ((image_info->adjoin != MagickFalse) && (pages > 1))
2908 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2909 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
2910 }
2911 (void) TIFFSetProperties(tiff,image);
2912 if (0)
2913 (void) TIFFSetEXIFProperties(tiff,image);
2914 /*
2915 Write image scanlines.
2916 */
2917 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
2918 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2919 pixels=GetQuantumPixels(quantum_info);
2920 tiff_info.scanline=GetQuantumPixels(quantum_info);
2921 switch (photometric)
2922 {
2923 case PHOTOMETRIC_CIELAB:
2924 case PHOTOMETRIC_YCBCR:
2925 case PHOTOMETRIC_RGB:
2926 {
2927 /*
2928 RGB TIFF image.
2929 */
2930 switch (image_info->interlace)
2931 {
2932 case NoInterlace:
2933 default:
2934 {
2935 quantum_type=RGBQuantum;
2936 if (image->matte != MagickFalse)
2937 quantum_type=RGBAQuantum;
2938 for (y=0; y < (long) image->rows; y++)
2939 {
2940 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002941 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002942
2943 p=GetVirtualPixels(image,0,y,image->columns,1,
2944 &image->exception);
2945 if (p == (const PixelPacket *) NULL)
2946 break;
2947 length=ExportQuantumPixels(image,(const CacheView *) NULL,
2948 quantum_info,quantum_type,pixels,&image->exception);
2949 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
2950 break;
2951 if (image->previous == (Image *) NULL)
2952 {
2953 status=SetImageProgress(image,SaveImageTag,y,image->rows);
2954 if (status == MagickFalse)
2955 break;
2956 }
2957 }
2958 break;
2959 }
2960 case PlaneInterlace:
2961 case PartitionInterlace:
2962 {
2963 /*
2964 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
2965 */
2966 for (y=0; y < (long) image->rows; y++)
2967 {
2968 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002969 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002970
2971 p=GetVirtualPixels(image,0,y,image->columns,1,
2972 &image->exception);
2973 if (p == (const PixelPacket *) NULL)
2974 break;
2975 length=ExportQuantumPixels(image,(const CacheView *) NULL,
2976 quantum_info,RedQuantum,pixels,&image->exception);
2977 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
2978 break;
2979 }
2980 if (image->previous == (Image *) NULL)
2981 {
2982 status=SetImageProgress(image,SaveImageTag,100,400);
2983 if (status == MagickFalse)
2984 break;
2985 }
2986 for (y=0; y < (long) image->rows; y++)
2987 {
2988 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00002989 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002990
2991 p=GetVirtualPixels(image,0,y,image->columns,1,
2992 &image->exception);
2993 if (p == (const PixelPacket *) NULL)
2994 break;
2995 length=ExportQuantumPixels(image,(const CacheView *) NULL,
2996 quantum_info,GreenQuantum,pixels,&image->exception);
2997 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
2998 break;
2999 }
3000 if (image->previous == (Image *) NULL)
3001 {
3002 status=SetImageProgress(image,SaveImageTag,200,400);
3003 if (status == MagickFalse)
3004 break;
3005 }
3006 for (y=0; y < (long) image->rows; y++)
3007 {
3008 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003009 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003010
3011 p=GetVirtualPixels(image,0,y,image->columns,1,
3012 &image->exception);
3013 if (p == (const PixelPacket *) NULL)
3014 break;
3015 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3016 quantum_info,BlueQuantum,pixels,&image->exception);
3017 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3018 break;
3019 }
3020 if (image->previous == (Image *) NULL)
3021 {
3022 status=SetImageProgress(image,SaveImageTag,300,400);
3023 if (status == MagickFalse)
3024 break;
3025 }
3026 if (image->matte != MagickFalse)
3027 for (y=0; y < (long) image->rows; y++)
3028 {
3029 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003030 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003031
3032 p=GetVirtualPixels(image,0,y,image->columns,1,
3033 &image->exception);
3034 if (p == (const PixelPacket *) NULL)
3035 break;
3036 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3037 quantum_info,AlphaQuantum,pixels,&image->exception);
3038 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3039 break;
3040 }
3041 if (image->previous == (Image *) NULL)
3042 {
3043 status=SetImageProgress(image,SaveImageTag,400,400);
3044 if (status == MagickFalse)
3045 break;
3046 }
3047 break;
3048 }
3049 }
3050 break;
3051 }
3052 case PHOTOMETRIC_SEPARATED:
3053 {
3054 /*
3055 CMYK TIFF image.
3056 */
3057 quantum_type=CMYKQuantum;
3058 if (image->matte != MagickFalse)
3059 quantum_type=CMYKAQuantum;
3060 if (image->colorspace != CMYKColorspace)
3061 (void) TransformImageColorspace(image,CMYKColorspace);
3062 for (y=0; y < (long) image->rows; y++)
3063 {
3064 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003065 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003066
3067 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3068 if (p == (const PixelPacket *) NULL)
3069 break;
3070 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3071 quantum_info,quantum_type,pixels,&image->exception);
3072 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3073 break;
3074 if (image->previous == (Image *) NULL)
3075 {
3076 status=SetImageProgress(image,SaveImageTag,y,image->rows);
3077 if (status == MagickFalse)
3078 break;
3079 }
3080 }
3081 break;
3082 }
3083 case PHOTOMETRIC_PALETTE:
3084 {
3085 uint16
3086 *blue,
3087 *green,
3088 *red;
3089
3090 /*
3091 Colormapped TIFF image.
3092 */
3093 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3094 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3095 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3096 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3097 (blue == (uint16 *) NULL))
3098 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3099 /*
3100 Initialize TIFF colormap.
3101 */
3102 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3103 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3104 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3105 for (i=0; i < (long) image->colors; i++)
3106 {
3107 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3108 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3109 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3110 }
3111 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3112 red=(uint16 *) RelinquishMagickMemory(red);
3113 green=(uint16 *) RelinquishMagickMemory(green);
3114 blue=(uint16 *) RelinquishMagickMemory(blue);
3115 }
3116 default:
3117 {
3118 /*
3119 Convert PseudoClass packets to contiguous grayscale scanlines.
3120 */
3121 quantum_type=IndexQuantum;
3122 if (image->matte != MagickFalse)
3123 {
3124 if (photometric != PHOTOMETRIC_PALETTE)
3125 quantum_type=GrayAlphaQuantum;
3126 else
3127 quantum_type=IndexAlphaQuantum;
3128 }
3129 else
3130 if (photometric != PHOTOMETRIC_PALETTE)
3131 quantum_type=GrayQuantum;
3132 for (y=0; y < (long) image->rows; y++)
3133 {
3134 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003135 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003136
3137 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3138 if (p == (const PixelPacket *) NULL)
3139 break;
3140 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3141 quantum_info,quantum_type,pixels,&image->exception);
3142 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3143 break;
3144 if (image->previous == (Image *) NULL)
3145 {
3146 status=SetImageProgress(image,SaveImageTag,y,image->rows);
3147 if (status == MagickFalse)
3148 break;
3149 }
3150 }
3151 break;
3152 }
3153 }
3154 quantum_info=DestroyQuantumInfo(quantum_info);
3155 DestroyTIFFInfo(&tiff_info);
3156 if (0 && (image_info->verbose == MagickTrue))
3157 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3158 (void) TIFFWriteDirectory(tiff);
3159 image->endian=MSBEndian;
3160 if (endian == FILLORDER_LSB2MSB)
3161 image->endian=LSBEndian;
3162 image=SyncNextImageInList(image);
3163 if (image == (Image *) NULL)
3164 break;
3165 status=SetImageProgress(image,SaveImagesTag,scene++,
3166 GetImageListLength(image));
3167 if (status == MagickFalse)
3168 break;
3169 } while (image_info->adjoin != MagickFalse);
3170 (void) TIFFSetWarningHandler(warning_handler);
3171 (void) TIFFSetErrorHandler(error_handler);
3172 TIFFClose(tiff);
3173 return(MagickTrue);
3174}
3175#endif