2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % TTTTT IIIII FFFFF FFFFF %
13 % Read/Write TIFF Image Format %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/artifact.h"
44 #include "MagickCore/attribute.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/color.h"
49 #include "MagickCore/color-private.h"
50 #include "MagickCore/colormap.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/constitute.h"
54 #include "MagickCore/enhance.h"
55 #include "MagickCore/exception.h"
56 #include "MagickCore/exception-private.h"
57 #include "MagickCore/geometry.h"
58 #include "MagickCore/image.h"
59 #include "MagickCore/image-private.h"
60 #include "MagickCore/list.h"
61 #include "MagickCore/log.h"
62 #include "MagickCore/magick.h"
63 #include "MagickCore/memory_.h"
64 #include "MagickCore/module.h"
65 #include "MagickCore/monitor.h"
66 #include "MagickCore/monitor-private.h"
67 #include "MagickCore/option.h"
68 #include "MagickCore/pixel-accessor.h"
69 #include "MagickCore/property.h"
70 #include "MagickCore/quantum.h"
71 #include "MagickCore/quantum-private.h"
72 #include "MagickCore/profile.h"
73 #include "MagickCore/resize.h"
74 #include "MagickCore/resource_.h"
75 #include "MagickCore/semaphore.h"
76 #include "MagickCore/splay-tree.h"
77 #include "MagickCore/static.h"
78 #include "MagickCore/statistic.h"
79 #include "MagickCore/string_.h"
80 #include "MagickCore/string-private.h"
81 #include "MagickCore/thread_.h"
82 #include "MagickCore/token.h"
83 #include "MagickCore/utility.h"
84 #if defined(MAGICKCORE_TIFF_DELEGATE)
85 # if defined(MAGICKCORE_HAVE_TIFFCONF_H)
86 # include "tiffconf.h"
90 # if !defined(COMPRESSION_ADOBE_DEFLATE)
91 # define COMPRESSION_ADOBE_DEFLATE 8
93 # if !defined(PREDICTOR_HORIZONTAL)
94 # define PREDICTOR_HORIZONTAL 2
96 # if !defined(TIFFTAG_COPYRIGHT)
97 # define TIFFTAG_COPYRIGHT 33432
99 # if !defined(TIFFTAG_OPIIMAGEID)
100 # define TIFF_OPIIMAGEID 32781
104 Typedef declarations.
106 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
107 typedef struct _ExifInfo
117 static const ExifInfo
119 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
120 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
121 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
122 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
123 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
124 { EXIFTAG_OECF, TIFF_NOTYPE, "exif:OptoelectricConversionFactor" },
125 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, "exif:ExifVersion" },
126 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
127 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
128 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, "exif:ComponentsConfiguration" },
129 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
130 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
131 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
132 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
133 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
134 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
135 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
136 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
137 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
138 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
139 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
140 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, "exif:SubjectArea" },
141 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, "exif:MakerNote" },
142 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, "exif:UserComment" },
143 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
144 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
145 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
146 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, "exif:FlashpixVersion" },
147 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
148 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
149 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
150 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
151 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
152 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
153 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, "exif:SpatialFrequencyResponse" },
154 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
155 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
156 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
157 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
158 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
159 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
160 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, "exif:FileSource" },
161 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, "exif:SceneType" },
162 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, "exif:CFAPattern" },
163 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
164 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
165 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
166 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
167 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
168 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
169 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
170 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
171 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
172 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
173 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, "exif:DeviceSettingDescription" },
174 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
175 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
176 { 0, 0, (char *) NULL }
179 #endif /* MAGICKCORE_TIFF_DELEGATE */
184 static MagickThreadKey
188 *tiff_semaphore = (SemaphoreInfo *) NULL;
190 static volatile MagickBooleanType
191 instantiate_key = MagickFalse;
194 Forward declarations.
196 #if defined(MAGICKCORE_TIFF_DELEGATE)
198 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
200 static MagickBooleanType
201 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
202 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
203 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % IsTIFF() returns MagickTrue if the image format type, identified by the
218 % magick string, is TIFF.
220 % The format of the IsTIFF method is:
222 % MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
224 % A description of each parameter follows:
226 % o magick: compare image format pattern against these bytes.
228 % o length: Specifies the length of the magick string.
231 static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
235 if (memcmp(magick,"\115\115\000\052",4) == 0)
237 if (memcmp(magick,"\111\111\052\000",4) == 0)
239 #if defined(TIFF_VERSION_BIG)
242 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
244 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
250 #if defined(MAGICKCORE_TIFF_DELEGATE)
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256 % R e a d G R O U P 4 I m a g e %
260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 % ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
263 % allocates the memory necessary for the new Image structure and returns a
264 % pointer to the new image.
266 % The format of the ReadGROUP4Image method is:
268 % Image *ReadGROUP4Image(const ImageInfo *image_info,
269 % ExceptionInfo *exception)
271 % A description of each parameter follows:
273 % o image_info: the image info.
275 % o exception: return any errors or warnings in this structure.
279 static inline size_t WriteLSBLong(FILE *file,const size_t value)
284 buffer[0]=(unsigned char) value;
285 buffer[1]=(unsigned char) (value >> 8);
286 buffer[2]=(unsigned char) (value >> 16);
287 buffer[3]=(unsigned char) (value >> 24);
288 return(fwrite(buffer,1,4,file));
291 static Image *ReadGROUP4Image(const ImageInfo *image_info,
292 ExceptionInfo *exception)
295 filename[MaxTextExtent];
323 assert(image_info != (const ImageInfo *) NULL);
324 assert(image_info->signature == MagickSignature);
325 if (image_info->debug != MagickFalse)
326 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
327 image_info->filename);
328 assert(exception != (ExceptionInfo *) NULL);
329 assert(exception->signature == MagickSignature);
330 image=AcquireImage(image_info,exception);
331 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
332 if (status == MagickFalse)
334 image=DestroyImageList(image);
335 return((Image *) NULL);
338 Write raw CCITT Group 4 wrapped as a TIFF image file.
341 unique_file=AcquireUniqueFileResource(filename);
342 if (unique_file != -1)
343 file=fdopen(unique_file,"wb");
344 if ((unique_file == -1) || (file == (FILE *) NULL))
345 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
346 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
347 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
348 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
349 length=WriteLSBLong(file,image->columns);
350 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
351 length=WriteLSBLong(file,image->rows);
352 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
353 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
354 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
355 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
356 strip_offset=10+(12*14)+4+8;
357 length=WriteLSBLong(file,(size_t) strip_offset);
358 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
359 length=WriteLSBLong(file,(size_t) image_info->orientation);
360 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
361 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
362 length=WriteLSBLong(file,image->rows);
363 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
364 offset=(ssize_t) ftell(file)-4;
365 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
366 length=WriteLSBLong(file,(size_t) (strip_offset-8));
367 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
368 length=WriteLSBLong(file,(size_t) (strip_offset-8));
369 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
370 length=fwrite("\000\000\000\000",1,4,file);
371 length=WriteLSBLong(file,(long) image->resolution.x);
372 length=WriteLSBLong(file,1);
373 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
374 (void) fputc(c,file);
375 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
376 length=WriteLSBLong(file,(unsigned int) length);
378 (void) CloseBlob(image);
379 image=DestroyImage(image);
383 read_info=CloneImageInfo((ImageInfo *) NULL);
384 (void) FormatLocaleString(read_info->filename,MaxTextExtent,"%s",filename);
385 image=ReadTIFFImage(read_info,exception);
386 read_info=DestroyImageInfo(read_info);
387 if (image != (Image *) NULL)
389 (void) CopyMagickString(image->filename,image_info->filename,
391 (void) CopyMagickString(image->magick_filename,image_info->filename,
393 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
395 (void) RelinquishUniqueFileResource(filename);
400 #if defined(MAGICKCORE_TIFF_DELEGATE)
402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 % R e a d T I F F I m a g e %
410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
412 % ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
413 % memory necessary for the new Image structure and returns a pointer to the
416 % The format of the ReadTIFFImage method is:
418 % Image *ReadTIFFImage(const ImageInfo *image_info,
419 % ExceptionInfo *exception)
421 % A description of each parameter follows:
423 % o image_info: the image info.
425 % o exception: return any errors or warnings in this structure.
429 #if defined(MAGICKCORE_HDRI_SUPPORT)
430 static MagickBooleanType LabImage(Image *image,ExceptionInfo *exception)
442 image_view=AcquireAuthenticCacheView(image,exception);
443 for (y=0; y < (ssize_t) image->rows; y++)
451 if (status == MagickFalse)
453 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
454 if (q == (Quantum *) NULL)
459 for (x=0; x < (ssize_t) image->columns; x++)
461 if ((QuantumScale*GetPixela(image,q)) > 0.5)
462 SetPixela(image,QuantumRange*(QuantumScale*GetPixela(image,q)-1.0),q);
463 if ((QuantumScale*GetPixelb(image,q)) > 0.5)
464 SetPixelb(image,QuantumRange*(QuantumScale*GetPixelb(image,q)-1.0),q);
465 q+=GetPixelChannels(image);
467 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
470 image_view=DestroyCacheView(image_view);
475 static inline size_t MagickMax(const size_t x,const size_t y)
482 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
489 static MagickBooleanType ReadProfile(Image *image,const char *name,
490 unsigned char *datum,ssize_t length,ExceptionInfo *exception)
504 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
506 for (i=0; i < (length-4); i+=2)
507 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
516 profile=BlobToStringInfo(datum+i,(size_t) length);
517 if (profile == (StringInfo *) NULL)
518 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
520 status=SetImageProfile(image,name,profile,exception);
521 profile=DestroyStringInfo(profile);
522 if (status == MagickFalse)
523 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
528 #if defined(__cplusplus) || defined(c_plusplus)
532 static int TIFFCloseBlob(thandle_t image)
534 (void) CloseBlob((Image *) image);
538 static void TIFFErrors(const char *module,const char *format,va_list error)
541 message[MaxTextExtent];
546 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
547 (void) vsnprintf(message,MaxTextExtent,format,error);
549 (void) vsprintf(message,format,error);
551 (void) ConcatenateMagickString(message,".",MaxTextExtent);
552 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
553 if (exception != (ExceptionInfo *) NULL)
554 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
558 static toff_t TIFFGetBlobSize(thandle_t image)
560 return((toff_t) GetBlobSize((Image *) image));
563 static void TIFFGetProfiles(TIFF *tiff,Image *image,ExceptionInfo *exception)
571 #if defined(TIFFTAG_ICCPROFILE)
573 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
574 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
576 #if defined(TIFFTAG_PHOTOSHOP)
578 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
579 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
581 #if defined(TIFFTAG_RICHTIFFIPTC)
583 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
585 if (TIFFIsByteSwapped(tiff) != 0)
586 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
587 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
590 #if defined(TIFFTAG_XMLPACKET)
592 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
593 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
596 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
597 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
600 static void TIFFGetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
603 message[MaxTextExtent],
609 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
610 (void) SetImageProperty(image,"tiff:artist",text,exception);
611 if (TIFFGetField(tiff,TIFFTAG_COPYRIGHT,&text) == 1)
612 (void) SetImageProperty(image,"tiff:copyright",text,exception);
613 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
614 (void) SetImageProperty(image,"tiff:timestamp",text,exception);
615 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
616 (void) SetImageProperty(image,"tiff:document",text,exception);
617 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
618 (void) SetImageProperty(image,"tiff:hostcomputer",text,exception);
619 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
620 (void) SetImageProperty(image,"comment",text,exception);
621 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
622 (void) SetImageProperty(image,"tiff:make",text,exception);
623 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
624 (void) SetImageProperty(image,"tiff:model",text,exception);
625 if (TIFFGetField(tiff,TIFFTAG_OPIIMAGEID,&count,&text) == 1)
627 if (count >= MaxTextExtent)
628 count=MaxTextExtent-1;
629 (void) CopyMagickString(message,text,count+1);
630 (void) SetImageProperty(image,"tiff:image-id",message,exception);
632 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
633 (void) SetImageProperty(image,"label",text,exception);
634 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
635 (void) SetImageProperty(image,"tiff:software",text,exception);
636 if (TIFFGetField(tiff,33423,&count,&text) == 1)
638 if (count >= MaxTextExtent)
639 count=MaxTextExtent-1;
640 (void) CopyMagickString(message,text,count+1);
641 (void) SetImageProperty(image,"tiff:kodak-33423",message,exception);
643 if (TIFFGetField(tiff,36867,&count,&text) == 1)
645 if (count >= MaxTextExtent)
646 count=MaxTextExtent-1;
647 (void) CopyMagickString(message,text,count+1);
648 (void) SetImageProperty(image,"tiff:kodak-36867",message,exception);
652 static void TIFFGetEXIFProperties(TIFF *tiff,Image *image,
653 ExceptionInfo *exception)
655 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
657 value[MaxTextExtent];
665 #if defined(TIFF_VERSION_BIG)
676 Read EXIF properties.
679 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
681 directory=TIFFCurrentDirectory(tiff);
682 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
685 for (i=0; exif_info[i].tag != 0; i++)
688 switch (exif_info[i].type)
696 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans,&sans) != 0) &&
697 (ascii != (char *) NULL) && (*ascii != '\0'))
698 (void) CopyMagickString(value,ascii,MaxTextExtent);
706 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans,&sans) != 0)
707 (void) FormatLocaleString(value,MaxTextExtent,"%d",(int) shorty[0]);
715 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans,&sans) != 0)
716 (void) FormatLocaleString(value,MaxTextExtent,"%d",longy);
719 #if defined(TIFF_VERSION_BIG)
725 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans,&sans) != 0)
726 (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double)
727 ((MagickOffsetType) longy));
739 if (TIFFGetField(tiff,exif_info[i].tag,&rational,&sans,&sans) != 0)
740 (void) FormatLocaleString(value,MaxTextExtent,"%g",rational[0]);
747 (void) SetImageProperty(image,exif_info[i].property,value,exception);
749 TIFFSetDirectory(tiff,directory);
756 static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
758 *base=(tdata_t *) GetBlobStreamData((Image *) image);
759 if (*base != (tdata_t *) NULL)
760 *size=(toff_t) GetBlobSize((Image *) image);
761 if (*base != (tdata_t *) NULL)
766 static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
771 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
772 (unsigned char *) data);
776 static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
777 tsample_t sample,ssize_t row,tdata_t scanline)
782 (void) bits_per_sample;
783 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
787 static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
789 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
792 static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
799 static void TIFFWarnings(const char *module,const char *format,va_list warning)
802 message[MaxTextExtent];
807 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
808 (void) vsnprintf(message,MaxTextExtent,format,warning);
810 (void) vsprintf(message,format,warning);
812 (void) ConcatenateMagickString(message,".",MaxTextExtent);
813 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
814 if (exception != (ExceptionInfo *) NULL)
815 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
816 message,"`%s'",module);
819 static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
824 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
825 (unsigned char *) data);
829 #if defined(__cplusplus) || defined(c_plusplus)
833 static Image *ReadTIFFImage(const ImageInfo *image_info,
834 ExceptionInfo *exception)
838 ReadSingleSampleMethod,
921 assert(image_info != (const ImageInfo *) NULL);
922 assert(image_info->signature == MagickSignature);
923 if (image_info->debug != MagickFalse)
924 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
925 image_info->filename);
926 assert(exception != (ExceptionInfo *) NULL);
927 assert(exception->signature == MagickSignature);
928 image=AcquireImage(image_info,exception);
929 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
930 if (status == MagickFalse)
932 image=DestroyImageList(image);
933 return((Image *) NULL);
935 (void) MagickSetThreadValue(tiff_exception,exception);
936 error_handler=TIFFSetErrorHandler(TIFFErrors);
937 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
938 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
939 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
941 if (tiff == (TIFF *) NULL)
943 (void) TIFFSetWarningHandler(warning_handler);
944 (void) TIFFSetErrorHandler(error_handler);
945 image=DestroyImageList(image);
946 return((Image *) NULL);
948 debug=IsEventLogging();
950 if (image_info->number_scenes != 0)
953 Generate blank images for subimage specification (e.g. image.tif[4].
955 for (i=0; i < (ssize_t) image_info->scene; i++)
957 (void) TIFFReadDirectory(tiff);
958 AcquireNextImage(image_info,image,exception);
959 if (GetNextImageInList(image) == (Image *) NULL)
961 image=DestroyImageList(image);
962 return((Image *) NULL);
964 image=SyncNextImageInList(image);
969 if (0 && (image_info->verbose != MagickFalse))
970 TIFFPrintDirectory(tiff,stdout,MagickFalse);
971 #if defined(MAGICKCORE_HAVE_TIFFISBIGENDIAN)
972 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
973 "lsb" : "msb",exception);
975 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
976 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
977 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
978 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
979 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
980 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
981 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
982 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
983 if (sample_format == SAMPLEFORMAT_IEEEFP)
984 (void) SetImageProperty(image,"quantum:format","floating-point",
986 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
987 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
988 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
991 case PHOTOMETRIC_MINISBLACK:
993 (void) SetImageProperty(image,"tiff:photometric","min-is-black",
997 case PHOTOMETRIC_MINISWHITE:
999 (void) SetImageProperty(image,"tiff:photometric","min-is-white",
1003 case PHOTOMETRIC_PALETTE:
1005 (void) SetImageProperty(image,"tiff:photometric","palette",exception);
1008 case PHOTOMETRIC_RGB:
1010 (void) SetImageProperty(image,"tiff:photometric","RGB",exception);
1013 case PHOTOMETRIC_CIELAB:
1015 (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception);
1018 case PHOTOMETRIC_SEPARATED:
1020 (void) SetImageProperty(image,"tiff:photometric","separated",exception);
1023 case PHOTOMETRIC_YCBCR:
1025 (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception);
1030 (void) SetImageProperty(image,"tiff:photometric","unknown",exception);
1034 if (image->debug != MagickFalse)
1036 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
1037 (unsigned int) width,(unsigned int) height);
1038 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
1040 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1041 "Bits per sample: %u",bits_per_sample);
1042 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1043 "Min sample value: %u",min_sample_value);
1044 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1045 "Max sample value: %u",max_sample_value);
1046 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
1047 "interpretation: %s",GetImageProperty(image,"tiff:photometric",
1050 image->columns=(size_t) width;
1051 image->rows=(size_t) height;
1052 image->depth=(size_t) bits_per_sample;
1053 if (image->debug != MagickFalse)
1054 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
1055 (double) image->depth);
1056 image->endian=MSBEndian;
1057 if (endian == FILLORDER_LSB2MSB)
1058 image->endian=LSBEndian;
1059 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1060 (photometric == PHOTOMETRIC_MINISWHITE))
1061 SetImageColorspace(image,GRAYColorspace,exception);
1062 if (photometric == PHOTOMETRIC_SEPARATED)
1063 SetImageColorspace(image,CMYKColorspace,exception);
1064 if (photometric == PHOTOMETRIC_CIELAB)
1065 SetImageColorspace(image,LabColorspace,exception);
1066 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
1067 &samples_per_pixel);
1068 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
1069 x_resolution=(float) image->resolution.x;
1070 y_resolution=(float) image->resolution.y;
1071 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
1072 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
1073 image->resolution.x=x_resolution;
1074 image->resolution.y=y_resolution;
1075 x_position=(float) image->page.x/x_resolution;
1076 y_position=(float) image->page.y/y_resolution;
1077 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
1078 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
1079 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
1080 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
1081 image->orientation=(OrientationType) orientation;
1082 chromaticity=(float *) NULL;
1083 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
1084 if (chromaticity != (float *) NULL)
1086 image->chromaticity.white_point.x=chromaticity[0];
1087 image->chromaticity.white_point.y=chromaticity[1];
1089 chromaticity=(float *) NULL;
1090 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
1091 if (chromaticity != (float *) NULL)
1093 image->chromaticity.red_primary.x=chromaticity[0];
1094 image->chromaticity.red_primary.y=chromaticity[1];
1095 image->chromaticity.green_primary.x=chromaticity[2];
1096 image->chromaticity.green_primary.y=chromaticity[3];
1097 image->chromaticity.blue_primary.x=chromaticity[4];
1098 image->chromaticity.blue_primary.y=chromaticity[5];
1100 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1101 if ((compress_tag != COMPRESSION_NONE) &&
1102 (TIFFIsCODECConfigured(compress_tag) == 0))
1105 ThrowReaderException(CoderError,"CompressNotSupported");
1108 switch (compress_tag)
1110 case COMPRESSION_NONE: image->compression=NoCompression; break;
1111 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1112 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1113 case COMPRESSION_JPEG:
1115 image->compression=JPEGCompression;
1116 #if defined(JPEG_SUPPORT)
1119 sampling_factor[MaxTextExtent];
1125 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1126 &horizontal,&vertical);
1127 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
1128 horizontal,vertical);
1129 (void) SetImageProperty(image,"jpeg:sampling-factor",
1130 sampling_factor,exception);
1131 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1132 "Sampling Factors: %s",sampling_factor);
1133 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1135 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1137 photometric=PHOTOMETRIC_RGB;
1143 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1144 #if defined(COMPRESSION_LZMA)
1145 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1147 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1148 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1149 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1150 default: image->compression=RLECompression; break;
1152 TIFFGetProfiles(tiff,image,exception);
1153 TIFFGetProperties(tiff,image,exception);
1154 option=GetImageOption(image_info,"tiff:exif-properties");
1155 if (IfMagickTrue(IsStringNotFalse(option))) /* enabled by default */
1156 TIFFGetEXIFProperties(tiff,image,exception);
1158 Allocate memory for the image and pixel buffer.
1160 quantum_info=AcquireQuantumInfo(image_info,image);
1161 if (quantum_info == (QuantumInfo *) NULL)
1164 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1166 if (sample_format == SAMPLEFORMAT_UINT)
1167 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1168 if (sample_format == SAMPLEFORMAT_INT)
1169 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1170 if (sample_format == SAMPLEFORMAT_IEEEFP)
1171 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1172 if (status == MagickFalse)
1174 quantum_info=DestroyQuantumInfo(quantum_info);
1176 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1179 switch (photometric)
1181 case PHOTOMETRIC_MINISBLACK:
1183 quantum_info->min_is_white=MagickFalse;
1186 case PHOTOMETRIC_MINISWHITE:
1188 quantum_info->min_is_white=MagickTrue;
1194 associated_alpha=MagickFalse;
1196 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1198 if (extra_samples == 0)
1200 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1201 image->matte=MagickTrue;
1204 for (i=0; i < extra_samples; i++)
1206 image->matte=MagickTrue;
1207 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
1208 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
1210 option=GetImageOption(image_info,"tiff:alpha");
1211 if (option != (const char *) NULL)
1212 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1214 if (image->matte != MagickFalse)
1215 (void) SetImageProperty(image,"tiff:alpha",
1216 associated_alpha != MagickFalse ? "associated" : "unassociated",
1218 if ((photometric == PHOTOMETRIC_PALETTE) &&
1219 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1224 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
1225 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
1228 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1231 if (units == RESUNIT_INCH)
1232 image->units=PixelsPerInchResolution;
1233 if (units == RESUNIT_CENTIMETER)
1234 image->units=PixelsPerCentimeterResolution;
1235 value=(unsigned short) image->scene;
1236 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1238 if (image_info->ping != MagickFalse)
1240 if (image_info->number_scenes != 0)
1241 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1243 goto next_tiff_frame;
1245 method=ReadGenericMethod;
1246 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1249 value[MaxTextExtent];
1251 method=ReadStripMethod;
1252 (void) FormatLocaleString(value,MaxTextExtent,"%u",
1253 (unsigned int) rows_per_strip);
1254 (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception);
1256 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1257 method=ReadRGBAMethod;
1258 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1259 method=ReadCMYKAMethod;
1260 if ((photometric != PHOTOMETRIC_RGB) &&
1261 (photometric != PHOTOMETRIC_CIELAB) &&
1262 (photometric != PHOTOMETRIC_SEPARATED))
1263 method=ReadGenericMethod;
1264 if (image->storage_class == PseudoClass)
1265 method=ReadSingleSampleMethod;
1266 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1267 (photometric == PHOTOMETRIC_MINISWHITE))
1268 method=ReadSingleSampleMethod;
1269 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1270 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
1271 method=ReadGenericMethod;
1272 if (TIFFIsTiled(tiff) != MagickFalse)
1273 method=ReadTileMethod;
1274 quantum_info->endian=LSBEndian;
1275 quantum_type=RGBQuantum;
1276 pixels=GetQuantumPixels(quantum_info);
1279 case ReadSingleSampleMethod:
1282 Convert TIFF image to PseudoClass MIFF image.
1284 if ((image->storage_class == PseudoClass) &&
1285 (photometric == PHOTOMETRIC_PALETTE))
1296 Initialize colormap.
1298 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1299 &green_colormap,&blue_colormap);
1300 range=255; /* might be old style 8-bit colormap */
1301 for (i=0; i < (ssize_t) image->colors; i++)
1302 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1303 (blue_colormap[i] >= 256))
1308 for (i=0; i < (ssize_t) image->colors; i++)
1310 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
1311 red_colormap[i])/range);
1312 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
1313 green_colormap[i])/range);
1314 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
1315 blue_colormap[i])/range);
1318 quantum_type=IndexQuantum;
1319 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1320 if (image->matte != MagickFalse)
1322 if (image->storage_class != PseudoClass)
1324 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1326 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1330 quantum_type=IndexAlphaQuantum;
1331 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1335 if (image->storage_class != PseudoClass)
1337 quantum_type=GrayQuantum;
1338 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1340 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1341 if (status == MagickFalse)
1344 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1346 pixels=GetQuantumPixels(quantum_info);
1347 for (y=0; y < (ssize_t) image->rows; y++)
1355 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1358 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1359 if (q == (Quantum *) NULL)
1361 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1362 quantum_type,pixels,exception);
1364 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1366 if (image->previous == (Image *) NULL)
1368 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1370 if (status == MagickFalse)
1376 case ReadRGBAMethod:
1379 Convert TIFF image to DirectClass MIFF image.
1381 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1382 quantum_type=RGBQuantum;
1383 if (image->matte != MagickFalse)
1385 quantum_type=RGBAQuantum;
1386 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1388 if (image->colorspace == CMYKColorspace)
1390 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1391 quantum_type=CMYKQuantum;
1392 if (image->matte != MagickFalse)
1394 quantum_type=CMYKAQuantum;
1395 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1398 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1399 if (status == MagickFalse)
1402 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1404 pixels=GetQuantumPixels(quantum_info);
1405 for (y=0; y < (ssize_t) image->rows; y++)
1413 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1416 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1417 if (q == (Quantum *) NULL)
1419 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1420 quantum_type,pixels,exception);
1421 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1423 if (image->previous == (Image *) NULL)
1425 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1427 if (status == MagickFalse)
1433 case ReadCMYKAMethod:
1436 Convert TIFF image to DirectClass MIFF image.
1438 for (i=0; i < (ssize_t) samples_per_pixel; i++)
1440 for (y=0; y < (ssize_t) image->rows; y++)
1448 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1452 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1453 if (q == (Quantum *) NULL)
1455 if (image->colorspace != CMYKColorspace)
1458 case 0: quantum_type=RedQuantum; break;
1459 case 1: quantum_type=GreenQuantum; break;
1460 case 2: quantum_type=BlueQuantum; break;
1461 case 3: quantum_type=AlphaQuantum; break;
1462 default: quantum_type=UndefinedQuantum; break;
1467 case 0: quantum_type=CyanQuantum; break;
1468 case 1: quantum_type=MagentaQuantum; break;
1469 case 2: quantum_type=YellowQuantum; break;
1470 case 3: quantum_type=BlackQuantum; break;
1471 case 4: quantum_type=AlphaQuantum; break;
1472 default: quantum_type=UndefinedQuantum; break;
1474 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1475 quantum_type,pixels,exception);
1476 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1479 if (image->previous == (Image *) NULL)
1481 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1483 if (status == MagickFalse)
1489 case ReadStripMethod:
1495 Convert stripped TIFF image to DirectClass MIFF image.
1499 for (y=0; y < (ssize_t) image->rows; y++)
1507 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1508 if (q == (Quantum *) NULL)
1512 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1514 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1518 p=((uint32 *) pixels)+image->columns*i;
1519 for (x=0; x < (ssize_t) image->columns; x++)
1521 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1523 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1525 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1527 if (image->matte != MagickFalse)
1528 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1531 q+=GetPixelChannels(image);
1533 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1535 if (image->previous == (Image *) NULL)
1537 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1539 if (status == MagickFalse)
1545 case ReadTileMethod:
1559 Convert tiled TIFF image to DirectClass MIFF image.
1561 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1562 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1565 ThrowReaderException(CoderError,"ImageIsNotTiled");
1567 (void) SetImageStorageClass(image,DirectClass,exception);
1568 number_pixels=columns*rows;
1569 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
1570 sizeof(*tile_pixels));
1571 if (tile_pixels == (uint32 *) NULL)
1574 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1576 for (y=0; y < (ssize_t) image->rows; y+=rows)
1589 rows_remaining=image->rows-y;
1590 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
1591 rows_remaining=rows;
1592 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1594 if (tile == (Quantum *) NULL)
1596 for (x=0; x < (ssize_t) image->columns; x+=columns)
1602 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1604 columns_remaining=image->columns-x;
1605 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
1606 columns_remaining=columns;
1607 p=tile_pixels+(rows-rows_remaining)*columns;
1608 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1610 for (row=rows_remaining; row > 0; row--)
1612 if (image->matte != MagickFalse)
1613 for (column=columns_remaining; column > 0; column--)
1615 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1617 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1619 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1621 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1624 q+=GetPixelChannels(image);
1627 for (column=columns_remaining; column > 0; column--)
1629 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1631 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1633 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1636 q+=GetPixelChannels(image);
1638 p+=columns-columns_remaining;
1639 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
1642 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1644 if (image->previous == (Image *) NULL)
1646 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1648 if (status == MagickFalse)
1652 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1655 case ReadGenericMethod:
1665 Convert TIFF image to DirectClass MIFF image.
1667 number_pixels=(MagickSizeType) image->columns*image->rows;
1668 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1669 (number_pixels*sizeof(uint32))))
1672 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1674 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1676 if (pixels == (uint32 *) NULL)
1679 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1681 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1682 (uint32) image->rows,(uint32 *) pixels,0);
1684 Convert image to DirectClass pixel packets.
1686 p=pixels+number_pixels-1;
1687 for (y=0; y < (ssize_t) image->rows; y++)
1695 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1696 if (q == (Quantum *) NULL)
1698 q+=GetPixelChannels(image)*(image->columns-1);
1699 for (x=0; x < (ssize_t) image->columns; x++)
1701 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1703 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1705 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1707 if (image->matte != MagickFalse)
1708 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1711 q-=GetPixelChannels(image);;
1713 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1715 if (image->previous == (Image *) NULL)
1717 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1719 if (status == MagickFalse)
1723 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1727 SetQuantumImageType(image,quantum_type);
1729 quantum_info=DestroyQuantumInfo(quantum_info);
1730 #if defined(MAGICKCORE_HDRI_SUPPORT)
1731 if (photometric == PHOTOMETRIC_CIELAB)
1732 LabImage(image,exception);
1734 if ((photometric == PHOTOMETRIC_LOGL) ||
1735 (photometric == PHOTOMETRIC_MINISBLACK) ||
1736 (photometric == PHOTOMETRIC_MINISWHITE))
1738 image->type=GrayscaleType;
1739 if (bits_per_sample == 1)
1740 image->type=BilevelType;
1742 if (image->storage_class == PseudoClass)
1743 image->depth=GetImageDepth(image,exception);
1744 if ((photometric == PHOTOMETRIC_LOGL) ||
1745 (photometric == PHOTOMETRIC_MINISBLACK) ||
1746 (photometric == PHOTOMETRIC_MINISWHITE))
1748 image->type=GrayscaleType;
1749 if (bits_per_sample == 1)
1750 image->type=BilevelType;
1753 Proceed to next image.
1755 if (image_info->number_scenes != 0)
1756 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1758 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1759 if (status == MagickTrue)
1762 Allocate next image structure.
1764 AcquireNextImage(image_info,image,exception);
1765 if (GetNextImageInList(image) == (Image *) NULL)
1767 image=DestroyImageList(image);
1768 return((Image *) NULL);
1770 image=SyncNextImageInList(image);
1771 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1773 if (status == MagickFalse)
1776 } while (status == MagickTrue);
1777 (void) TIFFSetWarningHandler(warning_handler);
1778 (void) TIFFSetErrorHandler(error_handler);
1780 return(GetFirstImageInList(image));
1785 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1789 % R e g i s t e r T I F F I m a g e %
1793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1795 % RegisterTIFFImage() adds properties for the TIFF image format to
1796 % the list of supported formats. The properties include the image format
1797 % tag, a method to read and/or write the format, whether the format
1798 % supports the saving of more than one frame to the same file or blob,
1799 % whether the format supports native in-memory I/O, and a brief
1800 % description of the format.
1802 % The format of the RegisterTIFFImage method is:
1804 % size_t RegisterTIFFImage(void)
1808 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
1809 static TIFFExtendProc
1810 tag_extender = (TIFFExtendProc) NULL;
1812 static void TIFFTagExtender(TIFF *tiff)
1814 static const TIFFFieldInfo
1818 37724, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1,
1819 (char *) "PhotoshopLayerData"
1823 TIFFMergeFieldInfo(tiff,TIFFExtensions,sizeof(TIFFExtensions)/
1824 sizeof(*TIFFExtensions));
1825 if (tag_extender != (TIFFExtendProc) NULL)
1826 (*tag_extender)(tiff);
1830 ModuleExport size_t RegisterTIFFImage(void)
1832 #define TIFFDescription "Tagged Image File Format"
1835 version[MaxTextExtent];
1840 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1841 tiff_semaphore=AllocateSemaphoreInfo();
1842 LockSemaphoreInfo(tiff_semaphore);
1843 if (instantiate_key == MagickFalse)
1845 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1846 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1847 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
1848 if (tag_extender == (TIFFExtendProc) NULL)
1849 tag_extender=TIFFSetTagExtender(TIFFTagExtender);
1851 instantiate_key=MagickTrue;
1853 UnlockSemaphoreInfo(tiff_semaphore);
1855 #if defined(TIFF_VERSION)
1856 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
1858 #if defined(MAGICKCORE_TIFF_DELEGATE)
1867 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1873 entry=SetMagickInfo("GROUP4");
1874 #if defined(MAGICKCORE_TIFF_DELEGATE)
1875 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1876 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1878 entry->raw=MagickTrue;
1879 entry->endian_support=MagickTrue;
1880 entry->adjoin=MagickFalse;
1881 entry->format_type=ImplicitFormatType;
1882 entry->seekable_stream=MagickTrue;
1883 entry->thread_support=NoThreadSupport;
1884 entry->description=ConstantString("Raw CCITT Group4");
1885 entry->module=ConstantString("TIFF");
1886 (void) RegisterMagickInfo(entry);
1887 entry=SetMagickInfo("PTIF");
1888 #if defined(MAGICKCORE_TIFF_DELEGATE)
1889 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1890 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1892 entry->endian_support=MagickTrue;
1893 entry->seekable_stream=MagickTrue;
1894 entry->thread_support=NoThreadSupport;
1895 entry->description=ConstantString("Pyramid encoded TIFF");
1896 entry->module=ConstantString("TIFF");
1897 (void) RegisterMagickInfo(entry);
1898 entry=SetMagickInfo("TIF");
1899 #if defined(MAGICKCORE_TIFF_DELEGATE)
1900 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1901 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1903 entry->endian_support=MagickTrue;
1904 entry->seekable_stream=MagickTrue;
1905 entry->stealth=MagickTrue;
1906 entry->thread_support=NoThreadSupport;
1907 entry->description=ConstantString(TIFFDescription);
1908 if (*version != '\0')
1909 entry->version=ConstantString(version);
1910 entry->module=ConstantString("TIFF");
1911 (void) RegisterMagickInfo(entry);
1912 entry=SetMagickInfo("TIFF");
1913 #if defined(MAGICKCORE_TIFF_DELEGATE)
1914 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1915 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1917 entry->magick=(IsImageFormatHandler *) IsTIFF;
1918 entry->endian_support=MagickTrue;
1919 entry->seekable_stream=MagickTrue;
1920 entry->thread_support=NoThreadSupport;
1921 entry->description=ConstantString(TIFFDescription);
1922 if (*version != '\0')
1923 entry->version=ConstantString(version);
1924 entry->module=ConstantString("TIFF");
1925 (void) RegisterMagickInfo(entry);
1926 entry=SetMagickInfo("TIFF64");
1927 #if defined(TIFF_VERSION_BIG)
1928 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1929 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1931 entry->adjoin=MagickFalse;
1932 entry->endian_support=MagickTrue;
1933 entry->seekable_stream=MagickTrue;
1934 entry->thread_support=NoThreadSupport;
1935 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1936 if (*version != '\0')
1937 entry->version=ConstantString(version);
1938 entry->module=ConstantString("TIFF");
1939 (void) RegisterMagickInfo(entry);
1940 return(MagickImageCoderSignature);
1944 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1948 % U n r e g i s t e r T I F F I m a g e %
1952 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1954 % UnregisterTIFFImage() removes format registrations made by the TIFF module
1955 % from the list of supported formats.
1957 % The format of the UnregisterTIFFImage method is:
1959 % UnregisterTIFFImage(void)
1962 ModuleExport void UnregisterTIFFImage(void)
1964 (void) UnregisterMagickInfo("TIFF64");
1965 (void) UnregisterMagickInfo("TIFF");
1966 (void) UnregisterMagickInfo("TIF");
1967 (void) UnregisterMagickInfo("PTIF");
1968 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1969 tiff_semaphore=AllocateSemaphoreInfo();
1970 LockSemaphoreInfo(tiff_semaphore);
1971 if (instantiate_key != MagickFalse)
1973 #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER)
1974 if (tag_extender == (TIFFExtendProc) NULL)
1975 (void) TIFFSetTagExtender(tag_extender);
1977 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1978 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1979 instantiate_key=MagickFalse;
1981 UnlockSemaphoreInfo(tiff_semaphore);
1982 DestroySemaphoreInfo(&tiff_semaphore);
1985 #if defined(MAGICKCORE_TIFF_DELEGATE)
1987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1991 % W r i t e G R O U P 4 I m a g e %
1995 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1997 % WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1999 % The format of the WriteGROUP4Image method is:
2001 % MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2002 % Image *image,ExceptionInfo *)
2004 % A description of each parameter follows:
2006 % o image_info: the image info.
2008 % o image: The image.
2010 % o exception: return any errors or warnings in this structure.
2013 static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
2014 Image *image,ExceptionInfo *exception)
2017 filename[MaxTextExtent];
2051 Write image as CCITT Group4 TIFF image to a temporary file.
2053 assert(image_info != (const ImageInfo *) NULL);
2054 assert(image_info->signature == MagickSignature);
2055 assert(image != (Image *) NULL);
2056 assert(image->signature == MagickSignature);
2057 if (image->debug != MagickFalse)
2058 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2059 assert(exception != (ExceptionInfo *) NULL);
2060 assert(exception->signature == MagickSignature);
2061 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2062 if (status == MagickFalse)
2064 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
2065 if (huffman_image == (Image *) NULL)
2067 (void) CloseBlob(image);
2068 return(MagickFalse);
2070 huffman_image->endian=MSBEndian;
2072 unique_file=AcquireUniqueFileResource(filename);
2073 if (unique_file != -1)
2074 file=fdopen(unique_file,"wb");
2075 if ((unique_file == -1) || (file == (FILE *) NULL))
2077 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
2079 return(MagickFalse);
2081 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
2083 (void) SetImageType(huffman_image,BilevelType,exception);
2084 write_info=CloneImageInfo((ImageInfo *) NULL);
2085 SetImageInfoFile(write_info,file);
2086 write_info->compression=Group4Compression;
2087 write_info->type=BilevelType;
2088 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
2089 status=WriteTIFFImage(write_info,huffman_image,exception);
2090 (void) fflush(file);
2091 write_info=DestroyImageInfo(write_info);
2092 if (status == MagickFalse)
2094 huffman_image=DestroyImage(huffman_image);
2095 (void) fclose(file);
2096 (void) RelinquishUniqueFileResource(filename);
2097 return(MagickFalse);
2099 tiff=TIFFOpen(filename,"rb");
2100 if (tiff == (TIFF *) NULL)
2102 huffman_image=DestroyImage(huffman_image);
2103 (void) fclose(file);
2104 (void) RelinquishUniqueFileResource(filename);
2105 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
2106 image_info->filename);
2107 return(MagickFalse);
2110 Allocate raw strip buffer.
2112 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
2115 huffman_image=DestroyImage(huffman_image);
2116 (void) fclose(file);
2117 (void) RelinquishUniqueFileResource(filename);
2118 return(MagickFalse);
2120 strip_size=byte_count[0];
2121 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2122 if (byte_count[i] > strip_size)
2123 strip_size=byte_count[i];
2124 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
2126 if (buffer == (unsigned char *) NULL)
2129 huffman_image=DestroyImage(huffman_image);
2130 (void) fclose(file);
2131 (void) RelinquishUniqueFileResource(filename);
2132 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2133 image_info->filename);
2136 Compress runlength encoded to 2D Huffman pixels.
2138 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2140 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
2141 if (WriteBlob(image,(size_t) count,buffer) != count)
2144 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2146 huffman_image=DestroyImage(huffman_image);
2147 (void) fclose(file);
2148 (void) RelinquishUniqueFileResource(filename);
2149 (void) CloseBlob(image);
2154 #if defined(MAGICKCORE_TIFF_DELEGATE)
2156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2160 % W r i t e P T I F I m a g e %
2164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2166 % WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2169 % The format of the WritePTIFImage method is:
2171 % MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2172 % Image *image,ExceptionInfo *exception)
2174 % A description of each parameter follows:
2176 % o image_info: the image info.
2178 % o image: The image.
2180 % o exception: return any errors or warnings in this structure.
2183 static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2184 Image *image,ExceptionInfo *exception)
2205 Create pyramid-encoded TIFF image.
2207 images=NewImageList();
2208 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2210 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,exception));
2211 columns=next->columns;
2213 resolution=next->resolution;
2214 while ((columns > 64) && (rows > 64))
2220 pyramid_image=ResizeImage(next,columns,rows,image->filter,exception);
2221 if (pyramid_image == (Image *) NULL)
2223 pyramid_image->resolution=resolution;
2224 AppendImageToList(&images,pyramid_image);
2227 images=GetFirstImageInList(images);
2229 Write pyramid-encoded TIFF image.
2231 write_info=CloneImageInfo(image_info);
2232 write_info->adjoin=MagickTrue;
2233 (void) CopyMagickString(write_info->magick,"TIFF",MaxTextExtent);
2234 (void) CopyMagickString(images->magick,"TIFF",MaxTextExtent);
2235 status=WriteTIFFImage(write_info,images,exception);
2236 images=DestroyImageList(images);
2237 write_info=DestroyImageInfo(write_info);
2242 #if defined(MAGICKCORE_TIFF_DELEGATE)
2244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2247 % W r i t e T I F F I m a g e %
2251 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2253 % WriteTIFFImage() writes an image in the Tagged image file format.
2255 % The format of the WriteTIFFImage method is:
2257 % MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2258 % Image *image,ExceptionInfo *exception)
2260 % A description of each parameter follows:
2262 % o image_info: the image info.
2264 % o image: The image.
2266 % o exception: return any errors or warnings in this structure.
2270 typedef struct _TIFFInfo
2281 static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2283 assert(tiff_info != (TIFFInfo *) NULL);
2284 if (tiff_info->scanlines != (unsigned char *) NULL)
2285 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2286 tiff_info->scanlines);
2287 if (tiff_info->pixels != (unsigned char *) NULL)
2288 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2292 static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2293 TIFFInfo *tiff_info)
2305 assert(tiff_info != (TIFFInfo *) NULL);
2306 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2307 option=GetImageOption(image_info,"tiff:tile-geometry");
2308 if (option == (const char *) NULL)
2310 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2311 if ((flags & HeightValue) == 0)
2312 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2313 tile_columns=(uint32) tiff_info->tile_geometry.width;
2314 tile_rows=(uint32) tiff_info->tile_geometry.height;
2315 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2316 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2317 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2318 tiff_info->tile_geometry.width=tile_columns;
2319 tiff_info->tile_geometry.height=tile_rows;
2320 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2321 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
2322 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2323 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2324 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2325 (tiff_info->pixels == (unsigned char *) NULL))
2327 DestroyTIFFInfo(tiff_info);
2328 return(MagickFalse);
2333 static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
2334 tsample_t sample,Image *image)
2342 register unsigned char
2356 if (TIFFIsTiled(tiff) == 0)
2357 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2359 Fill scanlines to tile height.
2361 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2362 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2363 (size_t) TIFFScanlineSize(tiff));
2364 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2365 (tiff_info->tile_geometry.height-1)) &&
2366 (row != (ssize_t) (image->rows-1)))
2369 Write tile to TIFF image.
2372 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (
2373 tiff_info->tile_geometry.height*tiff_info->tile_geometry.width);
2374 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2375 tiff_info->tile_geometry.width;
2376 for (i=0; i < (ssize_t) number_tiles; i++)
2378 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
2379 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2380 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2381 for (k=0; k < (ssize_t) tile_width; k++)
2383 if (bytes_per_pixel == 0)
2385 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2386 tiff_info->tile_geometry.width+k)/8);
2387 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2391 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2392 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2393 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2394 for (l=0; l < bytes_per_pixel; l++)
2397 if ((i*tiff_info->tile_geometry.width) != image->columns)
2398 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2399 tiff_info->tile_geometry.width),(uint32) ((row/
2400 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2408 static void TIFFSetProfiles(TIFF *tiff,Image *image)
2416 if (image->profiles == (void *) NULL)
2418 ResetImageProfileIterator(image);
2419 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2421 profile=GetImageProfile(image,name);
2422 #if defined(TIFFTAG_XMLPACKET)
2423 if (LocaleCompare(name,"xmp") == 0)
2424 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2425 profile),GetStringInfoDatum(profile));
2427 #if defined(TIFFTAG_ICCPROFILE)
2428 if (LocaleCompare(name,"icc") == 0)
2429 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2430 profile),GetStringInfoDatum(profile));
2432 if (LocaleCompare(name,"iptc") == 0)
2440 iptc_profile=CloneStringInfo(profile);
2441 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2443 SetStringInfoLength(iptc_profile,length);
2444 if (TIFFIsByteSwapped(tiff))
2445 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2446 (unsigned long) (length/4));
2447 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2448 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2449 iptc_profile=DestroyStringInfo(iptc_profile);
2451 #if defined(TIFFTAG_PHOTOSHOP)
2452 if (LocaleCompare(name,"8bim") == 0)
2453 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2454 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2456 if (LocaleCompare(name,"tiff:37724") == 0)
2457 (void) TIFFSetField(tiff,37724,(uint32) GetStringInfoLength(profile),
2458 GetStringInfoDatum(profile));
2459 name=GetNextImageProfile(image);
2463 static void TIFFSetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception)
2468 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
2469 value=GetImageProperty(image,"tiff:hostcomputer",exception);
2470 if (value != (const char *) NULL)
2471 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2472 value=GetImageProperty(image,"tiff:artist",exception);
2473 if (value != (const char *) NULL)
2474 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2475 value=GetImageProperty(image,"tiff:timestamp",exception);
2476 if (value != (const char *) NULL)
2477 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2478 value=GetImageProperty(image,"tiff:make",exception);
2479 if (value != (const char *) NULL)
2480 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2481 value=GetImageProperty(image,"tiff:model",exception);
2482 if (value != (const char *) NULL)
2483 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2484 value=GetImageProperty(image,"tiff:software",exception);
2485 if (value != (const char *) NULL)
2486 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
2487 value=GetImageProperty(image,"tiff:copyright",exception);
2488 if (value != (const char *) NULL)
2489 (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value);
2490 value=GetImageProperty(image,"kodak-33423",exception);
2491 if (value != (const char *) NULL)
2492 (void) TIFFSetField(tiff,33423,value);
2493 value=GetImageProperty(image,"kodak-36867",exception);
2494 if (value != (const char *) NULL)
2495 (void) TIFFSetField(tiff,36867,value);
2496 value=GetImageProperty(image,"label",exception);
2497 if (value != (const char *) NULL)
2498 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2499 value=GetImageProperty(image,"comment",exception);
2500 if (value != (const char *) NULL)
2501 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2504 static void TIFFSetEXIFProperties(TIFF *tiff,Image *image,
2505 ExceptionInfo *exception)
2507 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2518 Write EXIF properties.
2521 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2522 for (i=0; exif_info[i].tag != 0; i++)
2524 value=GetImageProperty(image,exif_info[i].property,exception);
2525 if (value == (const char *) NULL)
2527 switch (exif_info[i].type)
2531 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2539 shorty=(uint16) StringToLong(value);
2540 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2548 ssize_ty=(uint16) StringToLong(value);
2549 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
2553 case TIFF_SRATIONAL:
2558 rational=StringToDouble(value,(char **) NULL);
2559 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2566 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2573 static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2574 Image *image,ExceptionInfo *exception)
2576 #if !defined(TIFFDefaultStripSize)
2577 #define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2637 assert(image_info != (const ImageInfo *) NULL);
2638 assert(image_info->signature == MagickSignature);
2639 assert(image != (Image *) NULL);
2640 assert(image->signature == MagickSignature);
2641 if (image->debug != MagickFalse)
2642 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2643 assert(exception != (ExceptionInfo *) NULL);
2644 assert(exception->signature == MagickSignature);
2645 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2646 if (status == MagickFalse)
2648 (void) MagickSetThreadValue(tiff_exception,exception);
2649 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2650 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
2651 endian_type=UndefinedEndian;
2652 option=GetImageArtifact(image,"tiff:endian");
2653 if (option != (const char *) NULL)
2655 if (LocaleNCompare(option,"msb",3) == 0)
2656 endian_type=MSBEndian;
2657 if (LocaleNCompare(option,"lsb",3) == 0)
2658 endian_type=LSBEndian;;
2660 switch (endian_type)
2662 case LSBEndian: mode="wl"; break;
2663 case MSBEndian: mode="wb"; break;
2664 default: mode="w"; break;
2666 #if defined(TIFF_VERSION_BIG)
2667 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
2668 switch (endian_type)
2670 case LSBEndian: mode="wl8"; break;
2671 case MSBEndian: mode="wb8"; break;
2672 default: mode="w8"; break;
2675 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2676 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2678 if (tiff == (TIFF *) NULL)
2680 (void) TIFFSetWarningHandler(warning_handler);
2681 (void) TIFFSetErrorHandler(error_handler);
2682 return(MagickFalse);
2685 debug=IsEventLogging();
2690 Initialize TIFF fields.
2692 if ((image_info->type != UndefinedType) &&
2693 (image_info->type != OptimizeType))
2694 (void) SetImageType(image,image_info->type,exception);
2695 quantum_info=AcquireQuantumInfo(image_info,image);
2696 if (quantum_info == (QuantumInfo *) NULL)
2697 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2698 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2699 (quantum_info->format == UndefinedQuantumFormat) &&
2700 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
2702 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2703 if (status == MagickFalse)
2704 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2706 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2707 (GetPreviousImageInList(image) != (Image *) NULL))
2708 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2709 if ((image->columns != (uint32) image->columns) ||
2710 (image->rows != (uint32) image->rows))
2711 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2712 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2713 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2714 compression=image->compression;
2715 if (image_info->compression != UndefinedCompression)
2716 compression=image_info->compression;
2717 switch (compression)
2719 case FaxCompression:
2721 compress_tag=COMPRESSION_CCITTFAX3;
2722 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2725 case Group4Compression:
2727 compress_tag=COMPRESSION_CCITTFAX4;
2728 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2731 #if defined(COMPRESSION_JBIG)
2732 case JBIG1Compression:
2734 compress_tag=COMPRESSION_JBIG;
2738 case JPEGCompression:
2740 compress_tag=COMPRESSION_JPEG;
2743 #if defined(COMPRESSION_LZMA)
2744 case LZMACompression:
2746 compress_tag=COMPRESSION_LZMA;
2750 case LZWCompression:
2752 compress_tag=COMPRESSION_LZW;
2755 case RLECompression:
2757 compress_tag=COMPRESSION_PACKBITS;
2760 case ZipCompression:
2762 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2768 compress_tag=COMPRESSION_NONE;
2772 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2773 if ((compress_tag != COMPRESSION_NONE) &&
2774 (TIFFIsCODECConfigured(compress_tag) == 0))
2776 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2777 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
2778 MagickCompressOptions,(ssize_t) compression));
2779 compress_tag=COMPRESSION_NONE;
2780 compression=NoCompression;
2783 switch (compress_tag)
2785 #if defined(CCITT_SUPPORT)
2786 case COMPRESSION_CCITTFAX3:
2787 case COMPRESSION_CCITTFAX4:
2789 #if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2790 case COMPRESSION_JPEG:
2792 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2793 case COMPRESSION_LZMA:
2795 #if defined(LZW_SUPPORT)
2796 case COMPRESSION_LZW:
2798 #if defined(PACKBITS_SUPPORT)
2799 case COMPRESSION_PACKBITS:
2801 #if defined(ZIP_SUPPORT)
2802 case COMPRESSION_ADOBE_DEFLATE:
2804 case COMPRESSION_NONE:
2808 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2809 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
2810 MagickCompressOptions,(ssize_t) compression));
2811 compress_tag=COMPRESSION_NONE;
2812 compression=NoCompression;
2817 switch (compression)
2819 case FaxCompression:
2820 case Group4Compression:
2822 (void) SetImageType(image,BilevelType,exception);
2825 case JPEGCompression:
2827 (void) SetImageStorageClass(image,DirectClass,exception);
2828 (void) SetImageDepth(image,8,exception);
2834 if (image->colorspace == CMYKColorspace)
2836 photometric=PHOTOMETRIC_SEPARATED;
2837 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2838 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2843 Full color TIFF raster.
2845 if (image->colorspace == LabColorspace)
2846 photometric=PHOTOMETRIC_CIELAB;
2848 if (image->colorspace == YCbCrColorspace)
2850 photometric=PHOTOMETRIC_YCBCR;
2851 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
2852 (void) SetImageStorageClass(image,DirectClass,exception);
2853 (void) SetImageDepth(image,8,exception);
2856 photometric=PHOTOMETRIC_RGB;
2857 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2858 if ((image_info->type != TrueColorType) &&
2859 (image_info->type != TrueColorMatteType))
2861 if ((image_info->type != PaletteType) &&
2862 (IsImageGray(image,exception) != MagickFalse))
2864 photometric=(uint16) (quantum_info->min_is_white !=
2865 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2866 PHOTOMETRIC_MINISBLACK);
2867 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
2868 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
2869 (IsImageMonochrome(image,exception) != MagickFalse))
2871 status=SetQuantumDepth(image,quantum_info,1);
2872 if (status == MagickFalse)
2873 ThrowWriterException(ResourceLimitError,
2874 "MemoryAllocationFailed");
2878 if (image->storage_class == PseudoClass)
2884 Colormapped TIFF raster.
2886 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
2887 photometric=PHOTOMETRIC_PALETTE;
2889 while ((GetQuantumRange(depth)+1) < image->colors)
2891 status=SetQuantumDepth(image,quantum_info,depth);
2892 if (status == MagickFalse)
2893 ThrowWriterException(ResourceLimitError,
2894 "MemoryAllocationFailed");
2898 if ((photometric == PHOTOMETRIC_RGB) &&
2899 (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse))
2900 (void) TransformImageColorspace(image,sRGBColorspace,exception);
2901 switch (image->endian)
2905 endian=FILLORDER_LSB2MSB;
2910 endian=FILLORDER_MSB2LSB;
2913 case UndefinedEndian:
2916 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2920 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2921 (photometric != PHOTOMETRIC_MINISWHITE))
2923 compress_tag=COMPRESSION_NONE;
2924 endian=FILLORDER_MSB2LSB;
2927 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2928 (photometric != PHOTOMETRIC_MINISWHITE))
2930 compress_tag=COMPRESSION_NONE;
2931 endian=FILLORDER_MSB2LSB;
2933 option=GetImageArtifact(image,"tiff:fill-order");
2934 if (option != (const char *) NULL)
2936 if (LocaleNCompare(option,"msb",3) == 0)
2937 endian=FILLORDER_MSB2LSB;
2938 if (LocaleNCompare(option,"lsb",3) == 0)
2939 endian=FILLORDER_LSB2MSB;
2941 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2942 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
2943 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2944 if (image->matte != MagickFalse)
2952 TIFF has a matte channel.
2955 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2956 option=GetImageArtifact(image,"tiff:alpha");
2957 if ((option != (const char *) NULL) &&
2958 (LocaleCompare(option,"associated") == 0))
2959 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2960 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2961 &samples_per_pixel);
2962 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2963 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2965 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2966 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2968 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2969 switch (quantum_info->format)
2971 case FloatingPointQuantumFormat:
2973 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2974 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2975 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2978 case SignedQuantumFormat:
2980 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2983 case UnsignedQuantumFormat:
2985 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2991 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2992 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2993 if (photometric == PHOTOMETRIC_RGB)
2994 if ((image_info->interlace == PlaneInterlace) ||
2995 (image_info->interlace == PartitionInterlace))
2996 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2998 if (TIFFScanlineSize(tiff) != 0)
2999 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
3001 option=GetImageArtifact(image,"tiff:rows-per-strip");
3002 if (option != (const char *) NULL)
3003 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
3004 switch (compress_tag)
3006 case COMPRESSION_JPEG:
3008 #if defined(JPEG_SUPPORT)
3018 rows_per_strip+=(16-(rows_per_strip % 16));
3019 if (image->quality != 0)
3020 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
3021 if (image_info->quality != UndefinedCompressionQuality)
3022 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
3023 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
3024 if (IssRGBCompatibleColorspace(image->colorspace) != MagickFalse)
3029 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
3030 sampling_factor=(const char *) NULL;
3031 value=GetImageProperty(image,"jpeg:sampling-factor",exception);
3032 if (value != (char *) NULL)
3034 sampling_factor=value;
3035 if (image->debug != MagickFalse)
3036 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3037 " Input sampling-factors=%s",sampling_factor);
3039 if (image_info->sampling_factor != (char *) NULL)
3040 sampling_factor=image_info->sampling_factor;
3041 if (sampling_factor != (const char *) NULL)
3043 flags=ParseGeometry(sampling_factor,&geometry_info);
3044 if ((flags & SigmaValue) == 0)
3045 geometry_info.sigma=geometry_info.rho;
3046 if (image->colorspace == YCbCrColorspace)
3047 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
3048 geometry_info.rho,(uint16) geometry_info.sigma);
3051 if (bits_per_sample == 12)
3052 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
3056 case COMPRESSION_ADOBE_DEFLATE:
3058 rows_per_strip=(uint32) image->rows;
3059 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3061 if (((photometric == PHOTOMETRIC_RGB) ||
3062 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3063 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3064 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3065 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
3066 image_info->quality == UndefinedCompressionQuality ? 7 :
3067 MagickMin((ssize_t) image_info->quality/10,9)));
3070 case COMPRESSION_CCITTFAX3:
3075 rows_per_strip=(uint32) image->rows;
3076 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
3079 case COMPRESSION_CCITTFAX4:
3081 rows_per_strip=(uint32) image->rows;
3084 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
3085 case COMPRESSION_LZMA:
3087 if (((photometric == PHOTOMETRIC_RGB) ||
3088 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3089 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3090 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3091 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
3092 image_info->quality == UndefinedCompressionQuality ? 7 :
3093 MagickMin((ssize_t) image_info->quality/10,9)));
3097 case COMPRESSION_LZW:
3099 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
3101 if (((photometric == PHOTOMETRIC_RGB) ||
3102 (photometric == PHOTOMETRIC_MINISBLACK)) &&
3103 ((bits_per_sample == 8) || (bits_per_sample == 16)))
3104 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
3110 option=GetImageArtifact(image,"tiff:rows-per-strip");
3111 if (option == (const char *) NULL)
3112 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
3113 if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0))
3119 Set image resolution.
3122 if (image->units == PixelsPerInchResolution)
3124 if (image->units == PixelsPerCentimeterResolution)
3125 units=RESUNIT_CENTIMETER;
3126 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
3127 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x);
3128 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y);
3129 if ((image->page.x != 0) || (image->page.y != 0))
3134 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
3135 image->resolution.x);
3136 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
3137 image->resolution.y);
3140 if (image->chromaticity.white_point.x != 0.0)
3146 Set image chromaticity.
3148 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3149 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3150 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3151 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3152 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3153 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3154 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3155 chromaticity[0]=(float) image->chromaticity.white_point.x;
3156 chromaticity[1]=(float) image->chromaticity.white_point.y;
3157 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3159 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3160 (image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3162 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3163 if (image->scene != 0)
3164 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3165 GetImageListLength(image));
3167 if (image->orientation != UndefinedOrientation)
3168 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3169 (void) TIFFSetProfiles(tiff,image);
3175 page=(uint16) scene;
3176 pages=(uint16) GetImageListLength(image);
3177 if ((LocaleCompare(image_info->magick,"PTIF") != 0) &&
3178 (image_info->adjoin != MagickFalse) && (pages > 1))
3179 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3180 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3182 (void) TIFFSetProperties(tiff,image,exception);
3184 (void) TIFFSetEXIFProperties(tiff,image,exception);
3186 Write image scanlines.
3188 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3189 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3190 quantum_info->endian=LSBEndian;
3191 pixels=GetQuantumPixels(quantum_info);
3192 tiff_info.scanline=GetQuantumPixels(quantum_info);
3193 switch (photometric)
3195 case PHOTOMETRIC_CIELAB:
3196 case PHOTOMETRIC_YCBCR:
3197 case PHOTOMETRIC_RGB:
3202 switch (image_info->interlace)
3207 quantum_type=RGBQuantum;
3208 if (image->matte != MagickFalse)
3209 quantum_type=RGBAQuantum;
3210 for (y=0; y < (ssize_t) image->rows; y++)
3212 register const Quantum
3215 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3216 if (p == (const Quantum *) NULL)
3218 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3219 quantum_type,pixels,exception);
3221 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3223 if (image->previous == (Image *) NULL)
3225 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3227 if (status == MagickFalse)
3233 case PlaneInterlace:
3234 case PartitionInterlace:
3237 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3239 for (y=0; y < (ssize_t) image->rows; y++)
3241 register const Quantum
3244 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3245 if (p == (const Quantum *) NULL)
3247 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3248 RedQuantum,pixels,exception);
3249 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3252 if (image->previous == (Image *) NULL)
3254 status=SetImageProgress(image,SaveImageTag,100,400);
3255 if (status == MagickFalse)
3258 for (y=0; y < (ssize_t) image->rows; y++)
3260 register const Quantum
3263 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3264 if (p == (const Quantum *) NULL)
3266 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3267 GreenQuantum,pixels,exception);
3268 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3271 if (image->previous == (Image *) NULL)
3273 status=SetImageProgress(image,SaveImageTag,200,400);
3274 if (status == MagickFalse)
3277 for (y=0; y < (ssize_t) image->rows; y++)
3279 register const Quantum
3282 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3283 if (p == (const Quantum *) NULL)
3285 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3286 BlueQuantum,pixels,exception);
3287 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3290 if (image->previous == (Image *) NULL)
3292 status=SetImageProgress(image,SaveImageTag,300,400);
3293 if (status == MagickFalse)
3296 if (image->matte != MagickFalse)
3297 for (y=0; y < (ssize_t) image->rows; y++)
3299 register const Quantum
3302 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3303 if (p == (const Quantum *) NULL)
3305 length=ExportQuantumPixels(image,(CacheView *) NULL,
3306 quantum_info,AlphaQuantum,pixels,exception);
3307 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3310 if (image->previous == (Image *) NULL)
3312 status=SetImageProgress(image,SaveImageTag,400,400);
3313 if (status == MagickFalse)
3321 case PHOTOMETRIC_SEPARATED:
3326 quantum_type=CMYKQuantum;
3327 if (image->matte != MagickFalse)
3328 quantum_type=CMYKAQuantum;
3329 if (image->colorspace != CMYKColorspace)
3330 (void) TransformImageColorspace(image,CMYKColorspace,exception);
3331 for (y=0; y < (ssize_t) image->rows; y++)
3333 register const Quantum
3336 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3337 if (p == (const Quantum *) NULL)
3339 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3340 quantum_type,pixels,exception);
3341 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3343 if (image->previous == (Image *) NULL)
3345 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3347 if (status == MagickFalse)
3353 case PHOTOMETRIC_PALETTE:
3361 Colormapped TIFF image.
3363 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3364 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3365 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3366 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3367 (blue == (uint16 *) NULL))
3368 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3370 Initialize TIFF colormap.
3372 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3373 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3374 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3375 for (i=0; i < (ssize_t) image->colors; i++)
3377 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3378 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3379 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3381 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3382 red=(uint16 *) RelinquishMagickMemory(red);
3383 green=(uint16 *) RelinquishMagickMemory(green);
3384 blue=(uint16 *) RelinquishMagickMemory(blue);
3389 Convert PseudoClass packets to contiguous grayscale scanlines.
3391 quantum_type=IndexQuantum;
3392 if (image->matte != MagickFalse)
3394 if (photometric != PHOTOMETRIC_PALETTE)
3395 quantum_type=GrayAlphaQuantum;
3397 quantum_type=IndexAlphaQuantum;
3400 if (photometric != PHOTOMETRIC_PALETTE)
3401 quantum_type=GrayQuantum;
3402 for (y=0; y < (ssize_t) image->rows; y++)
3404 register const Quantum
3407 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3408 if (p == (const Quantum *) NULL)
3410 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3411 quantum_type,pixels,exception);
3412 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3414 if (image->previous == (Image *) NULL)
3416 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3418 if (status == MagickFalse)
3425 quantum_info=DestroyQuantumInfo(quantum_info);
3426 DestroyTIFFInfo(&tiff_info);
3427 if (0 && (image_info->verbose == MagickTrue))
3428 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3429 (void) TIFFWriteDirectory(tiff);
3430 image=SyncNextImageInList(image);
3431 if (image == (Image *) NULL)
3433 status=SetImageProgress(image,SaveImagesTag,scene++,
3434 GetImageListLength(image));
3435 if (status == MagickFalse)
3437 } while (image_info->adjoin != MagickFalse);
3438 (void) TIFFSetWarningHandler(warning_handler);
3439 (void) TIFFSetErrorHandler(error_handler);