2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % TTTTT IIIII FFFFF FFFFF %
13 % Read/Write TIFF Image Format %
20 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40 #define JPEG_SUPPORT 1
46 #include "MagickCore/studio.h"
47 #include "MagickCore/artifact.h"
48 #include "MagickCore/attribute.h"
49 #include "MagickCore/blob.h"
50 #include "MagickCore/blob-private.h"
51 #include "MagickCore/cache.h"
52 #include "MagickCore/color.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colormap.h"
55 #include "MagickCore/colorspace.h"
56 #include "MagickCore/colorspace-private.h"
57 #include "MagickCore/constitute.h"
58 #include "MagickCore/enhance.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/geometry.h"
62 #include "MagickCore/image.h"
63 #include "MagickCore/image-private.h"
64 #include "MagickCore/list.h"
65 #include "MagickCore/log.h"
66 #include "MagickCore/magick.h"
67 #include "MagickCore/memory_.h"
68 #include "MagickCore/memory-private.h"
69 #include "MagickCore/module.h"
70 #include "MagickCore/monitor.h"
71 #include "MagickCore/monitor-private.h"
72 #include "MagickCore/option.h"
73 #include "MagickCore/pixel-accessor.h"
74 #include "MagickCore/pixel-private.h"
75 #include "MagickCore/property.h"
76 #include "MagickCore/quantum.h"
77 #include "MagickCore/quantum-private.h"
78 #include "MagickCore/profile.h"
79 #include "MagickCore/resize.h"
80 #include "MagickCore/resource_.h"
81 #include "MagickCore/semaphore.h"
82 #include "MagickCore/splay-tree.h"
83 #include "MagickCore/static.h"
84 #include "MagickCore/statistic.h"
85 #include "MagickCore/string_.h"
86 #include "MagickCore/string-private.h"
87 #include "MagickCore/thread_.h"
88 #include "MagickCore/token.h"
89 #include "MagickCore/utility.h"
90 #if defined(MAGICKCORE_TIFF_DELEGATE)
91 # if defined(MAGICKCORE_HAVE_TIFFCONF_H)
92 # include "tiffconf.h"
96 # if !defined(COMPRESSION_ADOBE_DEFLATE)
97 # define COMPRESSION_ADOBE_DEFLATE 8
99 # if !defined(PREDICTOR_HORIZONTAL)
100 # define PREDICTOR_HORIZONTAL 2
102 # if !defined(TIFFTAG_COPYRIGHT)
103 # define TIFFTAG_COPYRIGHT 33432
105 # if !defined(TIFFTAG_OPIIMAGEID)
106 # define TIFFTAG_OPIIMAGEID 32781
108 #include "psd-private.h"
111 Typedef declarations.
115 ReadSingleSampleMethod,
124 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
125 typedef struct _ExifInfo
136 static const ExifInfo
138 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, 0, "exif:ExposureTime" },
139 { EXIFTAG_FNUMBER, TIFF_RATIONAL, 0, "exif:FNumber" },
140 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, 0, "exif:ExposureProgram" },
141 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, 0, "exif:SpectralSensitivity" },
142 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, 1, "exif:ISOSpeedRatings" },
143 { EXIFTAG_OECF, TIFF_NOTYPE, 0, "exif:OptoelectricConversionFactor" },
144 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, 0, "exif:ExifVersion" },
145 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, 0, "exif:DateTimeOriginal" },
146 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, 0, "exif:DateTimeDigitized" },
147 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, 0, "exif:ComponentsConfiguration" },
148 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, 0, "exif:CompressedBitsPerPixel" },
149 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, 0, "exif:ShutterSpeedValue" },
150 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, 0, "exif:ApertureValue" },
151 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, 0, "exif:BrightnessValue" },
152 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, 0, "exif:ExposureBiasValue" },
153 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, 0, "exif:MaxApertureValue" },
154 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, 0, "exif:SubjectDistance" },
155 { EXIFTAG_METERINGMODE, TIFF_SHORT, 0, "exif:MeteringMode" },
156 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, 0, "exif:LightSource" },
157 { EXIFTAG_FLASH, TIFF_SHORT, 0, "exif:Flash" },
158 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, 0, "exif:FocalLength" },
159 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, 0, "exif:SubjectArea" },
160 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, 0, "exif:MakerNote" },
161 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, 0, "exif:UserComment" },
162 { EXIFTAG_SUBSECTIME, TIFF_ASCII, 0, "exif:SubSecTime" },
163 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, 0, "exif:SubSecTimeOriginal" },
164 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, 0, "exif:SubSecTimeDigitized" },
165 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, 0, "exif:FlashpixVersion" },
166 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, 0, "exif:PixelXDimension" },
167 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, 0, "exif:PixelYDimension" },
168 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, 0, "exif:RelatedSoundFile" },
169 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, 0, "exif:FlashEnergy" },
170 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, 0, "exif:SpatialFrequencyResponse" },
171 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, 0, "exif:FocalPlaneXResolution" },
172 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, 0, "exif:FocalPlaneYResolution" },
173 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, 0, "exif:FocalPlaneResolutionUnit" },
174 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, 0, "exif:SubjectLocation" },
175 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, 0, "exif:ExposureIndex" },
176 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, 0, "exif:SensingMethod" },
177 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, 0, "exif:FileSource" },
178 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, 0, "exif:SceneType" },
179 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, 0, "exif:CFAPattern" },
180 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, 0, "exif:CustomRendered" },
181 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, 0, "exif:ExposureMode" },
182 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, 0, "exif:WhiteBalance" },
183 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, 0, "exif:DigitalZoomRatio" },
184 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, 0, "exif:FocalLengthIn35mmFilm" },
185 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, 0, "exif:SceneCaptureType" },
186 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, 0, "exif:GainControl" },
187 { EXIFTAG_CONTRAST, TIFF_SHORT, 0, "exif:Contrast" },
188 { EXIFTAG_SATURATION, TIFF_SHORT, 0, "exif:Saturation" },
189 { EXIFTAG_SHARPNESS, TIFF_SHORT, 0, "exif:Sharpness" },
190 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, 0, "exif:DeviceSettingDescription" },
191 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, 0, "exif:SubjectDistanceRange" },
192 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, 0, "exif:ImageUniqueID" },
193 { 0, 0, 0, (char *) NULL }
196 #endif /* MAGICKCORE_TIFF_DELEGATE */
201 static MagickThreadKey
205 *tiff_semaphore = (SemaphoreInfo *) NULL;
207 static TIFFErrorHandler
211 static volatile MagickBooleanType
212 instantiate_key = MagickFalse;
215 Forward declarations.
217 #if defined(MAGICKCORE_TIFF_DELEGATE)
219 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
221 static MagickBooleanType
222 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
223 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
224 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 % IsTIFF() returns MagickTrue if the image format type, identified by the
239 % magick string, is TIFF.
241 % The format of the IsTIFF method is:
243 % MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
245 % A description of each parameter follows:
247 % o magick: compare image format pattern against these bytes.
249 % o length: Specifies the length of the magick string.
252 static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
256 if (memcmp(magick,"\115\115\000\052",4) == 0)
258 if (memcmp(magick,"\111\111\052\000",4) == 0)
260 #if defined(TIFF_VERSION_BIG)
263 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
265 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
271 #if defined(MAGICKCORE_TIFF_DELEGATE)
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % R e a d G R O U P 4 I m a g e %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
284 % allocates the memory necessary for the new Image structure and returns a
285 % pointer to the new image.
287 % The format of the ReadGROUP4Image method is:
289 % Image *ReadGROUP4Image(const ImageInfo *image_info,
290 % ExceptionInfo *exception)
292 % A description of each parameter follows:
294 % o image_info: the image info.
296 % o exception: return any errors or warnings in this structure.
300 static inline size_t WriteLSBLong(FILE *file,const size_t value)
305 buffer[0]=(unsigned char) value;
306 buffer[1]=(unsigned char) (value >> 8);
307 buffer[2]=(unsigned char) (value >> 16);
308 buffer[3]=(unsigned char) (value >> 24);
309 return(fwrite(buffer,1,4,file));
312 static Image *ReadGROUP4Image(const ImageInfo *image_info,
313 ExceptionInfo *exception)
316 filename[MagickPathExtent];
344 assert(image_info != (const ImageInfo *) NULL);
345 assert(image_info->signature == MagickCoreSignature);
346 if (image_info->debug != MagickFalse)
347 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
348 image_info->filename);
349 assert(exception != (ExceptionInfo *) NULL);
350 assert(exception->signature == MagickCoreSignature);
351 image=AcquireImage(image_info,exception);
352 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
353 if (status == MagickFalse)
355 image=DestroyImageList(image);
356 return((Image *) NULL);
359 Write raw CCITT Group 4 wrapped as a TIFF image file.
362 unique_file=AcquireUniqueFileResource(filename);
363 if (unique_file != -1)
364 file=fdopen(unique_file,"wb");
365 if ((unique_file == -1) || (file == (FILE *) NULL))
366 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
367 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
369 ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
370 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
371 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
372 length=WriteLSBLong(file,image->columns);
373 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
374 length=WriteLSBLong(file,image->rows);
375 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
376 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
377 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
378 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
379 strip_offset=10+(12*14)+4+8;
380 length=WriteLSBLong(file,(size_t) strip_offset);
381 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
382 length=WriteLSBLong(file,(size_t) image_info->orientation);
383 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
384 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
385 length=WriteLSBLong(file,image->rows);
386 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
387 offset=(ssize_t) ftell(file)-4;
388 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
389 length=WriteLSBLong(file,(size_t) (strip_offset-8));
390 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
391 length=WriteLSBLong(file,(size_t) (strip_offset-8));
392 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
393 length=fwrite("\000\000\000\000",1,4,file);
394 length=WriteLSBLong(file,(long) image->resolution.x);
395 length=WriteLSBLong(file,1);
397 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
398 if (fputc(c,file) != c)
400 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
401 length=WriteLSBLong(file,(unsigned int) length);
402 if (ferror(file) != 0)
403 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
405 (void) CloseBlob(image);
406 image=DestroyImage(image);
410 read_info=CloneImageInfo((ImageInfo *) NULL);
411 (void) FormatLocaleString(read_info->filename,MagickPathExtent,"%s",filename);
412 image=ReadTIFFImage(read_info,exception);
413 read_info=DestroyImageInfo(read_info);
414 if (image != (Image *) NULL)
416 (void) CopyMagickString(image->filename,image_info->filename,
418 (void) CopyMagickString(image->magick_filename,image_info->filename,
420 (void) CopyMagickString(image->magick,"GROUP4",MagickPathExtent);
422 (void) RelinquishUniqueFileResource(filename);
423 if (status == MagickFalse)
424 image=DestroyImage(image);
429 #if defined(MAGICKCORE_TIFF_DELEGATE)
431 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
435 % R e a d T I F F I m a g e %
439 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
441 % ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
442 % memory necessary for the new Image structure and returns a pointer to the
445 % The format of the ReadTIFFImage method is:
447 % Image *ReadTIFFImage(const ImageInfo *image_info,
448 % ExceptionInfo *exception)
450 % A description of each parameter follows:
452 % o image_info: the image info.
454 % o exception: return any errors or warnings in this structure.
458 static inline unsigned char ClampYCC(double value)
462 return((unsigned char)0);
464 return((unsigned char)255);
465 return((unsigned char)(value));
468 static MagickBooleanType DecodeLabImage(Image *image,ExceptionInfo *exception)
480 image_view=AcquireAuthenticCacheView(image,exception);
481 for (y=0; y < (ssize_t) image->rows; y++)
489 if (status == MagickFalse)
491 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
492 if (q == (Quantum *) NULL)
497 for (x=0; x < (ssize_t) image->columns; x++)
503 a=QuantumScale*GetPixela(image,q)+0.5;
506 b=QuantumScale*GetPixelb(image,q)+0.5;
509 SetPixela(image,QuantumRange*a,q);
510 SetPixelb(image,QuantumRange*b,q);
511 q+=GetPixelChannels(image);
513 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
516 image_view=DestroyCacheView(image_view);
520 static MagickBooleanType ReadProfile(Image *image,const char *name,
521 const unsigned char *datum,ssize_t length,ExceptionInfo *exception)
531 profile=BlobToStringInfo(datum,(size_t) length);
532 if (profile == (StringInfo *) NULL)
533 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
535 status=SetImageProfile(image,name,profile,exception);
536 profile=DestroyStringInfo(profile);
537 if (status == MagickFalse)
538 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
543 #if defined(__cplusplus) || defined(c_plusplus)
547 static int TIFFCloseBlob(thandle_t image)
549 (void) CloseBlob((Image *) image);
553 static void TIFFErrors(const char *module,const char *format,va_list error)
556 message[MagickPathExtent];
561 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
562 (void) vsnprintf(message,MagickPathExtent,format,error);
564 (void) vsprintf(message,format,error);
566 (void) ConcatenateMagickString(message,".",MagickPathExtent);
567 exception=(ExceptionInfo *) GetMagickThreadValue(tiff_exception);
568 if (exception != (ExceptionInfo *) NULL)
569 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
573 static toff_t TIFFGetBlobSize(thandle_t image)
575 return((toff_t) GetBlobSize((Image *) image));
578 static void TIFFGetProfiles(TIFF *tiff,Image *image,MagickBooleanType ping,
579 ExceptionInfo *exception)
588 if (ping == MagickFalse)
590 #if defined(TIFFTAG_ICCPROFILE)
591 if ((TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1) &&
592 (profile != (unsigned char *) NULL))
593 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
595 #if defined(TIFFTAG_PHOTOSHOP)
596 if ((TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1) &&
597 (profile != (unsigned char *) NULL))
598 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
600 #if defined(TIFFTAG_RICHTIFFIPTC)
601 if ((TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1) &&
602 (profile != (unsigned char *) NULL))
604 if (TIFFIsByteSwapped(tiff) != 0)
605 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
606 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
609 #if defined(TIFFTAG_XMLPACKET)
610 if ((TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1) &&
611 (profile != (unsigned char *) NULL))
612 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
614 if ((TIFFGetField(tiff,34118,&length,&profile) == 1) &&
615 (profile != (unsigned char *) NULL))
616 (void) ReadProfile(image,"tiff:34118",profile,(ssize_t) length,
619 if ((TIFFGetField(tiff,37724,&length,&profile) == 1) &&
620 (profile != (unsigned char *) NULL))
621 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
624 static void TIFFGetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
627 message[MagickPathExtent],
638 if ((TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1) &&
639 (text != (char *) NULL))
640 (void) SetImageProperty(image,"tiff:artist",text,exception);
641 if ((TIFFGetField(tiff,TIFFTAG_COPYRIGHT,&text) == 1) &&
642 (text != (char *) NULL))
643 (void) SetImageProperty(image,"tiff:copyright",text,exception);
644 if ((TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1) &&
645 (text != (char *) NULL))
646 (void) SetImageProperty(image,"tiff:timestamp",text,exception);
647 if ((TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1) &&
648 (text != (char *) NULL))
649 (void) SetImageProperty(image,"tiff:document",text,exception);
650 if ((TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1) &&
651 (text != (char *) NULL))
652 (void) SetImageProperty(image,"tiff:hostcomputer",text,exception);
653 if ((TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1) &&
654 (text != (char *) NULL))
655 (void) SetImageProperty(image,"comment",text,exception);
656 if ((TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1) &&
657 (text != (char *) NULL))
658 (void) SetImageProperty(image,"tiff:make",text,exception);
659 if ((TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1) &&
660 (text != (char *) NULL))
661 (void) SetImageProperty(image,"tiff:model",text,exception);
662 if ((TIFFGetField(tiff,TIFFTAG_OPIIMAGEID,&count,&text) == 1) &&
663 (text != (char *) NULL))
665 if (count >= MagickPathExtent)
666 count=MagickPathExtent-1;
667 (void) CopyMagickString(message,text,count+1);
668 (void) SetImageProperty(image,"tiff:image-id",message,exception);
670 if ((TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1) &&
671 (text != (char *) NULL))
672 (void) SetImageProperty(image,"label",text,exception);
673 if ((TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1) &&
674 (text != (char *) NULL))
675 (void) SetImageProperty(image,"tiff:software",text,exception);
676 if ((TIFFGetField(tiff,33423,&count,&text) == 1) &&
677 (text != (char *) NULL))
679 if (count >= MagickPathExtent)
680 count=MagickPathExtent-1;
681 (void) CopyMagickString(message,text,count+1);
682 (void) SetImageProperty(image,"tiff:kodak-33423",message,exception);
684 if ((TIFFGetField(tiff,36867,&count,&text) == 1) &&
685 (text != (char *) NULL))
687 if (count >= MagickPathExtent)
688 count=MagickPathExtent-1;
689 (void) CopyMagickString(message,text,count+1);
690 (void) SetImageProperty(image,"tiff:kodak-36867",message,exception);
692 if (TIFFGetField(tiff,TIFFTAG_SUBFILETYPE,&type) == 1)
697 (void) SetImageProperty(image,"tiff:subfiletype","REDUCEDIMAGE",
703 (void) SetImageProperty(image,"tiff:subfiletype","PAGE",exception);
708 (void) SetImageProperty(image,"tiff:subfiletype","MASK",exception);
714 if ((TIFFGetField(tiff,37706,&length,&tietz) == 1) &&
715 (tietz != (unsigned long *) NULL))
717 (void) FormatLocaleString(message,MagickPathExtent,"%lu",tietz[0]);
718 (void) SetImageProperty(image,"tiff:tietz_offset",message,exception);
722 static void TIFFGetEXIFProperties(TIFF *tiff,Image *image,
723 ExceptionInfo *exception)
725 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
727 value[MagickPathExtent];
735 #if defined(TIFF_VERSION_BIG)
746 Read EXIF properties.
749 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) != 1)
751 directory=TIFFCurrentDirectory(tiff);
752 if (TIFFReadEXIFDirectory(tiff,offset) != 1)
754 TIFFSetDirectory(tiff,directory);
758 for (i=0; exif_info[i].tag != 0; i++)
761 switch (exif_info[i].type)
769 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans,&sans) == 1) &&
770 (ascii != (char *) NULL) && (*ascii != '\0'))
771 (void) CopyMagickString(value,ascii,MagickPathExtent);
776 if (exif_info[i].variable_length == 0)
782 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans,&sans) == 1)
783 (void) FormatLocaleString(value,MagickPathExtent,"%d",shorty);
796 tiff_status=TIFFGetField(tiff,exif_info[i].tag,&shorty_num,&shorty,
798 if (tiff_status == 1)
799 (void) FormatLocaleString(value,MagickPathExtent,"%d",
800 shorty_num != 0 ? shorty[0] : 0);
810 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans,&sans) == 1)
811 (void) FormatLocaleString(value,MagickPathExtent,"%d",longy);
814 #if defined(TIFF_VERSION_BIG)
821 if (TIFFGetField(tiff,exif_info[i].tag,&long8y,&sans,&sans) == 1)
822 (void) FormatLocaleString(value,MagickPathExtent,"%.20g",(double)
823 ((MagickOffsetType) long8y));
835 if (TIFFGetField(tiff,exif_info[i].tag,&floaty,&sans,&sans) == 1)
836 (void) FormatLocaleString(value,MagickPathExtent,"%g",(double)
846 if (TIFFGetField(tiff,exif_info[i].tag,&doubley,&sans,&sans) == 1)
847 (void) FormatLocaleString(value,MagickPathExtent,"%g",doubley);
854 (void) SetImageProperty(image,exif_info[i].property,value,exception);
856 TIFFSetDirectory(tiff,directory);
863 static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
865 *base=(tdata_t *) GetBlobStreamData((Image *) image);
866 if (*base != (tdata_t *) NULL)
867 *size=(toff_t) GetBlobSize((Image *) image);
868 if (*base != (tdata_t *) NULL)
873 static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
878 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
879 (unsigned char *) data);
883 static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
884 tsample_t sample,ssize_t row,tdata_t scanline)
889 (void) bits_per_sample;
890 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
894 static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
896 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
899 static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
906 static void TIFFWarnings(const char *module,const char *format,va_list warning)
909 message[MagickPathExtent];
914 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
915 (void) vsnprintf(message,MagickPathExtent,format,warning);
917 (void) vsprintf(message,format,warning);
919 (void) ConcatenateMagickString(message,".",MagickPathExtent);
920 exception=(ExceptionInfo *) GetMagickThreadValue(tiff_exception);
921 if (exception != (ExceptionInfo *) NULL)
922 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
923 message,"`%s'",module);
926 static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
931 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
932 (unsigned char *) data);
936 static TIFFMethodType GetJPEGMethod(Image* image,TIFF *tiff,uint16 photometric,
937 uint16 bits_per_sample,uint16 samples_per_pixel)
939 #define BUFFER_SIZE 2048
951 #if defined(TIFF_VERSION_BIG)
959 buffer[BUFFER_SIZE+32];
964 /* only support 8 bit for now */
965 if ((photometric != PHOTOMETRIC_SEPARATED) || (bits_per_sample != 8) ||
966 (samples_per_pixel != 4))
967 return(ReadGenericMethod);
968 /* Search for Adobe APP14 JPEG Marker */
969 if (!TIFFGetField(tiff,TIFFTAG_STRIPOFFSETS,&value))
970 return(ReadRGBAMethod);
971 position=TellBlob(image);
972 offset=(MagickOffsetType) (value[0]);
973 if (SeekBlob(image,offset,SEEK_SET) != offset)
974 return(ReadRGBAMethod);
975 method=ReadRGBAMethod;
976 if (ReadBlob(image,BUFFER_SIZE,buffer) == BUFFER_SIZE)
978 for (i=0; i < BUFFER_SIZE; i++)
980 while (i < BUFFER_SIZE)
982 if (buffer[i++] == 255)
985 while (i < BUFFER_SIZE)
987 if (buffer[++i] != 255)
990 if (buffer[i++] == 216) /* JPEG_MARKER_SOI */
992 length=(unsigned short) (((unsigned int) (buffer[i] << 8) |
993 (unsigned int) buffer[i+1]) & 0xffff);
994 if (i+(size_t) length >= BUFFER_SIZE)
996 if (buffer[i-1] == 238) /* JPEG_MARKER_APP0+14 */
1000 /* 0 == CMYK, 1 == YCbCr, 2 = YCCK */
1001 if (buffer[i+13] == 2)
1002 method=ReadYCCKMethod;
1008 (void) SeekBlob(image,position,SEEK_SET);
1012 static void TIFFReadPhotoshopLayers(Image* image,const ImageInfo *image_info,
1013 ExceptionInfo *exception)
1030 if (GetImageListLength(image) != 1)
1032 if ((image_info->number_scenes == 1) && (image_info->scene == 0))
1034 option=GetImageOption(image_info,"tiff:ignore-layers");
1035 if (option != (const char * ) NULL)
1037 layer_info=GetImageProfile(image,"tiff:37724");
1038 if (layer_info == (const StringInfo *) NULL)
1040 for (i=0; i < (ssize_t) layer_info->length-8; i++)
1042 if (LocaleNCompare((const char *) (layer_info->datum+i),
1043 image->endian == MSBEndian ? "8BIM" : "MIB8",4) != 0)
1046 if ((LocaleNCompare((const char *) (layer_info->datum+i),
1047 image->endian == MSBEndian ? "Layr" : "ryaL",4) == 0) ||
1048 (LocaleNCompare((const char *) (layer_info->datum+i),
1049 image->endian == MSBEndian ? "LMsk" : "ksML",4) == 0) ||
1050 (LocaleNCompare((const char *) (layer_info->datum+i),
1051 image->endian == MSBEndian ? "Lr16" : "61rL",4) == 0) ||
1052 (LocaleNCompare((const char *) (layer_info->datum+i),
1053 image->endian == MSBEndian ? "Lr32" : "23rL",4) == 0))
1057 if (i >= (ssize_t) (layer_info->length-8))
1059 layers=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
1060 (void) DeleteImageProfile(layers,"tiff:37724");
1061 AttachBlob(layers->blob,layer_info->datum,layer_info->length);
1062 SeekBlob(layers,(MagickOffsetType) i,SEEK_SET);
1064 info.columns=layers->columns;
1065 info.rows=layers->rows;
1066 info.channels=(unsigned short) layers->number_channels;
1067 /* Setting the mode to a value that won't change the colorspace */
1069 ReadPSDLayers(layers,image_info,&info,MagickFalse,exception);
1070 DeleteImageFromList(&layers);
1071 if (layers != (Image *) NULL)
1073 SetImageArtifact(image,"tiff:has-layers","true");
1074 AppendImageToList(&image,layers);
1075 while (layers != (Image *) NULL)
1077 SetImageArtifact(layers,"tiff:has-layers","true");
1078 DetachBlob(layers->blob);
1079 layers=GetNextImageInList(layers);
1084 #if defined(__cplusplus) || defined(c_plusplus)
1088 static Image *ReadTIFFImage(const ImageInfo *image_info,
1089 ExceptionInfo *exception)
1162 assert(image_info != (const ImageInfo *) NULL);
1163 assert(image_info->signature == MagickCoreSignature);
1164 if (image_info->debug != MagickFalse)
1165 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1166 image_info->filename);
1167 assert(exception != (ExceptionInfo *) NULL);
1168 assert(exception->signature == MagickCoreSignature);
1169 image=AcquireImage(image_info,exception);
1170 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1171 if (status == MagickFalse)
1173 image=DestroyImageList(image);
1174 return((Image *) NULL);
1176 (void) SetMagickThreadValue(tiff_exception,exception);
1177 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
1178 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
1180 if (tiff == (TIFF *) NULL)
1182 image=DestroyImageList(image);
1183 return((Image *) NULL);
1185 if (image_info->number_scenes != 0)
1188 Generate blank images for subimage specification (e.g. image.tif[4].
1189 We need to check the number of directores because it is possible that
1190 the subimage(s) are stored in the photoshop profile.
1192 if (image_info->scene < (size_t) TIFFNumberOfDirectories(tiff))
1194 for (i=0; i < (ssize_t) image_info->scene; i++)
1196 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1197 if (status == MagickFalse)
1200 image=DestroyImageList(image);
1201 return((Image *) NULL);
1203 AcquireNextImage(image_info,image,exception);
1204 if (GetNextImageInList(image) == (Image *) NULL)
1207 image=DestroyImageList(image);
1208 return((Image *) NULL);
1210 image=SyncNextImageInList(image);
1216 DisableMSCWarning(4127)
1217 if (0 && (image_info->verbose != MagickFalse))
1218 TIFFPrintDirectory(tiff,stdout,MagickFalse);
1220 if ((TIFFGetField(tiff,TIFFTAG_IMAGEWIDTH,&width) != 1) ||
1221 (TIFFGetField(tiff,TIFFTAG_IMAGELENGTH,&height) != 1) ||
1222 (TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag) != 1) ||
1223 (TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian) != 1) ||
1224 (TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace) != 1) ||
1225 (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,&samples_per_pixel) != 1) ||
1226 (TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample) != 1) ||
1227 (TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format) != 1) ||
1228 (TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value) != 1) ||
1229 (TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value) != 1) ||
1230 (TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric) != 1))
1233 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1235 if (sample_format == SAMPLEFORMAT_IEEEFP)
1236 (void) SetImageProperty(image,"quantum:format","floating-point",
1238 switch (photometric)
1240 case PHOTOMETRIC_MINISBLACK:
1242 (void) SetImageProperty(image,"tiff:photometric","min-is-black",
1246 case PHOTOMETRIC_MINISWHITE:
1248 (void) SetImageProperty(image,"tiff:photometric","min-is-white",
1252 case PHOTOMETRIC_PALETTE:
1254 (void) SetImageProperty(image,"tiff:photometric","palette",exception);
1257 case PHOTOMETRIC_RGB:
1259 (void) SetImageProperty(image,"tiff:photometric","RGB",exception);
1262 case PHOTOMETRIC_CIELAB:
1264 (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception);
1267 case PHOTOMETRIC_LOGL:
1269 (void) SetImageProperty(image,"tiff:photometric","CIE Log2(L)",
1273 case PHOTOMETRIC_LOGLUV:
1275 (void) SetImageProperty(image,"tiff:photometric","LOGLUV",exception);
1278 #if defined(PHOTOMETRIC_MASK)
1279 case PHOTOMETRIC_MASK:
1281 (void) SetImageProperty(image,"tiff:photometric","MASK",exception);
1285 case PHOTOMETRIC_SEPARATED:
1287 (void) SetImageProperty(image,"tiff:photometric","separated",exception);
1290 case PHOTOMETRIC_YCBCR:
1292 (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception);
1297 (void) SetImageProperty(image,"tiff:photometric","unknown",exception);
1301 if (image->debug != MagickFalse)
1303 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
1304 (unsigned int) width,(unsigned int) height);
1305 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
1307 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1308 "Bits per sample: %u",bits_per_sample);
1309 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1310 "Min sample value: %u",min_sample_value);
1311 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1312 "Max sample value: %u",max_sample_value);
1313 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
1314 "interpretation: %s",GetImageProperty(image,"tiff:photometric",
1317 image->columns=(size_t) width;
1318 image->rows=(size_t) height;
1319 image->depth=(size_t) bits_per_sample;
1320 if (image->debug != MagickFalse)
1321 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
1322 (double) image->depth);
1323 image->endian=MSBEndian;
1324 if (endian == FILLORDER_LSB2MSB)
1325 image->endian=LSBEndian;
1326 #if defined(MAGICKCORE_HAVE_TIFFISBIGENDIAN)
1327 if (TIFFIsBigEndian(tiff) == 0)
1329 (void) SetImageProperty(image,"tiff:endian","lsb",exception);
1330 image->endian=LSBEndian;
1334 (void) SetImageProperty(image,"tiff:endian","msb",exception);
1335 image->endian=MSBEndian;
1338 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1339 (photometric == PHOTOMETRIC_MINISWHITE))
1340 SetImageColorspace(image,GRAYColorspace,exception);
1341 if (photometric == PHOTOMETRIC_SEPARATED)
1342 SetImageColorspace(image,CMYKColorspace,exception);
1343 if (photometric == PHOTOMETRIC_CIELAB)
1344 SetImageColorspace(image,LabColorspace,exception);
1345 TIFFGetProfiles(tiff,image,image_info->ping,exception);
1346 TIFFGetProperties(tiff,image,exception);
1347 option=GetImageOption(image_info,"tiff:exif-properties");
1348 if (IsStringFalse(option) == MagickFalse) /* enabled by default */
1349 TIFFGetEXIFProperties(tiff,image,exception);
1350 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
1351 &samples_per_pixel);
1352 if ((TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution) == 1) &&
1353 (TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution) == 1))
1355 image->resolution.x=x_resolution;
1356 image->resolution.y=y_resolution;
1358 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units) == 1)
1360 if (units == RESUNIT_INCH)
1361 image->units=PixelsPerInchResolution;
1362 if (units == RESUNIT_CENTIMETER)
1363 image->units=PixelsPerCentimeterResolution;
1365 if ((TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position) == 1) &&
1366 (TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position) == 1))
1368 image->page.x=(ssize_t) ceil(x_position*image->resolution.x-0.5);
1369 image->page.y=(ssize_t) ceil(y_position*image->resolution.y-0.5);
1371 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation) == 1)
1372 image->orientation=(OrientationType) orientation;
1373 if (TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity) == 1)
1375 if (chromaticity != (float *) NULL)
1377 image->chromaticity.white_point.x=chromaticity[0];
1378 image->chromaticity.white_point.y=chromaticity[1];
1381 if (TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity) == 1)
1383 if (chromaticity != (float *) NULL)
1385 image->chromaticity.red_primary.x=chromaticity[0];
1386 image->chromaticity.red_primary.y=chromaticity[1];
1387 image->chromaticity.green_primary.x=chromaticity[2];
1388 image->chromaticity.green_primary.y=chromaticity[3];
1389 image->chromaticity.blue_primary.x=chromaticity[4];
1390 image->chromaticity.blue_primary.y=chromaticity[5];
1393 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1394 if ((compress_tag != COMPRESSION_NONE) &&
1395 (TIFFIsCODECConfigured(compress_tag) == 0))
1398 ThrowReaderException(CoderError,"CompressNotSupported");
1401 switch (compress_tag)
1403 case COMPRESSION_NONE: image->compression=NoCompression; break;
1404 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1405 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1406 case COMPRESSION_JPEG:
1408 image->compression=JPEGCompression;
1409 #if defined(JPEG_SUPPORT)
1412 sampling_factor[MagickPathExtent];
1421 tiff_status=TIFFGetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,&horizontal,
1423 if (tiff_status == 1)
1425 (void) FormatLocaleString(sampling_factor,MagickPathExtent,
1426 "%dx%d",horizontal,vertical);
1427 (void) SetImageProperty(image,"jpeg:sampling-factor",
1428 sampling_factor,exception);
1429 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1430 "Sampling Factors: %s",sampling_factor);
1436 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1437 #if defined(COMPRESSION_LZMA)
1438 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1440 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1441 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1442 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1443 default: image->compression=RLECompression; break;
1445 quantum_info=(QuantumInfo *) NULL;
1446 if ((photometric == PHOTOMETRIC_PALETTE) &&
1447 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1452 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
1453 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
1456 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1459 value=(unsigned short) image->scene;
1460 if (TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages) == 1)
1462 if (image->storage_class == PseudoClass)
1476 Initialize colormap.
1478 tiff_status=TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1479 &green_colormap,&blue_colormap);
1480 if (tiff_status == 1)
1482 if ((red_colormap != (uint16 *) NULL) &&
1483 (green_colormap != (uint16 *) NULL) &&
1484 (blue_colormap != (uint16 *) NULL))
1486 range=255; /* might be old style 8-bit colormap */
1487 for (i=0; i < (ssize_t) image->colors; i++)
1488 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1489 (blue_colormap[i] >= 256))
1494 for (i=0; i < (ssize_t) image->colors; i++)
1496 image->colormap[i].red=ClampToQuantum(((double)
1497 QuantumRange*red_colormap[i])/range);
1498 image->colormap[i].green=ClampToQuantum(((double)
1499 QuantumRange*green_colormap[i])/range);
1500 image->colormap[i].blue=ClampToQuantum(((double)
1501 QuantumRange*blue_colormap[i])/range);
1506 if (image_info->ping != MagickFalse)
1508 if (image_info->number_scenes != 0)
1509 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1511 goto next_tiff_frame;
1513 status=SetImageExtent(image,image->columns,image->rows,exception);
1514 if (status == MagickFalse)
1515 return(DestroyImageList(image));
1517 Allocate memory for the image and pixel buffer.
1519 quantum_info=AcquireQuantumInfo(image_info,image);
1520 if (quantum_info == (QuantumInfo *) NULL)
1523 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1525 if (sample_format == SAMPLEFORMAT_UINT)
1526 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1527 if (sample_format == SAMPLEFORMAT_INT)
1528 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1529 if (sample_format == SAMPLEFORMAT_IEEEFP)
1530 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1531 if (status == MagickFalse)
1534 quantum_info=DestroyQuantumInfo(quantum_info);
1535 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1538 switch (photometric)
1540 case PHOTOMETRIC_MINISBLACK:
1542 quantum_info->min_is_white=MagickFalse;
1545 case PHOTOMETRIC_MINISWHITE:
1547 quantum_info->min_is_white=MagickTrue;
1553 tiff_status=TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1555 if (tiff_status == 1)
1557 (void) SetImageProperty(image,"tiff:alpha","unspecified",exception);
1558 if (extra_samples == 0)
1560 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1561 image->alpha_trait=BlendPixelTrait;
1564 for (i=0; i < extra_samples; i++)
1566 image->alpha_trait=BlendPixelTrait;
1567 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
1569 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
1570 (void) SetImageProperty(image,"tiff:alpha","associated",
1574 if (sample_info[i] == EXTRASAMPLE_UNASSALPHA)
1575 (void) SetImageProperty(image,"tiff:alpha","unassociated",
1579 method=ReadGenericMethod;
1580 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) == 1)
1583 value[MagickPathExtent];
1585 method=ReadStripMethod;
1586 (void) FormatLocaleString(value,MagickPathExtent,"%u",
1587 (unsigned int) rows_per_strip);
1588 (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception);
1590 if ((samples_per_pixel >= 3) && (interlace == PLANARCONFIG_CONTIG))
1591 method=ReadRGBAMethod;
1592 if ((samples_per_pixel >= 4) && (interlace == PLANARCONFIG_SEPARATE))
1593 method=ReadCMYKAMethod;
1594 if ((photometric != PHOTOMETRIC_RGB) &&
1595 (photometric != PHOTOMETRIC_CIELAB) &&
1596 (photometric != PHOTOMETRIC_SEPARATED))
1597 method=ReadGenericMethod;
1598 if (image->storage_class == PseudoClass)
1599 method=ReadSingleSampleMethod;
1600 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1601 (photometric == PHOTOMETRIC_MINISWHITE))
1602 method=ReadSingleSampleMethod;
1603 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1604 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
1605 method=ReadGenericMethod;
1606 if (image->compression == JPEGCompression)
1607 method=GetJPEGMethod(image,tiff,photometric,bits_per_sample,
1609 if (compress_tag == COMPRESSION_JBIG)
1610 method=ReadStripMethod;
1611 if (TIFFIsTiled(tiff) != MagickFalse)
1612 method=ReadTileMethod;
1613 quantum_info->endian=LSBEndian;
1614 quantum_type=RGBQuantum;
1615 tiff_pixels=(unsigned char *) AcquireMagickMemory(MagickMax(
1616 TIFFScanlineSize(tiff),(size_t) (image->columns*samples_per_pixel*
1617 pow(2.0,ceil(log(bits_per_sample)/log(2.0))))));
1618 if (tiff_pixels == (unsigned char *) NULL)
1621 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1625 case ReadSingleSampleMethod:
1628 Convert TIFF image to PseudoClass MIFF image.
1630 quantum_type=IndexQuantum;
1631 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1632 if (image->alpha_trait != UndefinedPixelTrait)
1634 if (image->storage_class != PseudoClass)
1636 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1638 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1642 quantum_type=IndexAlphaQuantum;
1643 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1647 if (image->storage_class != PseudoClass)
1649 quantum_type=GrayQuantum;
1650 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1652 status=SetQuantumPad(image,quantum_info,pad*pow(2,ceil(log(
1653 bits_per_sample)/log(2))));
1654 if (status == MagickFalse)
1657 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1659 for (y=0; y < (ssize_t) image->rows; y++)
1667 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) tiff_pixels);
1670 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1671 if (q == (Quantum *) NULL)
1673 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1674 quantum_type,tiff_pixels,exception);
1675 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1677 if (image->previous == (Image *) NULL)
1679 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1681 if (status == MagickFalse)
1687 case ReadRGBAMethod:
1690 Convert TIFF image to DirectClass MIFF image.
1692 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1693 quantum_type=RGBQuantum;
1694 if (image->alpha_trait != UndefinedPixelTrait)
1696 quantum_type=RGBAQuantum;
1697 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1699 if (image->colorspace == CMYKColorspace)
1701 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1702 quantum_type=CMYKQuantum;
1703 if (image->alpha_trait != UndefinedPixelTrait)
1705 quantum_type=CMYKAQuantum;
1706 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1709 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1710 if (status == MagickFalse)
1713 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1715 for (y=0; y < (ssize_t) image->rows; y++)
1723 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) tiff_pixels);
1726 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1727 if (q == (Quantum *) NULL)
1729 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1730 quantum_type,tiff_pixels,exception);
1731 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1733 if (image->previous == (Image *) NULL)
1735 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1737 if (status == MagickFalse)
1743 case ReadCMYKAMethod:
1746 Convert TIFF image to DirectClass MIFF image.
1748 for (i=0; i < (ssize_t) samples_per_pixel; i++)
1750 for (y=0; y < (ssize_t) image->rows; y++)
1758 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1762 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1763 if (q == (Quantum *) NULL)
1765 if (image->colorspace != CMYKColorspace)
1768 case 0: quantum_type=RedQuantum; break;
1769 case 1: quantum_type=GreenQuantum; break;
1770 case 2: quantum_type=BlueQuantum; break;
1771 case 3: quantum_type=AlphaQuantum; break;
1772 default: quantum_type=UndefinedQuantum; break;
1777 case 0: quantum_type=CyanQuantum; break;
1778 case 1: quantum_type=MagentaQuantum; break;
1779 case 2: quantum_type=YellowQuantum; break;
1780 case 3: quantum_type=BlackQuantum; break;
1781 case 4: quantum_type=AlphaQuantum; break;
1782 default: quantum_type=UndefinedQuantum; break;
1784 (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1785 quantum_type,tiff_pixels,exception);
1786 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1789 if (image->previous == (Image *) NULL)
1791 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1793 if (status == MagickFalse)
1799 case ReadYCCKMethod:
1801 for (y=0; y < (ssize_t) image->rows; y++)
1815 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) tiff_pixels);
1818 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1819 if (q == (Quantum *) NULL)
1822 for (x=0; x < (ssize_t) image->columns; x++)
1824 SetPixelCyan(image,ScaleCharToQuantum(ClampYCC((double) *p+
1825 (1.402*(double) *(p+2))-179.456)),q);
1826 SetPixelMagenta(image,ScaleCharToQuantum(ClampYCC((double) *p-
1827 (0.34414*(double) *(p+1))-(0.71414*(double ) *(p+2))+
1829 SetPixelYellow(image,ScaleCharToQuantum(ClampYCC((double) *p+
1830 (1.772*(double) *(p+1))-226.816)),q);
1831 SetPixelBlack(image,ScaleCharToQuantum((unsigned char) *(p+3)),q);
1832 q+=GetPixelChannels(image);
1835 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1837 if (image->previous == (Image *) NULL)
1839 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1841 if (status == MagickFalse)
1847 case ReadStripMethod:
1853 Convert stripped TIFF image to DirectClass MIFF image.
1857 for (y=0; y < (ssize_t) image->rows; y++)
1865 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1866 if (q == (Quantum *) NULL)
1870 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) tiff_pixels) == 0)
1872 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1876 p=((uint32 *) tiff_pixels)+image->columns*i;
1877 for (x=0; x < (ssize_t) image->columns; x++)
1879 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1881 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1883 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1885 if (image->alpha_trait != UndefinedPixelTrait)
1886 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1889 q+=GetPixelChannels(image);
1891 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1893 if (image->previous == (Image *) NULL)
1895 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1897 if (status == MagickFalse)
1903 case ReadTileMethod:
1914 Convert tiled TIFF image to DirectClass MIFF image.
1916 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) != 1) ||
1917 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) != 1))
1920 ThrowReaderException(CoderError,"ImageIsNotTiled");
1922 (void) SetImageStorageClass(image,DirectClass,exception);
1923 number_pixels=(MagickSizeType) columns*rows;
1924 if (HeapOverflowSanityCheck(rows,sizeof(*tile_pixels)) != MagickFalse)
1927 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1929 tile_pixels=(uint32 *) AcquireQuantumMemory(columns,rows*
1930 sizeof(*tile_pixels));
1931 if (tile_pixels == (uint32 *) NULL)
1934 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1936 for (y=0; y < (ssize_t) image->rows; y+=rows)
1943 *magick_restrict tile;
1949 rows_remaining=image->rows-y;
1950 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
1951 rows_remaining=rows;
1952 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1954 if (tile == (Quantum *) NULL)
1956 for (x=0; x < (ssize_t) image->columns; x+=columns)
1962 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1964 columns_remaining=image->columns-x;
1965 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
1966 columns_remaining=columns;
1967 p=tile_pixels+(rows-rows_remaining)*columns;
1968 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1970 for (row=rows_remaining; row > 0; row--)
1972 if (image->alpha_trait != UndefinedPixelTrait)
1973 for (column=columns_remaining; column > 0; column--)
1975 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1977 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1979 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1981 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1984 q+=GetPixelChannels(image);
1987 for (column=columns_remaining; column > 0; column--)
1989 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1991 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1993 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1996 q+=GetPixelChannels(image);
1998 p+=columns-columns_remaining;
1999 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
2002 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2004 if (image->previous == (Image *) NULL)
2006 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
2008 if (status == MagickFalse)
2012 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
2015 case ReadGenericMethod:
2028 Convert TIFF image to DirectClass MIFF image.
2030 number_pixels=(MagickSizeType) image->columns*image->rows;
2031 if (HeapOverflowSanityCheck(image->rows,sizeof(*pixels)) != MagickFalse)
2034 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2036 pixel_info=AcquireVirtualMemory(image->columns,image->rows*
2038 if (pixel_info == (MemoryInfo *) NULL)
2041 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2043 pixels=(uint32 *) GetVirtualMemoryBlob(pixel_info);
2044 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,(uint32)
2045 image->rows,(uint32 *) pixels,0);
2047 Convert image to DirectClass pixel packets.
2049 p=pixels+number_pixels-1;
2050 for (y=0; y < (ssize_t) image->rows; y++)
2058 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
2059 if (q == (Quantum *) NULL)
2061 q+=GetPixelChannels(image)*(image->columns-1);
2062 for (x=0; x < (ssize_t) image->columns; x++)
2064 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
2066 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
2068 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
2070 if (image->alpha_trait != UndefinedPixelTrait)
2071 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
2074 q-=GetPixelChannels(image);
2076 if (SyncAuthenticPixels(image,exception) == MagickFalse)
2078 if (image->previous == (Image *) NULL)
2080 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
2082 if (status == MagickFalse)
2086 pixel_info=RelinquishVirtualMemory(pixel_info);
2090 tiff_pixels=(unsigned char *) RelinquishMagickMemory(tiff_pixels);
2091 SetQuantumImageType(image,quantum_type);
2093 if (quantum_info != (QuantumInfo *) NULL)
2094 quantum_info=DestroyQuantumInfo(quantum_info);
2095 if (photometric == PHOTOMETRIC_CIELAB)
2096 DecodeLabImage(image,exception);
2097 if ((photometric == PHOTOMETRIC_LOGL) ||
2098 (photometric == PHOTOMETRIC_MINISBLACK) ||
2099 (photometric == PHOTOMETRIC_MINISWHITE))
2101 image->type=GrayscaleType;
2102 if (bits_per_sample == 1)
2103 image->type=BilevelType;
2106 Proceed to next image.
2108 if (image_info->number_scenes != 0)
2109 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
2111 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
2112 if (status != MagickFalse)
2115 Allocate next image structure.
2117 AcquireNextImage(image_info,image,exception);
2118 if (GetNextImageInList(image) == (Image *) NULL)
2120 image=DestroyImageList(image);
2121 return((Image *) NULL);
2123 image=SyncNextImageInList(image);
2124 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
2126 if (status == MagickFalse)
2129 } while (status != MagickFalse);
2131 TIFFReadPhotoshopLayers(image,image_info,exception);
2132 if (image_info->number_scenes != 0)
2134 if (image_info->scene >= GetImageListLength(image))
2136 /* Subimage was not found in the Photoshop layer */
2137 image=DestroyImageList(image);
2138 return((Image *)NULL);
2141 return(GetFirstImageInList(image));
2146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2150 % R e g i s t e r T I F F I m a g e %
2154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2156 % RegisterTIFFImage() adds properties for the TIFF image format to
2157 % the list of supported formats. The properties include the image format
2158 % tag, a method to read and/or write the format, whether the format
2159 % supports the saving of more than one frame to the same file or blob,
2160 % whether the format supports native in-memory I/O, and a brief
2161 % description of the format.
2163 % The format of the RegisterTIFFImage method is:
2165 % size_t RegisterTIFFImage(void)
2169 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2170 static TIFFExtendProc
2171 tag_extender = (TIFFExtendProc) NULL;
2173 static void TIFFIgnoreTags(TIFF *tiff)
2194 if (TIFFGetReadProc(tiff) != TIFFReadBlob)
2196 image=(Image *)TIFFClientdata(tiff);
2197 tags=GetImageArtifact(image,"tiff:ignore-tags");
2198 if (tags == (const char *) NULL)
2204 while ((isspace((int) ((unsigned char) *p)) != 0))
2207 (void) strtol(p,&q,10);
2214 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2221 ignore=(TIFFFieldInfo *) AcquireQuantumMemory(count,sizeof(*ignore));
2222 /* This also sets field_bit to 0 (FIELD_IGNORE) */
2223 ResetMagickMemory(ignore,0,count*sizeof(*ignore));
2226 while ((isspace((int) ((unsigned char) *p)) != 0))
2229 ignore[i].field_tag=(ttag_t) strtol(p,&q,10);
2234 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2237 (void) TIFFMergeFieldInfo(tiff,ignore,(uint32) count);
2238 ignore=(TIFFFieldInfo *) RelinquishMagickMemory(ignore);
2241 static void TIFFTagExtender(TIFF *tiff)
2243 static const TIFFFieldInfo
2246 { 37724, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
2247 (char *) "PhotoshopLayerData" },
2248 { 34118, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
2249 (char *) "Microscope" }
2252 TIFFMergeFieldInfo(tiff,TIFFExtensions,sizeof(TIFFExtensions)/
2253 sizeof(*TIFFExtensions));
2254 if (tag_extender != (TIFFExtendProc) NULL)
2255 (*tag_extender)(tiff);
2256 TIFFIgnoreTags(tiff);
2260 ModuleExport size_t RegisterTIFFImage(void)
2262 #define TIFFDescription "Tagged Image File Format"
2265 version[MagickPathExtent];
2270 if (tiff_semaphore == (SemaphoreInfo *) NULL)
2271 ActivateSemaphoreInfo(&tiff_semaphore);
2272 LockSemaphoreInfo(tiff_semaphore);
2273 if (instantiate_key == MagickFalse)
2275 if (CreateMagickThreadKey(&tiff_exception,NULL) == MagickFalse)
2276 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2277 error_handler=TIFFSetErrorHandler(TIFFErrors);
2278 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
2279 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2280 if (tag_extender == (TIFFExtendProc) NULL)
2281 tag_extender=TIFFSetTagExtender(TIFFTagExtender);
2283 instantiate_key=MagickTrue;
2285 UnlockSemaphoreInfo(tiff_semaphore);
2287 #if defined(TIFF_VERSION)
2288 (void) FormatLocaleString(version,MagickPathExtent,"%d",TIFF_VERSION);
2290 #if defined(MAGICKCORE_TIFF_DELEGATE)
2299 for (i=0; (i < (MagickPathExtent-1)) && (*p != 0) && (*p != '\n'); i++)
2305 entry=AcquireMagickInfo("TIFF","GROUP4","Raw CCITT Group4");
2306 #if defined(MAGICKCORE_TIFF_DELEGATE)
2307 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
2308 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
2310 entry->flags|=CoderRawSupportFlag;
2311 entry->flags|=CoderEndianSupportFlag;
2312 entry->flags|=CoderSeekableStreamFlag;
2313 entry->flags^=CoderAdjoinFlag;
2314 entry->flags^=CoderUseExtensionFlag;
2315 entry->format_type=ImplicitFormatType;
2316 entry->mime_type=ConstantString("image/tiff");
2317 (void) RegisterMagickInfo(entry);
2318 entry=AcquireMagickInfo("TIFF","PTIF","Pyramid encoded TIFF");
2319 #if defined(MAGICKCORE_TIFF_DELEGATE)
2320 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2321 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
2323 entry->flags|=CoderEndianSupportFlag;
2324 entry->flags|=CoderSeekableStreamFlag;
2325 entry->flags^=CoderUseExtensionFlag;
2326 entry->mime_type=ConstantString("image/tiff");
2327 (void) RegisterMagickInfo(entry);
2328 entry=AcquireMagickInfo("TIFF","TIF",TIFFDescription);
2329 #if defined(MAGICKCORE_TIFF_DELEGATE)
2330 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2331 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2333 entry->flags|=CoderEndianSupportFlag;
2334 entry->flags|=CoderSeekableStreamFlag;
2335 entry->flags|=CoderStealthFlag;
2336 entry->flags^=CoderUseExtensionFlag;
2337 if (*version != '\0')
2338 entry->version=ConstantString(version);
2339 entry->mime_type=ConstantString("image/tiff");
2340 (void) RegisterMagickInfo(entry);
2341 entry=AcquireMagickInfo("TIFF","TIFF",TIFFDescription);
2342 #if defined(MAGICKCORE_TIFF_DELEGATE)
2343 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2344 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2346 entry->magick=(IsImageFormatHandler *) IsTIFF;
2347 entry->flags|=CoderEndianSupportFlag;
2348 entry->flags|=CoderSeekableStreamFlag;
2349 entry->flags^=CoderUseExtensionFlag;
2350 if (*version != '\0')
2351 entry->version=ConstantString(version);
2352 entry->mime_type=ConstantString("image/tiff");
2353 (void) RegisterMagickInfo(entry);
2354 entry=AcquireMagickInfo("TIFF","TIFF64","Tagged Image File Format (64-bit)");
2355 #if defined(TIFF_VERSION_BIG)
2356 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
2357 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
2359 entry->flags|=CoderEndianSupportFlag;
2360 entry->flags|=CoderSeekableStreamFlag;
2361 entry->flags^=CoderAdjoinFlag;
2362 entry->flags^=CoderUseExtensionFlag;
2363 if (*version != '\0')
2364 entry->version=ConstantString(version);
2365 entry->mime_type=ConstantString("image/tiff");
2366 (void) RegisterMagickInfo(entry);
2367 return(MagickImageCoderSignature);
2371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2375 % U n r e g i s t e r T I F F I m a g e %
2379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2381 % UnregisterTIFFImage() removes format registrations made by the TIFF module
2382 % from the list of supported formats.
2384 % The format of the UnregisterTIFFImage method is:
2386 % UnregisterTIFFImage(void)
2389 ModuleExport void UnregisterTIFFImage(void)
2391 (void) UnregisterMagickInfo("TIFF64");
2392 (void) UnregisterMagickInfo("TIFF");
2393 (void) UnregisterMagickInfo("TIF");
2394 (void) UnregisterMagickInfo("PTIF");
2395 if (tiff_semaphore == (SemaphoreInfo *) NULL)
2396 ActivateSemaphoreInfo(&tiff_semaphore);
2397 LockSemaphoreInfo(tiff_semaphore);
2398 if (instantiate_key != MagickFalse)
2400 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
2401 if (tag_extender == (TIFFExtendProc) NULL)
2402 (void) TIFFSetTagExtender(tag_extender);
2404 if (DeleteMagickThreadKey(tiff_exception) == MagickFalse)
2405 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
2406 (void) TIFFSetWarningHandler(warning_handler);
2407 (void) TIFFSetErrorHandler(error_handler);
2408 instantiate_key=MagickFalse;
2410 UnlockSemaphoreInfo(tiff_semaphore);
2411 RelinquishSemaphoreInfo(&tiff_semaphore);
2414 #if defined(MAGICKCORE_TIFF_DELEGATE)
2416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2420 % W r i t e G R O U P 4 I m a g e %
2424 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2426 % WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
2428 % The format of the WriteGROUP4Image method is:
2430 % MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2431 % Image *image,ExceptionInfo *)
2433 % A description of each parameter follows:
2435 % o image_info: the image info.
2437 % o image: The image.
2439 % o exception: return any errors or warnings in this structure.
2442 static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2443 Image *image,ExceptionInfo *exception)
2446 filename[MagickPathExtent];
2480 Write image as CCITT Group4 TIFF image to a temporary file.
2482 assert(image_info != (const ImageInfo *) NULL);
2483 assert(image_info->signature == MagickCoreSignature);
2484 assert(image != (Image *) NULL);
2485 assert(image->signature == MagickCoreSignature);
2486 if (image->debug != MagickFalse)
2487 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2488 assert(exception != (ExceptionInfo *) NULL);
2489 assert(exception->signature == MagickCoreSignature);
2490 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2491 if (status == MagickFalse)
2493 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
2494 if (huffman_image == (Image *) NULL)
2496 (void) CloseBlob(image);
2497 return(MagickFalse);
2499 huffman_image->endian=MSBEndian;
2501 unique_file=AcquireUniqueFileResource(filename);
2502 if (unique_file != -1)
2503 file=fdopen(unique_file,"wb");
2504 if ((unique_file == -1) || (file == (FILE *) NULL))
2506 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2508 return(MagickFalse);
2510 (void) FormatLocaleString(huffman_image->filename,MagickPathExtent,"tiff:%s",
2512 (void) SetImageType(huffman_image,BilevelType,exception);
2513 write_info=CloneImageInfo((ImageInfo *) NULL);
2514 SetImageInfoFile(write_info,file);
2515 (void) SetImageDepth(image,1,exception);
2516 (void) SetImageType(image,BilevelType,exception);
2517 write_info->compression=Group4Compression;
2518 write_info->type=BilevelType;
2519 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
2520 status=WriteTIFFImage(write_info,huffman_image,exception);
2521 (void) fflush(file);
2522 write_info=DestroyImageInfo(write_info);
2523 if (status == MagickFalse)
2525 huffman_image=DestroyImage(huffman_image);
2526 (void) fclose(file);
2527 (void) RelinquishUniqueFileResource(filename);
2528 return(MagickFalse);
2530 tiff=TIFFOpen(filename,"rb");
2531 if (tiff == (TIFF *) NULL)
2533 huffman_image=DestroyImage(huffman_image);
2534 (void) fclose(file);
2535 (void) RelinquishUniqueFileResource(filename);
2536 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
2537 image_info->filename);
2538 return(MagickFalse);
2541 Allocate raw strip buffer.
2543 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2546 huffman_image=DestroyImage(huffman_image);
2547 (void) fclose(file);
2548 (void) RelinquishUniqueFileResource(filename);
2549 return(MagickFalse);
2551 strip_size=byte_count[0];
2552 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2553 if (byte_count[i] > strip_size)
2554 strip_size=byte_count[i];
2555 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2557 if (buffer == (unsigned char *) NULL)
2560 huffman_image=DestroyImage(huffman_image);
2561 (void) fclose(file);
2562 (void) RelinquishUniqueFileResource(filename);
2563 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2564 image_info->filename);
2567 Compress runlength encoded to 2D Huffman pixels.
2569 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2571 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
2572 if (WriteBlob(image,(size_t) count,buffer) != count)
2575 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2577 huffman_image=DestroyImage(huffman_image);
2578 (void) fclose(file);
2579 (void) RelinquishUniqueFileResource(filename);
2580 (void) CloseBlob(image);
2585 #if defined(MAGICKCORE_TIFF_DELEGATE)
2587 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2591 % W r i t e P T I F I m a g e %
2595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2597 % WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2600 % The format of the WritePTIFImage method is:
2602 % MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2603 % Image *image,ExceptionInfo *exception)
2605 % A description of each parameter follows:
2607 % o image_info: the image info.
2609 % o image: The image.
2611 % o exception: return any errors or warnings in this structure.
2614 static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2615 Image *image,ExceptionInfo *exception)
2636 Create pyramid-encoded TIFF image.
2638 images=NewImageList();
2639 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2644 clone_image=CloneImage(next,0,0,MagickFalse,exception);
2645 if (clone_image == (Image *) NULL)
2647 clone_image->previous=NewImageList();
2648 clone_image->next=NewImageList();
2649 (void) SetImageProperty(clone_image,"tiff:subfiletype","none",exception);
2650 AppendImageToList(&images,clone_image);
2651 columns=next->columns;
2653 resolution=next->resolution;
2654 while ((columns > 64) && (rows > 64))
2660 pyramid_image=ResizeImage(next,columns,rows,image->filter,exception);
2661 if (pyramid_image == (Image *) NULL)
2663 pyramid_image->resolution=resolution;
2664 (void) SetImageProperty(pyramid_image,"tiff:subfiletype","REDUCEDIMAGE",
2666 AppendImageToList(&images,pyramid_image);
2669 images=GetFirstImageInList(images);
2671 Write pyramid-encoded TIFF image.
2673 write_info=CloneImageInfo(image_info);
2674 write_info->adjoin=MagickTrue;
2675 (void) CopyMagickString(write_info->magick,"TIFF",MagickPathExtent);
2676 (void) CopyMagickString(images->magick,"TIFF",MagickPathExtent);
2677 status=WriteTIFFImage(write_info,images,exception);
2678 images=DestroyImageList(images);
2679 write_info=DestroyImageInfo(write_info);
2684 #if defined(MAGICKCORE_TIFF_DELEGATE)
2686 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2689 % W r i t e T I F F I m a g e %
2693 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2695 % WriteTIFFImage() writes an image in the Tagged image file format.
2697 % The format of the WriteTIFFImage method is:
2699 % MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2700 % Image *image,ExceptionInfo *exception)
2702 % A description of each parameter follows:
2704 % o image_info: the image info.
2706 % o image: The image.
2708 % o exception: return any errors or warnings in this structure.
2712 typedef struct _TIFFInfo
2723 static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2725 assert(tiff_info != (TIFFInfo *) NULL);
2726 if (tiff_info->scanlines != (unsigned char *) NULL)
2727 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2728 tiff_info->scanlines);
2729 if (tiff_info->pixels != (unsigned char *) NULL)
2730 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2734 static MagickBooleanType EncodeLabImage(Image *image,ExceptionInfo *exception)
2746 image_view=AcquireAuthenticCacheView(image,exception);
2747 for (y=0; y < (ssize_t) image->rows; y++)
2755 if (status == MagickFalse)
2757 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2758 if (q == (Quantum *) NULL)
2763 for (x=0; x < (ssize_t) image->columns; x++)
2769 a=QuantumScale*GetPixela(image,q)-0.5;
2772 b=QuantumScale*GetPixelb(image,q)-0.5;
2775 SetPixela(image,QuantumRange*a,q);
2776 SetPixelb(image,QuantumRange*b,q);
2777 q+=GetPixelChannels(image);
2779 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2782 image_view=DestroyCacheView(image_view);
2786 static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,
2787 TIFF *tiff,TIFFInfo *tiff_info)
2799 assert(tiff_info != (TIFFInfo *) NULL);
2800 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2801 option=GetImageOption(image_info,"tiff:tile-geometry");
2802 if (option == (const char *) NULL)
2807 option=GetImageOption(image_info,"tiff:rows-per-strip");
2808 if (option != (const char *) NULL)
2809 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
2811 if (TIFFGetField(tiff,TIFFTAG_IMAGELENGTH,&rows_per_strip) == 0)
2812 rows_per_strip=0; /* use default */
2813 rows_per_strip=TIFFDefaultStripSize(tiff,rows_per_strip);
2814 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
2818 Create tiled TIFF, ignore "tiff:rows-per-strip".
2820 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2821 if ((flags & HeightValue) == 0)
2822 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2823 tile_columns=(uint32) tiff_info->tile_geometry.width;
2824 tile_rows=(uint32) tiff_info->tile_geometry.height;
2825 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2826 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2827 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2828 tiff_info->tile_geometry.width=tile_columns;
2829 tiff_info->tile_geometry.height=tile_rows;
2830 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2831 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
2832 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2833 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2834 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2835 (tiff_info->pixels == (unsigned char *) NULL))
2837 DestroyTIFFInfo(tiff_info);
2838 return(MagickFalse);
2843 static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
2844 tsample_t sample,Image *image)
2852 register unsigned char
2866 if (TIFFIsTiled(tiff) == 0)
2867 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2869 Fill scanlines to tile height.
2871 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2872 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2873 (size_t) TIFFScanlineSize(tiff));
2874 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2875 (tiff_info->tile_geometry.height-1)) &&
2876 (row != (ssize_t) (image->rows-1)))
2879 Write tile to TIFF image.
2882 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (
2883 tiff_info->tile_geometry.height*tiff_info->tile_geometry.width);
2884 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2885 tiff_info->tile_geometry.width;
2886 for (i=0; i < (ssize_t) number_tiles; i++)
2888 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
2889 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2890 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2891 for (k=0; k < (ssize_t) tile_width; k++)
2893 if (bytes_per_pixel == 0)
2895 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2896 tiff_info->tile_geometry.width+k)/8);
2897 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2901 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2902 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2903 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2904 for (l=0; l < bytes_per_pixel; l++)
2907 if ((i*tiff_info->tile_geometry.width) != image->columns)
2908 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2909 tiff_info->tile_geometry.width),(uint32) ((row/
2910 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2918 static void TIFFSetProfiles(TIFF *tiff,Image *image)
2926 if (image->profiles == (void *) NULL)
2928 ResetImageProfileIterator(image);
2929 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2931 profile=GetImageProfile(image,name);
2932 if (GetStringInfoLength(profile) == 0)
2934 name=GetNextImageProfile(image);
2937 #if defined(TIFFTAG_XMLPACKET)
2938 if (LocaleCompare(name,"xmp") == 0)
2939 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2940 profile),GetStringInfoDatum(profile));
2942 #if defined(TIFFTAG_ICCPROFILE)
2943 if (LocaleCompare(name,"icc") == 0)
2944 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2945 profile),GetStringInfoDatum(profile));
2947 if (LocaleCompare(name,"iptc") == 0)
2955 iptc_profile=CloneStringInfo(profile);
2956 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2958 SetStringInfoLength(iptc_profile,length);
2959 if (TIFFIsByteSwapped(tiff))
2960 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2961 (unsigned long) (length/4));
2962 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2963 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2964 iptc_profile=DestroyStringInfo(iptc_profile);
2966 #if defined(TIFFTAG_PHOTOSHOP)
2967 if (LocaleCompare(name,"8bim") == 0)
2968 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2969 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2971 if (LocaleCompare(name,"tiff:37724") == 0)
2972 (void) TIFFSetField(tiff,37724,(uint32) GetStringInfoLength(profile),
2973 GetStringInfoDatum(profile));
2974 if (LocaleCompare(name,"tiff:34118") == 0)
2975 (void) TIFFSetField(tiff,34118,(uint32) GetStringInfoLength(profile),
2976 GetStringInfoDatum(profile));
2977 name=GetNextImageProfile(image);
2981 static void TIFFSetProperties(TIFF *tiff,const ImageInfo *image_info,
2982 Image *image,ExceptionInfo *exception)
2987 value=GetImageArtifact(image,"tiff:document");
2988 if (value != (const char *) NULL)
2989 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,value);
2990 value=GetImageArtifact(image,"tiff:hostcomputer");
2991 if (value != (const char *) NULL)
2992 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2993 value=GetImageArtifact(image,"tiff:artist");
2994 if (value != (const char *) NULL)
2995 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2996 value=GetImageArtifact(image,"tiff:timestamp");
2997 if (value != (const char *) NULL)
2998 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2999 value=GetImageArtifact(image,"tiff:make");
3000 if (value != (const char *) NULL)
3001 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
3002 value=GetImageArtifact(image,"tiff:model");
3003 if (value != (const char *) NULL)
3004 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
3005 value=GetImageArtifact(image,"tiff:software");
3006 if (value != (const char *) NULL)
3007 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
3008 value=GetImageArtifact(image,"tiff:copyright");
3009 if (value != (const char *) NULL)
3010 (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value);
3011 value=GetImageArtifact(image,"kodak-33423");
3012 if (value != (const char *) NULL)
3013 (void) TIFFSetField(tiff,33423,value);
3014 value=GetImageArtifact(image,"kodak-36867");
3015 if (value != (const char *) NULL)
3016 (void) TIFFSetField(tiff,36867,value);
3017 value=GetImageProperty(image,"label",exception);
3018 if (value != (const char *) NULL)
3019 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
3020 value=GetImageProperty(image,"comment",exception);
3021 if (value != (const char *) NULL)
3022 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
3023 value=GetImageArtifact(image,"tiff:subfiletype");
3024 if (value != (const char *) NULL)
3026 if (LocaleCompare(value,"REDUCEDIMAGE") == 0)
3027 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
3029 if (LocaleCompare(value,"PAGE") == 0)
3030 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3032 if (LocaleCompare(value,"MASK") == 0)
3033 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_MASK);
3041 page=(uint16) image->scene;
3042 pages=(uint16) GetImageListLength(image);
3043 if ((image_info->adjoin != MagickFalse) && (pages > 1))
3044 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3045 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3049 static void TIFFSetEXIFProperties(TIFF *tiff,Image *image,
3050 ExceptionInfo *exception)
3052 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
3063 Write EXIF properties.
3066 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
3067 for (i=0; exif_info[i].tag != 0; i++)
3069 value=GetImageProperty(image,exif_info[i].property,exception);
3070 if (value == (const char *) NULL)
3072 switch (exif_info[i].type)
3076 (void) TIFFSetField(tiff,exif_info[i].tag,value);
3084 field=(uint16) StringToLong(value);
3085 (void) TIFFSetField(tiff,exif_info[i].tag,field);
3093 field=(uint16) StringToLong(value);
3094 (void) TIFFSetField(tiff,exif_info[i].tag,field);
3098 case TIFF_SRATIONAL:
3103 field=StringToDouble(value,(char **) NULL);
3104 (void) TIFFSetField(tiff,exif_info[i].tag,field);
3111 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
3118 static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
3119 Image *image,ExceptionInfo *exception)
3171 assert(image_info != (const ImageInfo *) NULL);
3172 assert(image_info->signature == MagickCoreSignature);
3173 assert(image != (Image *) NULL);
3174 assert(image->signature == MagickCoreSignature);
3175 if (image->debug != MagickFalse)
3176 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3177 assert(exception != (ExceptionInfo *) NULL);
3178 assert(exception->signature == MagickCoreSignature);
3179 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
3180 if (status == MagickFalse)
3182 (void) SetMagickThreadValue(tiff_exception,exception);
3183 endian_type=UndefinedEndian;
3184 option=GetImageOption(image_info,"tiff:endian");
3185 if (option != (const char *) NULL)
3187 if (LocaleNCompare(option,"msb",3) == 0)
3188 endian_type=MSBEndian;
3189 if (LocaleNCompare(option,"lsb",3) == 0)
3190 endian_type=LSBEndian;;
3192 switch (endian_type)
3194 case LSBEndian: mode="wl"; break;
3195 case MSBEndian: mode="wb"; break;
3196 default: mode="w"; break;
3198 #if defined(TIFF_VERSION_BIG)
3199 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
3200 switch (endian_type)
3202 case LSBEndian: mode="wl8"; break;
3203 case MSBEndian: mode="wb8"; break;
3204 default: mode="w8"; break;
3207 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
3208 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
3210 if (tiff == (TIFF *) NULL)
3211 return(MagickFalse);
3213 debug=IsEventLogging();
3218 Initialize TIFF fields.
3220 if ((image_info->type != UndefinedType) &&
3221 (image_info->type != OptimizeType))
3222 (void) SetImageType(image,image_info->type,exception);
3223 compression=UndefinedCompression;
3224 if (image->compression != JPEGCompression)
3225 compression=image->compression;
3226 if (image_info->compression != UndefinedCompression)
3227 compression=image_info->compression;
3228 switch (compression)
3230 case FaxCompression:
3231 case Group4Compression:
3233 (void) SetImageType(image,BilevelType,exception);
3234 (void) SetImageDepth(image,1,exception);
3237 case JPEGCompression:
3239 (void) SetImageStorageClass(image,DirectClass,exception);
3240 (void) SetImageDepth(image,8,exception);
3246 quantum_info=AcquireQuantumInfo(image_info,image);
3247 if (quantum_info == (QuantumInfo *) NULL)
3248 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3249 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
3250 (quantum_info->format == UndefinedQuantumFormat) &&
3251 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
3253 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
3254 if (status == MagickFalse)
3255 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3257 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
3258 (GetPreviousImageInList(image) != (Image *) NULL))
3259 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
3260 if ((image->columns != (uint32) image->columns) ||
3261 (image->rows != (uint32) image->rows))
3262 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
3263 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
3264 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
3265 switch (compression)
3267 case FaxCompression:
3269 compress_tag=COMPRESSION_CCITTFAX3;
3270 SetQuantumMinIsWhite(quantum_info,MagickTrue);
3273 case Group4Compression:
3275 compress_tag=COMPRESSION_CCITTFAX4;
3276 SetQuantumMinIsWhite(quantum_info,MagickTrue);
3279 #if defined(COMPRESSION_JBIG)
3280 case JBIG1Compression:
3282 compress_tag=COMPRESSION_JBIG;
3286 case JPEGCompression:
3288 compress_tag=COMPRESSION_JPEG;
3291 #if defined(COMPRESSION_LZMA)
3292 case LZMACompression:
3294 compress_tag=COMPRESSION_LZMA;
3298 case LZWCompression:
3300 compress_tag=COMPRESSION_LZW;
3303 case RLECompression:
3305 compress_tag=COMPRESSION_PACKBITS;
3308 case ZipCompression:
3310 compress_tag=COMPRESSION_ADOBE_DEFLATE;
3316 compress_tag=COMPRESSION_NONE;
3320 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
3321 if ((compress_tag != COMPRESSION_NONE) &&
3322 (TIFFIsCODECConfigured(compress_tag) == 0))
3324 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3325 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
3326 MagickCompressOptions,(ssize_t) compression));
3327 compress_tag=COMPRESSION_NONE;
3328 compression=NoCompression;
3331 switch (compress_tag)
3333 #if defined(CCITT_SUPPORT)
3334 case COMPRESSION_CCITTFAX3:
3335 case COMPRESSION_CCITTFAX4:
3337 #if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
3338 case COMPRESSION_JPEG:
3340 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3341 case COMPRESSION_LZMA:
3343 #if defined(LZW_SUPPORT)
3344 case COMPRESSION_LZW:
3346 #if defined(PACKBITS_SUPPORT)
3347 case COMPRESSION_PACKBITS:
3349 #if defined(ZIP_SUPPORT)
3350 case COMPRESSION_ADOBE_DEFLATE:
3352 case COMPRESSION_NONE:
3356 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3357 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
3358 MagickCompressOptions,(ssize_t) compression));
3359 compress_tag=COMPRESSION_NONE;
3360 compression=NoCompression;
3365 if (image->colorspace == CMYKColorspace)
3367 photometric=PHOTOMETRIC_SEPARATED;
3368 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
3369 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
3374 Full color TIFF raster.
3376 if (image->colorspace == LabColorspace)
3378 photometric=PHOTOMETRIC_CIELAB;
3379 EncodeLabImage(image,exception);
3382 if (image->colorspace == YCbCrColorspace)
3384 photometric=PHOTOMETRIC_YCBCR;
3385 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
3386 (void) SetImageStorageClass(image,DirectClass,exception);
3387 (void) SetImageDepth(image,8,exception);
3390 photometric=PHOTOMETRIC_RGB;
3391 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
3392 if ((image_info->type != TrueColorType) &&
3393 (image_info->type != TrueColorAlphaType))
3395 if ((image_info->type != PaletteType) &&
3396 (SetImageGray(image,exception) != MagickFalse))
3398 photometric=(uint16) (quantum_info->min_is_white !=
3399 MagickFalse ? PHOTOMETRIC_MINISWHITE :
3400 PHOTOMETRIC_MINISBLACK);
3401 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
3402 if ((image->depth == 1) &&
3403 (image->alpha_trait == UndefinedPixelTrait))
3404 SetImageMonochrome(image,exception);
3407 if (image->storage_class == PseudoClass)
3413 Colormapped TIFF raster.
3415 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
3416 photometric=PHOTOMETRIC_PALETTE;
3418 while ((GetQuantumRange(depth)+1) < image->colors)
3420 status=SetQuantumDepth(image,quantum_info,depth);
3421 if (status == MagickFalse)
3422 ThrowWriterException(ResourceLimitError,
3423 "MemoryAllocationFailed");
3427 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
3428 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
3429 (photometric != PHOTOMETRIC_MINISWHITE))
3431 compress_tag=COMPRESSION_NONE;
3432 endian=FILLORDER_MSB2LSB;
3435 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
3436 (photometric != PHOTOMETRIC_MINISWHITE))
3438 compress_tag=COMPRESSION_NONE;
3439 endian=FILLORDER_MSB2LSB;
3441 option=GetImageOption(image_info,"tiff:fill-order");
3442 if (option != (const char *) NULL)
3444 if (LocaleNCompare(option,"msb",3) == 0)
3445 endian=FILLORDER_MSB2LSB;
3446 if (LocaleNCompare(option,"lsb",3) == 0)
3447 endian=FILLORDER_LSB2MSB;
3449 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
3450 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
3451 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
3452 if (image->alpha_trait != UndefinedPixelTrait)
3460 TIFF has a matte channel.
3463 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
3464 option=GetImageOption(image_info,"tiff:alpha");
3465 if (option != (const char *) NULL)
3467 if (LocaleCompare(option,"associated") == 0)
3468 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
3470 if (LocaleCompare(option,"unspecified") == 0)
3471 sample_info[0]=EXTRASAMPLE_UNSPECIFIED;
3473 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
3474 &samples_per_pixel);
3475 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
3476 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
3478 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
3479 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
3481 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
3482 switch (quantum_info->format)
3484 case FloatingPointQuantumFormat:
3486 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
3487 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
3488 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
3491 case SignedQuantumFormat:
3493 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
3496 case UnsignedQuantumFormat:
3498 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
3504 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
3505 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
3506 if (photometric == PHOTOMETRIC_RGB)
3507 if ((image_info->interlace == PlaneInterlace) ||
3508 (image_info->interlace == PartitionInterlace))
3509 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
3510 switch (compress_tag)
3512 case COMPRESSION_JPEG:
3514 #if defined(JPEG_SUPPORT)
3517 if (image_info->quality != UndefinedCompressionQuality)
3518 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
3519 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
3520 if (IssRGBCompatibleColorspace(image->colorspace) != MagickFalse)
3525 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
3526 if (image->colorspace == YCbCrColorspace)
3537 sampling_factor=(const char *) NULL;
3538 value=GetImageProperty(image,"jpeg:sampling-factor",exception);
3539 if (value != (char *) NULL)
3541 sampling_factor=value;
3542 if (image->debug != MagickFalse)
3543 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3544 " Input sampling-factors=%s",sampling_factor);
3546 if (image_info->sampling_factor != (char *) NULL)
3547 sampling_factor=image_info->sampling_factor;
3548 if (sampling_factor != (const char *) NULL)
3550 flags=ParseGeometry(sampling_factor,&geometry_info);
3551 if ((flags & SigmaValue) == 0)
3552 geometry_info.sigma=geometry_info.rho;
3553 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
3554 geometry_info.rho,(uint16) geometry_info.sigma);
3558 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3560 if (bits_per_sample == 12)
3561 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
3565 case COMPRESSION_ADOBE_DEFLATE:
3567 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3569 if (((photometric == PHOTOMETRIC_RGB) ||
3570 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3571 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3572 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3573 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
3574 image_info->quality == UndefinedCompressionQuality ? 7 :
3575 MagickMin((ssize_t) image_info->quality/10,9)));
3578 case COMPRESSION_CCITTFAX3:
3583 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
3586 case COMPRESSION_CCITTFAX4:
3588 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3589 case COMPRESSION_LZMA:
3591 if (((photometric == PHOTOMETRIC_RGB) ||
3592 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3593 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3594 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3595 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
3596 image_info->quality == UndefinedCompressionQuality ? 7 :
3597 MagickMin((ssize_t) image_info->quality/10,9)));
3601 case COMPRESSION_LZW:
3603 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3605 if (((photometric == PHOTOMETRIC_RGB) ||
3606 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3607 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3608 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3614 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
3620 Set image resolution.
3623 if (image->units == PixelsPerInchResolution)
3625 if (image->units == PixelsPerCentimeterResolution)
3626 units=RESUNIT_CENTIMETER;
3627 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
3628 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x);
3629 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y);
3630 if ((image->page.x < 0) || (image->page.y < 0))
3631 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
3632 "TIFF: negative image positions unsupported","%s",image->filename);
3633 if ((image->page.x > 0) && (image->resolution.x > 0.0))
3636 Set horizontal image position.
3638 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
3639 image->resolution.x);
3641 if ((image->page.y > 0) && (image->resolution.y > 0.0))
3644 Set vertical image position.
3646 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
3647 image->resolution.y);
3650 if (image->chromaticity.white_point.x != 0.0)
3656 Set image chromaticity.
3658 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3659 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3660 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3661 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3662 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3663 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3664 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3665 chromaticity[0]=(float) image->chromaticity.white_point.x;
3666 chromaticity[1]=(float) image->chromaticity.white_point.y;
3667 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3669 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3670 (image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3672 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3673 if (image->scene != 0)
3674 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3675 GetImageListLength(image));
3677 if (image->orientation != UndefinedOrientation)
3678 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3679 (void) TIFFSetProfiles(tiff,image);
3685 page=(uint16) scene;
3686 pages=(uint16) GetImageListLength(image);
3687 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3688 (image_info->adjoin != MagickFalse) && (pages > 1))
3689 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3690 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3692 (void) TIFFSetProperties(tiff,image_info,image,exception);
3693 DisableMSCWarning(4127)
3696 (void) TIFFSetEXIFProperties(tiff,image,exception);
3698 Write image scanlines.
3700 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3701 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3702 quantum_info->endian=LSBEndian;
3703 pixels=(unsigned char *) GetQuantumPixels(quantum_info);
3704 tiff_info.scanline=(unsigned char *) GetQuantumPixels(quantum_info);
3705 switch (photometric)
3707 case PHOTOMETRIC_CIELAB:
3708 case PHOTOMETRIC_YCBCR:
3709 case PHOTOMETRIC_RGB:
3714 switch (image_info->interlace)
3719 quantum_type=RGBQuantum;
3720 if (image->alpha_trait != UndefinedPixelTrait)
3721 quantum_type=RGBAQuantum;
3722 for (y=0; y < (ssize_t) image->rows; y++)
3724 register const Quantum
3727 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3728 if (p == (const Quantum *) NULL)
3730 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3731 quantum_type,pixels,exception);
3733 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3735 if (image->previous == (Image *) NULL)
3737 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3739 if (status == MagickFalse)
3745 case PlaneInterlace:
3746 case PartitionInterlace:
3749 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3751 for (y=0; y < (ssize_t) image->rows; y++)
3753 register const Quantum
3756 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3757 if (p == (const Quantum *) NULL)
3759 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3760 RedQuantum,pixels,exception);
3761 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3764 if (image->previous == (Image *) NULL)
3766 status=SetImageProgress(image,SaveImageTag,100,400);
3767 if (status == MagickFalse)
3770 for (y=0; y < (ssize_t) image->rows; y++)
3772 register const Quantum
3775 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3776 if (p == (const Quantum *) NULL)
3778 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3779 GreenQuantum,pixels,exception);
3780 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3783 if (image->previous == (Image *) NULL)
3785 status=SetImageProgress(image,SaveImageTag,200,400);
3786 if (status == MagickFalse)
3789 for (y=0; y < (ssize_t) image->rows; y++)
3791 register const Quantum
3794 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3795 if (p == (const Quantum *) NULL)
3797 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3798 BlueQuantum,pixels,exception);
3799 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3802 if (image->previous == (Image *) NULL)
3804 status=SetImageProgress(image,SaveImageTag,300,400);
3805 if (status == MagickFalse)
3808 if (image->alpha_trait != UndefinedPixelTrait)
3809 for (y=0; y < (ssize_t) image->rows; y++)
3811 register const Quantum
3814 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3815 if (p == (const Quantum *) NULL)
3817 length=ExportQuantumPixels(image,(CacheView *) NULL,
3818 quantum_info,AlphaQuantum,pixels,exception);
3819 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3822 if (image->previous == (Image *) NULL)
3824 status=SetImageProgress(image,SaveImageTag,400,400);
3825 if (status == MagickFalse)
3833 case PHOTOMETRIC_SEPARATED:
3838 quantum_type=CMYKQuantum;
3839 if (image->alpha_trait != UndefinedPixelTrait)
3840 quantum_type=CMYKAQuantum;
3841 if (image->colorspace != CMYKColorspace)
3842 (void) TransformImageColorspace(image,CMYKColorspace,exception);
3843 for (y=0; y < (ssize_t) image->rows; y++)
3845 register const Quantum
3848 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3849 if (p == (const Quantum *) NULL)
3851 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3852 quantum_type,pixels,exception);
3853 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3855 if (image->previous == (Image *) NULL)
3857 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3859 if (status == MagickFalse)
3865 case PHOTOMETRIC_PALETTE:
3873 Colormapped TIFF image.
3875 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3876 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3877 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3878 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3879 (blue == (uint16 *) NULL))
3880 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3882 Initialize TIFF colormap.
3884 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3885 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3886 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3887 for (i=0; i < (ssize_t) image->colors; i++)
3889 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3890 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3891 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3893 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3894 red=(uint16 *) RelinquishMagickMemory(red);
3895 green=(uint16 *) RelinquishMagickMemory(green);
3896 blue=(uint16 *) RelinquishMagickMemory(blue);
3901 Convert PseudoClass packets to contiguous grayscale scanlines.
3903 quantum_type=IndexQuantum;
3904 if (image->alpha_trait != UndefinedPixelTrait)
3906 if (photometric != PHOTOMETRIC_PALETTE)
3907 quantum_type=GrayAlphaQuantum;
3909 quantum_type=IndexAlphaQuantum;
3912 if (photometric != PHOTOMETRIC_PALETTE)
3913 quantum_type=GrayQuantum;
3914 for (y=0; y < (ssize_t) image->rows; y++)
3916 register const Quantum
3919 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3920 if (p == (const Quantum *) NULL)
3922 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3923 quantum_type,pixels,exception);
3924 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3926 if (image->previous == (Image *) NULL)
3928 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3930 if (status == MagickFalse)
3937 quantum_info=DestroyQuantumInfo(quantum_info);
3938 if (image->colorspace == LabColorspace)
3939 DecodeLabImage(image,exception);
3940 DestroyTIFFInfo(&tiff_info);
3941 DisableMSCWarning(4127)
3942 if (0 && (image_info->verbose != MagickFalse))
3944 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3945 (void) TIFFWriteDirectory(tiff);
3946 image=SyncNextImageInList(image);
3947 if (image == (Image *) NULL)
3949 status=SetImageProgress(image,SaveImagesTag,scene++,
3950 GetImageListLength(image));
3951 if (status == MagickFalse)
3953 } while (image_info->adjoin != MagickFalse);