2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % TTTTT IIIII FFFFF FFFFF %
13 % Read/Write TIFF Image Format %
20 % Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 #define JPEG_SUPPORT 1
46 #include "MagickCore/studio.h"
47 #include "MagickCore/artifact.h"
48 #include "MagickCore/attribute.h"
49 #include "MagickCore/blob.h"
50 #include "MagickCore/blob-private.h"
51 #include "MagickCore/cache.h"
52 #include "MagickCore/color.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colormap.h"
55 #include "MagickCore/colorspace.h"
56 #include "MagickCore/colorspace-private.h"
57 #include "MagickCore/constitute.h"
58 #include "MagickCore/enhance.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/geometry.h"
62 #include "MagickCore/image.h"
63 #include "MagickCore/image-private.h"
64 #include "MagickCore/list.h"
65 #include "MagickCore/log.h"
66 #include "MagickCore/magick.h"
67 #include "MagickCore/memory_.h"
68 #include "MagickCore/memory-private.h"
69 #include "MagickCore/module.h"
70 #include "MagickCore/monitor.h"
71 #include "MagickCore/monitor-private.h"
72 #include "MagickCore/option.h"
73 #include "MagickCore/pixel-accessor.h"
74 #include "MagickCore/pixel-private.h"
75 #include "MagickCore/property.h"
76 #include "MagickCore/quantum.h"
77 #include "MagickCore/quantum-private.h"
78 #include "MagickCore/profile.h"
79 #include "MagickCore/resize.h"
80 #include "MagickCore/resource_.h"
81 #include "MagickCore/semaphore.h"
82 #include "MagickCore/splay-tree.h"
83 #include "MagickCore/static.h"
84 #include "MagickCore/statistic.h"
85 #include "MagickCore/string_.h"
86 #include "MagickCore/string-private.h"
87 #include "MagickCore/thread_.h"
88 #include "MagickCore/token.h"
89 #include "MagickCore/utility.h"
90 #if defined(MAGICKCORE_TIFF_DELEGATE)
91 # if defined(MAGICKCORE_HAVE_TIFFCONF_H)
92 # include "tiffconf.h"
96 # if !defined(COMPRESSION_ADOBE_DEFLATE)
97 # define COMPRESSION_ADOBE_DEFLATE 8
99 # if !defined(PREDICTOR_HORIZONTAL)
100 # define PREDICTOR_HORIZONTAL 2
102 # if !defined(TIFFTAG_COPYRIGHT)
103 # define TIFFTAG_COPYRIGHT 33432
105 # if !defined(TIFFTAG_OPIIMAGEID)
106 # define TIFFTAG_OPIIMAGEID 32781
108 #include "psd-private.h"
111 Typedef declarations.
115 ReadSingleSampleMethod,
124 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
125 typedef struct _ExifInfo
136 static const ExifInfo
138 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, 0, "exif:ExposureTime" },
139 { EXIFTAG_FNUMBER, TIFF_RATIONAL, 0, "exif:FNumber" },
140 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, 0, "exif:ExposureProgram" },
141 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, 0, "exif:SpectralSensitivity" },
142 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, 1, "exif:ISOSpeedRatings" },
143 { EXIFTAG_OECF, TIFF_NOTYPE, 0, "exif:OptoelectricConversionFactor" },
144 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, 0, "exif:ExifVersion" },
145 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, 0, "exif:DateTimeOriginal" },
146 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, 0, "exif:DateTimeDigitized" },
147 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, 0, "exif:ComponentsConfiguration" },
148 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, 0, "exif:CompressedBitsPerPixel" },
149 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, 0, "exif:ShutterSpeedValue" },
150 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, 0, "exif:ApertureValue" },
151 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, 0, "exif:BrightnessValue" },
152 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, 0, "exif:ExposureBiasValue" },
153 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, 0, "exif:MaxApertureValue" },
154 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, 0, "exif:SubjectDistance" },
155 { EXIFTAG_METERINGMODE, TIFF_SHORT, 0, "exif:MeteringMode" },
156 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, 0, "exif:LightSource" },
157 { EXIFTAG_FLASH, TIFF_SHORT, 0, "exif:Flash" },
158 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, 0, "exif:FocalLength" },
159 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, 0, "exif:SubjectArea" },
160 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, 0, "exif:MakerNote" },
161 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, 0, "exif:UserComment" },
162 { EXIFTAG_SUBSECTIME, TIFF_ASCII, 0, "exif:SubSecTime" },
163 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, 0, "exif:SubSecTimeOriginal" },
164 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, 0, "exif:SubSecTimeDigitized" },
165 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, 0, "exif:FlashpixVersion" },
166 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, 0, "exif:PixelXDimension" },
167 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, 0, "exif:PixelYDimension" },
168 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, 0, "exif:RelatedSoundFile" },
169 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, 0, "exif:FlashEnergy" },
170 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, 0, "exif:SpatialFrequencyResponse" },
171 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, 0, "exif:FocalPlaneXResolution" },
172 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, 0, "exif:FocalPlaneYResolution" },
173 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, 0, "exif:FocalPlaneResolutionUnit" },
174 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, 0, "exif:SubjectLocation" },
175 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, 0, "exif:ExposureIndex" },
176 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, 0, "exif:SensingMethod" },
177 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, 0, "exif:FileSource" },
178 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, 0, "exif:SceneType" },
179 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, 0, "exif:CFAPattern" },
180 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, 0, "exif:CustomRendered" },
181 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, 0, "exif:ExposureMode" },
182 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, 0, "exif:WhiteBalance" },
183 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, 0, "exif:DigitalZoomRatio" },
184 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, 0, "exif:FocalLengthIn35mmFilm" },
185 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, 0, "exif:SceneCaptureType" },
186 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, 0, "exif:GainControl" },
187 { EXIFTAG_CONTRAST, TIFF_SHORT, 0, "exif:Contrast" },
188 { EXIFTAG_SATURATION, TIFF_SHORT, 0, "exif:Saturation" },
189 { EXIFTAG_SHARPNESS, TIFF_SHORT, 0, "exif:Sharpness" },
190 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, 0, "exif:DeviceSettingDescription" },
191 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, 0, "exif:SubjectDistanceRange" },
192 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, 0, "exif:ImageUniqueID" },
193 { 0, 0, 0, (char *) NULL }
196 #endif /* MAGICKCORE_TIFF_DELEGATE */
201 static MagickThreadKey
205 *tiff_semaphore = (SemaphoreInfo *) NULL;
207 static TIFFErrorHandler
211 static volatile MagickBooleanType
212 instantiate_key = MagickFalse;
215 Forward declarations.
217 #if defined(MAGICKCORE_TIFF_DELEGATE)
219 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
221 static MagickBooleanType
222 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
223 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
224 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 % IsTIFF() returns MagickTrue if the image format type, identified by the
239 % magick string, is TIFF.
241 % The format of the IsTIFF method is:
243 % MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
245 % A description of each parameter follows:
247 % o magick: compare image format pattern against these bytes.
249 % o length: Specifies the length of the magick string.
252 static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
256 if (memcmp(magick,"\115\115\000\052",4) == 0)
258 if (memcmp(magick,"\111\111\052\000",4) == 0)
260 #if defined(TIFF_VERSION_BIG)
263 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
265 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
271 #if defined(MAGICKCORE_TIFF_DELEGATE)
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % R e a d G R O U P 4 I m a g e %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
284 % allocates the memory necessary for the new Image structure and returns a
285 % pointer to the new image.
287 % The format of the ReadGROUP4Image method is:
289 % Image *ReadGROUP4Image(const ImageInfo *image_info,
290 % ExceptionInfo *exception)
292 % A description of each parameter follows:
294 % o image_info: the image info.
296 % o exception: return any errors or warnings in this structure.
300 static inline size_t WriteLSBLong(FILE *file,const size_t value)
305 buffer[0]=(unsigned char) value;
306 buffer[1]=(unsigned char) (value >> 8);
307 buffer[2]=(unsigned char) (value >> 16);
308 buffer[3]=(unsigned char) (value >> 24);
309 return(fwrite(buffer,1,4,file));
312 static Image *ReadGROUP4Image(const ImageInfo *image_info,
313 ExceptionInfo *exception)
316 filename[MagickPathExtent];
344 assert(image_info != (const ImageInfo *) NULL);
345 assert(image_info->signature == MagickCoreSignature);
346 if (image_info->debug != MagickFalse)
347 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
348 image_info->filename);
349 assert(exception != (ExceptionInfo *) NULL);
350 assert(exception->signature == MagickCoreSignature);
351 image=AcquireImage(image_info,exception);
352 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
353 if (status == MagickFalse)
355 image=DestroyImageList(image);
356 return((Image *) NULL);
359 Write raw CCITT Group 4 wrapped as a TIFF image file.
362 unique_file=AcquireUniqueFileResource(filename);
363 if (unique_file != -1)
364 file=fdopen(unique_file,"wb");
365 if ((unique_file == -1) || (file == (FILE *) NULL))
366 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
367 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
368 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
369 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
370 length=WriteLSBLong(file,image->columns);
371 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
372 length=WriteLSBLong(file,image->rows);
373 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
374 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
375 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
376 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
377 strip_offset=10+(12*14)+4+8;
378 length=WriteLSBLong(file,(size_t) strip_offset);
379 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
380 length=WriteLSBLong(file,(size_t) image_info->orientation);
381 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
382 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
383 length=WriteLSBLong(file,image->rows);
384 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
385 offset=(ssize_t) ftell(file)-4;
386 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
387 length=WriteLSBLong(file,(size_t) (strip_offset-8));
388 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
389 length=WriteLSBLong(file,(size_t) (strip_offset-8));
390 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
391 length=fwrite("\000\000\000\000",1,4,file);
392 length=WriteLSBLong(file,(long) image->resolution.x);
393 length=WriteLSBLong(file,1);
395 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
396 if (fputc(c,file) != c)
398 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
399 length=WriteLSBLong(file,(unsigned int) length);
401 (void) CloseBlob(image);
402 image=DestroyImage(image);
406 read_info=CloneImageInfo((ImageInfo *) NULL);
407 (void) FormatLocaleString(read_info->filename,MagickPathExtent,"%s",filename);
408 image=ReadTIFFImage(read_info,exception);
409 read_info=DestroyImageInfo(read_info);
410 if (image != (Image *) NULL)
412 (void) CopyMagickString(image->filename,image_info->filename,
414 (void) CopyMagickString(image->magick_filename,image_info->filename,
416 (void) CopyMagickString(image->magick,"GROUP4",MagickPathExtent);
418 (void) RelinquishUniqueFileResource(filename);
419 if (status == MagickFalse)
420 image=DestroyImage(image);
425 #if defined(MAGICKCORE_TIFF_DELEGATE)
427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
431 % R e a d T I F F I m a g e %
435 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
437 % ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
438 % memory necessary for the new Image structure and returns a pointer to the
441 % The format of the ReadTIFFImage method is:
443 % Image *ReadTIFFImage(const ImageInfo *image_info,
444 % ExceptionInfo *exception)
446 % A description of each parameter follows:
448 % o image_info: the image info.
450 % o exception: return any errors or warnings in this structure.
454 static inline unsigned char ClampYCC(double value)
458 return((unsigned char)0);
460 return((unsigned char)255);
461 return((unsigned char)(value));
464 static MagickBooleanType DecodeLabImage(Image *image,ExceptionInfo *exception)
476 image_view=AcquireAuthenticCacheView(image,exception);
477 for (y=0; y < (ssize_t) image->rows; y++)
485 if (status == MagickFalse)
487 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
488 if (q == (Quantum *) NULL)
493 for (x=0; x < (ssize_t) image->columns; x++)
499 a=QuantumScale*GetPixela(image,q)+0.5;
502 b=QuantumScale*GetPixelb(image,q)+0.5;
505 SetPixela(image,QuantumRange*a,q);
506 SetPixelb(image,QuantumRange*b,q);
507 q+=GetPixelChannels(image);
509 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
512 image_view=DestroyCacheView(image_view);
516 static MagickBooleanType ReadProfile(Image *image,const char *name,
517 const unsigned char *datum,ssize_t length,ExceptionInfo *exception)
527 profile=BlobToStringInfo(datum,(size_t) length);
528 if (profile == (StringInfo *) NULL)
529 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
531 status=SetImageProfile(image,name,profile,exception);
532 profile=DestroyStringInfo(profile);
533 if (status == MagickFalse)
534 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
539 #if defined(__cplusplus) || defined(c_plusplus)
543 static int TIFFCloseBlob(thandle_t image)
545 (void) CloseBlob((Image *) image);
549 static void TIFFErrors(const char *module,const char *format,va_list error)
552 message[MagickPathExtent];
557 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
558 (void) vsnprintf(message,MagickPathExtent,format,error);
560 (void) vsprintf(message,format,error);
562 (void) ConcatenateMagickString(message,".",MagickPathExtent);
563 exception=(ExceptionInfo *) GetMagickThreadValue(tiff_exception);
564 if (exception != (ExceptionInfo *) NULL)
565 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
569 static toff_t TIFFGetBlobSize(thandle_t image)
571 return((toff_t) GetBlobSize((Image *) image));
574 static void TIFFGetProfiles(TIFF *tiff,Image *image,MagickBooleanType ping,
575 ExceptionInfo *exception)
584 if (ping == MagickFalse)
586 #if defined(TIFFTAG_ICCPROFILE)
587 if ((TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1) &&
588 (profile != (unsigned char *) NULL))
589 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
591 #if defined(TIFFTAG_PHOTOSHOP)
592 if ((TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1) &&
593 (profile != (unsigned char *) NULL))
594 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
596 #if defined(TIFFTAG_RICHTIFFIPTC)
597 if ((TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1) &&
598 (profile != (unsigned char *) NULL))
600 if (TIFFIsByteSwapped(tiff) != 0)
601 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
602 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
605 #if defined(TIFFTAG_XMLPACKET)
606 if ((TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1) &&
607 (profile != (unsigned char *) NULL))
608 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
610 if ((TIFFGetField(tiff,34118,&length,&profile) == 1) &&
611 (profile != (unsigned char *) NULL))
612 (void) ReadProfile(image,"tiff:34118",profile,(ssize_t) length,
615 if ((TIFFGetField(tiff,37724,&length,&profile) == 1) &&
616 (profile != (unsigned char *) NULL))
617 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
620 static void TIFFGetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
623 message[MagickPathExtent],
635 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
636 (void) SetImageProperty(image,"tiff:artist",text,exception);
637 if (TIFFGetField(tiff,TIFFTAG_COPYRIGHT,&text) == 1)
638 (void) SetImageProperty(image,"tiff:copyright",text,exception);
639 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
640 (void) SetImageProperty(image,"tiff:timestamp",text,exception);
641 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
642 (void) SetImageProperty(image,"tiff:document",text,exception);
643 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
644 (void) SetImageProperty(image,"tiff:hostcomputer",text,exception);
645 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
646 (void) SetImageProperty(image,"comment",text,exception);
647 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
648 (void) SetImageProperty(image,"tiff:make",text,exception);
649 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
650 (void) SetImageProperty(image,"tiff:model",text,exception);
651 if (TIFFGetField(tiff,TIFFTAG_OPIIMAGEID,&count,&text) == 1)
653 if (count >= MagickPathExtent)
654 count=MagickPathExtent-1;
655 (void) CopyMagickString(message,text,count+1);
656 (void) SetImageProperty(image,"tiff:image-id",message,exception);
658 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
659 (void) SetImageProperty(image,"label",text,exception);
660 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
661 (void) SetImageProperty(image,"tiff:software",text,exception);
662 if (TIFFGetField(tiff,33423,&count,&text) == 1)
664 if (count >= MagickPathExtent)
665 count=MagickPathExtent-1;
666 (void) CopyMagickString(message,text,count+1);
667 (void) SetImageProperty(image,"tiff:kodak-33423",message,exception);
669 if (TIFFGetField(tiff,36867,&count,&text) == 1)
671 if (count >= MagickPathExtent)
672 count=MagickPathExtent-1;
673 (void) CopyMagickString(message,text,count+1);
674 (void) SetImageProperty(image,"tiff:kodak-36867",message,exception);
676 if (TIFFGetField(tiff,TIFFTAG_SUBFILETYPE,&type) == 1)
681 (void) SetImageProperty(image,"tiff:subfiletype","REDUCEDIMAGE",
687 (void) SetImageProperty(image,"tiff:subfiletype","PAGE",exception);
692 (void) SetImageProperty(image,"tiff:subfiletype","MASK",exception);
698 if (TIFFGetField(tiff,37706,&length,&tietz) == 1)
700 (void) FormatLocaleString(message,MagickPathExtent,"%lu",tietz[0]);
701 (void) SetImageProperty(image,"tiff:tietz_offset",message,exception);
705 static void TIFFGetEXIFProperties(TIFF *tiff,Image *image,
706 ExceptionInfo *exception)
708 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
710 value[MagickPathExtent];
718 #if defined(TIFF_VERSION_BIG)
729 Read EXIF properties.
732 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) != 1)
734 directory=TIFFCurrentDirectory(tiff);
735 if (TIFFReadEXIFDirectory(tiff,offset) != 1)
737 TIFFSetDirectory(tiff,directory);
741 for (i=0; exif_info[i].tag != 0; i++)
744 switch (exif_info[i].type)
752 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans,&sans) == 1) &&
753 (ascii != (char *) NULL) && (*ascii != '\0'))
754 (void) CopyMagickString(value,ascii,MagickPathExtent);
759 if (exif_info[i].variable_length == 0)
765 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans,&sans) == 1)
766 (void) FormatLocaleString(value,MagickPathExtent,"%d",shorty);
779 tiff_status=TIFFGetField(tiff,exif_info[i].tag,&shorty_num,&shorty,
781 if (tiff_status == 1)
782 (void) FormatLocaleString(value,MagickPathExtent,"%d",
783 shorty_num != 0 ? shorty[0] : 0);
793 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans,&sans) == 1)
794 (void) FormatLocaleString(value,MagickPathExtent,"%d",longy);
797 #if defined(TIFF_VERSION_BIG)
804 if (TIFFGetField(tiff,exif_info[i].tag,&long8y,&sans,&sans) == 1)
805 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
806 ((MagickOffsetType) long8y));
818 if (TIFFGetField(tiff,exif_info[i].tag,&floaty,&sans,&sans) == 1)
819 (void) FormatLocaleString(value,MagickPathExtent,"%g",(double)
829 if (TIFFGetField(tiff,exif_info[i].tag,&doubley,&sans,&sans) == 1)
830 (void) FormatLocaleString(value,MagickPathExtent,"%g",doubley);
837 (void) SetImageProperty(image,exif_info[i].property,value,exception);
839 TIFFSetDirectory(tiff,directory);
846 static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
848 *base=(tdata_t *) GetBlobStreamData((Image *) image);
849 if (*base != (tdata_t *) NULL)
850 *size=(toff_t) GetBlobSize((Image *) image);
851 if (*base != (tdata_t *) NULL)
856 static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
861 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
862 (unsigned char *) data);
866 static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
867 tsample_t sample,ssize_t row,tdata_t scanline)
872 (void) bits_per_sample;
873 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
877 static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
879 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
882 static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
889 static void TIFFWarnings(const char *module,const char *format,va_list warning)
892 message[MagickPathExtent];
897 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
898 (void) vsnprintf(message,MagickPathExtent,format,warning);
900 (void) vsprintf(message,format,warning);
902 (void) ConcatenateMagickString(message,".",MagickPathExtent);
903 exception=(ExceptionInfo *) GetMagickThreadValue(tiff_exception);
904 if (exception != (ExceptionInfo *) NULL)
905 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
906 message,"`%s'",module);
909 static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
914 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
915 (unsigned char *) data);
919 static TIFFMethodType GetJPEGMethod(Image* image,TIFF *tiff,uint16 photometric,
920 uint16 bits_per_sample,uint16 samples_per_pixel)
922 #define BUFFER_SIZE 2048
934 #if defined(TIFF_VERSION_BIG)
942 buffer[BUFFER_SIZE+32];
947 /* only support 8 bit for now */
948 if ((photometric != PHOTOMETRIC_SEPARATED) || (bits_per_sample != 8) ||
949 (samples_per_pixel != 4))
950 return(ReadGenericMethod);
951 /* Search for Adobe APP14 JPEG Marker */
952 if (!TIFFGetField(tiff,TIFFTAG_STRIPOFFSETS,&value))
953 return(ReadRGBAMethod);
954 position=TellBlob(image);
955 offset=(MagickOffsetType) (value[0]);
956 if (SeekBlob(image,offset,SEEK_SET) != offset)
957 return(ReadRGBAMethod);
958 method=ReadRGBAMethod;
959 if (ReadBlob(image,BUFFER_SIZE,buffer) == BUFFER_SIZE)
961 for (i=0; i < BUFFER_SIZE; i++)
963 while (i < BUFFER_SIZE)
965 if (buffer[i++] == 255)
968 while (i < BUFFER_SIZE)
970 if (buffer[++i] != 255)
973 if (buffer[i++] == 216) /* JPEG_MARKER_SOI */
975 length=(unsigned short) (((unsigned int) (buffer[i] << 8) |
976 (unsigned int) buffer[i+1]) & 0xffff);
977 if (i+(size_t) length >= BUFFER_SIZE)
979 if (buffer[i-1] == 238) /* JPEG_MARKER_APP0+14 */
983 /* 0 == CMYK, 1 == YCbCr, 2 = YCCK */
984 if (buffer[i+13] == 2)
985 method=ReadYCCKMethod;
991 (void) SeekBlob(image,position,SEEK_SET);
995 static void TIFFReadPhotoshopLayers(Image* image,const ImageInfo *image_info,
996 ExceptionInfo *exception)
1013 if (GetImageListLength(image) != 1)
1015 if ((image_info->number_scenes == 1) && (image_info->scene == 0))
1017 option=GetImageOption(image_info,"tiff:ignore-layers");
1018 if (option != (const char * ) NULL)
1020 layer_info=GetImageProfile(image,"tiff:37724");
1021 if (layer_info == (const StringInfo *) NULL)
1023 for (i=0; i < (ssize_t) layer_info->length-8; i++)
1025 if (LocaleNCompare((const char *) (layer_info->datum+i),
1026 image->endian == MSBEndian ? "8BIM" : "MIB8",4) != 0)
1029 if ((LocaleNCompare((const char *) (layer_info->datum+i),
1030 image->endian == MSBEndian ? "Layr" : "ryaL",4) == 0) ||
1031 (LocaleNCompare((const char *) (layer_info->datum+i),
1032 image->endian == MSBEndian ? "LMsk" : "ksML",4) == 0) ||
1033 (LocaleNCompare((const char *) (layer_info->datum+i),
1034 image->endian == MSBEndian ? "Lr16" : "61rL",4) == 0) ||
1035 (LocaleNCompare((const char *) (layer_info->datum+i),
1036 image->endian == MSBEndian ? "Lr32" : "23rL",4) == 0))
1040 if (i >= (ssize_t) (layer_info->length-8))
1042 layers=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1043 (void) DeleteImageProfile(layers,"tiff:37724");
1044 AttachBlob(layers->blob,layer_info->datum,layer_info->length);
1045 SeekBlob(layers,(MagickOffsetType) i,SEEK_SET);
1047 info.columns=layers->columns;
1048 info.rows=layers->rows;
1049 info.channels=(unsigned short) layers->number_channels;
1050 /* Setting the mode to a value that won't change the colorspace */
1052 ReadPSDLayers(layers,image_info,&info,MagickFalse,exception);
1053 DeleteImageFromList(&layers);
1054 if (layers != (Image *) NULL)
1056 SetImageArtifact(image,"tiff:has-layers","true");
1057 AppendImageToList(&image,layers);
1058 while (layers != (Image *) NULL)
1060 SetImageArtifact(layers,"tiff:has-layers","true");
1061 DetachBlob(layers->blob);
1062 layers=GetNextImageInList(layers);
1067 #if defined(__cplusplus) || defined(c_plusplus)
1071 static Image *ReadTIFFImage(const ImageInfo *image_info,
1072 ExceptionInfo *exception)
1145 assert(image_info != (const ImageInfo *) NULL);
1146 assert(image_info->signature == MagickCoreSignature);
1147 if (image_info->debug != MagickFalse)
1148 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1149 image_info->filename);
1150 assert(exception != (ExceptionInfo *) NULL);
1151 assert(exception->signature == MagickCoreSignature);
1152 image=AcquireImage(image_info,exception);
1153 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1154 if (status == MagickFalse)
1156 image=DestroyImageList(image);
1157 return((Image *) NULL);
1159 (void) SetMagickThreadValue(tiff_exception,exception);
1160 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
1161 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
1163 if (tiff == (TIFF *) NULL)
1165 image=DestroyImageList(image);
1166 return((Image *) NULL);
1168 if (image_info->number_scenes != 0)
1171 Generate blank images for subimage specification (e.g. image.tif[4].
1172 We need to check the number of directores because it is possible that
1173 the subimage(s) are stored in the photoshop profile.
1175 if (image_info->scene < (size_t) TIFFNumberOfDirectories(tiff))
1177 for (i=0; i < (ssize_t) image_info->scene; i++)
1179 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1180 if (status == MagickFalse)
1183 image=DestroyImageList(image);
1184 return((Image *) NULL);
1186 AcquireNextImage(image_info,image,exception);
1187 if (GetNextImageInList(image) == (Image *) NULL)
1190 image=DestroyImageList(image);
1191 return((Image *) NULL);
1193 image=SyncNextImageInList(image);
1199 DisableMSCWarning(4127)
1200 if (0 && (image_info->verbose != MagickFalse))
1201 TIFFPrintDirectory(tiff,stdout,MagickFalse);
1203 if ((TIFFGetField(tiff,TIFFTAG_IMAGEWIDTH,&width) != 1) ||
1204 (TIFFGetField(tiff,TIFFTAG_IMAGELENGTH,&height) != 1) ||
1205 (TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag) != 1) ||
1206 (TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian) != 1) ||
1207 (TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace) != 1) ||
1208 (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,&samples_per_pixel) != 1) ||
1209 (TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample) != 1) ||
1210 (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format) != 1) ||
1211 (TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value) != 1) ||
1212 (TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value) != 1) ||
1213 (TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric) != 1))
1216 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1218 if (sample_format == SAMPLEFORMAT_IEEEFP)
1219 (void) SetImageProperty(image,"quantum:format","floating-point",
1221 switch (photometric)
1223 case PHOTOMETRIC_MINISBLACK:
1225 (void) SetImageProperty(image,"tiff:photometric","min-is-black",
1229 case PHOTOMETRIC_MINISWHITE:
1231 (void) SetImageProperty(image,"tiff:photometric","min-is-white",
1235 case PHOTOMETRIC_PALETTE:
1237 (void) SetImageProperty(image,"tiff:photometric","palette",exception);
1240 case PHOTOMETRIC_RGB:
1242 (void) SetImageProperty(image,"tiff:photometric","RGB",exception);
1245 case PHOTOMETRIC_CIELAB:
1247 (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception);
1250 case PHOTOMETRIC_LOGL:
1252 (void) SetImageProperty(image,"tiff:photometric","CIE Log2(L)",
1256 case PHOTOMETRIC_LOGLUV:
1258 (void) SetImageProperty(image,"tiff:photometric","LOGLUV",exception);
1261 #if defined(PHOTOMETRIC_MASK)
1262 case PHOTOMETRIC_MASK:
1264 (void) SetImageProperty(image,"tiff:photometric","MASK",exception);
1268 case PHOTOMETRIC_SEPARATED:
1270 (void) SetImageProperty(image,"tiff:photometric","separated",exception);
1273 case PHOTOMETRIC_YCBCR:
1275 (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception);
1280 (void) SetImageProperty(image,"tiff:photometric","unknown",exception);
1284 if (image->debug != MagickFalse)
1286 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
1287 (unsigned int) width,(unsigned int) height);
1288 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
1290 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1291 "Bits per sample: %u",bits_per_sample);
1292 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1293 "Min sample value: %u",min_sample_value);
1294 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1295 "Max sample value: %u",max_sample_value);
1296 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
1297 "interpretation: %s",GetImageProperty(image,"tiff:photometric",
1300 image->columns=(size_t) width;
1301 image->rows=(size_t) height;
1302 image->depth=(size_t) bits_per_sample;
1303 if (image->debug != MagickFalse)
1304 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
1305 (double) image->depth);
1306 image->endian=MSBEndian;
1307 if (endian == FILLORDER_LSB2MSB)
1308 image->endian=LSBEndian;
1309 #if defined(MAGICKCORE_HAVE_TIFFISBIGENDIAN)
1310 if (TIFFIsBigEndian(tiff) == 0)
1312 (void) SetImageProperty(image,"tiff:endian","lsb",exception);
1313 image->endian=LSBEndian;
1317 (void) SetImageProperty(image,"tiff:endian","msb",exception);
1318 image->endian=MSBEndian;
1321 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1322 (photometric == PHOTOMETRIC_MINISWHITE))
1323 SetImageColorspace(image,GRAYColorspace,exception);
1324 if (photometric == PHOTOMETRIC_SEPARATED)
1325 SetImageColorspace(image,CMYKColorspace,exception);
1326 if (photometric == PHOTOMETRIC_CIELAB)
1327 SetImageColorspace(image,LabColorspace,exception);
1328 TIFFGetProfiles(tiff,image,image_info->ping,exception);
1329 TIFFGetProperties(tiff,image,exception);
1330 option=GetImageOption(image_info,"tiff:exif-properties");
1331 if (IsStringFalse(option) == MagickFalse) /* enabled by default */
1332 TIFFGetEXIFProperties(tiff,image,exception);
1333 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
1334 &samples_per_pixel);
1335 if ((TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution) == 1) &&
1336 (TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution) == 1))
1338 image->resolution.x=x_resolution;
1339 image->resolution.y=y_resolution;
1341 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units) == 1)
1343 if (units == RESUNIT_INCH)
1344 image->units=PixelsPerInchResolution;
1345 if (units == RESUNIT_CENTIMETER)
1346 image->units=PixelsPerCentimeterResolution;
1348 if ((TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position) == 1) &&
1349 (TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position) == 1))
1351 image->page.x=(ssize_t) ceil(x_position*image->resolution.x-0.5);
1352 image->page.y=(ssize_t) ceil(y_position*image->resolution.y-0.5);
1354 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation) == 1)
1355 image->orientation=(OrientationType) orientation;
1356 if (TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity) == 1)
1358 if (chromaticity != (float *) NULL)
1360 image->chromaticity.white_point.x=chromaticity[0];
1361 image->chromaticity.white_point.y=chromaticity[1];
1364 if (TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity) == 1)
1366 if (chromaticity != (float *) NULL)
1368 image->chromaticity.red_primary.x=chromaticity[0];
1369 image->chromaticity.red_primary.y=chromaticity[1];
1370 image->chromaticity.green_primary.x=chromaticity[2];
1371 image->chromaticity.green_primary.y=chromaticity[3];
1372 image->chromaticity.blue_primary.x=chromaticity[4];
1373 image->chromaticity.blue_primary.y=chromaticity[5];
1376 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1377 if ((compress_tag != COMPRESSION_NONE) &&
1378 (TIFFIsCODECConfigured(compress_tag) == 0))
1381 ThrowReaderException(CoderError,"CompressNotSupported");
1384 switch (compress_tag)
1386 case COMPRESSION_NONE: image->compression=NoCompression; break;
1387 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1388 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1389 case COMPRESSION_JPEG:
1391 image->compression=JPEGCompression;
1392 #if defined(JPEG_SUPPORT)
1395 sampling_factor[MagickPathExtent];
1404 tiff_status=TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1405 &horizontal,&vertical);
1406 if (tiff_status == 1)
1408 (void) FormatLocaleString(sampling_factor,MagickPathExtent,
1409 "%dx%d",horizontal,vertical);
1410 (void) SetImageProperty(image,"jpeg:sampling-factor",
1411 sampling_factor,exception);
1412 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1413 "Sampling Factors: %s",sampling_factor);
1419 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1420 #if defined(COMPRESSION_LZMA)
1421 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1423 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1424 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1425 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1426 default: image->compression=RLECompression; break;
1429 Allocate memory for the image and pixel buffer.
1431 quantum_info=AcquireQuantumInfo(image_info,image);
1432 if (quantum_info == (QuantumInfo *) NULL)
1435 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1437 if (sample_format == SAMPLEFORMAT_UINT)
1438 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1439 if (sample_format == SAMPLEFORMAT_INT)
1440 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1441 if (sample_format == SAMPLEFORMAT_IEEEFP)
1442 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1443 if (status == MagickFalse)
1446 quantum_info=DestroyQuantumInfo(quantum_info);
1447 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1450 switch (photometric)
1452 case PHOTOMETRIC_MINISBLACK:
1454 quantum_info->min_is_white=MagickFalse;
1457 case PHOTOMETRIC_MINISWHITE:
1459 quantum_info->min_is_white=MagickTrue;
1465 tiff_status=TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1467 if (tiff_status == 1)
1469 (void) SetImageProperty(image,"tiff:alpha","unspecified",exception);
1470 if (extra_samples == 0)
1472 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1473 image->alpha_trait=BlendPixelTrait;
1476 for (i=0; i < extra_samples; i++)
1478 image->alpha_trait=BlendPixelTrait;
1479 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
1481 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
1482 (void) SetImageProperty(image,"tiff:alpha","associated",
1486 if (sample_info[i] == EXTRASAMPLE_UNASSALPHA)
1487 (void) SetImageProperty(image,"tiff:alpha","unassociated",
1491 if ((photometric == PHOTOMETRIC_PALETTE) &&
1492 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1497 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
1498 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
1501 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1504 value=(unsigned short) image->scene;
1505 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages) == 1)
1507 if (image->storage_class == PseudoClass)
1521 Initialize colormap.
1523 tiff_status=TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1524 &green_colormap,&blue_colormap);
1525 if (tiff_status == 1)
1527 if ((red_colormap != (uint16 *) NULL) &&
1528 (green_colormap != (uint16 *) NULL) &&
1529 (blue_colormap != (uint16 *) NULL))
1531 range=255; /* might be old style 8-bit colormap */
1532 for (i=0; i < (ssize_t) image->colors; i++)
1533 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1534 (blue_colormap[i] >= 256))
1539 for (i=0; i < (ssize_t) image->colors; i++)
1541 image->colormap[i].red=ClampToQuantum(((double)
1542 QuantumRange*red_colormap[i])/range);
1543 image->colormap[i].green=ClampToQuantum(((double)
1544 QuantumRange*green_colormap[i])/range);
1545 image->colormap[i].blue=ClampToQuantum(((double)
1546 QuantumRange*blue_colormap[i])/range);
1550 if (image->alpha_trait == UndefinedPixelTrait)
1551 image->depth=GetImageDepth(image,exception);
1553 if (image_info->ping != MagickFalse)
1555 if (image_info->number_scenes != 0)
1556 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1558 quantum_info=DestroyQuantumInfo(quantum_info);
1561 goto next_tiff_frame;
1563 status=SetImageExtent(image,image->columns,image->rows,exception);
1564 if (status == MagickFalse)
1565 return(DestroyImageList(image));
1566 method=ReadGenericMethod;
1567 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) == 1)
1570 value[MagickPathExtent];
1572 method=ReadStripMethod;
1573 (void) FormatLocaleString(value,MagickPathExtent,"%u",
1574 (unsigned int) rows_per_strip);
1575 (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception);
1577 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1578 method=ReadRGBAMethod;
1579 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1580 method=ReadCMYKAMethod;
1581 if ((photometric != PHOTOMETRIC_RGB) &&
1582 (photometric != PHOTOMETRIC_CIELAB) &&
1583 (photometric != PHOTOMETRIC_SEPARATED))
1584 method=ReadGenericMethod;
1585 if (image->storage_class == PseudoClass)
1586 method=ReadSingleSampleMethod;
1587 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1588 (photometric == PHOTOMETRIC_MINISWHITE))
1589 method=ReadSingleSampleMethod;
1590 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1591 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
1592 method=ReadGenericMethod;
1593 if (image->compression == JPEGCompression)
1594 method=GetJPEGMethod(image,tiff,photometric,bits_per_sample,
1596 if (compress_tag == COMPRESSION_JBIG)
1597 method=ReadStripMethod;
1598 if (TIFFIsTiled(tiff) != MagickFalse)
1599 method=ReadTileMethod;
1600 quantum_info->endian=LSBEndian;
1601 quantum_type=RGBQuantum;
1602 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1605 case ReadSingleSampleMethod:
1608 Convert TIFF image to PseudoClass MIFF image.
1610 quantum_type=IndexQuantum;
1611 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1612 if (image->alpha_trait != UndefinedPixelTrait)
1614 if (image->storage_class != PseudoClass)
1616 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1618 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1622 quantum_type=IndexAlphaQuantum;
1623 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1627 if (image->storage_class != PseudoClass)
1629 quantum_type=GrayQuantum;
1630 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1632 status=SetQuantumPad(image,quantum_info,pad*pow(2,ceil(log(
1633 bits_per_sample)/log(2))));
1634 if (status == MagickFalse)
1637 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1639 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1640 for (y=0; y < (ssize_t) image->rows; y++)
1648 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1651 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1652 if (q == (Quantum *) NULL)
1654 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1655 quantum_type,pixels,exception);
1656 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1658 if (image->previous == (Image *) NULL)
1660 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1662 if (status == MagickFalse)
1668 case ReadRGBAMethod:
1671 Convert TIFF image to DirectClass MIFF image.
1673 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1674 quantum_type=RGBQuantum;
1675 if (image->alpha_trait != UndefinedPixelTrait)
1677 quantum_type=RGBAQuantum;
1678 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1680 if (image->colorspace == CMYKColorspace)
1682 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1683 quantum_type=CMYKQuantum;
1684 if (image->alpha_trait != UndefinedPixelTrait)
1686 quantum_type=CMYKAQuantum;
1687 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1690 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1691 if (status == MagickFalse)
1694 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1696 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1697 for (y=0; y < (ssize_t) image->rows; y++)
1705 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1708 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1709 if (q == (Quantum *) NULL)
1711 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1712 quantum_type,pixels,exception);
1713 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1715 if (image->previous == (Image *) NULL)
1717 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1719 if (status == MagickFalse)
1725 case ReadCMYKAMethod:
1728 Convert TIFF image to DirectClass MIFF image.
1730 for (i=0; i < (ssize_t) samples_per_pixel; i++)
1732 for (y=0; y < (ssize_t) image->rows; y++)
1740 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1744 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1745 if (q == (Quantum *) NULL)
1747 if (image->colorspace != CMYKColorspace)
1750 case 0: quantum_type=RedQuantum; break;
1751 case 1: quantum_type=GreenQuantum; break;
1752 case 2: quantum_type=BlueQuantum; break;
1753 case 3: quantum_type=AlphaQuantum; break;
1754 default: quantum_type=UndefinedQuantum; break;
1759 case 0: quantum_type=CyanQuantum; break;
1760 case 1: quantum_type=MagentaQuantum; break;
1761 case 2: quantum_type=YellowQuantum; break;
1762 case 3: quantum_type=BlackQuantum; break;
1763 case 4: quantum_type=AlphaQuantum; break;
1764 default: quantum_type=UndefinedQuantum; break;
1766 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1767 quantum_type,pixels,exception);
1768 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1771 if (image->previous == (Image *) NULL)
1773 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1775 if (status == MagickFalse)
1781 case ReadYCCKMethod:
1783 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1784 for (y=0; y < (ssize_t) image->rows; y++)
1798 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1801 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1802 if (q == (Quantum *) NULL)
1805 for (x=0; x < (ssize_t) image->columns; x++)
1807 SetPixelCyan(image,ScaleCharToQuantum(ClampYCC((double) *p+
1808 (1.402*(double) *(p+2))-179.456)),q);
1809 SetPixelMagenta(image,ScaleCharToQuantum(ClampYCC((double) *p-
1810 (0.34414*(double) *(p+1))-(0.71414*(double ) *(p+2))+
1812 SetPixelYellow(image,ScaleCharToQuantum(ClampYCC((double) *p+
1813 (1.772*(double) *(p+1))-226.816)),q);
1814 SetPixelBlack(image,ScaleCharToQuantum((unsigned char) *(p+3)),q);
1815 q+=GetPixelChannels(image);
1818 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1820 if (image->previous == (Image *) NULL)
1822 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1824 if (status == MagickFalse)
1830 case ReadStripMethod:
1836 Convert stripped TIFF image to DirectClass MIFF image.
1840 for (y=0; y < (ssize_t) image->rows; y++)
1848 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1849 if (q == (Quantum *) NULL)
1853 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1855 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1859 p=((uint32 *) pixels)+image->columns*i;
1860 for (x=0; x < (ssize_t) image->columns; x++)
1862 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1864 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1866 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1868 if (image->alpha_trait != UndefinedPixelTrait)
1869 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1872 q+=GetPixelChannels(image);
1874 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1876 if (image->previous == (Image *) NULL)
1878 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1880 if (status == MagickFalse)
1886 case ReadTileMethod:
1897 Convert tiled TIFF image to DirectClass MIFF image.
1899 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) != 1) ||
1900 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) != 1))
1903 ThrowReaderException(CoderError,"ImageIsNotTiled");
1905 (void) SetImageStorageClass(image,DirectClass,exception);
1906 number_pixels=(MagickSizeType) columns*rows;
1907 if (HeapOverflowSanityCheck(rows,sizeof(*tile_pixels)) != MagickFalse)
1910 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1912 tile_pixels=(uint32 *) AcquireQuantumMemory(columns,rows*
1913 sizeof(*tile_pixels));
1914 if (tile_pixels == (uint32 *) NULL)
1917 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1919 for (y=0; y < (ssize_t) image->rows; y+=rows)
1926 *magick_restrict tile;
1932 rows_remaining=image->rows-y;
1933 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
1934 rows_remaining=rows;
1935 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1937 if (tile == (Quantum *) NULL)
1939 for (x=0; x < (ssize_t) image->columns; x+=columns)
1945 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1947 columns_remaining=image->columns-x;
1948 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
1949 columns_remaining=columns;
1950 p=tile_pixels+(rows-rows_remaining)*columns;
1951 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1953 for (row=rows_remaining; row > 0; row--)
1955 if (image->alpha_trait != UndefinedPixelTrait)
1956 for (column=columns_remaining; column > 0; column--)
1958 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1960 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1962 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1964 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1967 q+=GetPixelChannels(image);
1970 for (column=columns_remaining; column > 0; column--)
1972 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1974 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1976 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1979 q+=GetPixelChannels(image);
1981 p+=columns-columns_remaining;
1982 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
1985 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1987 if (image->previous == (Image *) NULL)
1989 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1991 if (status == MagickFalse)
1995 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1998 case ReadGenericMethod:
2011 Convert TIFF image to DirectClass MIFF image.
2013 number_pixels=(MagickSizeType) image->columns*image->rows;
2014 if (HeapOverflowSanityCheck(image->rows,sizeof(*pixels)) != MagickFalse)
2017 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2019 pixel_info=AcquireVirtualMemory(image->columns,image->rows*
2021 if (pixel_info == (MemoryInfo *) NULL)
2024 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2026 pixels=(uint32 *) GetVirtualMemoryBlob(pixel_info);
2027 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,(uint32)
2028 image->rows,(uint32 *) pixels,0);
2030 Convert image to DirectClass pixel packets.
2032 p=pixels+number_pixels-1;
2033 for (y=0; y < (ssize_t) image->rows; y++)
2041 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
2042 if (q == (Quantum *) NULL)
2044 q+=GetPixelChannels(image)*(image->columns-1);
2045 for (x=0; x < (ssize_t) image->columns; x++)
2047 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
2049 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
2051 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
2053 if (image->alpha_trait != UndefinedPixelTrait)
2054 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
2057 q-=GetPixelChannels(image);
2059 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2061 if (image->previous == (Image *) NULL)
2063 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
2065 if (status == MagickFalse)
2069 pixel_info=RelinquishVirtualMemory(pixel_info);
2073 SetQuantumImageType(image,quantum_type);
2075 quantum_info=DestroyQuantumInfo(quantum_info);
2076 if (photometric == PHOTOMETRIC_CIELAB)
2077 DecodeLabImage(image,exception);
2078 if ((photometric == PHOTOMETRIC_LOGL) ||
2079 (photometric == PHOTOMETRIC_MINISBLACK) ||
2080 (photometric == PHOTOMETRIC_MINISWHITE))
2082 image->type=GrayscaleType;
2083 if (bits_per_sample == 1)
2084 image->type=BilevelType;
2087 Proceed to next image.
2089 if (image_info->number_scenes != 0)
2090 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
2092 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
2093 if (status != MagickFalse)
2096 Allocate next image structure.
2098 AcquireNextImage(image_info,image,exception);
2099 if (GetNextImageInList(image) == (Image *) NULL)
2101 image=DestroyImageList(image);
2102 return((Image *) NULL);
2104 image=SyncNextImageInList(image);
2105 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
2107 if (status == MagickFalse)
2110 } while (status != MagickFalse);
2112 TIFFReadPhotoshopLayers(image,image_info,exception);
2113 if (image_info->number_scenes != 0)
2115 if (image_info->scene >= GetImageListLength(image))
2117 /* Subimage was not found in the Photoshop layer */
2118 image=DestroyImageList(image);
2119 return((Image *)NULL);
2122 return(GetFirstImageInList(image));
2127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2131 % R e g i s t e r T I F F I m a g e %
2135 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2137 % RegisterTIFFImage() adds properties for the TIFF image format to
2138 % the list of supported formats. The properties include the image format
2139 % tag, a method to read and/or write the format, whether the format
2140 % supports the saving of more than one frame to the same file or blob,
2141 % whether the format supports native in-memory I/O, and a brief
2142 % description of the format.
2144 % The format of the RegisterTIFFImage method is:
2146 % size_t RegisterTIFFImage(void)
2150 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2151 static TIFFExtendProc
2152 tag_extender = (TIFFExtendProc) NULL;
2154 static void TIFFIgnoreTags(TIFF *tiff)
2175 if (TIFFGetReadProc(tiff) != TIFFReadBlob)
2177 image=(Image *)TIFFClientdata(tiff);
2178 tags=GetImageArtifact(image,"tiff:ignore-tags");
2179 if (tags == (const char *) NULL)
2185 while ((isspace((int) ((unsigned char) *p)) != 0))
2188 (void) strtol(p,&q,10);
2195 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2202 ignore=(TIFFFieldInfo *) AcquireQuantumMemory(count,sizeof(*ignore));
2203 /* This also sets field_bit to 0 (FIELD_IGNORE) */
2204 ResetMagickMemory(ignore,0,count*sizeof(*ignore));
2207 while ((isspace((int) ((unsigned char) *p)) != 0))
2210 ignore[i].field_tag=(ttag_t) strtol(p,&q,10);
2215 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2218 (void) TIFFMergeFieldInfo(tiff,ignore,(uint32) count);
2219 ignore=(TIFFFieldInfo *) RelinquishMagickMemory(ignore);
2222 static void TIFFTagExtender(TIFF *tiff)
2224 static const TIFFFieldInfo
2227 { 37724, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
2228 (char *) "PhotoshopLayerData" },
2229 { 34118, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
2230 (char *) "Microscope" }
2233 TIFFMergeFieldInfo(tiff,TIFFExtensions,sizeof(TIFFExtensions)/
2234 sizeof(*TIFFExtensions));
2235 if (tag_extender != (TIFFExtendProc) NULL)
2236 (*tag_extender)(tiff);
2237 TIFFIgnoreTags(tiff);
2241 ModuleExport size_t RegisterTIFFImage(void)
2243 #define TIFFDescription "Tagged Image File Format"
2246 version[MagickPathExtent];
2251 if (tiff_semaphore == (SemaphoreInfo *) NULL)
2252 ActivateSemaphoreInfo(&tiff_semaphore);
2253 LockSemaphoreInfo(tiff_semaphore);
2254 if (instantiate_key == MagickFalse)
2256 if (CreateMagickThreadKey(&tiff_exception,NULL) == MagickFalse)
2257 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2258 error_handler=TIFFSetErrorHandler(TIFFErrors);
2259 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
2260 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2261 if (tag_extender == (TIFFExtendProc) NULL)
2262 tag_extender=TIFFSetTagExtender(TIFFTagExtender);
2264 instantiate_key=MagickTrue;
2266 UnlockSemaphoreInfo(tiff_semaphore);
2268 #if defined(TIFF_VERSION)
2269 (void) FormatLocaleString(version,MagickPathExtent,"%d",TIFF_VERSION);
2271 #if defined(MAGICKCORE_TIFF_DELEGATE)
2280 for (i=0; (i < (MagickPathExtent-1)) && (*p != 0) && (*p != '\n'); i++)
2286 entry=AcquireMagickInfo("TIFF","GROUP4","Raw CCITT Group4");
2287 #if defined(MAGICKCORE_TIFF_DELEGATE)
2288 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
2289 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
2291 entry->flags|=CoderRawSupportFlag;
2292 entry->flags|=CoderEndianSupportFlag;
2293 entry->flags|=CoderSeekableStreamFlag;
2294 entry->flags^=CoderAdjoinFlag;
2295 entry->flags^=CoderUseExtensionFlag;
2296 entry->format_type=ImplicitFormatType;
2297 entry->mime_type=ConstantString("image/tiff");
2298 (void) RegisterMagickInfo(entry);
2299 entry=AcquireMagickInfo("TIFF","PTIF","Pyramid encoded TIFF");
2300 #if defined(MAGICKCORE_TIFF_DELEGATE)
2301 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2302 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
2304 entry->flags|=CoderEndianSupportFlag;
2305 entry->flags|=CoderSeekableStreamFlag;
2306 entry->flags^=CoderUseExtensionFlag;
2307 entry->mime_type=ConstantString("image/tiff");
2308 (void) RegisterMagickInfo(entry);
2309 entry=AcquireMagickInfo("TIFF","TIF",TIFFDescription);
2310 #if defined(MAGICKCORE_TIFF_DELEGATE)
2311 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2312 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2314 entry->flags|=CoderEndianSupportFlag;
2315 entry->flags|=CoderSeekableStreamFlag;
2316 entry->flags|=CoderStealthFlag;
2317 entry->flags^=CoderUseExtensionFlag;
2318 if (*version != '\0')
2319 entry->version=ConstantString(version);
2320 entry->mime_type=ConstantString("image/tiff");
2321 (void) RegisterMagickInfo(entry);
2322 entry=AcquireMagickInfo("TIFF","TIFF",TIFFDescription);
2323 #if defined(MAGICKCORE_TIFF_DELEGATE)
2324 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2325 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2327 entry->magick=(IsImageFormatHandler *) IsTIFF;
2328 entry->flags|=CoderEndianSupportFlag;
2329 entry->flags|=CoderSeekableStreamFlag;
2330 entry->flags^=CoderUseExtensionFlag;
2331 if (*version != '\0')
2332 entry->version=ConstantString(version);
2333 entry->mime_type=ConstantString("image/tiff");
2334 (void) RegisterMagickInfo(entry);
2335 entry=AcquireMagickInfo("TIFF","TIFF64","Tagged Image File Format (64-bit)");
2336 #if defined(TIFF_VERSION_BIG)
2337 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2338 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2340 entry->flags|=CoderEndianSupportFlag;
2341 entry->flags|=CoderSeekableStreamFlag;
2342 entry->flags^=CoderAdjoinFlag;
2343 entry->flags^=CoderUseExtensionFlag;
2344 if (*version != '\0')
2345 entry->version=ConstantString(version);
2346 entry->mime_type=ConstantString("image/tiff");
2347 (void) RegisterMagickInfo(entry);
2348 return(MagickImageCoderSignature);
2352 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2356 % U n r e g i s t e r T I F F I m a g e %
2360 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2362 % UnregisterTIFFImage() removes format registrations made by the TIFF module
2363 % from the list of supported formats.
2365 % The format of the UnregisterTIFFImage method is:
2367 % UnregisterTIFFImage(void)
2370 ModuleExport void UnregisterTIFFImage(void)
2372 (void) UnregisterMagickInfo("TIFF64");
2373 (void) UnregisterMagickInfo("TIFF");
2374 (void) UnregisterMagickInfo("TIF");
2375 (void) UnregisterMagickInfo("PTIF");
2376 if (tiff_semaphore == (SemaphoreInfo *) NULL)
2377 ActivateSemaphoreInfo(&tiff_semaphore);
2378 LockSemaphoreInfo(tiff_semaphore);
2379 if (instantiate_key != MagickFalse)
2381 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2382 if (tag_extender == (TIFFExtendProc) NULL)
2383 (void) TIFFSetTagExtender(tag_extender);
2385 if (DeleteMagickThreadKey(tiff_exception) == MagickFalse)
2386 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2387 (void) TIFFSetWarningHandler(warning_handler);
2388 (void) TIFFSetErrorHandler(error_handler);
2389 instantiate_key=MagickFalse;
2391 UnlockSemaphoreInfo(tiff_semaphore);
2392 RelinquishSemaphoreInfo(&tiff_semaphore);
2395 #if defined(MAGICKCORE_TIFF_DELEGATE)
2397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2401 % W r i t e G R O U P 4 I m a g e %
2405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2407 % WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
2409 % The format of the WriteGROUP4Image method is:
2411 % MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2412 % Image *image,ExceptionInfo *)
2414 % A description of each parameter follows:
2416 % o image_info: the image info.
2418 % o image: The image.
2420 % o exception: return any errors or warnings in this structure.
2423 static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2424 Image *image,ExceptionInfo *exception)
2427 filename[MagickPathExtent];
2461 Write image as CCITT Group4 TIFF image to a temporary file.
2463 assert(image_info != (const ImageInfo *) NULL);
2464 assert(image_info->signature == MagickCoreSignature);
2465 assert(image != (Image *) NULL);
2466 assert(image->signature == MagickCoreSignature);
2467 if (image->debug != MagickFalse)
2468 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2469 assert(exception != (ExceptionInfo *) NULL);
2470 assert(exception->signature == MagickCoreSignature);
2471 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2472 if (status == MagickFalse)
2474 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
2475 if (huffman_image == (Image *) NULL)
2477 (void) CloseBlob(image);
2478 return(MagickFalse);
2480 huffman_image->endian=MSBEndian;
2482 unique_file=AcquireUniqueFileResource(filename);
2483 if (unique_file != -1)
2484 file=fdopen(unique_file,"wb");
2485 if ((unique_file == -1) || (file == (FILE *) NULL))
2487 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2489 return(MagickFalse);
2491 (void) FormatLocaleString(huffman_image->filename,MagickPathExtent,"tiff:%s",
2493 (void) SetImageType(huffman_image,BilevelType,exception);
2494 write_info=CloneImageInfo((ImageInfo *) NULL);
2495 SetImageInfoFile(write_info,file);
2496 (void) SetImageDepth(image,1,exception);
2497 (void) SetImageType(image,BilevelType,exception);
2498 write_info->compression=Group4Compression;
2499 write_info->type=BilevelType;
2500 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
2501 status=WriteTIFFImage(write_info,huffman_image,exception);
2502 (void) fflush(file);
2503 write_info=DestroyImageInfo(write_info);
2504 if (status == MagickFalse)
2506 huffman_image=DestroyImage(huffman_image);
2507 (void) fclose(file);
2508 (void) RelinquishUniqueFileResource(filename);
2509 return(MagickFalse);
2511 tiff=TIFFOpen(filename,"rb");
2512 if (tiff == (TIFF *) NULL)
2514 huffman_image=DestroyImage(huffman_image);
2515 (void) fclose(file);
2516 (void) RelinquishUniqueFileResource(filename);
2517 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
2518 image_info->filename);
2519 return(MagickFalse);
2522 Allocate raw strip buffer.
2524 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2527 huffman_image=DestroyImage(huffman_image);
2528 (void) fclose(file);
2529 (void) RelinquishUniqueFileResource(filename);
2530 return(MagickFalse);
2532 strip_size=byte_count[0];
2533 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2534 if (byte_count[i] > strip_size)
2535 strip_size=byte_count[i];
2536 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2538 if (buffer == (unsigned char *) NULL)
2541 huffman_image=DestroyImage(huffman_image);
2542 (void) fclose(file);
2543 (void) RelinquishUniqueFileResource(filename);
2544 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2545 image_info->filename);
2548 Compress runlength encoded to 2D Huffman pixels.
2550 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2552 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
2553 if (WriteBlob(image,(size_t) count,buffer) != count)
2556 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2558 huffman_image=DestroyImage(huffman_image);
2559 (void) fclose(file);
2560 (void) RelinquishUniqueFileResource(filename);
2561 (void) CloseBlob(image);
2566 #if defined(MAGICKCORE_TIFF_DELEGATE)
2568 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2572 % W r i t e P T I F I m a g e %
2576 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2578 % WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2581 % The format of the WritePTIFImage method is:
2583 % MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2584 % Image *image,ExceptionInfo *exception)
2586 % A description of each parameter follows:
2588 % o image_info: the image info.
2590 % o image: The image.
2592 % o exception: return any errors or warnings in this structure.
2595 static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2596 Image *image,ExceptionInfo *exception)
2617 Create pyramid-encoded TIFF image.
2619 images=NewImageList();
2620 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2625 clone_image=CloneImage(next,0,0,MagickFalse,exception);
2626 if (clone_image == (Image *) NULL)
2628 clone_image->previous=NewImageList();
2629 clone_image->next=NewImageList();
2630 (void) SetImageProperty(clone_image,"tiff:subfiletype","none",exception);
2631 AppendImageToList(&images,clone_image);
2632 columns=next->columns;
2634 resolution=next->resolution;
2635 while ((columns > 64) && (rows > 64))
2641 pyramid_image=ResizeImage(next,columns,rows,image->filter,exception);
2642 if (pyramid_image == (Image *) NULL)
2644 pyramid_image->resolution=resolution;
2645 (void) SetImageProperty(pyramid_image,"tiff:subfiletype","REDUCEDIMAGE",
2647 AppendImageToList(&images,pyramid_image);
2650 images=GetFirstImageInList(images);
2652 Write pyramid-encoded TIFF image.
2654 write_info=CloneImageInfo(image_info);
2655 write_info->adjoin=MagickTrue;
2656 (void) CopyMagickString(write_info->magick,"TIFF",MagickPathExtent);
2657 (void) CopyMagickString(images->magick,"TIFF",MagickPathExtent);
2658 status=WriteTIFFImage(write_info,images,exception);
2659 images=DestroyImageList(images);
2660 write_info=DestroyImageInfo(write_info);
2665 #if defined(MAGICKCORE_TIFF_DELEGATE)
2667 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2670 % W r i t e T I F F I m a g e %
2674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2676 % WriteTIFFImage() writes an image in the Tagged image file format.
2678 % The format of the WriteTIFFImage method is:
2680 % MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2681 % Image *image,ExceptionInfo *exception)
2683 % A description of each parameter follows:
2685 % o image_info: the image info.
2687 % o image: The image.
2689 % o exception: return any errors or warnings in this structure.
2693 typedef struct _TIFFInfo
2704 static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2706 assert(tiff_info != (TIFFInfo *) NULL);
2707 if (tiff_info->scanlines != (unsigned char *) NULL)
2708 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2709 tiff_info->scanlines);
2710 if (tiff_info->pixels != (unsigned char *) NULL)
2711 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2715 static MagickBooleanType EncodeLabImage(Image *image,ExceptionInfo *exception)
2727 image_view=AcquireAuthenticCacheView(image,exception);
2728 for (y=0; y < (ssize_t) image->rows; y++)
2736 if (status == MagickFalse)
2738 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2739 if (q == (Quantum *) NULL)
2744 for (x=0; x < (ssize_t) image->columns; x++)
2750 a=QuantumScale*GetPixela(image,q)-0.5;
2753 b=QuantumScale*GetPixelb(image,q)-0.5;
2756 SetPixela(image,QuantumRange*a,q);
2757 SetPixelb(image,QuantumRange*b,q);
2758 q+=GetPixelChannels(image);
2760 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2763 image_view=DestroyCacheView(image_view);
2767 static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,
2768 TIFF *tiff,TIFFInfo *tiff_info)
2780 assert(tiff_info != (TIFFInfo *) NULL);
2781 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2782 option=GetImageOption(image_info,"tiff:tile-geometry");
2783 if (option == (const char *) NULL)
2785 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2786 if ((flags & HeightValue) == 0)
2787 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2788 tile_columns=(uint32) tiff_info->tile_geometry.width;
2789 tile_rows=(uint32) tiff_info->tile_geometry.height;
2790 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2791 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2792 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2793 tiff_info->tile_geometry.width=tile_columns;
2794 tiff_info->tile_geometry.height=tile_rows;
2795 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2796 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
2797 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2798 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2799 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2800 (tiff_info->pixels == (unsigned char *) NULL))
2802 DestroyTIFFInfo(tiff_info);
2803 return(MagickFalse);
2808 static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
2809 tsample_t sample,Image *image)
2817 register unsigned char
2831 if (TIFFIsTiled(tiff) == 0)
2832 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2834 Fill scanlines to tile height.
2836 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2837 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2838 (size_t) TIFFScanlineSize(tiff));
2839 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2840 (tiff_info->tile_geometry.height-1)) &&
2841 (row != (ssize_t) (image->rows-1)))
2844 Write tile to TIFF image.
2847 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (
2848 tiff_info->tile_geometry.height*tiff_info->tile_geometry.width);
2849 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2850 tiff_info->tile_geometry.width;
2851 for (i=0; i < (ssize_t) number_tiles; i++)
2853 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
2854 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2855 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2856 for (k=0; k < (ssize_t) tile_width; k++)
2858 if (bytes_per_pixel == 0)
2860 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2861 tiff_info->tile_geometry.width+k)/8);
2862 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2866 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2867 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2868 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2869 for (l=0; l < bytes_per_pixel; l++)
2872 if ((i*tiff_info->tile_geometry.width) != image->columns)
2873 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2874 tiff_info->tile_geometry.width),(uint32) ((row/
2875 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2883 static void TIFFSetProfiles(TIFF *tiff,Image *image)
2891 if (image->profiles == (void *) NULL)
2893 ResetImageProfileIterator(image);
2894 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2896 profile=GetImageProfile(image,name);
2897 if (GetStringInfoLength(profile) == 0)
2899 name=GetNextImageProfile(image);
2902 #if defined(TIFFTAG_XMLPACKET)
2903 if (LocaleCompare(name,"xmp") == 0)
2904 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2905 profile),GetStringInfoDatum(profile));
2907 #if defined(TIFFTAG_ICCPROFILE)
2908 if (LocaleCompare(name,"icc") == 0)
2909 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2910 profile),GetStringInfoDatum(profile));
2912 if (LocaleCompare(name,"iptc") == 0)
2920 iptc_profile=CloneStringInfo(profile);
2921 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2923 SetStringInfoLength(iptc_profile,length);
2924 if (TIFFIsByteSwapped(tiff))
2925 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2926 (unsigned long) (length/4));
2927 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2928 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2929 iptc_profile=DestroyStringInfo(iptc_profile);
2931 #if defined(TIFFTAG_PHOTOSHOP)
2932 if (LocaleCompare(name,"8bim") == 0)
2933 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2934 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2936 if (LocaleCompare(name,"tiff:37724") == 0)
2937 (void) TIFFSetField(tiff,37724,(uint32) GetStringInfoLength(profile),
2938 GetStringInfoDatum(profile));
2939 if (LocaleCompare(name,"tiff:34118") == 0)
2940 (void) TIFFSetField(tiff,34118,(uint32) GetStringInfoLength(profile),
2941 GetStringInfoDatum(profile));
2942 name=GetNextImageProfile(image);
2946 static void TIFFSetProperties(TIFF *tiff,const ImageInfo *image_info,
2947 Image *image,ExceptionInfo *exception)
2952 value=GetImageArtifact(image,"tiff:document");
2953 if (value != (const char *) NULL)
2954 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,value);
2955 value=GetImageArtifact(image,"tiff:hostcomputer");
2956 if (value != (const char *) NULL)
2957 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2958 value=GetImageArtifact(image,"tiff:artist");
2959 if (value != (const char *) NULL)
2960 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2961 value=GetImageArtifact(image,"tiff:timestamp");
2962 if (value != (const char *) NULL)
2963 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2964 value=GetImageArtifact(image,"tiff:make");
2965 if (value != (const char *) NULL)
2966 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2967 value=GetImageArtifact(image,"tiff:model");
2968 if (value != (const char *) NULL)
2969 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2970 value=GetImageArtifact(image,"tiff:software");
2971 if (value != (const char *) NULL)
2972 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
2973 value=GetImageArtifact(image,"tiff:copyright");
2974 if (value != (const char *) NULL)
2975 (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value);
2976 value=GetImageArtifact(image,"kodak-33423");
2977 if (value != (const char *) NULL)
2978 (void) TIFFSetField(tiff,33423,value);
2979 value=GetImageArtifact(image,"kodak-36867");
2980 if (value != (const char *) NULL)
2981 (void) TIFFSetField(tiff,36867,value);
2982 value=GetImageProperty(image,"label",exception);
2983 if (value != (const char *) NULL)
2984 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2985 value=GetImageProperty(image,"comment",exception);
2986 if (value != (const char *) NULL)
2987 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2988 value=GetImageArtifact(image,"tiff:subfiletype");
2989 if (value != (const char *) NULL)
2991 if (LocaleCompare(value,"REDUCEDIMAGE") == 0)
2992 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2994 if (LocaleCompare(value,"PAGE") == 0)
2995 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2997 if (LocaleCompare(value,"MASK") == 0)
2998 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_MASK);
3006 page=(uint16) image->scene;
3007 pages=(uint16) GetImageListLength(image);
3008 if ((image_info->adjoin != MagickFalse) && (pages > 1))
3009 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3010 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3014 static void TIFFSetEXIFProperties(TIFF *tiff,Image *image,
3015 ExceptionInfo *exception)
3017 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
3028 Write EXIF properties.
3031 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
3032 for (i=0; exif_info[i].tag != 0; i++)
3034 value=GetImageProperty(image,exif_info[i].property,exception);
3035 if (value == (const char *) NULL)
3037 switch (exif_info[i].type)
3041 (void) TIFFSetField(tiff,exif_info[i].tag,value);
3049 field=(uint16) StringToLong(value);
3050 (void) TIFFSetField(tiff,exif_info[i].tag,field);
3058 field=(uint16) StringToLong(value);
3059 (void) TIFFSetField(tiff,exif_info[i].tag,field);
3063 case TIFF_SRATIONAL:
3068 field=StringToDouble(value,(char **) NULL);
3069 (void) TIFFSetField(tiff,exif_info[i].tag,field);
3076 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
3083 static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
3084 Image *image,ExceptionInfo *exception)
3086 #if !defined(TIFFDefaultStripSize)
3087 #define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
3143 assert(image_info != (const ImageInfo *) NULL);
3144 assert(image_info->signature == MagickCoreSignature);
3145 assert(image != (Image *) NULL);
3146 assert(image->signature == MagickCoreSignature);
3147 if (image->debug != MagickFalse)
3148 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3149 assert(exception != (ExceptionInfo *) NULL);
3150 assert(exception->signature == MagickCoreSignature);
3151 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
3152 if (status == MagickFalse)
3154 (void) SetMagickThreadValue(tiff_exception,exception);
3155 endian_type=UndefinedEndian;
3156 option=GetImageOption(image_info,"tiff:endian");
3157 if (option != (const char *) NULL)
3159 if (LocaleNCompare(option,"msb",3) == 0)
3160 endian_type=MSBEndian;
3161 if (LocaleNCompare(option,"lsb",3) == 0)
3162 endian_type=LSBEndian;;
3164 switch (endian_type)
3166 case LSBEndian: mode="wl"; break;
3167 case MSBEndian: mode="wb"; break;
3168 default: mode="w"; break;
3170 #if defined(TIFF_VERSION_BIG)
3171 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
3172 switch (endian_type)
3174 case LSBEndian: mode="wl8"; break;
3175 case MSBEndian: mode="wb8"; break;
3176 default: mode="w8"; break;
3179 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
3180 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
3182 if (tiff == (TIFF *) NULL)
3183 return(MagickFalse);
3185 debug=IsEventLogging();
3190 Initialize TIFF fields.
3192 if ((image_info->type != UndefinedType) &&
3193 (image_info->type != OptimizeType))
3194 (void) SetImageType(image,image_info->type,exception);
3195 compression=UndefinedCompression;
3196 if (image->compression != JPEGCompression)
3197 compression=image->compression;
3198 if (image_info->compression != UndefinedCompression)
3199 compression=image_info->compression;
3200 switch (compression)
3202 case FaxCompression:
3203 case Group4Compression:
3205 (void) SetImageType(image,BilevelType,exception);
3206 (void) SetImageDepth(image,1,exception);
3209 case JPEGCompression:
3211 (void) SetImageStorageClass(image,DirectClass,exception);
3212 (void) SetImageDepth(image,8,exception);
3218 quantum_info=AcquireQuantumInfo(image_info,image);
3219 if (quantum_info == (QuantumInfo *) NULL)
3220 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3221 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
3222 (quantum_info->format == UndefinedQuantumFormat) &&
3223 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
3225 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
3226 if (status == MagickFalse)
3227 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3229 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
3230 (GetPreviousImageInList(image) != (Image *) NULL))
3231 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
3232 if ((image->columns != (uint32) image->columns) ||
3233 (image->rows != (uint32) image->rows))
3234 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
3235 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
3236 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
3237 switch (compression)
3239 case FaxCompression:
3241 compress_tag=COMPRESSION_CCITTFAX3;
3242 SetQuantumMinIsWhite(quantum_info,MagickTrue);
3245 case Group4Compression:
3247 compress_tag=COMPRESSION_CCITTFAX4;
3248 SetQuantumMinIsWhite(quantum_info,MagickTrue);
3251 #if defined(COMPRESSION_JBIG)
3252 case JBIG1Compression:
3254 compress_tag=COMPRESSION_JBIG;
3258 case JPEGCompression:
3260 compress_tag=COMPRESSION_JPEG;
3263 #if defined(COMPRESSION_LZMA)
3264 case LZMACompression:
3266 compress_tag=COMPRESSION_LZMA;
3270 case LZWCompression:
3272 compress_tag=COMPRESSION_LZW;
3275 case RLECompression:
3277 compress_tag=COMPRESSION_PACKBITS;
3280 case ZipCompression:
3282 compress_tag=COMPRESSION_ADOBE_DEFLATE;
3288 compress_tag=COMPRESSION_NONE;
3292 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
3293 if ((compress_tag != COMPRESSION_NONE) &&
3294 (TIFFIsCODECConfigured(compress_tag) == 0))
3296 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3297 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
3298 MagickCompressOptions,(ssize_t) compression));
3299 compress_tag=COMPRESSION_NONE;
3300 compression=NoCompression;
3303 switch (compress_tag)
3305 #if defined(CCITT_SUPPORT)
3306 case COMPRESSION_CCITTFAX3:
3307 case COMPRESSION_CCITTFAX4:
3309 #if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
3310 case COMPRESSION_JPEG:
3312 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3313 case COMPRESSION_LZMA:
3315 #if defined(LZW_SUPPORT)
3316 case COMPRESSION_LZW:
3318 #if defined(PACKBITS_SUPPORT)
3319 case COMPRESSION_PACKBITS:
3321 #if defined(ZIP_SUPPORT)
3322 case COMPRESSION_ADOBE_DEFLATE:
3324 case COMPRESSION_NONE:
3328 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3329 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
3330 MagickCompressOptions,(ssize_t) compression));
3331 compress_tag=COMPRESSION_NONE;
3332 compression=NoCompression;
3337 if (image->colorspace == CMYKColorspace)
3339 photometric=PHOTOMETRIC_SEPARATED;
3340 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
3341 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
3346 Full color TIFF raster.
3348 if (image->colorspace == LabColorspace)
3350 photometric=PHOTOMETRIC_CIELAB;
3351 EncodeLabImage(image,exception);
3354 if (image->colorspace == YCbCrColorspace)
3356 photometric=PHOTOMETRIC_YCBCR;
3357 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
3358 (void) SetImageStorageClass(image,DirectClass,exception);
3359 (void) SetImageDepth(image,8,exception);
3362 photometric=PHOTOMETRIC_RGB;
3363 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
3364 if ((image_info->type != TrueColorType) &&
3365 (image_info->type != TrueColorAlphaType))
3367 if ((image_info->type != PaletteType) &&
3368 (SetImageGray(image,exception) != MagickFalse))
3370 photometric=(uint16) (quantum_info->min_is_white !=
3371 MagickFalse ? PHOTOMETRIC_MINISWHITE :
3372 PHOTOMETRIC_MINISBLACK);
3373 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
3374 if ((image->depth == 1) &&
3375 (image->alpha_trait == UndefinedPixelTrait))
3376 SetImageMonochrome(image,exception);
3379 if (image->storage_class == PseudoClass)
3385 Colormapped TIFF raster.
3387 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
3388 photometric=PHOTOMETRIC_PALETTE;
3390 while ((GetQuantumRange(depth)+1) < image->colors)
3392 status=SetQuantumDepth(image,quantum_info,depth);
3393 if (status == MagickFalse)
3394 ThrowWriterException(ResourceLimitError,
3395 "MemoryAllocationFailed");
3399 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
3400 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
3401 (photometric != PHOTOMETRIC_MINISWHITE))
3403 compress_tag=COMPRESSION_NONE;
3404 endian=FILLORDER_MSB2LSB;
3407 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
3408 (photometric != PHOTOMETRIC_MINISWHITE))
3410 compress_tag=COMPRESSION_NONE;
3411 endian=FILLORDER_MSB2LSB;
3413 option=GetImageOption(image_info,"tiff:fill-order");
3414 if (option != (const char *) NULL)
3416 if (LocaleNCompare(option,"msb",3) == 0)
3417 endian=FILLORDER_MSB2LSB;
3418 if (LocaleNCompare(option,"lsb",3) == 0)
3419 endian=FILLORDER_LSB2MSB;
3421 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
3422 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
3423 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
3424 if (image->alpha_trait != UndefinedPixelTrait)
3432 TIFF has a matte channel.
3435 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
3436 option=GetImageOption(image_info,"tiff:alpha");
3437 if (option != (const char *) NULL)
3439 if (LocaleCompare(option,"associated") == 0)
3440 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
3442 if (LocaleCompare(option,"unspecified") == 0)
3443 sample_info[0]=EXTRASAMPLE_UNSPECIFIED;
3445 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
3446 &samples_per_pixel);
3447 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
3448 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
3450 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
3451 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
3453 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
3454 switch (quantum_info->format)
3456 case FloatingPointQuantumFormat:
3458 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
3459 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
3460 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
3463 case SignedQuantumFormat:
3465 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
3468 case UnsignedQuantumFormat:
3470 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
3476 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
3477 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
3478 if (photometric == PHOTOMETRIC_RGB)
3479 if ((image_info->interlace == PlaneInterlace) ||
3480 (image_info->interlace == PartitionInterlace))
3481 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
3483 if (TIFFScanlineSize(tiff) != 0)
3484 rows_per_strip=TIFFDefaultStripSize(tiff,0);
3485 option=GetImageOption(image_info,"tiff:rows-per-strip");
3486 if (option != (const char *) NULL)
3487 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
3488 switch (compress_tag)
3490 case COMPRESSION_JPEG:
3492 #if defined(JPEG_SUPPORT)
3502 rows_per_strip+=(16-(rows_per_strip % 16));
3503 if (image_info->quality != UndefinedCompressionQuality)
3504 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
3505 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
3506 if (IssRGBCompatibleColorspace(image->colorspace) != MagickFalse)
3511 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
3512 sampling_factor=(const char *) NULL;
3513 value=GetImageProperty(image,"jpeg:sampling-factor",exception);
3514 if (value != (char *) NULL)
3516 sampling_factor=value;
3517 if (image->debug != MagickFalse)
3518 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3519 " Input sampling-factors=%s",sampling_factor);
3521 if (image_info->sampling_factor != (char *) NULL)
3522 sampling_factor=image_info->sampling_factor;
3523 if (sampling_factor != (const char *) NULL)
3525 flags=ParseGeometry(sampling_factor,&geometry_info);
3526 if ((flags & SigmaValue) == 0)
3527 geometry_info.sigma=geometry_info.rho;
3528 if (image->colorspace == YCbCrColorspace)
3529 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
3530 geometry_info.rho,(uint16) geometry_info.sigma);
3533 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3535 if (bits_per_sample == 12)
3536 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
3540 case COMPRESSION_ADOBE_DEFLATE:
3542 rows_per_strip=(uint32) image->rows;
3543 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3545 if (((photometric == PHOTOMETRIC_RGB) ||
3546 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3547 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3548 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3549 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
3550 image_info->quality == UndefinedCompressionQuality ? 7 :
3551 MagickMin((ssize_t) image_info->quality/10,9)));
3554 case COMPRESSION_CCITTFAX3:
3559 rows_per_strip=(uint32) image->rows;
3560 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
3563 case COMPRESSION_CCITTFAX4:
3565 rows_per_strip=(uint32) image->rows;
3568 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3569 case COMPRESSION_LZMA:
3571 if (((photometric == PHOTOMETRIC_RGB) ||
3572 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3573 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3574 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3575 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
3576 image_info->quality == UndefinedCompressionQuality ? 7 :
3577 MagickMin((ssize_t) image_info->quality/10,9)));
3581 case COMPRESSION_LZW:
3583 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3585 if (((photometric == PHOTOMETRIC_RGB) ||
3586 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3587 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3588 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3594 if (rows_per_strip < 1)
3596 if ((image->rows/rows_per_strip) >= (1UL << 15))
3597 rows_per_strip=(uint32) (image->rows >> 15);
3598 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
3599 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
3605 Set image resolution.
3608 if (image->units == PixelsPerInchResolution)
3610 if (image->units == PixelsPerCentimeterResolution)
3611 units=RESUNIT_CENTIMETER;
3612 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
3613 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x);
3614 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y);
3615 if ((image->page.x < 0) || (image->page.y < 0))
3616 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3617 "TIFF: negative image positions unsupported","%s",image->filename);
3618 if ((image->page.x > 0) && (image->resolution.x > 0.0))
3621 Set horizontal image position.
3623 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
3624 image->resolution.x);
3626 if ((image->page.y > 0) && (image->resolution.y > 0.0))
3629 Set vertical image position.
3631 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
3632 image->resolution.y);
3635 if (image->chromaticity.white_point.x != 0.0)
3641 Set image chromaticity.
3643 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3644 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3645 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3646 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3647 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3648 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3649 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3650 chromaticity[0]=(float) image->chromaticity.white_point.x;
3651 chromaticity[1]=(float) image->chromaticity.white_point.y;
3652 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3654 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3655 (image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3657 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3658 if (image->scene != 0)
3659 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3660 GetImageListLength(image));
3662 if (image->orientation != UndefinedOrientation)
3663 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3664 (void) TIFFSetProfiles(tiff,image);
3670 page=(uint16) scene;
3671 pages=(uint16) GetImageListLength(image);
3672 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3673 (image_info->adjoin != MagickFalse) && (pages > 1))
3674 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3675 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3677 (void) TIFFSetProperties(tiff,image_info,image,exception);
3678 DisableMSCWarning(4127)
3681 (void) TIFFSetEXIFProperties(tiff,image,exception);
3683 Write image scanlines.
3685 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3686 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3687 quantum_info->endian=LSBEndian;
3688 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
3689 tiff_info.scanline=(unsigned char *) GetQuantumPixels(quantum_info);
3690 switch (photometric)
3692 case PHOTOMETRIC_CIELAB:
3693 case PHOTOMETRIC_YCBCR:
3694 case PHOTOMETRIC_RGB:
3699 switch (image_info->interlace)
3704 quantum_type=RGBQuantum;
3705 if (image->alpha_trait != UndefinedPixelTrait)
3706 quantum_type=RGBAQuantum;
3707 for (y=0; y < (ssize_t) image->rows; y++)
3709 register const Quantum
3712 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3713 if (p == (const Quantum *) NULL)
3715 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3716 quantum_type,pixels,exception);
3718 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3720 if (image->previous == (Image *) NULL)
3722 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3724 if (status == MagickFalse)
3730 case PlaneInterlace:
3731 case PartitionInterlace:
3734 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3736 for (y=0; y < (ssize_t) image->rows; y++)
3738 register const Quantum
3741 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3742 if (p == (const Quantum *) NULL)
3744 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3745 RedQuantum,pixels,exception);
3746 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3749 if (image->previous == (Image *) NULL)
3751 status=SetImageProgress(image,SaveImageTag,100,400);
3752 if (status == MagickFalse)
3755 for (y=0; y < (ssize_t) image->rows; y++)
3757 register const Quantum
3760 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3761 if (p == (const Quantum *) NULL)
3763 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3764 GreenQuantum,pixels,exception);
3765 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3768 if (image->previous == (Image *) NULL)
3770 status=SetImageProgress(image,SaveImageTag,200,400);
3771 if (status == MagickFalse)
3774 for (y=0; y < (ssize_t) image->rows; y++)
3776 register const Quantum
3779 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3780 if (p == (const Quantum *) NULL)
3782 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3783 BlueQuantum,pixels,exception);
3784 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3787 if (image->previous == (Image *) NULL)
3789 status=SetImageProgress(image,SaveImageTag,300,400);
3790 if (status == MagickFalse)
3793 if (image->alpha_trait != UndefinedPixelTrait)
3794 for (y=0; y < (ssize_t) image->rows; y++)
3796 register const Quantum
3799 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3800 if (p == (const Quantum *) NULL)
3802 length=ExportQuantumPixels(image,(CacheView *) NULL,
3803 quantum_info,AlphaQuantum,pixels,exception);
3804 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3807 if (image->previous == (Image *) NULL)
3809 status=SetImageProgress(image,SaveImageTag,400,400);
3810 if (status == MagickFalse)
3818 case PHOTOMETRIC_SEPARATED:
3823 quantum_type=CMYKQuantum;
3824 if (image->alpha_trait != UndefinedPixelTrait)
3825 quantum_type=CMYKAQuantum;
3826 if (image->colorspace != CMYKColorspace)
3827 (void) TransformImageColorspace(image,CMYKColorspace,exception);
3828 for (y=0; y < (ssize_t) image->rows; y++)
3830 register const Quantum
3833 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3834 if (p == (const Quantum *) NULL)
3836 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3837 quantum_type,pixels,exception);
3838 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3840 if (image->previous == (Image *) NULL)
3842 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3844 if (status == MagickFalse)
3850 case PHOTOMETRIC_PALETTE:
3858 Colormapped TIFF image.
3860 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3861 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3862 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3863 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3864 (blue == (uint16 *) NULL))
3865 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3867 Initialize TIFF colormap.
3869 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3870 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3871 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3872 for (i=0; i < (ssize_t) image->colors; i++)
3874 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3875 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3876 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3878 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3879 red=(uint16 *) RelinquishMagickMemory(red);
3880 green=(uint16 *) RelinquishMagickMemory(green);
3881 blue=(uint16 *) RelinquishMagickMemory(blue);
3886 Convert PseudoClass packets to contiguous grayscale scanlines.
3888 quantum_type=IndexQuantum;
3889 if (image->alpha_trait != UndefinedPixelTrait)
3891 if (photometric != PHOTOMETRIC_PALETTE)
3892 quantum_type=GrayAlphaQuantum;
3894 quantum_type=IndexAlphaQuantum;
3897 if (photometric != PHOTOMETRIC_PALETTE)
3898 quantum_type=GrayQuantum;
3899 for (y=0; y < (ssize_t) image->rows; y++)
3901 register const Quantum
3904 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3905 if (p == (const Quantum *) NULL)
3907 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3908 quantum_type,pixels,exception);
3909 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3911 if (image->previous == (Image *) NULL)
3913 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3915 if (status == MagickFalse)
3922 quantum_info=DestroyQuantumInfo(quantum_info);
3923 if (image->colorspace == LabColorspace)
3924 DecodeLabImage(image,exception);
3925 DestroyTIFFInfo(&tiff_info);
3926 DisableMSCWarning(4127)
3927 if (0 && (image_info->verbose != MagickFalse))
3929 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3930 (void) TIFFWriteDirectory(tiff);
3931 image=SyncNextImageInList(image);
3932 if (image == (Image *) NULL)
3934 status=SetImageProgress(image,SaveImagesTag,scene++,
3935 GetImageListLength(image));
3936 if (status == MagickFalse)
3938 } while (image_info->adjoin != MagickFalse);