2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % TTTTT IIIII FFFFF FFFFF %
13 % Read/Write TIFF Image Format %
20 % Copyright 1999-2015 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/artifact.h"
44 #include "MagickCore/attribute.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/color.h"
49 #include "MagickCore/color-private.h"
50 #include "MagickCore/colormap.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/enhance.h"
55 #include "MagickCore/exception.h"
56 #include "MagickCore/exception-private.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/log.h"
62 #include "MagickCore/magick.h"
63 #include "MagickCore/memory_.h"
64 #include "MagickCore/module.h"
65 #include "MagickCore/monitor.h"
66 #include "MagickCore/monitor-private.h"
67 #include "MagickCore/option.h"
68 #include "MagickCore/pixel-accessor.h"
69 #include "MagickCore/pixel-private.h"
70 #include "MagickCore/property.h"
71 #include "MagickCore/quantum.h"
72 #include "MagickCore/quantum-private.h"
73 #include "MagickCore/profile.h"
74 #include "MagickCore/resize.h"
75 #include "MagickCore/resource_.h"
76 #include "MagickCore/semaphore.h"
77 #include "MagickCore/splay-tree.h"
78 #include "MagickCore/static.h"
79 #include "MagickCore/statistic.h"
80 #include "MagickCore/string_.h"
81 #include "MagickCore/string-private.h"
82 #include "MagickCore/thread_.h"
83 #include "MagickCore/token.h"
84 #include "MagickCore/utility.h"
85 #if defined(MAGICKCORE_TIFF_DELEGATE)
86 # if defined(MAGICKCORE_HAVE_TIFFCONF_H)
87 # include "tiffconf.h"
91 # if !defined(COMPRESSION_ADOBE_DEFLATE)
92 # define COMPRESSION_ADOBE_DEFLATE 8
94 # if !defined(PREDICTOR_HORIZONTAL)
95 # define PREDICTOR_HORIZONTAL 2
97 # if !defined(TIFFTAG_COPYRIGHT)
98 # define TIFFTAG_COPYRIGHT 33432
100 # if !defined(TIFFTAG_OPIIMAGEID)
101 # define TIFFTAG_OPIIMAGEID 32781
103 #include "psd-private.h"
106 Typedef declarations.
110 ReadSingleSampleMethod,
119 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
120 typedef struct _ExifInfo
131 static const ExifInfo
133 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, 0, "exif:ExposureTime" },
134 { EXIFTAG_FNUMBER, TIFF_RATIONAL, 0, "exif:FNumber" },
135 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, 0, "exif:ExposureProgram" },
136 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, 0, "exif:SpectralSensitivity" },
137 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, 1, "exif:ISOSpeedRatings" },
138 { EXIFTAG_OECF, TIFF_NOTYPE, 0, "exif:OptoelectricConversionFactor" },
139 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, 0, "exif:ExifVersion" },
140 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, 0, "exif:DateTimeOriginal" },
141 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, 0, "exif:DateTimeDigitized" },
142 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, 0, "exif:ComponentsConfiguration" },
143 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, 0, "exif:CompressedBitsPerPixel" },
144 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, 0, "exif:ShutterSpeedValue" },
145 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, 0, "exif:ApertureValue" },
146 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, 0, "exif:BrightnessValue" },
147 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, 0, "exif:ExposureBiasValue" },
148 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, 0, "exif:MaxApertureValue" },
149 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, 0, "exif:SubjectDistance" },
150 { EXIFTAG_METERINGMODE, TIFF_SHORT, 0, "exif:MeteringMode" },
151 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, 0, "exif:LightSource" },
152 { EXIFTAG_FLASH, TIFF_SHORT, 0, "exif:Flash" },
153 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, 0, "exif:FocalLength" },
154 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, 0, "exif:SubjectArea" },
155 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, 0, "exif:MakerNote" },
156 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, 0, "exif:UserComment" },
157 { EXIFTAG_SUBSECTIME, TIFF_ASCII, 0, "exif:SubSecTime" },
158 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, 0, "exif:SubSecTimeOriginal" },
159 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, 0, "exif:SubSecTimeDigitized" },
160 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, 0, "exif:FlashpixVersion" },
161 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, 0, "exif:PixelXDimension" },
162 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, 0, "exif:PixelYDimension" },
163 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, 0, "exif:RelatedSoundFile" },
164 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, 0, "exif:FlashEnergy" },
165 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, 0, "exif:SpatialFrequencyResponse" },
166 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, 0, "exif:FocalPlaneXResolution" },
167 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, 0, "exif:FocalPlaneYResolution" },
168 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, 0, "exif:FocalPlaneResolutionUnit" },
169 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, 0, "exif:SubjectLocation" },
170 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, 0, "exif:ExposureIndex" },
171 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, 0, "exif:SensingMethod" },
172 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, 0, "exif:FileSource" },
173 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, 0, "exif:SceneType" },
174 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, 0, "exif:CFAPattern" },
175 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, 0, "exif:CustomRendered" },
176 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, 0, "exif:ExposureMode" },
177 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, 0, "exif:WhiteBalance" },
178 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, 0, "exif:DigitalZoomRatio" },
179 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, 0, "exif:FocalLengthIn35mmFilm" },
180 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, 0, "exif:SceneCaptureType" },
181 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, 0, "exif:GainControl" },
182 { EXIFTAG_CONTRAST, TIFF_SHORT, 0, "exif:Contrast" },
183 { EXIFTAG_SATURATION, TIFF_SHORT, 0, "exif:Saturation" },
184 { EXIFTAG_SHARPNESS, TIFF_SHORT, 0, "exif:Sharpness" },
185 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, 0, "exif:DeviceSettingDescription" },
186 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, 0, "exif:SubjectDistanceRange" },
187 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, 0, "exif:ImageUniqueID" },
188 { 0, 0, 0, (char *) NULL }
191 #endif /* MAGICKCORE_TIFF_DELEGATE */
196 static MagickThreadKey
200 *tiff_semaphore = (SemaphoreInfo *) NULL;
202 static volatile MagickBooleanType
203 instantiate_key = MagickFalse;
206 Forward declarations.
208 #if defined(MAGICKCORE_TIFF_DELEGATE)
210 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
212 static MagickBooleanType
213 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
214 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
215 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229 % IsTIFF() returns MagickTrue if the image format type, identified by the
230 % magick string, is TIFF.
232 % The format of the IsTIFF method is:
234 % MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
236 % A description of each parameter follows:
238 % o magick: compare image format pattern against these bytes.
240 % o length: Specifies the length of the magick string.
243 static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
247 if (memcmp(magick,"\115\115\000\052",4) == 0)
249 if (memcmp(magick,"\111\111\052\000",4) == 0)
251 #if defined(TIFF_VERSION_BIG)
254 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
256 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
262 #if defined(MAGICKCORE_TIFF_DELEGATE)
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268 % R e a d G R O U P 4 I m a g e %
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
275 % allocates the memory necessary for the new Image structure and returns a
276 % pointer to the new image.
278 % The format of the ReadGROUP4Image method is:
280 % Image *ReadGROUP4Image(const ImageInfo *image_info,
281 % ExceptionInfo *exception)
283 % A description of each parameter follows:
285 % o image_info: the image info.
287 % o exception: return any errors or warnings in this structure.
291 static inline size_t WriteLSBLong(FILE *file,const size_t value)
296 buffer[0]=(unsigned char) value;
297 buffer[1]=(unsigned char) (value >> 8);
298 buffer[2]=(unsigned char) (value >> 16);
299 buffer[3]=(unsigned char) (value >> 24);
300 return(fwrite(buffer,1,4,file));
303 static Image *ReadGROUP4Image(const ImageInfo *image_info,
304 ExceptionInfo *exception)
307 filename[MagickPathExtent];
335 assert(image_info != (const ImageInfo *) NULL);
336 assert(image_info->signature == MagickSignature);
337 if (image_info->debug != MagickFalse)
338 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
339 image_info->filename);
340 assert(exception != (ExceptionInfo *) NULL);
341 assert(exception->signature == MagickSignature);
342 image=AcquireImage(image_info,exception);
343 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
344 if (status == MagickFalse)
346 image=DestroyImageList(image);
347 return((Image *) NULL);
350 Write raw CCITT Group 4 wrapped as a TIFF image file.
353 unique_file=AcquireUniqueFileResource(filename);
354 if (unique_file != -1)
355 file=fdopen(unique_file,"wb");
356 if ((unique_file == -1) || (file == (FILE *) NULL))
357 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
358 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
359 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
360 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
361 length=WriteLSBLong(file,image->columns);
362 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
363 length=WriteLSBLong(file,image->rows);
364 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
365 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
366 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
367 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
368 strip_offset=10+(12*14)+4+8;
369 length=WriteLSBLong(file,(size_t) strip_offset);
370 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
371 length=WriteLSBLong(file,(size_t) image_info->orientation);
372 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
373 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
374 length=WriteLSBLong(file,image->rows);
375 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
376 offset=(ssize_t) ftell(file)-4;
377 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
378 length=WriteLSBLong(file,(size_t) (strip_offset-8));
379 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
380 length=WriteLSBLong(file,(size_t) (strip_offset-8));
381 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
382 length=fwrite("\000\000\000\000",1,4,file);
383 length=WriteLSBLong(file,(long) image->resolution.x);
384 length=WriteLSBLong(file,1);
385 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
386 (void) fputc(c,file);
387 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
388 length=WriteLSBLong(file,(unsigned int) length);
390 (void) CloseBlob(image);
391 image=DestroyImage(image);
395 read_info=CloneImageInfo((ImageInfo *) NULL);
396 (void) FormatLocaleString(read_info->filename,MagickPathExtent,"%s",filename);
397 image=ReadTIFFImage(read_info,exception);
398 read_info=DestroyImageInfo(read_info);
399 if (image != (Image *) NULL)
401 (void) CopyMagickString(image->filename,image_info->filename,
403 (void) CopyMagickString(image->magick_filename,image_info->filename,
405 (void) CopyMagickString(image->magick,"GROUP4",MagickPathExtent);
407 (void) RelinquishUniqueFileResource(filename);
412 #if defined(MAGICKCORE_TIFF_DELEGATE)
414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418 % R e a d T I F F I m a g e %
422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
424 % ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
425 % memory necessary for the new Image structure and returns a pointer to the
428 % The format of the ReadTIFFImage method is:
430 % Image *ReadTIFFImage(const ImageInfo *image_info,
431 % ExceptionInfo *exception)
433 % A description of each parameter follows:
435 % o image_info: the image info.
437 % o exception: return any errors or warnings in this structure.
441 static inline unsigned char ClampYCC(double value)
445 return((unsigned char)0);
447 return((unsigned char)255);
448 return((unsigned char)(value));
451 static MagickBooleanType DecodeLabImage(Image *image,ExceptionInfo *exception)
463 image_view=AcquireAuthenticCacheView(image,exception);
464 for (y=0; y < (ssize_t) image->rows; y++)
472 if (status == MagickFalse)
474 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
475 if (q == (Quantum *) NULL)
480 for (x=0; x < (ssize_t) image->columns; x++)
486 a=QuantumScale*GetPixela(image,q)+0.5;
489 b=QuantumScale*GetPixelb(image,q)+0.5;
492 SetPixela(image,QuantumRange*a,q);
493 SetPixelb(image,QuantumRange*b,q);
494 q+=GetPixelChannels(image);
496 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
499 image_view=DestroyCacheView(image_view);
503 static MagickBooleanType ReadProfile(Image *image,const char *name,
504 const unsigned char *datum,ssize_t length,ExceptionInfo *exception)
514 profile=BlobToStringInfo(datum,(size_t) length);
515 if (profile == (StringInfo *) NULL)
516 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
518 status=SetImageProfile(image,name,profile,exception);
519 profile=DestroyStringInfo(profile);
520 if (status == MagickFalse)
521 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
526 #if defined(__cplusplus) || defined(c_plusplus)
530 static int TIFFCloseBlob(thandle_t image)
532 (void) CloseBlob((Image *) image);
536 static void TIFFErrors(const char *module,const char *format,va_list error)
539 message[MagickPathExtent];
544 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
545 (void) vsnprintf(message,MagickPathExtent,format,error);
547 (void) vsprintf(message,format,error);
549 (void) ConcatenateMagickString(message,".",MagickPathExtent);
550 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
551 if (exception != (ExceptionInfo *) NULL)
552 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
556 static toff_t TIFFGetBlobSize(thandle_t image)
558 return((toff_t) GetBlobSize((Image *) image));
561 static void TIFFGetProfiles(TIFF *tiff,Image *image,MagickBooleanType ping,
562 ExceptionInfo *exception)
571 if (ping == MagickFalse)
573 #if defined(TIFFTAG_ICCPROFILE)
574 if ((TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1) &&
575 (profile != (unsigned char *) NULL))
576 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
578 #if defined(TIFFTAG_PHOTOSHOP)
579 if ((TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1) &&
580 (profile != (unsigned char *) NULL))
581 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
583 #if defined(TIFFTAG_RICHTIFFIPTC)
584 if ((TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1) &&
585 (profile != (unsigned char *) NULL))
587 if (TIFFIsByteSwapped(tiff) != 0)
588 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
589 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
592 #if defined(TIFFTAG_XMLPACKET)
593 if ((TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1) &&
594 (profile != (unsigned char *) NULL))
595 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
597 if ((TIFFGetField(tiff,34118,&length,&profile) == 1) &&
598 (profile != (unsigned char *) NULL))
599 (void) ReadProfile(image,"tiff:34118",profile,(ssize_t) length,exception);
601 if ((TIFFGetField(tiff,37724,&length,&profile) == 1) &&
602 (profile != (unsigned char *) NULL))
603 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
606 static void TIFFGetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
609 message[MagickPathExtent],
615 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
616 (void) SetImageProperty(image,"tiff:artist",text,exception);
617 if (TIFFGetField(tiff,TIFFTAG_COPYRIGHT,&text) == 1)
618 (void) SetImageProperty(image,"tiff:copyright",text,exception);
619 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
620 (void) SetImageProperty(image,"tiff:timestamp",text,exception);
621 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
622 (void) SetImageProperty(image,"tiff:document",text,exception);
623 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
624 (void) SetImageProperty(image,"tiff:hostcomputer",text,exception);
625 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
626 (void) SetImageProperty(image,"comment",text,exception);
627 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
628 (void) SetImageProperty(image,"tiff:make",text,exception);
629 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
630 (void) SetImageProperty(image,"tiff:model",text,exception);
631 if (TIFFGetField(tiff,TIFFTAG_OPIIMAGEID,&count,&text) == 1)
633 if (count >= MagickPathExtent)
634 count=MagickPathExtent-1;
635 (void) CopyMagickString(message,text,count+1);
636 (void) SetImageProperty(image,"tiff:image-id",message,exception);
638 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
639 (void) SetImageProperty(image,"label",text,exception);
640 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
641 (void) SetImageProperty(image,"tiff:software",text,exception);
642 if (TIFFGetField(tiff,33423,&count,&text) == 1)
644 if (count >= MagickPathExtent)
645 count=MagickPathExtent-1;
646 (void) CopyMagickString(message,text,count+1);
647 (void) SetImageProperty(image,"tiff:kodak-33423",message,exception);
649 if (TIFFGetField(tiff,36867,&count,&text) == 1)
651 if (count >= MagickPathExtent)
652 count=MagickPathExtent-1;
653 (void) CopyMagickString(message,text,count+1);
654 (void) SetImageProperty(image,"tiff:kodak-36867",message,exception);
658 static void TIFFGetEXIFProperties(TIFF *tiff,Image *image,
659 ExceptionInfo *exception)
661 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
663 value[MagickPathExtent];
671 #if defined(TIFF_VERSION_BIG)
682 Read EXIF properties.
685 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) != 1)
687 directory=TIFFCurrentDirectory(tiff);
688 if (TIFFReadEXIFDirectory(tiff,offset) != 1)
690 TIFFSetDirectory(tiff,directory);
694 for (i=0; exif_info[i].tag != 0; i++)
697 switch (exif_info[i].type)
705 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans,&sans) == 1) &&
706 (ascii != (char *) NULL) && (*ascii != '\0'))
707 (void) CopyMagickString(value,ascii,MagickPathExtent);
712 if (exif_info[i].variable_length == 0)
718 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans,&sans) == 1)
719 (void) FormatLocaleString(value,MagickPathExtent,"%d",shorty);
732 tiff_status=TIFFGetField(tiff,exif_info[i].tag,&shorty_num,&shorty,
734 if (tiff_status == 1)
735 (void) FormatLocaleString(value,MagickPathExtent,"%d",
736 shorty_num != 0 ? shorty[0] : 0);
746 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans,&sans) == 1)
747 (void) FormatLocaleString(value,MagickPathExtent,"%d",longy);
750 #if defined(TIFF_VERSION_BIG)
757 if (TIFFGetField(tiff,exif_info[i].tag,&long8y,&sans,&sans) == 1)
758 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
759 ((MagickOffsetType) long8y));
771 if (TIFFGetField(tiff,exif_info[i].tag,&floaty,&sans,&sans) == 1)
772 (void) FormatLocaleString(value,MagickPathExtent,"%g",(double) floaty);
781 if (TIFFGetField(tiff,exif_info[i].tag,&doubley,&sans,&sans) == 1)
782 (void) FormatLocaleString(value,MagickPathExtent,"%g",doubley);
789 (void) SetImageProperty(image,exif_info[i].property,value,exception);
791 TIFFSetDirectory(tiff,directory);
798 static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
800 *base=(tdata_t *) GetBlobStreamData((Image *) image);
801 if (*base != (tdata_t *) NULL)
802 *size=(toff_t) GetBlobSize((Image *) image);
803 if (*base != (tdata_t *) NULL)
808 static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
813 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
814 (unsigned char *) data);
818 static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
819 tsample_t sample,ssize_t row,tdata_t scanline)
824 (void) bits_per_sample;
825 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
829 static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
831 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
834 static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
841 static void TIFFWarnings(const char *module,const char *format,va_list warning)
844 message[MagickPathExtent];
849 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
850 (void) vsnprintf(message,MagickPathExtent,format,warning);
852 (void) vsprintf(message,format,warning);
854 (void) ConcatenateMagickString(message,".",MagickPathExtent);
855 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
856 if (exception != (ExceptionInfo *) NULL)
857 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
858 message,"`%s'",module);
861 static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
866 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
867 (unsigned char *) data);
871 static TIFFMethodType GetJPEGMethod(Image* image,TIFF *tiff,uint16 photometric,
872 uint16 bits_per_sample,uint16 samples_per_pixel)
874 #define BUFFER_SIZE 2048
886 #if defined(TIFF_VERSION_BIG)
894 buffer[BUFFER_SIZE+32];
899 /* only support 8 bit for now */
900 if ((photometric != PHOTOMETRIC_SEPARATED) || (bits_per_sample != 8) ||
901 (samples_per_pixel != 4))
902 return(ReadGenericMethod);
903 /* Search for Adobe APP14 JPEG Marker */
904 if (!TIFFGetField(tiff,TIFFTAG_STRIPOFFSETS,&value))
905 return(ReadRGBAMethod);
906 position=TellBlob(image);
907 offset=(MagickOffsetType) (value[0]);
908 if (SeekBlob(image,offset,SEEK_SET) != offset)
909 return(ReadRGBAMethod);
910 method=ReadRGBAMethod;
911 if (ReadBlob(image,BUFFER_SIZE,buffer) == BUFFER_SIZE)
913 for (i=0; i < BUFFER_SIZE; i++)
915 while (i < BUFFER_SIZE)
917 if (buffer[i++] == 255)
920 while (i < BUFFER_SIZE)
922 if (buffer[++i] != 255)
925 if (buffer[i++] == 216) /* JPEG_MARKER_SOI */
927 length=(unsigned short) (((unsigned int) (buffer[i] << 8) |
928 (unsigned int) buffer[i+1]) & 0xffff);
929 if (i+(size_t) length >= BUFFER_SIZE)
931 if (buffer[i-1] == 238) /* JPEG_MARKER_APP0+14 */
935 /* 0 == CMYK, 1 == YCbCr, 2 = YCCK */
936 if (buffer[i+13] == 2)
937 method=ReadYCCKMethod;
943 (void) SeekBlob(image,position,SEEK_SET);
947 static void TIFFReadPhotoshopLayers(Image* image,const ImageInfo *image_info,
948 ExceptionInfo *exception)
965 if (GetImageListLength(image) != 1)
967 option=GetImageOption(image_info,"tiff:ignore-layers");
968 if (IsStringTrue(option) != MagickFalse)
970 layer_info=GetImageProfile(image,"tiff:37724");
971 if (layer_info == (const StringInfo *) NULL)
973 for (i=0; i < (ssize_t) layer_info->length-8; i++)
975 if (LocaleNCompare((const char *) (layer_info->datum+i),"8BIM",4) != 0)
978 if ((LocaleNCompare((const char *) (layer_info->datum+i),"Layr",4) == 0) ||
979 (LocaleNCompare((const char *) (layer_info->datum+i),"LMsk",4) == 0) ||
980 (LocaleNCompare((const char *) (layer_info->datum+i),"Lr16",4) == 0) ||
981 (LocaleNCompare((const char *) (layer_info->datum+i),"Lr32",4) == 0))
985 if (i >= (ssize_t) (layer_info->length-8))
987 layers=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
988 RemoveImageProfile(layers,"tiff:37724");
989 AttachBlob(layers->blob,layer_info->datum,layer_info->length);
990 SeekBlob(layers,(MagickOffsetType) i,SEEK_SET);
992 info.columns=layers->columns;
993 info.rows=layers->rows;
994 info.channels=(unsigned short) layers->number_channels;
995 ReadPSDLayers(layers,image_info,&info,MagickFalse,exception);
996 DeleteImageFromList(&layers);
997 if (layers != (Image *) NULL)
999 AppendImageToList(&image,layers);
1000 while (layers != (Image *) NULL)
1002 DetachBlob(layers->blob);
1003 layers=GetNextImageInList(layers);
1008 #if defined(__cplusplus) || defined(c_plusplus)
1012 static Image *ReadTIFFImage(const ImageInfo *image_info,
1013 ExceptionInfo *exception)
1091 assert(image_info != (const ImageInfo *) NULL);
1092 assert(image_info->signature == MagickSignature);
1093 if (image_info->debug != MagickFalse)
1094 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1095 image_info->filename);
1096 assert(exception != (ExceptionInfo *) NULL);
1097 assert(exception->signature == MagickSignature);
1098 image=AcquireImage(image_info,exception);
1099 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1100 if (status == MagickFalse)
1102 image=DestroyImageList(image);
1103 return((Image *) NULL);
1105 (void) MagickSetThreadValue(tiff_exception,exception);
1106 error_handler=TIFFSetErrorHandler(TIFFErrors);
1107 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
1108 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
1109 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
1111 if (tiff == (TIFF *) NULL)
1113 (void) TIFFSetWarningHandler(warning_handler);
1114 (void) TIFFSetErrorHandler(error_handler);
1115 image=DestroyImageList(image);
1116 return((Image *) NULL);
1118 debug=IsEventLogging();
1120 if (image_info->number_scenes != 0)
1123 Generate blank images for subimage specification (e.g. image.tif[4].
1125 for (i=0; i < (ssize_t) image_info->scene; i++)
1127 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1128 if (status == MagickFalse)
1131 image=DestroyImageList(image);
1132 return((Image *) NULL);
1134 AcquireNextImage(image_info,image,exception);
1135 if (GetNextImageInList(image) == (Image *) NULL)
1138 image=DestroyImageList(image);
1139 return((Image *) NULL);
1141 image=SyncNextImageInList(image);
1146 DisableMSCWarning(4127)
1147 if (0 && (image_info->verbose != MagickFalse))
1148 TIFFPrintDirectory(tiff,stdout,MagickFalse);
1150 if ((TIFFGetField(tiff,TIFFTAG_IMAGEWIDTH,&width) != 1) ||
1151 (TIFFGetField(tiff,TIFFTAG_IMAGELENGTH,&height) != 1) ||
1152 (TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag) != 1) ||
1153 (TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian) != 1) ||
1154 (TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace) != 1) ||
1155 (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,&samples_per_pixel) != 1) ||
1156 (TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample) != 1) ||
1157 (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format) != 1) ||
1158 (TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value) != 1) ||
1159 (TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value) != 1) ||
1160 (TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric) != 1))
1163 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1165 if (sample_format == SAMPLEFORMAT_IEEEFP)
1166 (void) SetImageProperty(image,"quantum:format","floating-point",
1168 switch (photometric)
1170 case PHOTOMETRIC_MINISBLACK:
1172 (void) SetImageProperty(image,"tiff:photometric","min-is-black",
1176 case PHOTOMETRIC_MINISWHITE:
1178 (void) SetImageProperty(image,"tiff:photometric","min-is-white",
1182 case PHOTOMETRIC_PALETTE:
1184 (void) SetImageProperty(image,"tiff:photometric","palette",exception);
1187 case PHOTOMETRIC_RGB:
1189 (void) SetImageProperty(image,"tiff:photometric","RGB",exception);
1192 case PHOTOMETRIC_CIELAB:
1194 (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception);
1197 case PHOTOMETRIC_LOGL:
1199 (void) SetImageProperty(image,"tiff:photometric","CIE Log2(L)",exception);
1202 case PHOTOMETRIC_LOGLUV:
1204 (void) SetImageProperty(image,"tiff:photometric","LOGLUV",exception);
1207 #if defined(PHOTOMETRIC_MASK)
1208 case PHOTOMETRIC_MASK:
1210 (void) SetImageProperty(image,"tiff:photometric","MASK",exception);
1214 case PHOTOMETRIC_SEPARATED:
1216 (void) SetImageProperty(image,"tiff:photometric","separated",exception);
1219 case PHOTOMETRIC_YCBCR:
1221 (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception);
1226 (void) SetImageProperty(image,"tiff:photometric","unknown",exception);
1230 if (image->debug != MagickFalse)
1232 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
1233 (unsigned int) width,(unsigned int) height);
1234 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
1236 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1237 "Bits per sample: %u",bits_per_sample);
1238 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1239 "Min sample value: %u",min_sample_value);
1240 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1241 "Max sample value: %u",max_sample_value);
1242 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
1243 "interpretation: %s",GetImageProperty(image,"tiff:photometric",
1246 image->columns=(size_t) width;
1247 image->rows=(size_t) height;
1248 image->depth=(size_t) bits_per_sample;
1249 status=SetImageExtent(image,image->columns,image->rows,exception);
1250 if (status == MagickFalse)
1251 return(DestroyImageList(image));
1252 if (image->debug != MagickFalse)
1253 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
1254 (double) image->depth);
1255 image->endian=MSBEndian;
1256 if (endian == FILLORDER_LSB2MSB)
1257 image->endian=LSBEndian;
1258 #if defined(MAGICKCORE_HAVE_TIFFISBIGENDIAN)
1259 if (TIFFIsBigEndian(tiff) == 0)
1261 (void) SetImageProperty(image,"tiff:endian","lsb",exception);
1262 image->endian=LSBEndian;
1266 (void) SetImageProperty(image,"tiff:endian","msb",exception);
1267 image->endian=MSBEndian;
1270 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1271 (photometric == PHOTOMETRIC_MINISWHITE))
1272 SetImageColorspace(image,GRAYColorspace,exception);
1273 if (photometric == PHOTOMETRIC_SEPARATED)
1274 SetImageColorspace(image,CMYKColorspace,exception);
1275 if (photometric == PHOTOMETRIC_CIELAB)
1276 SetImageColorspace(image,LabColorspace,exception);
1277 TIFFGetProfiles(tiff,image,image_info->ping,exception);
1278 TIFFGetProperties(tiff,image,exception);
1279 option=GetImageOption(image_info,"tiff:exif-properties");
1280 if (IfMagickTrue(IsStringNotFalse(option))) /* enabled by default */
1281 TIFFGetEXIFProperties(tiff,image,exception);
1282 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
1283 &samples_per_pixel);
1284 if ((TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution) == 1) &&
1285 (TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution) == 1))
1287 image->resolution.x=x_resolution;
1288 image->resolution.y=y_resolution;
1290 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units) == 1)
1292 if (units == RESUNIT_INCH)
1293 image->units=PixelsPerInchResolution;
1294 if (units == RESUNIT_CENTIMETER)
1295 image->units=PixelsPerCentimeterResolution;
1297 if ((TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position) == 1) &&
1298 (TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position) == 1))
1300 image->page.x=(ssize_t) ceil(x_position*image->resolution.x-0.5);
1301 image->page.y=(ssize_t) ceil(y_position*image->resolution.y-0.5);
1303 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation) == 1)
1304 image->orientation=(OrientationType) orientation;
1305 if (TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity) == 1)
1307 if (chromaticity != (float *) NULL)
1309 image->chromaticity.white_point.x=chromaticity[0];
1310 image->chromaticity.white_point.y=chromaticity[1];
1313 if (TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity) == 1)
1315 if (chromaticity != (float *) NULL)
1317 image->chromaticity.red_primary.x=chromaticity[0];
1318 image->chromaticity.red_primary.y=chromaticity[1];
1319 image->chromaticity.green_primary.x=chromaticity[2];
1320 image->chromaticity.green_primary.y=chromaticity[3];
1321 image->chromaticity.blue_primary.x=chromaticity[4];
1322 image->chromaticity.blue_primary.y=chromaticity[5];
1325 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1326 if ((compress_tag != COMPRESSION_NONE) &&
1327 (TIFFIsCODECConfigured(compress_tag) == 0))
1330 ThrowReaderException(CoderError,"CompressNotSupported");
1333 switch (compress_tag)
1335 case COMPRESSION_NONE: image->compression=NoCompression; break;
1336 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1337 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1338 case COMPRESSION_JPEG:
1340 image->compression=JPEGCompression;
1341 #if defined(JPEG_SUPPORT)
1344 sampling_factor[MagickPathExtent];
1353 tiff_status=TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1354 &horizontal,&vertical);
1355 if (tiff_status == 1)
1357 (void) FormatLocaleString(sampling_factor,MagickPathExtent,"%dx%d",
1358 horizontal,vertical);
1359 (void) SetImageProperty(image,"jpeg:sampling-factor",
1360 sampling_factor,exception);
1361 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1362 "Sampling Factors: %s",sampling_factor);
1368 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1369 #if defined(COMPRESSION_LZMA)
1370 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1372 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1373 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1374 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1375 default: image->compression=RLECompression; break;
1378 Allocate memory for the image and pixel buffer.
1380 quantum_info=AcquireQuantumInfo(image_info,image);
1381 if (quantum_info == (QuantumInfo *) NULL)
1384 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1386 if (sample_format == SAMPLEFORMAT_UINT)
1387 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1388 if (sample_format == SAMPLEFORMAT_INT)
1389 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1390 if (sample_format == SAMPLEFORMAT_IEEEFP)
1391 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1392 if (status == MagickFalse)
1395 quantum_info=DestroyQuantumInfo(quantum_info);
1396 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1399 switch (photometric)
1401 case PHOTOMETRIC_MINISBLACK:
1403 quantum_info->min_is_white=MagickFalse;
1406 case PHOTOMETRIC_MINISWHITE:
1408 quantum_info->min_is_white=MagickTrue;
1414 tiff_status=TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1416 if (tiff_status == 1)
1418 (void) SetImageProperty(image,"tiff:alpha","unspecified",exception);
1419 if (extra_samples == 0)
1421 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1422 image->alpha_trait=BlendPixelTrait;
1425 for (i=0; i < extra_samples; i++)
1427 image->alpha_trait=BlendPixelTrait;
1428 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
1430 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
1431 (void) SetImageProperty(image,"tiff:alpha","associated",
1435 if (sample_info[i] == EXTRASAMPLE_UNASSALPHA)
1436 (void) SetImageProperty(image,"tiff:alpha","unassociated",
1440 if ((photometric == PHOTOMETRIC_PALETTE) &&
1441 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1446 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
1447 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
1450 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1453 value=(unsigned short) image->scene;
1454 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages) == 1)
1456 if (image->storage_class == PseudoClass)
1470 Initialize colormap.
1472 tiff_status=TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1473 &green_colormap,&blue_colormap);
1474 if (tiff_status == 1)
1476 if ((red_colormap != (uint16 *) NULL) &&
1477 (green_colormap != (uint16 *) NULL) &&
1478 (blue_colormap != (uint16 *) NULL))
1480 range=255; /* might be old style 8-bit colormap */
1481 for (i=0; i < (ssize_t) image->colors; i++)
1482 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1483 (blue_colormap[i] >= 256))
1488 for (i=0; i < (ssize_t) image->colors; i++)
1490 image->colormap[i].red=ClampToQuantum(((double)
1491 QuantumRange*red_colormap[i])/range);
1492 image->colormap[i].green=ClampToQuantum(((double)
1493 QuantumRange*green_colormap[i])/range);
1494 image->colormap[i].blue=ClampToQuantum(((double)
1495 QuantumRange*blue_colormap[i])/range);
1499 if (image->alpha_trait == UndefinedPixelTrait)
1500 image->depth=GetImageDepth(image,exception);
1502 if (image_info->ping != MagickFalse)
1504 if (image_info->number_scenes != 0)
1505 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1507 quantum_info=DestroyQuantumInfo(quantum_info);
1510 goto next_tiff_frame;
1512 method=ReadGenericMethod;
1513 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) == 1)
1516 value[MagickPathExtent];
1518 method=ReadStripMethod;
1519 (void) FormatLocaleString(value,MagickPathExtent,"%u",
1520 (unsigned int) rows_per_strip);
1521 (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception);
1523 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1524 method=ReadRGBAMethod;
1525 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1526 method=ReadCMYKAMethod;
1527 if ((photometric != PHOTOMETRIC_RGB) &&
1528 (photometric != PHOTOMETRIC_CIELAB) &&
1529 (photometric != PHOTOMETRIC_SEPARATED))
1530 method=ReadGenericMethod;
1531 if (image->storage_class == PseudoClass)
1532 method=ReadSingleSampleMethod;
1533 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1534 (photometric == PHOTOMETRIC_MINISWHITE))
1535 method=ReadSingleSampleMethod;
1536 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1537 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
1538 method=ReadGenericMethod;
1539 if (image->compression == JPEGCompression)
1540 method=GetJPEGMethod(image,tiff,photometric,bits_per_sample,
1542 if (compress_tag == COMPRESSION_JBIG)
1543 method=ReadStripMethod;
1544 if (TIFFIsTiled(tiff) != MagickFalse)
1545 method=ReadTileMethod;
1546 quantum_info->endian=LSBEndian;
1547 quantum_type=RGBQuantum;
1548 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1551 case ReadSingleSampleMethod:
1554 Convert TIFF image to PseudoClass MIFF image.
1556 quantum_type=IndexQuantum;
1557 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1558 if (image->alpha_trait != UndefinedPixelTrait)
1560 if (image->storage_class != PseudoClass)
1562 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1564 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1568 quantum_type=IndexAlphaQuantum;
1569 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1573 if (image->storage_class != PseudoClass)
1575 quantum_type=GrayQuantum;
1576 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1578 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1579 if (status == MagickFalse)
1582 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1584 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1585 for (y=0; y < (ssize_t) image->rows; y++)
1593 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1596 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1597 if (q == (Quantum *) NULL)
1599 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1600 quantum_type,pixels,exception);
1601 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1603 if (image->previous == (Image *) NULL)
1605 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1607 if (status == MagickFalse)
1613 case ReadRGBAMethod:
1616 Convert TIFF image to DirectClass MIFF image.
1618 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1619 quantum_type=RGBQuantum;
1620 if (image->alpha_trait != UndefinedPixelTrait)
1622 quantum_type=RGBAQuantum;
1623 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1625 if (image->colorspace == CMYKColorspace)
1627 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1628 quantum_type=CMYKQuantum;
1629 if (image->alpha_trait != UndefinedPixelTrait)
1631 quantum_type=CMYKAQuantum;
1632 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1635 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1636 if (status == MagickFalse)
1639 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1641 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1642 for (y=0; y < (ssize_t) image->rows; y++)
1650 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1653 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1654 if (q == (Quantum *) NULL)
1656 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1657 quantum_type,pixels,exception);
1658 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1660 if (image->previous == (Image *) NULL)
1662 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1664 if (status == MagickFalse)
1670 case ReadCMYKAMethod:
1673 Convert TIFF image to DirectClass MIFF image.
1675 for (i=0; i < (ssize_t) samples_per_pixel; i++)
1677 for (y=0; y < (ssize_t) image->rows; y++)
1685 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1689 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1690 if (q == (Quantum *) NULL)
1692 if (image->colorspace != CMYKColorspace)
1695 case 0: quantum_type=RedQuantum; break;
1696 case 1: quantum_type=GreenQuantum; break;
1697 case 2: quantum_type=BlueQuantum; break;
1698 case 3: quantum_type=AlphaQuantum; break;
1699 default: quantum_type=UndefinedQuantum; break;
1704 case 0: quantum_type=CyanQuantum; break;
1705 case 1: quantum_type=MagentaQuantum; break;
1706 case 2: quantum_type=YellowQuantum; break;
1707 case 3: quantum_type=BlackQuantum; break;
1708 case 4: quantum_type=AlphaQuantum; break;
1709 default: quantum_type=UndefinedQuantum; break;
1711 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1712 quantum_type,pixels,exception);
1713 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1716 if (image->previous == (Image *) NULL)
1718 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1720 if (status == MagickFalse)
1726 case ReadYCCKMethod:
1728 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1729 for (y=0; y < (ssize_t) image->rows; y++)
1743 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1746 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1747 if (q == (Quantum *) NULL)
1750 for (x=0; x < (ssize_t) image->columns; x++)
1752 SetPixelCyan(image,ScaleCharToQuantum(ClampYCC((double) *p+
1753 (1.402*(double) *(p+2))-179.456)),q);
1754 SetPixelMagenta(image,ScaleCharToQuantum(ClampYCC((double) *p-
1755 (0.34414*(double) *(p+1))-(0.71414*(double ) *(p+2))+
1757 SetPixelYellow(image,ScaleCharToQuantum(ClampYCC((double) *p+
1758 (1.772*(double) *(p+1))-226.816)),q);
1759 SetPixelBlack(image,ScaleCharToQuantum((unsigned char) *(p+3)),q);
1760 q+=GetPixelChannels(image);
1763 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1765 if (image->previous == (Image *) NULL)
1767 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1769 if (status == MagickFalse)
1775 case ReadStripMethod:
1781 Convert stripped TIFF image to DirectClass MIFF image.
1785 for (y=0; y < (ssize_t) image->rows; y++)
1793 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1794 if (q == (Quantum *) NULL)
1798 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1800 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1804 p=((uint32 *) pixels)+image->columns*i;
1805 for (x=0; x < (ssize_t) image->columns; x++)
1807 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1809 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1811 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1813 if (image->alpha_trait != UndefinedPixelTrait)
1814 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1817 q+=GetPixelChannels(image);
1819 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1821 if (image->previous == (Image *) NULL)
1823 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1825 if (status == MagickFalse)
1831 case ReadTileMethod:
1845 Convert tiled TIFF image to DirectClass MIFF image.
1847 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) != 1) ||
1848 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) != 1))
1851 ThrowReaderException(CoderError,"ImageIsNotTiled");
1853 (void) SetImageStorageClass(image,DirectClass,exception);
1854 number_pixels=columns*rows;
1855 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
1856 sizeof(*tile_pixels));
1857 if (tile_pixels == (uint32 *) NULL)
1860 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1862 for (y=0; y < (ssize_t) image->rows; y+=rows)
1875 rows_remaining=image->rows-y;
1876 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
1877 rows_remaining=rows;
1878 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1880 if (tile == (Quantum *) NULL)
1882 for (x=0; x < (ssize_t) image->columns; x+=columns)
1888 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1890 columns_remaining=image->columns-x;
1891 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
1892 columns_remaining=columns;
1893 p=tile_pixels+(rows-rows_remaining)*columns;
1894 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1896 for (row=rows_remaining; row > 0; row--)
1898 if (image->alpha_trait != UndefinedPixelTrait)
1899 for (column=columns_remaining; column > 0; column--)
1901 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1903 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1905 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1907 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1910 q+=GetPixelChannels(image);
1913 for (column=columns_remaining; column > 0; column--)
1915 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1917 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1919 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1922 q+=GetPixelChannels(image);
1924 p+=columns-columns_remaining;
1925 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
1928 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1930 if (image->previous == (Image *) NULL)
1932 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1934 if (status == MagickFalse)
1938 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1941 case ReadGenericMethod:
1954 Convert TIFF image to DirectClass MIFF image.
1956 number_pixels=(MagickSizeType) image->columns*image->rows;
1957 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1958 (number_pixels*sizeof(uint32))))
1961 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1963 pixel_info=AcquireVirtualMemory(image->columns,image->rows*
1965 if (pixel_info == (MemoryInfo *) NULL)
1968 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1970 pixels=(uint32 *) GetVirtualMemoryBlob(pixel_info);
1971 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1972 (uint32) image->rows,(uint32 *) pixels,0);
1974 Convert image to DirectClass pixel packets.
1976 p=pixels+number_pixels-1;
1977 for (y=0; y < (ssize_t) image->rows; y++)
1985 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1986 if (q == (Quantum *) NULL)
1988 q+=GetPixelChannels(image)*(image->columns-1);
1989 for (x=0; x < (ssize_t) image->columns; x++)
1991 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1993 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1995 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1997 if (image->alpha_trait != UndefinedPixelTrait)
1998 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
2001 q-=GetPixelChannels(image);
2003 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2005 if (image->previous == (Image *) NULL)
2007 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
2009 if (status == MagickFalse)
2013 pixel_info=RelinquishVirtualMemory(pixel_info);
2017 SetQuantumImageType(image,quantum_type);
2019 quantum_info=DestroyQuantumInfo(quantum_info);
2020 if (photometric == PHOTOMETRIC_CIELAB)
2021 DecodeLabImage(image,exception);
2022 if ((photometric == PHOTOMETRIC_LOGL) ||
2023 (photometric == PHOTOMETRIC_MINISBLACK) ||
2024 (photometric == PHOTOMETRIC_MINISWHITE))
2026 image->type=GrayscaleType;
2027 if (bits_per_sample == 1)
2028 image->type=BilevelType;
2031 Proceed to next image.
2033 if (image_info->number_scenes != 0)
2034 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
2036 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
2037 if (status != MagickFalse)
2040 Allocate next image structure.
2042 AcquireNextImage(image_info,image,exception);
2043 if (GetNextImageInList(image) == (Image *) NULL)
2045 image=DestroyImageList(image);
2046 return((Image *) NULL);
2048 image=SyncNextImageInList(image);
2049 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
2051 if (status == MagickFalse)
2054 } while (status != MagickFalse);
2055 (void) TIFFSetWarningHandler(warning_handler);
2056 (void) TIFFSetErrorHandler(error_handler);
2058 TIFFReadPhotoshopLayers(image,image_info,exception);
2059 return(GetFirstImageInList(image));
2064 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2068 % R e g i s t e r T I F F I m a g e %
2072 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2074 % RegisterTIFFImage() adds properties for the TIFF image format to
2075 % the list of supported formats. The properties include the image format
2076 % tag, a method to read and/or write the format, whether the format
2077 % supports the saving of more than one frame to the same file or blob,
2078 % whether the format supports native in-memory I/O, and a brief
2079 % description of the format.
2081 % The format of the RegisterTIFFImage method is:
2083 % size_t RegisterTIFFImage(void)
2087 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2088 static TIFFExtendProc
2089 tag_extender = (TIFFExtendProc) NULL;
2091 static void TIFFIgnoreTags(TIFF *tiff)
2112 if (TIFFGetReadProc(tiff) != TIFFReadBlob)
2114 image=(Image *)TIFFClientdata(tiff);
2115 tags=GetImageArtifact(image,"tiff:ignore-tags");
2116 if (tags == (const char *) NULL)
2122 while ((isspace((int) ((unsigned char) *p)) != 0))
2125 (void) strtol(p,&q,10);
2132 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2139 ignore=(TIFFFieldInfo *) AcquireQuantumMemory(count,sizeof(*ignore));
2140 /* This also sets field_bit to 0 (FIELD_IGNORE) */
2141 ResetMagickMemory(ignore,0,count*sizeof(*ignore));
2144 while ((isspace((int) ((unsigned char) *p)) != 0))
2147 ignore[i].field_tag=(ttag_t) strtol(p,&q,10);
2152 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2155 (void) TIFFMergeFieldInfo(tiff,ignore,(uint32) count);
2156 ignore=(TIFFFieldInfo *) RelinquishMagickMemory(ignore);
2159 static void TIFFTagExtender(TIFF *tiff)
2161 static const TIFFFieldInfo
2164 { 37724, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
2165 (char *) "PhotoshopLayerData" },
2166 { 34118, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
2167 (char *) "Microscope" }
2170 TIFFMergeFieldInfo(tiff,TIFFExtensions,sizeof(TIFFExtensions)/
2171 sizeof(*TIFFExtensions));
2172 if (tag_extender != (TIFFExtendProc) NULL)
2173 (*tag_extender)(tiff);
2174 TIFFIgnoreTags(tiff);
2178 ModuleExport size_t RegisterTIFFImage(void)
2180 #define TIFFDescription "Tagged Image File Format"
2183 version[MagickPathExtent];
2188 if (tiff_semaphore == (SemaphoreInfo *) NULL)
2189 ActivateSemaphoreInfo(&tiff_semaphore);
2190 LockSemaphoreInfo(tiff_semaphore);
2191 if (instantiate_key == MagickFalse)
2193 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
2194 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2195 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2196 if (tag_extender == (TIFFExtendProc) NULL)
2197 tag_extender=TIFFSetTagExtender(TIFFTagExtender);
2199 instantiate_key=MagickTrue;
2201 UnlockSemaphoreInfo(tiff_semaphore);
2203 #if defined(TIFF_VERSION)
2204 (void) FormatLocaleString(version,MagickPathExtent,"%d",TIFF_VERSION);
2206 #if defined(MAGICKCORE_TIFF_DELEGATE)
2215 for (i=0; (i < (MagickPathExtent-1)) && (*p != 0) && (*p != '\n'); i++)
2221 entry=AcquireMagickInfo("TIFF","GROUP4","Raw CCITT Group4");
2222 #if defined(MAGICKCORE_TIFF_DELEGATE)
2223 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
2224 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
2226 entry->flags|=CoderRawSupportFlag;
2227 entry->flags|=CoderEndianSupportFlag;
2228 entry->flags|=CoderSeekableStreamFlag;
2229 entry->flags^=CoderAdjoinFlag;
2230 entry->flags^=CoderUseExtensionFlag;
2231 entry->format_type=ImplicitFormatType;
2232 entry->mime_type=ConstantString("image/tiff");
2233 (void) RegisterMagickInfo(entry);
2234 entry=AcquireMagickInfo("TIFF","PTIF","Pyramid encoded TIFF");
2235 #if defined(MAGICKCORE_TIFF_DELEGATE)
2236 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2237 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
2239 entry->flags|=CoderEndianSupportFlag;
2240 entry->flags|=CoderSeekableStreamFlag;
2241 entry->flags^=CoderUseExtensionFlag;
2242 entry->mime_type=ConstantString("image/tiff");
2243 (void) RegisterMagickInfo(entry);
2244 entry=AcquireMagickInfo("TIFF","TIF",TIFFDescription);
2245 #if defined(MAGICKCORE_TIFF_DELEGATE)
2246 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2247 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2249 entry->flags|=CoderEndianSupportFlag;
2250 entry->flags|=CoderSeekableStreamFlag;
2251 entry->flags|=CoderStealthFlag;
2252 entry->flags^=CoderUseExtensionFlag;
2253 if (*version != '\0')
2254 entry->version=ConstantString(version);
2255 entry->mime_type=ConstantString("image/tiff");
2256 (void) RegisterMagickInfo(entry);
2257 entry=AcquireMagickInfo("TIFF","TIFF",TIFFDescription);
2258 #if defined(MAGICKCORE_TIFF_DELEGATE)
2259 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2260 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2262 entry->magick=(IsImageFormatHandler *) IsTIFF;
2263 entry->flags|=CoderEndianSupportFlag;
2264 entry->flags|=CoderSeekableStreamFlag;
2265 entry->flags^=CoderUseExtensionFlag;
2266 if (*version != '\0')
2267 entry->version=ConstantString(version);
2268 entry->mime_type=ConstantString("image/tiff");
2269 (void) RegisterMagickInfo(entry);
2270 entry=AcquireMagickInfo("TIFF","TIFF64","Tagged Image File Format (64-bit)");
2271 #if defined(TIFF_VERSION_BIG)
2272 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2273 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2275 entry->flags|=CoderEndianSupportFlag;
2276 entry->flags|=CoderSeekableStreamFlag;
2277 entry->flags^=CoderAdjoinFlag;
2278 entry->flags^=CoderUseExtensionFlag;
2279 if (*version != '\0')
2280 entry->version=ConstantString(version);
2281 entry->mime_type=ConstantString("image/tiff");
2282 (void) RegisterMagickInfo(entry);
2283 return(MagickImageCoderSignature);
2287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2291 % U n r e g i s t e r T I F F I m a g e %
2295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2297 % UnregisterTIFFImage() removes format registrations made by the TIFF module
2298 % from the list of supported formats.
2300 % The format of the UnregisterTIFFImage method is:
2302 % UnregisterTIFFImage(void)
2305 ModuleExport void UnregisterTIFFImage(void)
2307 (void) UnregisterMagickInfo("TIFF64");
2308 (void) UnregisterMagickInfo("TIFF");
2309 (void) UnregisterMagickInfo("TIF");
2310 (void) UnregisterMagickInfo("PTIF");
2311 if (tiff_semaphore == (SemaphoreInfo *) NULL)
2312 ActivateSemaphoreInfo(&tiff_semaphore);
2313 LockSemaphoreInfo(tiff_semaphore);
2314 if (instantiate_key != MagickFalse)
2316 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2317 if (tag_extender == (TIFFExtendProc) NULL)
2318 (void) TIFFSetTagExtender(tag_extender);
2320 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
2321 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2322 instantiate_key=MagickFalse;
2324 UnlockSemaphoreInfo(tiff_semaphore);
2325 RelinquishSemaphoreInfo(&tiff_semaphore);
2328 #if defined(MAGICKCORE_TIFF_DELEGATE)
2330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2334 % W r i t e G R O U P 4 I m a g e %
2338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2340 % WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
2342 % The format of the WriteGROUP4Image method is:
2344 % MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2345 % Image *image,ExceptionInfo *)
2347 % A description of each parameter follows:
2349 % o image_info: the image info.
2351 % o image: The image.
2353 % o exception: return any errors or warnings in this structure.
2356 static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2357 Image *image,ExceptionInfo *exception)
2360 filename[MagickPathExtent];
2394 Write image as CCITT Group4 TIFF image to a temporary file.
2396 assert(image_info != (const ImageInfo *) NULL);
2397 assert(image_info->signature == MagickSignature);
2398 assert(image != (Image *) NULL);
2399 assert(image->signature == MagickSignature);
2400 if (image->debug != MagickFalse)
2401 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2402 assert(exception != (ExceptionInfo *) NULL);
2403 assert(exception->signature == MagickSignature);
2404 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2405 if (status == MagickFalse)
2407 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
2408 if (huffman_image == (Image *) NULL)
2410 (void) CloseBlob(image);
2411 return(MagickFalse);
2413 huffman_image->endian=MSBEndian;
2415 unique_file=AcquireUniqueFileResource(filename);
2416 if (unique_file != -1)
2417 file=fdopen(unique_file,"wb");
2418 if ((unique_file == -1) || (file == (FILE *) NULL))
2420 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2422 return(MagickFalse);
2424 (void) FormatLocaleString(huffman_image->filename,MagickPathExtent,"tiff:%s",
2426 (void) SetImageType(huffman_image,BilevelType,exception);
2427 write_info=CloneImageInfo((ImageInfo *) NULL);
2428 SetImageInfoFile(write_info,file);
2429 write_info->compression=Group4Compression;
2430 write_info->type=BilevelType;
2431 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
2432 status=WriteTIFFImage(write_info,huffman_image,exception);
2433 (void) fflush(file);
2434 write_info=DestroyImageInfo(write_info);
2435 if (status == MagickFalse)
2437 huffman_image=DestroyImage(huffman_image);
2438 (void) fclose(file);
2439 (void) RelinquishUniqueFileResource(filename);
2440 return(MagickFalse);
2442 tiff=TIFFOpen(filename,"rb");
2443 if (tiff == (TIFF *) NULL)
2445 huffman_image=DestroyImage(huffman_image);
2446 (void) fclose(file);
2447 (void) RelinquishUniqueFileResource(filename);
2448 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
2449 image_info->filename);
2450 return(MagickFalse);
2453 Allocate raw strip buffer.
2455 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2458 huffman_image=DestroyImage(huffman_image);
2459 (void) fclose(file);
2460 (void) RelinquishUniqueFileResource(filename);
2461 return(MagickFalse);
2463 strip_size=byte_count[0];
2464 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2465 if (byte_count[i] > strip_size)
2466 strip_size=byte_count[i];
2467 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2469 if (buffer == (unsigned char *) NULL)
2472 huffman_image=DestroyImage(huffman_image);
2473 (void) fclose(file);
2474 (void) RelinquishUniqueFileResource(filename);
2475 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2476 image_info->filename);
2479 Compress runlength encoded to 2D Huffman pixels.
2481 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2483 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
2484 if (WriteBlob(image,(size_t) count,buffer) != count)
2487 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2489 huffman_image=DestroyImage(huffman_image);
2490 (void) fclose(file);
2491 (void) RelinquishUniqueFileResource(filename);
2492 (void) CloseBlob(image);
2497 #if defined(MAGICKCORE_TIFF_DELEGATE)
2499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2503 % W r i t e P T I F I m a g e %
2507 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2509 % WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2512 % The format of the WritePTIFImage method is:
2514 % MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2515 % Image *image,ExceptionInfo *exception)
2517 % A description of each parameter follows:
2519 % o image_info: the image info.
2521 % o image: The image.
2523 % o exception: return any errors or warnings in this structure.
2526 static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2527 Image *image,ExceptionInfo *exception)
2548 Create pyramid-encoded TIFF image.
2550 images=NewImageList();
2551 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2556 clone_image=CloneImage(next,0,0,MagickFalse,exception);
2557 clone_image->previous=NewImageList();
2558 clone_image->next=NewImageList();
2559 AppendImageToList(&images,clone_image);
2560 columns=next->columns;
2562 resolution=next->resolution;
2563 while ((columns > 64) && (rows > 64))
2569 pyramid_image=ResizeImage(next,columns,rows,image->filter,exception);
2570 if (pyramid_image == (Image *) NULL)
2572 pyramid_image->resolution=resolution;
2573 AppendImageToList(&images,pyramid_image);
2576 images=GetFirstImageInList(images);
2578 Write pyramid-encoded TIFF image.
2580 write_info=CloneImageInfo(image_info);
2581 write_info->adjoin=MagickTrue;
2582 (void) CopyMagickString(write_info->magick,"TIFF",MagickPathExtent);
2583 (void) CopyMagickString(images->magick,"TIFF",MagickPathExtent);
2584 status=WriteTIFFImage(write_info,images,exception);
2585 images=DestroyImageList(images);
2586 write_info=DestroyImageInfo(write_info);
2591 #if defined(MAGICKCORE_TIFF_DELEGATE)
2593 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2596 % W r i t e T I F F I m a g e %
2600 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2602 % WriteTIFFImage() writes an image in the Tagged image file format.
2604 % The format of the WriteTIFFImage method is:
2606 % MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2607 % Image *image,ExceptionInfo *exception)
2609 % A description of each parameter follows:
2611 % o image_info: the image info.
2613 % o image: The image.
2615 % o exception: return any errors or warnings in this structure.
2619 typedef struct _TIFFInfo
2630 static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2632 assert(tiff_info != (TIFFInfo *) NULL);
2633 if (tiff_info->scanlines != (unsigned char *) NULL)
2634 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2635 tiff_info->scanlines);
2636 if (tiff_info->pixels != (unsigned char *) NULL)
2637 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2641 static MagickBooleanType EncodeLabImage(Image *image,ExceptionInfo *exception)
2653 image_view=AcquireAuthenticCacheView(image,exception);
2654 for (y=0; y < (ssize_t) image->rows; y++)
2662 if (status == MagickFalse)
2664 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2665 if (q == (Quantum *) NULL)
2670 for (x=0; x < (ssize_t) image->columns; x++)
2676 a=QuantumScale*GetPixela(image,q)-0.5;
2679 b=QuantumScale*GetPixelb(image,q)-0.5;
2682 SetPixela(image,QuantumRange*a,q);
2683 SetPixelb(image,QuantumRange*b,q);
2684 q+=GetPixelChannels(image);
2686 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2689 image_view=DestroyCacheView(image_view);
2693 static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,
2694 TIFF *tiff,TIFFInfo *tiff_info)
2706 assert(tiff_info != (TIFFInfo *) NULL);
2707 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2708 option=GetImageOption(image_info,"tiff:tile-geometry");
2709 if (option == (const char *) NULL)
2711 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2712 if ((flags & HeightValue) == 0)
2713 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2714 tile_columns=(uint32) tiff_info->tile_geometry.width;
2715 tile_rows=(uint32) tiff_info->tile_geometry.height;
2716 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2717 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2718 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2719 tiff_info->tile_geometry.width=tile_columns;
2720 tiff_info->tile_geometry.height=tile_rows;
2721 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2722 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
2723 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2724 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2725 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2726 (tiff_info->pixels == (unsigned char *) NULL))
2728 DestroyTIFFInfo(tiff_info);
2729 return(MagickFalse);
2734 static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
2735 tsample_t sample,Image *image)
2743 register unsigned char
2757 if (TIFFIsTiled(tiff) == 0)
2758 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2760 Fill scanlines to tile height.
2762 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2763 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2764 (size_t) TIFFScanlineSize(tiff));
2765 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2766 (tiff_info->tile_geometry.height-1)) &&
2767 (row != (ssize_t) (image->rows-1)))
2770 Write tile to TIFF image.
2773 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (
2774 tiff_info->tile_geometry.height*tiff_info->tile_geometry.width);
2775 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2776 tiff_info->tile_geometry.width;
2777 for (i=0; i < (ssize_t) number_tiles; i++)
2779 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
2780 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2781 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2782 for (k=0; k < (ssize_t) tile_width; k++)
2784 if (bytes_per_pixel == 0)
2786 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2787 tiff_info->tile_geometry.width+k)/8);
2788 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2792 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2793 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2794 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2795 for (l=0; l < bytes_per_pixel; l++)
2798 if ((i*tiff_info->tile_geometry.width) != image->columns)
2799 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2800 tiff_info->tile_geometry.width),(uint32) ((row/
2801 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2809 static void TIFFSetProfiles(TIFF *tiff,Image *image)
2817 if (image->profiles == (void *) NULL)
2819 ResetImageProfileIterator(image);
2820 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2822 profile=GetImageProfile(image,name);
2823 if (GetStringInfoLength(profile) == 0)
2825 name=GetNextImageProfile(image);
2828 #if defined(TIFFTAG_XMLPACKET)
2829 if (LocaleCompare(name,"xmp") == 0)
2830 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2831 profile),GetStringInfoDatum(profile));
2833 #if defined(TIFFTAG_ICCPROFILE)
2834 if (LocaleCompare(name,"icc") == 0)
2835 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2836 profile),GetStringInfoDatum(profile));
2838 if (LocaleCompare(name,"iptc") == 0)
2846 iptc_profile=CloneStringInfo(profile);
2847 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2849 SetStringInfoLength(iptc_profile,length);
2850 if (TIFFIsByteSwapped(tiff))
2851 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2852 (unsigned long) (length/4));
2853 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2854 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2855 iptc_profile=DestroyStringInfo(iptc_profile);
2857 #if defined(TIFFTAG_PHOTOSHOP)
2858 if (LocaleCompare(name,"8bim") == 0)
2859 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2860 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2862 if (LocaleCompare(name,"tiff:37724") == 0)
2863 (void) TIFFSetField(tiff,37724,(uint32) GetStringInfoLength(profile),
2864 GetStringInfoDatum(profile));
2865 if (LocaleCompare(name,"tiff:34118") == 0)
2866 (void) TIFFSetField(tiff,34118,(uint32) GetStringInfoLength(profile),
2867 GetStringInfoDatum(profile));
2868 name=GetNextImageProfile(image);
2872 static void TIFFSetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
2877 value=GetImageArtifact(image,"tiff:document");
2878 if (value != (const char *) NULL)
2879 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,value);
2880 value=GetImageArtifact(image,"tiff:hostcomputer");
2881 if (value != (const char *) NULL)
2882 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2883 value=GetImageArtifact(image,"tiff:artist");
2884 if (value != (const char *) NULL)
2885 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2886 value=GetImageArtifact(image,"tiff:timestamp");
2887 if (value != (const char *) NULL)
2888 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2889 value=GetImageArtifact(image,"tiff:make");
2890 if (value != (const char *) NULL)
2891 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2892 value=GetImageArtifact(image,"tiff:model");
2893 if (value != (const char *) NULL)
2894 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2895 value=GetImageArtifact(image,"tiff:software");
2896 if (value != (const char *) NULL)
2897 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
2898 value=GetImageArtifact(image,"tiff:copyright");
2899 if (value != (const char *) NULL)
2900 (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value);
2901 value=GetImageArtifact(image,"kodak-33423");
2902 if (value != (const char *) NULL)
2903 (void) TIFFSetField(tiff,33423,value);
2904 value=GetImageArtifact(image,"kodak-36867");
2905 if (value != (const char *) NULL)
2906 (void) TIFFSetField(tiff,36867,value);
2907 value=GetImageProperty(image,"label",exception);
2908 if (value != (const char *) NULL)
2909 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2910 value=GetImageProperty(image,"comment",exception);
2911 if (value != (const char *) NULL)
2912 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2915 static void TIFFSetEXIFProperties(TIFF *tiff,Image *image,
2916 ExceptionInfo *exception)
2918 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2929 Write EXIF properties.
2932 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2933 for (i=0; exif_info[i].tag != 0; i++)
2935 value=GetImageProperty(image,exif_info[i].property,exception);
2936 if (value == (const char *) NULL)
2938 switch (exif_info[i].type)
2942 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2950 field=(uint16) StringToLong(value);
2951 (void) TIFFSetField(tiff,exif_info[i].tag,field);
2959 field=(uint16) StringToLong(value);
2960 (void) TIFFSetField(tiff,exif_info[i].tag,field);
2964 case TIFF_SRATIONAL:
2969 field=StringToDouble(value,(char **) NULL);
2970 (void) TIFFSetField(tiff,exif_info[i].tag,field);
2977 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2984 static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2985 Image *image,ExceptionInfo *exception)
2987 #if !defined(TIFFDefaultStripSize)
2988 #define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
3048 assert(image_info != (const ImageInfo *) NULL);
3049 assert(image_info->signature == MagickSignature);
3050 assert(image != (Image *) NULL);
3051 assert(image->signature == MagickSignature);
3052 if (image->debug != MagickFalse)
3053 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3054 assert(exception != (ExceptionInfo *) NULL);
3055 assert(exception->signature == MagickSignature);
3056 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
3057 if (status == MagickFalse)
3059 (void) MagickSetThreadValue(tiff_exception,exception);
3060 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
3061 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
3062 endian_type=UndefinedEndian;
3063 option=GetImageOption(image_info,"tiff:endian");
3064 if (option != (const char *) NULL)
3066 if (LocaleNCompare(option,"msb",3) == 0)
3067 endian_type=MSBEndian;
3068 if (LocaleNCompare(option,"lsb",3) == 0)
3069 endian_type=LSBEndian;;
3071 switch (endian_type)
3073 case LSBEndian: mode="wl"; break;
3074 case MSBEndian: mode="wb"; break;
3075 default: mode="w"; break;
3077 #if defined(TIFF_VERSION_BIG)
3078 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
3079 switch (endian_type)
3081 case LSBEndian: mode="wl8"; break;
3082 case MSBEndian: mode="wb8"; break;
3083 default: mode="w8"; break;
3086 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
3087 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
3089 if (tiff == (TIFF *) NULL)
3091 (void) TIFFSetWarningHandler(warning_handler);
3092 (void) TIFFSetErrorHandler(error_handler);
3093 return(MagickFalse);
3096 debug=IsEventLogging();
3101 Initialize TIFF fields.
3103 if ((image_info->type != UndefinedType) &&
3104 (image_info->type != OptimizeType))
3105 (void) SetImageType(image,image_info->type,exception);
3106 compression=UndefinedCompression;
3107 if (image->compression != JPEGCompression)
3108 compression=image->compression;
3109 if (image_info->compression != UndefinedCompression)
3110 compression=image_info->compression;
3111 switch (compression)
3113 case FaxCompression:
3114 case Group4Compression:
3116 (void) SetImageType(image,BilevelType,exception);
3119 case JPEGCompression:
3121 (void) SetImageStorageClass(image,DirectClass,exception);
3122 (void) SetImageDepth(image,8,exception);
3128 quantum_info=AcquireQuantumInfo(image_info,image);
3129 if (quantum_info == (QuantumInfo *) NULL)
3130 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3131 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
3132 (quantum_info->format == UndefinedQuantumFormat) &&
3133 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
3135 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
3136 if (status == MagickFalse)
3137 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3139 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
3140 (GetPreviousImageInList(image) != (Image *) NULL))
3141 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
3142 if ((image->columns != (uint32) image->columns) ||
3143 (image->rows != (uint32) image->rows))
3144 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
3145 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
3146 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
3147 switch (compression)
3149 case FaxCompression:
3151 compress_tag=COMPRESSION_CCITTFAX3;
3152 SetQuantumMinIsWhite(quantum_info,MagickTrue);
3155 case Group4Compression:
3157 compress_tag=COMPRESSION_CCITTFAX4;
3158 SetQuantumMinIsWhite(quantum_info,MagickTrue);
3161 #if defined(COMPRESSION_JBIG)
3162 case JBIG1Compression:
3164 compress_tag=COMPRESSION_JBIG;
3168 case JPEGCompression:
3170 compress_tag=COMPRESSION_JPEG;
3173 #if defined(COMPRESSION_LZMA)
3174 case LZMACompression:
3176 compress_tag=COMPRESSION_LZMA;
3180 case LZWCompression:
3182 compress_tag=COMPRESSION_LZW;
3185 case RLECompression:
3187 compress_tag=COMPRESSION_PACKBITS;
3190 case ZipCompression:
3192 compress_tag=COMPRESSION_ADOBE_DEFLATE;
3198 compress_tag=COMPRESSION_NONE;
3202 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
3203 if ((compress_tag != COMPRESSION_NONE) &&
3204 (TIFFIsCODECConfigured(compress_tag) == 0))
3206 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3207 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
3208 MagickCompressOptions,(ssize_t) compression));
3209 compress_tag=COMPRESSION_NONE;
3210 compression=NoCompression;
3213 switch (compress_tag)
3215 #if defined(CCITT_SUPPORT)
3216 case COMPRESSION_CCITTFAX3:
3217 case COMPRESSION_CCITTFAX4:
3219 #if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
3220 case COMPRESSION_JPEG:
3222 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3223 case COMPRESSION_LZMA:
3225 #if defined(LZW_SUPPORT)
3226 case COMPRESSION_LZW:
3228 #if defined(PACKBITS_SUPPORT)
3229 case COMPRESSION_PACKBITS:
3231 #if defined(ZIP_SUPPORT)
3232 case COMPRESSION_ADOBE_DEFLATE:
3234 case COMPRESSION_NONE:
3238 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3239 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
3240 MagickCompressOptions,(ssize_t) compression));
3241 compress_tag=COMPRESSION_NONE;
3242 compression=NoCompression;
3247 if (image->colorspace == CMYKColorspace)
3249 photometric=PHOTOMETRIC_SEPARATED;
3250 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
3251 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
3256 Full color TIFF raster.
3258 if (image->colorspace == LabColorspace)
3260 photometric=PHOTOMETRIC_CIELAB;
3261 EncodeLabImage(image,exception);
3264 if (image->colorspace == YCbCrColorspace)
3266 photometric=PHOTOMETRIC_YCBCR;
3267 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
3268 (void) SetImageStorageClass(image,DirectClass,exception);
3269 (void) SetImageDepth(image,8,exception);
3272 photometric=PHOTOMETRIC_RGB;
3273 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
3274 if ((image_info->type != TrueColorType) &&
3275 (image_info->type != TrueColorAlphaType))
3277 if ((image_info->type != PaletteType) &&
3278 (SetImageGray(image,exception) != MagickFalse))
3280 photometric=(uint16) (quantum_info->min_is_white !=
3281 MagickFalse ? PHOTOMETRIC_MINISWHITE :
3282 PHOTOMETRIC_MINISBLACK);
3283 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
3284 if ((image_info->depth == 0) &&
3285 (image->alpha_trait == UndefinedPixelTrait) &&
3286 (SetImageMonochrome(image,exception) != MagickFalse))
3288 status=SetQuantumDepth(image,quantum_info,1);
3289 if (status == MagickFalse)
3290 ThrowWriterException(ResourceLimitError,
3291 "MemoryAllocationFailed");
3295 if (image->storage_class == PseudoClass)
3301 Colormapped TIFF raster.
3303 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
3304 photometric=PHOTOMETRIC_PALETTE;
3306 while ((GetQuantumRange(depth)+1) < image->colors)
3308 status=SetQuantumDepth(image,quantum_info,depth);
3309 if (status == MagickFalse)
3310 ThrowWriterException(ResourceLimitError,
3311 "MemoryAllocationFailed");
3315 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
3316 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
3317 (photometric != PHOTOMETRIC_MINISWHITE))
3319 compress_tag=COMPRESSION_NONE;
3320 endian=FILLORDER_MSB2LSB;
3323 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
3324 (photometric != PHOTOMETRIC_MINISWHITE))
3326 compress_tag=COMPRESSION_NONE;
3327 endian=FILLORDER_MSB2LSB;
3329 option=GetImageOption(image_info,"tiff:fill-order");
3330 if (option != (const char *) NULL)
3332 if (LocaleNCompare(option,"msb",3) == 0)
3333 endian=FILLORDER_MSB2LSB;
3334 if (LocaleNCompare(option,"lsb",3) == 0)
3335 endian=FILLORDER_LSB2MSB;
3337 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
3338 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
3339 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
3340 if (image->alpha_trait != UndefinedPixelTrait)
3348 TIFF has a matte channel.
3351 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
3352 option=GetImageOption(image_info,"tiff:alpha");
3353 if (option != (const char *) NULL)
3355 if (LocaleCompare(option,"associated") == 0)
3356 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
3358 if (LocaleCompare(option,"unspecified") == 0)
3359 sample_info[0]=EXTRASAMPLE_UNSPECIFIED;
3361 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
3362 &samples_per_pixel);
3363 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
3364 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
3366 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
3367 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
3369 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
3370 switch (quantum_info->format)
3372 case FloatingPointQuantumFormat:
3374 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
3375 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
3376 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
3379 case SignedQuantumFormat:
3381 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
3384 case UnsignedQuantumFormat:
3386 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
3392 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
3393 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
3394 if (photometric == PHOTOMETRIC_RGB)
3395 if ((image_info->interlace == PlaneInterlace) ||
3396 (image_info->interlace == PartitionInterlace))
3397 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
3398 rows_per_strip=TIFFDefaultStripSize(tiff,0);
3399 option=GetImageOption(image_info,"tiff:rows-per-strip");
3400 if (option != (const char *) NULL)
3401 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
3402 switch (compress_tag)
3404 case COMPRESSION_JPEG:
3406 #if defined(JPEG_SUPPORT)
3416 rows_per_strip+=(16-(rows_per_strip % 16));
3417 if (image_info->quality != UndefinedCompressionQuality)
3418 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
3419 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
3420 if (IssRGBCompatibleColorspace(image->colorspace) != MagickFalse)
3425 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
3426 sampling_factor=(const char *) NULL;
3427 value=GetImageProperty(image,"jpeg:sampling-factor",exception);
3428 if (value != (char *) NULL)
3430 sampling_factor=value;
3431 if (image->debug != MagickFalse)
3432 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3433 " Input sampling-factors=%s",sampling_factor);
3435 if (image_info->sampling_factor != (char *) NULL)
3436 sampling_factor=image_info->sampling_factor;
3437 if (sampling_factor != (const char *) NULL)
3439 flags=ParseGeometry(sampling_factor,&geometry_info);
3440 if ((flags & SigmaValue) == 0)
3441 geometry_info.sigma=geometry_info.rho;
3442 if (image->colorspace == YCbCrColorspace)
3443 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
3444 geometry_info.rho,(uint16) geometry_info.sigma);
3447 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3449 if (bits_per_sample == 12)
3450 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
3454 case COMPRESSION_ADOBE_DEFLATE:
3456 rows_per_strip=(uint32) image->rows;
3457 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3459 if (((photometric == PHOTOMETRIC_RGB) ||
3460 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3461 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3462 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3463 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
3464 image_info->quality == UndefinedCompressionQuality ? 7 :
3465 MagickMin((ssize_t) image_info->quality/10,9)));
3468 case COMPRESSION_CCITTFAX3:
3473 rows_per_strip=(uint32) image->rows;
3474 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
3477 case COMPRESSION_CCITTFAX4:
3479 rows_per_strip=(uint32) image->rows;
3482 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3483 case COMPRESSION_LZMA:
3485 if (((photometric == PHOTOMETRIC_RGB) ||
3486 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3487 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3488 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3489 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
3490 image_info->quality == UndefinedCompressionQuality ? 7 :
3491 MagickMin((ssize_t) image_info->quality/10,9)));
3495 case COMPRESSION_LZW:
3497 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3499 if (((photometric == PHOTOMETRIC_RGB) ||
3500 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3501 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3502 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3508 if (rows_per_strip < 1)
3510 if ((image->rows/rows_per_strip) >= (1UL << 15))
3511 rows_per_strip=(uint32) (image->rows >> 15);
3512 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
3513 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
3519 Set image resolution.
3522 if (image->units == PixelsPerInchResolution)
3524 if (image->units == PixelsPerCentimeterResolution)
3525 units=RESUNIT_CENTIMETER;
3526 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
3527 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x);
3528 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y);
3529 if ((image->page.x < 0) || (image->page.y < 0))
3530 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3531 "TIFF: negative image positions unsupported","%s",image->filename);
3532 if ((image->page.x > 0) && (image->resolution.x > 0.0))
3535 Set horizontal image position.
3537 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
3538 image->resolution.x);
3540 if ((image->page.y > 0) && (image->resolution.y > 0.0))
3543 Set vertical image position.
3545 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
3546 image->resolution.y);
3549 if (image->chromaticity.white_point.x != 0.0)
3555 Set image chromaticity.
3557 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3558 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3559 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3560 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3561 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3562 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3563 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3564 chromaticity[0]=(float) image->chromaticity.white_point.x;
3565 chromaticity[1]=(float) image->chromaticity.white_point.y;
3566 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3568 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3569 (image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3571 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3572 if (image->scene != 0)
3573 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3574 GetImageListLength(image));
3576 if (image->orientation != UndefinedOrientation)
3577 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3578 (void) TIFFSetProfiles(tiff,image);
3584 page=(uint16) scene;
3585 pages=(uint16) GetImageListLength(image);
3586 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3587 (image_info->adjoin != MagickFalse) && (pages > 1))
3588 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3589 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3591 (void) TIFFSetProperties(tiff,image,exception);
3592 DisableMSCWarning(4127)
3595 (void) TIFFSetEXIFProperties(tiff,image,exception);
3597 Write image scanlines.
3599 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3600 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3601 quantum_info->endian=LSBEndian;
3602 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
3603 tiff_info.scanline=(unsigned char *) GetQuantumPixels(quantum_info);
3604 switch (photometric)
3606 case PHOTOMETRIC_CIELAB:
3607 case PHOTOMETRIC_YCBCR:
3608 case PHOTOMETRIC_RGB:
3613 switch (image_info->interlace)
3618 quantum_type=RGBQuantum;
3619 if (image->alpha_trait != UndefinedPixelTrait)
3620 quantum_type=RGBAQuantum;
3621 for (y=0; y < (ssize_t) image->rows; y++)
3623 register const Quantum
3626 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3627 if (p == (const Quantum *) NULL)
3629 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3630 quantum_type,pixels,exception);
3632 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3634 if (image->previous == (Image *) NULL)
3636 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3638 if (status == MagickFalse)
3644 case PlaneInterlace:
3645 case PartitionInterlace:
3648 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3650 for (y=0; y < (ssize_t) image->rows; y++)
3652 register const Quantum
3655 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3656 if (p == (const Quantum *) NULL)
3658 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3659 RedQuantum,pixels,exception);
3660 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3663 if (image->previous == (Image *) NULL)
3665 status=SetImageProgress(image,SaveImageTag,100,400);
3666 if (status == MagickFalse)
3669 for (y=0; y < (ssize_t) image->rows; y++)
3671 register const Quantum
3674 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3675 if (p == (const Quantum *) NULL)
3677 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3678 GreenQuantum,pixels,exception);
3679 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3682 if (image->previous == (Image *) NULL)
3684 status=SetImageProgress(image,SaveImageTag,200,400);
3685 if (status == MagickFalse)
3688 for (y=0; y < (ssize_t) image->rows; y++)
3690 register const Quantum
3693 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3694 if (p == (const Quantum *) NULL)
3696 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3697 BlueQuantum,pixels,exception);
3698 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3701 if (image->previous == (Image *) NULL)
3703 status=SetImageProgress(image,SaveImageTag,300,400);
3704 if (status == MagickFalse)
3707 if (image->alpha_trait != UndefinedPixelTrait)
3708 for (y=0; y < (ssize_t) image->rows; y++)
3710 register const Quantum
3713 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3714 if (p == (const Quantum *) NULL)
3716 length=ExportQuantumPixels(image,(CacheView *) NULL,
3717 quantum_info,AlphaQuantum,pixels,exception);
3718 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3721 if (image->previous == (Image *) NULL)
3723 status=SetImageProgress(image,SaveImageTag,400,400);
3724 if (status == MagickFalse)
3732 case PHOTOMETRIC_SEPARATED:
3737 quantum_type=CMYKQuantum;
3738 if (image->alpha_trait != UndefinedPixelTrait)
3739 quantum_type=CMYKAQuantum;
3740 if (image->colorspace != CMYKColorspace)
3741 (void) TransformImageColorspace(image,CMYKColorspace,exception);
3742 for (y=0; y < (ssize_t) image->rows; y++)
3744 register const Quantum
3747 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3748 if (p == (const Quantum *) NULL)
3750 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3751 quantum_type,pixels,exception);
3752 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3754 if (image->previous == (Image *) NULL)
3756 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3758 if (status == MagickFalse)
3764 case PHOTOMETRIC_PALETTE:
3772 Colormapped TIFF image.
3774 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3775 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3776 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3777 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3778 (blue == (uint16 *) NULL))
3779 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3781 Initialize TIFF colormap.
3783 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3784 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3785 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3786 for (i=0; i < (ssize_t) image->colors; i++)
3788 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3789 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3790 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3792 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3793 red=(uint16 *) RelinquishMagickMemory(red);
3794 green=(uint16 *) RelinquishMagickMemory(green);
3795 blue=(uint16 *) RelinquishMagickMemory(blue);
3800 Convert PseudoClass packets to contiguous grayscale scanlines.
3802 quantum_type=IndexQuantum;
3803 if (image->alpha_trait != UndefinedPixelTrait)
3805 if (photometric != PHOTOMETRIC_PALETTE)
3806 quantum_type=GrayAlphaQuantum;
3808 quantum_type=IndexAlphaQuantum;
3811 if (photometric != PHOTOMETRIC_PALETTE)
3812 quantum_type=GrayQuantum;
3813 for (y=0; y < (ssize_t) image->rows; y++)
3815 register const Quantum
3818 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3819 if (p == (const Quantum *) NULL)
3821 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3822 quantum_type,pixels,exception);
3823 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3825 if (image->previous == (Image *) NULL)
3827 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3829 if (status == MagickFalse)
3836 quantum_info=DestroyQuantumInfo(quantum_info);
3837 if (image->colorspace == LabColorspace)
3838 DecodeLabImage(image,exception);
3839 DestroyTIFFInfo(&tiff_info);
3840 DisableMSCWarning(4127)
3841 if (0 && (image_info->verbose != MagickFalse))
3843 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3844 (void) TIFFWriteDirectory(tiff);
3845 image=SyncNextImageInList(image);
3846 if (image == (Image *) NULL)
3848 status=SetImageProgress(image,SaveImagesTag,scene++,
3849 GetImageListLength(image));
3850 if (status == MagickFalse)
3852 } while (image_info->adjoin != MagickFalse);
3853 (void) TIFFSetWarningHandler(warning_handler);
3854 (void) TIFFSetErrorHandler(error_handler);