2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % TTTTT IIIII FFFFF FFFFF %
13 % Read/Write TIFF Image Format %
20 % Copyright 1999-2010 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
93 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
94 typedef struct _ExifInfo
104 static const ExifInfo
106 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
107 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
108 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
109 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
110 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
111 { EXIFTAG_OECF, TIFF_UNDEFINED, "exif:OptoelectricConversionFactor" },
112 { EXIFTAG_EXIFVERSION, TIFF_UNDEFINED, "exif:ExifVersion" },
113 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
114 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
115 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_UNDEFINED, "exif:ComponentsConfiguration" },
116 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
117 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
118 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
119 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
120 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
121 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
122 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
123 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
124 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
125 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
126 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
127 /* { EXIFTAG_SUBJECTAREA, TIFF_SHORT, "exif:SubjectArea" }, */
128 { EXIFTAG_MAKERNOTE, TIFF_UNDEFINED, "exif:MakerNote" },
129 { EXIFTAG_USERCOMMENT, TIFF_UNDEFINED, "exif:UserComment" },
130 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
131 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
132 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
133 { EXIFTAG_FLASHPIXVERSION, TIFF_UNDEFINED, "exif:FlashpixVersion" },
134 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
135 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
136 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
137 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
138 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
139 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
140 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_UNDEFINED, "exif:SpatialFrequencyResponse" },
141 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
142 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
143 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
144 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
145 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
146 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
147 { EXIFTAG_FILESOURCE, TIFF_UNDEFINED, "exif:FileSource" },
148 { EXIFTAG_SCENETYPE, TIFF_UNDEFINED, "exif:SceneType" },
149 { EXIFTAG_CFAPATTERN, TIFF_UNDEFINED, "exif:CFAPattern" },
150 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
151 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
152 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
153 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
154 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
155 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
156 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
157 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
158 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
159 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
160 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_UNDEFINED, "exif:DeviceSettingDescription" },
161 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
162 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
163 { 0, 0, (char *) NULL }
170 static MagickThreadKey
174 *tiff_semaphore = (SemaphoreInfo *) NULL;
176 static volatile MagickBooleanType
177 instantiate_key = MagickFalse;
181 Forward declarations.
183 #if defined(MAGICKCORE_TIFF_DELEGATE)
185 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
187 static MagickBooleanType
188 WriteGROUP4Image(const ImageInfo *,Image *),
189 WritePTIFImage(const ImageInfo *,Image *),
190 WriteTIFFImage(const ImageInfo *,Image *);
194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204 % IsTIFF() returns MagickTrue if the image format type, identified by the
205 % magick string, is TIFF.
207 % The format of the IsTIFF method is:
209 % MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
211 % A description of each parameter follows:
213 % o magick: compare image format pattern against these bytes.
215 % o length: Specifies the length of the magick string.
218 static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
222 if (memcmp(magick,"\115\115\000\052",4) == 0)
224 if (memcmp(magick,"\111\111\052\000",4) == 0)
226 #if defined(TIFF_VERSION_BIG)
229 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
231 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
237 #if defined(MAGICKCORE_TIFF_DELEGATE)
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 % R e a d G R O U P 4 I m a g e %
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 % ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
250 % allocates the memory necessary for the new Image structure and returns a
251 % pointer to the new image.
253 % The format of the ReadGROUP4Image method is:
255 % Image *ReadGROUP4Image(const ImageInfo *image_info,
256 % ExceptionInfo *exception)
258 % A description of each parameter follows:
260 % o image_info: the image info.
262 % o exception: return any errors or warnings in this structure.
266 static inline size_t WriteLSBLong(FILE *file,const unsigned int value)
271 buffer[0]=(unsigned char) value;
272 buffer[1]=(unsigned char) (value >> 8);
273 buffer[2]=(unsigned char) (value >> 16);
274 buffer[3]=(unsigned char) (value >> 24);
275 return(fwrite(buffer,1,4,file));
278 static Image *ReadGROUP4Image(const ImageInfo *image_info,
279 ExceptionInfo *exception)
282 filename[MaxTextExtent];
310 assert(image_info != (const ImageInfo *) NULL);
311 assert(image_info->signature == MagickSignature);
312 if (image_info->debug != MagickFalse)
313 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
314 image_info->filename);
315 assert(exception != (ExceptionInfo *) NULL);
316 assert(exception->signature == MagickSignature);
317 image=AcquireImage(image_info);
318 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
319 if (status == MagickFalse)
321 image=DestroyImageList(image);
322 return((Image *) NULL);
325 Write raw CCITT Group 4 wrapped as a TIFF image file.
328 unique_file=AcquireUniqueFileResource(filename);
329 if (unique_file != -1)
330 file=fdopen(unique_file,"wb");
331 if ((unique_file == -1) || (file == (FILE *) NULL))
332 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
333 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
334 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
335 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
336 length=WriteLSBLong(file,image->columns);
337 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
338 length=WriteLSBLong(file,image->rows);
339 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
340 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
341 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
342 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
343 strip_offset=10+(12*14)+4+8;
344 length=WriteLSBLong(file,(unsigned long) strip_offset);
345 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
346 length=WriteLSBLong(file,(unsigned long) image_info->orientation);
347 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
348 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
349 length=WriteLSBLong(file,image->rows);
350 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
351 offset=(ssize_t) ftell(file)-4;
352 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
353 length=WriteLSBLong(file,(unsigned long) (strip_offset-8));
354 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
355 length=WriteLSBLong(file,(unsigned long) (strip_offset-8));
356 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
357 length=fwrite("\000\000\000\000",1,4,file);
358 length=WriteLSBLong(file,image->x_resolution);
359 length=WriteLSBLong(file,1);
360 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
361 (void) fputc(c,file);
362 offset=(ssize_t) fseek(file,(long) offset,SEEK_SET);
363 length=WriteLSBLong(file,(unsigned int) length);
365 (void) CloseBlob(image);
366 image=DestroyImage(image);
370 read_info=CloneImageInfo((ImageInfo *) NULL);
371 (void) FormatMagickString(read_info->filename,MaxTextExtent,"%.1024s",
373 image=ReadTIFFImage(read_info,exception);
374 read_info=DestroyImageInfo(read_info);
375 if (image != (Image *) NULL)
377 (void) CopyMagickString(image->filename,image_info->filename,
379 (void) CopyMagickString(image->magick_filename,image_info->filename,
381 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
383 (void) RelinquishUniqueFileResource(filename);
388 #if defined(MAGICKCORE_TIFF_DELEGATE)
390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 % R e a d T I F F I m a g e %
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 % ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
401 % memory necessary for the new Image structure and returns a pointer to the
404 % The format of the ReadTIFFImage method is:
406 % Image *ReadTIFFImage(const ImageInfo *image_info,
407 % ExceptionInfo *exception)
409 % A description of each parameter follows:
411 % o image_info: the image info.
413 % o exception: return any errors or warnings in this structure.
417 static inline size_t MagickMax(const size_t x,const size_t y)
424 static inline long MagickMin(const long x,const long y)
431 static MagickBooleanType ReadProfile(Image *image,const char *name,
432 unsigned char *datum,long length)
446 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
448 for (i=0; i < (length-4); i+=2)
449 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
458 profile=AcquireStringInfo((size_t) length);
459 SetStringInfoDatum(profile,datum+i);
460 status=SetImageProfile(image,name,profile);
461 profile=DestroyStringInfo(profile);
462 if (status == MagickFalse)
463 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
468 #if defined(__cplusplus) || defined(c_plusplus)
472 static int TIFFCloseBlob(thandle_t image)
474 (void) CloseBlob((Image *) image);
478 static void TIFFErrors(const char *module,const char *format,va_list error)
481 message[MaxTextExtent];
486 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
487 (void) vsnprintf(message,MaxTextExtent,format,error);
489 (void) vsprintf(message,format,error);
491 (void) ConcatenateMagickString(message,".",MaxTextExtent);
492 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
493 if (exception != (ExceptionInfo *) NULL)
494 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
495 message,"`%s'",module);
498 static void TIFFGetProfiles(TIFF *tiff,Image *image)
506 #if defined(TIFFTAG_ICCPROFILE)
508 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
509 (void) ReadProfile(image,"icc",profile,(long) length);
511 #if defined(TIFFTAG_PHOTOSHOP)
513 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
514 (void) ReadProfile(image,"8bim",profile,(long) length);
516 #if defined(TIFFTAG_RICHTIFFIPTC)
518 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
520 if (TIFFIsByteSwapped(tiff) != 0)
521 TIFFSwabArrayOfLong((uint32 *) profile,(unsigned long) length);
522 (void) ReadProfile(image,"iptc",profile,4L*length);
525 #if defined(TIFFTAG_XMLPACKET)
527 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
528 (void) ReadProfile(image,"xmp",profile,(long) length);
531 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
532 (void) ReadProfile(image,"tiff:37724",profile,(long) length);
535 static void TIFFGetProperties(TIFF *tiff,Image *image)
540 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
541 (void) SetImageProperty(image,"tiff:artist",text);
542 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
543 (void) SetImageProperty(image,"tiff:timestamp",text);
544 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
545 (void) SetImageProperty(image,"tiff:software",text);
546 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
547 (void) SetImageProperty(image,"tiff:hostcomputer",text);
548 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
549 (void) SetImageProperty(image,"tiff:document",text);
550 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
551 (void) SetImageProperty(image,"tiff:make",text);
552 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
553 (void) SetImageProperty(image,"tiff:model",text);
554 if (TIFFGetField(tiff,33432,&text) == 1)
555 (void) SetImageProperty(image,"tiff:copyright",text);
556 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
557 (void) SetImageProperty(image,"label",text);
558 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
559 (void) SetImageProperty(image,"comment",text);
562 static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
564 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
566 value[MaxTextExtent];
578 Read EXIF properties.
580 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
582 directory=TIFFCurrentDirectory(tiff);
583 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
585 for (i=0; exif_info[i].tag != 0; i++)
588 switch (exif_info[i].type)
595 if (TIFFGetField(tiff,exif_info[i].tag,&ascii) != 0)
596 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
604 if (TIFFGetField(tiff,exif_info[i].tag,&shorty) != 0)
605 (void) FormatMagickString(value,MaxTextExtent,"%d",shorty);
613 if (TIFFGetField(tiff,exif_info[i].tag,&longy) != 0)
614 (void) FormatMagickString(value,MaxTextExtent,"%d",longy);
623 if (TIFFGetField(tiff,exif_info[i].tag,&rational) != 0)
624 (void) FormatMagickString(value,MaxTextExtent,"%g",rational);
631 (void) SetImageProperty(image,exif_info[i].property,value);
633 TIFFSetDirectory(tiff,directory);
640 static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
642 *base=(tdata_t *) GetBlobStreamData((Image *) image);
643 if (*base != (tdata_t *) NULL)
644 *size=(toff_t) GetBlobSize((Image *) image);
645 if (*base != (tdata_t *) NULL)
650 static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
655 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
656 (unsigned char *) data);
660 static int32 TIFFReadPixels(TIFF *tiff,unsigned long bits_per_sample,
661 tsample_t sample,long row,tdata_t scanline)
666 (void) bits_per_sample;
667 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
671 static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
673 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
676 static toff_t TIFFGetBlobSize(thandle_t image)
678 return((toff_t) GetBlobSize((Image *) image));
681 static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
688 static void TIFFWarnings(const char *module,const char *format,va_list warning)
691 message[MaxTextExtent];
696 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
697 (void) vsnprintf(message,MaxTextExtent,format,warning);
699 (void) vsprintf(message,format,warning);
701 (void) ConcatenateMagickString(message,".",MaxTextExtent);
702 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
703 if (exception != (ExceptionInfo *) NULL)
704 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
705 message,"`%s'",module);
708 static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
713 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
714 (unsigned char *) data);
718 #if defined(__cplusplus) || defined(c_plusplus)
722 static Image *ReadTIFFImage(const ImageInfo *image_info,
723 ExceptionInfo *exception)
727 ReadSingleSampleMethod,
813 assert(image_info != (const ImageInfo *) NULL);
814 assert(image_info->signature == MagickSignature);
815 if (image_info->debug != MagickFalse)
816 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
817 image_info->filename);
818 assert(exception != (ExceptionInfo *) NULL);
819 assert(exception->signature == MagickSignature);
820 image=AcquireImage(image_info);
821 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
822 if (status == MagickFalse)
824 image=DestroyImageList(image);
825 return((Image *) NULL);
827 (void) MagickSetThreadValue(tiff_exception,exception);
828 error_handler=TIFFSetErrorHandler(TIFFErrors);
829 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
830 tiff=TIFFClientOpen(image->filename,"r",(thandle_t) image,TIFFReadBlob,
831 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
833 if (tiff == (TIFF *) NULL)
835 (void) TIFFSetWarningHandler(warning_handler);
836 (void) TIFFSetErrorHandler(error_handler);
837 image=DestroyImageList(image);
838 return((Image *) NULL);
840 debug=IsEventLogging();
841 if (image_info->number_scenes != 0)
844 Generate blank images for subimage specification (e.g. image.tif[4].
846 for (i=0; i < (long) image_info->scene; i++)
848 (void) TIFFReadDirectory(tiff);
849 AcquireNextImage(image_info,image);
850 if (GetNextImageInList(image) == (Image *) NULL)
852 image=DestroyImageList(image);
853 return((Image *) NULL);
855 image=SyncNextImageInList(image);
860 if (0 && (image_info->verbose != MagickFalse))
861 TIFFPrintDirectory(tiff,stdout,MagickFalse);
862 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
863 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
864 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
865 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
866 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
867 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
872 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
875 case PHOTOMETRIC_MINISBLACK:
877 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
880 case PHOTOMETRIC_MINISWHITE:
882 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
885 case PHOTOMETRIC_PALETTE:
887 (void) SetImageProperty(image,"tiff:photometric","palette");
890 case PHOTOMETRIC_RGB:
892 (void) SetImageProperty(image,"tiff:photometric","RGB");
895 case PHOTOMETRIC_CIELAB:
897 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
900 case PHOTOMETRIC_SEPARATED:
902 (void) SetImageProperty(image,"tiff:photometric","separated");
907 (void) SetImageProperty(image,"tiff:photometric","unknown");
911 if (image->debug != MagickFalse)
913 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
914 (unsigned int) width,(unsigned int) height);
915 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
917 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
918 "Bits per sample: %u",bits_per_sample);
919 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
920 "Min sample value: %u",min_sample_value);
921 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
922 "Max sample value: %u",max_sample_value);
923 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
924 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
926 image->columns=(unsigned long) width;
927 image->rows=(unsigned long) height;
928 image->depth=(unsigned long) bits_per_sample;
929 if (image->debug != MagickFalse)
930 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %lu",
933 image->endian=MSBEndian;
934 if ((int) (*(char *) &lsb_first) != 0)
935 image->endian=LSBEndian;
936 if (photometric == PHOTOMETRIC_SEPARATED)
937 image->colorspace=CMYKColorspace;
938 if (photometric == PHOTOMETRIC_CIELAB)
939 image->colorspace=LabColorspace;
940 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
942 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
943 x_resolution=(float) image->x_resolution;
944 y_resolution=(float) image->y_resolution;
945 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
946 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
947 image->x_resolution=x_resolution;
948 image->y_resolution=y_resolution;
949 x_position=(float) image->page.x/x_resolution;
950 y_position=(float) image->page.y/y_resolution;
951 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
952 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
953 image->page.x=(long) ceil(x_position*x_resolution-0.5);
954 image->page.y=(long) ceil(y_position*y_resolution-0.5);
955 image->orientation=(OrientationType) orientation;
956 chromaticity=(float *) NULL;
957 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
958 if (chromaticity != (float *) NULL)
960 image->chromaticity.white_point.x=chromaticity[0];
961 image->chromaticity.white_point.y=chromaticity[1];
963 chromaticity=(float *) NULL;
964 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
965 if (chromaticity != (float *) NULL)
967 image->chromaticity.red_primary.x=chromaticity[0];
968 image->chromaticity.red_primary.y=chromaticity[1];
969 image->chromaticity.green_primary.x=chromaticity[2];
970 image->chromaticity.green_primary.y=chromaticity[3];
971 image->chromaticity.blue_primary.x=chromaticity[4];
972 image->chromaticity.blue_primary.y=chromaticity[5];
974 TIFFGetProperties(tiff,image);
975 option=GetImageOption(image_info,"tiff:exif");
976 if ((option != (const char *) NULL) &&
977 (IsMagickTrue(option) != MagickFalse))
978 TIFFGetEXIFProperties(tiff,image);
979 TIFFGetProfiles(tiff,image);
981 Allocate memory for the image and pixel buffer.
983 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
984 if ((compress_tag != COMPRESSION_NONE) &&
985 (TIFFIsCODECConfigured(compress_tag) == 0))
988 ThrowReaderException(CoderError,"CompressNotSupported");
991 switch (compress_tag)
993 case COMPRESSION_NONE: image->compression=NoCompression; break;
994 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
995 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
996 case COMPRESSION_JPEG:
998 image->compression=JPEGCompression;
999 #if defined(JPEG_SUPPORT)
1002 sampling_factor[MaxTextExtent];
1008 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1009 &horizontal,&vertical);
1010 (void) FormatMagickString(sampling_factor,MaxTextExtent,"%dx%d",
1011 horizontal,vertical);
1012 (void) SetImageProperty(image,"jpeg:sampling-factor",
1014 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1015 "Sampling Factors: %s",sampling_factor);
1016 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1018 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1020 photometric=PHOTOMETRIC_RGB;
1026 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
1027 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1028 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1029 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1030 default: image->compression=RLECompression; break;
1032 quantum_info=AcquireQuantumInfo(image_info,image);
1033 if (quantum_info == (QuantumInfo *) NULL)
1036 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1038 if (sample_format == SAMPLEFORMAT_UINT)
1039 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1040 if (sample_format == SAMPLEFORMAT_INT)
1041 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1042 if (sample_format == SAMPLEFORMAT_IEEEFP)
1043 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1044 if (status == MagickFalse)
1047 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1050 switch (photometric)
1052 case PHOTOMETRIC_MINISBLACK:
1054 quantum_info->min_is_white=MagickFalse;
1057 case PHOTOMETRIC_MINISWHITE:
1059 quantum_info->min_is_white=MagickTrue;
1065 associated_alpha=MagickFalse;
1067 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1069 if (extra_samples == 0)
1071 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1072 image->matte=MagickTrue;
1075 for (i=0; i < extra_samples; i++)
1077 image->matte=MagickTrue;
1078 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
1079 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
1081 option=GetImageOption(image_info,"tiff:alpha");
1082 if (option != (const char *) NULL)
1083 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1085 if (image->matte != MagickFalse)
1086 (void) SetImageProperty(image,"tiff:alpha",
1087 associated_alpha != MagickFalse ? "associated" : "unassociated");
1088 if ((photometric == PHOTOMETRIC_PALETTE) &&
1089 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1094 colors=(unsigned long) GetQuantumRange(bits_per_sample)+1;
1095 if (AcquireImageColormap(image,colors) == MagickFalse)
1098 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1101 if (units == RESUNIT_INCH)
1102 image->units=PixelsPerInchResolution;
1103 if (units == RESUNIT_CENTIMETER)
1104 image->units=PixelsPerCentimeterResolution;
1105 value=(unsigned short) image->scene;
1106 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1108 if (image_info->ping != MagickFalse)
1110 if (image_info->number_scenes != 0)
1111 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1113 goto next_tiff_frame;
1115 method=ReadGenericMethod;
1116 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1119 value[MaxTextExtent];
1121 method=ReadStripMethod;
1122 (void) FormatMagickString(value,MaxTextExtent,"%u",
1123 (unsigned int) rows_per_strip);
1124 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1126 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1127 method=ReadRGBAMethod;
1128 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1129 method=ReadCMYKAMethod;
1130 if ((photometric != PHOTOMETRIC_RGB) &&
1131 (photometric != PHOTOMETRIC_CIELAB) &&
1132 (photometric != PHOTOMETRIC_SEPARATED))
1133 method=ReadGenericMethod;
1134 if (image->storage_class == PseudoClass)
1135 method=ReadSingleSampleMethod;
1136 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1137 (photometric == PHOTOMETRIC_MINISWHITE))
1138 method=ReadSingleSampleMethod;
1139 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1140 (interlace == PLANARCONFIG_SEPARATE))
1141 method=ReadGenericMethod;
1142 if (TIFFIsTiled(tiff) != MagickFalse)
1143 method=ReadTileMethod;
1144 quantum_type=RGBQuantum;
1145 pixels=GetQuantumPixels(quantum_info);
1148 case ReadSingleSampleMethod:
1151 Convert TIFF image to PseudoClass MIFF image.
1153 if ((image->storage_class == PseudoClass) &&
1154 (photometric == PHOTOMETRIC_PALETTE))
1165 Initialize colormap.
1167 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1168 &green_colormap,&blue_colormap);
1169 range=255; /* might be old style 8-bit colormap */
1170 for (i=0; i < (long) image->colors; i++)
1171 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1172 (blue_colormap[i] >= 256))
1177 for (i=0; i < (long) image->colors; i++)
1179 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
1180 red_colormap[i])/range);
1181 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
1182 green_colormap[i])/range);
1183 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
1184 blue_colormap[i])/range);
1187 quantum_type=IndexQuantum;
1188 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1189 if (image->matte != MagickFalse)
1191 if (image->storage_class != PseudoClass)
1193 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1195 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1199 quantum_type=IndexAlphaQuantum;
1200 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1204 if (image->storage_class != PseudoClass)
1206 quantum_type=GrayQuantum;
1207 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1209 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1210 if (status == MagickFalse)
1213 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1215 pixels=GetQuantumPixels(quantum_info);
1216 for (y=0; y < (long) image->rows; y++)
1221 register PixelPacket
1224 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1227 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1228 if (q == (PixelPacket *) NULL)
1230 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1231 quantum_type,pixels,exception);
1232 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1234 if (image->previous == (Image *) NULL)
1236 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1237 if (status == MagickFalse)
1243 case ReadRGBAMethod:
1246 Convert TIFF image to DirectClass MIFF image.
1248 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1249 quantum_type=RGBQuantum;
1250 if (image->matte != MagickFalse)
1252 quantum_type=RGBAQuantum;
1253 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1255 if (image->colorspace == CMYKColorspace)
1257 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1258 quantum_type=CMYKQuantum;
1259 if (image->matte != MagickFalse)
1261 quantum_type=CMYKAQuantum;
1262 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1265 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1266 if (status == MagickFalse)
1269 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1271 pixels=GetQuantumPixels(quantum_info);
1272 for (y=0; y < (long) image->rows; y++)
1277 register PixelPacket
1280 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1283 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1284 if (q == (PixelPacket *) NULL)
1286 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1287 quantum_type,pixels,exception);
1288 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1290 if (image->previous == (Image *) NULL)
1292 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1293 if (status == MagickFalse)
1299 case ReadCMYKAMethod:
1302 Convert TIFF image to DirectClass MIFF image.
1304 for (i=0; i < (long) samples_per_pixel; i++)
1306 for (y=0; y < (long) image->rows; y++)
1308 register PixelPacket
1314 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1318 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1319 if (q == (PixelPacket *) NULL)
1321 if (image->colorspace != CMYKColorspace)
1324 case 0: quantum_type=RedQuantum; break;
1325 case 1: quantum_type=GreenQuantum; break;
1326 case 2: quantum_type=BlueQuantum; break;
1327 case 3: quantum_type=AlphaQuantum; break;
1328 default: quantum_type=UndefinedQuantum; break;
1333 case 0: quantum_type=CyanQuantum; break;
1334 case 1: quantum_type=MagentaQuantum; break;
1335 case 2: quantum_type=YellowQuantum; break;
1336 case 3: quantum_type=BlackQuantum; break;
1337 case 4: quantum_type=AlphaQuantum; break;
1338 default: quantum_type=UndefinedQuantum; break;
1340 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1341 quantum_type,pixels,exception);
1342 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1345 if (image->previous == (Image *) NULL)
1347 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1348 if (status == MagickFalse)
1354 case ReadStripMethod:
1356 register unsigned char
1360 Convert stripped TIFF image to DirectClass MIFF image.
1363 p=(unsigned char *) NULL;
1364 for (y=0; y < (long) image->rows; y++)
1369 register PixelPacket
1372 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1373 if (q == (PixelPacket *) NULL)
1377 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1379 i=(long) MagickMin((long) rows_per_strip,(long) image->rows-y);
1382 p=pixels+image->columns*i;
1383 for (x=0; x < (long) image->columns; x++)
1385 q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
1386 q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
1387 q->blue=ScaleCharToQuantum((unsigned char) (TIFFGetB(*p)));
1388 if (image->matte != MagickFalse)
1389 q->opacity=ScaleCharToQuantum((unsigned char) (TIFFGetA(*p)));
1393 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1395 if (image->previous == (Image *) NULL)
1397 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1398 if (status == MagickFalse)
1404 case ReadTileMethod:
1418 Convert tiled TIFF image to DirectClass MIFF image.
1420 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1421 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1424 ThrowReaderException(CoderError,"ImageIsNotTiled");
1426 (void) SetImageStorageClass(image,DirectClass);
1427 number_pixels=columns*rows;
1428 tile_pixels=(uint32 *) AcquireQuantumMemory((size_t) columns*rows,
1429 sizeof(*tile_pixels));
1430 if (tile_pixels == (uint32 *) NULL)
1433 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1435 for (y=0; y < (long) image->rows; y+=rows)
1443 register PixelPacket
1450 rows_remaining=image->rows-y;
1451 if ((long) (y+rows) < (long) image->rows)
1452 rows_remaining=rows;
1453 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1455 if (tile == (PixelPacket *) NULL)
1457 for (x=0; x < (long) image->columns; x+=columns)
1463 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1465 columns_remaining=image->columns-x;
1466 if ((long) (x+columns) < (long) image->columns)
1467 columns_remaining=columns;
1468 p=tile_pixels+(rows-rows_remaining)*columns;
1469 q=tile+(image->columns*(rows_remaining-1)+x);
1470 for (row=rows_remaining; row > 0; row--)
1472 if (image->matte != MagickFalse)
1473 for (column=columns_remaining; column > 0; column--)
1475 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1476 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1477 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1478 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1479 (unsigned char) TIFFGetA(*p)));
1484 for (column=columns_remaining; column > 0; column--)
1486 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1487 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1488 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1492 p+=columns-columns_remaining;
1493 q-=(image->columns+columns_remaining);
1496 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1498 if (image->previous == (Image *) NULL)
1500 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1501 if (status == MagickFalse)
1505 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1508 case ReadGenericMethod:
1518 Convert TIFF image to DirectClass MIFF image.
1520 number_pixels=(MagickSizeType) image->columns*image->rows;
1521 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1522 (number_pixels*sizeof(uint32))))
1525 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1527 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1529 if (pixels == (uint32 *) NULL)
1532 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1534 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1535 (uint32) image->rows,(uint32 *) pixels,0);
1537 Convert image to DirectClass pixel packets.
1539 p=pixels+number_pixels-1;
1540 for (y=0; y < (long) image->rows; y++)
1545 register PixelPacket
1548 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1549 if (q == (PixelPacket *) NULL)
1551 q+=image->columns-1;
1552 for (x=0; x < (long) image->columns; x++)
1554 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1555 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1556 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1557 if (image->matte != MagickFalse)
1558 q->opacity=ScaleCharToQuantum((unsigned char) TIFFGetA(*p));
1562 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1564 if (image->previous == (Image *) NULL)
1566 status=SetImageProgress(image,LoadImageTag,y,image->rows);
1567 if (status == MagickFalse)
1571 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1575 SetQuantumImageType(image,quantum_type);
1577 if ((photometric == PHOTOMETRIC_LOGL) ||
1578 (photometric == PHOTOMETRIC_MINISBLACK) ||
1579 (photometric == PHOTOMETRIC_MINISWHITE))
1581 image->type=GrayscaleType;
1582 if (bits_per_sample == 1)
1583 image->type=BilevelType;
1585 if (image->storage_class == PseudoClass)
1586 image->depth=GetImageDepth(image,exception);
1587 image->endian=MSBEndian;
1588 if (endian == FILLORDER_LSB2MSB)
1589 image->endian=LSBEndian;
1590 if ((photometric == PHOTOMETRIC_LOGL) ||
1591 (photometric == PHOTOMETRIC_MINISBLACK) ||
1592 (photometric == PHOTOMETRIC_MINISWHITE))
1594 image->type=GrayscaleType;
1595 if (bits_per_sample == 1)
1596 image->type=BilevelType;
1599 Proceed to next image.
1601 if (image_info->number_scenes != 0)
1602 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1604 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1605 if (status == MagickTrue)
1608 Allocate next image structure.
1610 AcquireNextImage(image_info,image);
1611 if (GetNextImageInList(image) == (Image *) NULL)
1613 image=DestroyImageList(image);
1614 return((Image *) NULL);
1616 image=SyncNextImageInList(image);
1617 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1619 if (status == MagickFalse)
1622 quantum_info=DestroyQuantumInfo(quantum_info);
1623 } while (status == MagickTrue);
1624 (void) TIFFSetWarningHandler(warning_handler);
1625 (void) TIFFSetErrorHandler(error_handler);
1627 return(GetFirstImageInList(image));
1632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1636 % R e g i s t e r T I F F I m a g e %
1640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1642 % RegisterTIFFImage() adds properties for the TIFF image format to
1643 % the list of supported formats. The properties include the image format
1644 % tag, a method to read and/or write the format, whether the format
1645 % supports the saving of more than one frame to the same file or blob,
1646 % whether the format supports native in-memory I/O, and a brief
1647 % description of the format.
1649 % The format of the RegisterTIFFImage method is:
1651 % unsigned long RegisterTIFFImage(void)
1654 ModuleExport unsigned long RegisterTIFFImage(void)
1656 #define TIFFDescription "Tagged Image File Format"
1659 version[MaxTextExtent];
1664 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1665 tiff_semaphore=AllocateSemaphoreInfo();
1666 LockSemaphoreInfo(tiff_semaphore);
1667 if (instantiate_key == MagickFalse)
1669 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1670 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1671 instantiate_key=MagickTrue;
1673 UnlockSemaphoreInfo(tiff_semaphore);
1675 #if defined(TIFF_VERSION)
1676 (void) FormatMagickString(version,MaxTextExtent,"%d",TIFF_VERSION);
1678 #if defined(MAGICKCORE_TIFF_DELEGATE)
1687 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1693 entry=SetMagickInfo("GROUP4");
1694 #if defined(MAGICKCORE_TIFF_DELEGATE)
1695 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1696 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1698 entry->raw=MagickTrue;
1699 entry->endian_support=MagickTrue;
1700 entry->adjoin=MagickFalse;
1701 entry->seekable_stream=MagickTrue;
1702 entry->thread_support=NoThreadSupport;
1703 entry->description=ConstantString("Raw CCITT Group4");
1704 entry->module=ConstantString("TIFF");
1705 (void) RegisterMagickInfo(entry);
1706 entry=SetMagickInfo("PTIF");
1707 #if defined(MAGICKCORE_TIFF_DELEGATE)
1708 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1709 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1711 entry->endian_support=MagickTrue;
1712 entry->seekable_stream=MagickTrue;
1713 entry->thread_support=NoThreadSupport;
1714 entry->description=ConstantString("Pyramid encoded TIFF");
1715 entry->module=ConstantString("TIFF");
1716 (void) RegisterMagickInfo(entry);
1717 entry=SetMagickInfo("TIF");
1718 #if defined(MAGICKCORE_TIFF_DELEGATE)
1719 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1720 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1722 entry->endian_support=MagickTrue;
1723 entry->seekable_stream=MagickTrue;
1724 entry->stealth=MagickTrue;
1725 entry->thread_support=NoThreadSupport;
1726 entry->description=ConstantString(TIFFDescription);
1727 if (*version != '\0')
1728 entry->version=ConstantString(version);
1729 entry->module=ConstantString("TIFF");
1730 (void) RegisterMagickInfo(entry);
1731 entry=SetMagickInfo("TIFF");
1732 #if defined(MAGICKCORE_TIFF_DELEGATE)
1733 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1734 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1736 entry->magick=(IsImageFormatHandler *) IsTIFF;
1737 entry->endian_support=MagickTrue;
1738 entry->seekable_stream=MagickTrue;
1739 entry->thread_support=NoThreadSupport;
1740 entry->description=ConstantString(TIFFDescription);
1741 if (*version != '\0')
1742 entry->version=ConstantString(version);
1743 entry->module=ConstantString("TIFF");
1744 (void) RegisterMagickInfo(entry);
1745 entry=SetMagickInfo("TIFF64");
1746 #if defined(TIFF_VERSION_BIG)
1747 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1748 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1750 entry->adjoin=MagickFalse;
1751 entry->endian_support=MagickTrue;
1752 entry->seekable_stream=MagickTrue;
1753 entry->thread_support=NoThreadSupport;
1754 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1755 if (*version != '\0')
1756 entry->version=ConstantString(version);
1757 entry->module=ConstantString("TIFF");
1758 (void) RegisterMagickInfo(entry);
1759 return(MagickImageCoderSignature);
1763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1767 % U n r e g i s t e r T I F F I m a g e %
1771 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1773 % UnregisterTIFFImage() removes format registrations made by the TIFF module
1774 % from the list of supported formats.
1776 % The format of the UnregisterTIFFImage method is:
1778 % UnregisterTIFFImage(void)
1781 ModuleExport void UnregisterTIFFImage(void)
1783 (void) UnregisterMagickInfo("RAWGROUP4");
1784 (void) UnregisterMagickInfo("PTIF");
1785 (void) UnregisterMagickInfo("TIF");
1786 (void) UnregisterMagickInfo("TIFF");
1787 (void) UnregisterMagickInfo("TIFF64");
1788 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1789 tiff_semaphore=AllocateSemaphoreInfo();
1790 LockSemaphoreInfo(tiff_semaphore);
1791 if (instantiate_key != MagickFalse)
1792 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1793 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1794 instantiate_key=MagickFalse;
1795 UnlockSemaphoreInfo(tiff_semaphore);
1796 DestroySemaphoreInfo(&tiff_semaphore);
1799 #if defined(MAGICKCORE_TIFF_DELEGATE)
1801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1805 % W r i t e G R O U P 4 I m a g e %
1809 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1811 % WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1813 % The format of the WriteGROUP4Image method is:
1815 % MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1818 % A description of each parameter follows:
1820 % o image_info: the image info.
1822 % o image: The image.
1825 static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1829 filename[MaxTextExtent];
1863 Write image as CCITT Group4 TIFF image to a temporary file.
1865 assert(image_info != (const ImageInfo *) NULL);
1866 assert(image_info->signature == MagickSignature);
1867 assert(image != (Image *) NULL);
1868 assert(image->signature == MagickSignature);
1869 if (image->debug != MagickFalse)
1870 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1871 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1872 if (status == MagickFalse)
1874 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1875 if (huffman_image == (Image *) NULL)
1877 (void) CloseBlob(image);
1878 return(MagickFalse);
1880 huffman_image->endian=MSBEndian;
1882 unique_file=AcquireUniqueFileResource(filename);
1883 if (unique_file != -1)
1884 file=fdopen(unique_file,"wb");
1885 if ((unique_file == -1) || (file == (FILE *) NULL))
1887 ThrowFileException(&image->exception,FileOpenError,
1888 "UnableToCreateTemporaryFile",filename);
1889 return(MagickFalse);
1891 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1893 (void) SetImageType(huffman_image,BilevelType);
1894 write_info=CloneImageInfo((ImageInfo *) NULL);
1895 SetImageInfoFile(write_info,file);
1896 write_info->compression=Group4Compression;
1897 write_info->type=BilevelType;
1898 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1899 status=WriteTIFFImage(write_info,huffman_image);
1900 (void) fflush(file);
1901 write_info=DestroyImageInfo(write_info);
1902 if (status == MagickFalse)
1904 InheritException(&image->exception,&huffman_image->exception);
1905 huffman_image=DestroyImage(huffman_image);
1906 (void) fclose(file);
1907 (void) RelinquishUniqueFileResource(filename);
1908 return(MagickFalse);
1910 tiff=TIFFOpen(filename,"rb");
1911 if (tiff == (TIFF *) NULL)
1913 huffman_image=DestroyImage(huffman_image);
1914 (void) fclose(file);
1915 (void) RelinquishUniqueFileResource(filename);
1916 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1917 image_info->filename);
1918 return(MagickFalse);
1921 Allocate raw strip buffer.
1923 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1926 huffman_image=DestroyImage(huffman_image);
1927 (void) fclose(file);
1928 (void) RelinquishUniqueFileResource(filename);
1929 return(MagickFalse);
1931 strip_size=byte_count[0];
1932 for (i=1; i < (long) TIFFNumberOfStrips(tiff); i++)
1933 if (byte_count[i] > strip_size)
1934 strip_size=byte_count[i];
1935 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1937 if (buffer == (unsigned char *) NULL)
1940 huffman_image=DestroyImage(huffman_image);
1941 (void) fclose(file);
1942 (void) RelinquishUniqueFileResource(filename);
1943 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1944 image_info->filename);
1947 Compress runlength encoded to 2D Huffman pixels.
1949 for (i=0; i < (long) TIFFNumberOfStrips(tiff); i++)
1951 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
1952 if (WriteBlob(image,(size_t) count,buffer) != count)
1955 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1957 huffman_image=DestroyImage(huffman_image);
1958 (void) fclose(file);
1959 (void) RelinquishUniqueFileResource(filename);
1960 (void) CloseBlob(image);
1965 #if defined(MAGICKCORE_TIFF_DELEGATE)
1967 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1971 % W r i t e P T I F I m a g e %
1975 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1977 % WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
1980 % The format of the WritePTIFImage method is:
1982 % MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
1985 % A description of each parameter follows:
1987 % o image_info: the image info.
1989 % o image: The image.
1992 static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2011 Create pyramid-encoded TIFF image.
2013 images=NewImageList();
2014 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2016 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2017 &image->exception));
2018 columns=next->columns;
2020 while ((columns > 64) && (rows > 64))
2024 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2026 AppendImageToList(&images,pyramid_image);
2030 Write pyramid-encoded TIFF image.
2032 write_info=CloneImageInfo(image_info);
2033 *write_info->magick='\0';
2034 write_info->adjoin=MagickTrue;
2035 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2036 images=DestroyImageList(images);
2037 write_info=DestroyImageInfo(write_info);
2042 #if defined(MAGICKCORE_TIFF_DELEGATE)
2044 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2047 % W r i t e T I F F I m a g e %
2051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2053 % WriteTIFFImage() writes an image in the Tagged image file format.
2055 % The format of the WriteTIFFImage method is:
2057 % MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2060 % A description of each parameter follows:
2062 % o image_info: the image info.
2064 % o image: The image.
2068 typedef struct _TIFFInfo
2079 static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2081 assert(tiff_info != (TIFFInfo *) NULL);
2082 if (tiff_info->scanlines != (unsigned char *) NULL)
2083 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2084 tiff_info->scanlines);
2085 if (tiff_info->pixels != (unsigned char *) NULL)
2086 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2090 static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2091 TIFFInfo *tiff_info)
2099 assert(tiff_info != (TIFFInfo *) NULL);
2100 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2101 option=GetImageOption(image_info,"tiff:tile-geometry");
2102 if (option == (const char *) NULL)
2104 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2105 if ((flags & HeightValue) == 0)
2106 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
2107 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,(uint32)
2108 tiff_info->tile_geometry.width);
2109 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,(uint32)
2110 tiff_info->tile_geometry.height);
2111 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
2112 tiff_info->tile_geometry.height*TIFFScanlineSize(tiff),
2113 sizeof(*tiff_info->scanlines));
2114 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
2115 TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
2116 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2117 (tiff_info->pixels == (unsigned char *) NULL))
2119 DestroyTIFFInfo(tiff_info);
2120 return(MagickFalse);
2125 static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,long row,
2126 tsample_t sample,Image *image)
2140 register unsigned char
2148 if (TIFFIsTiled(tiff) == 0)
2149 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2151 Fill scanlines to tile height.
2153 i=(long) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
2154 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2155 (size_t) TIFFScanlineSize(tiff));
2156 if (((unsigned long) (row % tiff_info->tile_geometry.height) !=
2157 (tiff_info->tile_geometry.height-1)) && (row != (long) (image->rows-1)))
2160 Write tile to TIFF image.
2163 bytes_per_pixel=TIFFTileSize(tiff)/(long) (tiff_info->tile_geometry.height*
2164 tiff_info->tile_geometry.width);
2165 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2166 tiff_info->tile_geometry.width;
2167 for (i=0; i < (long) number_tiles; i++)
2169 tile_width=(i == (long) (number_tiles-1)) ? image->columns-(i*
2170 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
2171 for (j=0; j < (long) ((row % tiff_info->tile_geometry.height)+1); j++)
2172 for (k=0; k < (long) tile_width; k++)
2174 if (bytes_per_pixel == 0)
2176 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2177 tiff_info->tile_geometry.width+k)/8);
2178 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2182 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2183 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2184 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2185 for (l=0; l < bytes_per_pixel; l++)
2188 if ((i*tiff_info->tile_geometry.width) != image->columns)
2189 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2190 tiff_info->tile_geometry.width),(uint32) ((row/
2191 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2199 static void TIFFSetProfiles(TIFF *tiff,Image *image)
2207 if (image->profiles == (void *) NULL)
2209 ResetImageProfileIterator(image);
2210 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2212 profile=GetImageProfile(image,name);
2213 #if defined(TIFFTAG_XMLPACKET)
2214 if (LocaleCompare(name,"xmp") == 0)
2215 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2216 profile),GetStringInfoDatum(profile));
2218 #if defined(TIFFTAG_ICCPROFILE)
2219 if (LocaleCompare(name,"icc") == 0)
2220 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2221 profile),GetStringInfoDatum(profile));
2223 if (LocaleCompare(name,"iptc") == 0)
2231 iptc_profile=CloneStringInfo(profile);
2232 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2234 SetStringInfoLength(iptc_profile,length);
2235 if (TIFFIsByteSwapped(tiff))
2236 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
2237 (unsigned long) (length/4));
2238 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2239 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2240 iptc_profile=DestroyStringInfo(iptc_profile);
2242 #if defined(TIFFTAG_PHOTOSHOP)
2243 if (LocaleCompare(name,"8bim") == 0)
2244 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2245 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2247 if (LocaleCompare(name,"tiff:37724") == 0)
2248 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2249 GetStringInfoDatum(profile));
2250 name=GetNextImageProfile(image);
2254 static void TIFFSetProperties(TIFF *tiff,Image *image)
2259 value=GetImageProperty(image,"tiff:hostcomputer");
2260 if (value != (const char *) NULL)
2261 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2262 value=GetImageProperty(image,"tiff:artist");
2263 if (value != (const char *) NULL)
2264 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2265 value=GetImageProperty(image,"tiff:timestamp");
2266 if (value != (const char *) NULL)
2267 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2268 value=GetImageProperty(image,"tiff:make");
2269 if (value != (const char *) NULL)
2270 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2271 value=GetImageProperty(image,"tiff:model");
2272 if (value != (const char *) NULL)
2273 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
2274 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,
2275 GetMagickVersion((unsigned long *) NULL));
2276 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
2277 value=GetImageProperty(image,"tiff:copyright");
2278 if (value != (const char *) NULL)
2279 (void) TIFFSetField(tiff,33432,value);
2280 value=GetImageProperty(image,"kodak-33423");
2281 if (value != (const char *) NULL)
2282 (void) TIFFSetField(tiff,33423,value);
2283 value=GetImageProperty(image,"kodak-36867");
2284 if (value != (const char *) NULL)
2285 (void) TIFFSetField(tiff,36867,value);
2286 value=GetImageProperty(image,"label");
2287 if (value != (const char *) NULL)
2288 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2289 value=GetImageProperty(image,"comment");
2290 if (value != (const char *) NULL)
2291 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2294 static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2296 #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2307 Write EXIF properties.
2310 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2311 for (i=0; exif_info[i].tag != 0; i++)
2313 value=GetImageProperty(image,exif_info[i].property);
2314 if (value == (const char *) NULL)
2316 switch (exif_info[i].type)
2320 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2328 shorty=(uint16) StringToLong(value);
2329 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2337 longy=(uint16) StringToLong(value);
2338 (void) TIFFSetField(tiff,exif_info[i].tag,longy);
2342 case TIFF_SRATIONAL:
2347 rational=StringToDouble(value);
2348 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2355 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2362 static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2365 #if !defined(TIFFDefaultStripSize)
2366 #define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2427 assert(image_info != (const ImageInfo *) NULL);
2428 assert(image_info->signature == MagickSignature);
2429 assert(image != (Image *) NULL);
2430 assert(image->signature == MagickSignature);
2431 if (image->debug != MagickFalse)
2432 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2433 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2434 if (status == MagickFalse)
2436 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2437 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2438 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
2439 switch (image_info->endian)
2441 case LSBEndian: mode="wl"; break;
2442 case MSBEndian: mode="wb"; break;
2443 default: mode="w"; break;
2445 #if defined(TIFF_VERSION_BIG)
2446 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
2447 switch (image_info->endian)
2449 case LSBEndian: mode="wl8"; break;
2450 case MSBEndian: mode="wb8"; break;
2451 default: mode="w8"; break;
2454 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2455 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2457 if (tiff == (TIFF *) NULL)
2459 (void) TIFFSetWarningHandler(warning_handler);
2460 (void) TIFFSetErrorHandler(error_handler);
2461 return(MagickFalse);
2464 debug=IsEventLogging();
2468 Initialize TIFF fields.
2470 if ((image_info->type != UndefinedType) &&
2471 (image_info->type != OptimizeType))
2472 (void) SetImageType(image,image_info->type);
2473 quantum_info=AcquireQuantumInfo(image_info,image);
2474 if (quantum_info == (QuantumInfo *) NULL)
2475 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2476 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2477 (quantum_info->format == UndefinedQuantumFormat) &&
2478 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2480 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2481 if (status == MagickFalse)
2482 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2484 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2485 (GetPreviousImageInList(image) != (Image *) NULL))
2486 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2487 if ((image->columns != (uint32) image->columns) ||
2488 (image->rows != (uint32) image->rows))
2489 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2490 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2491 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2492 compression=image->compression;
2493 if (image_info->compression != UndefinedCompression)
2494 compression=image_info->compression;
2495 switch (compression)
2497 case FaxCompression:
2499 compress_tag=COMPRESSION_CCITTFAX3;
2500 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2503 case Group4Compression:
2505 compress_tag=COMPRESSION_CCITTFAX4;
2506 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2509 case JPEGCompression:
2511 compress_tag=COMPRESSION_JPEG;
2514 case LZWCompression:
2516 compress_tag=COMPRESSION_LZW;
2519 case RLECompression:
2521 compress_tag=COMPRESSION_PACKBITS;
2524 case ZipCompression:
2526 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2532 compress_tag=COMPRESSION_NONE;
2536 #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2537 if ((compress_tag != COMPRESSION_NONE) &&
2538 (TIFFIsCODECConfigured(compress_tag) == 0))
2540 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2541 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
2542 MagickCompressOptions,(long) compression));
2543 compress_tag=COMPRESSION_NONE;
2544 compression=NoCompression;
2547 switch (compress_tag)
2549 #if defined(CCITT_SUPPORT)
2550 case COMPRESSION_CCITTFAX3:
2551 case COMPRESSION_CCITTFAX4:
2553 #if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2554 case COMPRESSION_JPEG:
2556 #if defined(LZW_SUPPORT)
2557 case COMPRESSION_LZW:
2559 #if defined(PACKBITS_SUPPORT)
2560 case COMPRESSION_PACKBITS:
2562 #if defined(ZIP_SUPPORT)
2563 case COMPRESSION_ADOBE_DEFLATE:
2565 case COMPRESSION_NONE:
2569 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2570 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
2571 MagickCompressOptions,(long) compression));
2572 compress_tag=COMPRESSION_NONE;
2573 compression=NoCompression;
2578 switch (compression)
2580 case FaxCompression:
2581 case Group4Compression:
2583 (void) SetImageType(image,BilevelType);
2586 case JPEGCompression:
2588 (void) SetImageStorageClass(image,DirectClass);
2589 (void) SetImageDepth(image,8);
2595 if (image->colorspace == CMYKColorspace)
2597 photometric=PHOTOMETRIC_SEPARATED;
2598 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2599 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2604 Full color TIFF raster.
2606 if (image->colorspace == LabColorspace)
2607 photometric=PHOTOMETRIC_CIELAB;
2609 if (image->colorspace == YCbCrColorspace)
2611 photometric=PHOTOMETRIC_YCBCR;
2612 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
2613 (void) SetImageDepth(image,8);
2617 if (image->colorspace != RGBColorspace)
2618 (void) TransformImageColorspace(image,RGBColorspace);
2619 photometric=PHOTOMETRIC_RGB;
2621 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2622 if ((image_info->type != TrueColorType) &&
2623 (image_info->type != TrueColorMatteType))
2625 if ((image_info->type != PaletteType) &&
2626 (IsGrayImage(image,&image->exception) != MagickFalse))
2628 photometric=(uint16) (quantum_info->min_is_white !=
2629 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2630 PHOTOMETRIC_MINISBLACK);
2631 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
2632 if ((image_info->depth == 0) &&
2633 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2635 status=SetQuantumDepth(image,quantum_info,1);
2636 if (status == MagickFalse)
2637 ThrowWriterException(ResourceLimitError,
2638 "MemoryAllocationFailed");
2642 if (image->storage_class == PseudoClass)
2648 Colormapped TIFF raster.
2650 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
2651 photometric=PHOTOMETRIC_PALETTE;
2653 while ((GetQuantumRange(depth)+1) < image->colors)
2655 status=SetQuantumDepth(image,quantum_info,depth);
2656 if (status == MagickFalse)
2657 ThrowWriterException(ResourceLimitError,
2658 "MemoryAllocationFailed");
2662 switch (image->endian)
2666 endian=FILLORDER_LSB2MSB;
2671 endian=FILLORDER_MSB2LSB;
2674 case UndefinedEndian:
2677 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2682 image->endian=MSBEndian;
2683 if ((int) (*(char *) &lsb_first) != 0)
2684 image->endian=LSBEndian;
2685 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2686 (photometric != PHOTOMETRIC_MINISWHITE))
2688 compress_tag=COMPRESSION_NONE;
2689 endian=FILLORDER_MSB2LSB;
2692 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2693 (photometric != PHOTOMETRIC_MINISWHITE))
2695 compress_tag=COMPRESSION_NONE;
2696 endian=FILLORDER_MSB2LSB;
2698 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2699 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
2700 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2701 if (image->matte != MagickFalse)
2709 TIFF has a matte channel.
2712 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2713 option=GetImageProperty(image,"tiff:alpha");
2714 if ((option != (const char *) NULL) &&
2715 (LocaleCompare(option,"associated") == 0))
2716 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2717 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2718 &samples_per_pixel);
2719 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2720 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2722 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2723 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2725 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2726 switch (quantum_info->format)
2728 case FloatingPointQuantumFormat:
2730 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2731 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2732 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2735 case SignedQuantumFormat:
2737 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2740 case UnsignedQuantumFormat:
2742 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2748 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2749 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2750 if (photometric == PHOTOMETRIC_RGB)
2751 if ((image_info->interlace == PlaneInterlace) ||
2752 (image_info->interlace == PartitionInterlace))
2753 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2755 if (TIFFScanlineSize(tiff) != 0)
2756 rows_per_strip=(unsigned long) MagickMax((size_t)
2757 TIFFDefaultStripSize(tiff,0),1);
2758 option=GetImageOption(image_info,"tiff:rows-per-strip");
2759 if (option != (const char *) NULL)
2760 rows_per_strip=(unsigned long) strtol(option,(char **) NULL,10);
2761 switch (compress_tag)
2763 case COMPRESSION_JPEG:
2765 #if defined(JPEG_SUPPORT)
2775 rows_per_strip+=(16-(rows_per_strip % 16));
2776 if (image->quality != 0)
2777 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2778 if (image_info->quality != UndefinedCompressionQuality)
2779 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2780 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2781 if (image->colorspace == RGBColorspace)
2783 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2784 sampling_factor=(const char *) NULL;
2785 value=GetImageProperty(image,"jpeg:sampling-factor");
2786 if (value != (char *) NULL)
2788 sampling_factor=value;
2789 if (image->debug != MagickFalse)
2790 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2791 " Input sampling-factors=%s",sampling_factor);
2793 if (image_info->sampling_factor != (char *) NULL)
2794 sampling_factor=image_info->sampling_factor;
2795 if (sampling_factor != (const char *) NULL)
2797 flags=ParseGeometry(sampling_factor,&geometry_info);
2798 if ((flags & SigmaValue) == 0)
2799 geometry_info.sigma=geometry_info.rho;
2800 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2801 geometry_info.rho,(uint16) geometry_info.sigma);
2804 if (bits_per_sample == 12)
2805 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2809 case COMPRESSION_ADOBE_DEFLATE:
2811 rows_per_strip=image->rows;
2812 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2814 if (((photometric == PHOTOMETRIC_RGB) ||
2815 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2816 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2817 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
2818 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,image_info->quality ==
2819 UndefinedCompressionQuality ? 7 : MagickMin(1L*image_info->quality/10,
2823 case COMPRESSION_CCITTFAX3:
2828 rows_per_strip=image->rows;
2829 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2832 case COMPRESSION_CCITTFAX4:
2834 rows_per_strip=image->rows;
2837 case COMPRESSION_LZW:
2839 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2841 if (((photometric == PHOTOMETRIC_RGB) ||
2842 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2843 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2844 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,2);
2850 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
2851 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2857 Set image resolution.
2860 if (image->units == PixelsPerInchResolution)
2862 if (image->units == PixelsPerCentimeterResolution)
2863 units=RESUNIT_CENTIMETER;
2864 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2865 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2866 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2867 if ((image->page.x != 0) || (image->page.y != 0))
2872 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2873 image->x_resolution);
2874 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2875 image->y_resolution);
2878 if (image->chromaticity.white_point.x != 0.0)
2884 Set image chromaticity.
2886 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2887 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2888 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2889 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2890 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2891 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2892 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2893 chromaticity[0]=(float) image->chromaticity.white_point.x;
2894 chromaticity[1]=(float) image->chromaticity.white_point.y;
2895 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2897 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2899 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2900 if (image->scene != 0)
2901 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
2902 GetImageListLength(image));
2904 if (image->orientation != UndefinedOrientation)
2905 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
2906 (void) TIFFSetProfiles(tiff,image);
2912 page=(uint16) scene;
2913 pages=GetImageListLength(image);
2914 if ((image_info->adjoin != MagickFalse) && (pages > 1))
2915 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2916 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
2918 (void) TIFFSetProperties(tiff,image);
2920 (void) TIFFSetEXIFProperties(tiff,image);
2922 Write image scanlines.
2924 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
2925 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2926 pixels=GetQuantumPixels(quantum_info);
2927 tiff_info.scanline=GetQuantumPixels(quantum_info);
2928 switch (photometric)
2930 case PHOTOMETRIC_CIELAB:
2931 case PHOTOMETRIC_YCBCR:
2932 case PHOTOMETRIC_RGB:
2937 switch (image_info->interlace)
2942 quantum_type=RGBQuantum;
2943 if (image->matte != MagickFalse)
2944 quantum_type=RGBAQuantum;
2945 for (y=0; y < (long) image->rows; y++)
2947 register const PixelPacket
2950 p=GetVirtualPixels(image,0,y,image->columns,1,
2952 if (p == (const PixelPacket *) NULL)
2954 length=ExportQuantumPixels(image,(const CacheView *) NULL,
2955 quantum_info,quantum_type,pixels,&image->exception);
2956 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
2958 if (image->previous == (Image *) NULL)
2960 status=SetImageProgress(image,SaveImageTag,y,image->rows);
2961 if (status == MagickFalse)
2967 case PlaneInterlace:
2968 case PartitionInterlace:
2971 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
2973 for (y=0; y < (long) image->rows; y++)
2975 register const PixelPacket
2978 p=GetVirtualPixels(image,0,y,image->columns,1,
2980 if (p == (const PixelPacket *) NULL)
2982 length=ExportQuantumPixels(image,(const CacheView *) NULL,
2983 quantum_info,RedQuantum,pixels,&image->exception);
2984 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
2987 if (image->previous == (Image *) NULL)
2989 status=SetImageProgress(image,SaveImageTag,100,400);
2990 if (status == MagickFalse)
2993 for (y=0; y < (long) image->rows; y++)
2995 register const PixelPacket
2998 p=GetVirtualPixels(image,0,y,image->columns,1,
3000 if (p == (const PixelPacket *) NULL)
3002 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3003 quantum_info,GreenQuantum,pixels,&image->exception);
3004 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3007 if (image->previous == (Image *) NULL)
3009 status=SetImageProgress(image,SaveImageTag,200,400);
3010 if (status == MagickFalse)
3013 for (y=0; y < (long) image->rows; y++)
3015 register const PixelPacket
3018 p=GetVirtualPixels(image,0,y,image->columns,1,
3020 if (p == (const PixelPacket *) NULL)
3022 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3023 quantum_info,BlueQuantum,pixels,&image->exception);
3024 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3027 if (image->previous == (Image *) NULL)
3029 status=SetImageProgress(image,SaveImageTag,300,400);
3030 if (status == MagickFalse)
3033 if (image->matte != MagickFalse)
3034 for (y=0; y < (long) 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,AlphaQuantum,pixels,&image->exception);
3045 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3048 if (image->previous == (Image *) NULL)
3050 status=SetImageProgress(image,SaveImageTag,400,400);
3051 if (status == MagickFalse)
3059 case PHOTOMETRIC_SEPARATED:
3064 quantum_type=CMYKQuantum;
3065 if (image->matte != MagickFalse)
3066 quantum_type=CMYKAQuantum;
3067 if (image->colorspace != CMYKColorspace)
3068 (void) TransformImageColorspace(image,CMYKColorspace);
3069 for (y=0; y < (long) image->rows; y++)
3071 register const PixelPacket
3074 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3075 if (p == (const PixelPacket *) NULL)
3077 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3078 quantum_info,quantum_type,pixels,&image->exception);
3079 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3081 if (image->previous == (Image *) NULL)
3083 status=SetImageProgress(image,SaveImageTag,y,image->rows);
3084 if (status == MagickFalse)
3090 case PHOTOMETRIC_PALETTE:
3098 Colormapped TIFF image.
3100 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3101 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3102 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3103 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3104 (blue == (uint16 *) NULL))
3105 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3107 Initialize TIFF colormap.
3109 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3110 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3111 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
3112 for (i=0; i < (long) image->colors; i++)
3114 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3115 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3116 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3118 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3119 red=(uint16 *) RelinquishMagickMemory(red);
3120 green=(uint16 *) RelinquishMagickMemory(green);
3121 blue=(uint16 *) RelinquishMagickMemory(blue);
3126 Convert PseudoClass packets to contiguous grayscale scanlines.
3128 quantum_type=IndexQuantum;
3129 if (image->matte != MagickFalse)
3131 if (photometric != PHOTOMETRIC_PALETTE)
3132 quantum_type=GrayAlphaQuantum;
3134 quantum_type=IndexAlphaQuantum;
3137 if (photometric != PHOTOMETRIC_PALETTE)
3138 quantum_type=GrayQuantum;
3139 for (y=0; y < (long) image->rows; y++)
3141 register const PixelPacket
3144 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3145 if (p == (const PixelPacket *) NULL)
3147 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3148 quantum_info,quantum_type,pixels,&image->exception);
3149 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3151 if (image->previous == (Image *) NULL)
3153 status=SetImageProgress(image,SaveImageTag,y,image->rows);
3154 if (status == MagickFalse)
3161 quantum_info=DestroyQuantumInfo(quantum_info);
3162 DestroyTIFFInfo(&tiff_info);
3163 if (0 && (image_info->verbose == MagickTrue))
3164 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3165 (void) TIFFWriteDirectory(tiff);
3166 image->endian=MSBEndian;
3167 if (endian == FILLORDER_LSB2MSB)
3168 image->endian=LSBEndian;
3169 image=SyncNextImageInList(image);
3170 if (image == (Image *) NULL)
3172 status=SetImageProgress(image,SaveImagesTag,scene++,
3173 GetImageListLength(image));
3174 if (status == MagickFalse)
3176 } while (image_info->adjoin != MagickFalse);
3177 (void) TIFFSetWarningHandler(warning_handler);
3178 (void) TIFFSetErrorHandler(error_handler);