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_UNDEFINED, "exif:OptoelectricConversionFactor" },
117 { EXIFTAG_EXIFVERSION, TIFF_UNDEFINED, "exif:ExifVersion" },
118 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
119 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
120 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_UNDEFINED, "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_SHORT, "exif:SubjectArea" }, */
133 { EXIFTAG_MAKERNOTE, TIFF_UNDEFINED, "exif:MakerNote" },
134 { EXIFTAG_USERCOMMENT, TIFF_UNDEFINED, "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_UNDEFINED, "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_UNDEFINED, "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_UNDEFINED, "exif:FileSource" },
153 { EXIFTAG_SCENETYPE, TIFF_UNDEFINED, "exif:SceneType" },
154 { EXIFTAG_CFAPATTERN, TIFF_UNDEFINED, "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_UNDEFINED, "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);
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,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)
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)
512 #if defined(TIFFTAG_ICCPROFILE)
514 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
515 (void) ReadProfile(image,"icc",profile,(ssize_t) length);
517 #if defined(TIFFTAG_PHOTOSHOP)
519 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
520 (void) ReadProfile(image,"8bim",profile,(ssize_t) length);
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);
531 #if defined(TIFFTAG_XMLPACKET)
533 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
534 (void) ReadProfile(image,"xmp",profile,(ssize_t) length);
537 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
538 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length);
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];
584 Read EXIF properties.
586 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
588 directory=TIFFCurrentDirectory(tiff);
589 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
591 for (i=0; exif_info[i].tag != 0; i++)
594 switch (exif_info[i].type)
601 if (TIFFGetField(tiff,exif_info[i].tag,&ascii) != 0)
602 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
610 if (TIFFGetField(tiff,exif_info[i].tag,&shorty) != 0)
611 (void) FormatLocaleString(value,MaxTextExtent,"%d",shorty);
619 if (TIFFGetField(tiff,exif_info[i].tag,&ssize_ty) != 0)
620 (void) FormatLocaleString(value,MaxTextExtent,"%d",ssize_ty);
629 if (TIFFGetField(tiff,exif_info[i].tag,&rational) != 0)
630 (void) FormatLocaleString(value,MaxTextExtent,"%g",rational);
637 (void) SetImageProperty(image,exif_info[i].property,value);
639 TIFFSetDirectory(tiff,directory);
646 static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
648 *base=(tdata_t *) GetBlobStreamData((Image *) image);
649 if (*base != (tdata_t *) NULL)
650 *size=(toff_t) GetBlobSize((Image *) image);
651 if (*base != (tdata_t *) NULL)
656 static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
661 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
662 (unsigned char *) data);
666 static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
667 tsample_t sample,ssize_t row,tdata_t scanline)
672 (void) bits_per_sample;
673 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
677 static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
679 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
682 static toff_t TIFFGetBlobSize(thandle_t image)
684 return((toff_t) GetBlobSize((Image *) image));
687 static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
694 static void TIFFWarnings(const char *module,const char *format,va_list warning)
697 message[MaxTextExtent];
702 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
703 (void) vsnprintf(message,MaxTextExtent,format,warning);
705 (void) vsprintf(message,format,warning);
707 (void) ConcatenateMagickString(message,".",MaxTextExtent);
708 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
709 if (exception != (ExceptionInfo *) NULL)
710 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
711 message,"`%s'",module);
714 static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
719 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
720 (unsigned char *) data);
724 #if defined(__cplusplus) || defined(c_plusplus)
728 static Image *ReadTIFFImage(const ImageInfo *image_info,
729 ExceptionInfo *exception)
733 ReadSingleSampleMethod,
817 assert(image_info != (const ImageInfo *) NULL);
818 assert(image_info->signature == MagickSignature);
819 if (image_info->debug != MagickFalse)
820 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
821 image_info->filename);
822 assert(exception != (ExceptionInfo *) NULL);
823 assert(exception->signature == MagickSignature);
824 image=AcquireImage(image_info);
825 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
826 if (status == MagickFalse)
828 image=DestroyImageList(image);
829 return((Image *) NULL);
831 (void) MagickSetThreadValue(tiff_exception,exception);
832 error_handler=TIFFSetErrorHandler(TIFFErrors);
833 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
834 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
835 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
837 if (tiff == (TIFF *) NULL)
839 (void) TIFFSetWarningHandler(warning_handler);
840 (void) TIFFSetErrorHandler(error_handler);
841 image=DestroyImageList(image);
842 return((Image *) NULL);
844 debug=IsEventLogging();
846 if (image_info->number_scenes != 0)
849 Generate blank images for subimage specification (e.g. image.tif[4].
851 for (i=0; i < (ssize_t) image_info->scene; i++)
853 (void) TIFFReadDirectory(tiff);
854 AcquireNextImage(image_info,image);
855 if (GetNextImageInList(image) == (Image *) NULL)
857 image=DestroyImageList(image);
858 return((Image *) NULL);
860 image=SyncNextImageInList(image);
865 if (0 && (image_info->verbose != MagickFalse))
866 TIFFPrintDirectory(tiff,stdout,MagickFalse);
867 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
872 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
873 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
874 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
875 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
876 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
877 if (sample_format == SAMPLEFORMAT_IEEEFP)
878 (void) SetImageProperty(image,"quantum:format","floating-point");
879 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
880 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
881 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
884 case PHOTOMETRIC_MINISBLACK:
886 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
889 case PHOTOMETRIC_MINISWHITE:
891 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
894 case PHOTOMETRIC_PALETTE:
896 (void) SetImageProperty(image,"tiff:photometric","palette");
899 case PHOTOMETRIC_RGB:
901 (void) SetImageProperty(image,"tiff:photometric","RGB");
904 case PHOTOMETRIC_CIELAB:
906 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
909 case PHOTOMETRIC_SEPARATED:
911 (void) SetImageProperty(image,"tiff:photometric","separated");
914 case PHOTOMETRIC_YCBCR:
916 (void) SetImageProperty(image,"tiff:photometric","YCBCR");
921 (void) SetImageProperty(image,"tiff:photometric","unknown");
925 if (image->debug != MagickFalse)
927 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
928 (unsigned int) width,(unsigned int) height);
929 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
931 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
932 "Bits per sample: %u",bits_per_sample);
933 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
934 "Min sample value: %u",min_sample_value);
935 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
936 "Max sample value: %u",max_sample_value);
937 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
938 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
940 image->columns=(size_t) width;
941 image->rows=(size_t) height;
942 image->depth=(size_t) bits_per_sample;
943 if (image->debug != MagickFalse)
944 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
945 (double) image->depth);
947 image->endian=MSBEndian;
948 if ((int) (*(char *) &lsb_first) != 0)
949 image->endian=LSBEndian;
950 if (photometric == PHOTOMETRIC_SEPARATED)
951 image->colorspace=CMYKColorspace;
952 if (photometric == PHOTOMETRIC_CIELAB)
953 image->colorspace=LabColorspace;
954 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
956 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
957 x_resolution=(float) image->x_resolution;
958 y_resolution=(float) image->y_resolution;
959 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
960 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
961 image->x_resolution=x_resolution;
962 image->y_resolution=y_resolution;
963 x_position=(float) image->page.x/x_resolution;
964 y_position=(float) image->page.y/y_resolution;
965 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
966 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
967 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
968 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
969 image->orientation=(OrientationType) orientation;
970 chromaticity=(float *) NULL;
971 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
972 if (chromaticity != (float *) NULL)
974 image->chromaticity.white_point.x=chromaticity[0];
975 image->chromaticity.white_point.y=chromaticity[1];
977 chromaticity=(float *) NULL;
978 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
979 if (chromaticity != (float *) NULL)
981 image->chromaticity.red_primary.x=chromaticity[0];
982 image->chromaticity.red_primary.y=chromaticity[1];
983 image->chromaticity.green_primary.x=chromaticity[2];
984 image->chromaticity.green_primary.y=chromaticity[3];
985 image->chromaticity.blue_primary.x=chromaticity[4];
986 image->chromaticity.blue_primary.y=chromaticity[5];
988 TIFFGetProperties(tiff,image);
989 option=GetImageOption(image_info,"tiff:exif");
990 if ((option != (const char *) NULL) &&
991 (IsMagickTrue(option) != MagickFalse))
992 TIFFGetEXIFProperties(tiff,image);
993 TIFFGetProfiles(tiff,image);
995 Allocate memory for the image and pixel buffer.
997 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
998 if ((compress_tag != COMPRESSION_NONE) &&
999 (TIFFIsCODECConfigured(compress_tag) == 0))
1002 ThrowReaderException(CoderError,"CompressNotSupported");
1005 switch (compress_tag)
1007 case COMPRESSION_NONE: image->compression=NoCompression; break;
1008 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1009 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1010 case COMPRESSION_JPEG:
1012 image->compression=JPEGCompression;
1013 #if defined(JPEG_SUPPORT)
1016 sampling_factor[MaxTextExtent];
1022 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1023 &horizontal,&vertical);
1024 (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d",
1025 horizontal,vertical);
1026 (void) SetImageProperty(image,"jpeg:sampling-factor",
1028 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1029 "Sampling Factors: %s",sampling_factor);
1030 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1032 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1034 photometric=PHOTOMETRIC_RGB;
1040 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1041 #if defined(COMPRESSION_LZMA)
1042 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1044 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1045 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1046 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1047 default: image->compression=RLECompression; break;
1049 quantum_info=AcquireQuantumInfo(image_info,image);
1050 if (quantum_info == (QuantumInfo *) NULL)
1053 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1055 if (sample_format == SAMPLEFORMAT_UINT)
1056 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1057 if (sample_format == SAMPLEFORMAT_INT)
1058 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1059 if (sample_format == SAMPLEFORMAT_IEEEFP)
1060 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1061 if (status == MagickFalse)
1064 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1067 switch (photometric)
1069 case PHOTOMETRIC_MINISBLACK:
1071 quantum_info->min_is_white=MagickFalse;
1074 case PHOTOMETRIC_MINISWHITE:
1076 quantum_info->min_is_white=MagickTrue;
1082 associated_alpha=MagickFalse;
1084 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1086 if (extra_samples == 0)
1088 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1089 image->matte=MagickTrue;
1092 for (i=0; i < extra_samples; i++)
1094 image->matte=MagickTrue;
1095 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
1096 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
1098 option=GetImageOption(image_info,"tiff:alpha");
1099 if (option != (const char *) NULL)
1100 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1102 if (image->matte != MagickFalse)
1103 (void) SetImageProperty(image,"tiff:alpha",
1104 associated_alpha != MagickFalse ? "associated" : "unassociated");
1105 if ((photometric == PHOTOMETRIC_PALETTE) &&
1106 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1111 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
1112 if (AcquireImageColormap(image,colors) == MagickFalse)
1115 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1118 if (units == RESUNIT_INCH)
1119 image->units=PixelsPerInchResolution;
1120 if (units == RESUNIT_CENTIMETER)
1121 image->units=PixelsPerCentimeterResolution;
1122 value=(unsigned short) image->scene;
1123 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1125 if (image_info->ping != MagickFalse)
1127 if (image_info->number_scenes != 0)
1128 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1130 goto next_tiff_frame;
1132 method=ReadGenericMethod;
1133 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1136 value[MaxTextExtent];
1138 method=ReadStripMethod;
1139 (void) FormatLocaleString(value,MaxTextExtent,"%u",
1140 (unsigned int) rows_per_strip);
1141 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1143 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1144 method=ReadRGBAMethod;
1145 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1146 method=ReadCMYKAMethod;
1147 if ((photometric != PHOTOMETRIC_RGB) &&
1148 (photometric != PHOTOMETRIC_CIELAB) &&
1149 (photometric != PHOTOMETRIC_SEPARATED))
1150 method=ReadGenericMethod;
1151 if (image->storage_class == PseudoClass)
1152 method=ReadSingleSampleMethod;
1153 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1154 (photometric == PHOTOMETRIC_MINISWHITE))
1155 method=ReadSingleSampleMethod;
1156 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1157 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
1158 method=ReadGenericMethod;
1159 if (image->compression == JPEGCompression)
1160 method=ReadGenericMethod;
1161 if (TIFFIsTiled(tiff) != MagickFalse)
1162 method=ReadTileMethod;
1163 quantum_type=RGBQuantum;
1164 pixels=GetQuantumPixels(quantum_info);
1167 case ReadSingleSampleMethod:
1170 Convert TIFF image to PseudoClass MIFF image.
1172 if ((image->storage_class == PseudoClass) &&
1173 (photometric == PHOTOMETRIC_PALETTE))
1184 Initialize colormap.
1186 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1187 &green_colormap,&blue_colormap);
1188 range=255; /* might be old style 8-bit colormap */
1189 for (i=0; i < (ssize_t) image->colors; i++)
1190 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1191 (blue_colormap[i] >= 256))
1196 for (i=0; i < (ssize_t) image->colors; i++)
1198 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
1199 red_colormap[i])/range);
1200 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
1201 green_colormap[i])/range);
1202 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
1203 blue_colormap[i])/range);
1206 quantum_type=IndexQuantum;
1207 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1208 if (image->matte != MagickFalse)
1210 if (image->storage_class != PseudoClass)
1212 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1214 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1218 quantum_type=IndexAlphaQuantum;
1219 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1223 if (image->storage_class != PseudoClass)
1225 quantum_type=GrayQuantum;
1226 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1228 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1229 if (status == MagickFalse)
1232 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1234 pixels=GetQuantumPixels(quantum_info);
1235 for (y=0; y < (ssize_t) image->rows; y++)
1243 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1246 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1247 if (q == (Quantum *) NULL)
1249 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1250 quantum_type,pixels,exception);
1252 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1254 if (image->previous == (Image *) NULL)
1256 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1258 if (status == MagickFalse)
1264 case ReadRGBAMethod:
1267 Convert TIFF image to DirectClass MIFF image.
1269 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1270 quantum_type=RGBQuantum;
1271 if (image->matte != MagickFalse)
1273 quantum_type=RGBAQuantum;
1274 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1276 if (image->colorspace == CMYKColorspace)
1278 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1279 quantum_type=CMYKQuantum;
1280 if (image->matte != MagickFalse)
1282 quantum_type=CMYKAQuantum;
1283 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1286 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1287 if (status == MagickFalse)
1290 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1292 pixels=GetQuantumPixels(quantum_info);
1293 for (y=0; y < (ssize_t) image->rows; y++)
1301 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1304 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1305 if (q == (Quantum *) NULL)
1307 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1308 quantum_type,pixels,exception);
1309 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1311 if (image->previous == (Image *) NULL)
1313 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1315 if (status == MagickFalse)
1321 case ReadCMYKAMethod:
1324 Convert TIFF image to DirectClass MIFF image.
1326 for (i=0; i < (ssize_t) samples_per_pixel; i++)
1328 for (y=0; y < (ssize_t) image->rows; y++)
1336 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1340 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1341 if (q == (Quantum *) NULL)
1343 if (image->colorspace != CMYKColorspace)
1346 case 0: quantum_type=RedQuantum; break;
1347 case 1: quantum_type=GreenQuantum; break;
1348 case 2: quantum_type=BlueQuantum; break;
1349 case 3: quantum_type=AlphaQuantum; break;
1350 default: quantum_type=UndefinedQuantum; break;
1355 case 0: quantum_type=CyanQuantum; break;
1356 case 1: quantum_type=MagentaQuantum; break;
1357 case 2: quantum_type=YellowQuantum; break;
1358 case 3: quantum_type=BlackQuantum; break;
1359 case 4: quantum_type=AlphaQuantum; break;
1360 default: quantum_type=UndefinedQuantum; break;
1362 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1363 quantum_type,pixels,exception);
1364 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1367 if (image->previous == (Image *) NULL)
1369 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1371 if (status == MagickFalse)
1377 case ReadStripMethod:
1379 register unsigned char
1383 Convert stripped TIFF image to DirectClass MIFF image.
1386 p=(unsigned char *) NULL;
1387 for (y=0; y < (ssize_t) image->rows; y++)
1395 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1396 if (q == (Quantum *) NULL)
1400 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1402 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1406 p=(unsigned char *) (((uint32 *) pixels)+image->columns*i);
1407 for (x=0; x < (ssize_t) image->columns; x++)
1409 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1411 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1413 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1415 if (image->matte != MagickFalse)
1416 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1419 q+=GetPixelChannels(image);
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 ReadTileMethod:
1447 Convert tiled TIFF image to DirectClass MIFF image.
1449 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1450 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1453 ThrowReaderException(CoderError,"ImageIsNotTiled");
1455 (void) SetImageStorageClass(image,DirectClass,&image->exception);
1456 number_pixels=columns*rows;
1457 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
1458 sizeof(*tile_pixels));
1459 if (tile_pixels == (uint32 *) NULL)
1462 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1464 for (y=0; y < (ssize_t) image->rows; y+=rows)
1477 rows_remaining=image->rows-y;
1478 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
1479 rows_remaining=rows;
1480 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1482 if (tile == (Quantum *) NULL)
1484 for (x=0; x < (ssize_t) image->columns; x+=columns)
1490 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1492 columns_remaining=image->columns-x;
1493 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
1494 columns_remaining=columns;
1495 p=tile_pixels+(rows-rows_remaining)*columns;
1496 q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+
1498 for (row=rows_remaining; row > 0; row--)
1500 if (image->matte != MagickFalse)
1501 for (column=columns_remaining; column > 0; column--)
1503 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1505 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1507 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1509 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1512 q+=GetPixelChannels(image);
1515 for (column=columns_remaining; column > 0; column--)
1517 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1519 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1521 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1524 q+=GetPixelChannels(image);
1526 p+=columns-columns_remaining;
1527 q-=GetPixelChannels(image)*(image->columns+columns_remaining);
1530 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1532 if (image->previous == (Image *) NULL)
1534 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1536 if (status == MagickFalse)
1540 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1543 case ReadGenericMethod:
1553 Convert TIFF image to DirectClass MIFF image.
1555 number_pixels=(MagickSizeType) image->columns*image->rows;
1556 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1557 (number_pixels*sizeof(uint32))))
1560 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1562 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1564 if (pixels == (uint32 *) NULL)
1567 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1569 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1570 (uint32) image->rows,(uint32 *) pixels,0);
1572 Convert image to DirectClass pixel packets.
1574 p=pixels+number_pixels-1;
1575 for (y=0; y < (ssize_t) image->rows; y++)
1583 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1584 if (q == (Quantum *) NULL)
1586 q+=GetPixelChannels(image)*(image->columns-1);
1587 for (x=0; x < (ssize_t) image->columns; x++)
1589 SetPixelRed(image,ScaleCharToQuantum((unsigned char)
1591 SetPixelGreen(image,ScaleCharToQuantum((unsigned char)
1593 SetPixelBlue(image,ScaleCharToQuantum((unsigned char)
1595 if (image->matte != MagickFalse)
1596 SetPixelAlpha(image,ScaleCharToQuantum((unsigned char)
1599 q-=GetPixelChannels(image);;
1601 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1603 if (image->previous == (Image *) NULL)
1605 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1607 if (status == MagickFalse)
1611 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1615 SetQuantumImageType(image,quantum_type);
1617 if ((photometric == PHOTOMETRIC_LOGL) ||
1618 (photometric == PHOTOMETRIC_MINISBLACK) ||
1619 (photometric == PHOTOMETRIC_MINISWHITE))
1621 image->type=GrayscaleType;
1622 if (bits_per_sample == 1)
1623 image->type=BilevelType;
1625 if (image->storage_class == PseudoClass)
1626 image->depth=GetImageDepth(image,exception);
1627 image->endian=MSBEndian;
1628 if (endian == FILLORDER_LSB2MSB)
1629 image->endian=LSBEndian;
1630 if ((photometric == PHOTOMETRIC_LOGL) ||
1631 (photometric == PHOTOMETRIC_MINISBLACK) ||
1632 (photometric == PHOTOMETRIC_MINISWHITE))
1634 image->type=GrayscaleType;
1635 if (bits_per_sample == 1)
1636 image->type=BilevelType;
1639 Proceed to next image.
1641 if (image_info->number_scenes != 0)
1642 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1644 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1645 if (status == MagickTrue)
1648 Allocate next image structure.
1650 AcquireNextImage(image_info,image);
1651 if (GetNextImageInList(image) == (Image *) NULL)
1653 image=DestroyImageList(image);
1654 return((Image *) NULL);
1656 image=SyncNextImageInList(image);
1657 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1659 if (status == MagickFalse)
1662 quantum_info=DestroyQuantumInfo(quantum_info);
1663 } while (status == MagickTrue);
1664 (void) TIFFSetWarningHandler(warning_handler);
1665 (void) TIFFSetErrorHandler(error_handler);
1667 return(GetFirstImageInList(image));
1672 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1676 % R e g i s t e r T I F F I m a g e %
1680 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1682 % RegisterTIFFImage() adds properties for the TIFF image format to
1683 % the list of supported formats. The properties include the image format
1684 % tag, a method to read and/or write the format, whether the format
1685 % supports the saving of more than one frame to the same file or blob,
1686 % whether the format supports native in-memory I/O, and a brief
1687 % description of the format.
1689 % The format of the RegisterTIFFImage method is:
1691 % size_t RegisterTIFFImage(void)
1694 ModuleExport size_t RegisterTIFFImage(void)
1696 #define TIFFDescription "Tagged Image File Format"
1699 version[MaxTextExtent];
1704 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1705 tiff_semaphore=AllocateSemaphoreInfo();
1706 LockSemaphoreInfo(tiff_semaphore);
1707 if (instantiate_key == MagickFalse)
1709 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1710 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1711 instantiate_key=MagickTrue;
1713 UnlockSemaphoreInfo(tiff_semaphore);
1715 #if defined(TIFF_VERSION)
1716 (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION);
1718 #if defined(MAGICKCORE_TIFF_DELEGATE)
1727 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1733 entry=SetMagickInfo("GROUP4");
1734 #if defined(MAGICKCORE_TIFF_DELEGATE)
1735 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1736 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1738 entry->raw=MagickTrue;
1739 entry->endian_support=MagickTrue;
1740 entry->adjoin=MagickFalse;
1741 entry->format_type=ImplicitFormatType;
1742 entry->seekable_stream=MagickTrue;
1743 entry->thread_support=NoThreadSupport;
1744 entry->description=ConstantString("Raw CCITT Group4");
1745 entry->module=ConstantString("TIFF");
1746 (void) RegisterMagickInfo(entry);
1747 entry=SetMagickInfo("PTIF");
1748 #if defined(MAGICKCORE_TIFF_DELEGATE)
1749 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1750 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1752 entry->endian_support=MagickTrue;
1753 entry->seekable_stream=MagickTrue;
1754 entry->thread_support=NoThreadSupport;
1755 entry->description=ConstantString("Pyramid encoded TIFF");
1756 entry->module=ConstantString("TIFF");
1757 (void) RegisterMagickInfo(entry);
1758 entry=SetMagickInfo("TIF");
1759 #if defined(MAGICKCORE_TIFF_DELEGATE)
1760 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1761 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1763 entry->endian_support=MagickTrue;
1764 entry->seekable_stream=MagickTrue;
1765 entry->stealth=MagickTrue;
1766 entry->thread_support=NoThreadSupport;
1767 entry->description=ConstantString(TIFFDescription);
1768 if (*version != '\0')
1769 entry->version=ConstantString(version);
1770 entry->module=ConstantString("TIFF");
1771 (void) RegisterMagickInfo(entry);
1772 entry=SetMagickInfo("TIFF");
1773 #if defined(MAGICKCORE_TIFF_DELEGATE)
1774 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1775 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1777 entry->magick=(IsImageFormatHandler *) IsTIFF;
1778 entry->endian_support=MagickTrue;
1779 entry->seekable_stream=MagickTrue;
1780 entry->thread_support=NoThreadSupport;
1781 entry->description=ConstantString(TIFFDescription);
1782 if (*version != '\0')
1783 entry->version=ConstantString(version);
1784 entry->module=ConstantString("TIFF");
1785 (void) RegisterMagickInfo(entry);
1786 entry=SetMagickInfo("TIFF64");
1787 #if defined(TIFF_VERSION_BIG)
1788 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1789 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1791 entry->adjoin=MagickFalse;
1792 entry->endian_support=MagickTrue;
1793 entry->seekable_stream=MagickTrue;
1794 entry->thread_support=NoThreadSupport;
1795 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1796 if (*version != '\0')
1797 entry->version=ConstantString(version);
1798 entry->module=ConstantString("TIFF");
1799 (void) RegisterMagickInfo(entry);
1800 return(MagickImageCoderSignature);
1804 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1808 % U n r e g i s t e r T I F F I m a g e %
1812 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1814 % UnregisterTIFFImage() removes format registrations made by the TIFF module
1815 % from the list of supported formats.
1817 % The format of the UnregisterTIFFImage method is:
1819 % UnregisterTIFFImage(void)
1822 ModuleExport void UnregisterTIFFImage(void)
1824 (void) UnregisterMagickInfo("RAWGROUP4");
1825 (void) UnregisterMagickInfo("PTIF");
1826 (void) UnregisterMagickInfo("TIF");
1827 (void) UnregisterMagickInfo("TIFF");
1828 (void) UnregisterMagickInfo("TIFF64");
1829 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1830 tiff_semaphore=AllocateSemaphoreInfo();
1831 LockSemaphoreInfo(tiff_semaphore);
1832 if (instantiate_key != MagickFalse)
1833 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1834 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1835 instantiate_key=MagickFalse;
1836 UnlockSemaphoreInfo(tiff_semaphore);
1837 DestroySemaphoreInfo(&tiff_semaphore);
1840 #if defined(MAGICKCORE_TIFF_DELEGATE)
1842 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1846 % W r i t e G R O U P 4 I m a g e %
1850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1852 % WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1854 % The format of the WriteGROUP4Image method is:
1856 % MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1857 % Image *image,ExceptionInfo *)
1859 % A description of each parameter follows:
1861 % o image_info: the image info.
1863 % o image: The image.
1865 % o exception: return any errors or warnings in this structure.
1868 static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1869 Image *image,ExceptionInfo *exception)
1872 filename[MaxTextExtent];
1906 Write image as CCITT Group4 TIFF image to a temporary file.
1908 assert(image_info != (const ImageInfo *) NULL);
1909 assert(image_info->signature == MagickSignature);
1910 assert(image != (Image *) NULL);
1911 assert(image->signature == MagickSignature);
1912 if (image->debug != MagickFalse)
1913 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1914 assert(exception != (ExceptionInfo *) NULL);
1915 assert(exception->signature == MagickSignature);
1916 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1917 if (status == MagickFalse)
1919 huffman_image=CloneImage(image,0,0,MagickTrue,exception);
1920 if (huffman_image == (Image *) NULL)
1922 (void) CloseBlob(image);
1923 return(MagickFalse);
1925 huffman_image->endian=MSBEndian;
1927 unique_file=AcquireUniqueFileResource(filename);
1928 if (unique_file != -1)
1929 file=fdopen(unique_file,"wb");
1930 if ((unique_file == -1) || (file == (FILE *) NULL))
1932 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
1934 return(MagickFalse);
1936 (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1938 (void) SetImageType(huffman_image,BilevelType);
1939 write_info=CloneImageInfo((ImageInfo *) NULL);
1940 SetImageInfoFile(write_info,file);
1941 write_info->compression=Group4Compression;
1942 write_info->type=BilevelType;
1943 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1944 status=WriteTIFFImage(write_info,huffman_image,exception);
1945 (void) fflush(file);
1946 write_info=DestroyImageInfo(write_info);
1947 if (status == MagickFalse)
1949 huffman_image=DestroyImage(huffman_image);
1950 (void) fclose(file);
1951 (void) RelinquishUniqueFileResource(filename);
1952 return(MagickFalse);
1954 tiff=TIFFOpen(filename,"rb");
1955 if (tiff == (TIFF *) NULL)
1957 huffman_image=DestroyImage(huffman_image);
1958 (void) fclose(file);
1959 (void) RelinquishUniqueFileResource(filename);
1960 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
1961 image_info->filename);
1962 return(MagickFalse);
1965 Allocate raw strip buffer.
1967 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1970 huffman_image=DestroyImage(huffman_image);
1971 (void) fclose(file);
1972 (void) RelinquishUniqueFileResource(filename);
1973 return(MagickFalse);
1975 strip_size=byte_count[0];
1976 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
1977 if (byte_count[i] > strip_size)
1978 strip_size=byte_count[i];
1979 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1981 if (buffer == (unsigned char *) NULL)
1984 huffman_image=DestroyImage(huffman_image);
1985 (void) fclose(file);
1986 (void) RelinquishUniqueFileResource(filename);
1987 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1988 image_info->filename);
1991 Compress runlength encoded to 2D Huffman pixels.
1993 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
1995 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
1996 if (WriteBlob(image,(size_t) count,buffer) != count)
1999 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
2001 huffman_image=DestroyImage(huffman_image);
2002 (void) fclose(file);
2003 (void) RelinquishUniqueFileResource(filename);
2004 (void) CloseBlob(image);
2009 #if defined(MAGICKCORE_TIFF_DELEGATE)
2011 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2015 % W r i t e P T I F I m a g e %
2019 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2021 % WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2024 % The format of the WritePTIFImage method is:
2026 % MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2027 % Image *image,ExceptionInfo *exception)
2029 % A description of each parameter follows:
2031 % o image_info: the image info.
2033 % o image: The image.
2035 % o exception: return any errors or warnings in this structure.
2038 static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2039 Image *image,ExceptionInfo *exception)
2057 Create pyramid-encoded TIFF image.
2059 images=NewImageList();
2060 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2062 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,exception));
2063 columns=next->columns;
2065 while ((columns > 64) && (rows > 64))
2069 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2071 AppendImageToList(&images,pyramid_image);
2075 Write pyramid-encoded TIFF image.
2077 write_info=CloneImageInfo(image_info);
2078 *write_info->magick='\0';
2079 write_info->adjoin=MagickTrue;
2080 status=WriteTIFFImage(write_info,GetFirstImageInList(images),exception);
2081 images=DestroyImageList(images);
2082 write_info=DestroyImageInfo(write_info);
2087 #if defined(MAGICKCORE_TIFF_DELEGATE)
2089 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2092 % W r i t e T I F F I m a g e %
2096 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2098 % WriteTIFFImage() writes an image in the Tagged image file format.
2100 % The format of the WriteTIFFImage method is:
2102 % MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2103 % Image *image,ExceptionInfo *exception)
2105 % A description of each parameter follows:
2107 % o image_info: the image info.
2109 % o image: The image.
2111 % o exception: return any errors or warnings in this structure.
2115 typedef struct _TIFFInfo
2126 static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2128 assert(tiff_info != (TIFFInfo *) NULL);
2129 if (tiff_info->scanlines != (unsigned char *) NULL)
2130 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2131 tiff_info->scanlines);
2132 if (tiff_info->pixels != (unsigned char *) NULL)
2133 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2137 static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2138 TIFFInfo *tiff_info)
2150 assert(tiff_info != (TIFFInfo *) NULL);
2151 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2152 option=GetImageOption(image_info,"tiff:tile-geometry");
2153 if (option == (const char *) NULL)
2155 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2156 if ((flags & HeightValue) == 0)
2157 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2158 tile_columns=(uint32) tiff_info->tile_geometry.width;
2159 tile_rows=(uint32) tiff_info->tile_geometry.height;
2160 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2161 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2162 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2163 tiff_info->tile_geometry.width=tile_columns;
2164 tiff_info->tile_geometry.height=tile_rows;
2165 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2166 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
2167 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2168 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2169 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2170 (tiff_info->pixels == (unsigned char *) NULL))
2172 DestroyTIFFInfo(tiff_info);
2173 return(MagickFalse);
2178 static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
2179 tsample_t sample,Image *image)
2187 register unsigned char
2201 if (TIFFIsTiled(tiff) == 0)
2202 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2204 Fill scanlines to tile height.
2206 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2207 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2208 (size_t) TIFFScanlineSize(tiff));
2209 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2210 (tiff_info->tile_geometry.height-1)) &&
2211 (row != (ssize_t) (image->rows-1)))
2214 Write tile to TIFF image.
2217 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
2218 tiff_info->tile_geometry.width);
2219 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2220 tiff_info->tile_geometry.width;
2221 for (i=0; i < (ssize_t) number_tiles; i++)
2223 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
2224 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2225 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2226 for (k=0; k < (ssize_t) tile_width; k++)
2228 if (bytes_per_pixel == 0)
2230 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2231 tiff_info->tile_geometry.width+k)/8);
2232 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2236 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2237 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2238 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2239 for (l=0; l < bytes_per_pixel; l++)
2242 if ((i*tiff_info->tile_geometry.width) != image->columns)
2243 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2244 tiff_info->tile_geometry.width),(uint32) ((row/
2245 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2253 static void TIFFSetProfiles(TIFF *tiff,Image *image)
2261 if (image->profiles == (void *) NULL)
2263 ResetImageProfileIterator(image);
2264 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2266 profile=GetImageProfile(image,name);
2267 #if defined(TIFFTAG_XMLPACKET)
2268 if (LocaleCompare(name,"xmp") == 0)
2269 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2270 profile),GetStringInfoDatum(profile));
2272 #if defined(TIFFTAG_ICCPROFILE)
2273 if (LocaleCompare(name,"icc") == 0)
2274 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2275 profile),GetStringInfoDatum(profile));
2277 if (LocaleCompare(name,"iptc") == 0)
2285 iptc_profile=CloneStringInfo(profile);
2286 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2288 SetStringInfoLength(iptc_profile,length);
2289 if (TIFFIsByteSwapped(tiff))
2290 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2291 (unsigned long) (length/4));
2292 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2293 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2294 iptc_profile=DestroyStringInfo(iptc_profile);
2296 #if defined(TIFFTAG_PHOTOSHOP)
2297 if (LocaleCompare(name,"8bim") == 0)
2298 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2299 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2301 if (LocaleCompare(name,"tiff:37724") == 0)
2302 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2303 GetStringInfoDatum(profile));
2304 name=GetNextImageProfile(image);
2308 static void TIFFSetProperties(TIFF *tiff,Image *image)
2313 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
2314 value=GetImageProperty(image,"tiff:hostcomputer");
2315 if (value != (const char *) NULL)
2316 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2317 value=GetImageProperty(image,"tiff:artist");
2318 if (value != (const char *) NULL)
2319 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2320 value=GetImageProperty(image,"tiff:timestamp");
2321 if (value != (const char *) NULL)
2322 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2323 value=GetImageProperty(image,"tiff:make");
2324 if (value != (const char *) NULL)
2325 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2326 value=GetImageProperty(image,"tiff:model");
2327 if (value != (const char *) NULL)
2328 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2329 value=GetImageProperty(image,"tiff:software");
2330 if (value != (const char *) NULL)
2331 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
2332 value=GetImageProperty(image,"tiff:copyright");
2333 if (value != (const char *) NULL)
2334 (void) TIFFSetField(tiff,33432,value);
2335 value=GetImageProperty(image,"kodak-33423");
2336 if (value != (const char *) NULL)
2337 (void) TIFFSetField(tiff,33423,value);
2338 value=GetImageProperty(image,"kodak-36867");
2339 if (value != (const char *) NULL)
2340 (void) TIFFSetField(tiff,36867,value);
2341 value=GetImageProperty(image,"label");
2342 if (value != (const char *) NULL)
2343 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2344 value=GetImageProperty(image,"comment");
2345 if (value != (const char *) NULL)
2346 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2349 static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2351 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2362 Write EXIF properties.
2365 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2366 for (i=0; exif_info[i].tag != 0; i++)
2368 value=GetImageProperty(image,exif_info[i].property);
2369 if (value == (const char *) NULL)
2371 switch (exif_info[i].type)
2375 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2383 shorty=(uint16) StringToLong(value);
2384 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2392 ssize_ty=(uint16) StringToLong(value);
2393 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
2397 case TIFF_SRATIONAL:
2402 rational=InterpretLocaleValue(value,(char **) NULL);
2403 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2410 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2417 static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2418 Image *image,ExceptionInfo *exception)
2420 #if !defined(TIFFDefaultStripSize)
2421 #define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2482 assert(image_info != (const ImageInfo *) NULL);
2483 assert(image_info->signature == MagickSignature);
2484 assert(image != (Image *) NULL);
2485 assert(image->signature == MagickSignature);
2486 if (image->debug != MagickFalse)
2487 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2488 assert(exception != (ExceptionInfo *) NULL);
2489 assert(exception->signature == MagickSignature);
2490 assert(exception != (ExceptionInfo *) NULL);
2491 assert(exception->signature == MagickSignature);
2492 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
2493 if (status == MagickFalse)
2495 (void) MagickSetThreadValue(tiff_exception,exception);
2496 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2497 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
2498 endian_type=UndefinedEndian;
2499 option=GetImageOption(image_info,"tiff:endian");
2500 if (option != (const char *) NULL)
2502 if (LocaleNCompare(option,"msb",3) == 0)
2503 endian_type=MSBEndian;
2504 if (LocaleNCompare(option,"lsb",3) == 0)
2505 endian_type=LSBEndian;;
2507 switch (endian_type)
2509 case LSBEndian: mode="wl"; break;
2510 case MSBEndian: mode="wb"; break;
2511 default: mode="w"; break;
2513 #if defined(TIFF_VERSION_BIG)
2514 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
2515 switch (endian_type)
2517 case LSBEndian: mode="wl8"; break;
2518 case MSBEndian: mode="wb8"; break;
2519 default: mode="w8"; break;
2522 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2523 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2525 if (tiff == (TIFF *) NULL)
2527 (void) TIFFSetWarningHandler(warning_handler);
2528 (void) TIFFSetErrorHandler(error_handler);
2529 return(MagickFalse);
2532 debug=IsEventLogging();
2537 Initialize TIFF fields.
2539 if ((image_info->type != UndefinedType) &&
2540 (image_info->type != OptimizeType))
2541 (void) SetImageType(image,image_info->type);
2542 quantum_info=AcquireQuantumInfo(image_info,image);
2543 if (quantum_info == (QuantumInfo *) NULL)
2544 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2545 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2546 (quantum_info->format == UndefinedQuantumFormat) &&
2547 (IsHighDynamicRangeImage(image,exception) != MagickFalse))
2549 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2550 if (status == MagickFalse)
2551 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2553 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2554 (GetPreviousImageInList(image) != (Image *) NULL))
2555 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2556 if ((image->columns != (uint32) image->columns) ||
2557 (image->rows != (uint32) image->rows))
2558 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2559 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2560 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2561 compression=image->compression;
2562 if (image_info->compression != UndefinedCompression)
2563 compression=image_info->compression;
2564 switch (compression)
2566 case FaxCompression:
2568 compress_tag=COMPRESSION_CCITTFAX3;
2569 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2572 case Group4Compression:
2574 compress_tag=COMPRESSION_CCITTFAX4;
2575 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2578 #if defined(COMPRESSION_JBIG)
2579 case JBIG1Compression:
2581 compress_tag=COMPRESSION_JBIG;
2585 case JPEGCompression:
2587 compress_tag=COMPRESSION_JPEG;
2590 #if defined(COMPRESSION_LZMA)
2591 case LZMACompression:
2593 compress_tag=COMPRESSION_LZMA;
2597 case LZWCompression:
2599 compress_tag=COMPRESSION_LZW;
2602 case RLECompression:
2604 compress_tag=COMPRESSION_PACKBITS;
2607 case ZipCompression:
2609 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2615 compress_tag=COMPRESSION_NONE;
2619 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2620 if ((compress_tag != COMPRESSION_NONE) &&
2621 (TIFFIsCODECConfigured(compress_tag) == 0))
2623 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2624 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
2625 MagickCompressOptions,(ssize_t) compression));
2626 compress_tag=COMPRESSION_NONE;
2627 compression=NoCompression;
2630 switch (compress_tag)
2632 #if defined(CCITT_SUPPORT)
2633 case COMPRESSION_CCITTFAX3:
2634 case COMPRESSION_CCITTFAX4:
2636 #if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2637 case COMPRESSION_JPEG:
2639 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2640 case COMPRESSION_LZMA:
2642 #if defined(LZW_SUPPORT)
2643 case COMPRESSION_LZW:
2645 #if defined(PACKBITS_SUPPORT)
2646 case COMPRESSION_PACKBITS:
2648 #if defined(ZIP_SUPPORT)
2649 case COMPRESSION_ADOBE_DEFLATE:
2651 case COMPRESSION_NONE:
2655 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
2656 "CompressionNotSupported","`%s'",CommandOptionToMnemonic(
2657 MagickCompressOptions,(ssize_t) compression));
2658 compress_tag=COMPRESSION_NONE;
2659 compression=NoCompression;
2664 switch (compression)
2666 case FaxCompression:
2667 case Group4Compression:
2669 (void) SetImageType(image,BilevelType);
2672 case JPEGCompression:
2674 (void) SetImageStorageClass(image,DirectClass,exception);
2675 (void) SetImageDepth(image,8);
2681 if (image->colorspace == CMYKColorspace)
2683 photometric=PHOTOMETRIC_SEPARATED;
2684 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2685 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2690 Full color TIFF raster.
2692 if (image->colorspace == LabColorspace)
2693 photometric=PHOTOMETRIC_CIELAB;
2695 if (image->colorspace == YCbCrColorspace)
2697 photometric=PHOTOMETRIC_YCBCR;
2698 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
2699 (void) SetImageStorageClass(image,DirectClass,exception);
2700 (void) SetImageDepth(image,8);
2704 if (IsRGBColorspace(image->colorspace) == MagickFalse)
2705 (void) TransformImageColorspace(image,RGBColorspace);
2706 photometric=PHOTOMETRIC_RGB;
2708 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2709 if ((image_info->type != TrueColorType) &&
2710 (image_info->type != TrueColorMatteType))
2712 if ((image_info->type != PaletteType) &&
2713 (IsImageGray(image,exception) != MagickFalse))
2715 photometric=(uint16) (quantum_info->min_is_white !=
2716 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2717 PHOTOMETRIC_MINISBLACK);
2718 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
2719 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
2720 (IsImageMonochrome(image,exception) != MagickFalse))
2722 status=SetQuantumDepth(image,quantum_info,1);
2723 if (status == MagickFalse)
2724 ThrowWriterException(ResourceLimitError,
2725 "MemoryAllocationFailed");
2729 if (image->storage_class == PseudoClass)
2735 Colormapped TIFF raster.
2737 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
2738 photometric=PHOTOMETRIC_PALETTE;
2740 while ((GetQuantumRange(depth)+1) < image->colors)
2742 status=SetQuantumDepth(image,quantum_info,depth);
2743 if (status == MagickFalse)
2744 ThrowWriterException(ResourceLimitError,
2745 "MemoryAllocationFailed");
2749 switch (image->endian)
2753 endian=FILLORDER_LSB2MSB;
2758 endian=FILLORDER_MSB2LSB;
2761 case UndefinedEndian:
2764 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2769 image->endian=MSBEndian;
2770 if ((int) (*(char *) &lsb_first) != 0)
2771 image->endian=LSBEndian;
2772 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2773 (photometric != PHOTOMETRIC_MINISWHITE))
2775 compress_tag=COMPRESSION_NONE;
2776 endian=FILLORDER_MSB2LSB;
2779 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2780 (photometric != PHOTOMETRIC_MINISWHITE))
2782 compress_tag=COMPRESSION_NONE;
2783 endian=FILLORDER_MSB2LSB;
2785 option=GetImageProperty(image,"tiff:fill-order");
2786 if (option != (const char *) NULL)
2788 if (LocaleNCompare(option,"msb",3) == 0)
2789 endian=FILLORDER_MSB2LSB;
2790 if (LocaleNCompare(option,"lsb",3) == 0)
2791 endian=FILLORDER_LSB2MSB;
2793 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2794 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
2795 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2796 if (image->matte != MagickFalse)
2804 TIFF has a matte channel.
2807 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2808 option=GetImageProperty(image,"tiff:alpha");
2809 if ((option != (const char *) NULL) &&
2810 (LocaleCompare(option,"associated") == 0))
2811 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2812 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2813 &samples_per_pixel);
2814 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2815 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2817 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2818 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2820 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2821 switch (quantum_info->format)
2823 case FloatingPointQuantumFormat:
2825 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2826 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2827 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2830 case SignedQuantumFormat:
2832 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2835 case UnsignedQuantumFormat:
2837 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2843 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2844 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2845 if (photometric == PHOTOMETRIC_RGB)
2846 if ((image_info->interlace == PlaneInterlace) ||
2847 (image_info->interlace == PartitionInterlace))
2848 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2850 if (TIFFScanlineSize(tiff) != 0)
2851 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2853 option=GetImageOption(image_info,"tiff:rows-per-strip");
2854 if (option != (const char *) NULL)
2855 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
2856 switch (compress_tag)
2858 case COMPRESSION_JPEG:
2860 #if defined(JPEG_SUPPORT)
2870 rows_per_strip+=(16-(rows_per_strip % 16));
2871 if (image->quality != 0)
2872 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2873 if (image_info->quality != UndefinedCompressionQuality)
2874 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2875 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2876 if (IsRGBColorspace(image->colorspace) == MagickTrue)
2881 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2882 sampling_factor=(const char *) NULL;
2883 value=GetImageProperty(image,"jpeg:sampling-factor");
2884 if (value != (char *) NULL)
2886 sampling_factor=value;
2887 if (image->debug != MagickFalse)
2888 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2889 " Input sampling-factors=%s",sampling_factor);
2891 if (image_info->sampling_factor != (char *) NULL)
2892 sampling_factor=image_info->sampling_factor;
2893 if (sampling_factor != (const char *) NULL)
2895 flags=ParseGeometry(sampling_factor,&geometry_info);
2896 if ((flags & SigmaValue) == 0)
2897 geometry_info.sigma=geometry_info.rho;
2898 if (image->colorspace == YCbCrColorspace)
2899 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2900 geometry_info.rho,(uint16) geometry_info.sigma);
2903 if (bits_per_sample == 12)
2904 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2908 case COMPRESSION_ADOBE_DEFLATE:
2910 rows_per_strip=(uint32) image->rows;
2911 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2913 if (((photometric == PHOTOMETRIC_RGB) ||
2914 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2915 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2916 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2917 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2918 image_info->quality == UndefinedCompressionQuality ? 7 :
2919 MagickMin((ssize_t) image_info->quality/10,9)));
2922 case COMPRESSION_CCITTFAX3:
2927 rows_per_strip=(uint32) image->rows;
2928 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2931 case COMPRESSION_CCITTFAX4:
2933 rows_per_strip=(uint32) image->rows;
2936 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2937 case COMPRESSION_LZMA:
2939 if (((photometric == PHOTOMETRIC_RGB) ||
2940 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2941 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2942 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2943 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
2944 image_info->quality == UndefinedCompressionQuality ? 7 :
2945 MagickMin((ssize_t) image_info->quality/10,9)));
2949 case COMPRESSION_LZW:
2951 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2953 if (((photometric == PHOTOMETRIC_RGB) ||
2954 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2955 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2956 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2962 option=GetImageOption(image_info,"tiff:tile-geometry");
2963 if (option == (const char *) NULL)
2964 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
2965 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2971 Set image resolution.
2974 if (image->units == PixelsPerInchResolution)
2976 if (image->units == PixelsPerCentimeterResolution)
2977 units=RESUNIT_CENTIMETER;
2978 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2979 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2980 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2981 if ((image->page.x != 0) || (image->page.y != 0))
2986 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2987 image->x_resolution);
2988 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2989 image->y_resolution);
2992 if (image->chromaticity.white_point.x != 0.0)
2998 Set image chromaticity.
3000 chromaticity[0]=(float) image->chromaticity.red_primary.x;
3001 chromaticity[1]=(float) image->chromaticity.red_primary.y;
3002 chromaticity[2]=(float) image->chromaticity.green_primary.x;
3003 chromaticity[3]=(float) image->chromaticity.green_primary.y;
3004 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
3005 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
3006 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
3007 chromaticity[0]=(float) image->chromaticity.white_point.x;
3008 chromaticity[1]=(float) image->chromaticity.white_point.y;
3009 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
3011 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
3013 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3014 if (image->scene != 0)
3015 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
3016 GetImageListLength(image));
3018 if (image->orientation != UndefinedOrientation)
3019 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
3020 (void) TIFFSetProfiles(tiff,image);
3026 page=(uint16) scene;
3027 pages=(uint16) GetImageListLength(image);
3028 if ((image_info->adjoin != MagickFalse) && (pages > 1))
3029 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3030 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3032 (void) TIFFSetProperties(tiff,image);
3034 (void) TIFFSetEXIFProperties(tiff,image);
3036 Write image scanlines.
3038 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3039 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3040 pixels=GetQuantumPixels(quantum_info);
3041 tiff_info.scanline=GetQuantumPixels(quantum_info);
3042 switch (photometric)
3044 case PHOTOMETRIC_CIELAB:
3045 case PHOTOMETRIC_YCBCR:
3046 case PHOTOMETRIC_RGB:
3051 switch (image_info->interlace)
3056 quantum_type=RGBQuantum;
3057 if (image->matte != MagickFalse)
3058 quantum_type=RGBAQuantum;
3059 for (y=0; y < (ssize_t) image->rows; y++)
3061 register const Quantum
3064 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3065 if (p == (const Quantum *) NULL)
3067 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3068 quantum_type,pixels,exception);
3070 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3072 if (image->previous == (Image *) NULL)
3074 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3076 if (status == MagickFalse)
3082 case PlaneInterlace:
3083 case PartitionInterlace:
3086 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3088 for (y=0; y < (ssize_t) image->rows; y++)
3090 register const Quantum
3093 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3094 if (p == (const Quantum *) NULL)
3096 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3097 RedQuantum,pixels,exception);
3098 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3101 if (image->previous == (Image *) NULL)
3103 status=SetImageProgress(image,SaveImageTag,100,400);
3104 if (status == MagickFalse)
3107 for (y=0; y < (ssize_t) image->rows; y++)
3109 register const Quantum
3112 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3113 if (p == (const Quantum *) NULL)
3115 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3116 GreenQuantum,pixels,exception);
3117 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3120 if (image->previous == (Image *) NULL)
3122 status=SetImageProgress(image,SaveImageTag,200,400);
3123 if (status == MagickFalse)
3126 for (y=0; y < (ssize_t) image->rows; y++)
3128 register const Quantum
3131 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3132 if (p == (const Quantum *) NULL)
3134 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3135 BlueQuantum,pixels,exception);
3136 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3139 if (image->previous == (Image *) NULL)
3141 status=SetImageProgress(image,SaveImageTag,300,400);
3142 if (status == MagickFalse)
3145 if (image->matte != MagickFalse)
3146 for (y=0; y < (ssize_t) image->rows; y++)
3148 register const Quantum
3151 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3152 if (p == (const Quantum *) NULL)
3154 length=ExportQuantumPixels(image,(CacheView *) NULL,
3155 quantum_info,AlphaQuantum,pixels,exception);
3156 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3159 if (image->previous == (Image *) NULL)
3161 status=SetImageProgress(image,SaveImageTag,400,400);
3162 if (status == MagickFalse)
3170 case PHOTOMETRIC_SEPARATED:
3175 quantum_type=CMYKQuantum;
3176 if (image->matte != MagickFalse)
3177 quantum_type=CMYKAQuantum;
3178 if (image->colorspace != CMYKColorspace)
3179 (void) TransformImageColorspace(image,CMYKColorspace);
3180 for (y=0; y < (ssize_t) image->rows; y++)
3182 register const Quantum
3185 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3186 if (p == (const Quantum *) NULL)
3188 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3189 quantum_type,pixels,exception);
3190 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3192 if (image->previous == (Image *) NULL)
3194 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3196 if (status == MagickFalse)
3202 case PHOTOMETRIC_PALETTE:
3210 Colormapped TIFF image.
3212 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3213 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3214 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3215 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3216 (blue == (uint16 *) NULL))
3217 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3219 Initialize TIFF colormap.
3221 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3222 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3223 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3224 for (i=0; i < (ssize_t) image->colors; i++)
3226 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3227 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3228 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3230 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3231 red=(uint16 *) RelinquishMagickMemory(red);
3232 green=(uint16 *) RelinquishMagickMemory(green);
3233 blue=(uint16 *) RelinquishMagickMemory(blue);
3238 Convert PseudoClass packets to contiguous grayscale scanlines.
3240 quantum_type=IndexQuantum;
3241 if (image->matte != MagickFalse)
3243 if (photometric != PHOTOMETRIC_PALETTE)
3244 quantum_type=GrayAlphaQuantum;
3246 quantum_type=IndexAlphaQuantum;
3249 if (photometric != PHOTOMETRIC_PALETTE)
3250 quantum_type=GrayQuantum;
3251 for (y=0; y < (ssize_t) image->rows; y++)
3253 register const Quantum
3256 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
3257 if (p == (const Quantum *) NULL)
3259 length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
3260 quantum_type,pixels,exception);
3261 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3263 if (image->previous == (Image *) NULL)
3265 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3267 if (status == MagickFalse)
3274 quantum_info=DestroyQuantumInfo(quantum_info);
3275 DestroyTIFFInfo(&tiff_info);
3276 if (0 && (image_info->verbose == MagickTrue))
3277 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3278 (void) TIFFWriteDirectory(tiff);
3279 image->endian=MSBEndian;
3280 if (endian == FILLORDER_LSB2MSB)
3281 image->endian=LSBEndian;
3282 image=SyncNextImageInList(image);
3283 if (image == (Image *) NULL)
3285 status=SetImageProgress(image,SaveImagesTag,scene++,
3286 GetImageListLength(image));
3287 if (status == MagickFalse)
3289 } while (image_info->adjoin != MagickFalse);
3290 (void) TIFFSetWarningHandler(warning_handler);
3291 (void) TIFFSetErrorHandler(error_handler);