2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % TTTTT IIIII FFFFF FFFFF %
13 % Read/Write TIFF Image Format %
20 % Copyright 1999-2011 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/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/constitute.h"
53 #include "MagickCore/enhance.h"
54 #include "MagickCore/exception.h"
55 #include "MagickCore/exception-private.h"
56 #include "MagickCore/geometry.h"
57 #include "MagickCore/image.h"
58 #include "MagickCore/image-private.h"
59 #include "MagickCore/list.h"
60 #include "MagickCore/log.h"
61 #include "MagickCore/magick.h"
62 #include "MagickCore/memory_.h"
63 #include "MagickCore/module.h"
64 #include "MagickCore/monitor.h"
65 #include "MagickCore/monitor-private.h"
66 #include "MagickCore/option.h"
67 #include "MagickCore/pixel-accessor.h"
68 #include "MagickCore/property.h"
69 #include "MagickCore/quantum.h"
70 #include "MagickCore/quantum-private.h"
71 #include "MagickCore/profile.h"
72 #include "MagickCore/resize.h"
73 #include "MagickCore/resource_.h"
74 #include "MagickCore/semaphore.h"
75 #include "MagickCore/splay-tree.h"
76 #include "MagickCore/static.h"
77 #include "MagickCore/statistic.h"
78 #include "MagickCore/string_.h"
79 #include "MagickCore/string-private.h"
80 #include "MagickCore/thread_.h"
81 #include "MagickCore/utility.h"
82 #if defined(MAGICKCORE_TIFF_DELEGATE)
83 # if defined(MAGICKCORE_HAVE_TIFFCONF_H)
84 # include "tiffconf.h"
88 # if !defined(COMPRESSION_ADOBE_DEFLATE)
89 # define COMPRESSION_ADOBE_DEFLATE 8
91 # if !defined(PREDICTOR_HORIZONTAL)
92 # define PREDICTOR_HORIZONTAL 2
98 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
99 typedef struct _ExifInfo
109 static const ExifInfo
111 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
112 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
113 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
114 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
115 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
116 { EXIFTAG_OECF, TIFF_NOTYPE, "exif:OptoelectricConversionFactor" },
117 { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, "exif:ExifVersion" },
118 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
119 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
120 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, "exif:ComponentsConfiguration" },
121 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
122 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
123 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
124 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
125 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
126 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
127 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
128 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
129 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
130 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
131 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
132 { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, "exif:SubjectArea" },
133 { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, "exif:MakerNote" },
134 { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, "exif:UserComment" },
135 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
136 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
137 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
138 { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, "exif:FlashpixVersion" },
139 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
140 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
141 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
142 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
143 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
144 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
145 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, "exif:SpatialFrequencyResponse" },
146 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
147 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
148 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
149 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
150 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
151 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
152 { EXIFTAG_FILESOURCE, TIFF_NOTYPE, "exif:FileSource" },
153 { EXIFTAG_SCENETYPE, TIFF_NOTYPE, "exif:SceneType" },
154 { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, "exif:CFAPattern" },
155 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
156 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
157 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
158 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
159 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
160 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
161 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
162 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
163 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
164 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
165 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, "exif:DeviceSettingDescription" },
166 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
167 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
168 { 0, 0, (char *) NULL }
171 #endif /* MAGICKCORE_TIFF_DELEGATE */
176 static MagickThreadKey
180 *tiff_semaphore = (SemaphoreInfo *) NULL;
182 static volatile MagickBooleanType
183 instantiate_key = MagickFalse;
186 Forward declarations.
188 #if defined(MAGICKCORE_TIFF_DELEGATE)
190 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
192 static MagickBooleanType
193 WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *),
194 WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *),
195 WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *);
199 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 % IsTIFF() returns MagickTrue if the image format type, identified by the
210 % magick string, is TIFF.
212 % The format of the IsTIFF method is:
214 % MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
216 % A description of each parameter follows:
218 % o magick: compare image format pattern against these bytes.
220 % o length: Specifies the length of the magick string.
223 static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
227 if (memcmp(magick,"\115\115\000\052",4) == 0)
229 if (memcmp(magick,"\111\111\052\000",4) == 0)
231 #if defined(TIFF_VERSION_BIG)
234 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
236 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
242 #if defined(MAGICKCORE_TIFF_DELEGATE)
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 % R e a d G R O U P 4 I m a g e %
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 % ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
255 % allocates the memory necessary for the new Image structure and returns a
256 % pointer to the new image.
258 % The format of the ReadGROUP4Image method is:
260 % Image *ReadGROUP4Image(const ImageInfo *image_info,
261 % ExceptionInfo *exception)
263 % A description of each parameter follows:
265 % o image_info: the image info.
267 % o exception: return any errors or warnings in this structure.
271 static inline size_t WriteLSBLong(FILE *file,const size_t value)
276 buffer[0]=(unsigned char) value;
277 buffer[1]=(unsigned char) (value >> 8);
278 buffer[2]=(unsigned char) (value >> 16);
279 buffer[3]=(unsigned char) (value >> 24);
280 return(fwrite(buffer,1,4,file));
283 static Image *ReadGROUP4Image(const ImageInfo *image_info,
284 ExceptionInfo *exception)
287 filename[MaxTextExtent];
315 assert(image_info != (const ImageInfo *) NULL);
316 assert(image_info->signature == MagickSignature);
317 if (image_info->debug != MagickFalse)
318 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
319 image_info->filename);
320 assert(exception != (ExceptionInfo *) NULL);
321 assert(exception->signature == MagickSignature);
322 image=AcquireImage(image_info,exception);
323 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
324 if (status == MagickFalse)
326 image=DestroyImageList(image);
327 return((Image *) NULL);
330 Write raw CCITT Group 4 wrapped as a TIFF image file.
333 unique_file=AcquireUniqueFileResource(filename);
334 if (unique_file != -1)
335 file=fdopen(unique_file,"wb");
336 if ((unique_file == -1) || (file == (FILE *) NULL))
337 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
338 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
339 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
340 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
341 length=WriteLSBLong(file,image->columns);
342 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
343 length=WriteLSBLong(file,image->rows);
344 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
345 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
346 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
347 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
348 strip_offset=10+(12*14)+4+8;
349 length=WriteLSBLong(file,(size_t) strip_offset);
350 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
351 length=WriteLSBLong(file,(size_t) image_info->orientation);
352 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
353 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
354 length=WriteLSBLong(file,image->rows);
355 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
356 offset=(ssize_t) ftell(file)-4;
357 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
358 length=WriteLSBLong(file,(size_t) (strip_offset-8));
359 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
360 length=WriteLSBLong(file,(size_t) (strip_offset-8));
361 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
362 length=fwrite("\000\000\000\000",1,4,file);
363 length=WriteLSBLong(file,(long) image->x_resolution);
364 length=WriteLSBLong(file,1);
365 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
366 (void) fputc(c,file);
367 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
368 length=WriteLSBLong(file,(unsigned int) length);
370 (void) CloseBlob(image);
371 image=DestroyImage(image);
375 read_info=CloneImageInfo((ImageInfo *) NULL);
376 (void) FormatLocaleString(read_info->filename,MaxTextExtent,"%s",filename);
377 image=ReadTIFFImage(read_info,exception);
378 read_info=DestroyImageInfo(read_info);
379 if (image != (Image *) NULL)
381 (void) CopyMagickString(image->filename,image_info->filename,
383 (void) CopyMagickString(image->magick_filename,image_info->filename,
385 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
387 (void) RelinquishUniqueFileResource(filename);
392 #if defined(MAGICKCORE_TIFF_DELEGATE)
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 % R e a d T I F F I m a g e %
402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 % ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
405 % memory necessary for the new Image structure and returns a pointer to the
408 % The format of the ReadTIFFImage method is:
410 % Image *ReadTIFFImage(const ImageInfo *image_info,
411 % ExceptionInfo *exception)
413 % A description of each parameter follows:
415 % o image_info: the image info.
417 % o exception: return any errors or warnings in this structure.
421 static inline size_t MagickMax(const size_t x,const size_t y)
428 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
435 static MagickBooleanType ReadProfile(Image *image,const char *name,
436 unsigned char *datum,ssize_t length,ExceptionInfo *exception)
450 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
452 for (i=0; i < (length-4); i+=2)
453 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
462 profile=BlobToStringInfo(datum+i,(size_t) length);
463 if (profile == (StringInfo *) NULL)
464 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
466 status=SetImageProfile(image,name,profile);
467 profile=DestroyStringInfo(profile);
468 if (status == MagickFalse)
469 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
474 #if defined(__cplusplus) || defined(c_plusplus)
478 static int TIFFCloseBlob(thandle_t image)
480 (void) CloseBlob((Image *) image);
484 static void TIFFErrors(const char *module,const char *format,va_list error)
487 message[MaxTextExtent];
492 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
493 (void) vsnprintf(message,MaxTextExtent,format,error);
495 (void) vsprintf(message,format,error);
497 (void) ConcatenateMagickString(message,".",MaxTextExtent);
498 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
499 if (exception != (ExceptionInfo *) NULL)
500 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
504 static void TIFFGetProfiles(TIFF *tiff,Image *image,ExceptionInfo *exception)
512 #if defined(TIFFTAG_ICCPROFILE)
514 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
515 (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception);
517 #if defined(TIFFTAG_PHOTOSHOP)
519 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
520 (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception);
522 #if defined(TIFFTAG_RICHTIFFIPTC)
524 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
526 if (TIFFIsByteSwapped(tiff) != 0)
527 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
528 (void) ReadProfile(image,"iptc",profile,4L*length,exception);
531 #if defined(TIFFTAG_XMLPACKET)
533 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
534 (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception);
537 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
538 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception);
541 static void TIFFGetProperties(TIFF *tiff,Image *image)
546 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
547 (void) SetImageProperty(image,"tiff:artist",text);
548 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
549 (void) SetImageProperty(image,"tiff:timestamp",text);
550 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
551 (void) SetImageProperty(image,"tiff:software",text);
552 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
553 (void) SetImageProperty(image,"tiff:hostcomputer",text);
554 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
555 (void) SetImageProperty(image,"tiff:document",text);
556 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
557 (void) SetImageProperty(image,"tiff:make",text);
558 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
559 (void) SetImageProperty(image,"tiff:model",text);
560 if (TIFFGetField(tiff,33432,&text) == 1)
561 (void) SetImageProperty(image,"tiff:copyright",text);
562 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
563 (void) SetImageProperty(image,"label",text);
564 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
565 (void) SetImageProperty(image,"comment",text);
568 static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
570 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
572 value[MaxTextExtent];
587 Read EXIF properties.
589 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
591 directory=TIFFCurrentDirectory(tiff);
592 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
595 for (i=0; exif_info[i].tag != 0; i++)
598 switch (exif_info[i].type)
606 if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans) != 0) &&
607 (ascii != (char *) NULL) && (*ascii != '\0'))
608 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
616 if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans) != 0)
617 (void) FormatLocaleString(value,MaxTextExtent,"%d",(int) shorty[0]);
625 if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans) != 0)
626 (void) FormatLocaleString(value,MaxTextExtent,"%d",longy);
637 if (TIFFGetField(tiff,exif_info[i].tag,&rational,&sans) != 0)
638 (void) FormatLocaleString(value,MaxTextExtent,"%g",rational[0]);
645 (void) SetImageProperty(image,exif_info[i].property,value);
647 TIFFSetDirectory(tiff,directory);
654 static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
656 *base=(tdata_t *) GetBlobStreamData((Image *) image);
657 if (*base != (tdata_t *) NULL)
658 *size=(toff_t) GetBlobSize((Image *) image);
659 if (*base != (tdata_t *) NULL)
664 static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
669 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
670 (unsigned char *) data);
674 static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
675 tsample_t sample,ssize_t row,tdata_t scanline)
680 (void) bits_per_sample;
681 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
685 static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
687 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
690 static toff_t TIFFGetBlobSize(thandle_t image)
692 return((toff_t) GetBlobSize((Image *) image));
695 static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
702 static void TIFFWarnings(const char *module,const char *format,va_list warning)
705 message[MaxTextExtent];
710 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
711 (void) vsnprintf(message,MaxTextExtent,format,warning);
713 (void) vsprintf(message,format,warning);
715 (void) ConcatenateMagickString(message,".",MaxTextExtent);
716 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
717 if (exception != (ExceptionInfo *) NULL)
718 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
719 message,"`%s'",module);
722 static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
727 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
728 (unsigned char *) data);
732 #if defined(__cplusplus) || defined(c_plusplus)
736 static Image *ReadTIFFImage(const ImageInfo *image_info,
737 ExceptionInfo *exception)
741 ReadSingleSampleMethod,
825 assert(image_info != (const ImageInfo *) NULL);
826 assert(image_info->signature == MagickSignature);
827 if (image_info->debug != MagickFalse)
828 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
829 image_info->filename);
830 assert(exception != (ExceptionInfo *) NULL);
831 assert(exception->signature == MagickSignature);
832 image=AcquireImage(image_info,exception);
833 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
834 if (status == MagickFalse)
836 image=DestroyImageList(image);
837 return((Image *) NULL);
839 (void) MagickSetThreadValue(tiff_exception,exception);
840 error_handler=TIFFSetErrorHandler(TIFFErrors);
841 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
842 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
843 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
845 if (tiff == (TIFF *) NULL)
847 (void) TIFFSetWarningHandler(warning_handler);
848 (void) TIFFSetErrorHandler(error_handler);
849 image=DestroyImageList(image);
850 return((Image *) NULL);
852 debug=IsEventLogging();
854 if (image_info->number_scenes != 0)
857 Generate blank images for subimage specification (e.g. image.tif[4].
859 for (i=0; i < (ssize_t) image_info->scene; i++)
861 (void) TIFFReadDirectory(tiff);
862 AcquireNextImage(image_info,image,exception);
863 if (GetNextImageInList(image) == (Image *) NULL)
865 image=DestroyImageList(image);
866 return((Image *) NULL);
868 image=SyncNextImageInList(image);
873 if (0 && (image_info->verbose != MagickFalse))
874 TIFFPrintDirectory(tiff,stdout,MagickFalse);
875 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
877 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
878 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
879 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
880 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
881 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
882 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
883 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
884 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
885 if (sample_format == SAMPLEFORMAT_IEEEFP)
886 (void) SetImageProperty(image,"quantum:format","floating-point");
887 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
888 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
889 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
892 case PHOTOMETRIC_MINISBLACK:
894 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
897 case PHOTOMETRIC_MINISWHITE:
899 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
902 case PHOTOMETRIC_PALETTE:
904 (void) SetImageProperty(image,"tiff:photometric","palette");
907 case PHOTOMETRIC_RGB:
909 (void) SetImageProperty(image,"tiff:photometric","RGB");
912 case PHOTOMETRIC_CIELAB:
914 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
917 case PHOTOMETRIC_SEPARATED:
919 (void) SetImageProperty(image,"tiff:photometric","separated");
922 case PHOTOMETRIC_YCBCR:
924 (void) SetImageProperty(image,"tiff:photometric","YCBCR");
929 (void) SetImageProperty(image,"tiff:photometric","unknown");
933 if (image->debug != MagickFalse)
935 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
936 (unsigned int) width,(unsigned int) height);
937 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
939 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
940 "Bits per sample: %u",bits_per_sample);
941 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
942 "Min sample value: %u",min_sample_value);
943 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
944 "Max sample value: %u",max_sample_value);
945 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
946 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
948 image->columns=(size_t) width;
949 image->rows=(size_t) height;
950 image->depth=(size_t) bits_per_sample;
951 if (image->debug != MagickFalse)
952 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
953 (double) image->depth);
955 image->endian=MSBEndian;
956 if ((int) (*(char *) &lsb_first) != 0)
957 image->endian=LSBEndian;
958 if (photometric == PHOTOMETRIC_SEPARATED)
959 image->colorspace=CMYKColorspace;
960 if (photometric == PHOTOMETRIC_CIELAB)
961 image->colorspace=LabColorspace;
962 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
964 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
965 x_resolution=(float) image->x_resolution;
966 y_resolution=(float) image->y_resolution;
967 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
968 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
969 image->x_resolution=x_resolution;
970 image->y_resolution=y_resolution;
971 x_position=(float) image->page.x/x_resolution;
972 y_position=(float) image->page.y/y_resolution;
973 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
974 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
975 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
976 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
977 image->orientation=(OrientationType) orientation;
978 chromaticity=(float *) NULL;
979 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
980 if (chromaticity != (float *) NULL)
982 image->chromaticity.white_point.x=chromaticity[0];
983 image->chromaticity.white_point.y=chromaticity[1];
985 chromaticity=(float *) NULL;
986 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
987 if (chromaticity != (float *) NULL)
989 image->chromaticity.red_primary.x=chromaticity[0];
990 image->chromaticity.red_primary.y=chromaticity[1];
991 image->chromaticity.green_primary.x=chromaticity[2];
992 image->chromaticity.green_primary.y=chromaticity[3];
993 image->chromaticity.blue_primary.x=chromaticity[4];
994 image->chromaticity.blue_primary.y=chromaticity[5];
996 TIFFGetProperties(tiff,image);
997 TIFFGetEXIFProperties(tiff,image);
998 TIFFGetProfiles(tiff,image,exception);
1000 Allocate memory for the image and pixel buffer.
1002 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
1003 if ((compress_tag != COMPRESSION_NONE) &&
1004 (TIFFIsCODECConfigured(compress_tag) == 0))
1007 ThrowReaderException(CoderError,"CompressNotSupported");
1010 switch (compress_tag)
1012 case COMPRESSION_NONE: image->compression=NoCompression; break;
1013 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1014 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1015 case COMPRESSION_JPEG:
1017 image->compression=JPEGCompression;
1018 #if defined(JPEG_SUPPORT)
1021 sampling_factor[MaxTextExtent];
1027 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1028 &horizontal,&vertical);
1029 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
1030 horizontal,vertical);
1031 (void) SetImageProperty(image,"jpeg:sampling-factor",
1033 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1034 "Sampling Factors: %s",sampling_factor);
1035 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1037 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1039 photometric=PHOTOMETRIC_RGB;
1045 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1046 #if defined(COMPRESSION_LZMA)
1047 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1049 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1050 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1051 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1052 default: image->compression=RLECompression; break;
1054 quantum_info=AcquireQuantumInfo(image_info,image);
1055 if (quantum_info == (QuantumInfo *) NULL)
1058 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1060 if (sample_format == SAMPLEFORMAT_UINT)
1061 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1062 if (sample_format == SAMPLEFORMAT_INT)
1063 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1064 if (sample_format == SAMPLEFORMAT_IEEEFP)
1065 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1066 if (status == MagickFalse)
1069 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1072 switch (photometric)
1074 case PHOTOMETRIC_MINISBLACK:
1076 quantum_info->min_is_white=MagickFalse;
1079 case PHOTOMETRIC_MINISWHITE:
1081 quantum_info->min_is_white=MagickTrue;
1087 associated_alpha=MagickFalse;
1089 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1091 if (extra_samples == 0)
1093 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1094 image->matte=MagickTrue;
1097 for (i=0; i < extra_samples; i++)
1099 image->matte=MagickTrue;
1100 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
1101 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
1103 option=GetImageOption(image_info,"tiff:alpha");
1104 if (option != (const char *) NULL)
1105 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1107 if (image->matte != MagickFalse)
1108 (void) SetImageProperty(image,"tiff:alpha",
1109 associated_alpha != MagickFalse ? "associated" : "unassociated");
1110 if ((photometric == PHOTOMETRIC_PALETTE) &&
1111 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1116 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
1117 if (AcquireImageColormap(image,colors,exception) == MagickFalse)
1120 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1123 if (units == RESUNIT_INCH)
1124 image->units=PixelsPerInchResolution;
1125 if (units == RESUNIT_CENTIMETER)
1126 image->units=PixelsPerCentimeterResolution;
1127 value=(unsigned short) image->scene;
1128 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1130 if (image_info->ping != MagickFalse)
1132 if (image_info->number_scenes != 0)
1133 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1135 goto next_tiff_frame;
1137 method=ReadGenericMethod;
1138 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1141 value[MaxTextExtent];
1143 method=ReadStripMethod;
1144 (void) FormatLocaleString(value,MaxTextExtent,"%u",
1145 (unsigned int) rows_per_strip);
1146 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1148 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1149 method=ReadRGBAMethod;
1150 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1151 method=ReadCMYKAMethod;
1152 if ((photometric != PHOTOMETRIC_RGB) &&
1153 (photometric != PHOTOMETRIC_CIELAB) &&
1154 (photometric != PHOTOMETRIC_SEPARATED))
1155 method=ReadGenericMethod;
1156 if (image->storage_class == PseudoClass)
1157 method=ReadSingleSampleMethod;
1158 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1159 (photometric == PHOTOMETRIC_MINISWHITE))
1160 method=ReadSingleSampleMethod;
1161 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1162 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
1163 method=ReadGenericMethod;
1164 if (image->compression == JPEGCompression)
1165 method=ReadGenericMethod;
1166 if (TIFFIsTiled(tiff) != MagickFalse)
1167 method=ReadTileMethod;
1168 quantum_type=RGBQuantum;
1169 pixels=GetQuantumPixels(quantum_info);
1172 case ReadSingleSampleMethod:
1175 Convert TIFF image to PseudoClass MIFF image.
1177 if ((image->storage_class == PseudoClass) &&
1178 (photometric == PHOTOMETRIC_PALETTE))
1189 Initialize colormap.
1191 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1192 &green_colormap,&blue_colormap);
1193 range=255; /* might be old style 8-bit colormap */
1194 for (i=0; i < (ssize_t) image->colors; i++)
1195 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1196 (blue_colormap[i] >= 256))
1201 for (i=0; i < (ssize_t) image->colors; i++)
1203 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
1204 red_colormap[i])/range);
1205 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
1206 green_colormap[i])/range);
1207 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
1208 blue_colormap[i])/range);
1211 quantum_type=IndexQuantum;
1212 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1213 if (image->matte != MagickFalse)
1215 if (image->storage_class != PseudoClass)
1217 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1219 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1223 quantum_type=IndexAlphaQuantum;
1224 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1228 if (image->storage_class != PseudoClass)
1230 quantum_type=GrayQuantum;
1231 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1233 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1234 if (status == MagickFalse)
1237 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1239 pixels=GetQuantumPixels(quantum_info);
1240 for (y=0; y < (ssize_t) image->rows; y++)
1248 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1251 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1252 if (q == (Quantum *) NULL)
1254 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1255 quantum_type,pixels,exception);
1257 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1259 if (image->previous == (Image *) NULL)
1261 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1263 if (status == MagickFalse)
1269 case ReadRGBAMethod:
1272 Convert TIFF image to DirectClass MIFF image.
1274 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1275 quantum_type=RGBQuantum;
1276 if (image->matte != MagickFalse)
1278 quantum_type=RGBAQuantum;
1279 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1281 if (image->colorspace == CMYKColorspace)
1283 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1284 quantum_type=CMYKQuantum;
1285 if (image->matte != MagickFalse)
1287 quantum_type=CMYKAQuantum;
1288 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1291 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1292 if (status == MagickFalse)
1295 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1297 pixels=GetQuantumPixels(quantum_info);
1298 for (y=0; y < (ssize_t) image->rows; y++)
1306 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1309 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1310 if (q == (Quantum *) NULL)
1312 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1313 quantum_type,pixels,exception);
1314 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1316 if (image->previous == (Image *) NULL)
1318 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1320 if (status == MagickFalse)
1326 case ReadCMYKAMethod:
1329 Convert TIFF image to DirectClass MIFF image.
1331 for (i=0; i < (ssize_t) samples_per_pixel; i++)
1333 for (y=0; y < (ssize_t) image->rows; y++)
1341 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1345 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1346 if (q == (Quantum *) NULL)
1348 if (image->colorspace != CMYKColorspace)
1351 case 0: quantum_type=RedQuantum; break;
1352 case 1: quantum_type=GreenQuantum; break;
1353 case 2: quantum_type=BlueQuantum; break;
1354 case 3: quantum_type=AlphaQuantum; break;
1355 default: quantum_type=UndefinedQuantum; break;
1360 case 0: quantum_type=CyanQuantum; break;
1361 case 1: quantum_type=MagentaQuantum; break;
1362 case 2: quantum_type=YellowQuantum; break;
1363 case 3: quantum_type=BlackQuantum; break;
1364 case 4: quantum_type=AlphaQuantum; break;
1365 default: quantum_type=UndefinedQuantum; break;
1367 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1368 quantum_type,pixels,exception);
1369 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1372 if (image->previous == (Image *) NULL)
1374 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1376 if (status == MagickFalse)
1382 case ReadStripMethod:
1384 register unsigned char
1388 Convert stripped TIFF image to DirectClass MIFF image.
1391 p=(unsigned char *) NULL;
1392 for (y=0; y < (ssize_t) image->rows; y++)
1400 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1401 if (q == (Quantum *) NULL)
1405 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1407 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1411 p=(unsigned char *) (((uint32 *) pixels)+image->columns*i);
1412 for (x=0; x < (ssize_t) image->columns; x++)
1414 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1416 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1418 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1420 if (image->matte != MagickFalse)
1421 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1424 q+=GetPixelChannels(image);
1426 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1428 if (image->previous == (Image *) NULL)
1430 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1432 if (status == MagickFalse)
1438 case ReadTileMethod:
1452 Convert tiled TIFF image to DirectClass MIFF image.
1454 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1455 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1458 ThrowReaderException(CoderError,"ImageIsNotTiled");
1460 (void) SetImageStorageClass(image,DirectClass,&image->exception);
1461 number_pixels=columns*rows;
1462 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
1463 sizeof(*tile_pixels));
1464 if (tile_pixels == (uint32 *) NULL)
1467 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1469 for (y=0; y < (ssize_t) image->rows; y+=rows)
1482 rows_remaining=image->rows-y;
1483 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
1484 rows_remaining=rows;
1485 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1487 if (tile == (Quantum *) NULL)
1489 for (x=0; x < (ssize_t) image->columns; x+=columns)
1495 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1497 columns_remaining=image->columns-x;
1498 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
1499 columns_remaining=columns;
1500 p=tile_pixels+(rows-rows_remaining)*columns;
1501 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1503 for (row=rows_remaining; row > 0; row--)
1505 if (image->matte != MagickFalse)
1506 for (column=columns_remaining; column > 0; column--)
1508 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1510 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1512 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1514 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1517 q+=GetPixelChannels(image);
1520 for (column=columns_remaining; column > 0; column--)
1522 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1524 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1526 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1529 q+=GetPixelChannels(image);
1531 p+=columns-columns_remaining;
1532 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
1535 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1537 if (image->previous == (Image *) NULL)
1539 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1541 if (status == MagickFalse)
1545 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1548 case ReadGenericMethod:
1558 Convert TIFF image to DirectClass MIFF image.
1560 number_pixels=(MagickSizeType) image->columns*image->rows;
1561 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1562 (number_pixels*sizeof(uint32))))
1565 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1567 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1569 if (pixels == (uint32 *) NULL)
1572 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1574 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1575 (uint32) image->rows,(uint32 *) pixels,0);
1577 Convert image to DirectClass pixel packets.
1579 p=pixels+number_pixels-1;
1580 for (y=0; y < (ssize_t) image->rows; y++)
1588 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1589 if (q == (Quantum *) NULL)
1591 q+=GetPixelChannels(image)*(image->columns-1);
1592 for (x=0; x < (ssize_t) image->columns; x++)
1594 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1596 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1598 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1600 if (image->matte != MagickFalse)
1601 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1604 q-=GetPixelChannels(image);;
1606 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1608 if (image->previous == (Image *) NULL)
1610 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1612 if (status == MagickFalse)
1616 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1620 SetQuantumImageType(image,quantum_type);
1622 if ((photometric == PHOTOMETRIC_LOGL) ||
1623 (photometric == PHOTOMETRIC_MINISBLACK) ||
1624 (photometric == PHOTOMETRIC_MINISWHITE))
1626 image->type=GrayscaleType;
1627 if (bits_per_sample == 1)
1628 image->type=BilevelType;
1630 if (image->storage_class == PseudoClass)
1631 image->depth=GetImageDepth(image,exception);
1632 image->endian=MSBEndian;
1633 if (endian == FILLORDER_LSB2MSB)
1634 image->endian=LSBEndian;
1635 if ((photometric == PHOTOMETRIC_LOGL) ||
1636 (photometric == PHOTOMETRIC_MINISBLACK) ||
1637 (photometric == PHOTOMETRIC_MINISWHITE))
1639 image->type=GrayscaleType;
1640 if (bits_per_sample == 1)
1641 image->type=BilevelType;
1644 Proceed to next image.
1646 if (image_info->number_scenes != 0)
1647 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1649 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1650 if (status == MagickTrue)
1653 Allocate next image structure.
1655 AcquireNextImage(image_info,image,exception);
1656 if (GetNextImageInList(image) == (Image *) NULL)
1658 image=DestroyImageList(image);
1659 return((Image *) NULL);
1661 image=SyncNextImageInList(image);
1662 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1664 if (status == MagickFalse)
1667 quantum_info=DestroyQuantumInfo(quantum_info);
1668 } while (status == MagickTrue);
1669 (void) TIFFSetWarningHandler(warning_handler);
1670 (void) TIFFSetErrorHandler(error_handler);
1672 return(GetFirstImageInList(image));
1677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1681 % R e g i s t e r T I F F I m a g e %
1685 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1687 % RegisterTIFFImage() adds properties for the TIFF image format to
1688 % the list of supported formats. The properties include the image format
1689 % tag, a method to read and/or write the format, whether the format
1690 % supports the saving of more than one frame to the same file or blob,
1691 % whether the format supports native in-memory I/O, and a brief
1692 % description of the format.
1694 % The format of the RegisterTIFFImage method is:
1696 % size_t RegisterTIFFImage(void)
1699 ModuleExport size_t RegisterTIFFImage(void)
1701 #define TIFFDescription "Tagged Image File Format"
1704 version[MaxTextExtent];
1709 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1710 tiff_semaphore=AllocateSemaphoreInfo();
1711 LockSemaphoreInfo(tiff_semaphore);
1712 if (instantiate_key == MagickFalse)
1714 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1715 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1716 instantiate_key=MagickTrue;
1718 UnlockSemaphoreInfo(tiff_semaphore);
1720 #if defined(TIFF_VERSION)
1721 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
1723 #if defined(MAGICKCORE_TIFF_DELEGATE)
1732 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1738 entry=SetMagickInfo("GROUP4");
1739 #if defined(MAGICKCORE_TIFF_DELEGATE)
1740 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1741 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1743 entry->raw=MagickTrue;
1744 entry->endian_support=MagickTrue;
1745 entry->adjoin=MagickFalse;
1746 entry->format_type=ImplicitFormatType;
1747 entry->seekable_stream=MagickTrue;
1748 entry->thread_support=NoThreadSupport;
1749 entry->description=ConstantString("Raw CCITT Group4");
1750 entry->module=ConstantString("TIFF");
1751 (void) RegisterMagickInfo(entry);
1752 entry=SetMagickInfo("PTIF");
1753 #if defined(MAGICKCORE_TIFF_DELEGATE)
1754 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1755 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1757 entry->endian_support=MagickTrue;
1758 entry->seekable_stream=MagickTrue;
1759 entry->thread_support=NoThreadSupport;
1760 entry->description=ConstantString("Pyramid encoded TIFF");
1761 entry->module=ConstantString("TIFF");
1762 (void) RegisterMagickInfo(entry);
1763 entry=SetMagickInfo("TIF");
1764 #if defined(MAGICKCORE_TIFF_DELEGATE)
1765 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1766 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1768 entry->endian_support=MagickTrue;
1769 entry->seekable_stream=MagickTrue;
1770 entry->stealth=MagickTrue;
1771 entry->thread_support=NoThreadSupport;
1772 entry->description=ConstantString(TIFFDescription);
1773 if (*version != '\0')
1774 entry->version=ConstantString(version);
1775 entry->module=ConstantString("TIFF");
1776 (void) RegisterMagickInfo(entry);
1777 entry=SetMagickInfo("TIFF");
1778 #if defined(MAGICKCORE_TIFF_DELEGATE)
1779 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1780 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1782 entry->magick=(IsImageFormatHandler *) IsTIFF;
1783 entry->endian_support=MagickTrue;
1784 entry->seekable_stream=MagickTrue;
1785 entry->thread_support=NoThreadSupport;
1786 entry->description=ConstantString(TIFFDescription);
1787 if (*version != '\0')
1788 entry->version=ConstantString(version);
1789 entry->module=ConstantString("TIFF");
1790 (void) RegisterMagickInfo(entry);
1791 entry=SetMagickInfo("TIFF64");
1792 #if defined(TIFF_VERSION_BIG)
1793 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1794 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1796 entry->adjoin=MagickFalse;
1797 entry->endian_support=MagickTrue;
1798 entry->seekable_stream=MagickTrue;
1799 entry->thread_support=NoThreadSupport;
1800 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1801 if (*version != '\0')
1802 entry->version=ConstantString(version);
1803 entry->module=ConstantString("TIFF");
1804 (void) RegisterMagickInfo(entry);
1805 return(MagickImageCoderSignature);
1809 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1813 % U n r e g i s t e r T I F F I m a g e %
1817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1819 % UnregisterTIFFImage() removes format registrations made by the TIFF module
1820 % from the list of supported formats.
1822 % The format of the UnregisterTIFFImage method is:
1824 % UnregisterTIFFImage(void)
1827 ModuleExport void UnregisterTIFFImage(void)
1829 (void) UnregisterMagickInfo("RAWGROUP4");
1830 (void) UnregisterMagickInfo("PTIF");
1831 (void) UnregisterMagickInfo("TIF");
1832 (void) UnregisterMagickInfo("TIFF");
1833 (void) UnregisterMagickInfo("TIFF64");
1834 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1835 tiff_semaphore=AllocateSemaphoreInfo();
1836 LockSemaphoreInfo(tiff_semaphore);
1837 if (instantiate_key != MagickFalse)
1838 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1839 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1840 instantiate_key=MagickFalse;
1841 UnlockSemaphoreInfo(tiff_semaphore);
1842 DestroySemaphoreInfo(&tiff_semaphore);
1845 #if defined(MAGICKCORE_TIFF_DELEGATE)
1847 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1851 % W r i t e G R O U P 4 I m a g e %
1855 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1857 % WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1859 % The format of the WriteGROUP4Image method is:
1861 % MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1862 % Image *image,ExceptionInfo *)
1864 % A description of each parameter follows:
1866 % o image_info: the image info.
1868 % o image: The image.
1870 % o exception: return any errors or warnings in this structure.
1873 static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1874 Image *image,ExceptionInfo *exception)
1877 filename[MaxTextExtent];
1911 Write image as CCITT Group4 TIFF image to a temporary file.
1913 assert(image_info != (const ImageInfo *) NULL);
1914 assert(image_info->signature == MagickSignature);
1915 assert(image != (Image *) NULL);
1916 assert(image->signature == MagickSignature);
1917 if (image->debug != MagickFalse)
1918 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1919 assert(exception != (ExceptionInfo *) NULL);
1920 assert(exception->signature == MagickSignature);
1921 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1922 if (status == MagickFalse)
1924 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
1925 if (huffman_image == (Image *) NULL)
1927 (void) CloseBlob(image);
1928 return(MagickFalse);
1930 huffman_image->endian=MSBEndian;
1932 unique_file=AcquireUniqueFileResource(filename);
1933 if (unique_file != -1)
1934 file=fdopen(unique_file,"wb");
1935 if ((unique_file == -1) || (file == (FILE *) NULL))
1937 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1939 return(MagickFalse);
1941 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1943 (void) SetImageType(huffman_image,BilevelType,exception);
1944 write_info=CloneImageInfo((ImageInfo *) NULL);
1945 SetImageInfoFile(write_info,file);
1946 write_info->compression=Group4Compression;
1947 write_info->type=BilevelType;
1948 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1949 status=WriteTIFFImage(write_info,huffman_image,exception);
1950 (void) fflush(file);
1951 write_info=DestroyImageInfo(write_info);
1952 if (status == MagickFalse)
1954 huffman_image=DestroyImage(huffman_image);
1955 (void) fclose(file);
1956 (void) RelinquishUniqueFileResource(filename);
1957 return(MagickFalse);
1959 tiff=TIFFOpen(filename,"rb");
1960 if (tiff == (TIFF *) NULL)
1962 huffman_image=DestroyImage(huffman_image);
1963 (void) fclose(file);
1964 (void) RelinquishUniqueFileResource(filename);
1965 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
1966 image_info->filename);
1967 return(MagickFalse);
1970 Allocate raw strip buffer.
1972 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1975 huffman_image=DestroyImage(huffman_image);
1976 (void) fclose(file);
1977 (void) RelinquishUniqueFileResource(filename);
1978 return(MagickFalse);
1980 strip_size=byte_count[0];
1981 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
1982 if (byte_count[i] > strip_size)
1983 strip_size=byte_count[i];
1984 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1986 if (buffer == (unsigned char *) NULL)
1989 huffman_image=DestroyImage(huffman_image);
1990 (void) fclose(file);
1991 (void) RelinquishUniqueFileResource(filename);
1992 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1993 image_info->filename);
1996 Compress runlength encoded to 2D Huffman pixels.
1998 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
2000 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
2001 if (WriteBlob(image,(size_t) count,buffer) != count)
2004 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2006 huffman_image=DestroyImage(huffman_image);
2007 (void) fclose(file);
2008 (void) RelinquishUniqueFileResource(filename);
2009 (void) CloseBlob(image);
2014 #if defined(MAGICKCORE_TIFF_DELEGATE)
2016 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2020 % W r i t e P T I F I m a g e %
2024 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2026 % WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2029 % The format of the WritePTIFImage method is:
2031 % MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2032 % Image *image,ExceptionInfo *exception)
2034 % A description of each parameter follows:
2036 % o image_info: the image info.
2038 % o image: The image.
2040 % o exception: return any errors or warnings in this structure.
2043 static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2044 Image *image,ExceptionInfo *exception)
2062 Create pyramid-encoded TIFF image.
2064 images=NewImageList();
2065 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2067 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,exception));
2068 columns=next->columns;
2070 while ((columns > 64) && (rows > 64))
2074 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2076 AppendImageToList(&images,pyramid_image);
2080 Write pyramid-encoded TIFF image.
2082 write_info=CloneImageInfo(image_info);
2083 *write_info->magick='\0';
2084 write_info->adjoin=MagickTrue;
2085 status=WriteTIFFImage(write_info,GetFirstImageInList(images),exception);
2086 images=DestroyImageList(images);
2087 write_info=DestroyImageInfo(write_info);
2092 #if defined(MAGICKCORE_TIFF_DELEGATE)
2094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2097 % W r i t e T I F F I m a g e %
2101 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2103 % WriteTIFFImage() writes an image in the Tagged image file format.
2105 % The format of the WriteTIFFImage method is:
2107 % MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2108 % Image *image,ExceptionInfo *exception)
2110 % A description of each parameter follows:
2112 % o image_info: the image info.
2114 % o image: The image.
2116 % o exception: return any errors or warnings in this structure.
2120 typedef struct _TIFFInfo
2131 static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2133 assert(tiff_info != (TIFFInfo *) NULL);
2134 if (tiff_info->scanlines != (unsigned char *) NULL)
2135 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2136 tiff_info->scanlines);
2137 if (tiff_info->pixels != (unsigned char *) NULL)
2138 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2142 static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2143 TIFFInfo *tiff_info)
2155 assert(tiff_info != (TIFFInfo *) NULL);
2156 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2157 option=GetImageOption(image_info,"tiff:tile-geometry");
2158 if (option == (const char *) NULL)
2160 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2161 if ((flags & HeightValue) == 0)
2162 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2163 tile_columns=(uint32) tiff_info->tile_geometry.width;
2164 tile_rows=(uint32) tiff_info->tile_geometry.height;
2165 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2166 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2167 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2168 tiff_info->tile_geometry.width=tile_columns;
2169 tiff_info->tile_geometry.height=tile_rows;
2170 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2171 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
2172 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2173 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2174 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2175 (tiff_info->pixels == (unsigned char *) NULL))
2177 DestroyTIFFInfo(tiff_info);
2178 return(MagickFalse);
2183 static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
2184 tsample_t sample,Image *image)
2192 register unsigned char
2206 if (TIFFIsTiled(tiff) == 0)
2207 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2209 Fill scanlines to tile height.
2211 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2212 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2213 (size_t) TIFFScanlineSize(tiff));
2214 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2215 (tiff_info->tile_geometry.height-1)) &&
2216 (row != (ssize_t) (image->rows-1)))
2219 Write tile to TIFF image.
2222 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
2223 tiff_info->tile_geometry.width);
2224 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2225 tiff_info->tile_geometry.width;
2226 for (i=0; i < (ssize_t) number_tiles; i++)
2228 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
2229 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2230 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2231 for (k=0; k < (ssize_t) tile_width; k++)
2233 if (bytes_per_pixel == 0)
2235 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2236 tiff_info->tile_geometry.width+k)/8);
2237 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2241 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2242 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2243 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2244 for (l=0; l < bytes_per_pixel; l++)
2247 if ((i*tiff_info->tile_geometry.width) != image->columns)
2248 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2249 tiff_info->tile_geometry.width),(uint32) ((row/
2250 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2258 static void TIFFSetProfiles(TIFF *tiff,Image *image)
2266 if (image->profiles == (void *) NULL)
2268 ResetImageProfileIterator(image);
2269 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2271 profile=GetImageProfile(image,name);
2272 #if defined(TIFFTAG_XMLPACKET)
2273 if (LocaleCompare(name,"xmp") == 0)
2274 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2275 profile),GetStringInfoDatum(profile));
2277 #if defined(TIFFTAG_ICCPROFILE)
2278 if (LocaleCompare(name,"icc") == 0)
2279 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2280 profile),GetStringInfoDatum(profile));
2282 if (LocaleCompare(name,"iptc") == 0)
2290 iptc_profile=CloneStringInfo(profile);
2291 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2293 SetStringInfoLength(iptc_profile,length);
2294 if (TIFFIsByteSwapped(tiff))
2295 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2296 (unsigned long) (length/4));
2297 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2298 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2299 iptc_profile=DestroyStringInfo(iptc_profile);
2301 #if defined(TIFFTAG_PHOTOSHOP)
2302 if (LocaleCompare(name,"8bim") == 0)
2303 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2304 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2306 if (LocaleCompare(name,"tiff:37724") == 0)
2307 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2308 GetStringInfoDatum(profile));
2309 name=GetNextImageProfile(image);
2313 static void TIFFSetProperties(TIFF *tiff,Image *image)
2318 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
2319 value=GetImageProperty(image,"tiff:hostcomputer");
2320 if (value != (const char *) NULL)
2321 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2322 value=GetImageProperty(image,"tiff:artist");
2323 if (value != (const char *) NULL)
2324 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2325 value=GetImageProperty(image,"tiff:timestamp");
2326 if (value != (const char *) NULL)
2327 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2328 value=GetImageProperty(image,"tiff:make");
2329 if (value != (const char *) NULL)
2330 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2331 value=GetImageProperty(image,"tiff:model");
2332 if (value != (const char *) NULL)
2333 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2334 value=GetImageProperty(image,"tiff:software");
2335 if (value != (const char *) NULL)
2336 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
2337 value=GetImageProperty(image,"tiff:copyright");
2338 if (value != (const char *) NULL)
2339 (void) TIFFSetField(tiff,33432,value);
2340 value=GetImageProperty(image,"kodak-33423");
2341 if (value != (const char *) NULL)
2342 (void) TIFFSetField(tiff,33423,value);
2343 value=GetImageProperty(image,"kodak-36867");
2344 if (value != (const char *) NULL)
2345 (void) TIFFSetField(tiff,36867,value);
2346 value=GetImageProperty(image,"label");
2347 if (value != (const char *) NULL)
2348 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2349 value=GetImageProperty(image,"comment");
2350 if (value != (const char *) NULL)
2351 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2354 static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2356 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2367 Write EXIF properties.
2370 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2371 for (i=0; exif_info[i].tag != 0; i++)
2373 value=GetImageProperty(image,exif_info[i].property);
2374 if (value == (const char *) NULL)
2376 switch (exif_info[i].type)
2380 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2388 shorty=(uint16) StringToLong(value);
2389 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2397 ssize_ty=(uint16) StringToLong(value);
2398 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
2402 case TIFF_SRATIONAL:
2407 rational=InterpretLocaleValue(value,(char **) NULL);
2408 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2415 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2422 static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2423 Image *image,ExceptionInfo *exception)
2425 #if !defined(TIFFDefaultStripSize)
2426 #define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2487 assert(image_info != (const ImageInfo *) NULL);
2488 assert(image_info->signature == MagickSignature);
2489 assert(image != (Image *) NULL);
2490 assert(image->signature == MagickSignature);
2491 if (image->debug != MagickFalse)
2492 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2493 assert(exception != (ExceptionInfo *) NULL);
2494 assert(exception->signature == MagickSignature);
2495 assert(exception != (ExceptionInfo *) NULL);
2496 assert(exception->signature == MagickSignature);
2497 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2498 if (status == MagickFalse)
2500 (void) MagickSetThreadValue(tiff_exception,exception);
2501 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2502 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
2503 endian_type=UndefinedEndian;
2504 option=GetImageOption(image_info,"tiff:endian");
2505 if (option != (const char *) NULL)
2507 if (LocaleNCompare(option,"msb",3) == 0)
2508 endian_type=MSBEndian;
2509 if (LocaleNCompare(option,"lsb",3) == 0)
2510 endian_type=LSBEndian;;
2512 switch (endian_type)
2514 case LSBEndian: mode="wl"; break;
2515 case MSBEndian: mode="wb"; break;
2516 default: mode="w"; break;
2518 #if defined(TIFF_VERSION_BIG)
2519 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
2520 switch (endian_type)
2522 case LSBEndian: mode="wl8"; break;
2523 case MSBEndian: mode="wb8"; break;
2524 default: mode="w8"; break;
2527 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2528 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2530 if (tiff == (TIFF *) NULL)
2532 (void) TIFFSetWarningHandler(warning_handler);
2533 (void) TIFFSetErrorHandler(error_handler);
2534 return(MagickFalse);
2537 debug=IsEventLogging();
2542 Initialize TIFF fields.
2544 if ((image_info->type != UndefinedType) &&
2545 (image_info->type != OptimizeType))
2546 (void) SetImageType(image,image_info->type,exception);
2547 quantum_info=AcquireQuantumInfo(image_info,image);
2548 if (quantum_info == (QuantumInfo *) NULL)
2549 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2550 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2551 (quantum_info->format == UndefinedQuantumFormat) &&
2552 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
2554 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2555 if (status == MagickFalse)
2556 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2558 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2559 (GetPreviousImageInList(image) != (Image *) NULL))
2560 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2561 if ((image->columns != (uint32) image->columns) ||
2562 (image->rows != (uint32) image->rows))
2563 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2564 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2565 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2566 compression=image->compression;
2567 if (image_info->compression != UndefinedCompression)
2568 compression=image_info->compression;
2569 switch (compression)
2571 case FaxCompression:
2573 compress_tag=COMPRESSION_CCITTFAX3;
2574 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2577 case Group4Compression:
2579 compress_tag=COMPRESSION_CCITTFAX4;
2580 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2583 #if defined(COMPRESSION_JBIG)
2584 case JBIG1Compression:
2586 compress_tag=COMPRESSION_JBIG;
2590 case JPEGCompression:
2592 compress_tag=COMPRESSION_JPEG;
2595 #if defined(COMPRESSION_LZMA)
2596 case LZMACompression:
2598 compress_tag=COMPRESSION_LZMA;
2602 case LZWCompression:
2604 compress_tag=COMPRESSION_LZW;
2607 case RLECompression:
2609 compress_tag=COMPRESSION_PACKBITS;
2612 case ZipCompression:
2614 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2620 compress_tag=COMPRESSION_NONE;
2624 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2625 if ((compress_tag != COMPRESSION_NONE) &&
2626 (TIFFIsCODECConfigured(compress_tag) == 0))
2628 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2629 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
2630 MagickCompressOptions,(ssize_t) compression));
2631 compress_tag=COMPRESSION_NONE;
2632 compression=NoCompression;
2635 switch (compress_tag)
2637 #if defined(CCITT_SUPPORT)
2638 case COMPRESSION_CCITTFAX3:
2639 case COMPRESSION_CCITTFAX4:
2641 #if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2642 case COMPRESSION_JPEG:
2644 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2645 case COMPRESSION_LZMA:
2647 #if defined(LZW_SUPPORT)
2648 case COMPRESSION_LZW:
2650 #if defined(PACKBITS_SUPPORT)
2651 case COMPRESSION_PACKBITS:
2653 #if defined(ZIP_SUPPORT)
2654 case COMPRESSION_ADOBE_DEFLATE:
2656 case COMPRESSION_NONE:
2660 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2661 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
2662 MagickCompressOptions,(ssize_t) compression));
2663 compress_tag=COMPRESSION_NONE;
2664 compression=NoCompression;
2669 switch (compression)
2671 case FaxCompression:
2672 case Group4Compression:
2674 (void) SetImageType(image,BilevelType,exception);
2677 case JPEGCompression:
2679 (void) SetImageStorageClass(image,DirectClass,exception);
2680 (void) SetImageDepth(image,8);
2686 if (image->colorspace == CMYKColorspace)
2688 photometric=PHOTOMETRIC_SEPARATED;
2689 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2690 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2695 Full color TIFF raster.
2697 if (image->colorspace == LabColorspace)
2698 photometric=PHOTOMETRIC_CIELAB;
2700 if (image->colorspace == YCbCrColorspace)
2702 photometric=PHOTOMETRIC_YCBCR;
2703 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
2704 (void) SetImageStorageClass(image,DirectClass,exception);
2705 (void) SetImageDepth(image,8);
2709 if (IsRGBColorspace(image->colorspace) == MagickFalse)
2710 (void) TransformImageColorspace(image,RGBColorspace);
2711 photometric=PHOTOMETRIC_RGB;
2713 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2714 if ((image_info->type != TrueColorType) &&
2715 (image_info->type != TrueColorMatteType))
2717 if ((image_info->type != PaletteType) &&
2718 (IsImageGray(image,exception) != MagickFalse))
2720 photometric=(uint16) (quantum_info->min_is_white !=
2721 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2722 PHOTOMETRIC_MINISBLACK);
2723 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
2724 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
2725 (IsImageMonochrome(image,exception) != MagickFalse))
2727 status=SetQuantumDepth(image,quantum_info,1);
2728 if (status == MagickFalse)
2729 ThrowWriterException(ResourceLimitError,
2730 "MemoryAllocationFailed");
2734 if (image->storage_class == PseudoClass)
2740 Colormapped TIFF raster.
2742 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
2743 photometric=PHOTOMETRIC_PALETTE;
2745 while ((GetQuantumRange(depth)+1) < image->colors)
2747 status=SetQuantumDepth(image,quantum_info,depth);
2748 if (status == MagickFalse)
2749 ThrowWriterException(ResourceLimitError,
2750 "MemoryAllocationFailed");
2754 switch (image->endian)
2758 endian=FILLORDER_LSB2MSB;
2763 endian=FILLORDER_MSB2LSB;
2766 case UndefinedEndian:
2769 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2774 image->endian=MSBEndian;
2775 if ((int) (*(char *) &lsb_first) != 0)
2776 image->endian=LSBEndian;
2777 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2778 (photometric != PHOTOMETRIC_MINISWHITE))
2780 compress_tag=COMPRESSION_NONE;
2781 endian=FILLORDER_MSB2LSB;
2784 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2785 (photometric != PHOTOMETRIC_MINISWHITE))
2787 compress_tag=COMPRESSION_NONE;
2788 endian=FILLORDER_MSB2LSB;
2790 option=GetImageProperty(image,"tiff:fill-order");
2791 if (option != (const char *) NULL)
2793 if (LocaleNCompare(option,"msb",3) == 0)
2794 endian=FILLORDER_MSB2LSB;
2795 if (LocaleNCompare(option,"lsb",3) == 0)
2796 endian=FILLORDER_LSB2MSB;
2798 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2799 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
2800 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2801 if (image->matte != MagickFalse)
2809 TIFF has a matte channel.
2812 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2813 option=GetImageProperty(image,"tiff:alpha");
2814 if ((option != (const char *) NULL) &&
2815 (LocaleCompare(option,"associated") == 0))
2816 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2817 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2818 &samples_per_pixel);
2819 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2820 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2822 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2823 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2825 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2826 switch (quantum_info->format)
2828 case FloatingPointQuantumFormat:
2830 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2831 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2832 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2835 case SignedQuantumFormat:
2837 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2840 case UnsignedQuantumFormat:
2842 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2848 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2849 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2850 if (photometric == PHOTOMETRIC_RGB)
2851 if ((image_info->interlace == PlaneInterlace) ||
2852 (image_info->interlace == PartitionInterlace))
2853 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2855 if (TIFFScanlineSize(tiff) != 0)
2856 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2858 option=GetImageOption(image_info,"tiff:rows-per-strip");
2859 if (option != (const char *) NULL)
2860 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
2861 switch (compress_tag)
2863 case COMPRESSION_JPEG:
2865 #if defined(JPEG_SUPPORT)
2875 rows_per_strip+=(16-(rows_per_strip % 16));
2876 if (image->quality != 0)
2877 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2878 if (image_info->quality != UndefinedCompressionQuality)
2879 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2880 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2881 if (IsRGBColorspace(image->colorspace) == MagickTrue)
2886 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2887 sampling_factor=(const char *) NULL;
2888 value=GetImageProperty(image,"jpeg:sampling-factor");
2889 if (value != (char *) NULL)
2891 sampling_factor=value;
2892 if (image->debug != MagickFalse)
2893 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2894 " Input sampling-factors=%s",sampling_factor);
2896 if (image_info->sampling_factor != (char *) NULL)
2897 sampling_factor=image_info->sampling_factor;
2898 if (sampling_factor != (const char *) NULL)
2900 flags=ParseGeometry(sampling_factor,&geometry_info);
2901 if ((flags & SigmaValue) == 0)
2902 geometry_info.sigma=geometry_info.rho;
2903 if (image->colorspace == YCbCrColorspace)
2904 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2905 geometry_info.rho,(uint16) geometry_info.sigma);
2908 if (bits_per_sample == 12)
2909 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2913 case COMPRESSION_ADOBE_DEFLATE:
2915 rows_per_strip=(uint32) image->rows;
2916 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2918 if (((photometric == PHOTOMETRIC_RGB) ||
2919 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2920 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2921 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2922 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2923 image_info->quality == UndefinedCompressionQuality ? 7 :
2924 MagickMin((ssize_t) image_info->quality/10,9)));
2927 case COMPRESSION_CCITTFAX3:
2932 rows_per_strip=(uint32) image->rows;
2933 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2936 case COMPRESSION_CCITTFAX4:
2938 rows_per_strip=(uint32) image->rows;
2941 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2942 case COMPRESSION_LZMA:
2944 if (((photometric == PHOTOMETRIC_RGB) ||
2945 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2946 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2947 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2948 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
2949 image_info->quality == UndefinedCompressionQuality ? 7 :
2950 MagickMin((ssize_t) image_info->quality/10,9)));
2954 case COMPRESSION_LZW:
2956 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2958 if (((photometric == PHOTOMETRIC_RGB) ||
2959 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2960 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2961 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2967 option=GetImageOption(image_info,"tiff:tile-geometry");
2968 if (option == (const char *) NULL)
2969 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
2970 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2976 Set image resolution.
2979 if (image->units == PixelsPerInchResolution)
2981 if (image->units == PixelsPerCentimeterResolution)
2982 units=RESUNIT_CENTIMETER;
2983 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2984 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2985 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2986 if ((image->page.x != 0) || (image->page.y != 0))
2991 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2992 image->x_resolution);
2993 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2994 image->y_resolution);
2997 if (image->chromaticity.white_point.x != 0.0)
3003 Set image chromaticity.
3005 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3006 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3007 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3008 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3009 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3010 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3011 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3012 chromaticity[0]=(float) image->chromaticity.white_point.x;
3013 chromaticity[1]=(float) image->chromaticity.white_point.y;
3014 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3016 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3018 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3019 if (image->scene != 0)
3020 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3021 GetImageListLength(image));
3023 if (image->orientation != UndefinedOrientation)
3024 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3025 (void) TIFFSetProfiles(tiff,image);
3031 page=(uint16) scene;
3032 pages=(uint16) GetImageListLength(image);
3033 if ((image_info->adjoin != MagickFalse) && (pages > 1))
3034 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3035 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3037 (void) TIFFSetProperties(tiff,image);
3039 (void) TIFFSetEXIFProperties(tiff,image);
3041 Write image scanlines.
3043 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3044 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3045 pixels=GetQuantumPixels(quantum_info);
3046 tiff_info.scanline=GetQuantumPixels(quantum_info);
3047 switch (photometric)
3049 case PHOTOMETRIC_CIELAB:
3050 case PHOTOMETRIC_YCBCR:
3051 case PHOTOMETRIC_RGB:
3056 switch (image_info->interlace)
3061 quantum_type=RGBQuantum;
3062 if (image->matte != MagickFalse)
3063 quantum_type=RGBAQuantum;
3064 for (y=0; y < (ssize_t) image->rows; y++)
3066 register const Quantum
3069 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3070 if (p == (const Quantum *) NULL)
3072 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3073 quantum_type,pixels,exception);
3075 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3077 if (image->previous == (Image *) NULL)
3079 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3081 if (status == MagickFalse)
3087 case PlaneInterlace:
3088 case PartitionInterlace:
3091 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3093 for (y=0; y < (ssize_t) image->rows; y++)
3095 register const Quantum
3098 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3099 if (p == (const Quantum *) NULL)
3101 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3102 RedQuantum,pixels,exception);
3103 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3106 if (image->previous == (Image *) NULL)
3108 status=SetImageProgress(image,SaveImageTag,100,400);
3109 if (status == MagickFalse)
3112 for (y=0; y < (ssize_t) image->rows; y++)
3114 register const Quantum
3117 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3118 if (p == (const Quantum *) NULL)
3120 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3121 GreenQuantum,pixels,exception);
3122 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3125 if (image->previous == (Image *) NULL)
3127 status=SetImageProgress(image,SaveImageTag,200,400);
3128 if (status == MagickFalse)
3131 for (y=0; y < (ssize_t) image->rows; y++)
3133 register const Quantum
3136 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3137 if (p == (const Quantum *) NULL)
3139 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3140 BlueQuantum,pixels,exception);
3141 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3144 if (image->previous == (Image *) NULL)
3146 status=SetImageProgress(image,SaveImageTag,300,400);
3147 if (status == MagickFalse)
3150 if (image->matte != MagickFalse)
3151 for (y=0; y < (ssize_t) image->rows; y++)
3153 register const Quantum
3156 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3157 if (p == (const Quantum *) NULL)
3159 length=ExportQuantumPixels(image,(CacheView *) NULL,
3160 quantum_info,AlphaQuantum,pixels,exception);
3161 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3164 if (image->previous == (Image *) NULL)
3166 status=SetImageProgress(image,SaveImageTag,400,400);
3167 if (status == MagickFalse)
3175 case PHOTOMETRIC_SEPARATED:
3180 quantum_type=CMYKQuantum;
3181 if (image->matte != MagickFalse)
3182 quantum_type=CMYKAQuantum;
3183 if (image->colorspace != CMYKColorspace)
3184 (void) TransformImageColorspace(image,CMYKColorspace);
3185 for (y=0; y < (ssize_t) image->rows; y++)
3187 register const Quantum
3190 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3191 if (p == (const Quantum *) NULL)
3193 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3194 quantum_type,pixels,exception);
3195 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3197 if (image->previous == (Image *) NULL)
3199 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3201 if (status == MagickFalse)
3207 case PHOTOMETRIC_PALETTE:
3215 Colormapped TIFF image.
3217 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3218 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3219 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3220 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3221 (blue == (uint16 *) NULL))
3222 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3224 Initialize TIFF colormap.
3226 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3227 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3228 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3229 for (i=0; i < (ssize_t) image->colors; i++)
3231 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3232 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3233 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3235 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3236 red=(uint16 *) RelinquishMagickMemory(red);
3237 green=(uint16 *) RelinquishMagickMemory(green);
3238 blue=(uint16 *) RelinquishMagickMemory(blue);
3243 Convert PseudoClass packets to contiguous grayscale scanlines.
3245 quantum_type=IndexQuantum;
3246 if (image->matte != MagickFalse)
3248 if (photometric != PHOTOMETRIC_PALETTE)
3249 quantum_type=GrayAlphaQuantum;
3251 quantum_type=IndexAlphaQuantum;
3254 if (photometric != PHOTOMETRIC_PALETTE)
3255 quantum_type=GrayQuantum;
3256 for (y=0; y < (ssize_t) image->rows; y++)
3258 register const Quantum
3261 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3262 if (p == (const Quantum *) NULL)
3264 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3265 quantum_type,pixels,exception);
3266 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3268 if (image->previous == (Image *) NULL)
3270 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3272 if (status == MagickFalse)
3279 quantum_info=DestroyQuantumInfo(quantum_info);
3280 DestroyTIFFInfo(&tiff_info);
3281 if (0 && (image_info->verbose == MagickTrue))
3282 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3283 (void) TIFFWriteDirectory(tiff);
3284 image->endian=MSBEndian;
3285 if (endian == FILLORDER_LSB2MSB)
3286 image->endian=LSBEndian;
3287 image=SyncNextImageInList(image);
3288 if (image == (Image *) NULL)
3290 status=SetImageProgress(image,SaveImagesTag,scene++,
3291 GetImageListLength(image));
3292 if (status == MagickFalse)
3294 } while (image_info->adjoin != MagickFalse);
3295 (void) TIFFSetWarningHandler(warning_handler);
3296 (void) TIFFSetErrorHandler(error_handler);