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 == MagickCoreSignature);
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 == MagickCoreSignature);
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 /* Setting the mode to a value that won't change the colorspace */
997 ReadPSDLayers(layers,image_info,&info,MagickFalse,exception);
998 DeleteImageFromList(&layers);
999 if (layers != (Image *) NULL)
1001 AppendImageToList(&image,layers);
1002 while (layers != (Image *) NULL)
1004 DetachBlob(layers->blob);
1005 layers=GetNextImageInList(layers);
1010 #if defined(__cplusplus) || defined(c_plusplus)
1014 static Image *ReadTIFFImage(const ImageInfo *image_info,
1015 ExceptionInfo *exception)
1093 assert(image_info != (const ImageInfo *) NULL);
1094 assert(image_info->signature == MagickCoreSignature);
1095 if (image_info->debug != MagickFalse)
1096 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1097 image_info->filename);
1098 assert(exception != (ExceptionInfo *) NULL);
1099 assert(exception->signature == MagickCoreSignature);
1100 image=AcquireImage(image_info,exception);
1101 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1102 if (status == MagickFalse)
1104 image=DestroyImageList(image);
1105 return((Image *) NULL);
1107 (void) MagickSetThreadValue(tiff_exception,exception);
1108 error_handler=TIFFSetErrorHandler(TIFFErrors);
1109 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
1110 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
1111 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
1113 if (tiff == (TIFF *) NULL)
1115 (void) TIFFSetWarningHandler(warning_handler);
1116 (void) TIFFSetErrorHandler(error_handler);
1117 image=DestroyImageList(image);
1118 return((Image *) NULL);
1120 debug=IsEventLogging();
1122 if (image_info->number_scenes != 0)
1125 Generate blank images for subimage specification (e.g. image.tif[4].
1127 for (i=0; i < (ssize_t) image_info->scene; i++)
1129 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1130 if (status == MagickFalse)
1133 image=DestroyImageList(image);
1134 return((Image *) NULL);
1136 AcquireNextImage(image_info,image,exception);
1137 if (GetNextImageInList(image) == (Image *) NULL)
1140 image=DestroyImageList(image);
1141 return((Image *) NULL);
1143 image=SyncNextImageInList(image);
1148 DisableMSCWarning(4127)
1149 if (0 && (image_info->verbose != MagickFalse))
1150 TIFFPrintDirectory(tiff,stdout,MagickFalse);
1152 if ((TIFFGetField(tiff,TIFFTAG_IMAGEWIDTH,&width) != 1) ||
1153 (TIFFGetField(tiff,TIFFTAG_IMAGELENGTH,&height) != 1) ||
1154 (TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag) != 1) ||
1155 (TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian) != 1) ||
1156 (TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace) != 1) ||
1157 (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,&samples_per_pixel) != 1) ||
1158 (TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample) != 1) ||
1159 (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format) != 1) ||
1160 (TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value) != 1) ||
1161 (TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value) != 1) ||
1162 (TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric) != 1))
1165 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1167 if (sample_format == SAMPLEFORMAT_IEEEFP)
1168 (void) SetImageProperty(image,"quantum:format","floating-point",
1170 switch (photometric)
1172 case PHOTOMETRIC_MINISBLACK:
1174 (void) SetImageProperty(image,"tiff:photometric","min-is-black",
1178 case PHOTOMETRIC_MINISWHITE:
1180 (void) SetImageProperty(image,"tiff:photometric","min-is-white",
1184 case PHOTOMETRIC_PALETTE:
1186 (void) SetImageProperty(image,"tiff:photometric","palette",exception);
1189 case PHOTOMETRIC_RGB:
1191 (void) SetImageProperty(image,"tiff:photometric","RGB",exception);
1194 case PHOTOMETRIC_CIELAB:
1196 (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception);
1199 case PHOTOMETRIC_LOGL:
1201 (void) SetImageProperty(image,"tiff:photometric","CIE Log2(L)",exception);
1204 case PHOTOMETRIC_LOGLUV:
1206 (void) SetImageProperty(image,"tiff:photometric","LOGLUV",exception);
1209 #if defined(PHOTOMETRIC_MASK)
1210 case PHOTOMETRIC_MASK:
1212 (void) SetImageProperty(image,"tiff:photometric","MASK",exception);
1216 case PHOTOMETRIC_SEPARATED:
1218 (void) SetImageProperty(image,"tiff:photometric","separated",exception);
1221 case PHOTOMETRIC_YCBCR:
1223 (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception);
1228 (void) SetImageProperty(image,"tiff:photometric","unknown",exception);
1232 if (image->debug != MagickFalse)
1234 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
1235 (unsigned int) width,(unsigned int) height);
1236 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
1238 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1239 "Bits per sample: %u",bits_per_sample);
1240 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1241 "Min sample value: %u",min_sample_value);
1242 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1243 "Max sample value: %u",max_sample_value);
1244 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
1245 "interpretation: %s",GetImageProperty(image,"tiff:photometric",
1248 image->columns=(size_t) width;
1249 image->rows=(size_t) height;
1250 image->depth=(size_t) bits_per_sample;
1251 status=SetImageExtent(image,image->columns,image->rows,exception);
1252 if (status == MagickFalse)
1253 return(DestroyImageList(image));
1254 if (image->debug != MagickFalse)
1255 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
1256 (double) image->depth);
1257 image->endian=MSBEndian;
1258 if (endian == FILLORDER_LSB2MSB)
1259 image->endian=LSBEndian;
1260 #if defined(MAGICKCORE_HAVE_TIFFISBIGENDIAN)
1261 if (TIFFIsBigEndian(tiff) == 0)
1263 (void) SetImageProperty(image,"tiff:endian","lsb",exception);
1264 image->endian=LSBEndian;
1268 (void) SetImageProperty(image,"tiff:endian","msb",exception);
1269 image->endian=MSBEndian;
1272 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1273 (photometric == PHOTOMETRIC_MINISWHITE))
1274 SetImageColorspace(image,GRAYColorspace,exception);
1275 if (photometric == PHOTOMETRIC_SEPARATED)
1276 SetImageColorspace(image,CMYKColorspace,exception);
1277 if (photometric == PHOTOMETRIC_CIELAB)
1278 SetImageColorspace(image,LabColorspace,exception);
1279 TIFFGetProfiles(tiff,image,image_info->ping,exception);
1280 TIFFGetProperties(tiff,image,exception);
1281 option=GetImageOption(image_info,"tiff:exif-properties");
1282 if (IfMagickTrue(IsStringNotFalse(option))) /* enabled by default */
1283 TIFFGetEXIFProperties(tiff,image,exception);
1284 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
1285 &samples_per_pixel);
1286 if ((TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution) == 1) &&
1287 (TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution) == 1))
1289 image->resolution.x=x_resolution;
1290 image->resolution.y=y_resolution;
1292 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units) == 1)
1294 if (units == RESUNIT_INCH)
1295 image->units=PixelsPerInchResolution;
1296 if (units == RESUNIT_CENTIMETER)
1297 image->units=PixelsPerCentimeterResolution;
1299 if ((TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position) == 1) &&
1300 (TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position) == 1))
1302 image->page.x=(ssize_t) ceil(x_position*image->resolution.x-0.5);
1303 image->page.y=(ssize_t) ceil(y_position*image->resolution.y-0.5);
1305 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation) == 1)
1306 image->orientation=(OrientationType) orientation;
1307 if (TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity) == 1)
1309 if (chromaticity != (float *) NULL)
1311 image->chromaticity.white_point.x=chromaticity[0];
1312 image->chromaticity.white_point.y=chromaticity[1];
1315 if (TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity) == 1)
1317 if (chromaticity != (float *) NULL)
1319 image->chromaticity.red_primary.x=chromaticity[0];
1320 image->chromaticity.red_primary.y=chromaticity[1];
1321 image->chromaticity.green_primary.x=chromaticity[2];
1322 image->chromaticity.green_primary.y=chromaticity[3];
1323 image->chromaticity.blue_primary.x=chromaticity[4];
1324 image->chromaticity.blue_primary.y=chromaticity[5];
1327 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1328 if ((compress_tag != COMPRESSION_NONE) &&
1329 (TIFFIsCODECConfigured(compress_tag) == 0))
1332 ThrowReaderException(CoderError,"CompressNotSupported");
1335 switch (compress_tag)
1337 case COMPRESSION_NONE: image->compression=NoCompression; break;
1338 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1339 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1340 case COMPRESSION_JPEG:
1342 image->compression=JPEGCompression;
1343 #if defined(JPEG_SUPPORT)
1346 sampling_factor[MagickPathExtent];
1355 tiff_status=TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1356 &horizontal,&vertical);
1357 if (tiff_status == 1)
1359 (void) FormatLocaleString(sampling_factor,MagickPathExtent,"%dx%d",
1360 horizontal,vertical);
1361 (void) SetImageProperty(image,"jpeg:sampling-factor",
1362 sampling_factor,exception);
1363 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1364 "Sampling Factors: %s",sampling_factor);
1370 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1371 #if defined(COMPRESSION_LZMA)
1372 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1374 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1375 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1376 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1377 default: image->compression=RLECompression; break;
1380 Allocate memory for the image and pixel buffer.
1382 quantum_info=AcquireQuantumInfo(image_info,image);
1383 if (quantum_info == (QuantumInfo *) NULL)
1386 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1388 if (sample_format == SAMPLEFORMAT_UINT)
1389 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1390 if (sample_format == SAMPLEFORMAT_INT)
1391 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1392 if (sample_format == SAMPLEFORMAT_IEEEFP)
1393 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1394 if (status == MagickFalse)
1397 quantum_info=DestroyQuantumInfo(quantum_info);
1398 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1401 switch (photometric)
1403 case PHOTOMETRIC_MINISBLACK:
1405 quantum_info->min_is_white=MagickFalse;
1408 case PHOTOMETRIC_MINISWHITE:
1410 quantum_info->min_is_white=MagickTrue;
1416 tiff_status=TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1418 if (tiff_status == 1)
1420 (void) SetImageProperty(image,"tiff:alpha","unspecified",exception);
1421 if (extra_samples == 0)
1423 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1424 image->alpha_trait=BlendPixelTrait;
1427 for (i=0; i < extra_samples; i++)
1429 image->alpha_trait=BlendPixelTrait;
1430 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
1432 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
1433 (void) SetImageProperty(image,"tiff:alpha","associated",
1437 if (sample_info[i] == EXTRASAMPLE_UNASSALPHA)
1438 (void) SetImageProperty(image,"tiff:alpha","unassociated",
1442 if ((photometric == PHOTOMETRIC_PALETTE) &&
1443 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1448 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
1449 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
1452 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1455 value=(unsigned short) image->scene;
1456 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages) == 1)
1458 if (image->storage_class == PseudoClass)
1472 Initialize colormap.
1474 tiff_status=TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1475 &green_colormap,&blue_colormap);
1476 if (tiff_status == 1)
1478 if ((red_colormap != (uint16 *) NULL) &&
1479 (green_colormap != (uint16 *) NULL) &&
1480 (blue_colormap != (uint16 *) NULL))
1482 range=255; /* might be old style 8-bit colormap */
1483 for (i=0; i < (ssize_t) image->colors; i++)
1484 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1485 (blue_colormap[i] >= 256))
1490 for (i=0; i < (ssize_t) image->colors; i++)
1492 image->colormap[i].red=ClampToQuantum(((double)
1493 QuantumRange*red_colormap[i])/range);
1494 image->colormap[i].green=ClampToQuantum(((double)
1495 QuantumRange*green_colormap[i])/range);
1496 image->colormap[i].blue=ClampToQuantum(((double)
1497 QuantumRange*blue_colormap[i])/range);
1501 if (image->alpha_trait == UndefinedPixelTrait)
1502 image->depth=GetImageDepth(image,exception);
1504 if (image_info->ping != MagickFalse)
1506 if (image_info->number_scenes != 0)
1507 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1509 quantum_info=DestroyQuantumInfo(quantum_info);
1512 goto next_tiff_frame;
1514 method=ReadGenericMethod;
1515 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) == 1)
1518 value[MagickPathExtent];
1520 method=ReadStripMethod;
1521 (void) FormatLocaleString(value,MagickPathExtent,"%u",
1522 (unsigned int) rows_per_strip);
1523 (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception);
1525 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1526 method=ReadRGBAMethod;
1527 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1528 method=ReadCMYKAMethod;
1529 if ((photometric != PHOTOMETRIC_RGB) &&
1530 (photometric != PHOTOMETRIC_CIELAB) &&
1531 (photometric != PHOTOMETRIC_SEPARATED))
1532 method=ReadGenericMethod;
1533 if (image->storage_class == PseudoClass)
1534 method=ReadSingleSampleMethod;
1535 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1536 (photometric == PHOTOMETRIC_MINISWHITE))
1537 method=ReadSingleSampleMethod;
1538 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1539 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
1540 method=ReadGenericMethod;
1541 if (image->compression == JPEGCompression)
1542 method=GetJPEGMethod(image,tiff,photometric,bits_per_sample,
1544 if (compress_tag == COMPRESSION_JBIG)
1545 method=ReadStripMethod;
1546 if (TIFFIsTiled(tiff) != MagickFalse)
1547 method=ReadTileMethod;
1548 quantum_info->endian=LSBEndian;
1549 quantum_type=RGBQuantum;
1550 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1553 case ReadSingleSampleMethod:
1556 Convert TIFF image to PseudoClass MIFF image.
1558 quantum_type=IndexQuantum;
1559 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1560 if (image->alpha_trait != UndefinedPixelTrait)
1562 if (image->storage_class != PseudoClass)
1564 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1566 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1570 quantum_type=IndexAlphaQuantum;
1571 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1575 if (image->storage_class != PseudoClass)
1577 quantum_type=GrayQuantum;
1578 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1580 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1581 if (status == MagickFalse)
1584 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1586 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1587 for (y=0; y < (ssize_t) image->rows; y++)
1595 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1598 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1599 if (q == (Quantum *) NULL)
1601 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1602 quantum_type,pixels,exception);
1603 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1605 if (image->previous == (Image *) NULL)
1607 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1609 if (status == MagickFalse)
1615 case ReadRGBAMethod:
1618 Convert TIFF image to DirectClass MIFF image.
1620 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1621 quantum_type=RGBQuantum;
1622 if (image->alpha_trait != UndefinedPixelTrait)
1624 quantum_type=RGBAQuantum;
1625 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1627 if (image->colorspace == CMYKColorspace)
1629 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1630 quantum_type=CMYKQuantum;
1631 if (image->alpha_trait != UndefinedPixelTrait)
1633 quantum_type=CMYKAQuantum;
1634 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1637 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1638 if (status == MagickFalse)
1641 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1643 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1644 for (y=0; y < (ssize_t) image->rows; y++)
1652 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1655 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1656 if (q == (Quantum *) NULL)
1658 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1659 quantum_type,pixels,exception);
1660 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1662 if (image->previous == (Image *) NULL)
1664 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1666 if (status == MagickFalse)
1672 case ReadCMYKAMethod:
1675 Convert TIFF image to DirectClass MIFF image.
1677 for (i=0; i < (ssize_t) samples_per_pixel; i++)
1679 for (y=0; y < (ssize_t) image->rows; y++)
1687 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1691 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1692 if (q == (Quantum *) NULL)
1694 if (image->colorspace != CMYKColorspace)
1697 case 0: quantum_type=RedQuantum; break;
1698 case 1: quantum_type=GreenQuantum; break;
1699 case 2: quantum_type=BlueQuantum; break;
1700 case 3: quantum_type=AlphaQuantum; break;
1701 default: quantum_type=UndefinedQuantum; break;
1706 case 0: quantum_type=CyanQuantum; break;
1707 case 1: quantum_type=MagentaQuantum; break;
1708 case 2: quantum_type=YellowQuantum; break;
1709 case 3: quantum_type=BlackQuantum; break;
1710 case 4: quantum_type=AlphaQuantum; break;
1711 default: quantum_type=UndefinedQuantum; break;
1713 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1714 quantum_type,pixels,exception);
1715 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1718 if (image->previous == (Image *) NULL)
1720 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1722 if (status == MagickFalse)
1728 case ReadYCCKMethod:
1730 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1731 for (y=0; y < (ssize_t) image->rows; y++)
1745 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1748 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1749 if (q == (Quantum *) NULL)
1752 for (x=0; x < (ssize_t) image->columns; x++)
1754 SetPixelCyan(image,ScaleCharToQuantum(ClampYCC((double) *p+
1755 (1.402*(double) *(p+2))-179.456)),q);
1756 SetPixelMagenta(image,ScaleCharToQuantum(ClampYCC((double) *p-
1757 (0.34414*(double) *(p+1))-(0.71414*(double ) *(p+2))+
1759 SetPixelYellow(image,ScaleCharToQuantum(ClampYCC((double) *p+
1760 (1.772*(double) *(p+1))-226.816)),q);
1761 SetPixelBlack(image,ScaleCharToQuantum((unsigned char) *(p+3)),q);
1762 q+=GetPixelChannels(image);
1765 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1767 if (image->previous == (Image *) NULL)
1769 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1771 if (status == MagickFalse)
1777 case ReadStripMethod:
1783 Convert stripped TIFF image to DirectClass MIFF image.
1787 for (y=0; y < (ssize_t) image->rows; y++)
1795 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1796 if (q == (Quantum *) NULL)
1800 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1802 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1806 p=((uint32 *) pixels)+image->columns*i;
1807 for (x=0; x < (ssize_t) image->columns; x++)
1809 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1811 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1813 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1815 if (image->alpha_trait != UndefinedPixelTrait)
1816 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1819 q+=GetPixelChannels(image);
1821 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1823 if (image->previous == (Image *) NULL)
1825 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1827 if (status == MagickFalse)
1833 case ReadTileMethod:
1847 Convert tiled TIFF image to DirectClass MIFF image.
1849 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) != 1) ||
1850 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) != 1))
1853 ThrowReaderException(CoderError,"ImageIsNotTiled");
1855 (void) SetImageStorageClass(image,DirectClass,exception);
1856 number_pixels=columns*rows;
1857 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
1858 sizeof(*tile_pixels));
1859 if (tile_pixels == (uint32 *) NULL)
1862 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1864 for (y=0; y < (ssize_t) image->rows; y+=rows)
1877 rows_remaining=image->rows-y;
1878 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
1879 rows_remaining=rows;
1880 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1882 if (tile == (Quantum *) NULL)
1884 for (x=0; x < (ssize_t) image->columns; x+=columns)
1890 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1892 columns_remaining=image->columns-x;
1893 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
1894 columns_remaining=columns;
1895 p=tile_pixels+(rows-rows_remaining)*columns;
1896 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1898 for (row=rows_remaining; row > 0; row--)
1900 if (image->alpha_trait != UndefinedPixelTrait)
1901 for (column=columns_remaining; column > 0; column--)
1903 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1905 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1907 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1909 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1912 q+=GetPixelChannels(image);
1915 for (column=columns_remaining; column > 0; column--)
1917 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1919 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1921 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1924 q+=GetPixelChannels(image);
1926 p+=columns-columns_remaining;
1927 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
1930 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1932 if (image->previous == (Image *) NULL)
1934 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1936 if (status == MagickFalse)
1940 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1943 case ReadGenericMethod:
1956 Convert TIFF image to DirectClass MIFF image.
1958 number_pixels=(MagickSizeType) image->columns*image->rows;
1959 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1960 (number_pixels*sizeof(uint32))))
1963 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1965 pixel_info=AcquireVirtualMemory(image->columns,image->rows*
1967 if (pixel_info == (MemoryInfo *) NULL)
1970 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1972 pixels=(uint32 *) GetVirtualMemoryBlob(pixel_info);
1973 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1974 (uint32) image->rows,(uint32 *) pixels,0);
1976 Convert image to DirectClass pixel packets.
1978 p=pixels+number_pixels-1;
1979 for (y=0; y < (ssize_t) image->rows; y++)
1987 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1988 if (q == (Quantum *) NULL)
1990 q+=GetPixelChannels(image)*(image->columns-1);
1991 for (x=0; x < (ssize_t) image->columns; x++)
1993 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1995 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1997 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1999 if (image->alpha_trait != UndefinedPixelTrait)
2000 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
2003 q-=GetPixelChannels(image);
2005 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2007 if (image->previous == (Image *) NULL)
2009 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
2011 if (status == MagickFalse)
2015 pixel_info=RelinquishVirtualMemory(pixel_info);
2019 SetQuantumImageType(image,quantum_type);
2021 quantum_info=DestroyQuantumInfo(quantum_info);
2022 if (photometric == PHOTOMETRIC_CIELAB)
2023 DecodeLabImage(image,exception);
2024 if ((photometric == PHOTOMETRIC_LOGL) ||
2025 (photometric == PHOTOMETRIC_MINISBLACK) ||
2026 (photometric == PHOTOMETRIC_MINISWHITE))
2028 image->type=GrayscaleType;
2029 if (bits_per_sample == 1)
2030 image->type=BilevelType;
2033 Proceed to next image.
2035 if (image_info->number_scenes != 0)
2036 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
2038 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
2039 if (status != MagickFalse)
2042 Allocate next image structure.
2044 AcquireNextImage(image_info,image,exception);
2045 if (GetNextImageInList(image) == (Image *) NULL)
2047 image=DestroyImageList(image);
2048 return((Image *) NULL);
2050 image=SyncNextImageInList(image);
2051 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
2053 if (status == MagickFalse)
2056 } while (status != MagickFalse);
2057 (void) TIFFSetWarningHandler(warning_handler);
2058 (void) TIFFSetErrorHandler(error_handler);
2060 TIFFReadPhotoshopLayers(image,image_info,exception);
2061 return(GetFirstImageInList(image));
2066 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2070 % R e g i s t e r T I F F I m a g e %
2074 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2076 % RegisterTIFFImage() adds properties for the TIFF image format to
2077 % the list of supported formats. The properties include the image format
2078 % tag, a method to read and/or write the format, whether the format
2079 % supports the saving of more than one frame to the same file or blob,
2080 % whether the format supports native in-memory I/O, and a brief
2081 % description of the format.
2083 % The format of the RegisterTIFFImage method is:
2085 % size_t RegisterTIFFImage(void)
2089 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2090 static TIFFExtendProc
2091 tag_extender = (TIFFExtendProc) NULL;
2093 static void TIFFIgnoreTags(TIFF *tiff)
2114 if (TIFFGetReadProc(tiff) != TIFFReadBlob)
2116 image=(Image *)TIFFClientdata(tiff);
2117 tags=GetImageArtifact(image,"tiff:ignore-tags");
2118 if (tags == (const char *) NULL)
2124 while ((isspace((int) ((unsigned char) *p)) != 0))
2127 (void) strtol(p,&q,10);
2134 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2141 ignore=(TIFFFieldInfo *) AcquireQuantumMemory(count,sizeof(*ignore));
2142 /* This also sets field_bit to 0 (FIELD_IGNORE) */
2143 ResetMagickMemory(ignore,0,count*sizeof(*ignore));
2146 while ((isspace((int) ((unsigned char) *p)) != 0))
2149 ignore[i].field_tag=(ttag_t) strtol(p,&q,10);
2154 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2157 (void) TIFFMergeFieldInfo(tiff,ignore,(uint32) count);
2158 ignore=(TIFFFieldInfo *) RelinquishMagickMemory(ignore);
2161 static void TIFFTagExtender(TIFF *tiff)
2163 static const TIFFFieldInfo
2166 { 37724, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
2167 (char *) "PhotoshopLayerData" },
2168 { 34118, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
2169 (char *) "Microscope" }
2172 TIFFMergeFieldInfo(tiff,TIFFExtensions,sizeof(TIFFExtensions)/
2173 sizeof(*TIFFExtensions));
2174 if (tag_extender != (TIFFExtendProc) NULL)
2175 (*tag_extender)(tiff);
2176 TIFFIgnoreTags(tiff);
2180 ModuleExport size_t RegisterTIFFImage(void)
2182 #define TIFFDescription "Tagged Image File Format"
2185 version[MagickPathExtent];
2190 if (tiff_semaphore == (SemaphoreInfo *) NULL)
2191 ActivateSemaphoreInfo(&tiff_semaphore);
2192 LockSemaphoreInfo(tiff_semaphore);
2193 if (instantiate_key == MagickFalse)
2195 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
2196 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2197 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2198 if (tag_extender == (TIFFExtendProc) NULL)
2199 tag_extender=TIFFSetTagExtender(TIFFTagExtender);
2201 instantiate_key=MagickTrue;
2203 UnlockSemaphoreInfo(tiff_semaphore);
2205 #if defined(TIFF_VERSION)
2206 (void) FormatLocaleString(version,MagickPathExtent,"%d",TIFF_VERSION);
2208 #if defined(MAGICKCORE_TIFF_DELEGATE)
2217 for (i=0; (i < (MagickPathExtent-1)) && (*p != 0) && (*p != '\n'); i++)
2223 entry=AcquireMagickInfo("TIFF","GROUP4","Raw CCITT Group4");
2224 #if defined(MAGICKCORE_TIFF_DELEGATE)
2225 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
2226 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
2228 entry->flags|=CoderRawSupportFlag;
2229 entry->flags|=CoderEndianSupportFlag;
2230 entry->flags|=CoderSeekableStreamFlag;
2231 entry->flags^=CoderAdjoinFlag;
2232 entry->flags^=CoderUseExtensionFlag;
2233 entry->format_type=ImplicitFormatType;
2234 entry->mime_type=ConstantString("image/tiff");
2235 (void) RegisterMagickInfo(entry);
2236 entry=AcquireMagickInfo("TIFF","PTIF","Pyramid encoded TIFF");
2237 #if defined(MAGICKCORE_TIFF_DELEGATE)
2238 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2239 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
2241 entry->flags|=CoderEndianSupportFlag;
2242 entry->flags|=CoderSeekableStreamFlag;
2243 entry->flags^=CoderUseExtensionFlag;
2244 entry->mime_type=ConstantString("image/tiff");
2245 (void) RegisterMagickInfo(entry);
2246 entry=AcquireMagickInfo("TIFF","TIF",TIFFDescription);
2247 #if defined(MAGICKCORE_TIFF_DELEGATE)
2248 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2249 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2251 entry->flags|=CoderEndianSupportFlag;
2252 entry->flags|=CoderSeekableStreamFlag;
2253 entry->flags|=CoderStealthFlag;
2254 entry->flags^=CoderUseExtensionFlag;
2255 if (*version != '\0')
2256 entry->version=ConstantString(version);
2257 entry->mime_type=ConstantString("image/tiff");
2258 (void) RegisterMagickInfo(entry);
2259 entry=AcquireMagickInfo("TIFF","TIFF",TIFFDescription);
2260 #if defined(MAGICKCORE_TIFF_DELEGATE)
2261 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2262 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2264 entry->magick=(IsImageFormatHandler *) IsTIFF;
2265 entry->flags|=CoderEndianSupportFlag;
2266 entry->flags|=CoderSeekableStreamFlag;
2267 entry->flags^=CoderUseExtensionFlag;
2268 if (*version != '\0')
2269 entry->version=ConstantString(version);
2270 entry->mime_type=ConstantString("image/tiff");
2271 (void) RegisterMagickInfo(entry);
2272 entry=AcquireMagickInfo("TIFF","TIFF64","Tagged Image File Format (64-bit)");
2273 #if defined(TIFF_VERSION_BIG)
2274 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2275 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2277 entry->flags|=CoderEndianSupportFlag;
2278 entry->flags|=CoderSeekableStreamFlag;
2279 entry->flags^=CoderAdjoinFlag;
2280 entry->flags^=CoderUseExtensionFlag;
2281 if (*version != '\0')
2282 entry->version=ConstantString(version);
2283 entry->mime_type=ConstantString("image/tiff");
2284 (void) RegisterMagickInfo(entry);
2285 return(MagickImageCoderSignature);
2289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2293 % U n r e g i s t e r T I F F I m a g e %
2297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2299 % UnregisterTIFFImage() removes format registrations made by the TIFF module
2300 % from the list of supported formats.
2302 % The format of the UnregisterTIFFImage method is:
2304 % UnregisterTIFFImage(void)
2307 ModuleExport void UnregisterTIFFImage(void)
2309 (void) UnregisterMagickInfo("TIFF64");
2310 (void) UnregisterMagickInfo("TIFF");
2311 (void) UnregisterMagickInfo("TIF");
2312 (void) UnregisterMagickInfo("PTIF");
2313 if (tiff_semaphore == (SemaphoreInfo *) NULL)
2314 ActivateSemaphoreInfo(&tiff_semaphore);
2315 LockSemaphoreInfo(tiff_semaphore);
2316 if (instantiate_key != MagickFalse)
2318 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2319 if (tag_extender == (TIFFExtendProc) NULL)
2320 (void) TIFFSetTagExtender(tag_extender);
2322 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
2323 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2324 instantiate_key=MagickFalse;
2326 UnlockSemaphoreInfo(tiff_semaphore);
2327 RelinquishSemaphoreInfo(&tiff_semaphore);
2330 #if defined(MAGICKCORE_TIFF_DELEGATE)
2332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2336 % W r i t e G R O U P 4 I m a g e %
2340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2342 % WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
2344 % The format of the WriteGROUP4Image method is:
2346 % MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2347 % Image *image,ExceptionInfo *)
2349 % A description of each parameter follows:
2351 % o image_info: the image info.
2353 % o image: The image.
2355 % o exception: return any errors or warnings in this structure.
2358 static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2359 Image *image,ExceptionInfo *exception)
2362 filename[MagickPathExtent];
2396 Write image as CCITT Group4 TIFF image to a temporary file.
2398 assert(image_info != (const ImageInfo *) NULL);
2399 assert(image_info->signature == MagickCoreSignature);
2400 assert(image != (Image *) NULL);
2401 assert(image->signature == MagickCoreSignature);
2402 if (image->debug != MagickFalse)
2403 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2404 assert(exception != (ExceptionInfo *) NULL);
2405 assert(exception->signature == MagickCoreSignature);
2406 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2407 if (status == MagickFalse)
2409 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
2410 if (huffman_image == (Image *) NULL)
2412 (void) CloseBlob(image);
2413 return(MagickFalse);
2415 huffman_image->endian=MSBEndian;
2417 unique_file=AcquireUniqueFileResource(filename);
2418 if (unique_file != -1)
2419 file=fdopen(unique_file,"wb");
2420 if ((unique_file == -1) || (file == (FILE *) NULL))
2422 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2424 return(MagickFalse);
2426 (void) FormatLocaleString(huffman_image->filename,MagickPathExtent,"tiff:%s",
2428 (void) SetImageType(huffman_image,BilevelType,exception);
2429 write_info=CloneImageInfo((ImageInfo *) NULL);
2430 SetImageInfoFile(write_info,file);
2431 write_info->compression=Group4Compression;
2432 write_info->type=BilevelType;
2433 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
2434 status=WriteTIFFImage(write_info,huffman_image,exception);
2435 (void) fflush(file);
2436 write_info=DestroyImageInfo(write_info);
2437 if (status == MagickFalse)
2439 huffman_image=DestroyImage(huffman_image);
2440 (void) fclose(file);
2441 (void) RelinquishUniqueFileResource(filename);
2442 return(MagickFalse);
2444 tiff=TIFFOpen(filename,"rb");
2445 if (tiff == (TIFF *) NULL)
2447 huffman_image=DestroyImage(huffman_image);
2448 (void) fclose(file);
2449 (void) RelinquishUniqueFileResource(filename);
2450 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
2451 image_info->filename);
2452 return(MagickFalse);
2455 Allocate raw strip buffer.
2457 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2460 huffman_image=DestroyImage(huffman_image);
2461 (void) fclose(file);
2462 (void) RelinquishUniqueFileResource(filename);
2463 return(MagickFalse);
2465 strip_size=byte_count[0];
2466 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2467 if (byte_count[i] > strip_size)
2468 strip_size=byte_count[i];
2469 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2471 if (buffer == (unsigned char *) NULL)
2474 huffman_image=DestroyImage(huffman_image);
2475 (void) fclose(file);
2476 (void) RelinquishUniqueFileResource(filename);
2477 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2478 image_info->filename);
2481 Compress runlength encoded to 2D Huffman pixels.
2483 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2485 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
2486 if (WriteBlob(image,(size_t) count,buffer) != count)
2489 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2491 huffman_image=DestroyImage(huffman_image);
2492 (void) fclose(file);
2493 (void) RelinquishUniqueFileResource(filename);
2494 (void) CloseBlob(image);
2499 #if defined(MAGICKCORE_TIFF_DELEGATE)
2501 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2505 % W r i t e P T I F I m a g e %
2509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2511 % WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2514 % The format of the WritePTIFImage method is:
2516 % MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2517 % Image *image,ExceptionInfo *exception)
2519 % A description of each parameter follows:
2521 % o image_info: the image info.
2523 % o image: The image.
2525 % o exception: return any errors or warnings in this structure.
2528 static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2529 Image *image,ExceptionInfo *exception)
2550 Create pyramid-encoded TIFF image.
2552 images=NewImageList();
2553 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2558 clone_image=CloneImage(next,0,0,MagickFalse,exception);
2559 clone_image->previous=NewImageList();
2560 clone_image->next=NewImageList();
2561 AppendImageToList(&images,clone_image);
2562 columns=next->columns;
2564 resolution=next->resolution;
2565 while ((columns > 64) && (rows > 64))
2571 pyramid_image=ResizeImage(next,columns,rows,image->filter,exception);
2572 if (pyramid_image == (Image *) NULL)
2574 pyramid_image->resolution=resolution;
2575 AppendImageToList(&images,pyramid_image);
2578 images=GetFirstImageInList(images);
2580 Write pyramid-encoded TIFF image.
2582 write_info=CloneImageInfo(image_info);
2583 write_info->adjoin=MagickTrue;
2584 (void) CopyMagickString(write_info->magick,"TIFF",MagickPathExtent);
2585 (void) CopyMagickString(images->magick,"TIFF",MagickPathExtent);
2586 status=WriteTIFFImage(write_info,images,exception);
2587 images=DestroyImageList(images);
2588 write_info=DestroyImageInfo(write_info);
2593 #if defined(MAGICKCORE_TIFF_DELEGATE)
2595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2598 % W r i t e T I F F I m a g e %
2602 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2604 % WriteTIFFImage() writes an image in the Tagged image file format.
2606 % The format of the WriteTIFFImage method is:
2608 % MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2609 % Image *image,ExceptionInfo *exception)
2611 % A description of each parameter follows:
2613 % o image_info: the image info.
2615 % o image: The image.
2617 % o exception: return any errors or warnings in this structure.
2621 typedef struct _TIFFInfo
2632 static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2634 assert(tiff_info != (TIFFInfo *) NULL);
2635 if (tiff_info->scanlines != (unsigned char *) NULL)
2636 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2637 tiff_info->scanlines);
2638 if (tiff_info->pixels != (unsigned char *) NULL)
2639 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2643 static MagickBooleanType EncodeLabImage(Image *image,ExceptionInfo *exception)
2655 image_view=AcquireAuthenticCacheView(image,exception);
2656 for (y=0; y < (ssize_t) image->rows; y++)
2664 if (status == MagickFalse)
2666 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2667 if (q == (Quantum *) NULL)
2672 for (x=0; x < (ssize_t) image->columns; x++)
2678 a=QuantumScale*GetPixela(image,q)-0.5;
2681 b=QuantumScale*GetPixelb(image,q)-0.5;
2684 SetPixela(image,QuantumRange*a,q);
2685 SetPixelb(image,QuantumRange*b,q);
2686 q+=GetPixelChannels(image);
2688 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2691 image_view=DestroyCacheView(image_view);
2695 static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,
2696 TIFF *tiff,TIFFInfo *tiff_info)
2708 assert(tiff_info != (TIFFInfo *) NULL);
2709 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2710 option=GetImageOption(image_info,"tiff:tile-geometry");
2711 if (option == (const char *) NULL)
2713 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2714 if ((flags & HeightValue) == 0)
2715 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2716 tile_columns=(uint32) tiff_info->tile_geometry.width;
2717 tile_rows=(uint32) tiff_info->tile_geometry.height;
2718 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2719 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2720 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2721 tiff_info->tile_geometry.width=tile_columns;
2722 tiff_info->tile_geometry.height=tile_rows;
2723 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2724 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
2725 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2726 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2727 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2728 (tiff_info->pixels == (unsigned char *) NULL))
2730 DestroyTIFFInfo(tiff_info);
2731 return(MagickFalse);
2736 static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
2737 tsample_t sample,Image *image)
2745 register unsigned char
2759 if (TIFFIsTiled(tiff) == 0)
2760 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2762 Fill scanlines to tile height.
2764 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2765 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2766 (size_t) TIFFScanlineSize(tiff));
2767 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2768 (tiff_info->tile_geometry.height-1)) &&
2769 (row != (ssize_t) (image->rows-1)))
2772 Write tile to TIFF image.
2775 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (
2776 tiff_info->tile_geometry.height*tiff_info->tile_geometry.width);
2777 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2778 tiff_info->tile_geometry.width;
2779 for (i=0; i < (ssize_t) number_tiles; i++)
2781 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
2782 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2783 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2784 for (k=0; k < (ssize_t) tile_width; k++)
2786 if (bytes_per_pixel == 0)
2788 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2789 tiff_info->tile_geometry.width+k)/8);
2790 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2794 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2795 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2796 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2797 for (l=0; l < bytes_per_pixel; l++)
2800 if ((i*tiff_info->tile_geometry.width) != image->columns)
2801 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2802 tiff_info->tile_geometry.width),(uint32) ((row/
2803 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2811 static void TIFFSetProfiles(TIFF *tiff,Image *image)
2819 if (image->profiles == (void *) NULL)
2821 ResetImageProfileIterator(image);
2822 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2824 profile=GetImageProfile(image,name);
2825 if (GetStringInfoLength(profile) == 0)
2827 name=GetNextImageProfile(image);
2830 #if defined(TIFFTAG_XMLPACKET)
2831 if (LocaleCompare(name,"xmp") == 0)
2832 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2833 profile),GetStringInfoDatum(profile));
2835 #if defined(TIFFTAG_ICCPROFILE)
2836 if (LocaleCompare(name,"icc") == 0)
2837 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2838 profile),GetStringInfoDatum(profile));
2840 if (LocaleCompare(name,"iptc") == 0)
2848 iptc_profile=CloneStringInfo(profile);
2849 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2851 SetStringInfoLength(iptc_profile,length);
2852 if (TIFFIsByteSwapped(tiff))
2853 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2854 (unsigned long) (length/4));
2855 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2856 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2857 iptc_profile=DestroyStringInfo(iptc_profile);
2859 #if defined(TIFFTAG_PHOTOSHOP)
2860 if (LocaleCompare(name,"8bim") == 0)
2861 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2862 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2864 if (LocaleCompare(name,"tiff:37724") == 0)
2865 (void) TIFFSetField(tiff,37724,(uint32) GetStringInfoLength(profile),
2866 GetStringInfoDatum(profile));
2867 if (LocaleCompare(name,"tiff:34118") == 0)
2868 (void) TIFFSetField(tiff,34118,(uint32) GetStringInfoLength(profile),
2869 GetStringInfoDatum(profile));
2870 name=GetNextImageProfile(image);
2874 static void TIFFSetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
2879 value=GetImageArtifact(image,"tiff:document");
2880 if (value != (const char *) NULL)
2881 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,value);
2882 value=GetImageArtifact(image,"tiff:hostcomputer");
2883 if (value != (const char *) NULL)
2884 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2885 value=GetImageArtifact(image,"tiff:artist");
2886 if (value != (const char *) NULL)
2887 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2888 value=GetImageArtifact(image,"tiff:timestamp");
2889 if (value != (const char *) NULL)
2890 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2891 value=GetImageArtifact(image,"tiff:make");
2892 if (value != (const char *) NULL)
2893 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2894 value=GetImageArtifact(image,"tiff:model");
2895 if (value != (const char *) NULL)
2896 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2897 value=GetImageArtifact(image,"tiff:software");
2898 if (value != (const char *) NULL)
2899 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
2900 value=GetImageArtifact(image,"tiff:copyright");
2901 if (value != (const char *) NULL)
2902 (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value);
2903 value=GetImageArtifact(image,"kodak-33423");
2904 if (value != (const char *) NULL)
2905 (void) TIFFSetField(tiff,33423,value);
2906 value=GetImageArtifact(image,"kodak-36867");
2907 if (value != (const char *) NULL)
2908 (void) TIFFSetField(tiff,36867,value);
2909 value=GetImageProperty(image,"label",exception);
2910 if (value != (const char *) NULL)
2911 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2912 value=GetImageProperty(image,"comment",exception);
2913 if (value != (const char *) NULL)
2914 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2917 static void TIFFSetEXIFProperties(TIFF *tiff,Image *image,
2918 ExceptionInfo *exception)
2920 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2931 Write EXIF properties.
2934 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2935 for (i=0; exif_info[i].tag != 0; i++)
2937 value=GetImageProperty(image,exif_info[i].property,exception);
2938 if (value == (const char *) NULL)
2940 switch (exif_info[i].type)
2944 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2952 field=(uint16) StringToLong(value);
2953 (void) TIFFSetField(tiff,exif_info[i].tag,field);
2961 field=(uint16) StringToLong(value);
2962 (void) TIFFSetField(tiff,exif_info[i].tag,field);
2966 case TIFF_SRATIONAL:
2971 field=StringToDouble(value,(char **) NULL);
2972 (void) TIFFSetField(tiff,exif_info[i].tag,field);
2979 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2986 static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2987 Image *image,ExceptionInfo *exception)
2989 #if !defined(TIFFDefaultStripSize)
2990 #define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
3050 assert(image_info != (const ImageInfo *) NULL);
3051 assert(image_info->signature == MagickCoreSignature);
3052 assert(image != (Image *) NULL);
3053 assert(image->signature == MagickCoreSignature);
3054 if (image->debug != MagickFalse)
3055 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3056 assert(exception != (ExceptionInfo *) NULL);
3057 assert(exception->signature == MagickCoreSignature);
3058 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
3059 if (status == MagickFalse)
3061 (void) MagickSetThreadValue(tiff_exception,exception);
3062 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
3063 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
3064 endian_type=UndefinedEndian;
3065 option=GetImageOption(image_info,"tiff:endian");
3066 if (option != (const char *) NULL)
3068 if (LocaleNCompare(option,"msb",3) == 0)
3069 endian_type=MSBEndian;
3070 if (LocaleNCompare(option,"lsb",3) == 0)
3071 endian_type=LSBEndian;;
3073 switch (endian_type)
3075 case LSBEndian: mode="wl"; break;
3076 case MSBEndian: mode="wb"; break;
3077 default: mode="w"; break;
3079 #if defined(TIFF_VERSION_BIG)
3080 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
3081 switch (endian_type)
3083 case LSBEndian: mode="wl8"; break;
3084 case MSBEndian: mode="wb8"; break;
3085 default: mode="w8"; break;
3088 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
3089 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
3091 if (tiff == (TIFF *) NULL)
3093 (void) TIFFSetWarningHandler(warning_handler);
3094 (void) TIFFSetErrorHandler(error_handler);
3095 return(MagickFalse);
3098 debug=IsEventLogging();
3103 Initialize TIFF fields.
3105 if ((image_info->type != UndefinedType) &&
3106 (image_info->type != OptimizeType))
3107 (void) SetImageType(image,image_info->type,exception);
3108 compression=UndefinedCompression;
3109 if (image->compression != JPEGCompression)
3110 compression=image->compression;
3111 if (image_info->compression != UndefinedCompression)
3112 compression=image_info->compression;
3113 switch (compression)
3115 case FaxCompression:
3116 case Group4Compression:
3118 (void) SetImageType(image,BilevelType,exception);
3121 case JPEGCompression:
3123 (void) SetImageStorageClass(image,DirectClass,exception);
3124 (void) SetImageDepth(image,8,exception);
3130 quantum_info=AcquireQuantumInfo(image_info,image);
3131 if (quantum_info == (QuantumInfo *) NULL)
3132 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3133 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
3134 (quantum_info->format == UndefinedQuantumFormat) &&
3135 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
3137 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
3138 if (status == MagickFalse)
3139 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3141 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
3142 (GetPreviousImageInList(image) != (Image *) NULL))
3143 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
3144 if ((image->columns != (uint32) image->columns) ||
3145 (image->rows != (uint32) image->rows))
3146 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
3147 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
3148 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
3149 switch (compression)
3151 case FaxCompression:
3153 compress_tag=COMPRESSION_CCITTFAX3;
3154 SetQuantumMinIsWhite(quantum_info,MagickTrue);
3157 case Group4Compression:
3159 compress_tag=COMPRESSION_CCITTFAX4;
3160 SetQuantumMinIsWhite(quantum_info,MagickTrue);
3163 #if defined(COMPRESSION_JBIG)
3164 case JBIG1Compression:
3166 compress_tag=COMPRESSION_JBIG;
3170 case JPEGCompression:
3172 compress_tag=COMPRESSION_JPEG;
3175 #if defined(COMPRESSION_LZMA)
3176 case LZMACompression:
3178 compress_tag=COMPRESSION_LZMA;
3182 case LZWCompression:
3184 compress_tag=COMPRESSION_LZW;
3187 case RLECompression:
3189 compress_tag=COMPRESSION_PACKBITS;
3192 case ZipCompression:
3194 compress_tag=COMPRESSION_ADOBE_DEFLATE;
3200 compress_tag=COMPRESSION_NONE;
3204 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
3205 if ((compress_tag != COMPRESSION_NONE) &&
3206 (TIFFIsCODECConfigured(compress_tag) == 0))
3208 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3209 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
3210 MagickCompressOptions,(ssize_t) compression));
3211 compress_tag=COMPRESSION_NONE;
3212 compression=NoCompression;
3215 switch (compress_tag)
3217 #if defined(CCITT_SUPPORT)
3218 case COMPRESSION_CCITTFAX3:
3219 case COMPRESSION_CCITTFAX4:
3221 #if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
3222 case COMPRESSION_JPEG:
3224 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3225 case COMPRESSION_LZMA:
3227 #if defined(LZW_SUPPORT)
3228 case COMPRESSION_LZW:
3230 #if defined(PACKBITS_SUPPORT)
3231 case COMPRESSION_PACKBITS:
3233 #if defined(ZIP_SUPPORT)
3234 case COMPRESSION_ADOBE_DEFLATE:
3236 case COMPRESSION_NONE:
3240 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3241 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
3242 MagickCompressOptions,(ssize_t) compression));
3243 compress_tag=COMPRESSION_NONE;
3244 compression=NoCompression;
3249 if (image->colorspace == CMYKColorspace)
3251 photometric=PHOTOMETRIC_SEPARATED;
3252 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
3253 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
3258 Full color TIFF raster.
3260 if (image->colorspace == LabColorspace)
3262 photometric=PHOTOMETRIC_CIELAB;
3263 EncodeLabImage(image,exception);
3266 if (image->colorspace == YCbCrColorspace)
3268 photometric=PHOTOMETRIC_YCBCR;
3269 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
3270 (void) SetImageStorageClass(image,DirectClass,exception);
3271 (void) SetImageDepth(image,8,exception);
3274 photometric=PHOTOMETRIC_RGB;
3275 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
3276 if ((image_info->type != TrueColorType) &&
3277 (image_info->type != TrueColorAlphaType))
3279 if ((image_info->type != PaletteType) &&
3280 (SetImageGray(image,exception) != MagickFalse))
3282 photometric=(uint16) (quantum_info->min_is_white !=
3283 MagickFalse ? PHOTOMETRIC_MINISWHITE :
3284 PHOTOMETRIC_MINISBLACK);
3285 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
3286 if ((image_info->depth == 0) &&
3287 (image->alpha_trait == UndefinedPixelTrait) &&
3288 (SetImageMonochrome(image,exception) != MagickFalse))
3290 status=SetQuantumDepth(image,quantum_info,1);
3291 if (status == MagickFalse)
3292 ThrowWriterException(ResourceLimitError,
3293 "MemoryAllocationFailed");
3297 if (image->storage_class == PseudoClass)
3303 Colormapped TIFF raster.
3305 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
3306 photometric=PHOTOMETRIC_PALETTE;
3308 while ((GetQuantumRange(depth)+1) < image->colors)
3310 status=SetQuantumDepth(image,quantum_info,depth);
3311 if (status == MagickFalse)
3312 ThrowWriterException(ResourceLimitError,
3313 "MemoryAllocationFailed");
3317 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
3318 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
3319 (photometric != PHOTOMETRIC_MINISWHITE))
3321 compress_tag=COMPRESSION_NONE;
3322 endian=FILLORDER_MSB2LSB;
3325 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
3326 (photometric != PHOTOMETRIC_MINISWHITE))
3328 compress_tag=COMPRESSION_NONE;
3329 endian=FILLORDER_MSB2LSB;
3331 option=GetImageOption(image_info,"tiff:fill-order");
3332 if (option != (const char *) NULL)
3334 if (LocaleNCompare(option,"msb",3) == 0)
3335 endian=FILLORDER_MSB2LSB;
3336 if (LocaleNCompare(option,"lsb",3) == 0)
3337 endian=FILLORDER_LSB2MSB;
3339 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
3340 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
3341 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
3342 if (image->alpha_trait != UndefinedPixelTrait)
3350 TIFF has a matte channel.
3353 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
3354 option=GetImageOption(image_info,"tiff:alpha");
3355 if (option != (const char *) NULL)
3357 if (LocaleCompare(option,"associated") == 0)
3358 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
3360 if (LocaleCompare(option,"unspecified") == 0)
3361 sample_info[0]=EXTRASAMPLE_UNSPECIFIED;
3363 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
3364 &samples_per_pixel);
3365 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
3366 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
3368 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
3369 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
3371 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
3372 switch (quantum_info->format)
3374 case FloatingPointQuantumFormat:
3376 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
3377 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
3378 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
3381 case SignedQuantumFormat:
3383 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
3386 case UnsignedQuantumFormat:
3388 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
3394 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
3395 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
3396 if (photometric == PHOTOMETRIC_RGB)
3397 if ((image_info->interlace == PlaneInterlace) ||
3398 (image_info->interlace == PartitionInterlace))
3399 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
3400 rows_per_strip=TIFFDefaultStripSize(tiff,0);
3401 option=GetImageOption(image_info,"tiff:rows-per-strip");
3402 if (option != (const char *) NULL)
3403 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
3404 switch (compress_tag)
3406 case COMPRESSION_JPEG:
3408 #if defined(JPEG_SUPPORT)
3418 rows_per_strip+=(16-(rows_per_strip % 16));
3419 if (image_info->quality != UndefinedCompressionQuality)
3420 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
3421 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
3422 if (IssRGBCompatibleColorspace(image->colorspace) != MagickFalse)
3427 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
3428 sampling_factor=(const char *) NULL;
3429 value=GetImageProperty(image,"jpeg:sampling-factor",exception);
3430 if (value != (char *) NULL)
3432 sampling_factor=value;
3433 if (image->debug != MagickFalse)
3434 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3435 " Input sampling-factors=%s",sampling_factor);
3437 if (image_info->sampling_factor != (char *) NULL)
3438 sampling_factor=image_info->sampling_factor;
3439 if (sampling_factor != (const char *) NULL)
3441 flags=ParseGeometry(sampling_factor,&geometry_info);
3442 if ((flags & SigmaValue) == 0)
3443 geometry_info.sigma=geometry_info.rho;
3444 if (image->colorspace == YCbCrColorspace)
3445 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
3446 geometry_info.rho,(uint16) geometry_info.sigma);
3449 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3451 if (bits_per_sample == 12)
3452 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
3456 case COMPRESSION_ADOBE_DEFLATE:
3458 rows_per_strip=(uint32) image->rows;
3459 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3461 if (((photometric == PHOTOMETRIC_RGB) ||
3462 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3463 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3464 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3465 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
3466 image_info->quality == UndefinedCompressionQuality ? 7 :
3467 MagickMin((ssize_t) image_info->quality/10,9)));
3470 case COMPRESSION_CCITTFAX3:
3475 rows_per_strip=(uint32) image->rows;
3476 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
3479 case COMPRESSION_CCITTFAX4:
3481 rows_per_strip=(uint32) image->rows;
3484 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3485 case COMPRESSION_LZMA:
3487 if (((photometric == PHOTOMETRIC_RGB) ||
3488 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3489 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3490 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3491 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
3492 image_info->quality == UndefinedCompressionQuality ? 7 :
3493 MagickMin((ssize_t) image_info->quality/10,9)));
3497 case COMPRESSION_LZW:
3499 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3501 if (((photometric == PHOTOMETRIC_RGB) ||
3502 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3503 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3504 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3510 if (rows_per_strip < 1)
3512 if ((image->rows/rows_per_strip) >= (1UL << 15))
3513 rows_per_strip=(uint32) (image->rows >> 15);
3514 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
3515 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
3521 Set image resolution.
3524 if (image->units == PixelsPerInchResolution)
3526 if (image->units == PixelsPerCentimeterResolution)
3527 units=RESUNIT_CENTIMETER;
3528 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
3529 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x);
3530 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y);
3531 if ((image->page.x < 0) || (image->page.y < 0))
3532 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3533 "TIFF: negative image positions unsupported","%s",image->filename);
3534 if ((image->page.x > 0) && (image->resolution.x > 0.0))
3537 Set horizontal image position.
3539 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
3540 image->resolution.x);
3542 if ((image->page.y > 0) && (image->resolution.y > 0.0))
3545 Set vertical image position.
3547 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
3548 image->resolution.y);
3551 if (image->chromaticity.white_point.x != 0.0)
3557 Set image chromaticity.
3559 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3560 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3561 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3562 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3563 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3564 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3565 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3566 chromaticity[0]=(float) image->chromaticity.white_point.x;
3567 chromaticity[1]=(float) image->chromaticity.white_point.y;
3568 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3570 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3571 (image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3573 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3574 if (image->scene != 0)
3575 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3576 GetImageListLength(image));
3578 if (image->orientation != UndefinedOrientation)
3579 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3580 (void) TIFFSetProfiles(tiff,image);
3586 page=(uint16) scene;
3587 pages=(uint16) GetImageListLength(image);
3588 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3589 (image_info->adjoin != MagickFalse) && (pages > 1))
3590 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3591 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3593 (void) TIFFSetProperties(tiff,image,exception);
3594 DisableMSCWarning(4127)
3597 (void) TIFFSetEXIFProperties(tiff,image,exception);
3599 Write image scanlines.
3601 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3602 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3603 quantum_info->endian=LSBEndian;
3604 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
3605 tiff_info.scanline=(unsigned char *) GetQuantumPixels(quantum_info);
3606 switch (photometric)
3608 case PHOTOMETRIC_CIELAB:
3609 case PHOTOMETRIC_YCBCR:
3610 case PHOTOMETRIC_RGB:
3615 switch (image_info->interlace)
3620 quantum_type=RGBQuantum;
3621 if (image->alpha_trait != UndefinedPixelTrait)
3622 quantum_type=RGBAQuantum;
3623 for (y=0; y < (ssize_t) image->rows; y++)
3625 register const Quantum
3628 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3629 if (p == (const Quantum *) NULL)
3631 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3632 quantum_type,pixels,exception);
3634 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3636 if (image->previous == (Image *) NULL)
3638 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3640 if (status == MagickFalse)
3646 case PlaneInterlace:
3647 case PartitionInterlace:
3650 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3652 for (y=0; y < (ssize_t) image->rows; y++)
3654 register const Quantum
3657 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3658 if (p == (const Quantum *) NULL)
3660 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3661 RedQuantum,pixels,exception);
3662 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3665 if (image->previous == (Image *) NULL)
3667 status=SetImageProgress(image,SaveImageTag,100,400);
3668 if (status == MagickFalse)
3671 for (y=0; y < (ssize_t) image->rows; y++)
3673 register const Quantum
3676 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3677 if (p == (const Quantum *) NULL)
3679 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3680 GreenQuantum,pixels,exception);
3681 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3684 if (image->previous == (Image *) NULL)
3686 status=SetImageProgress(image,SaveImageTag,200,400);
3687 if (status == MagickFalse)
3690 for (y=0; y < (ssize_t) image->rows; y++)
3692 register const Quantum
3695 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3696 if (p == (const Quantum *) NULL)
3698 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3699 BlueQuantum,pixels,exception);
3700 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3703 if (image->previous == (Image *) NULL)
3705 status=SetImageProgress(image,SaveImageTag,300,400);
3706 if (status == MagickFalse)
3709 if (image->alpha_trait != UndefinedPixelTrait)
3710 for (y=0; y < (ssize_t) image->rows; y++)
3712 register const Quantum
3715 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3716 if (p == (const Quantum *) NULL)
3718 length=ExportQuantumPixels(image,(CacheView *) NULL,
3719 quantum_info,AlphaQuantum,pixels,exception);
3720 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3723 if (image->previous == (Image *) NULL)
3725 status=SetImageProgress(image,SaveImageTag,400,400);
3726 if (status == MagickFalse)
3734 case PHOTOMETRIC_SEPARATED:
3739 quantum_type=CMYKQuantum;
3740 if (image->alpha_trait != UndefinedPixelTrait)
3741 quantum_type=CMYKAQuantum;
3742 if (image->colorspace != CMYKColorspace)
3743 (void) TransformImageColorspace(image,CMYKColorspace,exception);
3744 for (y=0; y < (ssize_t) image->rows; y++)
3746 register const Quantum
3749 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3750 if (p == (const Quantum *) NULL)
3752 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3753 quantum_type,pixels,exception);
3754 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3756 if (image->previous == (Image *) NULL)
3758 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3760 if (status == MagickFalse)
3766 case PHOTOMETRIC_PALETTE:
3774 Colormapped TIFF image.
3776 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3777 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3778 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3779 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3780 (blue == (uint16 *) NULL))
3781 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3783 Initialize TIFF colormap.
3785 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3786 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3787 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3788 for (i=0; i < (ssize_t) image->colors; i++)
3790 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3791 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3792 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3794 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3795 red=(uint16 *) RelinquishMagickMemory(red);
3796 green=(uint16 *) RelinquishMagickMemory(green);
3797 blue=(uint16 *) RelinquishMagickMemory(blue);
3802 Convert PseudoClass packets to contiguous grayscale scanlines.
3804 quantum_type=IndexQuantum;
3805 if (image->alpha_trait != UndefinedPixelTrait)
3807 if (photometric != PHOTOMETRIC_PALETTE)
3808 quantum_type=GrayAlphaQuantum;
3810 quantum_type=IndexAlphaQuantum;
3813 if (photometric != PHOTOMETRIC_PALETTE)
3814 quantum_type=GrayQuantum;
3815 for (y=0; y < (ssize_t) image->rows; y++)
3817 register const Quantum
3820 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3821 if (p == (const Quantum *) NULL)
3823 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3824 quantum_type,pixels,exception);
3825 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3827 if (image->previous == (Image *) NULL)
3829 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3831 if (status == MagickFalse)
3838 quantum_info=DestroyQuantumInfo(quantum_info);
3839 if (image->colorspace == LabColorspace)
3840 DecodeLabImage(image,exception);
3841 DestroyTIFFInfo(&tiff_info);
3842 DisableMSCWarning(4127)
3843 if (0 && (image_info->verbose != MagickFalse))
3845 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3846 (void) TIFFWriteDirectory(tiff);
3847 image=SyncNextImageInList(image);
3848 if (image == (Image *) NULL)
3850 status=SetImageProgress(image,SaveImagesTag,scene++,
3851 GetImageListLength(image));
3852 if (status == MagickFalse)
3854 } while (image_info->adjoin != MagickFalse);
3855 (void) TIFFSetWarningHandler(warning_handler);
3856 (void) TIFFSetErrorHandler(error_handler);