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 "magick/studio.h"
43 #include "magick/attribute.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/cache.h"
47 #include "magick/color.h"
48 #include "magick/color-private.h"
49 #include "magick/colormap.h"
50 #include "magick/colorspace.h"
51 #include "magick/constitute.h"
52 #include "magick/enhance.h"
53 #include "magick/exception.h"
54 #include "magick/exception-private.h"
55 #include "magick/geometry.h"
56 #include "magick/image.h"
57 #include "magick/image-private.h"
58 #include "magick/list.h"
59 #include "magick/log.h"
60 #include "magick/magick.h"
61 #include "magick/memory_.h"
62 #include "magick/module.h"
63 #include "magick/monitor.h"
64 #include "magick/monitor-private.h"
65 #include "magick/option.h"
66 #include "magick/property.h"
67 #include "magick/quantum.h"
68 #include "magick/quantum-private.h"
69 #include "magick/profile.h"
70 #include "magick/resize.h"
71 #include "magick/resource_.h"
72 #include "magick/semaphore.h"
73 #include "magick/splay-tree.h"
74 #include "magick/static.h"
75 #include "magick/statistic.h"
76 #include "magick/string_.h"
77 #include "magick/string-private.h"
78 #include "magick/thread_.h"
79 #include "magick/utility.h"
80 #if defined(MAGICKCORE_TIFF_DELEGATE)
81 # if defined(MAGICKCORE_HAVE_TIFFCONF_H)
82 # include "tiffconf.h"
86 # if !defined(COMPRESSION_ADOBE_DEFLATE)
87 # define COMPRESSION_ADOBE_DEFLATE 8
89 # if !defined(PREDICTOR_HORIZONTAL)
90 # define PREDICTOR_HORIZONTAL 2
96 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
97 typedef struct _ExifInfo
107 static const ExifInfo
109 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
110 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
111 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
112 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
113 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
114 { EXIFTAG_OECF, TIFF_UNDEFINED, "exif:OptoelectricConversionFactor" },
115 { EXIFTAG_EXIFVERSION, TIFF_UNDEFINED, "exif:ExifVersion" },
116 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
117 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
118 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_UNDEFINED, "exif:ComponentsConfiguration" },
119 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
120 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
121 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
122 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
123 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
124 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
125 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
126 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
127 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
128 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
129 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
130 /* { EXIFTAG_SUBJECTAREA, TIFF_SHORT, "exif:SubjectArea" }, */
131 { EXIFTAG_MAKERNOTE, TIFF_UNDEFINED, "exif:MakerNote" },
132 { EXIFTAG_USERCOMMENT, TIFF_UNDEFINED, "exif:UserComment" },
133 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
134 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
135 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
136 { EXIFTAG_FLASHPIXVERSION, TIFF_UNDEFINED, "exif:FlashpixVersion" },
137 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
138 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
139 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
140 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
141 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
142 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
143 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_UNDEFINED, "exif:SpatialFrequencyResponse" },
144 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
145 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
146 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
147 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
148 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
149 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
150 { EXIFTAG_FILESOURCE, TIFF_UNDEFINED, "exif:FileSource" },
151 { EXIFTAG_SCENETYPE, TIFF_UNDEFINED, "exif:SceneType" },
152 { EXIFTAG_CFAPATTERN, TIFF_UNDEFINED, "exif:CFAPattern" },
153 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
154 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
155 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
156 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
157 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
158 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
159 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
160 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
161 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
162 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
163 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_UNDEFINED, "exif:DeviceSettingDescription" },
164 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
165 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
166 { 0, 0, (char *) NULL }
169 #endif /* MAGICKCORE_TIFF_DELEGATE */
174 static MagickThreadKey
178 *tiff_semaphore = (SemaphoreInfo *) NULL;
180 static volatile MagickBooleanType
181 instantiate_key = MagickFalse;
184 Forward declarations.
186 #if defined(MAGICKCORE_TIFF_DELEGATE)
188 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
190 static MagickBooleanType
191 WriteGROUP4Image(const ImageInfo *,Image *),
192 WritePTIFImage(const ImageInfo *,Image *),
193 WriteTIFFImage(const ImageInfo *,Image *);
197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207 % IsTIFF() returns MagickTrue if the image format type, identified by the
208 % magick string, is TIFF.
210 % The format of the IsTIFF method is:
212 % MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
214 % A description of each parameter follows:
216 % o magick: compare image format pattern against these bytes.
218 % o length: Specifies the length of the magick string.
221 static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
225 if (memcmp(magick,"\115\115\000\052",4) == 0)
227 if (memcmp(magick,"\111\111\052\000",4) == 0)
229 #if defined(TIFF_VERSION_BIG)
232 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
234 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
240 #if defined(MAGICKCORE_TIFF_DELEGATE)
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 % R e a d G R O U P 4 I m a g e %
250 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 % ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
253 % allocates the memory necessary for the new Image structure and returns a
254 % pointer to the new image.
256 % The format of the ReadGROUP4Image method is:
258 % Image *ReadGROUP4Image(const ImageInfo *image_info,
259 % ExceptionInfo *exception)
261 % A description of each parameter follows:
263 % o image_info: the image info.
265 % o exception: return any errors or warnings in this structure.
269 static inline size_t WriteLSBLong(FILE *file,const size_t value)
274 buffer[0]=(unsigned char) value;
275 buffer[1]=(unsigned char) (value >> 8);
276 buffer[2]=(unsigned char) (value >> 16);
277 buffer[3]=(unsigned char) (value >> 24);
278 return(fwrite(buffer,1,4,file));
281 static Image *ReadGROUP4Image(const ImageInfo *image_info,
282 ExceptionInfo *exception)
285 filename[MaxTextExtent];
313 assert(image_info != (const ImageInfo *) NULL);
314 assert(image_info->signature == MagickSignature);
315 if (image_info->debug != MagickFalse)
316 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
317 image_info->filename);
318 assert(exception != (ExceptionInfo *) NULL);
319 assert(exception->signature == MagickSignature);
320 image=AcquireImage(image_info);
321 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
322 if (status == MagickFalse)
324 image=DestroyImageList(image);
325 return((Image *) NULL);
328 Write raw CCITT Group 4 wrapped as a TIFF image file.
331 unique_file=AcquireUniqueFileResource(filename);
332 if (unique_file != -1)
333 file=fdopen(unique_file,"wb");
334 if ((unique_file == -1) || (file == (FILE *) NULL))
335 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
336 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
337 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
338 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
339 length=WriteLSBLong(file,image->columns);
340 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
341 length=WriteLSBLong(file,image->rows);
342 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
343 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
344 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
345 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
346 strip_offset=10+(12*14)+4+8;
347 length=WriteLSBLong(file,(size_t) strip_offset);
348 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
349 length=WriteLSBLong(file,(size_t) image_info->orientation);
350 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
351 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
352 length=WriteLSBLong(file,image->rows);
353 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
354 offset=(ssize_t) ftell(file)-4;
355 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
356 length=WriteLSBLong(file,(size_t) (strip_offset-8));
357 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
358 length=WriteLSBLong(file,(size_t) (strip_offset-8));
359 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
360 length=fwrite("\000\000\000\000",1,4,file);
361 length=WriteLSBLong(file,image->x_resolution);
362 length=WriteLSBLong(file,1);
363 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
364 (void) fputc(c,file);
365 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
366 length=WriteLSBLong(file,(unsigned int) length);
368 (void) CloseBlob(image);
369 image=DestroyImage(image);
373 read_info=CloneImageInfo((ImageInfo *) NULL);
374 (void) FormatMagickString(read_info->filename,MaxTextExtent,"%s",filename);
375 image=ReadTIFFImage(read_info,exception);
376 read_info=DestroyImageInfo(read_info);
377 if (image != (Image *) NULL)
379 (void) CopyMagickString(image->filename,image_info->filename,
381 (void) CopyMagickString(image->magick_filename,image_info->filename,
383 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
385 (void) RelinquishUniqueFileResource(filename);
390 #if defined(MAGICKCORE_TIFF_DELEGATE)
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 % R e a d T I F F I m a g e %
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 % ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
403 % memory necessary for the new Image structure and returns a pointer to the
406 % The format of the ReadTIFFImage method is:
408 % Image *ReadTIFFImage(const ImageInfo *image_info,
409 % ExceptionInfo *exception)
411 % A description of each parameter follows:
413 % o image_info: the image info.
415 % o exception: return any errors or warnings in this structure.
419 static inline size_t MagickMax(const size_t x,const size_t y)
426 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
433 static MagickBooleanType ReadProfile(Image *image,const char *name,
434 unsigned char *datum,ssize_t length)
448 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
450 for (i=0; i < (length-4); i+=2)
451 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
460 profile=AcquireStringInfo((size_t) length);
461 SetStringInfoDatum(profile,datum+i);
462 status=SetImageProfile(image,name,profile);
463 profile=DestroyStringInfo(profile);
464 if (status == MagickFalse)
465 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
470 #if defined(__cplusplus) || defined(c_plusplus)
474 static int TIFFCloseBlob(thandle_t image)
476 (void) CloseBlob((Image *) image);
480 static void TIFFErrors(const char *module,const char *format,va_list error)
483 message[MaxTextExtent];
488 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
489 (void) vsnprintf(message,MaxTextExtent,format,error);
491 (void) vsprintf(message,format,error);
493 (void) ConcatenateMagickString(message,".",MaxTextExtent);
494 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
495 if (exception != (ExceptionInfo *) NULL)
496 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
500 static void TIFFGetProfiles(TIFF *tiff,Image *image)
508 #if defined(TIFFTAG_ICCPROFILE)
510 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
511 (void) ReadProfile(image,"icc",profile,(ssize_t) length);
513 #if defined(TIFFTAG_PHOTOSHOP)
515 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
516 (void) ReadProfile(image,"8bim",profile,(ssize_t) length);
518 #if defined(TIFFTAG_RICHTIFFIPTC)
520 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
522 if (TIFFIsByteSwapped(tiff) != 0)
523 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
524 (void) ReadProfile(image,"iptc",profile,4L*length);
527 #if defined(TIFFTAG_XMLPACKET)
529 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
530 (void) ReadProfile(image,"xmp",profile,(ssize_t) length);
533 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
534 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length);
537 static void TIFFGetProperties(TIFF *tiff,Image *image)
542 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
543 (void) SetImageProperty(image,"tiff:artist",text);
544 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
545 (void) SetImageProperty(image,"tiff:timestamp",text);
546 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
547 (void) SetImageProperty(image,"tiff:software",text);
548 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
549 (void) SetImageProperty(image,"tiff:hostcomputer",text);
550 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
551 (void) SetImageProperty(image,"tiff:document",text);
552 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
553 (void) SetImageProperty(image,"tiff:make",text);
554 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
555 (void) SetImageProperty(image,"tiff:model",text);
556 if (TIFFGetField(tiff,33432,&text) == 1)
557 (void) SetImageProperty(image,"tiff:copyright",text);
558 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
559 (void) SetImageProperty(image,"label",text);
560 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
561 (void) SetImageProperty(image,"comment",text);
564 static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
566 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
568 value[MaxTextExtent];
580 Read EXIF properties.
582 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
584 directory=TIFFCurrentDirectory(tiff);
585 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
587 for (i=0; exif_info[i].tag != 0; i++)
590 switch (exif_info[i].type)
597 if (TIFFGetField(tiff,exif_info[i].tag,&ascii) != 0)
598 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
606 if (TIFFGetField(tiff,exif_info[i].tag,&shorty) != 0)
607 (void) FormatMagickString(value,MaxTextExtent,"%d",shorty);
615 if (TIFFGetField(tiff,exif_info[i].tag,&ssize_ty) != 0)
616 (void) FormatMagickString(value,MaxTextExtent,"%d",ssize_ty);
625 if (TIFFGetField(tiff,exif_info[i].tag,&rational) != 0)
626 (void) FormatMagickString(value,MaxTextExtent,"%g",rational);
633 (void) SetImageProperty(image,exif_info[i].property,value);
635 TIFFSetDirectory(tiff,directory);
642 static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
644 *base=(tdata_t *) GetBlobStreamData((Image *) image);
645 if (*base != (tdata_t *) NULL)
646 *size=(toff_t) GetBlobSize((Image *) image);
647 if (*base != (tdata_t *) NULL)
652 static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
657 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
658 (unsigned char *) data);
662 static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
663 tsample_t sample,ssize_t row,tdata_t scanline)
668 (void) bits_per_sample;
669 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
673 static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
675 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
678 static toff_t TIFFGetBlobSize(thandle_t image)
680 return((toff_t) GetBlobSize((Image *) image));
683 static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
690 static void TIFFWarnings(const char *module,const char *format,va_list warning)
693 message[MaxTextExtent];
698 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
699 (void) vsnprintf(message,MaxTextExtent,format,warning);
701 (void) vsprintf(message,format,warning);
703 (void) ConcatenateMagickString(message,".",MaxTextExtent);
704 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
705 if (exception != (ExceptionInfo *) NULL)
706 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
707 message,"`%s'",module);
710 static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
715 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
716 (unsigned char *) data);
720 #if defined(__cplusplus) || defined(c_plusplus)
724 static Image *ReadTIFFImage(const ImageInfo *image_info,
725 ExceptionInfo *exception)
729 ReadSingleSampleMethod,
815 assert(image_info != (const ImageInfo *) NULL);
816 assert(image_info->signature == MagickSignature);
817 if (image_info->debug != MagickFalse)
818 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
819 image_info->filename);
820 assert(exception != (ExceptionInfo *) NULL);
821 assert(exception->signature == MagickSignature);
822 image=AcquireImage(image_info);
823 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
824 if (status == MagickFalse)
826 image=DestroyImageList(image);
827 return((Image *) NULL);
829 (void) MagickSetThreadValue(tiff_exception,exception);
830 error_handler=TIFFSetErrorHandler(TIFFErrors);
831 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
832 tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob,
833 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
835 if (tiff == (TIFF *) NULL)
837 (void) TIFFSetWarningHandler(warning_handler);
838 (void) TIFFSetErrorHandler(error_handler);
839 image=DestroyImageList(image);
840 return((Image *) NULL);
842 debug=IsEventLogging();
844 if (image_info->number_scenes != 0)
847 Generate blank images for subimage specification (e.g. image.tif[4].
849 for (i=0; i < (ssize_t) image_info->scene; i++)
851 (void) TIFFReadDirectory(tiff);
852 AcquireNextImage(image_info,image);
853 if (GetNextImageInList(image) == (Image *) NULL)
855 image=DestroyImageList(image);
856 return((Image *) NULL);
858 image=SyncNextImageInList(image);
863 if (0 && (image_info->verbose != MagickFalse))
864 TIFFPrintDirectory(tiff,stdout,MagickFalse);
865 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
867 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
872 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
873 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
874 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
875 if (sample_format == SAMPLEFORMAT_IEEEFP)
876 (void) SetImageProperty(image,"quantum:format","floating-point");
877 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
878 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
879 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
882 case PHOTOMETRIC_MINISBLACK:
884 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
887 case PHOTOMETRIC_MINISWHITE:
889 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
892 case PHOTOMETRIC_PALETTE:
894 (void) SetImageProperty(image,"tiff:photometric","palette");
897 case PHOTOMETRIC_RGB:
899 (void) SetImageProperty(image,"tiff:photometric","RGB");
902 case PHOTOMETRIC_CIELAB:
904 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
907 case PHOTOMETRIC_SEPARATED:
909 (void) SetImageProperty(image,"tiff:photometric","separated");
912 case PHOTOMETRIC_YCBCR:
914 (void) SetImageProperty(image,"tiff:photometric","YCBCR");
919 (void) SetImageProperty(image,"tiff:photometric","unknown");
923 if (image->debug != MagickFalse)
925 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
926 (unsigned int) width,(unsigned int) height);
927 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
929 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
930 "Bits per sample: %u",bits_per_sample);
931 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
932 "Min sample value: %u",min_sample_value);
933 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
934 "Max sample value: %u",max_sample_value);
935 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
936 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
938 image->columns=(size_t) width;
939 image->rows=(size_t) height;
940 image->depth=(size_t) bits_per_sample;
941 if (image->debug != MagickFalse)
942 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
943 (double) image->depth);
945 image->endian=MSBEndian;
946 if ((int) (*(char *) &lsb_first) != 0)
947 image->endian=LSBEndian;
948 if (photometric == PHOTOMETRIC_SEPARATED)
949 image->colorspace=CMYKColorspace;
950 if (photometric == PHOTOMETRIC_CIELAB)
951 image->colorspace=LabColorspace;
952 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
954 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
955 x_resolution=(float) image->x_resolution;
956 y_resolution=(float) image->y_resolution;
957 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
958 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
959 image->x_resolution=x_resolution;
960 image->y_resolution=y_resolution;
961 x_position=(float) image->page.x/x_resolution;
962 y_position=(float) image->page.y/y_resolution;
963 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
964 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
965 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
966 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
967 image->orientation=(OrientationType) orientation;
968 chromaticity=(float *) NULL;
969 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
970 if (chromaticity != (float *) NULL)
972 image->chromaticity.white_point.x=chromaticity[0];
973 image->chromaticity.white_point.y=chromaticity[1];
975 chromaticity=(float *) NULL;
976 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
977 if (chromaticity != (float *) NULL)
979 image->chromaticity.red_primary.x=chromaticity[0];
980 image->chromaticity.red_primary.y=chromaticity[1];
981 image->chromaticity.green_primary.x=chromaticity[2];
982 image->chromaticity.green_primary.y=chromaticity[3];
983 image->chromaticity.blue_primary.x=chromaticity[4];
984 image->chromaticity.blue_primary.y=chromaticity[5];
986 TIFFGetProperties(tiff,image);
987 option=GetImageOption(image_info,"tiff:exif");
988 if ((option != (const char *) NULL) &&
989 (IsMagickTrue(option) != MagickFalse))
990 TIFFGetEXIFProperties(tiff,image);
991 TIFFGetProfiles(tiff,image);
993 Allocate memory for the image and pixel buffer.
995 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
996 if ((compress_tag != COMPRESSION_NONE) &&
997 (TIFFIsCODECConfigured(compress_tag) == 0))
1000 ThrowReaderException(CoderError,"CompressNotSupported");
1003 switch (compress_tag)
1005 case COMPRESSION_NONE: image->compression=NoCompression; break;
1006 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1007 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1008 case COMPRESSION_JPEG:
1010 image->compression=JPEGCompression;
1011 #if defined(JPEG_SUPPORT)
1014 sampling_factor[MaxTextExtent];
1020 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1021 &horizontal,&vertical);
1022 (void) FormatMagickString(sampling_factor,MaxTextExtent,"%dx%d",
1023 horizontal,vertical);
1024 (void) SetImageProperty(image,"jpeg:sampling-factor",
1026 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1027 "Sampling Factors: %s",sampling_factor);
1028 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1030 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1032 photometric=PHOTOMETRIC_RGB;
1038 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1039 #if defined(COMPRESSION_LZMA)
1040 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1042 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1043 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1044 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1045 default: image->compression=RLECompression; break;
1047 quantum_info=AcquireQuantumInfo(image_info,image);
1048 if (quantum_info == (QuantumInfo *) NULL)
1051 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1053 if (sample_format == SAMPLEFORMAT_UINT)
1054 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1055 if (sample_format == SAMPLEFORMAT_INT)
1056 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1057 if (sample_format == SAMPLEFORMAT_IEEEFP)
1058 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1059 if (status == MagickFalse)
1062 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1065 switch (photometric)
1067 case PHOTOMETRIC_MINISBLACK:
1069 quantum_info->min_is_white=MagickFalse;
1072 case PHOTOMETRIC_MINISWHITE:
1074 quantum_info->min_is_white=MagickTrue;
1080 associated_alpha=MagickFalse;
1082 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1084 if (extra_samples == 0)
1086 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1087 image->matte=MagickTrue;
1090 for (i=0; i < extra_samples; i++)
1092 image->matte=MagickTrue;
1093 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
1094 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
1096 option=GetImageOption(image_info,"tiff:alpha");
1097 if (option != (const char *) NULL)
1098 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1100 if (image->matte != MagickFalse)
1101 (void) SetImageProperty(image,"tiff:alpha",
1102 associated_alpha != MagickFalse ? "associated" : "unassociated");
1103 if ((photometric == PHOTOMETRIC_PALETTE) &&
1104 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1109 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
1110 if (AcquireImageColormap(image,colors) == MagickFalse)
1113 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1116 if (units == RESUNIT_INCH)
1117 image->units=PixelsPerInchResolution;
1118 if (units == RESUNIT_CENTIMETER)
1119 image->units=PixelsPerCentimeterResolution;
1120 value=(unsigned short) image->scene;
1121 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1123 if (image_info->ping != MagickFalse)
1125 if (image_info->number_scenes != 0)
1126 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1128 goto next_tiff_frame;
1130 method=ReadGenericMethod;
1131 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1134 value[MaxTextExtent];
1136 method=ReadStripMethod;
1137 (void) FormatMagickString(value,MaxTextExtent,"%u",
1138 (unsigned int) rows_per_strip);
1139 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1141 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1142 method=ReadRGBAMethod;
1143 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1144 method=ReadCMYKAMethod;
1145 if ((photometric != PHOTOMETRIC_RGB) &&
1146 (photometric != PHOTOMETRIC_CIELAB) &&
1147 (photometric != PHOTOMETRIC_SEPARATED))
1148 method=ReadGenericMethod;
1149 if (image->storage_class == PseudoClass)
1150 method=ReadSingleSampleMethod;
1151 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1152 (photometric == PHOTOMETRIC_MINISWHITE))
1153 method=ReadSingleSampleMethod;
1154 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1155 (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64))
1156 method=ReadGenericMethod;
1157 if (image->compression == JPEGCompression)
1158 method=ReadGenericMethod;
1159 if (TIFFIsTiled(tiff) != MagickFalse)
1160 method=ReadTileMethod;
1161 quantum_type=RGBQuantum;
1162 pixels=GetQuantumPixels(quantum_info);
1165 case ReadSingleSampleMethod:
1168 Convert TIFF image to PseudoClass MIFF image.
1170 if ((image->storage_class == PseudoClass) &&
1171 (photometric == PHOTOMETRIC_PALETTE))
1182 Initialize colormap.
1184 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1185 &green_colormap,&blue_colormap);
1186 range=255; /* might be old style 8-bit colormap */
1187 for (i=0; i < (ssize_t) image->colors; i++)
1188 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1189 (blue_colormap[i] >= 256))
1194 for (i=0; i < (ssize_t) image->colors; i++)
1196 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
1197 red_colormap[i])/range);
1198 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
1199 green_colormap[i])/range);
1200 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
1201 blue_colormap[i])/range);
1204 quantum_type=IndexQuantum;
1205 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1206 if (image->matte != MagickFalse)
1208 if (image->storage_class != PseudoClass)
1210 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1212 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1216 quantum_type=IndexAlphaQuantum;
1217 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1221 if (image->storage_class != PseudoClass)
1223 quantum_type=GrayQuantum;
1224 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1226 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1227 if (status == MagickFalse)
1230 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1232 pixels=GetQuantumPixels(quantum_info);
1233 for (y=0; y < (ssize_t) image->rows; y++)
1238 register PixelPacket
1241 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1244 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1245 if (q == (PixelPacket *) NULL)
1247 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1248 quantum_type,pixels,exception);
1250 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1252 if (image->previous == (Image *) NULL)
1254 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1256 if (status == MagickFalse)
1262 case ReadRGBAMethod:
1265 Convert TIFF image to DirectClass MIFF image.
1267 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1268 quantum_type=RGBQuantum;
1269 if (image->matte != MagickFalse)
1271 quantum_type=RGBAQuantum;
1272 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1274 if (image->colorspace == CMYKColorspace)
1276 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1277 quantum_type=CMYKQuantum;
1278 if (image->matte != MagickFalse)
1280 quantum_type=CMYKAQuantum;
1281 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1284 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1285 if (status == MagickFalse)
1288 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1290 pixels=GetQuantumPixels(quantum_info);
1291 for (y=0; y < (ssize_t) image->rows; y++)
1296 register PixelPacket
1299 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1302 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1303 if (q == (PixelPacket *) NULL)
1305 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1306 quantum_type,pixels,exception);
1307 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1309 if (image->previous == (Image *) NULL)
1311 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1313 if (status == MagickFalse)
1319 case ReadCMYKAMethod:
1322 Convert TIFF image to DirectClass MIFF image.
1324 for (i=0; i < (ssize_t) samples_per_pixel; i++)
1326 for (y=0; y < (ssize_t) image->rows; y++)
1328 register PixelPacket
1334 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1338 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1339 if (q == (PixelPacket *) NULL)
1341 if (image->colorspace != CMYKColorspace)
1344 case 0: quantum_type=RedQuantum; break;
1345 case 1: quantum_type=GreenQuantum; break;
1346 case 2: quantum_type=BlueQuantum; break;
1347 case 3: quantum_type=AlphaQuantum; break;
1348 default: quantum_type=UndefinedQuantum; break;
1353 case 0: quantum_type=CyanQuantum; break;
1354 case 1: quantum_type=MagentaQuantum; break;
1355 case 2: quantum_type=YellowQuantum; break;
1356 case 3: quantum_type=BlackQuantum; break;
1357 case 4: quantum_type=AlphaQuantum; break;
1358 default: quantum_type=UndefinedQuantum; break;
1360 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1361 quantum_type,pixels,exception);
1362 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1365 if (image->previous == (Image *) NULL)
1367 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1369 if (status == MagickFalse)
1375 case ReadStripMethod:
1377 register unsigned char
1381 Convert stripped TIFF image to DirectClass MIFF image.
1384 p=(unsigned char *) NULL;
1385 for (y=0; y < (ssize_t) image->rows; y++)
1390 register PixelPacket
1393 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1394 if (q == (PixelPacket *) NULL)
1398 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1400 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1404 p=(unsigned char *) (((uint32 *) pixels)+image->columns*i);
1405 for (x=0; x < (ssize_t) image->columns; x++)
1407 q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
1408 q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
1409 q->blue=ScaleCharToQuantum((unsigned char) (TIFFGetB(*p)));
1410 if (image->matte != MagickFalse)
1411 q->opacity=ScaleCharToQuantum((unsigned char) (TIFFGetA(*p)));
1415 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1417 if (image->previous == (Image *) NULL)
1419 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1421 if (status == MagickFalse)
1427 case ReadTileMethod:
1441 Convert tiled TIFF image to DirectClass MIFF image.
1443 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1444 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1447 ThrowReaderException(CoderError,"ImageIsNotTiled");
1449 (void) SetImageStorageClass(image,DirectClass);
1450 number_pixels=columns*rows;
1451 tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels,
1452 sizeof(*tile_pixels));
1453 if (tile_pixels == (uint32 *) NULL)
1456 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1458 for (y=0; y < (ssize_t) image->rows; y+=rows)
1466 register PixelPacket
1473 rows_remaining=image->rows-y;
1474 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
1475 rows_remaining=rows;
1476 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1478 if (tile == (PixelPacket *) NULL)
1480 for (x=0; x < (ssize_t) image->columns; x+=columns)
1486 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1488 columns_remaining=image->columns-x;
1489 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
1490 columns_remaining=columns;
1491 p=tile_pixels+(rows-rows_remaining)*columns;
1492 q=tile+(image->columns*(rows_remaining-1)+x);
1493 for (row=rows_remaining; row > 0; row--)
1495 if (image->matte != MagickFalse)
1496 for (column=columns_remaining; column > 0; column--)
1498 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1499 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1500 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1501 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1502 (unsigned char) TIFFGetA(*p)));
1507 for (column=columns_remaining; column > 0; column--)
1509 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1510 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1511 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1515 p+=columns-columns_remaining;
1516 q-=(image->columns+columns_remaining);
1519 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1521 if (image->previous == (Image *) NULL)
1523 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1525 if (status == MagickFalse)
1529 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1532 case ReadGenericMethod:
1542 Convert TIFF image to DirectClass MIFF image.
1544 number_pixels=(MagickSizeType) image->columns*image->rows;
1545 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1546 (number_pixels*sizeof(uint32))))
1549 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1551 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1553 if (pixels == (uint32 *) NULL)
1556 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1558 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1559 (uint32) image->rows,(uint32 *) pixels,0);
1561 Convert image to DirectClass pixel packets.
1563 p=pixels+number_pixels-1;
1564 for (y=0; y < (ssize_t) image->rows; y++)
1569 register PixelPacket
1572 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1573 if (q == (PixelPacket *) NULL)
1575 q+=image->columns-1;
1576 for (x=0; x < (ssize_t) image->columns; x++)
1578 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1579 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1580 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1581 if (image->matte != MagickFalse)
1582 q->opacity=(Quantum) (QuantumRange-
1583 ScaleCharToQuantum((unsigned char) TIFFGetA(*p)));
1587 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1589 if (image->previous == (Image *) NULL)
1591 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1593 if (status == MagickFalse)
1597 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1601 SetQuantumImageType(image,quantum_type);
1603 if ((photometric == PHOTOMETRIC_LOGL) ||
1604 (photometric == PHOTOMETRIC_MINISBLACK) ||
1605 (photometric == PHOTOMETRIC_MINISWHITE))
1607 image->type=GrayscaleType;
1608 if (bits_per_sample == 1)
1609 image->type=BilevelType;
1611 if (image->storage_class == PseudoClass)
1612 image->depth=GetImageDepth(image,exception);
1613 image->endian=MSBEndian;
1614 if (endian == FILLORDER_LSB2MSB)
1615 image->endian=LSBEndian;
1616 if ((photometric == PHOTOMETRIC_LOGL) ||
1617 (photometric == PHOTOMETRIC_MINISBLACK) ||
1618 (photometric == PHOTOMETRIC_MINISWHITE))
1620 image->type=GrayscaleType;
1621 if (bits_per_sample == 1)
1622 image->type=BilevelType;
1625 Proceed to next image.
1627 if (image_info->number_scenes != 0)
1628 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1630 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1631 if (status == MagickTrue)
1634 Allocate next image structure.
1636 AcquireNextImage(image_info,image);
1637 if (GetNextImageInList(image) == (Image *) NULL)
1639 image=DestroyImageList(image);
1640 return((Image *) NULL);
1642 image=SyncNextImageInList(image);
1643 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1645 if (status == MagickFalse)
1648 quantum_info=DestroyQuantumInfo(quantum_info);
1649 } while (status == MagickTrue);
1650 (void) TIFFSetWarningHandler(warning_handler);
1651 (void) TIFFSetErrorHandler(error_handler);
1653 return(GetFirstImageInList(image));
1658 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1662 % R e g i s t e r T I F F I m a g e %
1666 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1668 % RegisterTIFFImage() adds properties for the TIFF image format to
1669 % the list of supported formats. The properties include the image format
1670 % tag, a method to read and/or write the format, whether the format
1671 % supports the saving of more than one frame to the same file or blob,
1672 % whether the format supports native in-memory I/O, and a brief
1673 % description of the format.
1675 % The format of the RegisterTIFFImage method is:
1677 % size_t RegisterTIFFImage(void)
1680 ModuleExport size_t RegisterTIFFImage(void)
1682 #define TIFFDescription "Tagged Image File Format"
1685 version[MaxTextExtent];
1690 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1691 tiff_semaphore=AllocateSemaphoreInfo();
1692 LockSemaphoreInfo(tiff_semaphore);
1693 if (instantiate_key == MagickFalse)
1695 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1696 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1697 instantiate_key=MagickTrue;
1699 UnlockSemaphoreInfo(tiff_semaphore);
1701 #if defined(TIFF_VERSION)
1702 (void) FormatMagickString(version,MaxTextExtent,"%d",TIFF_VERSION);
1704 #if defined(MAGICKCORE_TIFF_DELEGATE)
1713 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1719 entry=SetMagickInfo("GROUP4");
1720 #if defined(MAGICKCORE_TIFF_DELEGATE)
1721 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1722 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1724 entry->raw=MagickTrue;
1725 entry->endian_support=MagickTrue;
1726 entry->adjoin=MagickFalse;
1727 entry->format_type=ImplicitFormatType;
1728 entry->seekable_stream=MagickTrue;
1729 entry->thread_support=NoThreadSupport;
1730 entry->description=ConstantString("Raw CCITT Group4");
1731 entry->module=ConstantString("TIFF");
1732 (void) RegisterMagickInfo(entry);
1733 entry=SetMagickInfo("PTIF");
1734 #if defined(MAGICKCORE_TIFF_DELEGATE)
1735 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1736 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1738 entry->endian_support=MagickTrue;
1739 entry->seekable_stream=MagickTrue;
1740 entry->thread_support=NoThreadSupport;
1741 entry->description=ConstantString("Pyramid encoded TIFF");
1742 entry->module=ConstantString("TIFF");
1743 (void) RegisterMagickInfo(entry);
1744 entry=SetMagickInfo("TIF");
1745 #if defined(MAGICKCORE_TIFF_DELEGATE)
1746 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1747 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1749 entry->endian_support=MagickTrue;
1750 entry->seekable_stream=MagickTrue;
1751 entry->stealth=MagickTrue;
1752 entry->thread_support=NoThreadSupport;
1753 entry->description=ConstantString(TIFFDescription);
1754 if (*version != '\0')
1755 entry->version=ConstantString(version);
1756 entry->module=ConstantString("TIFF");
1757 (void) RegisterMagickInfo(entry);
1758 entry=SetMagickInfo("TIFF");
1759 #if defined(MAGICKCORE_TIFF_DELEGATE)
1760 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1761 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1763 entry->magick=(IsImageFormatHandler *) IsTIFF;
1764 entry->endian_support=MagickTrue;
1765 entry->seekable_stream=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("TIFF64");
1773 #if defined(TIFF_VERSION_BIG)
1774 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1775 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1777 entry->adjoin=MagickFalse;
1778 entry->endian_support=MagickTrue;
1779 entry->seekable_stream=MagickTrue;
1780 entry->thread_support=NoThreadSupport;
1781 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1782 if (*version != '\0')
1783 entry->version=ConstantString(version);
1784 entry->module=ConstantString("TIFF");
1785 (void) RegisterMagickInfo(entry);
1786 return(MagickImageCoderSignature);
1790 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1794 % U n r e g i s t e r T I F F I m a g e %
1798 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1800 % UnregisterTIFFImage() removes format registrations made by the TIFF module
1801 % from the list of supported formats.
1803 % The format of the UnregisterTIFFImage method is:
1805 % UnregisterTIFFImage(void)
1808 ModuleExport void UnregisterTIFFImage(void)
1810 (void) UnregisterMagickInfo("RAWGROUP4");
1811 (void) UnregisterMagickInfo("PTIF");
1812 (void) UnregisterMagickInfo("TIF");
1813 (void) UnregisterMagickInfo("TIFF");
1814 (void) UnregisterMagickInfo("TIFF64");
1815 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1816 tiff_semaphore=AllocateSemaphoreInfo();
1817 LockSemaphoreInfo(tiff_semaphore);
1818 if (instantiate_key != MagickFalse)
1819 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1820 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1821 instantiate_key=MagickFalse;
1822 UnlockSemaphoreInfo(tiff_semaphore);
1823 DestroySemaphoreInfo(&tiff_semaphore);
1826 #if defined(MAGICKCORE_TIFF_DELEGATE)
1828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1832 % W r i t e G R O U P 4 I m a g e %
1836 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1838 % WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1840 % The format of the WriteGROUP4Image method is:
1842 % MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1845 % A description of each parameter follows:
1847 % o image_info: the image info.
1849 % o image: The image.
1852 static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1856 filename[MaxTextExtent];
1890 Write image as CCITT Group4 TIFF image to a temporary file.
1892 assert(image_info != (const ImageInfo *) NULL);
1893 assert(image_info->signature == MagickSignature);
1894 assert(image != (Image *) NULL);
1895 assert(image->signature == MagickSignature);
1896 if (image->debug != MagickFalse)
1897 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1898 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1899 if (status == MagickFalse)
1901 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1902 if (huffman_image == (Image *) NULL)
1904 (void) CloseBlob(image);
1905 return(MagickFalse);
1907 huffman_image->endian=MSBEndian;
1909 unique_file=AcquireUniqueFileResource(filename);
1910 if (unique_file != -1)
1911 file=fdopen(unique_file,"wb");
1912 if ((unique_file == -1) || (file == (FILE *) NULL))
1914 ThrowFileException(&image->exception,FileOpenError,
1915 "UnableToCreateTemporaryFile",filename);
1916 return(MagickFalse);
1918 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1920 (void) SetImageType(huffman_image,BilevelType);
1921 write_info=CloneImageInfo((ImageInfo *) NULL);
1922 SetImageInfoFile(write_info,file);
1923 write_info->compression=Group4Compression;
1924 write_info->type=BilevelType;
1925 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1926 status=WriteTIFFImage(write_info,huffman_image);
1927 (void) fflush(file);
1928 write_info=DestroyImageInfo(write_info);
1929 if (status == MagickFalse)
1931 InheritException(&image->exception,&huffman_image->exception);
1932 huffman_image=DestroyImage(huffman_image);
1933 (void) fclose(file);
1934 (void) RelinquishUniqueFileResource(filename);
1935 return(MagickFalse);
1937 tiff=TIFFOpen(filename,"rb");
1938 if (tiff == (TIFF *) NULL)
1940 huffman_image=DestroyImage(huffman_image);
1941 (void) fclose(file);
1942 (void) RelinquishUniqueFileResource(filename);
1943 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1944 image_info->filename);
1945 return(MagickFalse);
1948 Allocate raw strip buffer.
1950 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1953 huffman_image=DestroyImage(huffman_image);
1954 (void) fclose(file);
1955 (void) RelinquishUniqueFileResource(filename);
1956 return(MagickFalse);
1958 strip_size=byte_count[0];
1959 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
1960 if (byte_count[i] > strip_size)
1961 strip_size=byte_count[i];
1962 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1964 if (buffer == (unsigned char *) NULL)
1967 huffman_image=DestroyImage(huffman_image);
1968 (void) fclose(file);
1969 (void) RelinquishUniqueFileResource(filename);
1970 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1971 image_info->filename);
1974 Compress runlength encoded to 2D Huffman pixels.
1976 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
1978 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
1979 if (WriteBlob(image,(size_t) count,buffer) != count)
1982 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1984 huffman_image=DestroyImage(huffman_image);
1985 (void) fclose(file);
1986 (void) RelinquishUniqueFileResource(filename);
1987 (void) CloseBlob(image);
1992 #if defined(MAGICKCORE_TIFF_DELEGATE)
1994 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1998 % W r i t e P T I F I m a g e %
2002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2004 % WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2007 % The format of the WritePTIFImage method is:
2009 % MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2012 % A description of each parameter follows:
2014 % o image_info: the image info.
2016 % o image: The image.
2019 static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2038 Create pyramid-encoded TIFF image.
2040 images=NewImageList();
2041 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2043 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2044 &image->exception));
2045 columns=next->columns;
2047 while ((columns > 64) && (rows > 64))
2051 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2053 AppendImageToList(&images,pyramid_image);
2057 Write pyramid-encoded TIFF image.
2059 write_info=CloneImageInfo(image_info);
2060 *write_info->magick='\0';
2061 write_info->adjoin=MagickTrue;
2062 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2063 images=DestroyImageList(images);
2064 write_info=DestroyImageInfo(write_info);
2069 #if defined(MAGICKCORE_TIFF_DELEGATE)
2071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2074 % W r i t e T I F F I m a g e %
2078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2080 % WriteTIFFImage() writes an image in the Tagged image file format.
2082 % The format of the WriteTIFFImage method is:
2084 % MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2087 % A description of each parameter follows:
2089 % o image_info: the image info.
2091 % o image: The image.
2095 typedef struct _TIFFInfo
2106 static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2108 assert(tiff_info != (TIFFInfo *) NULL);
2109 if (tiff_info->scanlines != (unsigned char *) NULL)
2110 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2111 tiff_info->scanlines);
2112 if (tiff_info->pixels != (unsigned char *) NULL)
2113 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2117 static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2118 TIFFInfo *tiff_info)
2130 assert(tiff_info != (TIFFInfo *) NULL);
2131 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2132 option=GetImageOption(image_info,"tiff:tile-geometry");
2133 if (option == (const char *) NULL)
2135 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2136 if ((flags & HeightValue) == 0)
2137 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2138 tile_columns=(uint32) tiff_info->tile_geometry.width;
2139 tile_rows=(uint32) tiff_info->tile_geometry.height;
2140 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2141 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2142 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2143 tiff_info->tile_geometry.width=tile_columns;
2144 tiff_info->tile_geometry.height=tile_rows;
2145 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2146 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
2147 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2148 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2149 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2150 (tiff_info->pixels == (unsigned char *) NULL))
2152 DestroyTIFFInfo(tiff_info);
2153 return(MagickFalse);
2158 static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
2159 tsample_t sample,Image *image)
2173 register unsigned char
2181 if (TIFFIsTiled(tiff) == 0)
2182 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2184 Fill scanlines to tile height.
2186 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2187 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2188 (size_t) TIFFScanlineSize(tiff));
2189 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2190 (tiff_info->tile_geometry.height-1)) && (row != (ssize_t) (image->rows-1)))
2193 Write tile to TIFF image.
2196 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
2197 tiff_info->tile_geometry.width);
2198 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2199 tiff_info->tile_geometry.width;
2200 for (i=0; i < (ssize_t) number_tiles; i++)
2202 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
2203 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2204 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2205 for (k=0; k < (ssize_t) tile_width; k++)
2207 if (bytes_per_pixel == 0)
2209 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2210 tiff_info->tile_geometry.width+k)/8);
2211 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2215 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2216 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2217 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2218 for (l=0; l < bytes_per_pixel; l++)
2221 if ((i*tiff_info->tile_geometry.width) != image->columns)
2222 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2223 tiff_info->tile_geometry.width),(uint32) ((row/
2224 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2232 static void TIFFSetProfiles(TIFF *tiff,Image *image)
2240 if (image->profiles == (void *) NULL)
2242 ResetImageProfileIterator(image);
2243 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2245 profile=GetImageProfile(image,name);
2246 #if defined(TIFFTAG_XMLPACKET)
2247 if (LocaleCompare(name,"xmp") == 0)
2248 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2249 profile),GetStringInfoDatum(profile));
2251 #if defined(TIFFTAG_ICCPROFILE)
2252 if (LocaleCompare(name,"icc") == 0)
2253 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2254 profile),GetStringInfoDatum(profile));
2256 if (LocaleCompare(name,"iptc") == 0)
2264 iptc_profile=CloneStringInfo(profile);
2265 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2267 SetStringInfoLength(iptc_profile,length);
2268 if (TIFFIsByteSwapped(tiff))
2269 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2270 (unsigned long) (length/4));
2271 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2272 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2273 iptc_profile=DestroyStringInfo(iptc_profile);
2275 #if defined(TIFFTAG_PHOTOSHOP)
2276 if (LocaleCompare(name,"8bim") == 0)
2277 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2278 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2280 if (LocaleCompare(name,"tiff:37724") == 0)
2281 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2282 GetStringInfoDatum(profile));
2283 name=GetNextImageProfile(image);
2287 static void TIFFSetProperties(TIFF *tiff,Image *image)
2292 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
2293 value=GetImageProperty(image,"tiff:hostcomputer");
2294 if (value != (const char *) NULL)
2295 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2296 value=GetImageProperty(image,"tiff:artist");
2297 if (value != (const char *) NULL)
2298 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2299 value=GetImageProperty(image,"tiff:timestamp");
2300 if (value != (const char *) NULL)
2301 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2302 value=GetImageProperty(image,"tiff:make");
2303 if (value != (const char *) NULL)
2304 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2305 value=GetImageProperty(image,"tiff:model");
2306 if (value != (const char *) NULL)
2307 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2308 value=GetImageProperty(image,"tiff:software");
2309 if (value != (const char *) NULL)
2310 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
2311 value=GetImageProperty(image,"tiff:copyright");
2312 if (value != (const char *) NULL)
2313 (void) TIFFSetField(tiff,33432,value);
2314 value=GetImageProperty(image,"kodak-33423");
2315 if (value != (const char *) NULL)
2316 (void) TIFFSetField(tiff,33423,value);
2317 value=GetImageProperty(image,"kodak-36867");
2318 if (value != (const char *) NULL)
2319 (void) TIFFSetField(tiff,36867,value);
2320 value=GetImageProperty(image,"label");
2321 if (value != (const char *) NULL)
2322 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2323 value=GetImageProperty(image,"comment");
2324 if (value != (const char *) NULL)
2325 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2328 static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2330 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2341 Write EXIF properties.
2344 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2345 for (i=0; exif_info[i].tag != 0; i++)
2347 value=GetImageProperty(image,exif_info[i].property);
2348 if (value == (const char *) NULL)
2350 switch (exif_info[i].type)
2354 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2362 shorty=(uint16) StringToLong(value);
2363 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2371 ssize_ty=(uint16) StringToLong(value);
2372 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
2376 case TIFF_SRATIONAL:
2381 rational=StringToDouble(value);
2382 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2389 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2396 static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2399 #if !defined(TIFFDefaultStripSize)
2400 #define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2461 assert(image_info != (const ImageInfo *) NULL);
2462 assert(image_info->signature == MagickSignature);
2463 assert(image != (Image *) NULL);
2464 assert(image->signature == MagickSignature);
2465 if (image->debug != MagickFalse)
2466 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2467 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2468 if (status == MagickFalse)
2470 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2471 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2472 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
2473 endian_type=UndefinedEndian;
2474 option=GetImageOption(image_info,"tiff:endian");
2475 if (option != (const char *) NULL)
2477 if (LocaleNCompare(option,"msb",3) == 0)
2478 endian_type=MSBEndian;
2479 if (LocaleNCompare(option,"lsb",3) == 0)
2480 endian_type=LSBEndian;;
2482 switch (endian_type)
2484 case LSBEndian: mode="wl"; break;
2485 case MSBEndian: mode="wb"; break;
2486 default: mode="w"; break;
2488 #if defined(TIFF_VERSION_BIG)
2489 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
2490 switch (endian_type)
2492 case LSBEndian: mode="wl8"; break;
2493 case MSBEndian: mode="wb8"; break;
2494 default: mode="w8"; break;
2497 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2498 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2500 if (tiff == (TIFF *) NULL)
2502 (void) TIFFSetWarningHandler(warning_handler);
2503 (void) TIFFSetErrorHandler(error_handler);
2504 return(MagickFalse);
2507 debug=IsEventLogging();
2512 Initialize TIFF fields.
2514 if ((image_info->type != UndefinedType) &&
2515 (image_info->type != OptimizeType))
2516 (void) SetImageType(image,image_info->type);
2517 quantum_info=AcquireQuantumInfo(image_info,image);
2518 if (quantum_info == (QuantumInfo *) NULL)
2519 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2520 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2521 (quantum_info->format == UndefinedQuantumFormat) &&
2522 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2524 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2525 if (status == MagickFalse)
2526 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2528 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2529 (GetPreviousImageInList(image) != (Image *) NULL))
2530 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2531 if ((image->columns != (uint32) image->columns) ||
2532 (image->rows != (uint32) image->rows))
2533 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2534 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2535 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2536 compression=image->compression;
2537 if (image_info->compression != UndefinedCompression)
2538 compression=image_info->compression;
2539 switch (compression)
2541 case FaxCompression:
2543 compress_tag=COMPRESSION_CCITTFAX3;
2544 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2547 case Group4Compression:
2549 compress_tag=COMPRESSION_CCITTFAX4;
2550 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2553 #if defined(COMPRESSION_JBIG)
2554 case JBIG1Compression:
2556 compress_tag=COMPRESSION_JBIG;
2560 case JPEGCompression:
2562 compress_tag=COMPRESSION_JPEG;
2565 #if defined(COMPRESSION_LZMA)
2566 case LZMACompression:
2568 compress_tag=COMPRESSION_LZMA;
2572 case LZWCompression:
2574 compress_tag=COMPRESSION_LZW;
2577 case RLECompression:
2579 compress_tag=COMPRESSION_PACKBITS;
2582 case ZipCompression:
2584 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2590 compress_tag=COMPRESSION_NONE;
2594 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2595 if ((compress_tag != COMPRESSION_NONE) &&
2596 (TIFFIsCODECConfigured(compress_tag) == 0))
2598 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2599 CoderError,"CompressionNotSupported","`%s'",CommandOptionToMnemonic(
2600 MagickCompressOptions,(ssize_t) compression));
2601 compress_tag=COMPRESSION_NONE;
2602 compression=NoCompression;
2605 switch (compress_tag)
2607 #if defined(CCITT_SUPPORT)
2608 case COMPRESSION_CCITTFAX3:
2609 case COMPRESSION_CCITTFAX4:
2611 #if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2612 case COMPRESSION_JPEG:
2614 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2615 case COMPRESSION_LZMA:
2617 #if defined(LZW_SUPPORT)
2618 case COMPRESSION_LZW:
2620 #if defined(PACKBITS_SUPPORT)
2621 case COMPRESSION_PACKBITS:
2623 #if defined(ZIP_SUPPORT)
2624 case COMPRESSION_ADOBE_DEFLATE:
2626 case COMPRESSION_NONE:
2630 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2631 CoderError,"CompressionNotSupported","`%s'",CommandOptionToMnemonic(
2632 MagickCompressOptions,(ssize_t) compression));
2633 compress_tag=COMPRESSION_NONE;
2634 compression=NoCompression;
2639 switch (compression)
2641 case FaxCompression:
2642 case Group4Compression:
2644 (void) SetImageType(image,BilevelType);
2647 case JPEGCompression:
2649 (void) SetImageStorageClass(image,DirectClass);
2650 (void) SetImageDepth(image,8);
2656 if (image->colorspace == CMYKColorspace)
2658 photometric=PHOTOMETRIC_SEPARATED;
2659 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2660 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2665 Full color TIFF raster.
2667 if (image->colorspace == LabColorspace)
2668 photometric=PHOTOMETRIC_CIELAB;
2670 if (image->colorspace == YCbCrColorspace)
2672 photometric=PHOTOMETRIC_YCBCR;
2673 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
2674 (void) SetImageStorageClass(image,DirectClass);
2675 (void) SetImageDepth(image,8);
2679 if (image->colorspace != RGBColorspace)
2680 (void) TransformImageColorspace(image,RGBColorspace);
2681 photometric=PHOTOMETRIC_RGB;
2683 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2684 if ((image_info->type != TrueColorType) &&
2685 (image_info->type != TrueColorMatteType))
2687 if ((image_info->type != PaletteType) &&
2688 (IsGrayImage(image,&image->exception) != MagickFalse))
2690 photometric=(uint16) (quantum_info->min_is_white !=
2691 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2692 PHOTOMETRIC_MINISBLACK);
2693 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
2694 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
2695 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2697 status=SetQuantumDepth(image,quantum_info,1);
2698 if (status == MagickFalse)
2699 ThrowWriterException(ResourceLimitError,
2700 "MemoryAllocationFailed");
2704 if (image->storage_class == PseudoClass)
2710 Colormapped TIFF raster.
2712 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
2713 photometric=PHOTOMETRIC_PALETTE;
2715 while ((GetQuantumRange(depth)+1) < image->colors)
2717 status=SetQuantumDepth(image,quantum_info,depth);
2718 if (status == MagickFalse)
2719 ThrowWriterException(ResourceLimitError,
2720 "MemoryAllocationFailed");
2724 switch (image->endian)
2728 endian=FILLORDER_LSB2MSB;
2733 endian=FILLORDER_MSB2LSB;
2736 case UndefinedEndian:
2739 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2744 image->endian=MSBEndian;
2745 if ((int) (*(char *) &lsb_first) != 0)
2746 image->endian=LSBEndian;
2747 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2748 (photometric != PHOTOMETRIC_MINISWHITE))
2750 compress_tag=COMPRESSION_NONE;
2751 endian=FILLORDER_MSB2LSB;
2754 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2755 (photometric != PHOTOMETRIC_MINISWHITE))
2757 compress_tag=COMPRESSION_NONE;
2758 endian=FILLORDER_MSB2LSB;
2760 option=GetImageProperty(image,"tiff:fill-order");
2761 if (option != (const char *) NULL)
2763 if (LocaleNCompare(option,"msb",3) == 0)
2764 endian=FILLORDER_MSB2LSB;
2765 if (LocaleNCompare(option,"lsb",3) == 0)
2766 endian=FILLORDER_LSB2MSB;
2768 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2769 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
2770 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2771 if (image->matte != MagickFalse)
2779 TIFF has a matte channel.
2782 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2783 option=GetImageProperty(image,"tiff:alpha");
2784 if ((option != (const char *) NULL) &&
2785 (LocaleCompare(option,"associated") == 0))
2786 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2787 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2788 &samples_per_pixel);
2789 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2790 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2792 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2793 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2795 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2796 switch (quantum_info->format)
2798 case FloatingPointQuantumFormat:
2800 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2801 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2802 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2805 case SignedQuantumFormat:
2807 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2810 case UnsignedQuantumFormat:
2812 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2818 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2819 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2820 if (photometric == PHOTOMETRIC_RGB)
2821 if ((image_info->interlace == PlaneInterlace) ||
2822 (image_info->interlace == PartitionInterlace))
2823 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2825 if (TIFFScanlineSize(tiff) != 0)
2826 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2828 option=GetImageOption(image_info,"tiff:rows-per-strip");
2829 if (option != (const char *) NULL)
2830 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
2831 switch (compress_tag)
2833 case COMPRESSION_JPEG:
2835 #if defined(JPEG_SUPPORT)
2845 rows_per_strip+=(16-(rows_per_strip % 16));
2846 if (image->quality != 0)
2847 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2848 if (image_info->quality != UndefinedCompressionQuality)
2849 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2850 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2851 if (image->colorspace == RGBColorspace)
2856 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2857 sampling_factor=(const char *) NULL;
2858 value=GetImageProperty(image,"jpeg:sampling-factor");
2859 if (value != (char *) NULL)
2861 sampling_factor=value;
2862 if (image->debug != MagickFalse)
2863 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2864 " Input sampling-factors=%s",sampling_factor);
2866 if (image_info->sampling_factor != (char *) NULL)
2867 sampling_factor=image_info->sampling_factor;
2868 if (sampling_factor != (const char *) NULL)
2870 flags=ParseGeometry(sampling_factor,&geometry_info);
2871 if ((flags & SigmaValue) == 0)
2872 geometry_info.sigma=geometry_info.rho;
2873 if (image->colorspace == YCbCrColorspace)
2874 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2875 geometry_info.rho,(uint16) geometry_info.sigma);
2878 if (bits_per_sample == 12)
2879 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2883 case COMPRESSION_ADOBE_DEFLATE:
2885 rows_per_strip=(uint32) image->rows;
2886 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2888 if (((photometric == PHOTOMETRIC_RGB) ||
2889 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2890 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2891 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2892 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2893 image_info->quality == UndefinedCompressionQuality ? 7 :
2894 MagickMin((ssize_t) image_info->quality/10,9)));
2897 case COMPRESSION_CCITTFAX3:
2902 rows_per_strip=(uint32) image->rows;
2903 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2906 case COMPRESSION_CCITTFAX4:
2908 rows_per_strip=(uint32) image->rows;
2911 #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2912 case COMPRESSION_LZMA:
2914 if (((photometric == PHOTOMETRIC_RGB) ||
2915 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2916 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2917 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2918 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
2919 image_info->quality == UndefinedCompressionQuality ? 7 :
2920 MagickMin((ssize_t) image_info->quality/10,9)));
2924 case COMPRESSION_LZW:
2926 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2928 if (((photometric == PHOTOMETRIC_RGB) ||
2929 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2930 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2931 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2937 option=GetImageOption(image_info,"tiff:tile-geometry");
2938 if (option == (const char *) NULL)
2939 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
2940 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2946 Set image resolution.
2949 if (image->units == PixelsPerInchResolution)
2951 if (image->units == PixelsPerCentimeterResolution)
2952 units=RESUNIT_CENTIMETER;
2953 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2954 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2955 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2956 if ((image->page.x != 0) || (image->page.y != 0))
2961 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2962 image->x_resolution);
2963 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2964 image->y_resolution);
2967 if (image->chromaticity.white_point.x != 0.0)
2973 Set image chromaticity.
2975 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2976 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2977 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2978 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2979 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2980 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2981 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2982 chromaticity[0]=(float) image->chromaticity.white_point.x;
2983 chromaticity[1]=(float) image->chromaticity.white_point.y;
2984 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2986 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2988 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2989 if (image->scene != 0)
2990 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
2991 GetImageListLength(image));
2993 if (image->orientation != UndefinedOrientation)
2994 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
2995 (void) TIFFSetProfiles(tiff,image);
3001 page=(uint16) scene;
3002 pages=(uint16) GetImageListLength(image);
3003 if ((image_info->adjoin != MagickFalse) && (pages > 1))
3004 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
3005 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
3007 (void) TIFFSetProperties(tiff,image);
3009 (void) TIFFSetEXIFProperties(tiff,image);
3011 Write image scanlines.
3013 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3014 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3015 pixels=GetQuantumPixels(quantum_info);
3016 tiff_info.scanline=GetQuantumPixels(quantum_info);
3017 switch (photometric)
3019 case PHOTOMETRIC_CIELAB:
3020 case PHOTOMETRIC_YCBCR:
3021 case PHOTOMETRIC_RGB:
3026 switch (image_info->interlace)
3031 quantum_type=RGBQuantum;
3032 if (image->matte != MagickFalse)
3033 quantum_type=RGBAQuantum;
3034 for (y=0; y < (ssize_t) image->rows; y++)
3036 register const PixelPacket
3039 p=GetVirtualPixels(image,0,y,image->columns,1,
3041 if (p == (const PixelPacket *) NULL)
3043 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3044 quantum_info,quantum_type,pixels,&image->exception);
3046 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3048 if (image->previous == (Image *) NULL)
3050 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3052 if (status == MagickFalse)
3058 case PlaneInterlace:
3059 case PartitionInterlace:
3062 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3064 for (y=0; y < (ssize_t) image->rows; y++)
3066 register const PixelPacket
3069 p=GetVirtualPixels(image,0,y,image->columns,1,
3071 if (p == (const PixelPacket *) NULL)
3073 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3074 quantum_info,RedQuantum,pixels,&image->exception);
3075 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3078 if (image->previous == (Image *) NULL)
3080 status=SetImageProgress(image,SaveImageTag,100,400);
3081 if (status == MagickFalse)
3084 for (y=0; y < (ssize_t) image->rows; y++)
3086 register const PixelPacket
3089 p=GetVirtualPixels(image,0,y,image->columns,1,
3091 if (p == (const PixelPacket *) NULL)
3093 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3094 quantum_info,GreenQuantum,pixels,&image->exception);
3095 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3098 if (image->previous == (Image *) NULL)
3100 status=SetImageProgress(image,SaveImageTag,200,400);
3101 if (status == MagickFalse)
3104 for (y=0; y < (ssize_t) image->rows; y++)
3106 register const PixelPacket
3109 p=GetVirtualPixels(image,0,y,image->columns,1,
3111 if (p == (const PixelPacket *) NULL)
3113 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3114 quantum_info,BlueQuantum,pixels,&image->exception);
3115 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3118 if (image->previous == (Image *) NULL)
3120 status=SetImageProgress(image,SaveImageTag,300,400);
3121 if (status == MagickFalse)
3124 if (image->matte != MagickFalse)
3125 for (y=0; y < (ssize_t) image->rows; y++)
3127 register const PixelPacket
3130 p=GetVirtualPixels(image,0,y,image->columns,1,
3132 if (p == (const PixelPacket *) NULL)
3134 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3135 quantum_info,AlphaQuantum,pixels,&image->exception);
3136 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3139 if (image->previous == (Image *) NULL)
3141 status=SetImageProgress(image,SaveImageTag,400,400);
3142 if (status == MagickFalse)
3150 case PHOTOMETRIC_SEPARATED:
3155 quantum_type=CMYKQuantum;
3156 if (image->matte != MagickFalse)
3157 quantum_type=CMYKAQuantum;
3158 if (image->colorspace != CMYKColorspace)
3159 (void) TransformImageColorspace(image,CMYKColorspace);
3160 for (y=0; y < (ssize_t) image->rows; y++)
3162 register const PixelPacket
3165 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3166 if (p == (const PixelPacket *) NULL)
3168 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3169 quantum_info,quantum_type,pixels,&image->exception);
3170 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3172 if (image->previous == (Image *) NULL)
3174 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3176 if (status == MagickFalse)
3182 case PHOTOMETRIC_PALETTE:
3190 Colormapped TIFF image.
3192 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3193 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3194 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3195 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3196 (blue == (uint16 *) NULL))
3197 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3199 Initialize TIFF colormap.
3201 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3202 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3203 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3204 for (i=0; i < (ssize_t) image->colors; i++)
3206 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3207 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3208 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3210 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3211 red=(uint16 *) RelinquishMagickMemory(red);
3212 green=(uint16 *) RelinquishMagickMemory(green);
3213 blue=(uint16 *) RelinquishMagickMemory(blue);
3218 Convert PseudoClass packets to contiguous grayscale scanlines.
3220 quantum_type=IndexQuantum;
3221 if (image->matte != MagickFalse)
3223 if (photometric != PHOTOMETRIC_PALETTE)
3224 quantum_type=GrayAlphaQuantum;
3226 quantum_type=IndexAlphaQuantum;
3229 if (photometric != PHOTOMETRIC_PALETTE)
3230 quantum_type=GrayQuantum;
3231 for (y=0; y < (ssize_t) image->rows; y++)
3233 register const PixelPacket
3236 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3237 if (p == (const PixelPacket *) NULL)
3239 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3240 quantum_info,quantum_type,pixels,&image->exception);
3241 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3243 if (image->previous == (Image *) NULL)
3245 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3247 if (status == MagickFalse)
3254 quantum_info=DestroyQuantumInfo(quantum_info);
3255 DestroyTIFFInfo(&tiff_info);
3256 if (0 && (image_info->verbose == MagickTrue))
3257 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3258 (void) TIFFWriteDirectory(tiff);
3259 image->endian=MSBEndian;
3260 if (endian == FILLORDER_LSB2MSB)
3261 image->endian=LSBEndian;
3262 image=SyncNextImageInList(image);
3263 if (image == (Image *) NULL)
3265 status=SetImageProgress(image,SaveImagesTag,scene++,
3266 GetImageListLength(image));
3267 if (status == MagickFalse)
3269 } while (image_info->adjoin != MagickFalse);
3270 (void) TIFFSetWarningHandler(warning_handler);
3271 (void) TIFFSetErrorHandler(error_handler);