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