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
105 Typedef declarations.
109 ReadSingleSampleMethod,
118 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
119 typedef struct _ExifInfo
130 static const ExifInfo
132 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, 0, "exif:ExposureTime" },
133 { EXIFTAG_FNUMBER, TIFF_RATIONAL, 0, "exif:FNumber" },
134 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, 0, "exif:ExposureProgram" },
135 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, 0, "exif:SpectralSensitivity" },
136 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, 1, "exif:ISOSpeedRatings" },
137 { EXIFTAG_OECF, TIFF_NOTYPE, 0, "exif:OptoelectricConversionFactor" },
138 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, 0, "exif:ExifVersion" },
139 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, 0, "exif:DateTimeOriginal" },
140 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, 0, "exif:DateTimeDigitized" },
141 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, 0, "exif:ComponentsConfiguration" },
142 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, 0, "exif:CompressedBitsPerPixel" },
143 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, 0, "exif:ShutterSpeedValue" },
144 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, 0, "exif:ApertureValue" },
145 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, 0, "exif:BrightnessValue" },
146 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, 0, "exif:ExposureBiasValue" },
147 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, 0, "exif:MaxApertureValue" },
148 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, 0, "exif:SubjectDistance" },
149 { EXIFTAG_METERINGMODE, TIFF_SHORT, 0, "exif:MeteringMode" },
150 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, 0, "exif:LightSource" },
151 { EXIFTAG_FLASH, TIFF_SHORT, 0, "exif:Flash" },
152 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, 0, "exif:FocalLength" },
153 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, 0, "exif:SubjectArea" },
154 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, 0, "exif:MakerNote" },
155 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, 0, "exif:UserComment" },
156 { EXIFTAG_SUBSECTIME, TIFF_ASCII, 0, "exif:SubSecTime" },
157 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, 0, "exif:SubSecTimeOriginal" },
158 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, 0, "exif:SubSecTimeDigitized" },
159 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, 0, "exif:FlashpixVersion" },
160 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, 0, "exif:PixelXDimension" },
161 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, 0, "exif:PixelYDimension" },
162 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, 0, "exif:RelatedSoundFile" },
163 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, 0, "exif:FlashEnergy" },
164 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, 0, "exif:SpatialFrequencyResponse" },
165 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, 0, "exif:FocalPlaneXResolution" },
166 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, 0, "exif:FocalPlaneYResolution" },
167 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, 0, "exif:FocalPlaneResolutionUnit" },
168 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, 0, "exif:SubjectLocation" },
169 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, 0, "exif:ExposureIndex" },
170 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, 0, "exif:SensingMethod" },
171 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, 0, "exif:FileSource" },
172 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, 0, "exif:SceneType" },
173 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, 0, "exif:CFAPattern" },
174 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, 0, "exif:CustomRendered" },
175 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, 0, "exif:ExposureMode" },
176 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, 0, "exif:WhiteBalance" },
177 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, 0, "exif:DigitalZoomRatio" },
178 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, 0, "exif:FocalLengthIn35mmFilm" },
179 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, 0, "exif:SceneCaptureType" },
180 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, 0, "exif:GainControl" },
181 { EXIFTAG_CONTRAST, TIFF_SHORT, 0, "exif:Contrast" },
182 { EXIFTAG_SATURATION, TIFF_SHORT, 0, "exif:Saturation" },
183 { EXIFTAG_SHARPNESS, TIFF_SHORT, 0, "exif:Sharpness" },
184 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, 0, "exif:DeviceSettingDescription" },
185 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, 0, "exif:SubjectDistanceRange" },
186 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, 0, "exif:ImageUniqueID" },
187 { 0, 0, 0, (char *) NULL }
190 #endif /* MAGICKCORE_TIFF_DELEGATE */
195 static MagickThreadKey
199 *tiff_semaphore = (SemaphoreInfo *) NULL;
201 static volatile MagickBooleanType
202 instantiate_key = MagickFalse;
205 Forward declarations.
207 #if defined(MAGICKCORE_TIFF_DELEGATE)
209 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
211 static MagickBooleanType
212 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
213 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
214 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
218 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
228 % IsTIFF() returns MagickTrue if the image format type, identified by the
229 % magick string, is TIFF.
231 % The format of the IsTIFF method is:
233 % MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
235 % A description of each parameter follows:
237 % o magick: compare image format pattern against these bytes.
239 % o length: Specifies the length of the magick string.
242 static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
246 if (memcmp(magick,"\115\115\000\052",4) == 0)
248 if (memcmp(magick,"\111\111\052\000",4) == 0)
250 #if defined(TIFF_VERSION_BIG)
253 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
255 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
261 #if defined(MAGICKCORE_TIFF_DELEGATE)
263 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
267 % R e a d G R O U P 4 I m a g e %
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 % ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
274 % allocates the memory necessary for the new Image structure and returns a
275 % pointer to the new image.
277 % The format of the ReadGROUP4Image method is:
279 % Image *ReadGROUP4Image(const ImageInfo *image_info,
280 % ExceptionInfo *exception)
282 % A description of each parameter follows:
284 % o image_info: the image info.
286 % o exception: return any errors or warnings in this structure.
290 static inline size_t WriteLSBLong(FILE *file,const size_t value)
295 buffer[0]=(unsigned char) value;
296 buffer[1]=(unsigned char) (value >> 8);
297 buffer[2]=(unsigned char) (value >> 16);
298 buffer[3]=(unsigned char) (value >> 24);
299 return(fwrite(buffer,1,4,file));
302 static Image *ReadGROUP4Image(const ImageInfo *image_info,
303 ExceptionInfo *exception)
306 filename[MaxTextExtent];
334 assert(image_info != (const ImageInfo *) NULL);
335 assert(image_info->signature == MagickSignature);
336 if (image_info->debug != MagickFalse)
337 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
338 image_info->filename);
339 assert(exception != (ExceptionInfo *) NULL);
340 assert(exception->signature == MagickSignature);
341 image=AcquireImage(image_info,exception);
342 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
343 if (status == MagickFalse)
345 image=DestroyImageList(image);
346 return((Image *) NULL);
349 Write raw CCITT Group 4 wrapped as a TIFF image file.
352 unique_file=AcquireUniqueFileResource(filename);
353 if (unique_file != -1)
354 file=fdopen(unique_file,"wb");
355 if ((unique_file == -1) || (file == (FILE *) NULL))
356 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
357 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
358 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
359 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
360 length=WriteLSBLong(file,image->columns);
361 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
362 length=WriteLSBLong(file,image->rows);
363 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
364 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
365 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
366 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
367 strip_offset=10+(12*14)+4+8;
368 length=WriteLSBLong(file,(size_t) strip_offset);
369 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
370 length=WriteLSBLong(file,(size_t) image_info->orientation);
371 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
372 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
373 length=WriteLSBLong(file,image->rows);
374 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
375 offset=(ssize_t) ftell(file)-4;
376 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
377 length=WriteLSBLong(file,(size_t) (strip_offset-8));
378 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
379 length=WriteLSBLong(file,(size_t) (strip_offset-8));
380 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
381 length=fwrite("\000\000\000\000",1,4,file);
382 length=WriteLSBLong(file,(long) image->resolution.x);
383 length=WriteLSBLong(file,1);
384 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
385 (void) fputc(c,file);
386 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
387 length=WriteLSBLong(file,(unsigned int) length);
389 (void) CloseBlob(image);
390 image=DestroyImage(image);
394 read_info=CloneImageInfo((ImageInfo *) NULL);
395 (void) FormatLocaleString(read_info->filename,MaxTextExtent,"%s",filename);
396 image=ReadTIFFImage(read_info,exception);
397 read_info=DestroyImageInfo(read_info);
398 if (image != (Image *) NULL)
400 (void) CopyMagickString(image->filename,image_info->filename,
402 (void) CopyMagickString(image->magick_filename,image_info->filename,
404 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
406 (void) RelinquishUniqueFileResource(filename);
411 #if defined(MAGICKCORE_TIFF_DELEGATE)
413 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417 % R e a d T I F F I m a g e %
421 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
423 % ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
424 % memory necessary for the new Image structure and returns a pointer to the
427 % The format of the ReadTIFFImage method is:
429 % Image *ReadTIFFImage(const ImageInfo *image_info,
430 % ExceptionInfo *exception)
432 % A description of each parameter follows:
434 % o image_info: the image info.
436 % o exception: return any errors or warnings in this structure.
440 static inline unsigned char ClampYCC(double value)
444 return((unsigned char)0);
446 return((unsigned char)255);
447 return((unsigned char)(value));
450 static MagickBooleanType DecodeLabImage(Image *image,ExceptionInfo *exception)
462 image_view=AcquireAuthenticCacheView(image,exception);
463 for (y=0; y < (ssize_t) image->rows; y++)
471 if (status == MagickFalse)
473 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
474 if (q == (Quantum *) NULL)
479 for (x=0; x < (ssize_t) image->columns; x++)
485 a=QuantumScale*GetPixela(image,q)+0.5;
488 b=QuantumScale*GetPixelb(image,q)+0.5;
491 SetPixela(image,QuantumRange*a,q);
492 SetPixelb(image,QuantumRange*b,q);
493 q+=GetPixelChannels(image);
495 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
498 image_view=DestroyCacheView(image_view);
502 static inline size_t MagickMax(const size_t x,const size_t y)
509 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
516 static MagickBooleanType ReadProfile(Image *image,const char *name,
517 unsigned char *datum,ssize_t length,ExceptionInfo *exception)
527 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
529 if (strstr((char *)datum,"8BIM") == (char *) NULL)
532 profile=BlobToStringInfo(datum,(size_t) length);
533 if (profile == (StringInfo *) NULL)
534 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
536 status=SetImageProfile(image,name,profile,exception);
537 profile=DestroyStringInfo(profile);
538 if (status == MagickFalse)
539 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
544 #if defined(__cplusplus) || defined(c_plusplus)
548 static int TIFFCloseBlob(thandle_t image)
550 (void) CloseBlob((Image *) image);
554 static void TIFFErrors(const char *module,const char *format,va_list error)
557 message[MaxTextExtent];
562 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
563 (void) vsnprintf(message,MaxTextExtent,format,error);
565 (void) vsprintf(message,format,error);
567 (void) ConcatenateMagickString(message,".",MaxTextExtent);
568 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
569 if (exception != (ExceptionInfo *) NULL)
570 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
574 static toff_t TIFFGetBlobSize(thandle_t image)
576 return((toff_t) GetBlobSize((Image *) image));
579 static void TIFFGetProfiles(TIFF *tiff,Image *image,ExceptionInfo *exception)
588 #if defined(TIFFTAG_ICCPROFILE)
589 if ((TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1) &&
590 (profile != (unsigned char *) NULL))
591 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
593 #if defined(TIFFTAG_PHOTOSHOP)
594 if ((TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1) &&
595 (profile != (unsigned char *) NULL))
596 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
598 #if defined(TIFFTAG_RICHTIFFIPTC)
599 if ((TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1) &&
600 (profile != (unsigned char *) NULL))
602 if (TIFFIsByteSwapped(tiff) != 0)
603 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
604 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
607 #if defined(TIFFTAG_XMLPACKET)
608 if ((TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1) &&
609 (profile != (unsigned char *) NULL))
610 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
612 if ((TIFFGetField(tiff,37724,&length,&profile) == 1) &&
613 (profile != (unsigned char *) NULL))
614 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
615 if ((TIFFGetField(tiff,34118,&length,&profile) == 1) &&
616 (profile != (unsigned char *) NULL))
617 (void) ReadProfile(image,"tiff:34118",profile,(ssize_t) length,exception);
620 static void TIFFGetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
623 message[MaxTextExtent],
629 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
630 (void) SetImageProperty(image,"tiff:artist",text,exception);
631 if (TIFFGetField(tiff,TIFFTAG_COPYRIGHT,&text) == 1)
632 (void) SetImageProperty(image,"tiff:copyright",text,exception);
633 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
634 (void) SetImageProperty(image,"tiff:timestamp",text,exception);
635 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
636 (void) SetImageProperty(image,"tiff:document",text,exception);
637 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
638 (void) SetImageProperty(image,"tiff:hostcomputer",text,exception);
639 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
640 (void) SetImageProperty(image,"comment",text,exception);
641 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
642 (void) SetImageProperty(image,"tiff:make",text,exception);
643 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
644 (void) SetImageProperty(image,"tiff:model",text,exception);
645 if (TIFFGetField(tiff,TIFFTAG_OPIIMAGEID,&count,&text) == 1)
647 if (count >= MaxTextExtent)
648 count=MaxTextExtent-1;
649 (void) CopyMagickString(message,text,count+1);
650 (void) SetImageProperty(image,"tiff:image-id",message,exception);
652 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
653 (void) SetImageProperty(image,"label",text,exception);
654 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
655 (void) SetImageProperty(image,"tiff:software",text,exception);
656 if (TIFFGetField(tiff,33423,&count,&text) == 1)
658 if (count >= MaxTextExtent)
659 count=MaxTextExtent-1;
660 (void) CopyMagickString(message,text,count+1);
661 (void) SetImageProperty(image,"tiff:kodak-33423",message,exception);
663 if (TIFFGetField(tiff,36867,&count,&text) == 1)
665 if (count >= MaxTextExtent)
666 count=MaxTextExtent-1;
667 (void) CopyMagickString(message,text,count+1);
668 (void) SetImageProperty(image,"tiff:kodak-36867",message,exception);
672 static void TIFFGetEXIFProperties(TIFF *tiff,Image *image,
673 ExceptionInfo *exception)
675 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
677 value[MaxTextExtent];
685 #if defined(TIFF_VERSION_BIG)
696 Read EXIF properties.
699 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) != 1)
701 directory=TIFFCurrentDirectory(tiff);
702 if (TIFFReadEXIFDirectory(tiff,offset) != 1)
704 directory=TIFFCurrentDirectory(tiff);
708 for (i=0; exif_info[i].tag != 0; i++)
711 switch (exif_info[i].type)
719 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans,&sans) == 1) &&
720 (ascii != (char *) NULL) && (*ascii != '\0'))
721 (void) CopyMagickString(value,ascii,MaxTextExtent);
726 if (exif_info[i].variable_length == 0)
732 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans,&sans) == 1)
733 (void) FormatLocaleString(value,MaxTextExtent,"%d",shorty);
746 tiff_status=TIFFGetField(tiff,exif_info[i].tag,&shorty_num,&shorty,
748 if (tiff_status == 1)
749 (void) FormatLocaleString(value,MaxTextExtent,"%d",
750 shorty_num != 0 ? shorty[0] : 0);
760 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans,&sans) == 1)
761 (void) FormatLocaleString(value,MaxTextExtent,"%d",longy);
764 #if defined(TIFF_VERSION_BIG)
771 if (TIFFGetField(tiff,exif_info[i].tag,&long8y,&sans,&sans) == 1)
772 (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
773 ((MagickOffsetType) long8y));
785 if (TIFFGetField(tiff,exif_info[i].tag,&floaty,&sans,&sans) == 1)
786 (void) FormatLocaleString(value,MaxTextExtent,"%g",(double) floaty);
795 if (TIFFGetField(tiff,exif_info[i].tag,&doubley,&sans,&sans) == 1)
796 (void) FormatLocaleString(value,MaxTextExtent,"%g",doubley);
803 (void) SetImageProperty(image,exif_info[i].property,value,exception);
805 TIFFSetDirectory(tiff,directory);
812 static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
814 *base=(tdata_t *) GetBlobStreamData((Image *) image);
815 if (*base != (tdata_t *) NULL)
816 *size=(toff_t) GetBlobSize((Image *) image);
817 if (*base != (tdata_t *) NULL)
822 static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
827 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
828 (unsigned char *) data);
832 static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
833 tsample_t sample,ssize_t row,tdata_t scanline)
838 (void) bits_per_sample;
839 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
843 static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
845 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
848 static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
855 static void TIFFWarnings(const char *module,const char *format,va_list warning)
858 message[MaxTextExtent];
863 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
864 (void) vsnprintf(message,MaxTextExtent,format,warning);
866 (void) vsprintf(message,format,warning);
868 (void) ConcatenateMagickString(message,".",MaxTextExtent);
869 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
870 if (exception != (ExceptionInfo *) NULL)
871 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
872 message,"`%s'",module);
875 static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
880 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
881 (unsigned char *) data);
885 static TIFFMethodType GetJpegMethod(Image* image,TIFF *tiff,uint16 photometric,
886 uint16 bits_per_sample,uint16 samples_per_pixel)
888 #define BUFFER_SIZE 2048
900 #if defined(TIFF_VERSION_BIG)
908 buffer[BUFFER_SIZE+1];
913 /* only support 8 bit for now */
914 if (photometric != PHOTOMETRIC_SEPARATED || bits_per_sample != 8 ||
915 samples_per_pixel != 4)
916 return(ReadGenericMethod);
917 /* Search for Adobe APP14 JPEG Marker */
918 if (!TIFFGetField(tiff,TIFFTAG_STRIPOFFSETS,&value))
919 return(ReadRGBAMethod);
920 position=TellBlob(image);
921 offset=(MagickOffsetType)value[0];
922 if (SeekBlob(image,offset,SEEK_SET) != offset)
923 return(ReadRGBAMethod);
924 method=ReadRGBAMethod;
925 if (ReadBlob(image,BUFFER_SIZE,buffer) == BUFFER_SIZE)
927 for (i=0; i < BUFFER_SIZE; i++)
929 while (i < BUFFER_SIZE)
931 if (buffer[i++] == 255)
934 while (i < BUFFER_SIZE)
936 if (buffer[++i] != 255)
939 if (buffer[i++] == 216) /* JPEG_MARKER_SOI */
941 length=(unsigned short) (((unsigned int) (buffer[i] << 8) |
942 (unsigned int) buffer[i+1]) & 0xffff);
943 if (i+(size_t) length >= BUFFER_SIZE)
945 if (buffer[i-1] == 238) /* JPEG_MARKER_APP0+14 */
949 /* 0 == CMYK, 1 == YCbCr, 2 = YCCK */
950 if (buffer[i+13] == 2)
951 method=ReadYCCKMethod;
957 SeekBlob(image,position,SEEK_SET);
961 #if defined(__cplusplus) || defined(c_plusplus)
965 static Image *ReadTIFFImage(const ImageInfo *image_info,
966 ExceptionInfo *exception)
1044 assert(image_info != (const ImageInfo *) NULL);
1045 assert(image_info->signature == MagickSignature);
1046 if (image_info->debug != MagickFalse)
1047 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1048 image_info->filename);
1049 assert(exception != (ExceptionInfo *) NULL);
1050 assert(exception->signature == MagickSignature);
1051 image=AcquireImage(image_info,exception);
1052 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1053 if (status == MagickFalse)
1055 image=DestroyImageList(image);
1056 return((Image *) NULL);
1058 (void) MagickSetThreadValue(tiff_exception,exception);
1059 error_handler=TIFFSetErrorHandler(TIFFErrors);
1060 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
1061 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
1062 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
1064 if (tiff == (TIFF *) NULL)
1066 (void) TIFFSetWarningHandler(warning_handler);
1067 (void) TIFFSetErrorHandler(error_handler);
1068 image=DestroyImageList(image);
1069 return((Image *) NULL);
1071 debug=IsEventLogging();
1073 if (image_info->number_scenes != 0)
1076 Generate blank images for subimage specification (e.g. image.tif[4].
1078 for (i=0; i < (ssize_t) image_info->scene; i++)
1080 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1081 if (status == MagickFalse)
1084 image=DestroyImageList(image);
1085 return((Image *) NULL);
1087 AcquireNextImage(image_info,image,exception);
1088 if (GetNextImageInList(image) == (Image *) NULL)
1091 image=DestroyImageList(image);
1092 return((Image *) NULL);
1094 image=SyncNextImageInList(image);
1099 DisableMSCWarning(4127)
1100 if (0 && (image_info->verbose != MagickFalse))
1101 TIFFPrintDirectory(tiff,stdout,MagickFalse);
1103 if ((TIFFGetField(tiff,TIFFTAG_IMAGEWIDTH,&width) != 1) ||
1104 (TIFFGetField(tiff,TIFFTAG_IMAGELENGTH,&height) != 1) ||
1105 (TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag) != 1) ||
1106 (TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian) != 1) ||
1107 (TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace) != 1) ||
1108 (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,&samples_per_pixel) != 1) ||
1109 (TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample) != 1) ||
1110 (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format) != 1) ||
1111 (TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value) != 1) ||
1112 (TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value) != 1) ||
1113 (TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric) != 1))
1116 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1118 if (sample_format == SAMPLEFORMAT_IEEEFP)
1119 (void) SetImageProperty(image,"quantum:format","floating-point",
1121 switch (photometric)
1123 case PHOTOMETRIC_MINISBLACK:
1125 (void) SetImageProperty(image,"tiff:photometric","min-is-black",
1129 case PHOTOMETRIC_MINISWHITE:
1131 (void) SetImageProperty(image,"tiff:photometric","min-is-white",
1135 case PHOTOMETRIC_PALETTE:
1137 (void) SetImageProperty(image,"tiff:photometric","palette",exception);
1140 case PHOTOMETRIC_RGB:
1142 (void) SetImageProperty(image,"tiff:photometric","RGB",exception);
1145 case PHOTOMETRIC_CIELAB:
1147 (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception);
1150 case PHOTOMETRIC_LOGL:
1151 case PHOTOMETRIC_LOGLUV:
1153 (void) TIFFSetField(tiff,TIFFTAG_SGILOGDATAFMT,SGILOGDATAFMT_FLOAT);
1156 case PHOTOMETRIC_SEPARATED:
1158 (void) SetImageProperty(image,"tiff:photometric","separated",exception);
1161 case PHOTOMETRIC_YCBCR:
1163 (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception);
1168 (void) SetImageProperty(image,"tiff:photometric","unknown",exception);
1172 if (image->debug != MagickFalse)
1174 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
1175 (unsigned int) width,(unsigned int) height);
1176 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
1178 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1179 "Bits per sample: %u",bits_per_sample);
1180 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1181 "Min sample value: %u",min_sample_value);
1182 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1183 "Max sample value: %u",max_sample_value);
1184 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
1185 "interpretation: %s",GetImageProperty(image,"tiff:photometric",
1188 image->columns=(size_t) width;
1189 image->rows=(size_t) height;
1190 image->depth=(size_t) bits_per_sample;
1191 status=SetImageExtent(image,image->columns,image->rows,exception);
1192 if (status == MagickFalse)
1193 return(DestroyImageList(image));
1194 if (image->debug != MagickFalse)
1195 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
1196 (double) image->depth);
1197 image->endian=MSBEndian;
1198 if (endian == FILLORDER_LSB2MSB)
1199 image->endian=LSBEndian;
1200 #if defined(MAGICKCORE_HAVE_TIFFISBIGENDIAN)
1201 if (TIFFIsBigEndian(tiff) == 0)
1203 (void) SetImageProperty(image,"tiff:endian","lsb",exception);
1204 image->endian=LSBEndian;
1208 (void) SetImageProperty(image,"tiff:endian","msb",exception);
1209 image->endian=MSBEndian;
1212 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1213 (photometric == PHOTOMETRIC_MINISWHITE))
1214 SetImageColorspace(image,GRAYColorspace,exception);
1215 if (photometric == PHOTOMETRIC_SEPARATED)
1216 SetImageColorspace(image,CMYKColorspace,exception);
1217 if (photometric == PHOTOMETRIC_CIELAB)
1218 SetImageColorspace(image,LabColorspace,exception);
1219 TIFFGetProfiles(tiff,image,exception);
1220 TIFFGetProperties(tiff,image,exception);
1221 option=GetImageOption(image_info,"tiff:exif-properties");
1222 if (IfMagickTrue(IsStringNotFalse(option))) /* enabled by default */
1223 TIFFGetEXIFProperties(tiff,image,exception);
1224 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
1225 &samples_per_pixel);
1226 if ((TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution) == 1) &&
1227 (TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution) == 1))
1229 image->resolution.x=x_resolution;
1230 image->resolution.y=y_resolution;
1232 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units) == 1)
1234 if (units == RESUNIT_INCH)
1235 image->units=PixelsPerInchResolution;
1236 if (units == RESUNIT_CENTIMETER)
1237 image->units=PixelsPerCentimeterResolution;
1239 if ((TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position) == 1) &&
1240 (TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position) == 1))
1242 image->page.x=(ssize_t) ceil(x_position*image->resolution.x-0.5);
1243 image->page.y=(ssize_t) ceil(y_position*image->resolution.y-0.5);
1245 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation) == 1)
1246 image->orientation=(OrientationType) orientation;
1247 if (TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity) == 1)
1249 if (chromaticity != (float *) NULL)
1251 image->chromaticity.white_point.x=chromaticity[0];
1252 image->chromaticity.white_point.y=chromaticity[1];
1255 if (TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity) == 1)
1257 if (chromaticity != (float *) NULL)
1259 image->chromaticity.red_primary.x=chromaticity[0];
1260 image->chromaticity.red_primary.y=chromaticity[1];
1261 image->chromaticity.green_primary.x=chromaticity[2];
1262 image->chromaticity.green_primary.y=chromaticity[3];
1263 image->chromaticity.blue_primary.x=chromaticity[4];
1264 image->chromaticity.blue_primary.y=chromaticity[5];
1267 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1268 if ((compress_tag != COMPRESSION_NONE) &&
1269 (TIFFIsCODECConfigured(compress_tag) == 0))
1272 ThrowReaderException(CoderError,"CompressNotSupported");
1275 switch (compress_tag)
1277 case COMPRESSION_NONE: image->compression=NoCompression; break;
1278 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1279 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1280 case COMPRESSION_JPEG:
1282 image->compression=JPEGCompression;
1283 #if defined(JPEG_SUPPORT)
1286 sampling_factor[MaxTextExtent];
1295 tiff_status=TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1296 &horizontal,&vertical);
1297 if (tiff_status == 1)
1299 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
1300 horizontal,vertical);
1301 (void) SetImageProperty(image,"jpeg:sampling-factor",
1302 sampling_factor,exception);
1303 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1304 "Sampling Factors: %s",sampling_factor);
1310 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1311 #if defined(COMPRESSION_LZMA)
1312 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1314 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1315 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1316 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1317 default: image->compression=RLECompression; break;
1320 Allocate memory for the image and pixel buffer.
1322 quantum_info=AcquireQuantumInfo(image_info,image);
1323 if (quantum_info == (QuantumInfo *) NULL)
1326 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1328 if (sample_format == SAMPLEFORMAT_UINT)
1329 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1330 if (sample_format == SAMPLEFORMAT_INT)
1331 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1332 if (sample_format == SAMPLEFORMAT_IEEEFP)
1333 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1334 if (status == MagickFalse)
1336 quantum_info=DestroyQuantumInfo(quantum_info);
1338 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1341 switch (photometric)
1343 case PHOTOMETRIC_MINISBLACK:
1345 quantum_info->min_is_white=MagickFalse;
1348 case PHOTOMETRIC_MINISWHITE:
1350 quantum_info->min_is_white=MagickTrue;
1356 tiff_status=TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1358 if (tiff_status == 1)
1360 (void) SetImageProperty(image,"tiff:alpha","unspecified",exception);
1361 if (extra_samples == 0)
1363 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1364 image->alpha_trait=BlendPixelTrait;
1367 for (i=0; i < extra_samples; i++)
1369 image->alpha_trait=BlendPixelTrait;
1370 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
1372 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
1373 (void) SetImageProperty(image,"tiff:alpha","associated",
1377 if (sample_info[i] == EXTRASAMPLE_UNASSALPHA)
1378 (void) SetImageProperty(image,"tiff:alpha","unassociated",
1382 if ((photometric == PHOTOMETRIC_PALETTE) &&
1383 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1388 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
1389 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
1392 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1395 value=(unsigned short) image->scene;
1396 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages) == 1)
1398 if (image_info->ping != MagickFalse)
1400 if (image_info->number_scenes != 0)
1401 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1403 goto next_tiff_frame;
1405 method=ReadGenericMethod;
1406 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) == 1)
1409 value[MaxTextExtent];
1411 method=ReadStripMethod;
1412 (void) FormatLocaleString(value,MaxTextExtent,"%u",
1413 (unsigned int) rows_per_strip);
1414 (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception);
1416 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1417 method=ReadRGBAMethod;
1418 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1419 method=ReadCMYKAMethod;
1420 if ((photometric != PHOTOMETRIC_RGB) &&
1421 (photometric != PHOTOMETRIC_CIELAB) &&
1422 (photometric != PHOTOMETRIC_SEPARATED))
1423 method=ReadGenericMethod;
1424 if (image->storage_class == PseudoClass)
1425 method=ReadSingleSampleMethod;
1426 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1427 (photometric == PHOTOMETRIC_MINISWHITE))
1428 method=ReadSingleSampleMethod;
1429 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1430 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
1431 method=ReadGenericMethod;
1432 if (image->compression == JPEGCompression)
1433 method=GetJpegMethod(image,tiff,photometric,bits_per_sample,
1435 if (TIFFIsTiled(tiff) != MagickFalse)
1436 method=ReadTileMethod;
1437 quantum_info->endian=LSBEndian;
1438 quantum_type=RGBQuantum;
1439 pixels=GetQuantumPixels(quantum_info);
1442 case ReadSingleSampleMethod:
1445 Convert TIFF image to PseudoClass MIFF image.
1447 if ((image->storage_class == PseudoClass) &&
1448 (photometric == PHOTOMETRIC_PALETTE))
1462 Initialize colormap.
1464 tiff_status=TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1465 &green_colormap,&blue_colormap);
1466 if (tiff_status == 1)
1468 if ((red_colormap != (uint16 *) NULL) &&
1469 (green_colormap != (uint16 *) NULL) &&
1470 (blue_colormap != (uint16 *) NULL))
1472 range=255; /* might be old style 8-bit colormap */
1473 for (i=0; i < (ssize_t) image->colors; i++)
1474 if ((red_colormap[i] >= 256) ||
1475 (green_colormap[i] >= 256) ||
1476 (blue_colormap[i] >= 256))
1481 for (i=0; i < (ssize_t) image->colors; i++)
1483 image->colormap[i].red=ClampToQuantum(((double)
1484 QuantumRange*red_colormap[i])/range);
1485 image->colormap[i].green=ClampToQuantum(((double)
1486 QuantumRange*green_colormap[i])/range);
1487 image->colormap[i].blue=ClampToQuantum(((double)
1488 QuantumRange*blue_colormap[i])/range);
1493 quantum_type=IndexQuantum;
1494 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1495 if (image->alpha_trait != UndefinedPixelTrait)
1497 if (image->storage_class != PseudoClass)
1499 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1501 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1505 quantum_type=IndexAlphaQuantum;
1506 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1510 if (image->storage_class != PseudoClass)
1512 quantum_type=GrayQuantum;
1513 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1515 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1516 if (status == MagickFalse)
1519 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1521 pixels=GetQuantumPixels(quantum_info);
1522 for (y=0; y < (ssize_t) image->rows; y++)
1530 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1533 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1534 if (q == (Quantum *) NULL)
1536 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1537 quantum_type,pixels,exception);
1538 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1540 if (image->previous == (Image *) NULL)
1542 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1544 if (status == MagickFalse)
1550 case ReadRGBAMethod:
1553 Convert TIFF image to DirectClass MIFF image.
1555 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1556 quantum_type=RGBQuantum;
1557 if (image->alpha_trait != UndefinedPixelTrait)
1559 quantum_type=RGBAQuantum;
1560 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1562 if (image->colorspace == CMYKColorspace)
1564 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1565 quantum_type=CMYKQuantum;
1566 if (image->alpha_trait != UndefinedPixelTrait)
1568 quantum_type=CMYKAQuantum;
1569 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1572 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1573 if (status == MagickFalse)
1576 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1578 pixels=GetQuantumPixels(quantum_info);
1579 for (y=0; y < (ssize_t) image->rows; y++)
1587 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1590 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1591 if (q == (Quantum *) NULL)
1593 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1594 quantum_type,pixels,exception);
1595 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1597 if (image->previous == (Image *) NULL)
1599 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1601 if (status == MagickFalse)
1607 case ReadCMYKAMethod:
1610 Convert TIFF image to DirectClass MIFF image.
1612 for (i=0; i < (ssize_t) samples_per_pixel; i++)
1614 for (y=0; y < (ssize_t) image->rows; y++)
1622 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1626 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1627 if (q == (Quantum *) NULL)
1629 if (image->colorspace != CMYKColorspace)
1632 case 0: quantum_type=RedQuantum; break;
1633 case 1: quantum_type=GreenQuantum; break;
1634 case 2: quantum_type=BlueQuantum; break;
1635 case 3: quantum_type=AlphaQuantum; break;
1636 default: quantum_type=UndefinedQuantum; break;
1641 case 0: quantum_type=CyanQuantum; break;
1642 case 1: quantum_type=MagentaQuantum; break;
1643 case 2: quantum_type=YellowQuantum; break;
1644 case 3: quantum_type=BlackQuantum; break;
1645 case 4: quantum_type=AlphaQuantum; break;
1646 default: quantum_type=UndefinedQuantum; break;
1648 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1649 quantum_type,pixels,exception);
1650 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1653 if (image->previous == (Image *) NULL)
1655 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1657 if (status == MagickFalse)
1663 case ReadYCCKMethod:
1665 pixels=GetQuantumPixels(quantum_info);
1666 for (y=0; y < (ssize_t) image->rows; y++)
1680 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1683 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1684 if (q == (Quantum *) NULL)
1687 for (x=0; x < (ssize_t) image->columns; x++)
1689 SetPixelCyan(image,ScaleCharToQuantum(ClampYCC((double) *p+
1690 (1.402*(double) *(p+2))-179.456)),q);
1691 SetPixelMagenta(image,ScaleCharToQuantum(ClampYCC((double) *p-
1692 (0.34414*(double) *(p+1))-(0.71414*(double ) *(p+2))+
1694 SetPixelYellow(image,ScaleCharToQuantum(ClampYCC((double) *p+
1695 (1.772*(double) *(p+1))-226.816)),q);
1696 SetPixelBlack(image,ScaleCharToQuantum((unsigned char) *(p+3)),q);
1697 q+=GetPixelChannels(image);
1700 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1702 if (image->previous == (Image *) NULL)
1704 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1706 if (status == MagickFalse)
1712 case ReadStripMethod:
1718 Convert stripped TIFF image to DirectClass MIFF image.
1722 for (y=0; y < (ssize_t) image->rows; y++)
1730 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1731 if (q == (Quantum *) NULL)
1735 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1737 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1741 p=((uint32 *) pixels)+image->columns*i;
1742 for (x=0; x < (ssize_t) image->columns; x++)
1744 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1746 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1748 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1750 if (image->alpha_trait != UndefinedPixelTrait)
1751 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1754 q+=GetPixelChannels(image);
1756 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1758 if (image->previous == (Image *) NULL)
1760 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1762 if (status == MagickFalse)
1768 case ReadTileMethod:
1782 Convert tiled TIFF image to DirectClass MIFF image.
1784 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) != 1) ||
1785 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) != 1))
1788 ThrowReaderException(CoderError,"ImageIsNotTiled");
1790 (void) SetImageStorageClass(image,DirectClass,exception);
1791 number_pixels=columns*rows;
1792 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
1793 sizeof(*tile_pixels));
1794 if (tile_pixels == (uint32 *) NULL)
1797 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1799 for (y=0; y < (ssize_t) image->rows; y+=rows)
1812 rows_remaining=image->rows-y;
1813 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
1814 rows_remaining=rows;
1815 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1817 if (tile == (Quantum *) NULL)
1819 for (x=0; x < (ssize_t) image->columns; x+=columns)
1825 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1827 columns_remaining=image->columns-x;
1828 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
1829 columns_remaining=columns;
1830 p=tile_pixels+(rows-rows_remaining)*columns;
1831 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1833 for (row=rows_remaining; row > 0; row--)
1835 if (image->alpha_trait != UndefinedPixelTrait)
1836 for (column=columns_remaining; column > 0; column--)
1838 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1840 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1842 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1844 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1847 q+=GetPixelChannels(image);
1850 for (column=columns_remaining; column > 0; column--)
1852 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1854 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1856 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1859 q+=GetPixelChannels(image);
1861 p+=columns-columns_remaining;
1862 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
1865 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1867 if (image->previous == (Image *) NULL)
1869 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1871 if (status == MagickFalse)
1875 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1878 case ReadGenericMethod:
1891 Convert TIFF image to DirectClass MIFF image.
1893 number_pixels=(MagickSizeType) image->columns*image->rows;
1894 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1895 (number_pixels*sizeof(uint32))))
1898 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1900 pixel_info=AcquireVirtualMemory(image->columns,image->rows*
1902 if (pixel_info == (MemoryInfo *) NULL)
1905 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1907 pixels=(uint32 *) GetVirtualMemoryBlob(pixel_info);
1908 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1909 (uint32) image->rows,(uint32 *) pixels,0);
1911 Convert image to DirectClass pixel packets.
1913 p=pixels+number_pixels-1;
1914 for (y=0; y < (ssize_t) image->rows; y++)
1922 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1923 if (q == (Quantum *) NULL)
1925 q+=GetPixelChannels(image)*(image->columns-1);
1926 for (x=0; x < (ssize_t) image->columns; x++)
1928 SetPixelRed(image,ScaleCharToQuantum((unsigned char) TIFFGetR(*p)),q);
1929 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1931 SetPixelBlue(image,ScaleCharToQuantum((unsigned char) TIFFGetB(*p)),
1933 if (image->alpha_trait != UndefinedPixelTrait)
1934 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1937 q-=GetPixelChannels(image);
1939 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1941 if (image->previous == (Image *) NULL)
1943 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1945 if (status == MagickFalse)
1949 pixel_info=RelinquishVirtualMemory(pixel_info);
1953 SetQuantumImageType(image,quantum_type);
1955 quantum_info=DestroyQuantumInfo(quantum_info);
1956 if (photometric == PHOTOMETRIC_CIELAB)
1957 DecodeLabImage(image,exception);
1958 if ((photometric == PHOTOMETRIC_LOGL) ||
1959 (photometric == PHOTOMETRIC_MINISBLACK) ||
1960 (photometric == PHOTOMETRIC_MINISWHITE))
1962 image->type=GrayscaleType;
1963 if (bits_per_sample == 1)
1964 image->type=BilevelType;
1966 if (image->storage_class == PseudoClass)
1967 image->depth=GetImageDepth(image,exception);
1969 Proceed to next image.
1971 if (image_info->number_scenes != 0)
1972 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1974 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1975 if (status != MagickFalse)
1978 Allocate next image structure.
1980 AcquireNextImage(image_info,image,exception);
1981 if (GetNextImageInList(image) == (Image *) NULL)
1983 image=DestroyImageList(image);
1984 return((Image *) NULL);
1986 image=SyncNextImageInList(image);
1987 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1989 if (status == MagickFalse)
1992 } while (status != MagickFalse);
1993 (void) TIFFSetWarningHandler(warning_handler);
1994 (void) TIFFSetErrorHandler(error_handler);
1996 return(GetFirstImageInList(image));
2001 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2005 % R e g i s t e r T I F F I m a g e %
2009 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2011 % RegisterTIFFImage() adds properties for the TIFF image format to
2012 % the list of supported formats. The properties include the image format
2013 % tag, a method to read and/or write the format, whether the format
2014 % supports the saving of more than one frame to the same file or blob,
2015 % whether the format supports native in-memory I/O, and a brief
2016 % description of the format.
2018 % The format of the RegisterTIFFImage method is:
2020 % size_t RegisterTIFFImage(void)
2024 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2025 static TIFFExtendProc
2026 tag_extender = (TIFFExtendProc) NULL;
2028 static void TIFFIgnoreTags(TIFF *tiff)
2049 if (TIFFGetReadProc(tiff) != TIFFReadBlob)
2051 image=(Image *)TIFFClientdata(tiff);
2052 tags=GetImageArtifact(image,"tiff:ignore-tags");
2053 if (tags == (const char *) NULL)
2059 while ((isspace((int) ((unsigned char) *p)) != 0))
2062 (void) strtol(p,&q,10);
2069 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2076 ignore=(TIFFFieldInfo *) AcquireQuantumMemory(count,sizeof(*ignore));
2077 // This also sets field_bit to 0 (FIELD_IGNORE)
2078 ResetMagickMemory(ignore,0,count*sizeof(*ignore));
2081 while ((isspace((int) ((unsigned char) *p)) != 0))
2084 ignore[i].field_tag=(ttag_t) strtol(p,&q,10);
2089 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2092 (void) TIFFMergeFieldInfo(tiff,ignore,(uint32) count);
2093 ignore=(TIFFFieldInfo *) RelinquishMagickMemory(ignore);
2096 static void TIFFTagExtender(TIFF *tiff)
2098 static const TIFFFieldInfo
2101 { 37724, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
2102 (char *) "PhotoshopLayerData" },
2103 { 34118, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
2104 (char *) "Microscope" }
2107 TIFFMergeFieldInfo(tiff,TIFFExtensions,sizeof(TIFFExtensions)/
2108 sizeof(*TIFFExtensions));
2109 if (tag_extender != (TIFFExtendProc) NULL)
2110 (*tag_extender)(tiff);
2111 TIFFIgnoreTags(tiff);
2115 ModuleExport size_t RegisterTIFFImage(void)
2117 #define TIFFDescription "Tagged Image File Format"
2120 version[MaxTextExtent];
2125 if (tiff_semaphore == (SemaphoreInfo *) NULL)
2126 ActivateSemaphoreInfo(&tiff_semaphore);
2127 LockSemaphoreInfo(tiff_semaphore);
2128 if (instantiate_key == MagickFalse)
2130 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
2131 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2132 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2133 if (tag_extender == (TIFFExtendProc) NULL)
2134 tag_extender=TIFFSetTagExtender(TIFFTagExtender);
2136 instantiate_key=MagickTrue;
2138 UnlockSemaphoreInfo(tiff_semaphore);
2140 #if defined(TIFF_VERSION)
2141 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
2143 #if defined(MAGICKCORE_TIFF_DELEGATE)
2152 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
2158 entry=SetMagickInfo("GROUP4");
2159 #if defined(MAGICKCORE_TIFF_DELEGATE)
2160 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
2161 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
2163 entry->raw=MagickTrue;
2164 entry->endian_support=MagickTrue;
2165 entry->adjoin=MagickFalse;
2166 entry->format_type=ImplicitFormatType;
2167 entry->seekable_stream=MagickTrue;
2168 entry->description=ConstantString("Raw CCITT Group4");
2169 entry->mime_type=ConstantString("image/tiff");
2170 entry->module=ConstantString("TIFF");
2171 (void) RegisterMagickInfo(entry);
2172 entry=SetMagickInfo("PTIF");
2173 #if defined(MAGICKCORE_TIFF_DELEGATE)
2174 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2175 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
2177 entry->endian_support=MagickTrue;
2178 entry->seekable_stream=MagickTrue;
2179 entry->description=ConstantString("Pyramid encoded TIFF");
2180 entry->mime_type=ConstantString("image/tiff");
2181 entry->module=ConstantString("TIFF");
2182 (void) RegisterMagickInfo(entry);
2183 entry=SetMagickInfo("TIF");
2184 #if defined(MAGICKCORE_TIFF_DELEGATE)
2185 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2186 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2188 entry->endian_support=MagickTrue;
2189 entry->seekable_stream=MagickTrue;
2190 entry->stealth=MagickTrue;
2191 entry->description=ConstantString(TIFFDescription);
2192 if (*version != '\0')
2193 entry->version=ConstantString(version);
2194 entry->mime_type=ConstantString("image/tiff");
2195 entry->module=ConstantString("TIFF");
2196 (void) RegisterMagickInfo(entry);
2197 entry=SetMagickInfo("TIFF");
2198 #if defined(MAGICKCORE_TIFF_DELEGATE)
2199 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2200 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2202 entry->magick=(IsImageFormatHandler *) IsTIFF;
2203 entry->endian_support=MagickTrue;
2204 entry->seekable_stream=MagickTrue;
2205 entry->description=ConstantString(TIFFDescription);
2206 if (*version != '\0')
2207 entry->version=ConstantString(version);
2208 entry->mime_type=ConstantString("image/tiff");
2209 entry->module=ConstantString("TIFF");
2210 (void) RegisterMagickInfo(entry);
2211 entry=SetMagickInfo("TIFF64");
2212 #if defined(TIFF_VERSION_BIG)
2213 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2214 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2216 entry->adjoin=MagickFalse;
2217 entry->endian_support=MagickTrue;
2218 entry->seekable_stream=MagickTrue;
2219 entry->description=ConstantString("Tagged Image File Format (64-bit)");
2220 if (*version != '\0')
2221 entry->version=ConstantString(version);
2222 entry->mime_type=ConstantString("image/tiff");
2223 entry->module=ConstantString("TIFF");
2224 (void) RegisterMagickInfo(entry);
2225 return(MagickImageCoderSignature);
2229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2233 % U n r e g i s t e r T I F F I m a g e %
2237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2239 % UnregisterTIFFImage() removes format registrations made by the TIFF module
2240 % from the list of supported formats.
2242 % The format of the UnregisterTIFFImage method is:
2244 % UnregisterTIFFImage(void)
2247 ModuleExport void UnregisterTIFFImage(void)
2249 (void) UnregisterMagickInfo("TIFF64");
2250 (void) UnregisterMagickInfo("TIFF");
2251 (void) UnregisterMagickInfo("TIF");
2252 (void) UnregisterMagickInfo("PTIF");
2253 if (tiff_semaphore == (SemaphoreInfo *) NULL)
2254 ActivateSemaphoreInfo(&tiff_semaphore);
2255 LockSemaphoreInfo(tiff_semaphore);
2256 if (instantiate_key != MagickFalse)
2258 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2259 if (tag_extender == (TIFFExtendProc) NULL)
2260 (void) TIFFSetTagExtender(tag_extender);
2262 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
2263 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2264 instantiate_key=MagickFalse;
2266 UnlockSemaphoreInfo(tiff_semaphore);
2267 RelinquishSemaphoreInfo(&tiff_semaphore);
2270 #if defined(MAGICKCORE_TIFF_DELEGATE)
2272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2276 % W r i t e G R O U P 4 I m a g e %
2280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2282 % WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
2284 % The format of the WriteGROUP4Image method is:
2286 % MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2287 % Image *image,ExceptionInfo *)
2289 % A description of each parameter follows:
2291 % o image_info: the image info.
2293 % o image: The image.
2295 % o exception: return any errors or warnings in this structure.
2298 static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2299 Image *image,ExceptionInfo *exception)
2302 filename[MaxTextExtent];
2336 Write image as CCITT Group4 TIFF image to a temporary file.
2338 assert(image_info != (const ImageInfo *) NULL);
2339 assert(image_info->signature == MagickSignature);
2340 assert(image != (Image *) NULL);
2341 assert(image->signature == MagickSignature);
2342 if (image->debug != MagickFalse)
2343 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2344 assert(exception != (ExceptionInfo *) NULL);
2345 assert(exception->signature == MagickSignature);
2346 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2347 if (status == MagickFalse)
2349 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
2350 if (huffman_image == (Image *) NULL)
2352 (void) CloseBlob(image);
2353 return(MagickFalse);
2355 huffman_image->endian=MSBEndian;
2357 unique_file=AcquireUniqueFileResource(filename);
2358 if (unique_file != -1)
2359 file=fdopen(unique_file,"wb");
2360 if ((unique_file == -1) || (file == (FILE *) NULL))
2362 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2364 return(MagickFalse);
2366 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
2368 (void) SetImageType(huffman_image,BilevelType,exception);
2369 write_info=CloneImageInfo((ImageInfo *) NULL);
2370 SetImageInfoFile(write_info,file);
2371 write_info->compression=Group4Compression;
2372 write_info->type=BilevelType;
2373 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
2374 status=WriteTIFFImage(write_info,huffman_image,exception);
2375 (void) fflush(file);
2376 write_info=DestroyImageInfo(write_info);
2377 if (status == MagickFalse)
2379 huffman_image=DestroyImage(huffman_image);
2380 (void) fclose(file);
2381 (void) RelinquishUniqueFileResource(filename);
2382 return(MagickFalse);
2384 tiff=TIFFOpen(filename,"rb");
2385 if (tiff == (TIFF *) NULL)
2387 huffman_image=DestroyImage(huffman_image);
2388 (void) fclose(file);
2389 (void) RelinquishUniqueFileResource(filename);
2390 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
2391 image_info->filename);
2392 return(MagickFalse);
2395 Allocate raw strip buffer.
2397 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2400 huffman_image=DestroyImage(huffman_image);
2401 (void) fclose(file);
2402 (void) RelinquishUniqueFileResource(filename);
2403 return(MagickFalse);
2405 strip_size=byte_count[0];
2406 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2407 if (byte_count[i] > strip_size)
2408 strip_size=byte_count[i];
2409 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2411 if (buffer == (unsigned char *) NULL)
2414 huffman_image=DestroyImage(huffman_image);
2415 (void) fclose(file);
2416 (void) RelinquishUniqueFileResource(filename);
2417 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2418 image_info->filename);
2421 Compress runlength encoded to 2D Huffman pixels.
2423 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2425 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
2426 if (WriteBlob(image,(size_t) count,buffer) != count)
2429 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2431 huffman_image=DestroyImage(huffman_image);
2432 (void) fclose(file);
2433 (void) RelinquishUniqueFileResource(filename);
2434 (void) CloseBlob(image);
2439 #if defined(MAGICKCORE_TIFF_DELEGATE)
2441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2445 % W r i t e P T I F I m a g e %
2449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2451 % WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2454 % The format of the WritePTIFImage method is:
2456 % MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2457 % Image *image,ExceptionInfo *exception)
2459 % A description of each parameter follows:
2461 % o image_info: the image info.
2463 % o image: The image.
2465 % o exception: return any errors or warnings in this structure.
2468 static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2469 Image *image,ExceptionInfo *exception)
2490 Create pyramid-encoded TIFF image.
2492 images=NewImageList();
2493 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2498 clone_image=CloneImage(next,0,0,MagickFalse,exception);
2499 clone_image->previous=NewImageList();
2500 clone_image->next=NewImageList();
2501 AppendImageToList(&images,clone_image);
2502 columns=next->columns;
2504 resolution=next->resolution;
2505 while ((columns > 64) && (rows > 64))
2511 pyramid_image=ResizeImage(next,columns,rows,image->filter,exception);
2512 if (pyramid_image == (Image *) NULL)
2514 pyramid_image->resolution=resolution;
2515 AppendImageToList(&images,pyramid_image);
2518 images=GetFirstImageInList(images);
2520 Write pyramid-encoded TIFF image.
2522 write_info=CloneImageInfo(image_info);
2523 write_info->adjoin=MagickTrue;
2524 (void) CopyMagickString(write_info->magick,"TIFF",MaxTextExtent);
2525 (void) CopyMagickString(images->magick,"TIFF",MaxTextExtent);
2526 status=WriteTIFFImage(write_info,images,exception);
2527 images=DestroyImageList(images);
2528 write_info=DestroyImageInfo(write_info);
2533 #if defined(MAGICKCORE_TIFF_DELEGATE)
2535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2538 % W r i t e T I F F I m a g e %
2542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2544 % WriteTIFFImage() writes an image in the Tagged image file format.
2546 % The format of the WriteTIFFImage method is:
2548 % MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2549 % Image *image,ExceptionInfo *exception)
2551 % A description of each parameter follows:
2553 % o image_info: the image info.
2555 % o image: The image.
2557 % o exception: return any errors or warnings in this structure.
2561 typedef struct _TIFFInfo
2572 static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2574 assert(tiff_info != (TIFFInfo *) NULL);
2575 if (tiff_info->scanlines != (unsigned char *) NULL)
2576 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2577 tiff_info->scanlines);
2578 if (tiff_info->pixels != (unsigned char *) NULL)
2579 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2583 static MagickBooleanType EncodeLabImage(Image *image,ExceptionInfo *exception)
2595 image_view=AcquireAuthenticCacheView(image,exception);
2596 for (y=0; y < (ssize_t) image->rows; y++)
2604 if (status == MagickFalse)
2606 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2607 if (q == (Quantum *) NULL)
2612 for (x=0; x < (ssize_t) image->columns; x++)
2618 a=QuantumScale*GetPixela(image,q)-0.5;
2621 b=QuantumScale*GetPixelb(image,q)-0.5;
2624 SetPixela(image,QuantumRange*a,q);
2625 SetPixelb(image,QuantumRange*b,q);
2626 q+=GetPixelChannels(image);
2628 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2631 image_view=DestroyCacheView(image_view);
2635 static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,
2636 TIFF *tiff,TIFFInfo *tiff_info)
2648 assert(tiff_info != (TIFFInfo *) NULL);
2649 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2650 option=GetImageOption(image_info,"tiff:tile-geometry");
2651 if (option == (const char *) NULL)
2653 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2654 if ((flags & HeightValue) == 0)
2655 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2656 tile_columns=(uint32) tiff_info->tile_geometry.width;
2657 tile_rows=(uint32) tiff_info->tile_geometry.height;
2658 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2659 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2660 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2661 tiff_info->tile_geometry.width=tile_columns;
2662 tiff_info->tile_geometry.height=tile_rows;
2663 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2664 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
2665 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2666 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2667 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2668 (tiff_info->pixels == (unsigned char *) NULL))
2670 DestroyTIFFInfo(tiff_info);
2671 return(MagickFalse);
2676 static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
2677 tsample_t sample,Image *image)
2685 register unsigned char
2699 if (TIFFIsTiled(tiff) == 0)
2700 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2702 Fill scanlines to tile height.
2704 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2705 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2706 (size_t) TIFFScanlineSize(tiff));
2707 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2708 (tiff_info->tile_geometry.height-1)) &&
2709 (row != (ssize_t) (image->rows-1)))
2712 Write tile to TIFF image.
2715 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (
2716 tiff_info->tile_geometry.height*tiff_info->tile_geometry.width);
2717 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2718 tiff_info->tile_geometry.width;
2719 for (i=0; i < (ssize_t) number_tiles; i++)
2721 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
2722 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2723 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2724 for (k=0; k < (ssize_t) tile_width; k++)
2726 if (bytes_per_pixel == 0)
2728 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2729 tiff_info->tile_geometry.width+k)/8);
2730 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2734 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2735 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2736 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2737 for (l=0; l < bytes_per_pixel; l++)
2740 if ((i*tiff_info->tile_geometry.width) != image->columns)
2741 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2742 tiff_info->tile_geometry.width),(uint32) ((row/
2743 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2751 static void TIFFSetProfiles(TIFF *tiff,Image *image)
2759 if (image->profiles == (void *) NULL)
2761 ResetImageProfileIterator(image);
2762 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2764 profile=GetImageProfile(image,name);
2765 if (GetStringInfoLength(profile) == 0)
2767 name=GetNextImageProfile(image);
2770 #if defined(TIFFTAG_XMLPACKET)
2771 if (LocaleCompare(name,"xmp") == 0)
2772 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2773 profile),GetStringInfoDatum(profile));
2775 #if defined(TIFFTAG_ICCPROFILE)
2776 if (LocaleCompare(name,"icc") == 0)
2777 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2778 profile),GetStringInfoDatum(profile));
2780 if (LocaleCompare(name,"iptc") == 0)
2788 iptc_profile=CloneStringInfo(profile);
2789 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2791 SetStringInfoLength(iptc_profile,length);
2792 if (TIFFIsByteSwapped(tiff))
2793 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2794 (unsigned long) (length/4));
2795 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2796 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2797 iptc_profile=DestroyStringInfo(iptc_profile);
2799 #if defined(TIFFTAG_PHOTOSHOP)
2800 if (LocaleCompare(name,"8bim") == 0)
2801 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2802 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2804 if (LocaleCompare(name,"tiff:37724") == 0)
2805 (void) TIFFSetField(tiff,37724,(uint32) GetStringInfoLength(profile),
2806 GetStringInfoDatum(profile));
2807 if (LocaleCompare(name,"tiff:34118") == 0)
2808 (void) TIFFSetField(tiff,34118,(uint32) GetStringInfoLength(profile),
2809 GetStringInfoDatum(profile));
2810 name=GetNextImageProfile(image);
2814 static void TIFFSetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
2819 value=GetImageArtifact(image,"tiff:document");
2820 if (value != (const char *) NULL)
2821 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,value);
2822 value=GetImageArtifact(image,"tiff:hostcomputer");
2823 if (value != (const char *) NULL)
2824 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2825 value=GetImageArtifact(image,"tiff:artist");
2826 if (value != (const char *) NULL)
2827 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2828 value=GetImageArtifact(image,"tiff:timestamp");
2829 if (value != (const char *) NULL)
2830 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2831 value=GetImageArtifact(image,"tiff:make");
2832 if (value != (const char *) NULL)
2833 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2834 value=GetImageArtifact(image,"tiff:model");
2835 if (value != (const char *) NULL)
2836 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2837 value=GetImageArtifact(image,"tiff:software");
2838 if (value != (const char *) NULL)
2839 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
2840 value=GetImageArtifact(image,"tiff:copyright");
2841 if (value != (const char *) NULL)
2842 (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value);
2843 value=GetImageArtifact(image,"kodak-33423");
2844 if (value != (const char *) NULL)
2845 (void) TIFFSetField(tiff,33423,value);
2846 value=GetImageArtifact(image,"kodak-36867");
2847 if (value != (const char *) NULL)
2848 (void) TIFFSetField(tiff,36867,value);
2849 value=GetImageArtifact(image,"label");
2850 if (value != (const char *) NULL)
2851 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2852 value=GetImageArtifact(image,"comment");
2853 if (value != (const char *) NULL)
2854 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2857 static void TIFFSetEXIFProperties(TIFF *tiff,Image *image,
2858 ExceptionInfo *exception)
2860 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2871 Write EXIF properties.
2874 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2875 for (i=0; exif_info[i].tag != 0; i++)
2877 value=GetImageProperty(image,exif_info[i].property,exception);
2878 if (value == (const char *) NULL)
2880 switch (exif_info[i].type)
2884 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2892 field=(uint16) StringToLong(value);
2893 (void) TIFFSetField(tiff,exif_info[i].tag,field);
2901 field=(uint16) StringToLong(value);
2902 (void) TIFFSetField(tiff,exif_info[i].tag,field);
2906 case TIFF_SRATIONAL:
2911 field=StringToDouble(value,(char **) NULL);
2912 (void) TIFFSetField(tiff,exif_info[i].tag,field);
2919 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2926 static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2927 Image *image,ExceptionInfo *exception)
2929 #if !defined(TIFFDefaultStripSize)
2930 #define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2990 assert(image_info != (const ImageInfo *) NULL);
2991 assert(image_info->signature == MagickSignature);
2992 assert(image != (Image *) NULL);
2993 assert(image->signature == MagickSignature);
2994 if (image->debug != MagickFalse)
2995 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2996 assert(exception != (ExceptionInfo *) NULL);
2997 assert(exception->signature == MagickSignature);
2998 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2999 if (status == MagickFalse)
3001 (void) MagickSetThreadValue(tiff_exception,exception);
3002 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
3003 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
3004 endian_type=UndefinedEndian;
3005 option=GetImageOption(image_info,"tiff:endian");
3006 if (option != (const char *) NULL)
3008 if (LocaleNCompare(option,"msb",3) == 0)
3009 endian_type=MSBEndian;
3010 if (LocaleNCompare(option,"lsb",3) == 0)
3011 endian_type=LSBEndian;;
3013 switch (endian_type)
3015 case LSBEndian: mode="wl"; break;
3016 case MSBEndian: mode="wb"; break;
3017 default: mode="w"; break;
3019 #if defined(TIFF_VERSION_BIG)
3020 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
3021 switch (endian_type)
3023 case LSBEndian: mode="wl8"; break;
3024 case MSBEndian: mode="wb8"; break;
3025 default: mode="w8"; break;
3028 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
3029 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
3031 if (tiff == (TIFF *) NULL)
3033 (void) TIFFSetWarningHandler(warning_handler);
3034 (void) TIFFSetErrorHandler(error_handler);
3035 return(MagickFalse);
3038 debug=IsEventLogging();
3043 Initialize TIFF fields.
3045 if ((image_info->type != UndefinedType) &&
3046 (image_info->type != OptimizeType))
3047 (void) SetImageType(image,image_info->type,exception);
3048 compression=UndefinedCompression;
3049 if (image->compression != JPEGCompression)
3050 compression=image->compression;
3051 if (image_info->compression != UndefinedCompression)
3052 compression=image_info->compression;
3053 switch (compression)
3055 case FaxCompression:
3056 case Group4Compression:
3058 (void) SetImageType(image,BilevelType,exception);
3061 case JPEGCompression:
3063 (void) SetImageStorageClass(image,DirectClass,exception);
3064 (void) SetImageDepth(image,8,exception);
3070 quantum_info=AcquireQuantumInfo(image_info,image);
3071 if (quantum_info == (QuantumInfo *) NULL)
3072 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3073 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
3074 (quantum_info->format == UndefinedQuantumFormat) &&
3075 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
3077 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
3078 if (status == MagickFalse)
3079 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3081 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
3082 (GetPreviousImageInList(image) != (Image *) NULL))
3083 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
3084 if ((image->columns != (uint32) image->columns) ||
3085 (image->rows != (uint32) image->rows))
3086 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
3087 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
3088 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
3089 switch (compression)
3091 case FaxCompression:
3093 compress_tag=COMPRESSION_CCITTFAX3;
3094 SetQuantumMinIsWhite(quantum_info,MagickTrue);
3097 case Group4Compression:
3099 compress_tag=COMPRESSION_CCITTFAX4;
3100 SetQuantumMinIsWhite(quantum_info,MagickTrue);
3103 #if defined(COMPRESSION_JBIG)
3104 case JBIG1Compression:
3106 compress_tag=COMPRESSION_JBIG;
3110 case JPEGCompression:
3112 compress_tag=COMPRESSION_JPEG;
3115 #if defined(COMPRESSION_LZMA)
3116 case LZMACompression:
3118 compress_tag=COMPRESSION_LZMA;
3122 case LZWCompression:
3124 compress_tag=COMPRESSION_LZW;
3127 case RLECompression:
3129 compress_tag=COMPRESSION_PACKBITS;
3132 case ZipCompression:
3134 compress_tag=COMPRESSION_ADOBE_DEFLATE;
3140 compress_tag=COMPRESSION_NONE;
3144 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
3145 if ((compress_tag != COMPRESSION_NONE) &&
3146 (TIFFIsCODECConfigured(compress_tag) == 0))
3148 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3149 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
3150 MagickCompressOptions,(ssize_t) compression));
3151 compress_tag=COMPRESSION_NONE;
3152 compression=NoCompression;
3155 switch (compress_tag)
3157 #if defined(CCITT_SUPPORT)
3158 case COMPRESSION_CCITTFAX3:
3159 case COMPRESSION_CCITTFAX4:
3161 #if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
3162 case COMPRESSION_JPEG:
3164 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3165 case COMPRESSION_LZMA:
3167 #if defined(LZW_SUPPORT)
3168 case COMPRESSION_LZW:
3170 #if defined(PACKBITS_SUPPORT)
3171 case COMPRESSION_PACKBITS:
3173 #if defined(ZIP_SUPPORT)
3174 case COMPRESSION_ADOBE_DEFLATE:
3176 case COMPRESSION_NONE:
3180 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3181 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
3182 MagickCompressOptions,(ssize_t) compression));
3183 compress_tag=COMPRESSION_NONE;
3184 compression=NoCompression;
3189 if (image->colorspace == CMYKColorspace)
3191 photometric=PHOTOMETRIC_SEPARATED;
3192 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
3193 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
3198 Full color TIFF raster.
3200 if (image->colorspace == LabColorspace)
3202 photometric=PHOTOMETRIC_CIELAB;
3203 EncodeLabImage(image,exception);
3206 if (image->colorspace == YCbCrColorspace)
3208 photometric=PHOTOMETRIC_YCBCR;
3209 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
3210 (void) SetImageStorageClass(image,DirectClass,exception);
3211 (void) SetImageDepth(image,8,exception);
3214 photometric=PHOTOMETRIC_RGB;
3215 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
3216 if ((image_info->type != TrueColorType) &&
3217 (image_info->type != TrueColorMatteType))
3219 if ((image_info->type != PaletteType) &&
3220 (IsImageGray(image,exception) != MagickFalse))
3222 photometric=(uint16) (quantum_info->min_is_white !=
3223 MagickFalse ? PHOTOMETRIC_MINISWHITE :
3224 PHOTOMETRIC_MINISBLACK);
3225 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
3226 if ((image_info->depth == 0) &&
3227 (image->alpha_trait == UndefinedPixelTrait) &&
3228 (IsImageMonochrome(image,exception) != MagickFalse))
3230 status=SetQuantumDepth(image,quantum_info,1);
3231 if (status == MagickFalse)
3232 ThrowWriterException(ResourceLimitError,
3233 "MemoryAllocationFailed");
3237 if (image->storage_class == PseudoClass)
3243 Colormapped TIFF raster.
3245 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
3246 photometric=PHOTOMETRIC_PALETTE;
3248 while ((GetQuantumRange(depth)+1) < image->colors)
3250 status=SetQuantumDepth(image,quantum_info,depth);
3251 if (status == MagickFalse)
3252 ThrowWriterException(ResourceLimitError,
3253 "MemoryAllocationFailed");
3257 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
3258 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
3259 (photometric != PHOTOMETRIC_MINISWHITE))
3261 compress_tag=COMPRESSION_NONE;
3262 endian=FILLORDER_MSB2LSB;
3265 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
3266 (photometric != PHOTOMETRIC_MINISWHITE))
3268 compress_tag=COMPRESSION_NONE;
3269 endian=FILLORDER_MSB2LSB;
3271 option=GetImageOption(image_info,"tiff:fill-order");
3272 if (option != (const char *) NULL)
3274 if (LocaleNCompare(option,"msb",3) == 0)
3275 endian=FILLORDER_MSB2LSB;
3276 if (LocaleNCompare(option,"lsb",3) == 0)
3277 endian=FILLORDER_LSB2MSB;
3279 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
3280 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
3281 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
3282 if (image->alpha_trait != UndefinedPixelTrait)
3290 TIFF has a matte channel.
3293 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
3294 option=GetImageOption(image_info,"tiff:alpha");
3295 if (option != (const char *) NULL)
3297 if (LocaleCompare(option,"associated") == 0)
3298 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
3300 if (LocaleCompare(option,"unspecified") == 0)
3301 sample_info[0]=EXTRASAMPLE_UNSPECIFIED;
3303 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
3304 &samples_per_pixel);
3305 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
3306 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
3308 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
3309 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
3311 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
3312 switch (quantum_info->format)
3314 case FloatingPointQuantumFormat:
3316 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
3317 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
3318 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
3321 case SignedQuantumFormat:
3323 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
3326 case UnsignedQuantumFormat:
3328 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
3334 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
3335 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
3336 if (photometric == PHOTOMETRIC_RGB)
3337 if ((image_info->interlace == PlaneInterlace) ||
3338 (image_info->interlace == PartitionInterlace))
3339 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
3340 rows_per_strip=TIFFDefaultStripSize(tiff,0);
3341 option=GetImageOption(image_info,"tiff:rows-per-strip");
3342 if (option != (const char *) NULL)
3343 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
3344 switch (compress_tag)
3346 case COMPRESSION_JPEG:
3348 #if defined(JPEG_SUPPORT)
3358 rows_per_strip+=(16-(rows_per_strip % 16));
3359 if (image_info->quality != UndefinedCompressionQuality)
3360 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
3361 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
3362 if (IssRGBCompatibleColorspace(image->colorspace) != MagickFalse)
3367 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
3368 sampling_factor=(const char *) NULL;
3369 value=GetImageProperty(image,"jpeg:sampling-factor",exception);
3370 if (value != (char *) NULL)
3372 sampling_factor=value;
3373 if (image->debug != MagickFalse)
3374 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3375 " Input sampling-factors=%s",sampling_factor);
3377 if (image_info->sampling_factor != (char *) NULL)
3378 sampling_factor=image_info->sampling_factor;
3379 if (sampling_factor != (const char *) NULL)
3381 flags=ParseGeometry(sampling_factor,&geometry_info);
3382 if ((flags & SigmaValue) == 0)
3383 geometry_info.sigma=geometry_info.rho;
3384 if (image->colorspace == YCbCrColorspace)
3385 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
3386 geometry_info.rho,(uint16) geometry_info.sigma);
3389 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3391 if (bits_per_sample == 12)
3392 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
3396 case COMPRESSION_ADOBE_DEFLATE:
3398 rows_per_strip=(uint32) image->rows;
3399 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3401 if (((photometric == PHOTOMETRIC_RGB) ||
3402 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3403 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3404 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3405 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
3406 image_info->quality == UndefinedCompressionQuality ? 7 :
3407 MagickMin((ssize_t) image_info->quality/10,9)));
3410 case COMPRESSION_CCITTFAX3:
3415 rows_per_strip=(uint32) image->rows;
3416 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
3419 case COMPRESSION_CCITTFAX4:
3421 rows_per_strip=(uint32) image->rows;
3424 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3425 case COMPRESSION_LZMA:
3427 if (((photometric == PHOTOMETRIC_RGB) ||
3428 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3429 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3430 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3431 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
3432 image_info->quality == UndefinedCompressionQuality ? 7 :
3433 MagickMin((ssize_t) image_info->quality/10,9)));
3437 case COMPRESSION_LZW:
3439 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3441 if (((photometric == PHOTOMETRIC_RGB) ||
3442 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3443 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3444 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3450 if (rows_per_strip < 1)
3452 if ((image->rows/rows_per_strip) >= (1UL << 15))
3453 rows_per_strip=(uint32) (image->rows >> 15);
3454 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
3455 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
3461 Set image resolution.
3464 if (image->units == PixelsPerInchResolution)
3466 if (image->units == PixelsPerCentimeterResolution)
3467 units=RESUNIT_CENTIMETER;
3468 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
3469 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x);
3470 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y);
3471 if ((image->page.x < 0) || (image->page.y < 0))
3472 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3473 "TIFF: negative image positions unsupported","%s",image->filename);
3474 if ((image->page.x > 0) && (image->resolution.x > 0.0))
3477 Set horizontal image position.
3479 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
3480 image->resolution.x);
3482 if ((image->page.y > 0) && (image->resolution.y > 0.0))
3485 Set vertical image position.
3487 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
3488 image->resolution.y);
3491 if (image->chromaticity.white_point.x != 0.0)
3497 Set image chromaticity.
3499 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3500 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3501 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3502 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3503 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3504 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3505 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3506 chromaticity[0]=(float) image->chromaticity.white_point.x;
3507 chromaticity[1]=(float) image->chromaticity.white_point.y;
3508 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3510 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3511 (image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3513 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3514 if (image->scene != 0)
3515 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3516 GetImageListLength(image));
3518 if (image->orientation != UndefinedOrientation)
3519 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3520 (void) TIFFSetProfiles(tiff,image);
3526 page=(uint16) scene;
3527 pages=(uint16) GetImageListLength(image);
3528 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3529 (image_info->adjoin != MagickFalse) && (pages > 1))
3530 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3531 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3533 (void) TIFFSetProperties(tiff,image,exception);
3534 DisableMSCWarning(4127)
3537 (void) TIFFSetEXIFProperties(tiff,image,exception);
3539 Write image scanlines.
3541 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3542 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3543 quantum_info->endian=LSBEndian;
3544 pixels=GetQuantumPixels(quantum_info);
3545 tiff_info.scanline=GetQuantumPixels(quantum_info);
3546 switch (photometric)
3548 case PHOTOMETRIC_CIELAB:
3549 case PHOTOMETRIC_YCBCR:
3550 case PHOTOMETRIC_RGB:
3555 switch (image_info->interlace)
3560 quantum_type=RGBQuantum;
3561 if (image->alpha_trait != UndefinedPixelTrait)
3562 quantum_type=RGBAQuantum;
3563 for (y=0; y < (ssize_t) image->rows; y++)
3565 register const Quantum
3568 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3569 if (p == (const Quantum *) NULL)
3571 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3572 quantum_type,pixels,exception);
3574 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3576 if (image->previous == (Image *) NULL)
3578 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3580 if (status == MagickFalse)
3586 case PlaneInterlace:
3587 case PartitionInterlace:
3590 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3592 for (y=0; y < (ssize_t) image->rows; y++)
3594 register const Quantum
3597 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3598 if (p == (const Quantum *) NULL)
3600 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3601 RedQuantum,pixels,exception);
3602 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3605 if (image->previous == (Image *) NULL)
3607 status=SetImageProgress(image,SaveImageTag,100,400);
3608 if (status == MagickFalse)
3611 for (y=0; y < (ssize_t) image->rows; y++)
3613 register const Quantum
3616 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3617 if (p == (const Quantum *) NULL)
3619 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3620 GreenQuantum,pixels,exception);
3621 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3624 if (image->previous == (Image *) NULL)
3626 status=SetImageProgress(image,SaveImageTag,200,400);
3627 if (status == MagickFalse)
3630 for (y=0; y < (ssize_t) image->rows; y++)
3632 register const Quantum
3635 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3636 if (p == (const Quantum *) NULL)
3638 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3639 BlueQuantum,pixels,exception);
3640 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3643 if (image->previous == (Image *) NULL)
3645 status=SetImageProgress(image,SaveImageTag,300,400);
3646 if (status == MagickFalse)
3649 if (image->alpha_trait != UndefinedPixelTrait)
3650 for (y=0; y < (ssize_t) image->rows; y++)
3652 register const Quantum
3655 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3656 if (p == (const Quantum *) NULL)
3658 length=ExportQuantumPixels(image,(CacheView *) NULL,
3659 quantum_info,AlphaQuantum,pixels,exception);
3660 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3663 if (image->previous == (Image *) NULL)
3665 status=SetImageProgress(image,SaveImageTag,400,400);
3666 if (status == MagickFalse)
3674 case PHOTOMETRIC_SEPARATED:
3679 quantum_type=CMYKQuantum;
3680 if (image->alpha_trait != UndefinedPixelTrait)
3681 quantum_type=CMYKAQuantum;
3682 if (image->colorspace != CMYKColorspace)
3683 (void) TransformImageColorspace(image,CMYKColorspace,exception);
3684 for (y=0; y < (ssize_t) image->rows; y++)
3686 register const Quantum
3689 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3690 if (p == (const Quantum *) NULL)
3692 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3693 quantum_type,pixels,exception);
3694 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3696 if (image->previous == (Image *) NULL)
3698 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3700 if (status == MagickFalse)
3706 case PHOTOMETRIC_PALETTE:
3714 Colormapped TIFF image.
3716 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3717 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3718 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3719 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3720 (blue == (uint16 *) NULL))
3721 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3723 Initialize TIFF colormap.
3725 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3726 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3727 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3728 for (i=0; i < (ssize_t) image->colors; i++)
3730 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3731 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3732 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3734 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3735 red=(uint16 *) RelinquishMagickMemory(red);
3736 green=(uint16 *) RelinquishMagickMemory(green);
3737 blue=(uint16 *) RelinquishMagickMemory(blue);
3742 Convert PseudoClass packets to contiguous grayscale scanlines.
3744 quantum_type=IndexQuantum;
3745 if (image->alpha_trait != UndefinedPixelTrait)
3747 if (photometric != PHOTOMETRIC_PALETTE)
3748 quantum_type=GrayAlphaQuantum;
3750 quantum_type=IndexAlphaQuantum;
3753 if (photometric != PHOTOMETRIC_PALETTE)
3754 quantum_type=GrayQuantum;
3755 for (y=0; y < (ssize_t) image->rows; y++)
3757 register const Quantum
3760 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3761 if (p == (const Quantum *) NULL)
3763 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3764 quantum_type,pixels,exception);
3765 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3767 if (image->previous == (Image *) NULL)
3769 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3771 if (status == MagickFalse)
3778 quantum_info=DestroyQuantumInfo(quantum_info);
3779 if (image->colorspace == LabColorspace)
3780 DecodeLabImage(image,exception);
3781 DestroyTIFFInfo(&tiff_info);
3782 DisableMSCWarning(4127)
3783 if (0 && (image_info->verbose != MagickFalse))
3785 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3786 (void) TIFFWriteDirectory(tiff);
3787 image=SyncNextImageInList(image);
3788 if (image == (Image *) NULL)
3790 status=SetImageProgress(image,SaveImagesTag,scene++,
3791 GetImageListLength(image));
3792 if (status == MagickFalse)
3794 } while (image_info->adjoin != MagickFalse);
3795 (void) TIFFSetWarningHandler(warning_handler);
3796 (void) TIFFSetErrorHandler(error_handler);