cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % TTTTT IIIII FFFFF FFFFF % |
| 7 | % T I F F % |
| 8 | % T I FFF FFF % |
| 9 | % T I F F % |
| 10 | % T IIIII F F % |
| 11 | % % |
| 12 | % % |
| 13 | % Read/Write TIFF Image Format % |
| 14 | % % |
| 15 | % Software Design % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 20 | % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 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. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | */ |
| 38 | |
| 39 | /* |
| 40 | Include declarations. |
| 41 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #include "MagickCore/studio.h" |
cristy | c4debe6 | 2012-02-09 13:51:53 +0000 | [diff] [blame] | 43 | #include "MagickCore/artifact.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 44 | #include "MagickCore/attribute.h" |
| 45 | #include "MagickCore/blob.h" |
| 46 | #include "MagickCore/blob-private.h" |
| 47 | #include "MagickCore/cache.h" |
| 48 | #include "MagickCore/color.h" |
| 49 | #include "MagickCore/color-private.h" |
| 50 | #include "MagickCore/colormap.h" |
| 51 | #include "MagickCore/colorspace.h" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 52 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 53 | #include "MagickCore/constitute.h" |
| 54 | #include "MagickCore/enhance.h" |
| 55 | #include "MagickCore/exception.h" |
| 56 | #include "MagickCore/exception-private.h" |
| 57 | #include "MagickCore/geometry.h" |
| 58 | #include "MagickCore/image.h" |
| 59 | #include "MagickCore/image-private.h" |
| 60 | #include "MagickCore/list.h" |
| 61 | #include "MagickCore/log.h" |
| 62 | #include "MagickCore/magick.h" |
| 63 | #include "MagickCore/memory_.h" |
| 64 | #include "MagickCore/module.h" |
| 65 | #include "MagickCore/monitor.h" |
| 66 | #include "MagickCore/monitor-private.h" |
| 67 | #include "MagickCore/option.h" |
| 68 | #include "MagickCore/pixel-accessor.h" |
cristy | 5ee04f1 | 2012-09-20 23:48:03 +0000 | [diff] [blame] | 69 | #include "MagickCore/pixel-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 70 | #include "MagickCore/property.h" |
| 71 | #include "MagickCore/quantum.h" |
| 72 | #include "MagickCore/quantum-private.h" |
| 73 | #include "MagickCore/profile.h" |
| 74 | #include "MagickCore/resize.h" |
| 75 | #include "MagickCore/resource_.h" |
| 76 | #include "MagickCore/semaphore.h" |
| 77 | #include "MagickCore/splay-tree.h" |
| 78 | #include "MagickCore/static.h" |
| 79 | #include "MagickCore/statistic.h" |
| 80 | #include "MagickCore/string_.h" |
| 81 | #include "MagickCore/string-private.h" |
| 82 | #include "MagickCore/thread_.h" |
cristy | e40005d | 2012-03-23 12:18:45 +0000 | [diff] [blame] | 83 | #include "MagickCore/token.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 84 | #include "MagickCore/utility.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 85 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
| 86 | # if defined(MAGICKCORE_HAVE_TIFFCONF_H) |
| 87 | # include "tiffconf.h" |
cristy | ef8f26b | 2010-12-19 20:29:16 +0000 | [diff] [blame] | 88 | # endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 89 | # include "tiff.h" |
| 90 | # include "tiffio.h" |
| 91 | # if !defined(COMPRESSION_ADOBE_DEFLATE) |
| 92 | # define COMPRESSION_ADOBE_DEFLATE 8 |
| 93 | # endif |
cristy | ef8f26b | 2010-12-19 20:29:16 +0000 | [diff] [blame] | 94 | # if !defined(PREDICTOR_HORIZONTAL) |
| 95 | # define PREDICTOR_HORIZONTAL 2 |
| 96 | # endif |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 97 | # if !defined(TIFFTAG_COPYRIGHT) |
| 98 | # define TIFFTAG_COPYRIGHT 33432 |
| 99 | # endif |
| 100 | # if !defined(TIFFTAG_OPIIMAGEID) |
cristy | d58a662 | 2013-12-18 13:51:01 +0000 | [diff] [blame] | 101 | # define TIFFTAG_OPIIMAGEID 32781 |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 102 | # endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | Typedef declarations. |
| 106 | */ |
| 107 | #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY) |
| 108 | typedef struct _ExifInfo |
| 109 | { |
| 110 | unsigned int |
| 111 | tag, |
cristy | 9e6517b | 2013-08-15 12:13:41 +0000 | [diff] [blame] | 112 | type, |
| 113 | variable_length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 114 | |
| 115 | const char |
| 116 | *property; |
| 117 | } ExifInfo; |
| 118 | |
| 119 | static const ExifInfo |
| 120 | exif_info[] = { |
cristy | 9e6517b | 2013-08-15 12:13:41 +0000 | [diff] [blame] | 121 | { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, 0, "exif:ExposureTime" }, |
| 122 | { EXIFTAG_FNUMBER, TIFF_RATIONAL, 0, "exif:FNumber" }, |
| 123 | { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, 0, "exif:ExposureProgram" }, |
| 124 | { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, 0, "exif:SpectralSensitivity" }, |
| 125 | { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, 1, "exif:ISOSpeedRatings" }, |
| 126 | { EXIFTAG_OECF, TIFF_NOTYPE, 0, "exif:OptoelectricConversionFactor" }, |
| 127 | { EXIFTAG_EXIFVERSION, TIFF_NOTYPE, 0, "exif:ExifVersion" }, |
| 128 | { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, 0, "exif:DateTimeOriginal" }, |
| 129 | { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, 0, "exif:DateTimeDigitized" }, |
| 130 | { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_NOTYPE, 0, "exif:ComponentsConfiguration" }, |
| 131 | { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, 0, "exif:CompressedBitsPerPixel" }, |
| 132 | { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, 0, "exif:ShutterSpeedValue" }, |
| 133 | { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, 0, "exif:ApertureValue" }, |
| 134 | { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, 0, "exif:BrightnessValue" }, |
| 135 | { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, 0, "exif:ExposureBiasValue" }, |
| 136 | { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, 0, "exif:MaxApertureValue" }, |
| 137 | { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, 0, "exif:SubjectDistance" }, |
| 138 | { EXIFTAG_METERINGMODE, TIFF_SHORT, 0, "exif:MeteringMode" }, |
| 139 | { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, 0, "exif:LightSource" }, |
| 140 | { EXIFTAG_FLASH, TIFF_SHORT, 0, "exif:Flash" }, |
| 141 | { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, 0, "exif:FocalLength" }, |
| 142 | { EXIFTAG_SUBJECTAREA, TIFF_NOTYPE, 0, "exif:SubjectArea" }, |
| 143 | { EXIFTAG_MAKERNOTE, TIFF_NOTYPE, 0, "exif:MakerNote" }, |
| 144 | { EXIFTAG_USERCOMMENT, TIFF_NOTYPE, 0, "exif:UserComment" }, |
| 145 | { EXIFTAG_SUBSECTIME, TIFF_ASCII, 0, "exif:SubSecTime" }, |
| 146 | { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, 0, "exif:SubSecTimeOriginal" }, |
| 147 | { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, 0, "exif:SubSecTimeDigitized" }, |
| 148 | { EXIFTAG_FLASHPIXVERSION, TIFF_NOTYPE, 0, "exif:FlashpixVersion" }, |
| 149 | { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, 0, "exif:PixelXDimension" }, |
cristy | 9e6517b | 2013-08-15 12:13:41 +0000 | [diff] [blame] | 150 | { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, 0, "exif:PixelYDimension" }, |
cristy | 9e6517b | 2013-08-15 12:13:41 +0000 | [diff] [blame] | 151 | { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, 0, "exif:RelatedSoundFile" }, |
| 152 | { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, 0, "exif:FlashEnergy" }, |
| 153 | { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_NOTYPE, 0, "exif:SpatialFrequencyResponse" }, |
| 154 | { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, 0, "exif:FocalPlaneXResolution" }, |
| 155 | { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, 0, "exif:FocalPlaneYResolution" }, |
| 156 | { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, 0, "exif:FocalPlaneResolutionUnit" }, |
| 157 | { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, 0, "exif:SubjectLocation" }, |
| 158 | { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, 0, "exif:ExposureIndex" }, |
| 159 | { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, 0, "exif:SensingMethod" }, |
| 160 | { EXIFTAG_FILESOURCE, TIFF_NOTYPE, 0, "exif:FileSource" }, |
| 161 | { EXIFTAG_SCENETYPE, TIFF_NOTYPE, 0, "exif:SceneType" }, |
| 162 | { EXIFTAG_CFAPATTERN, TIFF_NOTYPE, 0, "exif:CFAPattern" }, |
| 163 | { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, 0, "exif:CustomRendered" }, |
| 164 | { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, 0, "exif:ExposureMode" }, |
| 165 | { EXIFTAG_WHITEBALANCE, TIFF_SHORT, 0, "exif:WhiteBalance" }, |
| 166 | { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, 0, "exif:DigitalZoomRatio" }, |
| 167 | { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, 0, "exif:FocalLengthIn35mmFilm" }, |
| 168 | { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, 0, "exif:SceneCaptureType" }, |
| 169 | { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, 0, "exif:GainControl" }, |
| 170 | { EXIFTAG_CONTRAST, TIFF_SHORT, 0, "exif:Contrast" }, |
| 171 | { EXIFTAG_SATURATION, TIFF_SHORT, 0, "exif:Saturation" }, |
| 172 | { EXIFTAG_SHARPNESS, TIFF_SHORT, 0, "exif:Sharpness" }, |
| 173 | { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_NOTYPE, 0, "exif:DeviceSettingDescription" }, |
| 174 | { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, 0, "exif:SubjectDistanceRange" }, |
| 175 | { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, 0, "exif:ImageUniqueID" }, |
| 176 | { 0, 0, 0, (char *) NULL } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 177 | }; |
| 178 | #endif |
cristy | 0805e06 | 2011-01-30 02:56:36 +0000 | [diff] [blame] | 179 | #endif /* MAGICKCORE_TIFF_DELEGATE */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | Global declarations. |
| 183 | */ |
| 184 | static MagickThreadKey |
| 185 | tiff_exception; |
| 186 | |
| 187 | static SemaphoreInfo |
| 188 | *tiff_semaphore = (SemaphoreInfo *) NULL; |
| 189 | |
| 190 | static volatile MagickBooleanType |
| 191 | instantiate_key = MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | Forward declarations. |
| 195 | */ |
| 196 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 197 | static Image * |
| 198 | ReadTIFFImage(const ImageInfo *,ExceptionInfo *); |
| 199 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 200 | static MagickBooleanType |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 201 | WriteGROUP4Image(const ImageInfo *,Image *,ExceptionInfo *), |
| 202 | WritePTIFImage(const ImageInfo *,Image *,ExceptionInfo *), |
| 203 | WriteTIFFImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 204 | #endif |
| 205 | |
| 206 | /* |
| 207 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 208 | % % |
| 209 | % % |
| 210 | % % |
| 211 | % I s T I F F % |
| 212 | % % |
| 213 | % % |
| 214 | % % |
| 215 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 216 | % |
| 217 | % IsTIFF() returns MagickTrue if the image format type, identified by the |
| 218 | % magick string, is TIFF. |
| 219 | % |
| 220 | % The format of the IsTIFF method is: |
| 221 | % |
| 222 | % MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length) |
| 223 | % |
| 224 | % A description of each parameter follows: |
| 225 | % |
| 226 | % o magick: compare image format pattern against these bytes. |
| 227 | % |
| 228 | % o length: Specifies the length of the magick string. |
| 229 | % |
| 230 | */ |
| 231 | static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length) |
| 232 | { |
| 233 | if (length < 4) |
| 234 | return(MagickFalse); |
| 235 | if (memcmp(magick,"\115\115\000\052",4) == 0) |
| 236 | return(MagickTrue); |
| 237 | if (memcmp(magick,"\111\111\052\000",4) == 0) |
| 238 | return(MagickTrue); |
| 239 | #if defined(TIFF_VERSION_BIG) |
| 240 | if (length < 8) |
| 241 | return(MagickFalse); |
| 242 | if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0) |
| 243 | return(MagickTrue); |
| 244 | if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0) |
| 245 | return(MagickTrue); |
| 246 | #endif |
| 247 | return(MagickFalse); |
| 248 | } |
| 249 | |
| 250 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
| 251 | /* |
| 252 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 253 | % % |
| 254 | % % |
| 255 | % % |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 256 | % R e a d G R O U P 4 I m a g e % |
| 257 | % % |
| 258 | % % |
| 259 | % % |
| 260 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 261 | % |
| 262 | % ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It |
| 263 | % allocates the memory necessary for the new Image structure and returns a |
| 264 | % pointer to the new image. |
| 265 | % |
| 266 | % The format of the ReadGROUP4Image method is: |
| 267 | % |
| 268 | % Image *ReadGROUP4Image(const ImageInfo *image_info, |
| 269 | % ExceptionInfo *exception) |
| 270 | % |
| 271 | % A description of each parameter follows: |
| 272 | % |
| 273 | % o image_info: the image info. |
| 274 | % |
| 275 | % o exception: return any errors or warnings in this structure. |
| 276 | % |
| 277 | */ |
| 278 | |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 279 | static inline size_t WriteLSBLong(FILE *file,const size_t value) |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 280 | { |
| 281 | unsigned char |
| 282 | buffer[4]; |
| 283 | |
| 284 | buffer[0]=(unsigned char) value; |
| 285 | buffer[1]=(unsigned char) (value >> 8); |
| 286 | buffer[2]=(unsigned char) (value >> 16); |
| 287 | buffer[3]=(unsigned char) (value >> 24); |
| 288 | return(fwrite(buffer,1,4,file)); |
| 289 | } |
| 290 | |
| 291 | static Image *ReadGROUP4Image(const ImageInfo *image_info, |
| 292 | ExceptionInfo *exception) |
| 293 | { |
| 294 | char |
| 295 | filename[MaxTextExtent]; |
| 296 | |
| 297 | FILE |
| 298 | *file; |
| 299 | |
| 300 | Image |
| 301 | *image; |
| 302 | |
| 303 | ImageInfo |
| 304 | *read_info; |
| 305 | |
| 306 | int |
| 307 | c, |
| 308 | unique_file; |
| 309 | |
| 310 | MagickBooleanType |
| 311 | status; |
| 312 | |
| 313 | size_t |
| 314 | length; |
| 315 | |
| 316 | ssize_t |
| 317 | offset, |
| 318 | strip_offset; |
| 319 | |
| 320 | /* |
| 321 | Open image file. |
| 322 | */ |
| 323 | assert(image_info != (const ImageInfo *) NULL); |
| 324 | assert(image_info->signature == MagickSignature); |
| 325 | if (image_info->debug != MagickFalse) |
| 326 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 327 | image_info->filename); |
| 328 | assert(exception != (ExceptionInfo *) NULL); |
| 329 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 330 | image=AcquireImage(image_info,exception); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 331 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 332 | if (status == MagickFalse) |
| 333 | { |
| 334 | image=DestroyImageList(image); |
| 335 | return((Image *) NULL); |
| 336 | } |
| 337 | /* |
| 338 | Write raw CCITT Group 4 wrapped as a TIFF image file. |
| 339 | */ |
| 340 | file=(FILE *) NULL; |
| 341 | unique_file=AcquireUniqueFileResource(filename); |
| 342 | if (unique_file != -1) |
| 343 | file=fdopen(unique_file,"wb"); |
| 344 | if ((unique_file == -1) || (file == (FILE *) NULL)) |
| 345 | ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile"); |
| 346 | length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file); |
| 347 | length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file); |
| 348 | length=fwrite("\000\001\004\000\001\000\000\000",1,8,file); |
| 349 | length=WriteLSBLong(file,image->columns); |
| 350 | length=fwrite("\001\001\004\000\001\000\000\000",1,8,file); |
| 351 | length=WriteLSBLong(file,image->rows); |
| 352 | length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file); |
| 353 | length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file); |
| 354 | length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file); |
| 355 | length=fwrite("\021\001\003\000\001\000\000\000",1,8,file); |
| 356 | strip_offset=10+(12*14)+4+8; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 357 | length=WriteLSBLong(file,(size_t) strip_offset); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 358 | length=fwrite("\022\001\003\000\001\000\000\000",1,8,file); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 359 | length=WriteLSBLong(file,(size_t) image_info->orientation); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 360 | length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file); |
| 361 | length=fwrite("\026\001\004\000\001\000\000\000",1,8,file); |
cristy | 94c8fe4 | 2009-10-06 01:57:36 +0000 | [diff] [blame] | 362 | length=WriteLSBLong(file,image->rows); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 363 | length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file); |
| 364 | offset=(ssize_t) ftell(file)-4; |
| 365 | length=fwrite("\032\001\005\000\001\000\000\000",1,8,file); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 366 | length=WriteLSBLong(file,(size_t) (strip_offset-8)); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 367 | length=fwrite("\033\001\005\000\001\000\000\000",1,8,file); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 368 | length=WriteLSBLong(file,(size_t) (strip_offset-8)); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 369 | length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file); |
| 370 | length=fwrite("\000\000\000\000",1,4,file); |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 371 | length=WriteLSBLong(file,(long) image->resolution.x); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 372 | length=WriteLSBLong(file,1); |
| 373 | for (length=0; (c=ReadBlobByte(image)) != EOF; length++) |
| 374 | (void) fputc(c,file); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 375 | offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 376 | length=WriteLSBLong(file,(unsigned int) length); |
| 377 | (void) fclose(file); |
| 378 | (void) CloseBlob(image); |
| 379 | image=DestroyImage(image); |
| 380 | /* |
| 381 | Read TIFF image. |
| 382 | */ |
cristy | 14efd99 | 2009-09-26 23:46:03 +0000 | [diff] [blame] | 383 | read_info=CloneImageInfo((ImageInfo *) NULL); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 384 | (void) FormatLocaleString(read_info->filename,MaxTextExtent,"%s",filename); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 385 | image=ReadTIFFImage(read_info,exception); |
| 386 | read_info=DestroyImageInfo(read_info); |
| 387 | if (image != (Image *) NULL) |
| 388 | { |
| 389 | (void) CopyMagickString(image->filename,image_info->filename, |
| 390 | MaxTextExtent); |
| 391 | (void) CopyMagickString(image->magick_filename,image_info->filename, |
| 392 | MaxTextExtent); |
| 393 | (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent); |
| 394 | } |
| 395 | (void) RelinquishUniqueFileResource(filename); |
| 396 | return(image); |
| 397 | } |
| 398 | #endif |
| 399 | |
| 400 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
| 401 | /* |
| 402 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 403 | % % |
| 404 | % % |
| 405 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 406 | % R e a d T I F F I m a g e % |
| 407 | % % |
| 408 | % % |
| 409 | % % |
| 410 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 411 | % |
| 412 | % ReadTIFFImage() reads a Tagged image file and returns it. It allocates the |
| 413 | % memory necessary for the new Image structure and returns a pointer to the |
| 414 | % new image. |
| 415 | % |
| 416 | % The format of the ReadTIFFImage method is: |
| 417 | % |
| 418 | % Image *ReadTIFFImage(const ImageInfo *image_info, |
| 419 | % ExceptionInfo *exception) |
| 420 | % |
| 421 | % A description of each parameter follows: |
| 422 | % |
| 423 | % o image_info: the image info. |
| 424 | % |
| 425 | % o exception: return any errors or warnings in this structure. |
| 426 | % |
| 427 | */ |
| 428 | |
cristy | 514da94 | 2012-07-06 23:24:14 +0000 | [diff] [blame] | 429 | static MagickBooleanType DecodeLabImage(Image *image,ExceptionInfo *exception) |
cristy | f2273ac | 2012-06-30 22:45:42 +0000 | [diff] [blame] | 430 | { |
| 431 | CacheView |
| 432 | *image_view; |
| 433 | |
| 434 | MagickBooleanType |
| 435 | status; |
| 436 | |
| 437 | ssize_t |
| 438 | y; |
| 439 | |
| 440 | status=MagickTrue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 441 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | f2273ac | 2012-06-30 22:45:42 +0000 | [diff] [blame] | 442 | for (y=0; y < (ssize_t) image->rows; y++) |
| 443 | { |
| 444 | register Quantum |
| 445 | *restrict q; |
| 446 | |
| 447 | register ssize_t |
| 448 | x; |
| 449 | |
| 450 | if (status == MagickFalse) |
| 451 | continue; |
| 452 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 453 | if (q == (Quantum *) NULL) |
| 454 | { |
| 455 | status=MagickFalse; |
| 456 | continue; |
| 457 | } |
| 458 | for (x=0; x < (ssize_t) image->columns; x++) |
| 459 | { |
cristy | 514da94 | 2012-07-06 23:24:14 +0000 | [diff] [blame] | 460 | double |
| 461 | a, |
| 462 | b; |
| 463 | |
cristy | 459f766 | 2012-07-07 01:18:24 +0000 | [diff] [blame] | 464 | a=QuantumScale*GetPixela(image,q)+0.5; |
| 465 | if (a > 1.0) |
cristy | 514da94 | 2012-07-06 23:24:14 +0000 | [diff] [blame] | 466 | a-=1.0; |
cristy | 459f766 | 2012-07-07 01:18:24 +0000 | [diff] [blame] | 467 | b=QuantumScale*GetPixelb(image,q)+0.5; |
| 468 | if (b > 1.0) |
cristy | 514da94 | 2012-07-06 23:24:14 +0000 | [diff] [blame] | 469 | b-=1.0; |
cristy | 514da94 | 2012-07-06 23:24:14 +0000 | [diff] [blame] | 470 | SetPixela(image,QuantumRange*a,q); |
| 471 | SetPixelb(image,QuantumRange*b,q); |
cristy | f2273ac | 2012-06-30 22:45:42 +0000 | [diff] [blame] | 472 | q+=GetPixelChannels(image); |
| 473 | } |
| 474 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 475 | status=MagickFalse; |
| 476 | } |
| 477 | image_view=DestroyCacheView(image_view); |
| 478 | return(status); |
| 479 | } |
| 480 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 481 | static inline size_t MagickMax(const size_t x,const size_t y) |
| 482 | { |
| 483 | if (x > y) |
| 484 | return(x); |
| 485 | return(y); |
| 486 | } |
| 487 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 488 | static inline ssize_t MagickMin(const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 489 | { |
| 490 | if (x < y) |
| 491 | return(x); |
| 492 | return(y); |
| 493 | } |
| 494 | |
| 495 | static MagickBooleanType ReadProfile(Image *image,const char *name, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 496 | unsigned char *datum,ssize_t length,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 497 | { |
| 498 | MagickBooleanType |
| 499 | status; |
| 500 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 501 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 502 | i; |
| 503 | |
| 504 | StringInfo |
| 505 | *profile; |
| 506 | |
| 507 | if (length < 4) |
| 508 | return(MagickFalse); |
| 509 | i=0; |
| 510 | if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0)) |
| 511 | { |
| 512 | for (i=0; i < (length-4); i+=2) |
| 513 | if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0) |
| 514 | break; |
| 515 | if (i == length) |
| 516 | length-=i; |
| 517 | else |
| 518 | i=0; |
| 519 | if (length < 4) |
| 520 | return(MagickFalse); |
| 521 | } |
cristy | e8f8f38 | 2011-09-01 13:32:37 +0000 | [diff] [blame] | 522 | profile=BlobToStringInfo(datum+i,(size_t) length); |
| 523 | if (profile == (StringInfo *) NULL) |
| 524 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 525 | image->filename); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 526 | status=SetImageProfile(image,name,profile,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 527 | profile=DestroyStringInfo(profile); |
| 528 | if (status == MagickFalse) |
| 529 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 530 | image->filename); |
| 531 | return(MagickTrue); |
| 532 | } |
| 533 | |
| 534 | #if defined(__cplusplus) || defined(c_plusplus) |
| 535 | extern "C" { |
| 536 | #endif |
| 537 | |
| 538 | static int TIFFCloseBlob(thandle_t image) |
| 539 | { |
| 540 | (void) CloseBlob((Image *) image); |
| 541 | return(0); |
| 542 | } |
| 543 | |
| 544 | static void TIFFErrors(const char *module,const char *format,va_list error) |
| 545 | { |
| 546 | char |
| 547 | message[MaxTextExtent]; |
| 548 | |
| 549 | ExceptionInfo |
| 550 | *exception; |
| 551 | |
| 552 | #if defined(MAGICKCORE_HAVE_VSNPRINTF) |
| 553 | (void) vsnprintf(message,MaxTextExtent,format,error); |
| 554 | #else |
| 555 | (void) vsprintf(message,format,error); |
| 556 | #endif |
| 557 | (void) ConcatenateMagickString(message,".",MaxTextExtent); |
| 558 | exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception); |
| 559 | if (exception != (ExceptionInfo *) NULL) |
cristy | b1db623 | 2010-07-26 17:31:48 +0000 | [diff] [blame] | 560 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message, |
| 561 | "`%s'",module); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 562 | } |
| 563 | |
cristy | 9c47c73 | 2011-11-09 19:58:33 +0000 | [diff] [blame] | 564 | static toff_t TIFFGetBlobSize(thandle_t image) |
| 565 | { |
| 566 | return((toff_t) GetBlobSize((Image *) image)); |
| 567 | } |
| 568 | |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 569 | static void TIFFGetProfiles(TIFF *tiff,Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 570 | { |
| 571 | uint32 |
| 572 | length; |
| 573 | |
| 574 | unsigned char |
| 575 | *profile; |
| 576 | |
| 577 | #if defined(TIFFTAG_ICCPROFILE) |
| 578 | length=0; |
| 579 | if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 580 | (void) ReadProfile(image,"icc",profile,(ssize_t) length,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 581 | #endif |
| 582 | #if defined(TIFFTAG_PHOTOSHOP) |
| 583 | length=0; |
| 584 | if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 585 | (void) ReadProfile(image,"8bim",profile,(ssize_t) length,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 586 | #endif |
| 587 | #if defined(TIFFTAG_RICHTIFFIPTC) |
| 588 | length=0; |
| 589 | if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1) |
| 590 | { |
| 591 | if (TIFFIsByteSwapped(tiff) != 0) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 592 | TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 593 | (void) ReadProfile(image,"iptc",profile,4L*length,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 594 | } |
| 595 | #endif |
| 596 | #if defined(TIFFTAG_XMLPACKET) |
| 597 | length=0; |
| 598 | if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 599 | (void) ReadProfile(image,"xmp",profile,(ssize_t) length,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 600 | #endif |
| 601 | length=0; |
| 602 | if (TIFFGetField(tiff,37724,&length,&profile) == 1) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 603 | (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length,exception); |
cristy | 30df84f | 2012-10-04 01:34:18 +0000 | [diff] [blame] | 604 | if (TIFFGetField(tiff,34118,&length,&profile) == 1) |
| 605 | (void) ReadProfile(image,"tiff:34118",profile,(ssize_t) length,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 606 | } |
| 607 | |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 608 | static void TIFFGetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 609 | { |
| 610 | char |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 611 | message[MaxTextExtent], |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 612 | *text; |
| 613 | |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 614 | uint32 |
| 615 | count; |
| 616 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 617 | if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 618 | (void) SetImageProperty(image,"tiff:artist",text,exception); |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 619 | if (TIFFGetField(tiff,TIFFTAG_COPYRIGHT,&text) == 1) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 620 | (void) SetImageProperty(image,"tiff:copyright",text,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 621 | if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 622 | (void) SetImageProperty(image,"tiff:timestamp",text,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 623 | if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 624 | (void) SetImageProperty(image,"tiff:document",text,exception); |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 625 | if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 626 | (void) SetImageProperty(image,"tiff:hostcomputer",text,exception); |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 627 | if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 628 | (void) SetImageProperty(image,"comment",text,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 629 | if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 630 | (void) SetImageProperty(image,"tiff:make",text,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 631 | if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 632 | (void) SetImageProperty(image,"tiff:model",text,exception); |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 633 | if (TIFFGetField(tiff,TIFFTAG_OPIIMAGEID,&count,&text) == 1) |
| 634 | { |
| 635 | if (count >= MaxTextExtent) |
| 636 | count=MaxTextExtent-1; |
| 637 | (void) CopyMagickString(message,text,count+1); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 638 | (void) SetImageProperty(image,"tiff:image-id",message,exception); |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 639 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 640 | if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 641 | (void) SetImageProperty(image,"label",text,exception); |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 642 | if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 643 | (void) SetImageProperty(image,"tiff:software",text,exception); |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 644 | if (TIFFGetField(tiff,33423,&count,&text) == 1) |
| 645 | { |
| 646 | if (count >= MaxTextExtent) |
| 647 | count=MaxTextExtent-1; |
| 648 | (void) CopyMagickString(message,text,count+1); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 649 | (void) SetImageProperty(image,"tiff:kodak-33423",message,exception); |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 650 | } |
| 651 | if (TIFFGetField(tiff,36867,&count,&text) == 1) |
| 652 | { |
| 653 | if (count >= MaxTextExtent) |
| 654 | count=MaxTextExtent-1; |
| 655 | (void) CopyMagickString(message,text,count+1); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 656 | (void) SetImageProperty(image,"tiff:kodak-36867",message,exception); |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 657 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 658 | } |
| 659 | |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 660 | static void TIFFGetEXIFProperties(TIFF *tiff,Image *image, |
| 661 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 662 | { |
| 663 | #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY) |
| 664 | char |
| 665 | value[MaxTextExtent]; |
| 666 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 667 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 668 | i; |
| 669 | |
| 670 | tdir_t |
| 671 | directory; |
| 672 | |
cristy | ccf4e12 | 2011-10-29 00:29:19 +0000 | [diff] [blame] | 673 | #if defined(TIFF_VERSION_BIG) |
| 674 | uint64 |
| 675 | #else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 676 | uint32 |
cristy | ccf4e12 | 2011-10-29 00:29:19 +0000 | [diff] [blame] | 677 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 678 | offset; |
| 679 | |
cristy | 7992e40 | 2011-09-16 15:24:22 +0000 | [diff] [blame] | 680 | void |
| 681 | *sans; |
| 682 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 683 | /* |
| 684 | Read EXIF properties. |
| 685 | */ |
cristy | 00170cd | 2012-01-25 00:55:46 +0000 | [diff] [blame] | 686 | offset=0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 687 | if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0) |
| 688 | return; |
| 689 | directory=TIFFCurrentDirectory(tiff); |
| 690 | if (TIFFReadEXIFDirectory(tiff,offset) == 0) |
| 691 | return; |
cristy | 7992e40 | 2011-09-16 15:24:22 +0000 | [diff] [blame] | 692 | sans=NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 693 | for (i=0; exif_info[i].tag != 0; i++) |
| 694 | { |
| 695 | *value='\0'; |
| 696 | switch (exif_info[i].type) |
| 697 | { |
| 698 | case TIFF_ASCII: |
| 699 | { |
| 700 | char |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 701 | *ascii; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 702 | |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 703 | ascii=(char *) NULL; |
| 704 | if ((TIFFGetField(tiff,exif_info[i].tag,&ascii,&sans,&sans) != 0) && |
| 705 | (ascii != (char *) NULL) && (*ascii != '\0')) |
| 706 | (void) CopyMagickString(value,ascii,MaxTextExtent); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 707 | break; |
| 708 | } |
| 709 | case TIFF_SHORT: |
| 710 | { |
cristy | 9e6517b | 2013-08-15 12:13:41 +0000 | [diff] [blame] | 711 | if (exif_info[i].variable_length != 0) |
| 712 | { |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 713 | uint16 |
| 714 | *shorty; |
| 715 | |
| 716 | if (TIFFGetField(tiff,exif_info[i].tag,&sans,&shorty,&sans,&sans) != 0) |
| 717 | (void) FormatLocaleString(value,MaxTextExtent,"%d",shorty[0]); |
cristy | 9e6517b | 2013-08-15 12:13:41 +0000 | [diff] [blame] | 718 | } |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 719 | else |
| 720 | { |
| 721 | uint16 |
| 722 | shorty; |
| 723 | |
| 724 | shorty=0; |
| 725 | if (TIFFGetField(tiff,exif_info[i].tag,&shorty,&sans,&sans) != 0) |
| 726 | (void) FormatLocaleString(value,MaxTextExtent,"%d",shorty); |
| 727 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 728 | break; |
| 729 | } |
| 730 | case TIFF_LONG: |
| 731 | { |
cristy | a93d496 | 2013-09-29 20:20:01 +0000 | [diff] [blame] | 732 | uint32 |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 733 | longy; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 734 | |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 735 | longy=0; |
| 736 | if (TIFFGetField(tiff,exif_info[i].tag,&longy,&sans,&sans) != 0) |
| 737 | (void) FormatLocaleString(value,MaxTextExtent,"%d",longy); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 738 | break; |
| 739 | } |
cristy | ccf4e12 | 2011-10-29 00:29:19 +0000 | [diff] [blame] | 740 | #if defined(TIFF_VERSION_BIG) |
| 741 | case TIFF_LONG8: |
| 742 | { |
cristy | a93d496 | 2013-09-29 20:20:01 +0000 | [diff] [blame] | 743 | uint64 |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 744 | long8y; |
cristy | ccf4e12 | 2011-10-29 00:29:19 +0000 | [diff] [blame] | 745 | |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 746 | long8y=0; |
| 747 | if (TIFFGetField(tiff,exif_info[i].tag,&long8y,&sans,&sans) != 0) |
cristy | bcb0f45 | 2012-02-12 19:26:56 +0000 | [diff] [blame] | 748 | (void) FormatLocaleString(value,MaxTextExtent,"%.20g",(double) |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 749 | ((MagickOffsetType) long8y)); |
cristy | ccf4e12 | 2011-10-29 00:29:19 +0000 | [diff] [blame] | 750 | break; |
| 751 | } |
| 752 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 753 | case TIFF_RATIONAL: |
| 754 | case TIFF_SRATIONAL: |
cristy | 7992e40 | 2011-09-16 15:24:22 +0000 | [diff] [blame] | 755 | case TIFF_FLOAT: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 756 | { |
cristy | a93d496 | 2013-09-29 20:20:01 +0000 | [diff] [blame] | 757 | float |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 758 | floaty; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 759 | |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 760 | floaty=0.0; |
| 761 | if (TIFFGetField(tiff,exif_info[i].tag,&floaty,&sans,&sans) != 0) |
| 762 | (void) FormatLocaleString(value,MaxTextExtent,"%g",(double) floaty); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 763 | break; |
| 764 | } |
cristy | 31c130f | 2013-09-29 20:26:50 +0000 | [diff] [blame] | 765 | case TIFF_DOUBLE: |
| 766 | { |
| 767 | double |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 768 | doubley; |
cristy | 31c130f | 2013-09-29 20:26:50 +0000 | [diff] [blame] | 769 | |
dirk | 18ec356 | 2013-12-21 14:43:07 +0000 | [diff] [blame^] | 770 | doubley=0.0; |
| 771 | if (TIFFGetField(tiff,exif_info[i].tag,&doubley,&sans,&sans) != 0) |
| 772 | (void) FormatLocaleString(value,MaxTextExtent,"%g",doubley); |
cristy | 31c130f | 2013-09-29 20:26:50 +0000 | [diff] [blame] | 773 | break; |
| 774 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 775 | default: |
| 776 | break; |
| 777 | } |
| 778 | if (*value != '\0') |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 779 | (void) SetImageProperty(image,exif_info[i].property,value,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 780 | } |
| 781 | TIFFSetDirectory(tiff,directory); |
| 782 | #else |
| 783 | (void) tiff; |
| 784 | (void) image; |
| 785 | #endif |
| 786 | } |
| 787 | |
| 788 | static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size) |
| 789 | { |
| 790 | *base=(tdata_t *) GetBlobStreamData((Image *) image); |
| 791 | if (*base != (tdata_t *) NULL) |
| 792 | *size=(toff_t) GetBlobSize((Image *) image); |
| 793 | if (*base != (tdata_t *) NULL) |
| 794 | return(1); |
| 795 | return(0); |
| 796 | } |
| 797 | |
| 798 | static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size) |
| 799 | { |
| 800 | tsize_t |
| 801 | count; |
| 802 | |
| 803 | count=(tsize_t) ReadBlob((Image *) image,(size_t) size, |
| 804 | (unsigned char *) data); |
| 805 | return(count); |
| 806 | } |
| 807 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 808 | static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample, |
| 809 | tsample_t sample,ssize_t row,tdata_t scanline) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 810 | { |
| 811 | int32 |
| 812 | status; |
| 813 | |
| 814 | (void) bits_per_sample; |
| 815 | status=TIFFReadScanline(tiff,scanline,(uint32) row,sample); |
| 816 | return(status); |
| 817 | } |
| 818 | |
| 819 | static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence) |
| 820 | { |
| 821 | return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence)); |
| 822 | } |
| 823 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 824 | static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size) |
| 825 | { |
| 826 | (void) image; |
| 827 | (void) base; |
| 828 | (void) size; |
| 829 | } |
| 830 | |
| 831 | static void TIFFWarnings(const char *module,const char *format,va_list warning) |
| 832 | { |
| 833 | char |
| 834 | message[MaxTextExtent]; |
| 835 | |
| 836 | ExceptionInfo |
| 837 | *exception; |
| 838 | |
| 839 | #if defined(MAGICKCORE_HAVE_VSNPRINTF) |
| 840 | (void) vsnprintf(message,MaxTextExtent,format,warning); |
| 841 | #else |
| 842 | (void) vsprintf(message,format,warning); |
| 843 | #endif |
| 844 | (void) ConcatenateMagickString(message,".",MaxTextExtent); |
| 845 | exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception); |
| 846 | if (exception != (ExceptionInfo *) NULL) |
| 847 | (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning, |
| 848 | message,"`%s'",module); |
| 849 | } |
| 850 | |
| 851 | static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size) |
| 852 | { |
| 853 | tsize_t |
| 854 | count; |
| 855 | |
| 856 | count=(tsize_t) WriteBlob((Image *) image,(size_t) size, |
| 857 | (unsigned char *) data); |
| 858 | return(count); |
| 859 | } |
| 860 | |
| 861 | #if defined(__cplusplus) || defined(c_plusplus) |
| 862 | } |
| 863 | #endif |
| 864 | |
| 865 | static Image *ReadTIFFImage(const ImageInfo *image_info, |
| 866 | ExceptionInfo *exception) |
| 867 | { |
| 868 | typedef enum |
| 869 | { |
| 870 | ReadSingleSampleMethod, |
| 871 | ReadRGBAMethod, |
| 872 | ReadCMYKAMethod, |
| 873 | ReadStripMethod, |
| 874 | ReadTileMethod, |
| 875 | ReadGenericMethod |
| 876 | } TIFFMethodType; |
| 877 | |
| 878 | const char |
| 879 | *option; |
| 880 | |
| 881 | float |
| 882 | *chromaticity, |
| 883 | x_position, |
| 884 | y_position, |
| 885 | x_resolution, |
| 886 | y_resolution; |
| 887 | |
| 888 | Image |
| 889 | *image; |
| 890 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 891 | MagickBooleanType |
cristy | 891dc79 | 2010-03-04 01:47:16 +0000 | [diff] [blame] | 892 | associated_alpha, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 893 | debug, |
| 894 | status; |
| 895 | |
| 896 | MagickSizeType |
| 897 | number_pixels; |
| 898 | |
| 899 | QuantumInfo |
| 900 | *quantum_info; |
| 901 | |
| 902 | QuantumType |
| 903 | quantum_type; |
| 904 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 905 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 906 | i; |
| 907 | |
| 908 | size_t |
| 909 | length, |
| 910 | pad; |
| 911 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 912 | ssize_t |
| 913 | y; |
| 914 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 915 | TIFF |
| 916 | *tiff; |
| 917 | |
| 918 | TIFFErrorHandler |
| 919 | error_handler, |
| 920 | warning_handler; |
| 921 | |
| 922 | TIFFMethodType |
| 923 | method; |
| 924 | |
| 925 | uint16 |
| 926 | compress_tag, |
| 927 | bits_per_sample, |
cristy | f2687ca | 2010-06-29 16:32:38 +0000 | [diff] [blame] | 928 | endian, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 929 | extra_samples, |
| 930 | interlace, |
| 931 | max_sample_value, |
| 932 | min_sample_value, |
| 933 | orientation, |
| 934 | pages, |
| 935 | photometric, |
| 936 | *sample_info, |
| 937 | sample_format, |
| 938 | samples_per_pixel, |
| 939 | units, |
| 940 | value; |
| 941 | |
| 942 | uint32 |
| 943 | height, |
| 944 | rows_per_strip, |
| 945 | width; |
| 946 | |
| 947 | unsigned char |
| 948 | *pixels; |
| 949 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 950 | /* |
| 951 | Open image. |
| 952 | */ |
| 953 | assert(image_info != (const ImageInfo *) NULL); |
| 954 | assert(image_info->signature == MagickSignature); |
| 955 | if (image_info->debug != MagickFalse) |
| 956 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 957 | image_info->filename); |
| 958 | assert(exception != (ExceptionInfo *) NULL); |
| 959 | assert(exception->signature == MagickSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 960 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 961 | status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception); |
| 962 | if (status == MagickFalse) |
| 963 | { |
| 964 | image=DestroyImageList(image); |
| 965 | return((Image *) NULL); |
| 966 | } |
| 967 | (void) MagickSetThreadValue(tiff_exception,exception); |
| 968 | error_handler=TIFFSetErrorHandler(TIFFErrors); |
| 969 | warning_handler=TIFFSetWarningHandler(TIFFWarnings); |
cristy | 1630f7f | 2011-02-18 01:10:02 +0000 | [diff] [blame] | 970 | tiff=TIFFClientOpen(image->filename,"rb",(thandle_t) image,TIFFReadBlob, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 971 | TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob, |
| 972 | TIFFUnmapBlob); |
| 973 | if (tiff == (TIFF *) NULL) |
| 974 | { |
| 975 | (void) TIFFSetWarningHandler(warning_handler); |
| 976 | (void) TIFFSetErrorHandler(error_handler); |
| 977 | image=DestroyImageList(image); |
| 978 | return((Image *) NULL); |
| 979 | } |
| 980 | debug=IsEventLogging(); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 981 | (void) debug; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 982 | if (image_info->number_scenes != 0) |
| 983 | { |
| 984 | /* |
| 985 | Generate blank images for subimage specification (e.g. image.tif[4]. |
| 986 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 987 | for (i=0; i < (ssize_t) image_info->scene; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 988 | { |
| 989 | (void) TIFFReadDirectory(tiff); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 990 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 991 | if (GetNextImageInList(image) == (Image *) NULL) |
| 992 | { |
| 993 | image=DestroyImageList(image); |
| 994 | return((Image *) NULL); |
| 995 | } |
| 996 | image=SyncNextImageInList(image); |
| 997 | } |
| 998 | } |
| 999 | do |
| 1000 | { |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 1001 | DisableMSCWarning(4127) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1002 | if (0 && (image_info->verbose != MagickFalse)) |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 1003 | RestoreMSCWarning |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1004 | TIFFPrintDirectory(tiff,stdout,MagickFalse); |
cristy | 3f590e5 | 2011-11-14 14:29:44 +0000 | [diff] [blame] | 1005 | #if defined(MAGICKCORE_HAVE_TIFFISBIGENDIAN) |
cristy | 6b03282 | 2010-06-29 16:52:32 +0000 | [diff] [blame] | 1006 | (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ? |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1007 | "lsb" : "msb",exception); |
cristy | 3f590e5 | 2011-11-14 14:29:44 +0000 | [diff] [blame] | 1008 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1009 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag); |
| 1010 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation); |
| 1011 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width); |
| 1012 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height); |
cristy | f2687ca | 2010-06-29 16:32:38 +0000 | [diff] [blame] | 1013 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1014 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace); |
| 1015 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample); |
| 1016 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format); |
cristy | ae1319c | 2011-01-19 20:18:53 +0000 | [diff] [blame] | 1017 | if (sample_format == SAMPLEFORMAT_IEEEFP) |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1018 | (void) SetImageProperty(image,"quantum:format","floating-point", |
| 1019 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1020 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value); |
| 1021 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value); |
| 1022 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric); |
| 1023 | switch (photometric) |
| 1024 | { |
| 1025 | case PHOTOMETRIC_MINISBLACK: |
| 1026 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1027 | (void) SetImageProperty(image,"tiff:photometric","min-is-black", |
| 1028 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1029 | break; |
| 1030 | } |
| 1031 | case PHOTOMETRIC_MINISWHITE: |
| 1032 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1033 | (void) SetImageProperty(image,"tiff:photometric","min-is-white", |
| 1034 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | } |
| 1037 | case PHOTOMETRIC_PALETTE: |
| 1038 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1039 | (void) SetImageProperty(image,"tiff:photometric","palette",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1040 | break; |
| 1041 | } |
| 1042 | case PHOTOMETRIC_RGB: |
| 1043 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1044 | (void) SetImageProperty(image,"tiff:photometric","RGB",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1045 | break; |
| 1046 | } |
| 1047 | case PHOTOMETRIC_CIELAB: |
| 1048 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1049 | (void) SetImageProperty(image,"tiff:photometric","CIELAB",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1050 | break; |
| 1051 | } |
| 1052 | case PHOTOMETRIC_SEPARATED: |
| 1053 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1054 | (void) SetImageProperty(image,"tiff:photometric","separated",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1055 | break; |
| 1056 | } |
cristy | 79e5dad | 2010-09-16 19:48:33 +0000 | [diff] [blame] | 1057 | case PHOTOMETRIC_YCBCR: |
| 1058 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1059 | (void) SetImageProperty(image,"tiff:photometric","YCBCR",exception); |
cristy | 79e5dad | 2010-09-16 19:48:33 +0000 | [diff] [blame] | 1060 | break; |
| 1061 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1062 | default: |
| 1063 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1064 | (void) SetImageProperty(image,"tiff:photometric","unknown",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1065 | break; |
| 1066 | } |
| 1067 | } |
| 1068 | if (image->debug != MagickFalse) |
| 1069 | { |
| 1070 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u", |
| 1071 | (unsigned int) width,(unsigned int) height); |
| 1072 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u", |
| 1073 | interlace); |
| 1074 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1075 | "Bits per sample: %u",bits_per_sample); |
| 1076 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1077 | "Min sample value: %u",min_sample_value); |
| 1078 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1079 | "Max sample value: %u",max_sample_value); |
| 1080 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric " |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1081 | "interpretation: %s",GetImageProperty(image,"tiff:photometric", |
| 1082 | exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1083 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1084 | image->columns=(size_t) width; |
| 1085 | image->rows=(size_t) height; |
| 1086 | image->depth=(size_t) bits_per_sample; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1087 | if (image->debug != MagickFalse) |
cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 1088 | (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g", |
| 1089 | (double) image->depth); |
cristy | e669ae4 | 2012-02-08 19:07:45 +0000 | [diff] [blame] | 1090 | image->endian=MSBEndian; |
cristy | 50c2b69 | 2012-02-06 14:45:10 +0000 | [diff] [blame] | 1091 | if (endian == FILLORDER_LSB2MSB) |
cristy | e669ae4 | 2012-02-08 19:07:45 +0000 | [diff] [blame] | 1092 | image->endian=LSBEndian; |
cristy | b241f38 | 2012-01-01 01:45:57 +0000 | [diff] [blame] | 1093 | if ((photometric == PHOTOMETRIC_MINISBLACK) || |
| 1094 | (photometric == PHOTOMETRIC_MINISWHITE)) |
cristy | e2c4f18 | 2012-05-12 14:11:53 +0000 | [diff] [blame] | 1095 | SetImageColorspace(image,GRAYColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1096 | if (photometric == PHOTOMETRIC_SEPARATED) |
cristy | e2c4f18 | 2012-05-12 14:11:53 +0000 | [diff] [blame] | 1097 | SetImageColorspace(image,CMYKColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1098 | if (photometric == PHOTOMETRIC_CIELAB) |
cristy | e2c4f18 | 2012-05-12 14:11:53 +0000 | [diff] [blame] | 1099 | SetImageColorspace(image,LabColorspace,exception); |
cristy | e4f023a | 2013-02-27 21:01:50 +0000 | [diff] [blame] | 1100 | TIFFGetProfiles(tiff,image,exception); |
| 1101 | TIFFGetProperties(tiff,image,exception); |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1102 | option=GetImageOption(image_info,"tiff:exif-properties"); |
cristy | e4f023a | 2013-02-27 21:01:50 +0000 | [diff] [blame] | 1103 | if (IfMagickTrue(IsStringNotFalse(option))) /* enabled by default */ |
| 1104 | TIFFGetEXIFProperties(tiff,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1105 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL, |
| 1106 | &samples_per_pixel); |
| 1107 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units); |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1108 | x_resolution=(float) image->resolution.x; |
| 1109 | y_resolution=(float) image->resolution.y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1110 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution); |
| 1111 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution); |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 1112 | image->resolution.x=x_resolution; |
| 1113 | image->resolution.y=y_resolution; |
cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 1114 | x_position=(float) PerceptibleReciprocal(x_resolution)*image->page.x; |
| 1115 | y_position=(float) PerceptibleReciprocal(y_resolution)*image->page.y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1116 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position); |
| 1117 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1118 | image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5); |
| 1119 | image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1120 | image->orientation=(OrientationType) orientation; |
| 1121 | chromaticity=(float *) NULL; |
| 1122 | (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity); |
| 1123 | if (chromaticity != (float *) NULL) |
| 1124 | { |
| 1125 | image->chromaticity.white_point.x=chromaticity[0]; |
| 1126 | image->chromaticity.white_point.y=chromaticity[1]; |
| 1127 | } |
| 1128 | chromaticity=(float *) NULL; |
| 1129 | (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity); |
| 1130 | if (chromaticity != (float *) NULL) |
| 1131 | { |
| 1132 | image->chromaticity.red_primary.x=chromaticity[0]; |
| 1133 | image->chromaticity.red_primary.y=chromaticity[1]; |
| 1134 | image->chromaticity.green_primary.x=chromaticity[2]; |
| 1135 | image->chromaticity.green_primary.y=chromaticity[3]; |
| 1136 | image->chromaticity.blue_primary.x=chromaticity[4]; |
| 1137 | image->chromaticity.blue_primary.y=chromaticity[5]; |
| 1138 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1139 | #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919) |
| 1140 | if ((compress_tag != COMPRESSION_NONE) && |
| 1141 | (TIFFIsCODECConfigured(compress_tag) == 0)) |
| 1142 | { |
| 1143 | TIFFClose(tiff); |
| 1144 | ThrowReaderException(CoderError,"CompressNotSupported"); |
| 1145 | } |
| 1146 | #endif |
| 1147 | switch (compress_tag) |
| 1148 | { |
| 1149 | case COMPRESSION_NONE: image->compression=NoCompression; break; |
| 1150 | case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break; |
| 1151 | case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break; |
| 1152 | case COMPRESSION_JPEG: |
| 1153 | { |
| 1154 | image->compression=JPEGCompression; |
| 1155 | #if defined(JPEG_SUPPORT) |
| 1156 | { |
| 1157 | char |
| 1158 | sampling_factor[MaxTextExtent]; |
| 1159 | |
| 1160 | uint16 |
| 1161 | horizontal, |
| 1162 | vertical; |
| 1163 | |
| 1164 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING, |
| 1165 | &horizontal,&vertical); |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1166 | (void) FormatLocaleString(sampling_factor,MaxTextExtent,"%dx%d", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1167 | horizontal,vertical); |
| 1168 | (void) SetImageProperty(image,"jpeg:sampling-factor", |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1169 | sampling_factor,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1170 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 1171 | "Sampling Factors: %s",sampling_factor); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1172 | } |
| 1173 | #endif |
| 1174 | break; |
| 1175 | } |
| 1176 | case COMPRESSION_OJPEG: image->compression=JPEGCompression; break; |
cristy | fbb0ef0 | 2010-12-19 02:32:11 +0000 | [diff] [blame] | 1177 | #if defined(COMPRESSION_LZMA) |
| 1178 | case COMPRESSION_LZMA: image->compression=LZMACompression; break; |
| 1179 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1180 | case COMPRESSION_LZW: image->compression=LZWCompression; break; |
| 1181 | case COMPRESSION_DEFLATE: image->compression=ZipCompression; break; |
| 1182 | case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break; |
| 1183 | default: image->compression=RLECompression; break; |
| 1184 | } |
cristy | 809fdbd | 2011-12-01 01:26:27 +0000 | [diff] [blame] | 1185 | /* |
| 1186 | Allocate memory for the image and pixel buffer. |
| 1187 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1188 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 1189 | if (quantum_info == (QuantumInfo *) NULL) |
cristy | 8d137bf | 2010-04-21 23:52:04 +0000 | [diff] [blame] | 1190 | { |
| 1191 | TIFFClose(tiff); |
| 1192 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1193 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1194 | if (sample_format == SAMPLEFORMAT_UINT) |
| 1195 | status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat); |
| 1196 | if (sample_format == SAMPLEFORMAT_INT) |
| 1197 | status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat); |
| 1198 | if (sample_format == SAMPLEFORMAT_IEEEFP) |
| 1199 | status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat); |
| 1200 | if (status == MagickFalse) |
cristy | 8d137bf | 2010-04-21 23:52:04 +0000 | [diff] [blame] | 1201 | { |
cristy | a9a6ec4 | 2011-10-26 16:55:01 +0000 | [diff] [blame] | 1202 | quantum_info=DestroyQuantumInfo(quantum_info); |
cristy | 8d137bf | 2010-04-21 23:52:04 +0000 | [diff] [blame] | 1203 | TIFFClose(tiff); |
| 1204 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1205 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1206 | status=MagickTrue; |
| 1207 | switch (photometric) |
| 1208 | { |
| 1209 | case PHOTOMETRIC_MINISBLACK: |
| 1210 | { |
| 1211 | quantum_info->min_is_white=MagickFalse; |
| 1212 | break; |
| 1213 | } |
| 1214 | case PHOTOMETRIC_MINISWHITE: |
| 1215 | { |
| 1216 | quantum_info->min_is_white=MagickTrue; |
| 1217 | break; |
| 1218 | } |
| 1219 | default: |
| 1220 | break; |
| 1221 | } |
cristy | 891dc79 | 2010-03-04 01:47:16 +0000 | [diff] [blame] | 1222 | associated_alpha=MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1223 | extra_samples=0; |
| 1224 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples, |
| 1225 | &sample_info); |
| 1226 | if (extra_samples == 0) |
| 1227 | { |
| 1228 | if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB)) |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1229 | image->alpha_trait=BlendPixelTrait; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1230 | } |
| 1231 | else |
cristy | 6c20734 | 2010-02-19 17:49:50 +0000 | [diff] [blame] | 1232 | for (i=0; i < extra_samples; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1233 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1234 | image->alpha_trait=BlendPixelTrait; |
cristy | 6c20734 | 2010-02-19 17:49:50 +0000 | [diff] [blame] | 1235 | if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA) |
cristy | 891dc79 | 2010-03-04 01:47:16 +0000 | [diff] [blame] | 1236 | SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1237 | } |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 1238 | option=GetImageOption(image_info,"tiff:alpha"); |
cristy | 891dc79 | 2010-03-04 01:47:16 +0000 | [diff] [blame] | 1239 | if (option != (const char *) NULL) |
| 1240 | associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue : |
| 1241 | MagickFalse; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1242 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 891dc79 | 2010-03-04 01:47:16 +0000 | [diff] [blame] | 1243 | (void) SetImageProperty(image,"tiff:alpha", |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1244 | associated_alpha != MagickFalse ? "associated" : "unassociated", |
| 1245 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1246 | if ((photometric == PHOTOMETRIC_PALETTE) && |
| 1247 | (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize)) |
| 1248 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1249 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1250 | colors; |
| 1251 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1252 | colors=(size_t) GetQuantumRange(bits_per_sample)+1; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1253 | if (AcquireImageColormap(image,colors,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1254 | { |
| 1255 | TIFFClose(tiff); |
| 1256 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1257 | } |
| 1258 | } |
| 1259 | if (units == RESUNIT_INCH) |
| 1260 | image->units=PixelsPerInchResolution; |
| 1261 | if (units == RESUNIT_CENTIMETER) |
| 1262 | image->units=PixelsPerCentimeterResolution; |
| 1263 | value=(unsigned short) image->scene; |
| 1264 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages); |
| 1265 | image->scene=value; |
| 1266 | if (image_info->ping != MagickFalse) |
| 1267 | { |
| 1268 | if (image_info->number_scenes != 0) |
| 1269 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 1270 | break; |
| 1271 | goto next_tiff_frame; |
| 1272 | } |
| 1273 | method=ReadGenericMethod; |
| 1274 | if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0) |
| 1275 | { |
| 1276 | char |
| 1277 | value[MaxTextExtent]; |
| 1278 | |
| 1279 | method=ReadStripMethod; |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1280 | (void) FormatLocaleString(value,MaxTextExtent,"%u", |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1281 | (unsigned int) rows_per_strip); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 1282 | (void) SetImageProperty(image,"tiff:rows-per-strip",value,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1283 | } |
| 1284 | if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG)) |
| 1285 | method=ReadRGBAMethod; |
| 1286 | if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE)) |
| 1287 | method=ReadCMYKAMethod; |
| 1288 | if ((photometric != PHOTOMETRIC_RGB) && |
| 1289 | (photometric != PHOTOMETRIC_CIELAB) && |
| 1290 | (photometric != PHOTOMETRIC_SEPARATED)) |
| 1291 | method=ReadGenericMethod; |
| 1292 | if (image->storage_class == PseudoClass) |
| 1293 | method=ReadSingleSampleMethod; |
| 1294 | if ((photometric == PHOTOMETRIC_MINISBLACK) || |
| 1295 | (photometric == PHOTOMETRIC_MINISWHITE)) |
| 1296 | method=ReadSingleSampleMethod; |
| 1297 | if ((photometric != PHOTOMETRIC_SEPARATED) && |
cristy | bc73666 | 2011-03-09 13:39:42 +0000 | [diff] [blame] | 1298 | (interlace == PLANARCONFIG_SEPARATE) && (bits_per_sample < 64)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1299 | method=ReadGenericMethod; |
cristy | 0f4751f | 2013-07-24 13:06:44 +0000 | [diff] [blame] | 1300 | if (image->compression == JPEGCompression) |
| 1301 | method=ReadGenericMethod; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1302 | if (TIFFIsTiled(tiff) != MagickFalse) |
| 1303 | method=ReadTileMethod; |
cristy | 50c2b69 | 2012-02-06 14:45:10 +0000 | [diff] [blame] | 1304 | quantum_info->endian=LSBEndian; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1305 | quantum_type=RGBQuantum; |
| 1306 | pixels=GetQuantumPixels(quantum_info); |
| 1307 | switch (method) |
| 1308 | { |
| 1309 | case ReadSingleSampleMethod: |
| 1310 | { |
| 1311 | /* |
| 1312 | Convert TIFF image to PseudoClass MIFF image. |
| 1313 | */ |
| 1314 | if ((image->storage_class == PseudoClass) && |
| 1315 | (photometric == PHOTOMETRIC_PALETTE)) |
| 1316 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1317 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1318 | range; |
| 1319 | |
| 1320 | uint16 |
| 1321 | *blue_colormap, |
| 1322 | *green_colormap, |
| 1323 | *red_colormap; |
| 1324 | |
| 1325 | /* |
| 1326 | Initialize colormap. |
| 1327 | */ |
cristy | 3b016ea | 2012-08-23 13:58:32 +0000 | [diff] [blame] | 1328 | red_colormap=(uint16 *) NULL; |
| 1329 | green_colormap=(uint16 *) NULL; |
| 1330 | blue_colormap=(uint16 *) NULL; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1331 | (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap, |
| 1332 | &green_colormap,&blue_colormap); |
cristy | 3b016ea | 2012-08-23 13:58:32 +0000 | [diff] [blame] | 1333 | if ((red_colormap != (uint16 *) NULL) && |
| 1334 | (green_colormap != (uint16 *) NULL) && |
| 1335 | (blue_colormap != (uint16 *) NULL)) |
| 1336 | { |
| 1337 | range=255; /* might be old style 8-bit colormap */ |
| 1338 | for (i=0; i < (ssize_t) image->colors; i++) |
| 1339 | if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) || |
| 1340 | (blue_colormap[i] >= 256)) |
| 1341 | { |
| 1342 | range=65535; |
| 1343 | break; |
| 1344 | } |
| 1345 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1346 | { |
cristy | 3b016ea | 2012-08-23 13:58:32 +0000 | [diff] [blame] | 1347 | image->colormap[i].red=ClampToQuantum(((double) |
| 1348 | QuantumRange*red_colormap[i])/range); |
| 1349 | image->colormap[i].green=ClampToQuantum(((double) |
| 1350 | QuantumRange*green_colormap[i])/range); |
| 1351 | image->colormap[i].blue=ClampToQuantum(((double) |
| 1352 | QuantumRange*blue_colormap[i])/range); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1353 | } |
cristy | 3b016ea | 2012-08-23 13:58:32 +0000 | [diff] [blame] | 1354 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1355 | } |
| 1356 | quantum_type=IndexQuantum; |
| 1357 | pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1358 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1359 | { |
| 1360 | if (image->storage_class != PseudoClass) |
| 1361 | { |
| 1362 | quantum_type=samples_per_pixel == 1 ? AlphaQuantum : |
| 1363 | GrayAlphaQuantum; |
| 1364 | pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0); |
| 1365 | } |
| 1366 | else |
| 1367 | { |
| 1368 | quantum_type=IndexAlphaQuantum; |
| 1369 | pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0); |
| 1370 | } |
| 1371 | } |
| 1372 | else |
| 1373 | if (image->storage_class != PseudoClass) |
| 1374 | { |
| 1375 | quantum_type=GrayQuantum; |
| 1376 | pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0); |
| 1377 | } |
| 1378 | status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3)); |
| 1379 | if (status == MagickFalse) |
cristy | 8d137bf | 2010-04-21 23:52:04 +0000 | [diff] [blame] | 1380 | { |
| 1381 | TIFFClose(tiff); |
| 1382 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1383 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1384 | pixels=GetQuantumPixels(quantum_info); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1385 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1386 | { |
| 1387 | int |
| 1388 | status; |
| 1389 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1390 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1391 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1392 | |
| 1393 | status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels); |
| 1394 | if (status == -1) |
| 1395 | break; |
| 1396 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1397 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1398 | break; |
| 1399 | length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 1400 | quantum_type,pixels,exception); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1401 | (void) length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1402 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 1403 | break; |
| 1404 | if (image->previous == (Image *) NULL) |
| 1405 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1406 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 1407 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1408 | if (status == MagickFalse) |
| 1409 | break; |
| 1410 | } |
| 1411 | } |
| 1412 | break; |
| 1413 | } |
| 1414 | case ReadRGBAMethod: |
| 1415 | { |
| 1416 | /* |
| 1417 | Convert TIFF image to DirectClass MIFF image. |
| 1418 | */ |
| 1419 | pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0); |
| 1420 | quantum_type=RGBQuantum; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1421 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1422 | { |
| 1423 | quantum_type=RGBAQuantum; |
| 1424 | pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0); |
| 1425 | } |
| 1426 | if (image->colorspace == CMYKColorspace) |
| 1427 | { |
| 1428 | pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0); |
| 1429 | quantum_type=CMYKQuantum; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1430 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1431 | { |
| 1432 | quantum_type=CMYKAQuantum; |
| 1433 | pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0); |
| 1434 | } |
| 1435 | } |
| 1436 | status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3)); |
| 1437 | if (status == MagickFalse) |
cristy | 8d137bf | 2010-04-21 23:52:04 +0000 | [diff] [blame] | 1438 | { |
| 1439 | TIFFClose(tiff); |
| 1440 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1441 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1442 | pixels=GetQuantumPixels(quantum_info); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1443 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1444 | { |
| 1445 | int |
| 1446 | status; |
| 1447 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1448 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1449 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1450 | |
| 1451 | status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels); |
| 1452 | if (status == -1) |
| 1453 | break; |
| 1454 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1455 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1456 | break; |
cristy | 0b33489 | 2010-03-15 02:26:46 +0000 | [diff] [blame] | 1457 | length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 1458 | quantum_type,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1459 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 1460 | break; |
| 1461 | if (image->previous == (Image *) NULL) |
| 1462 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1463 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 1464 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1465 | if (status == MagickFalse) |
| 1466 | break; |
| 1467 | } |
| 1468 | } |
| 1469 | break; |
| 1470 | } |
| 1471 | case ReadCMYKAMethod: |
| 1472 | { |
| 1473 | /* |
| 1474 | Convert TIFF image to DirectClass MIFF image. |
| 1475 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1476 | for (i=0; i < (ssize_t) samples_per_pixel; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1477 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1478 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1479 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1480 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1481 | *restrict q; |
cristy | 6d9f12f | 2009-11-03 14:50:26 +0000 | [diff] [blame] | 1482 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1483 | int |
| 1484 | status; |
| 1485 | |
| 1486 | status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *) |
| 1487 | pixels); |
| 1488 | if (status == -1) |
| 1489 | break; |
| 1490 | q=GetAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1491 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1492 | break; |
| 1493 | if (image->colorspace != CMYKColorspace) |
| 1494 | switch (i) |
| 1495 | { |
| 1496 | case 0: quantum_type=RedQuantum; break; |
| 1497 | case 1: quantum_type=GreenQuantum; break; |
| 1498 | case 2: quantum_type=BlueQuantum; break; |
| 1499 | case 3: quantum_type=AlphaQuantum; break; |
| 1500 | default: quantum_type=UndefinedQuantum; break; |
| 1501 | } |
cristy | 6d9f12f | 2009-11-03 14:50:26 +0000 | [diff] [blame] | 1502 | else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1503 | switch (i) |
| 1504 | { |
| 1505 | case 0: quantum_type=CyanQuantum; break; |
| 1506 | case 1: quantum_type=MagentaQuantum; break; |
| 1507 | case 2: quantum_type=YellowQuantum; break; |
| 1508 | case 3: quantum_type=BlackQuantum; break; |
| 1509 | case 4: quantum_type=AlphaQuantum; break; |
| 1510 | default: quantum_type=UndefinedQuantum; break; |
| 1511 | } |
| 1512 | length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
| 1513 | quantum_type,pixels,exception); |
| 1514 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 1515 | break; |
| 1516 | } |
| 1517 | if (image->previous == (Image *) NULL) |
| 1518 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1519 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 1520 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1521 | if (status == MagickFalse) |
| 1522 | break; |
| 1523 | } |
| 1524 | } |
| 1525 | break; |
| 1526 | } |
| 1527 | case ReadStripMethod: |
| 1528 | { |
cristy | d8192d3 | 2012-03-14 01:35:47 +0000 | [diff] [blame] | 1529 | register uint32 |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1530 | *p; |
| 1531 | |
| 1532 | /* |
| 1533 | Convert stripped TIFF image to DirectClass MIFF image. |
| 1534 | */ |
| 1535 | i=0; |
cristy | d8192d3 | 2012-03-14 01:35:47 +0000 | [diff] [blame] | 1536 | p=(uint32 *) NULL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1537 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1538 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1539 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1540 | x; |
| 1541 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1542 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1543 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1544 | |
| 1545 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1546 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1547 | break; |
| 1548 | if (i == 0) |
| 1549 | { |
| 1550 | if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0) |
| 1551 | break; |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 1552 | i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t) |
| 1553 | image->rows-y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1554 | } |
| 1555 | i--; |
cristy | d8192d3 | 2012-03-14 01:35:47 +0000 | [diff] [blame] | 1556 | p=((uint32 *) pixels)+image->columns*i; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1557 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1558 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1559 | SetPixelRed(image,ScaleCharToQuantum((unsigned char) |
| 1560 | (TIFFGetR(*p))),q); |
| 1561 | SetPixelGreen(image,ScaleCharToQuantum((unsigned char) |
| 1562 | (TIFFGetG(*p))),q); |
| 1563 | SetPixelBlue(image,ScaleCharToQuantum((unsigned char) |
| 1564 | (TIFFGetB(*p))),q); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1565 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1566 | SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) |
| 1567 | (TIFFGetA(*p))),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1568 | p++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1569 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1570 | } |
| 1571 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 1572 | break; |
| 1573 | if (image->previous == (Image *) NULL) |
| 1574 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1575 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 1576 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1577 | if (status == MagickFalse) |
| 1578 | break; |
| 1579 | } |
| 1580 | } |
| 1581 | break; |
| 1582 | } |
| 1583 | case ReadTileMethod: |
| 1584 | { |
| 1585 | register uint32 |
| 1586 | *p; |
| 1587 | |
| 1588 | uint32 |
| 1589 | *tile_pixels, |
| 1590 | columns, |
| 1591 | rows; |
| 1592 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1593 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1594 | number_pixels; |
| 1595 | |
| 1596 | /* |
| 1597 | Convert tiled TIFF image to DirectClass MIFF image. |
| 1598 | */ |
| 1599 | if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) || |
| 1600 | (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0)) |
| 1601 | { |
| 1602 | TIFFClose(tiff); |
| 1603 | ThrowReaderException(CoderError,"ImageIsNotTiled"); |
| 1604 | } |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1605 | (void) SetImageStorageClass(image,DirectClass,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1606 | number_pixels=columns*rows; |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 1607 | tile_pixels=(uint32 *) AcquireQuantumMemory(number_pixels, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1608 | sizeof(*tile_pixels)); |
| 1609 | if (tile_pixels == (uint32 *) NULL) |
| 1610 | { |
| 1611 | TIFFClose(tiff); |
| 1612 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1613 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1614 | for (y=0; y < (ssize_t) image->rows; y+=rows) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1615 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1616 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1617 | x; |
| 1618 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1619 | register Quantum |
| 1620 | *restrict q, |
| 1621 | *restrict tile; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1622 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1623 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1624 | columns_remaining, |
| 1625 | rows_remaining; |
| 1626 | |
| 1627 | rows_remaining=image->rows-y; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1628 | if ((ssize_t) (y+rows) < (ssize_t) image->rows) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1629 | rows_remaining=rows; |
| 1630 | tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining, |
| 1631 | exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1632 | if (tile == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1633 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1634 | for (x=0; x < (ssize_t) image->columns; x+=columns) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1635 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1636 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1637 | column, |
| 1638 | row; |
| 1639 | |
| 1640 | if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0) |
| 1641 | break; |
| 1642 | columns_remaining=image->columns-x; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1643 | if ((ssize_t) (x+columns) < (ssize_t) image->columns) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1644 | columns_remaining=columns; |
| 1645 | p=tile_pixels+(rows-rows_remaining)*columns; |
cristy | 6847322 | 2011-08-17 17:39:19 +0000 | [diff] [blame] | 1646 | q=tile+GetPixelChannels(image)*(image->columns*(rows_remaining-1)+ |
| 1647 | x); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1648 | for (row=rows_remaining; row > 0; row--) |
| 1649 | { |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1650 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1651 | for (column=columns_remaining; column > 0; column--) |
| 1652 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1653 | SetPixelRed(image,ScaleCharToQuantum((unsigned char) |
| 1654 | TIFFGetR(*p)),q); |
| 1655 | SetPixelGreen(image,ScaleCharToQuantum((unsigned char) |
| 1656 | TIFFGetG(*p)),q); |
| 1657 | SetPixelBlue(image,ScaleCharToQuantum((unsigned char) |
| 1658 | TIFFGetB(*p)),q); |
| 1659 | SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) |
| 1660 | TIFFGetA(*p)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1661 | p++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1662 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1663 | } |
| 1664 | else |
| 1665 | for (column=columns_remaining; column > 0; column--) |
| 1666 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1667 | SetPixelRed(image,ScaleCharToQuantum((unsigned char) |
| 1668 | TIFFGetR(*p)),q); |
| 1669 | SetPixelGreen(image,ScaleCharToQuantum((unsigned char) |
| 1670 | TIFFGetG(*p)),q); |
| 1671 | SetPixelBlue(image,ScaleCharToQuantum((unsigned char) |
| 1672 | TIFFGetB(*p)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1673 | p++; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1674 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1675 | } |
| 1676 | p+=columns-columns_remaining; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1677 | q-=GetPixelChannels(image)*(image->columns+columns_remaining); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 1681 | break; |
| 1682 | if (image->previous == (Image *) NULL) |
| 1683 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1684 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 1685 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1686 | if (status == MagickFalse) |
| 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels); |
| 1691 | break; |
| 1692 | } |
| 1693 | case ReadGenericMethod: |
| 1694 | default: |
| 1695 | { |
cristy | 5def203 | 2013-06-30 17:44:08 +0000 | [diff] [blame] | 1696 | MemoryInfo |
| 1697 | *pixel_info; |
| 1698 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1699 | register uint32 |
| 1700 | *p; |
| 1701 | |
| 1702 | uint32 |
| 1703 | *pixels; |
| 1704 | |
| 1705 | /* |
| 1706 | Convert TIFF image to DirectClass MIFF image. |
| 1707 | */ |
| 1708 | number_pixels=(MagickSizeType) image->columns*image->rows; |
| 1709 | if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t) |
| 1710 | (number_pixels*sizeof(uint32)))) |
| 1711 | { |
| 1712 | TIFFClose(tiff); |
| 1713 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1714 | } |
cristy | 5def203 | 2013-06-30 17:44:08 +0000 | [diff] [blame] | 1715 | pixel_info=AcquireVirtualMemory(image->columns,image->rows* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1716 | sizeof(uint32)); |
cristy | 5def203 | 2013-06-30 17:44:08 +0000 | [diff] [blame] | 1717 | if (pixel_info == (MemoryInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1718 | { |
| 1719 | TIFFClose(tiff); |
| 1720 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 1721 | } |
cristy | 5def203 | 2013-06-30 17:44:08 +0000 | [diff] [blame] | 1722 | pixels=(uint32 *) GetVirtualMemoryBlob(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1723 | (void) TIFFReadRGBAImage(tiff,(uint32) image->columns, |
| 1724 | (uint32) image->rows,(uint32 *) pixels,0); |
| 1725 | /* |
| 1726 | Convert image to DirectClass pixel packets. |
| 1727 | */ |
| 1728 | p=pixels+number_pixels-1; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1729 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1730 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1731 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1732 | x; |
| 1733 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1734 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1735 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1736 | |
| 1737 | q=QueueAuthenticPixels(image,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1738 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1739 | break; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1740 | q+=GetPixelChannels(image)*(image->columns-1); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1741 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1742 | { |
cristy | 5def203 | 2013-06-30 17:44:08 +0000 | [diff] [blame] | 1743 | SetPixelRed(image,ScaleCharToQuantum((unsigned char) TIFFGetR(*p)), |
| 1744 | q); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1745 | SetPixelGreen(image,ScaleCharToQuantum((unsigned char) |
| 1746 | TIFFGetG(*p)),q); |
cristy | 5def203 | 2013-06-30 17:44:08 +0000 | [diff] [blame] | 1747 | SetPixelBlue(image,ScaleCharToQuantum((unsigned char) TIFFGetB(*p)), |
| 1748 | q); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 1749 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1750 | SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) |
| 1751 | TIFFGetA(*p)),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1752 | p--; |
cristy | 128e5be | 2013-11-09 02:47:12 +0000 | [diff] [blame] | 1753 | q-=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1754 | } |
| 1755 | if (SyncAuthenticPixels(image,exception) == MagickFalse) |
| 1756 | break; |
| 1757 | if (image->previous == (Image *) NULL) |
| 1758 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1759 | status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y, |
| 1760 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1761 | if (status == MagickFalse) |
| 1762 | break; |
| 1763 | } |
| 1764 | } |
cristy | 5def203 | 2013-06-30 17:44:08 +0000 | [diff] [blame] | 1765 | pixel_info=RelinquishVirtualMemory(pixel_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1766 | break; |
| 1767 | } |
| 1768 | } |
| 1769 | SetQuantumImageType(image,quantum_type); |
| 1770 | next_tiff_frame: |
cristy | a9a6ec4 | 2011-10-26 16:55:01 +0000 | [diff] [blame] | 1771 | quantum_info=DestroyQuantumInfo(quantum_info); |
cristy | f2273ac | 2012-06-30 22:45:42 +0000 | [diff] [blame] | 1772 | if (photometric == PHOTOMETRIC_CIELAB) |
cristy | 514da94 | 2012-07-06 23:24:14 +0000 | [diff] [blame] | 1773 | DecodeLabImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1774 | if ((photometric == PHOTOMETRIC_LOGL) || |
| 1775 | (photometric == PHOTOMETRIC_MINISBLACK) || |
| 1776 | (photometric == PHOTOMETRIC_MINISWHITE)) |
| 1777 | { |
| 1778 | image->type=GrayscaleType; |
| 1779 | if (bits_per_sample == 1) |
| 1780 | image->type=BilevelType; |
| 1781 | } |
| 1782 | if (image->storage_class == PseudoClass) |
| 1783 | image->depth=GetImageDepth(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1784 | /* |
| 1785 | Proceed to next image. |
| 1786 | */ |
| 1787 | if (image_info->number_scenes != 0) |
| 1788 | if (image->scene >= (image_info->scene+image_info->number_scenes-1)) |
| 1789 | break; |
| 1790 | status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse; |
| 1791 | if (status == MagickTrue) |
| 1792 | { |
| 1793 | /* |
| 1794 | Allocate next image structure. |
| 1795 | */ |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 1796 | AcquireNextImage(image_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1797 | if (GetNextImageInList(image) == (Image *) NULL) |
| 1798 | { |
| 1799 | image=DestroyImageList(image); |
| 1800 | return((Image *) NULL); |
| 1801 | } |
| 1802 | image=SyncNextImageInList(image); |
| 1803 | status=SetImageProgress(image,LoadImagesTag,image->scene-1, |
| 1804 | image->scene); |
| 1805 | if (status == MagickFalse) |
| 1806 | break; |
| 1807 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1808 | } while (status == MagickTrue); |
| 1809 | (void) TIFFSetWarningHandler(warning_handler); |
| 1810 | (void) TIFFSetErrorHandler(error_handler); |
| 1811 | TIFFClose(tiff); |
| 1812 | return(GetFirstImageInList(image)); |
| 1813 | } |
| 1814 | #endif |
| 1815 | |
| 1816 | /* |
| 1817 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1818 | % % |
| 1819 | % % |
| 1820 | % % |
| 1821 | % R e g i s t e r T I F F I m a g e % |
| 1822 | % % |
| 1823 | % % |
| 1824 | % % |
| 1825 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1826 | % |
| 1827 | % RegisterTIFFImage() adds properties for the TIFF image format to |
| 1828 | % the list of supported formats. The properties include the image format |
| 1829 | % tag, a method to read and/or write the format, whether the format |
| 1830 | % supports the saving of more than one frame to the same file or blob, |
| 1831 | % whether the format supports native in-memory I/O, and a brief |
| 1832 | % description of the format. |
| 1833 | % |
| 1834 | % The format of the RegisterTIFFImage method is: |
| 1835 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1836 | % size_t RegisterTIFFImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1837 | % |
| 1838 | */ |
cristy | 7e3eb0a | 2011-11-09 22:50:49 +0000 | [diff] [blame] | 1839 | |
| 1840 | #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER) |
| 1841 | static TIFFExtendProc |
cristy | 60e1bf9 | 2011-11-09 23:00:15 +0000 | [diff] [blame] | 1842 | tag_extender = (TIFFExtendProc) NULL; |
cristy | 7e3eb0a | 2011-11-09 22:50:49 +0000 | [diff] [blame] | 1843 | |
| 1844 | static void TIFFTagExtender(TIFF *tiff) |
| 1845 | { |
| 1846 | static const TIFFFieldInfo |
| 1847 | TIFFExtensions[] = |
| 1848 | { |
cristy | 504a72b | 2012-10-04 12:56:53 +0000 | [diff] [blame] | 1849 | { 37724, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1, |
| 1850 | (char *) "PhotoshopLayerData" }, |
| 1851 | { 34118, -3, -3, TIFF_UNDEFINED, FIELD_CUSTOM, 1, 1, |
| 1852 | (char *) "Microscope" } |
cristy | 7e3eb0a | 2011-11-09 22:50:49 +0000 | [diff] [blame] | 1853 | }; |
| 1854 | |
| 1855 | TIFFMergeFieldInfo(tiff,TIFFExtensions,sizeof(TIFFExtensions)/ |
| 1856 | sizeof(*TIFFExtensions)); |
cristy | 60e1bf9 | 2011-11-09 23:00:15 +0000 | [diff] [blame] | 1857 | if (tag_extender != (TIFFExtendProc) NULL) |
| 1858 | (*tag_extender)(tiff); |
cristy | 7e3eb0a | 2011-11-09 22:50:49 +0000 | [diff] [blame] | 1859 | } |
| 1860 | #endif |
| 1861 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1862 | ModuleExport size_t RegisterTIFFImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1863 | { |
| 1864 | #define TIFFDescription "Tagged Image File Format" |
| 1865 | |
| 1866 | char |
| 1867 | version[MaxTextExtent]; |
| 1868 | |
| 1869 | MagickInfo |
| 1870 | *entry; |
cristy | 6b03282 | 2010-06-29 16:52:32 +0000 | [diff] [blame] | 1871 | |
cristy | 18b1744 | 2009-10-25 18:36:48 +0000 | [diff] [blame] | 1872 | if (tiff_semaphore == (SemaphoreInfo *) NULL) |
| 1873 | tiff_semaphore=AllocateSemaphoreInfo(); |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 1874 | LockSemaphoreInfo(tiff_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1875 | if (instantiate_key == MagickFalse) |
| 1876 | { |
| 1877 | if (MagickCreateThreadKey(&tiff_exception) == MagickFalse) |
| 1878 | ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed"); |
cristy | 7e3eb0a | 2011-11-09 22:50:49 +0000 | [diff] [blame] | 1879 | #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER) |
cristy | 60e1bf9 | 2011-11-09 23:00:15 +0000 | [diff] [blame] | 1880 | if (tag_extender == (TIFFExtendProc) NULL) |
| 1881 | tag_extender=TIFFSetTagExtender(TIFFTagExtender); |
cristy | 7e3eb0a | 2011-11-09 22:50:49 +0000 | [diff] [blame] | 1882 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1883 | instantiate_key=MagickTrue; |
| 1884 | } |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 1885 | UnlockSemaphoreInfo(tiff_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1886 | *version='\0'; |
| 1887 | #if defined(TIFF_VERSION) |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 1888 | (void) FormatLocaleString(version,MaxTextExtent,"%d",TIFF_VERSION); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1889 | #endif |
| 1890 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
| 1891 | { |
| 1892 | const char |
| 1893 | *p; |
| 1894 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1895 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1896 | i; |
| 1897 | |
| 1898 | p=TIFFGetVersion(); |
| 1899 | for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++) |
| 1900 | version[i]=(*p++); |
| 1901 | version[i]='\0'; |
| 1902 | } |
| 1903 | #endif |
| 1904 | |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 1905 | entry=SetMagickInfo("GROUP4"); |
| 1906 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
| 1907 | entry->decoder=(DecodeImageHandler *) ReadGROUP4Image; |
| 1908 | entry->encoder=(EncodeImageHandler *) WriteGROUP4Image; |
| 1909 | #endif |
| 1910 | entry->raw=MagickTrue; |
| 1911 | entry->endian_support=MagickTrue; |
cristy | 2d6ccc3 | 2009-09-25 03:18:25 +0000 | [diff] [blame] | 1912 | entry->adjoin=MagickFalse; |
cristy | 38a69f1 | 2010-07-26 01:03:12 +0000 | [diff] [blame] | 1913 | entry->format_type=ImplicitFormatType; |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 1914 | entry->seekable_stream=MagickTrue; |
| 1915 | entry->thread_support=NoThreadSupport; |
| 1916 | entry->description=ConstantString("Raw CCITT Group4"); |
cristy | 9d97456 | 2013-08-09 11:51:49 +0000 | [diff] [blame] | 1917 | entry->mime_type=ConstantString("image/tiff"); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 1918 | entry->module=ConstantString("TIFF"); |
| 1919 | (void) RegisterMagickInfo(entry); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1920 | entry=SetMagickInfo("PTIF"); |
| 1921 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
| 1922 | entry->decoder=(DecodeImageHandler *) ReadTIFFImage; |
| 1923 | entry->encoder=(EncodeImageHandler *) WritePTIFImage; |
| 1924 | #endif |
| 1925 | entry->endian_support=MagickTrue; |
| 1926 | entry->seekable_stream=MagickTrue; |
| 1927 | entry->thread_support=NoThreadSupport; |
| 1928 | entry->description=ConstantString("Pyramid encoded TIFF"); |
cristy | 9d97456 | 2013-08-09 11:51:49 +0000 | [diff] [blame] | 1929 | entry->mime_type=ConstantString("image/tiff"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1930 | entry->module=ConstantString("TIFF"); |
| 1931 | (void) RegisterMagickInfo(entry); |
| 1932 | entry=SetMagickInfo("TIF"); |
| 1933 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
| 1934 | entry->decoder=(DecodeImageHandler *) ReadTIFFImage; |
| 1935 | entry->encoder=(EncodeImageHandler *) WriteTIFFImage; |
| 1936 | #endif |
| 1937 | entry->endian_support=MagickTrue; |
| 1938 | entry->seekable_stream=MagickTrue; |
| 1939 | entry->stealth=MagickTrue; |
| 1940 | entry->thread_support=NoThreadSupport; |
| 1941 | entry->description=ConstantString(TIFFDescription); |
| 1942 | if (*version != '\0') |
| 1943 | entry->version=ConstantString(version); |
cristy | 9d97456 | 2013-08-09 11:51:49 +0000 | [diff] [blame] | 1944 | entry->mime_type=ConstantString("image/tiff"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1945 | entry->module=ConstantString("TIFF"); |
| 1946 | (void) RegisterMagickInfo(entry); |
| 1947 | entry=SetMagickInfo("TIFF"); |
| 1948 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
| 1949 | entry->decoder=(DecodeImageHandler *) ReadTIFFImage; |
| 1950 | entry->encoder=(EncodeImageHandler *) WriteTIFFImage; |
| 1951 | #endif |
| 1952 | entry->magick=(IsImageFormatHandler *) IsTIFF; |
| 1953 | entry->endian_support=MagickTrue; |
| 1954 | entry->seekable_stream=MagickTrue; |
| 1955 | entry->thread_support=NoThreadSupport; |
| 1956 | entry->description=ConstantString(TIFFDescription); |
| 1957 | if (*version != '\0') |
| 1958 | entry->version=ConstantString(version); |
cristy | 9d97456 | 2013-08-09 11:51:49 +0000 | [diff] [blame] | 1959 | entry->mime_type=ConstantString("image/tiff"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1960 | entry->module=ConstantString("TIFF"); |
| 1961 | (void) RegisterMagickInfo(entry); |
| 1962 | entry=SetMagickInfo("TIFF64"); |
| 1963 | #if defined(TIFF_VERSION_BIG) |
| 1964 | entry->decoder=(DecodeImageHandler *) ReadTIFFImage; |
cristy | 9d6964e | 2011-11-10 17:48:45 +0000 | [diff] [blame] | 1965 | entry->encoder=(EncodeImageHandler *) WriteTIFFImage; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1966 | #endif |
| 1967 | entry->adjoin=MagickFalse; |
| 1968 | entry->endian_support=MagickTrue; |
| 1969 | entry->seekable_stream=MagickTrue; |
| 1970 | entry->thread_support=NoThreadSupport; |
| 1971 | entry->description=ConstantString("Tagged Image File Format (64-bit)"); |
| 1972 | if (*version != '\0') |
| 1973 | entry->version=ConstantString(version); |
cristy | 9d97456 | 2013-08-09 11:51:49 +0000 | [diff] [blame] | 1974 | entry->mime_type=ConstantString("image/tiff"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1975 | entry->module=ConstantString("TIFF"); |
| 1976 | (void) RegisterMagickInfo(entry); |
| 1977 | return(MagickImageCoderSignature); |
| 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1982 | % % |
| 1983 | % % |
| 1984 | % % |
| 1985 | % U n r e g i s t e r T I F F I m a g e % |
| 1986 | % % |
| 1987 | % % |
| 1988 | % % |
| 1989 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1990 | % |
| 1991 | % UnregisterTIFFImage() removes format registrations made by the TIFF module |
| 1992 | % from the list of supported formats. |
| 1993 | % |
| 1994 | % The format of the UnregisterTIFFImage method is: |
| 1995 | % |
| 1996 | % UnregisterTIFFImage(void) |
| 1997 | % |
| 1998 | */ |
| 1999 | ModuleExport void UnregisterTIFFImage(void) |
| 2000 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2001 | (void) UnregisterMagickInfo("TIFF64"); |
cristy | 9d6964e | 2011-11-10 17:48:45 +0000 | [diff] [blame] | 2002 | (void) UnregisterMagickInfo("TIFF"); |
| 2003 | (void) UnregisterMagickInfo("TIF"); |
| 2004 | (void) UnregisterMagickInfo("PTIF"); |
cristy | 514e9e7 | 2009-11-20 02:12:08 +0000 | [diff] [blame] | 2005 | if (tiff_semaphore == (SemaphoreInfo *) NULL) |
| 2006 | tiff_semaphore=AllocateSemaphoreInfo(); |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 2007 | LockSemaphoreInfo(tiff_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2008 | if (instantiate_key != MagickFalse) |
cristy | 9ebdd93 | 2011-11-09 23:07:28 +0000 | [diff] [blame] | 2009 | { |
| 2010 | #if defined(MAGICKCORE_HAVE_TIFFMERGEFIELDINFO) && defined(MAGICKCORE_HAVE_TIFFSETTAGEXTENDER) |
| 2011 | if (tag_extender == (TIFFExtendProc) NULL) |
| 2012 | (void) TIFFSetTagExtender(tag_extender); |
| 2013 | #endif |
| 2014 | if (MagickDeleteThreadKey(tiff_exception) == MagickFalse) |
| 2015 | ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed"); |
| 2016 | instantiate_key=MagickFalse; |
| 2017 | } |
cristy | f84a193 | 2010-01-03 18:00:18 +0000 | [diff] [blame] | 2018 | UnlockSemaphoreInfo(tiff_semaphore); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2019 | DestroySemaphoreInfo(&tiff_semaphore); |
| 2020 | } |
| 2021 | |
| 2022 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
| 2023 | /* |
| 2024 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2025 | % % |
| 2026 | % % |
| 2027 | % % |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2028 | % W r i t e G R O U P 4 I m a g e % |
| 2029 | % % |
| 2030 | % % |
| 2031 | % % |
| 2032 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2033 | % |
| 2034 | % WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format. |
| 2035 | % |
| 2036 | % The format of the WriteGROUP4Image method is: |
| 2037 | % |
| 2038 | % MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2039 | % Image *image,ExceptionInfo *) |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2040 | % |
| 2041 | % A description of each parameter follows: |
| 2042 | % |
| 2043 | % o image_info: the image info. |
| 2044 | % |
| 2045 | % o image: The image. |
| 2046 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2047 | % o exception: return any errors or warnings in this structure. |
| 2048 | % |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2049 | */ |
| 2050 | static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2051 | Image *image,ExceptionInfo *exception) |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2052 | { |
| 2053 | char |
| 2054 | filename[MaxTextExtent]; |
| 2055 | |
| 2056 | FILE |
| 2057 | *file; |
| 2058 | |
| 2059 | Image |
| 2060 | *huffman_image; |
| 2061 | |
| 2062 | ImageInfo |
| 2063 | *write_info; |
| 2064 | |
| 2065 | int |
| 2066 | unique_file; |
| 2067 | |
| 2068 | MagickBooleanType |
| 2069 | status; |
| 2070 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2071 | register ssize_t |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2072 | i; |
| 2073 | |
| 2074 | ssize_t |
| 2075 | count; |
| 2076 | |
| 2077 | TIFF |
| 2078 | *tiff; |
| 2079 | |
cristy | 94c8fe4 | 2009-10-06 01:57:36 +0000 | [diff] [blame] | 2080 | toff_t |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2081 | *byte_count, |
| 2082 | strip_size; |
| 2083 | |
| 2084 | unsigned char |
| 2085 | *buffer; |
| 2086 | |
| 2087 | /* |
| 2088 | Write image as CCITT Group4 TIFF image to a temporary file. |
| 2089 | */ |
| 2090 | assert(image_info != (const ImageInfo *) NULL); |
| 2091 | assert(image_info->signature == MagickSignature); |
| 2092 | assert(image != (Image *) NULL); |
| 2093 | assert(image->signature == MagickSignature); |
| 2094 | if (image->debug != MagickFalse) |
| 2095 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2096 | assert(exception != (ExceptionInfo *) NULL); |
| 2097 | assert(exception->signature == MagickSignature); |
| 2098 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2099 | if (status == MagickFalse) |
| 2100 | return(status); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2101 | huffman_image=CloneImage(image,0,0,MagickTrue,exception); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2102 | if (huffman_image == (Image *) NULL) |
| 2103 | { |
| 2104 | (void) CloseBlob(image); |
| 2105 | return(MagickFalse); |
| 2106 | } |
cristy | 94c8fe4 | 2009-10-06 01:57:36 +0000 | [diff] [blame] | 2107 | huffman_image->endian=MSBEndian; |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2108 | file=(FILE *) NULL; |
| 2109 | unique_file=AcquireUniqueFileResource(filename); |
| 2110 | if (unique_file != -1) |
cristy | 0accf6e | 2009-10-01 13:14:28 +0000 | [diff] [blame] | 2111 | file=fdopen(unique_file,"wb"); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2112 | if ((unique_file == -1) || (file == (FILE *) NULL)) |
| 2113 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2114 | ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile", |
| 2115 | filename); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2116 | return(MagickFalse); |
| 2117 | } |
cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 2118 | (void) FormatLocaleString(huffman_image->filename,MaxTextExtent,"tiff:%s", |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2119 | filename); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2120 | (void) SetImageType(huffman_image,BilevelType,exception); |
cristy | 14efd99 | 2009-09-26 23:46:03 +0000 | [diff] [blame] | 2121 | write_info=CloneImageInfo((ImageInfo *) NULL); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2122 | SetImageInfoFile(write_info,file); |
| 2123 | write_info->compression=Group4Compression; |
| 2124 | write_info->type=BilevelType; |
| 2125 | (void) SetImageOption(write_info,"quantum:polarity","min-is-white"); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2126 | status=WriteTIFFImage(write_info,huffman_image,exception); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2127 | (void) fflush(file); |
| 2128 | write_info=DestroyImageInfo(write_info); |
| 2129 | if (status == MagickFalse) |
| 2130 | { |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2131 | huffman_image=DestroyImage(huffman_image); |
| 2132 | (void) fclose(file); |
| 2133 | (void) RelinquishUniqueFileResource(filename); |
| 2134 | return(MagickFalse); |
| 2135 | } |
| 2136 | tiff=TIFFOpen(filename,"rb"); |
| 2137 | if (tiff == (TIFF *) NULL) |
| 2138 | { |
| 2139 | huffman_image=DestroyImage(huffman_image); |
| 2140 | (void) fclose(file); |
| 2141 | (void) RelinquishUniqueFileResource(filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2142 | ThrowFileException(exception,FileOpenError,"UnableToOpenFile", |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2143 | image_info->filename); |
| 2144 | return(MagickFalse); |
| 2145 | } |
| 2146 | /* |
| 2147 | Allocate raw strip buffer. |
| 2148 | */ |
cristy | 94c8fe4 | 2009-10-06 01:57:36 +0000 | [diff] [blame] | 2149 | if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1) |
| 2150 | { |
| 2151 | TIFFClose(tiff); |
| 2152 | huffman_image=DestroyImage(huffman_image); |
| 2153 | (void) fclose(file); |
| 2154 | (void) RelinquishUniqueFileResource(filename); |
| 2155 | return(MagickFalse); |
| 2156 | } |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2157 | strip_size=byte_count[0]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2158 | for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++) |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2159 | if (byte_count[i] > strip_size) |
| 2160 | strip_size=byte_count[i]; |
| 2161 | buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size, |
| 2162 | sizeof(*buffer)); |
| 2163 | if (buffer == (unsigned char *) NULL) |
| 2164 | { |
| 2165 | TIFFClose(tiff); |
| 2166 | huffman_image=DestroyImage(huffman_image); |
| 2167 | (void) fclose(file); |
| 2168 | (void) RelinquishUniqueFileResource(filename); |
| 2169 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 2170 | image_info->filename); |
| 2171 | } |
| 2172 | /* |
| 2173 | Compress runlength encoded to 2D Huffman pixels. |
| 2174 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2175 | for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++) |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2176 | { |
cristy | 94c8fe4 | 2009-10-06 01:57:36 +0000 | [diff] [blame] | 2177 | count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size); |
cristy | 3d7f806 | 2009-09-24 20:45:35 +0000 | [diff] [blame] | 2178 | if (WriteBlob(image,(size_t) count,buffer) != count) |
| 2179 | status=MagickFalse; |
| 2180 | } |
| 2181 | buffer=(unsigned char *) RelinquishMagickMemory(buffer); |
| 2182 | TIFFClose(tiff); |
| 2183 | huffman_image=DestroyImage(huffman_image); |
| 2184 | (void) fclose(file); |
| 2185 | (void) RelinquishUniqueFileResource(filename); |
| 2186 | (void) CloseBlob(image); |
| 2187 | return(status); |
| 2188 | } |
| 2189 | #endif |
| 2190 | |
| 2191 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
| 2192 | /* |
| 2193 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2194 | % % |
| 2195 | % % |
| 2196 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2197 | % W r i t e P T I F I m a g e % |
| 2198 | % % |
| 2199 | % % |
| 2200 | % % |
| 2201 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2202 | % |
| 2203 | % WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file |
| 2204 | % format. |
| 2205 | % |
| 2206 | % The format of the WritePTIFImage method is: |
| 2207 | % |
| 2208 | % MagickBooleanType WritePTIFImage(const ImageInfo *image_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2209 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2210 | % |
| 2211 | % A description of each parameter follows: |
| 2212 | % |
| 2213 | % o image_info: the image info. |
| 2214 | % |
| 2215 | % o image: The image. |
| 2216 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2217 | % o exception: return any errors or warnings in this structure. |
| 2218 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2219 | */ |
| 2220 | static MagickBooleanType WritePTIFImage(const ImageInfo *image_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2221 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2222 | { |
| 2223 | Image |
| 2224 | *images, |
| 2225 | *next, |
| 2226 | *pyramid_image; |
| 2227 | |
| 2228 | ImageInfo |
| 2229 | *write_info; |
| 2230 | |
| 2231 | MagickBooleanType |
| 2232 | status; |
| 2233 | |
cristy | f164a87 | 2012-01-20 18:26:34 +0000 | [diff] [blame] | 2234 | PointInfo |
| 2235 | resolution; |
| 2236 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2237 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2238 | columns, |
| 2239 | rows; |
| 2240 | |
| 2241 | /* |
| 2242 | Create pyramid-encoded TIFF image. |
| 2243 | */ |
| 2244 | images=NewImageList(); |
| 2245 | for (next=image; next != (Image *) NULL; next=GetNextImageInList(next)) |
| 2246 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2247 | AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2248 | columns=next->columns; |
| 2249 | rows=next->rows; |
cristy | f164a87 | 2012-01-20 18:26:34 +0000 | [diff] [blame] | 2250 | resolution=next->resolution; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2251 | while ((columns > 64) && (rows > 64)) |
| 2252 | { |
| 2253 | columns/=2; |
| 2254 | rows/=2; |
cristy | f164a87 | 2012-01-20 18:26:34 +0000 | [diff] [blame] | 2255 | resolution.x/=2; |
| 2256 | resolution.y/=2; |
cristy | aa2c16c | 2012-03-25 22:21:35 +0000 | [diff] [blame] | 2257 | pyramid_image=ResizeImage(next,columns,rows,image->filter,exception); |
cristy | f164a87 | 2012-01-20 18:26:34 +0000 | [diff] [blame] | 2258 | if (pyramid_image == (Image *) NULL) |
| 2259 | break; |
| 2260 | pyramid_image->resolution=resolution; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2261 | AppendImageToList(&images,pyramid_image); |
| 2262 | } |
| 2263 | } |
cristy | f164a87 | 2012-01-20 18:26:34 +0000 | [diff] [blame] | 2264 | images=GetFirstImageInList(images); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2265 | /* |
| 2266 | Write pyramid-encoded TIFF image. |
| 2267 | */ |
cristy | 5856753 | 2009-09-26 01:13:32 +0000 | [diff] [blame] | 2268 | write_info=CloneImageInfo(image_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2269 | write_info->adjoin=MagickTrue; |
cristy | f164a87 | 2012-01-20 18:26:34 +0000 | [diff] [blame] | 2270 | (void) CopyMagickString(write_info->magick,"TIFF",MaxTextExtent); |
| 2271 | (void) CopyMagickString(images->magick,"TIFF",MaxTextExtent); |
| 2272 | status=WriteTIFFImage(write_info,images,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2273 | images=DestroyImageList(images); |
| 2274 | write_info=DestroyImageInfo(write_info); |
| 2275 | return(status); |
| 2276 | } |
| 2277 | #endif |
| 2278 | |
| 2279 | #if defined(MAGICKCORE_TIFF_DELEGATE) |
| 2280 | /* |
| 2281 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2282 | % % |
| 2283 | % % |
| 2284 | % W r i t e T I F F I m a g e % |
| 2285 | % % |
| 2286 | % % |
| 2287 | % % |
| 2288 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2289 | % |
| 2290 | % WriteTIFFImage() writes an image in the Tagged image file format. |
| 2291 | % |
| 2292 | % The format of the WriteTIFFImage method is: |
| 2293 | % |
| 2294 | % MagickBooleanType WriteTIFFImage(const ImageInfo *image_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2295 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2296 | % |
| 2297 | % A description of each parameter follows: |
| 2298 | % |
| 2299 | % o image_info: the image info. |
| 2300 | % |
| 2301 | % o image: The image. |
| 2302 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2303 | % o exception: return any errors or warnings in this structure. |
| 2304 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2305 | */ |
| 2306 | |
| 2307 | typedef struct _TIFFInfo |
| 2308 | { |
| 2309 | RectangleInfo |
| 2310 | tile_geometry; |
| 2311 | |
| 2312 | unsigned char |
| 2313 | *scanline, |
| 2314 | *scanlines, |
| 2315 | *pixels; |
| 2316 | } TIFFInfo; |
| 2317 | |
| 2318 | static void DestroyTIFFInfo(TIFFInfo *tiff_info) |
| 2319 | { |
| 2320 | assert(tiff_info != (TIFFInfo *) NULL); |
| 2321 | if (tiff_info->scanlines != (unsigned char *) NULL) |
| 2322 | tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory( |
| 2323 | tiff_info->scanlines); |
| 2324 | if (tiff_info->pixels != (unsigned char *) NULL) |
| 2325 | tiff_info->pixels=(unsigned char *) RelinquishMagickMemory( |
| 2326 | tiff_info->pixels); |
| 2327 | } |
| 2328 | |
cristy | 514da94 | 2012-07-06 23:24:14 +0000 | [diff] [blame] | 2329 | static MagickBooleanType EncodeLabImage(Image *image,ExceptionInfo *exception) |
| 2330 | { |
| 2331 | CacheView |
| 2332 | *image_view; |
| 2333 | |
| 2334 | MagickBooleanType |
| 2335 | status; |
| 2336 | |
| 2337 | ssize_t |
| 2338 | y; |
| 2339 | |
| 2340 | status=MagickTrue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 2341 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | 514da94 | 2012-07-06 23:24:14 +0000 | [diff] [blame] | 2342 | for (y=0; y < (ssize_t) image->rows; y++) |
| 2343 | { |
| 2344 | register Quantum |
| 2345 | *restrict q; |
| 2346 | |
| 2347 | register ssize_t |
| 2348 | x; |
| 2349 | |
| 2350 | if (status == MagickFalse) |
| 2351 | continue; |
| 2352 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 2353 | if (q == (Quantum *) NULL) |
| 2354 | { |
| 2355 | status=MagickFalse; |
| 2356 | continue; |
| 2357 | } |
| 2358 | for (x=0; x < (ssize_t) image->columns; x++) |
| 2359 | { |
| 2360 | double |
| 2361 | a, |
| 2362 | b; |
| 2363 | |
| 2364 | a=QuantumScale*GetPixela(image,q)-0.5; |
| 2365 | if (a < 0.0) |
| 2366 | a+=1.0; |
| 2367 | b=QuantumScale*GetPixelb(image,q)-0.5; |
| 2368 | if (b < 0.0) |
| 2369 | b+=1.0; |
| 2370 | SetPixela(image,QuantumRange*a,q); |
| 2371 | SetPixelb(image,QuantumRange*b,q); |
| 2372 | q+=GetPixelChannels(image); |
| 2373 | } |
| 2374 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 2375 | status=MagickFalse; |
| 2376 | } |
| 2377 | image_view=DestroyCacheView(image_view); |
| 2378 | return(status); |
| 2379 | } |
| 2380 | |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 2381 | static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info, |
cristy | b0de93f | 2013-05-03 13:39:25 +0000 | [diff] [blame] | 2382 | TIFF *tiff,TIFFInfo *tiff_info) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2383 | { |
| 2384 | const char |
| 2385 | *option; |
| 2386 | |
| 2387 | MagickStatusType |
| 2388 | flags; |
| 2389 | |
cristy | 79e5dad | 2010-09-16 19:48:33 +0000 | [diff] [blame] | 2390 | uint32 |
| 2391 | tile_columns, |
| 2392 | tile_rows; |
| 2393 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2394 | assert(tiff_info != (TIFFInfo *) NULL); |
| 2395 | (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info)); |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 2396 | option=GetImageOption(image_info,"tiff:tile-geometry"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2397 | if (option == (const char *) NULL) |
| 2398 | return(MagickTrue); |
| 2399 | flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry); |
| 2400 | if ((flags & HeightValue) == 0) |
| 2401 | tiff_info->tile_geometry.height=tiff_info->tile_geometry.width; |
cristy | df6d6d4 | 2010-09-18 02:15:37 +0000 | [diff] [blame] | 2402 | tile_columns=(uint32) tiff_info->tile_geometry.width; |
| 2403 | tile_rows=(uint32) tiff_info->tile_geometry.height; |
cristy | 79e5dad | 2010-09-16 19:48:33 +0000 | [diff] [blame] | 2404 | TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows); |
| 2405 | (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns); |
| 2406 | (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows); |
| 2407 | tiff_info->tile_geometry.width=tile_columns; |
| 2408 | tiff_info->tile_geometry.height=tile_rows; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2409 | tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t) |
cristy | 79e5dad | 2010-09-16 19:48:33 +0000 | [diff] [blame] | 2410 | tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2411 | tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t) |
cristy | 79e5dad | 2010-09-16 19:48:33 +0000 | [diff] [blame] | 2412 | tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2413 | if ((tiff_info->scanlines == (unsigned char *) NULL) || |
| 2414 | (tiff_info->pixels == (unsigned char *) NULL)) |
| 2415 | { |
| 2416 | DestroyTIFFInfo(tiff_info); |
| 2417 | return(MagickFalse); |
| 2418 | } |
| 2419 | return(MagickTrue); |
| 2420 | } |
| 2421 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2422 | static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2423 | tsample_t sample,Image *image) |
| 2424 | { |
| 2425 | int32 |
| 2426 | status; |
| 2427 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2428 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2429 | i; |
| 2430 | |
| 2431 | register unsigned char |
| 2432 | *p, |
| 2433 | *q; |
| 2434 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2435 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2436 | number_tiles, |
| 2437 | tile_width; |
| 2438 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 2439 | ssize_t |
| 2440 | bytes_per_pixel, |
| 2441 | j, |
| 2442 | k, |
| 2443 | l; |
| 2444 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2445 | if (TIFFIsTiled(tiff) == 0) |
| 2446 | return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample)); |
| 2447 | /* |
| 2448 | Fill scanlines to tile height. |
| 2449 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2450 | i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2451 | (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline, |
| 2452 | (size_t) TIFFScanlineSize(tiff)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2453 | if (((size_t) (row % tiff_info->tile_geometry.height) != |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 2454 | (tiff_info->tile_geometry.height-1)) && |
| 2455 | (row != (ssize_t) (image->rows-1))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2456 | return(0); |
| 2457 | /* |
| 2458 | Write tile to TIFF image. |
| 2459 | */ |
| 2460 | status=0; |
cristy | 32fab70 | 2012-01-18 02:47:47 +0000 | [diff] [blame] | 2461 | bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) ( |
| 2462 | tiff_info->tile_geometry.height*tiff_info->tile_geometry.width); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2463 | number_tiles=(image->columns+tiff_info->tile_geometry.width)/ |
| 2464 | tiff_info->tile_geometry.width; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2465 | for (i=0; i < (ssize_t) number_tiles; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2466 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2467 | tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2468 | tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2469 | for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++) |
| 2470 | for (k=0; k < (ssize_t) tile_width; k++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2471 | { |
| 2472 | if (bytes_per_pixel == 0) |
| 2473 | { |
| 2474 | p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i* |
| 2475 | tiff_info->tile_geometry.width+k)/8); |
| 2476 | q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8); |
| 2477 | *q++=(*p++); |
| 2478 | continue; |
| 2479 | } |
| 2480 | p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i* |
| 2481 | tiff_info->tile_geometry.width+k)*bytes_per_pixel); |
| 2482 | q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel); |
| 2483 | for (l=0; l < bytes_per_pixel; l++) |
| 2484 | *q++=(*p++); |
| 2485 | } |
cristy | daff809 | 2010-04-22 14:50:53 +0000 | [diff] [blame] | 2486 | if ((i*tiff_info->tile_geometry.width) != image->columns) |
| 2487 | status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i* |
| 2488 | tiff_info->tile_geometry.width),(uint32) ((row/ |
| 2489 | tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0, |
| 2490 | sample); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2491 | if (status < 0) |
| 2492 | break; |
| 2493 | } |
| 2494 | return(status); |
| 2495 | } |
| 2496 | |
| 2497 | static void TIFFSetProfiles(TIFF *tiff,Image *image) |
| 2498 | { |
| 2499 | const char |
| 2500 | *name; |
| 2501 | |
| 2502 | const StringInfo |
| 2503 | *profile; |
| 2504 | |
| 2505 | if (image->profiles == (void *) NULL) |
| 2506 | return; |
| 2507 | ResetImageProfileIterator(image); |
| 2508 | for (name=GetNextImageProfile(image); name != (const char *) NULL; ) |
| 2509 | { |
| 2510 | profile=GetImageProfile(image,name); |
cristy | 4c69823 | 2013-03-17 20:55:22 +0000 | [diff] [blame] | 2511 | if (GetStringInfoLength(profile) == 0) |
| 2512 | { |
| 2513 | name=GetNextImageProfile(image); |
| 2514 | continue; |
| 2515 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2516 | #if defined(TIFFTAG_XMLPACKET) |
| 2517 | if (LocaleCompare(name,"xmp") == 0) |
| 2518 | (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength( |
| 2519 | profile),GetStringInfoDatum(profile)); |
| 2520 | #endif |
| 2521 | #if defined(TIFFTAG_ICCPROFILE) |
| 2522 | if (LocaleCompare(name,"icc") == 0) |
| 2523 | (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength( |
| 2524 | profile),GetStringInfoDatum(profile)); |
| 2525 | #endif |
| 2526 | if (LocaleCompare(name,"iptc") == 0) |
| 2527 | { |
| 2528 | size_t |
| 2529 | length; |
| 2530 | |
| 2531 | StringInfo |
| 2532 | *iptc_profile; |
| 2533 | |
| 2534 | iptc_profile=CloneStringInfo(profile); |
| 2535 | length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) & |
| 2536 | 0x03); |
| 2537 | SetStringInfoLength(iptc_profile,length); |
| 2538 | if (TIFFIsByteSwapped(tiff)) |
| 2539 | TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile), |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 2540 | (unsigned long) (length/4)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2541 | (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32) |
| 2542 | GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile)); |
| 2543 | iptc_profile=DestroyStringInfo(iptc_profile); |
| 2544 | } |
| 2545 | #if defined(TIFFTAG_PHOTOSHOP) |
| 2546 | if (LocaleCompare(name,"8bim") == 0) |
| 2547 | (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32) |
| 2548 | GetStringInfoLength(profile),GetStringInfoDatum(profile)); |
| 2549 | #endif |
| 2550 | if (LocaleCompare(name,"tiff:37724") == 0) |
cristy | e5d8550 | 2011-11-09 19:45:06 +0000 | [diff] [blame] | 2551 | (void) TIFFSetField(tiff,37724,(uint32) GetStringInfoLength(profile), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2552 | GetStringInfoDatum(profile)); |
cristy | 30df84f | 2012-10-04 01:34:18 +0000 | [diff] [blame] | 2553 | if (LocaleCompare(name,"tiff:34118") == 0) |
| 2554 | (void) TIFFSetField(tiff,34118,(uint32) GetStringInfoLength(profile), |
| 2555 | GetStringInfoDatum(profile)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2556 | name=GetNextImageProfile(image); |
| 2557 | } |
| 2558 | } |
| 2559 | |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2560 | static void TIFFSetProperties(TIFF *tiff,Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2561 | { |
| 2562 | const char |
| 2563 | *value; |
| 2564 | |
cristy | 15d8d21 | 2010-09-17 01:56:30 +0000 | [diff] [blame] | 2565 | (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2566 | value=GetImageProperty(image,"tiff:hostcomputer",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2567 | if (value != (const char *) NULL) |
| 2568 | (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2569 | value=GetImageProperty(image,"tiff:artist",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2570 | if (value != (const char *) NULL) |
| 2571 | (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2572 | value=GetImageProperty(image,"tiff:timestamp",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2573 | if (value != (const char *) NULL) |
| 2574 | (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2575 | value=GetImageProperty(image,"tiff:make",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2576 | if (value != (const char *) NULL) |
| 2577 | (void) TIFFSetField(tiff,TIFFTAG_MAKE,value); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2578 | value=GetImageProperty(image,"tiff:model",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2579 | if (value != (const char *) NULL) |
| 2580 | (void) TIFFSetField(tiff,TIFFTAG_MODEL,value); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2581 | value=GetImageProperty(image,"tiff:software",exception); |
cristy | 15d8d21 | 2010-09-17 01:56:30 +0000 | [diff] [blame] | 2582 | if (value != (const char *) NULL) |
| 2583 | (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2584 | value=GetImageProperty(image,"tiff:copyright",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2585 | if (value != (const char *) NULL) |
cristy | 98ca0f5 | 2011-10-08 23:19:10 +0000 | [diff] [blame] | 2586 | (void) TIFFSetField(tiff,TIFFTAG_COPYRIGHT,value); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2587 | value=GetImageProperty(image,"kodak-33423",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2588 | if (value != (const char *) NULL) |
| 2589 | (void) TIFFSetField(tiff,33423,value); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2590 | value=GetImageProperty(image,"kodak-36867",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2591 | if (value != (const char *) NULL) |
| 2592 | (void) TIFFSetField(tiff,36867,value); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2593 | value=GetImageProperty(image,"label",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2594 | if (value != (const char *) NULL) |
| 2595 | (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value); |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2596 | value=GetImageProperty(image,"comment",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2597 | if (value != (const char *) NULL) |
| 2598 | (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value); |
| 2599 | } |
| 2600 | |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2601 | static void TIFFSetEXIFProperties(TIFF *tiff,Image *image, |
| 2602 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2603 | { |
| 2604 | #if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY) |
| 2605 | const char |
| 2606 | *value; |
| 2607 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2608 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2609 | i; |
| 2610 | |
| 2611 | uint32 |
| 2612 | offset; |
| 2613 | |
| 2614 | /* |
| 2615 | Write EXIF properties. |
| 2616 | */ |
| 2617 | offset=0; |
| 2618 | (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset); |
| 2619 | for (i=0; exif_info[i].tag != 0; i++) |
| 2620 | { |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 2621 | value=GetImageProperty(image,exif_info[i].property,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2622 | if (value == (const char *) NULL) |
| 2623 | continue; |
| 2624 | switch (exif_info[i].type) |
| 2625 | { |
| 2626 | case TIFF_ASCII: |
| 2627 | { |
| 2628 | (void) TIFFSetField(tiff,exif_info[i].tag,value); |
| 2629 | break; |
| 2630 | } |
| 2631 | case TIFF_SHORT: |
| 2632 | { |
| 2633 | uint16 |
cristy | 0382cd3 | 2013-10-02 13:49:48 +0000 | [diff] [blame] | 2634 | field; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2635 | |
cristy | 0382cd3 | 2013-10-02 13:49:48 +0000 | [diff] [blame] | 2636 | field=(uint16) StringToLong(value); |
| 2637 | (void) TIFFSetField(tiff,exif_info[i].tag,field); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2638 | break; |
| 2639 | } |
| 2640 | case TIFF_LONG: |
| 2641 | { |
| 2642 | uint16 |
cristy | 0382cd3 | 2013-10-02 13:49:48 +0000 | [diff] [blame] | 2643 | field; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2644 | |
cristy | 0382cd3 | 2013-10-02 13:49:48 +0000 | [diff] [blame] | 2645 | field=(uint16) StringToLong(value); |
| 2646 | (void) TIFFSetField(tiff,exif_info[i].tag,field); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2647 | break; |
| 2648 | } |
| 2649 | case TIFF_RATIONAL: |
| 2650 | case TIFF_SRATIONAL: |
| 2651 | { |
| 2652 | float |
cristy | 0382cd3 | 2013-10-02 13:49:48 +0000 | [diff] [blame] | 2653 | field; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2654 | |
cristy | 0382cd3 | 2013-10-02 13:49:48 +0000 | [diff] [blame] | 2655 | field=StringToDouble(value,(char **) NULL); |
| 2656 | (void) TIFFSetField(tiff,exif_info[i].tag,field); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2657 | break; |
| 2658 | } |
| 2659 | default: |
| 2660 | break; |
| 2661 | } |
| 2662 | } |
| 2663 | /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */ |
| 2664 | #else |
| 2665 | (void) tiff; |
| 2666 | (void) image; |
| 2667 | #endif |
| 2668 | } |
| 2669 | |
| 2670 | static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2671 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2672 | { |
| 2673 | #if !defined(TIFFDefaultStripSize) |
| 2674 | #define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff)) |
| 2675 | #endif |
| 2676 | |
| 2677 | const char |
| 2678 | *mode, |
cristy | 949bf5b | 2010-05-08 02:47:03 +0000 | [diff] [blame] | 2679 | *option; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2680 | |
| 2681 | CompressionType |
| 2682 | compression; |
| 2683 | |
cristy | 6b03282 | 2010-06-29 16:52:32 +0000 | [diff] [blame] | 2684 | EndianType |
| 2685 | endian_type; |
| 2686 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2687 | MagickBooleanType |
| 2688 | debug, |
| 2689 | status; |
| 2690 | |
| 2691 | MagickOffsetType |
| 2692 | scene; |
| 2693 | |
| 2694 | QuantumInfo |
| 2695 | *quantum_info; |
| 2696 | |
| 2697 | QuantumType |
| 2698 | quantum_type; |
| 2699 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2700 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2701 | i; |
| 2702 | |
| 2703 | size_t |
cristy | 9945c6e | 2012-01-23 19:49:26 +0000 | [diff] [blame] | 2704 | length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2705 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 2706 | ssize_t |
| 2707 | y; |
| 2708 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2709 | TIFF |
| 2710 | *tiff; |
| 2711 | |
| 2712 | TIFFErrorHandler |
| 2713 | error_handler, |
| 2714 | warning_handler; |
| 2715 | |
| 2716 | TIFFInfo |
| 2717 | tiff_info; |
| 2718 | |
| 2719 | uint16 |
| 2720 | bits_per_sample, |
| 2721 | compress_tag, |
cristy | f2687ca | 2010-06-29 16:32:38 +0000 | [diff] [blame] | 2722 | endian, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2723 | photometric; |
| 2724 | |
| 2725 | uint32 |
| 2726 | rows_per_strip; |
| 2727 | |
| 2728 | unsigned char |
| 2729 | *pixels; |
| 2730 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2731 | /* |
| 2732 | Open TIFF file. |
| 2733 | */ |
| 2734 | assert(image_info != (const ImageInfo *) NULL); |
| 2735 | assert(image_info->signature == MagickSignature); |
| 2736 | assert(image != (Image *) NULL); |
| 2737 | assert(image->signature == MagickSignature); |
| 2738 | if (image->debug != MagickFalse) |
| 2739 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2740 | assert(exception != (ExceptionInfo *) NULL); |
| 2741 | assert(exception->signature == MagickSignature); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2742 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2743 | if (status == MagickFalse) |
| 2744 | return(status); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2745 | (void) MagickSetThreadValue(tiff_exception,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2746 | error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors); |
| 2747 | warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings); |
cristy | 6b03282 | 2010-06-29 16:52:32 +0000 | [diff] [blame] | 2748 | endian_type=UndefinedEndian; |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 2749 | option=GetImageOption(image_info,"tiff:endian"); |
cristy | 6b03282 | 2010-06-29 16:52:32 +0000 | [diff] [blame] | 2750 | if (option != (const char *) NULL) |
| 2751 | { |
| 2752 | if (LocaleNCompare(option,"msb",3) == 0) |
| 2753 | endian_type=MSBEndian; |
| 2754 | if (LocaleNCompare(option,"lsb",3) == 0) |
| 2755 | endian_type=LSBEndian;; |
| 2756 | } |
| 2757 | switch (endian_type) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2758 | { |
| 2759 | case LSBEndian: mode="wl"; break; |
| 2760 | case MSBEndian: mode="wb"; break; |
| 2761 | default: mode="w"; break; |
| 2762 | } |
| 2763 | #if defined(TIFF_VERSION_BIG) |
| 2764 | if (LocaleCompare(image_info->magick,"TIFF64") == 0) |
cristy | 6b03282 | 2010-06-29 16:52:32 +0000 | [diff] [blame] | 2765 | switch (endian_type) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2766 | { |
| 2767 | case LSBEndian: mode="wl8"; break; |
| 2768 | case MSBEndian: mode="wb8"; break; |
| 2769 | default: mode="w8"; break; |
| 2770 | } |
| 2771 | #endif |
| 2772 | tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob, |
| 2773 | TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob, |
| 2774 | TIFFUnmapBlob); |
| 2775 | if (tiff == (TIFF *) NULL) |
| 2776 | { |
| 2777 | (void) TIFFSetWarningHandler(warning_handler); |
| 2778 | (void) TIFFSetErrorHandler(error_handler); |
| 2779 | return(MagickFalse); |
| 2780 | } |
| 2781 | scene=0; |
| 2782 | debug=IsEventLogging(); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 2783 | (void) debug; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2784 | do |
| 2785 | { |
| 2786 | /* |
| 2787 | Initialize TIFF fields. |
| 2788 | */ |
cristy | 5f1c1ff | 2010-12-23 21:38:06 +0000 | [diff] [blame] | 2789 | if ((image_info->type != UndefinedType) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2790 | (image_info->type != OptimizeType)) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2791 | (void) SetImageType(image,image_info->type,exception); |
dirk | 29dd80e | 2013-10-31 23:11:11 +0000 | [diff] [blame] | 2792 | compression=UndefinedCompression; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2793 | if (image_info->compression != UndefinedCompression) |
| 2794 | compression=image_info->compression; |
| 2795 | switch (compression) |
| 2796 | { |
| 2797 | case FaxCompression: |
cristy | a5573bc | 2013-10-26 14:13:34 +0000 | [diff] [blame] | 2798 | case Group4Compression: |
| 2799 | { |
| 2800 | (void) SetImageType(image,BilevelType,exception); |
| 2801 | break; |
| 2802 | } |
| 2803 | case JPEGCompression: |
| 2804 | { |
| 2805 | (void) SetImageStorageClass(image,DirectClass,exception); |
| 2806 | (void) SetImageDepth(image,8,exception); |
| 2807 | break; |
| 2808 | } |
| 2809 | default: |
| 2810 | break; |
| 2811 | } |
| 2812 | quantum_info=AcquireQuantumInfo(image_info,image); |
| 2813 | if (quantum_info == (QuantumInfo *) NULL) |
| 2814 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 2815 | if ((image->storage_class != PseudoClass) && (image->depth >= 32) && |
| 2816 | (quantum_info->format == UndefinedQuantumFormat) && |
| 2817 | (IsHighDynamicRangeImage(image,exception) != MagickFalse)) |
| 2818 | { |
| 2819 | status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat); |
| 2820 | if (status == MagickFalse) |
| 2821 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 2822 | } |
| 2823 | if ((LocaleCompare(image_info->magick,"PTIF") == 0) && |
| 2824 | (GetPreviousImageInList(image) != (Image *) NULL)) |
| 2825 | (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE); |
| 2826 | if ((image->columns != (uint32) image->columns) || |
| 2827 | (image->rows != (uint32) image->rows)) |
| 2828 | ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit"); |
| 2829 | (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows); |
| 2830 | (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns); |
| 2831 | switch (compression) |
| 2832 | { |
| 2833 | case FaxCompression: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2834 | { |
| 2835 | compress_tag=COMPRESSION_CCITTFAX3; |
| 2836 | SetQuantumMinIsWhite(quantum_info,MagickTrue); |
| 2837 | break; |
| 2838 | } |
| 2839 | case Group4Compression: |
| 2840 | { |
| 2841 | compress_tag=COMPRESSION_CCITTFAX4; |
| 2842 | SetQuantumMinIsWhite(quantum_info,MagickTrue); |
| 2843 | break; |
| 2844 | } |
cristy | 6d5e20f | 2011-04-25 13:48:54 +0000 | [diff] [blame] | 2845 | #if defined(COMPRESSION_JBIG) |
| 2846 | case JBIG1Compression: |
| 2847 | { |
| 2848 | compress_tag=COMPRESSION_JBIG; |
| 2849 | break; |
| 2850 | } |
| 2851 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2852 | case JPEGCompression: |
| 2853 | { |
| 2854 | compress_tag=COMPRESSION_JPEG; |
| 2855 | break; |
| 2856 | } |
cristy | fbb0ef0 | 2010-12-19 02:32:11 +0000 | [diff] [blame] | 2857 | #if defined(COMPRESSION_LZMA) |
| 2858 | case LZMACompression: |
| 2859 | { |
| 2860 | compress_tag=COMPRESSION_LZMA; |
| 2861 | break; |
| 2862 | } |
| 2863 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2864 | case LZWCompression: |
| 2865 | { |
| 2866 | compress_tag=COMPRESSION_LZW; |
| 2867 | break; |
| 2868 | } |
| 2869 | case RLECompression: |
| 2870 | { |
| 2871 | compress_tag=COMPRESSION_PACKBITS; |
| 2872 | break; |
| 2873 | } |
| 2874 | case ZipCompression: |
| 2875 | { |
| 2876 | compress_tag=COMPRESSION_ADOBE_DEFLATE; |
| 2877 | break; |
| 2878 | } |
| 2879 | case NoCompression: |
| 2880 | default: |
| 2881 | { |
| 2882 | compress_tag=COMPRESSION_NONE; |
| 2883 | break; |
| 2884 | } |
| 2885 | } |
cristy | 5b67587 | 2009-10-21 13:12:00 +0000 | [diff] [blame] | 2886 | #if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919) |
| 2887 | if ((compress_tag != COMPRESSION_NONE) && |
| 2888 | (TIFFIsCODECConfigured(compress_tag) == 0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2889 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2890 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 2891 | "CompressionNotSupported","`%s'",CommandOptionToMnemonic( |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2892 | MagickCompressOptions,(ssize_t) compression)); |
cristy | 5b67587 | 2009-10-21 13:12:00 +0000 | [diff] [blame] | 2893 | compress_tag=COMPRESSION_NONE; |
| 2894 | compression=NoCompression; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2895 | } |
| 2896 | #else |
| 2897 | switch (compress_tag) |
| 2898 | { |
| 2899 | #if defined(CCITT_SUPPORT) |
| 2900 | case COMPRESSION_CCITTFAX3: |
| 2901 | case COMPRESSION_CCITTFAX4: |
| 2902 | #endif |
| 2903 | #if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT) |
| 2904 | case COMPRESSION_JPEG: |
| 2905 | #endif |
cristy | fbb0ef0 | 2010-12-19 02:32:11 +0000 | [diff] [blame] | 2906 | #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA) |
| 2907 | case COMPRESSION_LZMA: |
| 2908 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2909 | #if defined(LZW_SUPPORT) |
| 2910 | case COMPRESSION_LZW: |
| 2911 | #endif |
| 2912 | #if defined(PACKBITS_SUPPORT) |
| 2913 | case COMPRESSION_PACKBITS: |
| 2914 | #endif |
| 2915 | #if defined(ZIP_SUPPORT) |
| 2916 | case COMPRESSION_ADOBE_DEFLATE: |
| 2917 | #endif |
| 2918 | case COMPRESSION_NONE: |
| 2919 | break; |
| 2920 | default: |
| 2921 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2922 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 2923 | "CompressionNotSupported","`%s'",CommandOptionToMnemonic( |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2924 | MagickCompressOptions,(ssize_t) compression)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2925 | compress_tag=COMPRESSION_NONE; |
| 2926 | compression=NoCompression; |
| 2927 | break; |
| 2928 | } |
| 2929 | } |
| 2930 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2931 | if (image->colorspace == CMYKColorspace) |
| 2932 | { |
| 2933 | photometric=PHOTOMETRIC_SEPARATED; |
| 2934 | (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4); |
| 2935 | (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK); |
| 2936 | } |
| 2937 | else |
| 2938 | { |
| 2939 | /* |
| 2940 | Full color TIFF raster. |
| 2941 | */ |
| 2942 | if (image->colorspace == LabColorspace) |
cristy | 514da94 | 2012-07-06 23:24:14 +0000 | [diff] [blame] | 2943 | { |
| 2944 | photometric=PHOTOMETRIC_CIELAB; |
| 2945 | EncodeLabImage(image,exception); |
| 2946 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2947 | else |
| 2948 | if (image->colorspace == YCbCrColorspace) |
| 2949 | { |
| 2950 | photometric=PHOTOMETRIC_YCBCR; |
| 2951 | (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2952 | (void) SetImageStorageClass(image,DirectClass,exception); |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 2953 | (void) SetImageDepth(image,8,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2954 | } |
| 2955 | else |
cristy | bdf2cb2 | 2012-05-30 12:06:16 +0000 | [diff] [blame] | 2956 | photometric=PHOTOMETRIC_RGB; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2957 | (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3); |
| 2958 | if ((image_info->type != TrueColorType) && |
| 2959 | (image_info->type != TrueColorMatteType)) |
| 2960 | { |
cristy | 94c8fe4 | 2009-10-06 01:57:36 +0000 | [diff] [blame] | 2961 | if ((image_info->type != PaletteType) && |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2962 | (IsImageGray(image,exception) != MagickFalse)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2963 | { |
cristy | 94c8fe4 | 2009-10-06 01:57:36 +0000 | [diff] [blame] | 2964 | photometric=(uint16) (quantum_info->min_is_white != |
| 2965 | MagickFalse ? PHOTOMETRIC_MINISWHITE : |
| 2966 | PHOTOMETRIC_MINISBLACK); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2967 | (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1); |
cristy | 30df84f | 2012-10-04 01:34:18 +0000 | [diff] [blame] | 2968 | if ((image_info->depth == 0) && |
| 2969 | (image->alpha_trait != BlendPixelTrait) && |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 2970 | (IsImageMonochrome(image,exception) != MagickFalse)) |
cristy | 94c8fe4 | 2009-10-06 01:57:36 +0000 | [diff] [blame] | 2971 | { |
| 2972 | status=SetQuantumDepth(image,quantum_info,1); |
| 2973 | if (status == MagickFalse) |
| 2974 | ThrowWriterException(ResourceLimitError, |
| 2975 | "MemoryAllocationFailed"); |
| 2976 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2977 | } |
| 2978 | else |
cristy | 94c8fe4 | 2009-10-06 01:57:36 +0000 | [diff] [blame] | 2979 | if (image->storage_class == PseudoClass) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2980 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2981 | size_t |
cristy | 94c8fe4 | 2009-10-06 01:57:36 +0000 | [diff] [blame] | 2982 | depth; |
| 2983 | |
| 2984 | /* |
| 2985 | Colormapped TIFF raster. |
| 2986 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2987 | (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1); |
cristy | 94c8fe4 | 2009-10-06 01:57:36 +0000 | [diff] [blame] | 2988 | photometric=PHOTOMETRIC_PALETTE; |
| 2989 | depth=1; |
| 2990 | while ((GetQuantumRange(depth)+1) < image->colors) |
| 2991 | depth<<=1; |
| 2992 | status=SetQuantumDepth(image,quantum_info,depth); |
| 2993 | if (status == MagickFalse) |
| 2994 | ThrowWriterException(ResourceLimitError, |
| 2995 | "MemoryAllocationFailed"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2996 | } |
| 2997 | } |
| 2998 | } |
cristy | bdf2cb2 | 2012-05-30 12:06:16 +0000 | [diff] [blame] | 2999 | if ((photometric == PHOTOMETRIC_RGB) && |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 3000 | (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)) |
cristy | bdf2cb2 | 2012-05-30 12:06:16 +0000 | [diff] [blame] | 3001 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | f2687ca | 2010-06-29 16:32:38 +0000 | [diff] [blame] | 3002 | switch (image->endian) |
| 3003 | { |
| 3004 | case LSBEndian: |
| 3005 | { |
| 3006 | endian=FILLORDER_LSB2MSB; |
| 3007 | break; |
| 3008 | } |
| 3009 | case MSBEndian: |
| 3010 | { |
| 3011 | endian=FILLORDER_MSB2LSB; |
| 3012 | break; |
| 3013 | } |
| 3014 | case UndefinedEndian: |
| 3015 | default: |
| 3016 | { |
| 3017 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian); |
| 3018 | break; |
| 3019 | } |
| 3020 | } |
cristy | daff809 | 2010-04-22 14:50:53 +0000 | [diff] [blame] | 3021 | if ((compress_tag == COMPRESSION_CCITTFAX3) && |
| 3022 | (photometric != PHOTOMETRIC_MINISWHITE)) |
| 3023 | { |
| 3024 | compress_tag=COMPRESSION_NONE; |
cristy | f2687ca | 2010-06-29 16:32:38 +0000 | [diff] [blame] | 3025 | endian=FILLORDER_MSB2LSB; |
cristy | daff809 | 2010-04-22 14:50:53 +0000 | [diff] [blame] | 3026 | } |
cristy | 0accf6e | 2009-10-01 13:14:28 +0000 | [diff] [blame] | 3027 | else |
cristy | daff809 | 2010-04-22 14:50:53 +0000 | [diff] [blame] | 3028 | if ((compress_tag == COMPRESSION_CCITTFAX4) && |
| 3029 | (photometric != PHOTOMETRIC_MINISWHITE)) |
| 3030 | { |
| 3031 | compress_tag=COMPRESSION_NONE; |
cristy | f2687ca | 2010-06-29 16:32:38 +0000 | [diff] [blame] | 3032 | endian=FILLORDER_MSB2LSB; |
cristy | daff809 | 2010-04-22 14:50:53 +0000 | [diff] [blame] | 3033 | } |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 3034 | option=GetImageOption(image_info,"tiff:fill-order"); |
cristy | 62e282b | 2010-06-29 01:27:13 +0000 | [diff] [blame] | 3035 | if (option != (const char *) NULL) |
| 3036 | { |
| 3037 | if (LocaleNCompare(option,"msb",3) == 0) |
cristy | f2687ca | 2010-06-29 16:32:38 +0000 | [diff] [blame] | 3038 | endian=FILLORDER_MSB2LSB; |
cristy | 62e282b | 2010-06-29 01:27:13 +0000 | [diff] [blame] | 3039 | if (LocaleNCompare(option,"lsb",3) == 0) |
cristy | f2687ca | 2010-06-29 16:32:38 +0000 | [diff] [blame] | 3040 | endian=FILLORDER_LSB2MSB; |
cristy | 62e282b | 2010-06-29 01:27:13 +0000 | [diff] [blame] | 3041 | } |
cristy | f2687ca | 2010-06-29 16:32:38 +0000 | [diff] [blame] | 3042 | (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag); |
| 3043 | (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3044 | (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3045 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3046 | { |
| 3047 | uint16 |
| 3048 | extra_samples, |
| 3049 | sample_info[1], |
| 3050 | samples_per_pixel; |
| 3051 | |
| 3052 | /* |
| 3053 | TIFF has a matte channel. |
| 3054 | */ |
| 3055 | extra_samples=1; |
| 3056 | sample_info[0]=EXTRASAMPLE_UNASSALPHA; |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 3057 | option=GetImageOption(image_info,"tiff:alpha"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3058 | if ((option != (const char *) NULL) && |
| 3059 | (LocaleCompare(option,"associated") == 0)) |
| 3060 | sample_info[0]=EXTRASAMPLE_ASSOCALPHA; |
| 3061 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL, |
| 3062 | &samples_per_pixel); |
| 3063 | (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1); |
| 3064 | (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples, |
| 3065 | &sample_info); |
| 3066 | if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA) |
| 3067 | SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha); |
| 3068 | } |
| 3069 | (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric); |
| 3070 | switch (quantum_info->format) |
| 3071 | { |
| 3072 | case FloatingPointQuantumFormat: |
| 3073 | { |
| 3074 | (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP); |
| 3075 | (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum); |
| 3076 | (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum); |
| 3077 | break; |
| 3078 | } |
| 3079 | case SignedQuantumFormat: |
| 3080 | { |
| 3081 | (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT); |
| 3082 | break; |
| 3083 | } |
| 3084 | case UnsignedQuantumFormat: |
| 3085 | { |
| 3086 | (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT); |
| 3087 | break; |
| 3088 | } |
| 3089 | default: |
| 3090 | break; |
| 3091 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3092 | (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT); |
| 3093 | (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG); |
| 3094 | if (photometric == PHOTOMETRIC_RGB) |
| 3095 | if ((image_info->interlace == PlaneInterlace) || |
| 3096 | (image_info->interlace == PartitionInterlace)) |
| 3097 | (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE); |
cristy | efc63e4 | 2013-09-05 16:15:56 +0000 | [diff] [blame] | 3098 | rows_per_strip=TIFFDefaultStripSize(tiff,0); |
cristy | 092ec8d | 2013-04-26 13:46:22 +0000 | [diff] [blame] | 3099 | option=GetImageOption(image_info,"tiff:rows-per-strip"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3100 | if (option != (const char *) NULL) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3101 | rows_per_strip=(size_t) strtol(option,(char **) NULL,10); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3102 | switch (compress_tag) |
| 3103 | { |
| 3104 | case COMPRESSION_JPEG: |
| 3105 | { |
| 3106 | #if defined(JPEG_SUPPORT) |
| 3107 | const char |
| 3108 | *sampling_factor; |
| 3109 | |
| 3110 | GeometryInfo |
| 3111 | geometry_info; |
| 3112 | |
| 3113 | MagickStatusType |
| 3114 | flags; |
| 3115 | |
| 3116 | rows_per_strip+=(16-(rows_per_strip % 16)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3117 | if (image_info->quality != UndefinedCompressionQuality) |
| 3118 | (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality); |
| 3119 | (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW); |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 3120 | if (IssRGBCompatibleColorspace(image->colorspace) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3121 | { |
cristy | 949bf5b | 2010-05-08 02:47:03 +0000 | [diff] [blame] | 3122 | const char |
| 3123 | *value; |
| 3124 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3125 | (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB); |
| 3126 | sampling_factor=(const char *) NULL; |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 3127 | value=GetImageProperty(image,"jpeg:sampling-factor",exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3128 | if (value != (char *) NULL) |
| 3129 | { |
| 3130 | sampling_factor=value; |
| 3131 | if (image->debug != MagickFalse) |
| 3132 | (void) LogMagickEvent(CoderEvent,GetMagickModule(), |
| 3133 | " Input sampling-factors=%s",sampling_factor); |
| 3134 | } |
| 3135 | if (image_info->sampling_factor != (char *) NULL) |
| 3136 | sampling_factor=image_info->sampling_factor; |
| 3137 | if (sampling_factor != (const char *) NULL) |
| 3138 | { |
| 3139 | flags=ParseGeometry(sampling_factor,&geometry_info); |
| 3140 | if ((flags & SigmaValue) == 0) |
| 3141 | geometry_info.sigma=geometry_info.rho; |
cristy | 79e5dad | 2010-09-16 19:48:33 +0000 | [diff] [blame] | 3142 | if (image->colorspace == YCbCrColorspace) |
| 3143 | (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16) |
| 3144 | geometry_info.rho,(uint16) geometry_info.sigma); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3145 | } |
| 3146 | } |
| 3147 | if (bits_per_sample == 12) |
| 3148 | (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT); |
| 3149 | #endif |
| 3150 | break; |
| 3151 | } |
| 3152 | case COMPRESSION_ADOBE_DEFLATE: |
| 3153 | { |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 3154 | rows_per_strip=(uint32) image->rows; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3155 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE, |
| 3156 | &bits_per_sample); |
| 3157 | if (((photometric == PHOTOMETRIC_RGB) || |
| 3158 | (photometric == PHOTOMETRIC_MINISBLACK)) && |
| 3159 | ((bits_per_sample == 8) || (bits_per_sample == 16))) |
cristy | ef8f26b | 2010-12-19 20:29:16 +0000 | [diff] [blame] | 3160 | (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL); |
cristy | f6fe0a1 | 2010-05-30 00:44:47 +0000 | [diff] [blame] | 3161 | (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) ( |
| 3162 | image_info->quality == UndefinedCompressionQuality ? 7 : |
cristy | 0b29b25 | 2010-05-30 01:59:46 +0000 | [diff] [blame] | 3163 | MagickMin((ssize_t) image_info->quality/10,9))); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3164 | break; |
| 3165 | } |
| 3166 | case COMPRESSION_CCITTFAX3: |
| 3167 | { |
| 3168 | /* |
| 3169 | Byte-aligned EOL. |
| 3170 | */ |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 3171 | rows_per_strip=(uint32) image->rows; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3172 | (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4); |
| 3173 | break; |
| 3174 | } |
| 3175 | case COMPRESSION_CCITTFAX4: |
| 3176 | { |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 3177 | rows_per_strip=(uint32) image->rows; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3178 | break; |
| 3179 | } |
cristy | ef8f26b | 2010-12-19 20:29:16 +0000 | [diff] [blame] | 3180 | #if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA) |
| 3181 | case COMPRESSION_LZMA: |
| 3182 | { |
| 3183 | if (((photometric == PHOTOMETRIC_RGB) || |
| 3184 | (photometric == PHOTOMETRIC_MINISBLACK)) && |
| 3185 | ((bits_per_sample == 8) || (bits_per_sample == 16))) |
| 3186 | (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL); |
| 3187 | (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) ( |
| 3188 | image_info->quality == UndefinedCompressionQuality ? 7 : |
| 3189 | MagickMin((ssize_t) image_info->quality/10,9))); |
| 3190 | break; |
| 3191 | } |
| 3192 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3193 | case COMPRESSION_LZW: |
| 3194 | { |
| 3195 | (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE, |
| 3196 | &bits_per_sample); |
| 3197 | if (((photometric == PHOTOMETRIC_RGB) || |
| 3198 | (photometric == PHOTOMETRIC_MINISBLACK)) && |
| 3199 | ((bits_per_sample == 8) || (bits_per_sample == 16))) |
cristy | ef8f26b | 2010-12-19 20:29:16 +0000 | [diff] [blame] | 3200 | (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3201 | break; |
| 3202 | } |
| 3203 | default: |
| 3204 | break; |
| 3205 | } |
cristy | efc63e4 | 2013-09-05 16:15:56 +0000 | [diff] [blame] | 3206 | if (rows_per_strip < 1) |
| 3207 | rows_per_strip=1; |
cristy | c0cbda0 | 2013-09-05 16:05:31 +0000 | [diff] [blame] | 3208 | if ((image->rows/rows_per_strip) >= (1UL << 15)) |
| 3209 | rows_per_strip=(image->rows >> 15); |
cristy | 548dfc7 | 2013-05-07 13:34:36 +0000 | [diff] [blame] | 3210 | (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip); |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 3211 | if ((image->resolution.x != 0.0) && (image->resolution.y != 0.0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3212 | { |
| 3213 | unsigned short |
| 3214 | units; |
| 3215 | |
| 3216 | /* |
| 3217 | Set image resolution. |
| 3218 | */ |
| 3219 | units=RESUNIT_NONE; |
| 3220 | if (image->units == PixelsPerInchResolution) |
| 3221 | units=RESUNIT_INCH; |
| 3222 | if (image->units == PixelsPerCentimeterResolution) |
| 3223 | units=RESUNIT_CENTIMETER; |
| 3224 | (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units); |
cristy | 2a11bef | 2011-10-28 18:33:11 +0000 | [diff] [blame] | 3225 | (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->resolution.x); |
| 3226 | (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->resolution.y); |
cristy | aaf690a | 2013-07-25 12:00:12 +0000 | [diff] [blame] | 3227 | if ((image->page.x < 0) || (image->page.y < 0)) |
| 3228 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 3229 | "TIFF: negative image positions unsupported","%s",image->filename); |
| 3230 | if ((image->page.x > 0) && (image->resolution.x > 0.0)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3231 | { |
| 3232 | /* |
cristy | aaf690a | 2013-07-25 12:00:12 +0000 | [diff] [blame] | 3233 | Set horizontal image position. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3234 | */ |
cristy | aaf690a | 2013-07-25 12:00:12 +0000 | [diff] [blame] | 3235 | (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/ |
| 3236 | image->resolution.x); |
| 3237 | } |
| 3238 | if ((image->page.y > 0) && (image->resolution.y > 0.0)) |
| 3239 | { |
| 3240 | /* |
| 3241 | Set vertical image position. |
| 3242 | */ |
| 3243 | (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/ |
| 3244 | image->resolution.y); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3245 | } |
| 3246 | } |
| 3247 | if (image->chromaticity.white_point.x != 0.0) |
| 3248 | { |
| 3249 | float |
| 3250 | chromaticity[6]; |
| 3251 | |
| 3252 | /* |
| 3253 | Set image chromaticity. |
| 3254 | */ |
| 3255 | chromaticity[0]=(float) image->chromaticity.red_primary.x; |
| 3256 | chromaticity[1]=(float) image->chromaticity.red_primary.y; |
| 3257 | chromaticity[2]=(float) image->chromaticity.green_primary.x; |
| 3258 | chromaticity[3]=(float) image->chromaticity.green_primary.y; |
| 3259 | chromaticity[4]=(float) image->chromaticity.blue_primary.x; |
| 3260 | chromaticity[5]=(float) image->chromaticity.blue_primary.y; |
| 3261 | (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity); |
| 3262 | chromaticity[0]=(float) image->chromaticity.white_point.x; |
| 3263 | chromaticity[1]=(float) image->chromaticity.white_point.y; |
| 3264 | (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity); |
| 3265 | } |
cristy | 59788de | 2011-11-23 13:56:19 +0000 | [diff] [blame] | 3266 | if ((LocaleCompare(image_info->magick,"PTIF") != 0) && |
| 3267 | (image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3268 | { |
| 3269 | (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE); |
| 3270 | if (image->scene != 0) |
| 3271 | (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene, |
| 3272 | GetImageListLength(image)); |
| 3273 | } |
| 3274 | if (image->orientation != UndefinedOrientation) |
| 3275 | (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation); |
| 3276 | (void) TIFFSetProfiles(tiff,image); |
| 3277 | { |
| 3278 | uint16 |
| 3279 | page, |
| 3280 | pages; |
| 3281 | |
| 3282 | page=(uint16) scene; |
cristy | eaedf06 | 2010-05-29 22:36:02 +0000 | [diff] [blame] | 3283 | pages=(uint16) GetImageListLength(image); |
cristy | 59788de | 2011-11-23 13:56:19 +0000 | [diff] [blame] | 3284 | if ((LocaleCompare(image_info->magick,"PTIF") != 0) && |
| 3285 | (image_info->adjoin != MagickFalse) && (pages > 1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3286 | (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE); |
| 3287 | (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages); |
| 3288 | } |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 3289 | (void) TIFFSetProperties(tiff,image,exception); |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 3290 | DisableMSCWarning(4127) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3291 | if (0) |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 3292 | RestoreMSCWarning |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 3293 | (void) TIFFSetEXIFProperties(tiff,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3294 | /* |
| 3295 | Write image scanlines. |
| 3296 | */ |
cristy | b0de93f | 2013-05-03 13:39:25 +0000 | [diff] [blame] | 3297 | if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3298 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | f164a87 | 2012-01-20 18:26:34 +0000 | [diff] [blame] | 3299 | quantum_info->endian=LSBEndian; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3300 | pixels=GetQuantumPixels(quantum_info); |
| 3301 | tiff_info.scanline=GetQuantumPixels(quantum_info); |
| 3302 | switch (photometric) |
| 3303 | { |
| 3304 | case PHOTOMETRIC_CIELAB: |
| 3305 | case PHOTOMETRIC_YCBCR: |
| 3306 | case PHOTOMETRIC_RGB: |
| 3307 | { |
| 3308 | /* |
| 3309 | RGB TIFF image. |
| 3310 | */ |
| 3311 | switch (image_info->interlace) |
| 3312 | { |
| 3313 | case NoInterlace: |
| 3314 | default: |
| 3315 | { |
| 3316 | quantum_type=RGBQuantum; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3317 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3318 | quantum_type=RGBAQuantum; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3319 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3320 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3321 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3322 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3323 | |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3324 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3325 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3326 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3327 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3328 | quantum_type,pixels,exception); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 3329 | (void) length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3330 | if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1) |
| 3331 | break; |
| 3332 | if (image->previous == (Image *) NULL) |
| 3333 | { |
cristy | 2837bcc | 2010-08-07 23:57:39 +0000 | [diff] [blame] | 3334 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) |
| 3335 | y,image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3336 | if (status == MagickFalse) |
| 3337 | break; |
| 3338 | } |
| 3339 | } |
| 3340 | break; |
| 3341 | } |
| 3342 | case PlaneInterlace: |
| 3343 | case PartitionInterlace: |
| 3344 | { |
| 3345 | /* |
| 3346 | Plane interlacing: RRRRRR...GGGGGG...BBBBBB... |
| 3347 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3348 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3349 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3350 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3351 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3352 | |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3353 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3354 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3355 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3356 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3357 | RedQuantum,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3358 | if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1) |
| 3359 | break; |
| 3360 | } |
| 3361 | if (image->previous == (Image *) NULL) |
| 3362 | { |
| 3363 | status=SetImageProgress(image,SaveImageTag,100,400); |
| 3364 | if (status == MagickFalse) |
| 3365 | break; |
| 3366 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3367 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3368 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3369 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3370 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3371 | |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3372 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3373 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3374 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3375 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3376 | GreenQuantum,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3377 | if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1) |
| 3378 | break; |
| 3379 | } |
| 3380 | if (image->previous == (Image *) NULL) |
| 3381 | { |
| 3382 | status=SetImageProgress(image,SaveImageTag,200,400); |
| 3383 | if (status == MagickFalse) |
| 3384 | break; |
| 3385 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3386 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3387 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3388 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3389 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3390 | |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3391 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3392 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3393 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3394 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3395 | BlueQuantum,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3396 | if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1) |
| 3397 | break; |
| 3398 | } |
| 3399 | if (image->previous == (Image *) NULL) |
| 3400 | { |
| 3401 | status=SetImageProgress(image,SaveImageTag,300,400); |
| 3402 | if (status == MagickFalse) |
| 3403 | break; |
| 3404 | } |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3405 | if (image->alpha_trait == BlendPixelTrait) |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3406 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3407 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3408 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3409 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3410 | |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3411 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3412 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3413 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3414 | length=ExportQuantumPixels(image,(CacheView *) NULL, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3415 | quantum_info,AlphaQuantum,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3416 | if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1) |
| 3417 | break; |
| 3418 | } |
| 3419 | if (image->previous == (Image *) NULL) |
| 3420 | { |
| 3421 | status=SetImageProgress(image,SaveImageTag,400,400); |
| 3422 | if (status == MagickFalse) |
| 3423 | break; |
| 3424 | } |
| 3425 | break; |
| 3426 | } |
| 3427 | } |
| 3428 | break; |
| 3429 | } |
| 3430 | case PHOTOMETRIC_SEPARATED: |
| 3431 | { |
| 3432 | /* |
| 3433 | CMYK TIFF image. |
| 3434 | */ |
| 3435 | quantum_type=CMYKQuantum; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3436 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3437 | quantum_type=CMYKAQuantum; |
| 3438 | if (image->colorspace != CMYKColorspace) |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 3439 | (void) TransformImageColorspace(image,CMYKColorspace,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3440 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3441 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3442 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3443 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3444 | |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3445 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3446 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3447 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3448 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3449 | quantum_type,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3450 | if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1) |
| 3451 | break; |
| 3452 | if (image->previous == (Image *) NULL) |
| 3453 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 3454 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 3455 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3456 | if (status == MagickFalse) |
| 3457 | break; |
| 3458 | } |
| 3459 | } |
| 3460 | break; |
| 3461 | } |
| 3462 | case PHOTOMETRIC_PALETTE: |
| 3463 | { |
| 3464 | uint16 |
| 3465 | *blue, |
| 3466 | *green, |
| 3467 | *red; |
| 3468 | |
| 3469 | /* |
| 3470 | Colormapped TIFF image. |
| 3471 | */ |
| 3472 | red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red)); |
| 3473 | green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green)); |
| 3474 | blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue)); |
| 3475 | if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) || |
| 3476 | (blue == (uint16 *) NULL)) |
| 3477 | ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed"); |
| 3478 | /* |
| 3479 | Initialize TIFF colormap. |
| 3480 | */ |
| 3481 | (void) ResetMagickMemory(red,0,65536*sizeof(*red)); |
| 3482 | (void) ResetMagickMemory(green,0,65536*sizeof(*green)); |
| 3483 | (void) ResetMagickMemory(blue,0,65536*sizeof(*blue)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3484 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3485 | { |
| 3486 | red[i]=ScaleQuantumToShort(image->colormap[i].red); |
| 3487 | green[i]=ScaleQuantumToShort(image->colormap[i].green); |
| 3488 | blue[i]=ScaleQuantumToShort(image->colormap[i].blue); |
| 3489 | } |
| 3490 | (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue); |
| 3491 | red=(uint16 *) RelinquishMagickMemory(red); |
| 3492 | green=(uint16 *) RelinquishMagickMemory(green); |
| 3493 | blue=(uint16 *) RelinquishMagickMemory(blue); |
| 3494 | } |
| 3495 | default: |
| 3496 | { |
| 3497 | /* |
| 3498 | Convert PseudoClass packets to contiguous grayscale scanlines. |
| 3499 | */ |
| 3500 | quantum_type=IndexQuantum; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 3501 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3502 | { |
| 3503 | if (photometric != PHOTOMETRIC_PALETTE) |
| 3504 | quantum_type=GrayAlphaQuantum; |
| 3505 | else |
| 3506 | quantum_type=IndexAlphaQuantum; |
| 3507 | } |
| 3508 | else |
| 3509 | if (photometric != PHOTOMETRIC_PALETTE) |
| 3510 | quantum_type=GrayQuantum; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3511 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3512 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3513 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3514 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3515 | |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3516 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3517 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3518 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3519 | length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info, |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 3520 | quantum_type,pixels,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3521 | if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1) |
| 3522 | break; |
| 3523 | if (image->previous == (Image *) NULL) |
| 3524 | { |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 3525 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
| 3526 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3527 | if (status == MagickFalse) |
| 3528 | break; |
| 3529 | } |
| 3530 | } |
| 3531 | break; |
| 3532 | } |
| 3533 | } |
| 3534 | quantum_info=DestroyQuantumInfo(quantum_info); |
cristy | 514da94 | 2012-07-06 23:24:14 +0000 | [diff] [blame] | 3535 | if (image->colorspace == LabColorspace) |
| 3536 | DecodeLabImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3537 | DestroyTIFFInfo(&tiff_info); |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 3538 | DisableMSCWarning(4127) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3539 | if (0 && (image_info->verbose == MagickTrue)) |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 3540 | RestoreMSCWarning |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3541 | TIFFPrintDirectory(tiff,stdout,MagickFalse); |
| 3542 | (void) TIFFWriteDirectory(tiff); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3543 | image=SyncNextImageInList(image); |
| 3544 | if (image == (Image *) NULL) |
| 3545 | break; |
| 3546 | status=SetImageProgress(image,SaveImagesTag,scene++, |
| 3547 | GetImageListLength(image)); |
| 3548 | if (status == MagickFalse) |
| 3549 | break; |
| 3550 | } while (image_info->adjoin != MagickFalse); |
| 3551 | (void) TIFFSetWarningHandler(warning_handler); |
| 3552 | (void) TIFFSetErrorHandler(error_handler); |
| 3553 | TIFFClose(tiff); |
| 3554 | return(MagickTrue); |
| 3555 | } |
| 3556 | #endif |