blob: 24dc3d847da26318e329fba670d7fed8431b29d3 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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*/
42#include "magick/studio.h"
cristy5a2ca482009-10-14 18:24:56 +000043#include "magick/attribute.h"
cristy3ed852e2009-09-05 21:47:34 +000044#include "magick/blob.h"
45#include "magick/blob-private.h"
46#include "magick/cache.h"
47#include "magick/color.h"
48#include "magick/color-private.h"
cristyf4cf3072010-04-25 20:07:02 +000049#include "magick/colormap.h"
cristy3ed852e2009-09-05 21:47:34 +000050#include "magick/colorspace.h"
51#include "magick/constitute.h"
52#include "magick/enhance.h"
53#include "magick/exception.h"
54#include "magick/exception-private.h"
55#include "magick/geometry.h"
56#include "magick/image.h"
57#include "magick/image-private.h"
58#include "magick/list.h"
59#include "magick/log.h"
60#include "magick/magick.h"
61#include "magick/memory_.h"
62#include "magick/module.h"
63#include "magick/monitor.h"
64#include "magick/monitor-private.h"
65#include "magick/option.h"
66#include "magick/property.h"
67#include "magick/quantum.h"
68#include "magick/quantum-private.h"
69#include "magick/profile.h"
70#include "magick/resize.h"
cristy3d7f8062009-09-24 20:45:35 +000071#include "magick/resource_.h"
cristy3ed852e2009-09-05 21:47:34 +000072#include "magick/semaphore.h"
73#include "magick/splay-tree.h"
74#include "magick/static.h"
75#include "magick/statistic.h"
76#include "magick/string_.h"
cristyf2f27272009-12-17 14:48:46 +000077#include "magick/string-private.h"
cristy3ed852e2009-09-05 21:47:34 +000078#include "magick/thread_.h"
79#include "magick/utility.h"
cristy3ed852e2009-09-05 21:47:34 +000080#if defined(MAGICKCORE_TIFF_DELEGATE)
81# if defined(MAGICKCORE_HAVE_TIFFCONF_H)
82# include "tiffconf.h"
cristyef8f26b2010-12-19 20:29:16 +000083# endif
cristy3ed852e2009-09-05 21:47:34 +000084# include "tiff.h"
85# include "tiffio.h"
86# if !defined(COMPRESSION_ADOBE_DEFLATE)
87# define COMPRESSION_ADOBE_DEFLATE 8
88# endif
cristyef8f26b2010-12-19 20:29:16 +000089# if !defined(PREDICTOR_HORIZONTAL)
90# define PREDICTOR_HORIZONTAL 2
91# endif
cristy3ed852e2009-09-05 21:47:34 +000092
93/*
94 Typedef declarations.
95*/
96#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
97typedef struct _ExifInfo
98{
99 unsigned int
100 tag,
101 type;
102
103 const char
104 *property;
105} ExifInfo;
106
107static const ExifInfo
108 exif_info[] = {
109 { EXIFTAG_EXPOSURETIME, TIFF_RATIONAL, "exif:ExposureTime" },
110 { EXIFTAG_FNUMBER, TIFF_RATIONAL, "exif:FNumber" },
111 { EXIFTAG_EXPOSUREPROGRAM, TIFF_SHORT, "exif:ExposureProgram" },
112 { EXIFTAG_SPECTRALSENSITIVITY, TIFF_ASCII, "exif:SpectralSensitivity" },
cristy0accf6e2009-10-01 13:14:28 +0000113 { EXIFTAG_ISOSPEEDRATINGS, TIFF_SHORT, "exif:ISOSpeedRatings" },
cristy3ed852e2009-09-05 21:47:34 +0000114 { EXIFTAG_OECF, TIFF_UNDEFINED, "exif:OptoelectricConversionFactor" },
115 { EXIFTAG_EXIFVERSION, TIFF_UNDEFINED, "exif:ExifVersion" },
116 { EXIFTAG_DATETIMEORIGINAL, TIFF_ASCII, "exif:DateTimeOriginal" },
117 { EXIFTAG_DATETIMEDIGITIZED, TIFF_ASCII, "exif:DateTimeDigitized" },
118 { EXIFTAG_COMPONENTSCONFIGURATION, TIFF_UNDEFINED, "exif:ComponentsConfiguration" },
119 { EXIFTAG_COMPRESSEDBITSPERPIXEL, TIFF_RATIONAL, "exif:CompressedBitsPerPixel" },
120 { EXIFTAG_SHUTTERSPEEDVALUE, TIFF_SRATIONAL, "exif:ShutterSpeedValue" },
121 { EXIFTAG_APERTUREVALUE, TIFF_RATIONAL, "exif:ApertureValue" },
122 { EXIFTAG_BRIGHTNESSVALUE, TIFF_SRATIONAL, "exif:BrightnessValue" },
123 { EXIFTAG_EXPOSUREBIASVALUE, TIFF_SRATIONAL, "exif:ExposureBiasValue" },
124 { EXIFTAG_MAXAPERTUREVALUE, TIFF_RATIONAL, "exif:MaxApertureValue" },
125 { EXIFTAG_SUBJECTDISTANCE, TIFF_RATIONAL, "exif:SubjectDistance" },
126 { EXIFTAG_METERINGMODE, TIFF_SHORT, "exif:MeteringMode" },
127 { EXIFTAG_LIGHTSOURCE, TIFF_SHORT, "exif:LightSource" },
128 { EXIFTAG_FLASH, TIFF_SHORT, "exif:Flash" },
129 { EXIFTAG_FOCALLENGTH, TIFF_RATIONAL, "exif:FocalLength" },
130 /* { EXIFTAG_SUBJECTAREA, TIFF_SHORT, "exif:SubjectArea" }, */
131 { EXIFTAG_MAKERNOTE, TIFF_UNDEFINED, "exif:MakerNote" },
132 { EXIFTAG_USERCOMMENT, TIFF_UNDEFINED, "exif:UserComment" },
133 { EXIFTAG_SUBSECTIME, TIFF_ASCII, "exif:SubSecTime" },
134 { EXIFTAG_SUBSECTIMEORIGINAL, TIFF_ASCII, "exif:SubSecTimeOriginal" },
135 { EXIFTAG_SUBSECTIMEDIGITIZED, TIFF_ASCII, "exif:SubSecTimeDigitized" },
136 { EXIFTAG_FLASHPIXVERSION, TIFF_UNDEFINED, "exif:FlashpixVersion" },
137 { EXIFTAG_PIXELXDIMENSION, TIFF_LONG, "exif:PixelXDimension" },
138 { EXIFTAG_PIXELXDIMENSION, TIFF_SHORT, "exif:PixelXDimension" },
139 { EXIFTAG_PIXELYDIMENSION, TIFF_LONG, "exif:PixelYDimension" },
140 { EXIFTAG_PIXELYDIMENSION, TIFF_SHORT, "exif:PixelYDimension" },
141 { EXIFTAG_RELATEDSOUNDFILE, TIFF_ASCII, "exif:RelatedSoundFile" },
142 { EXIFTAG_FLASHENERGY, TIFF_RATIONAL, "exif:FlashEnergy" },
143 { EXIFTAG_SPATIALFREQUENCYRESPONSE, TIFF_UNDEFINED, "exif:SpatialFrequencyResponse" },
144 { EXIFTAG_FOCALPLANEXRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneXResolution" },
145 { EXIFTAG_FOCALPLANEYRESOLUTION, TIFF_RATIONAL, "exif:FocalPlaneYResolution" },
146 { EXIFTAG_FOCALPLANERESOLUTIONUNIT, TIFF_SHORT, "exif:FocalPlaneResolutionUnit" },
147 { EXIFTAG_SUBJECTLOCATION, TIFF_SHORT, "exif:SubjectLocation" },
148 { EXIFTAG_EXPOSUREINDEX, TIFF_RATIONAL, "exif:ExposureIndex" },
149 { EXIFTAG_SENSINGMETHOD, TIFF_SHORT, "exif:SensingMethod" },
150 { EXIFTAG_FILESOURCE, TIFF_UNDEFINED, "exif:FileSource" },
151 { EXIFTAG_SCENETYPE, TIFF_UNDEFINED, "exif:SceneType" },
152 { EXIFTAG_CFAPATTERN, TIFF_UNDEFINED, "exif:CFAPattern" },
153 { EXIFTAG_CUSTOMRENDERED, TIFF_SHORT, "exif:CustomRendered" },
154 { EXIFTAG_EXPOSUREMODE, TIFF_SHORT, "exif:ExposureMode" },
155 { EXIFTAG_WHITEBALANCE, TIFF_SHORT, "exif:WhiteBalance" },
156 { EXIFTAG_DIGITALZOOMRATIO, TIFF_RATIONAL, "exif:DigitalZoomRatio" },
157 { EXIFTAG_FOCALLENGTHIN35MMFILM, TIFF_SHORT, "exif:FocalLengthIn35mmFilm" },
158 { EXIFTAG_SCENECAPTURETYPE, TIFF_SHORT, "exif:SceneCaptureType" },
159 { EXIFTAG_GAINCONTROL, TIFF_RATIONAL, "exif:GainControl" },
160 { EXIFTAG_CONTRAST, TIFF_SHORT, "exif:Contrast" },
161 { EXIFTAG_SATURATION, TIFF_SHORT, "exif:Saturation" },
162 { EXIFTAG_SHARPNESS, TIFF_SHORT, "exif:Sharpness" },
163 { EXIFTAG_DEVICESETTINGDESCRIPTION, TIFF_UNDEFINED, "exif:DeviceSettingDescription" },
164 { EXIFTAG_SUBJECTDISTANCERANGE, TIFF_SHORT, "exif:SubjectDistanceRange" },
165 { EXIFTAG_IMAGEUNIQUEID, TIFF_ASCII, "exif:ImageUniqueID" },
166 { 0, 0, (char *) NULL }
167};
168#endif
169
170/*
171 Global declarations.
172*/
173static MagickThreadKey
174 tiff_exception;
175
176static SemaphoreInfo
177 *tiff_semaphore = (SemaphoreInfo *) NULL;
178
179static volatile MagickBooleanType
180 instantiate_key = MagickFalse;
181#endif
182
183/*
184 Forward declarations.
185*/
186#if defined(MAGICKCORE_TIFF_DELEGATE)
cristy3d7f8062009-09-24 20:45:35 +0000187static Image *
188 ReadTIFFImage(const ImageInfo *,ExceptionInfo *);
189
cristy3ed852e2009-09-05 21:47:34 +0000190static MagickBooleanType
cristy3d7f8062009-09-24 20:45:35 +0000191 WriteGROUP4Image(const ImageInfo *,Image *),
cristy3ed852e2009-09-05 21:47:34 +0000192 WritePTIFImage(const ImageInfo *,Image *),
193 WriteTIFFImage(const ImageInfo *,Image *);
194#endif
195
196/*
197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198% %
199% %
200% %
201% I s T I F F %
202% %
203% %
204% %
205%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
206%
207% IsTIFF() returns MagickTrue if the image format type, identified by the
208% magick string, is TIFF.
209%
210% The format of the IsTIFF method is:
211%
212% MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
213%
214% A description of each parameter follows:
215%
216% o magick: compare image format pattern against these bytes.
217%
218% o length: Specifies the length of the magick string.
219%
220*/
221static MagickBooleanType IsTIFF(const unsigned char *magick,const size_t length)
222{
223 if (length < 4)
224 return(MagickFalse);
225 if (memcmp(magick,"\115\115\000\052",4) == 0)
226 return(MagickTrue);
227 if (memcmp(magick,"\111\111\052\000",4) == 0)
228 return(MagickTrue);
229#if defined(TIFF_VERSION_BIG)
230 if (length < 8)
231 return(MagickFalse);
232 if (memcmp(magick,"\115\115\000\053\000\010\000\000",8) == 0)
233 return(MagickTrue);
234 if (memcmp(magick,"\111\111\053\000\010\000\000\000",8) == 0)
235 return(MagickTrue);
236#endif
237 return(MagickFalse);
238}
239
240#if defined(MAGICKCORE_TIFF_DELEGATE)
241/*
242%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243% %
244% %
245% %
cristy3d7f8062009-09-24 20:45:35 +0000246% R e a d G R O U P 4 I m a g e %
247% %
248% %
249% %
250%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251%
252% ReadGROUP4Image() reads a raw CCITT Group 4 image file and returns it. It
253% allocates the memory necessary for the new Image structure and returns a
254% pointer to the new image.
255%
256% The format of the ReadGROUP4Image method is:
257%
258% Image *ReadGROUP4Image(const ImageInfo *image_info,
259% ExceptionInfo *exception)
260%
261% A description of each parameter follows:
262%
263% o image_info: the image info.
264%
265% o exception: return any errors or warnings in this structure.
266%
267*/
268
cristyeaedf062010-05-29 22:36:02 +0000269static inline size_t WriteLSBLong(FILE *file,const size_t value)
cristy3d7f8062009-09-24 20:45:35 +0000270{
271 unsigned char
272 buffer[4];
273
274 buffer[0]=(unsigned char) value;
275 buffer[1]=(unsigned char) (value >> 8);
276 buffer[2]=(unsigned char) (value >> 16);
277 buffer[3]=(unsigned char) (value >> 24);
278 return(fwrite(buffer,1,4,file));
279}
280
281static Image *ReadGROUP4Image(const ImageInfo *image_info,
282 ExceptionInfo *exception)
283{
284 char
285 filename[MaxTextExtent];
286
287 FILE
288 *file;
289
290 Image
291 *image;
292
293 ImageInfo
294 *read_info;
295
296 int
297 c,
298 unique_file;
299
300 MagickBooleanType
301 status;
302
303 size_t
304 length;
305
306 ssize_t
307 offset,
308 strip_offset;
309
310 /*
311 Open image file.
312 */
313 assert(image_info != (const ImageInfo *) NULL);
314 assert(image_info->signature == MagickSignature);
315 if (image_info->debug != MagickFalse)
316 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
317 image_info->filename);
318 assert(exception != (ExceptionInfo *) NULL);
319 assert(exception->signature == MagickSignature);
320 image=AcquireImage(image_info);
321 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
322 if (status == MagickFalse)
323 {
324 image=DestroyImageList(image);
325 return((Image *) NULL);
326 }
327 /*
328 Write raw CCITT Group 4 wrapped as a TIFF image file.
329 */
330 file=(FILE *) NULL;
331 unique_file=AcquireUniqueFileResource(filename);
332 if (unique_file != -1)
333 file=fdopen(unique_file,"wb");
334 if ((unique_file == -1) || (file == (FILE *) NULL))
335 ThrowImageException(FileOpenError,"UnableToCreateTemporaryFile");
336 length=fwrite("\111\111\052\000\010\000\000\000\016\000",1,10,file);
337 length=fwrite("\376\000\003\000\001\000\000\000\000\000\000\000",1,12,file);
338 length=fwrite("\000\001\004\000\001\000\000\000",1,8,file);
339 length=WriteLSBLong(file,image->columns);
340 length=fwrite("\001\001\004\000\001\000\000\000",1,8,file);
341 length=WriteLSBLong(file,image->rows);
342 length=fwrite("\002\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
343 length=fwrite("\003\001\003\000\001\000\000\000\004\000\000\000",1,12,file);
344 length=fwrite("\006\001\003\000\001\000\000\000\000\000\000\000",1,12,file);
345 length=fwrite("\021\001\003\000\001\000\000\000",1,8,file);
346 strip_offset=10+(12*14)+4+8;
cristybb503372010-05-27 20:51:26 +0000347 length=WriteLSBLong(file,(size_t) strip_offset);
cristy3d7f8062009-09-24 20:45:35 +0000348 length=fwrite("\022\001\003\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000349 length=WriteLSBLong(file,(size_t) image_info->orientation);
cristy3d7f8062009-09-24 20:45:35 +0000350 length=fwrite("\025\001\003\000\001\000\000\000\001\000\000\000",1,12,file);
351 length=fwrite("\026\001\004\000\001\000\000\000",1,8,file);
cristy94c8fe42009-10-06 01:57:36 +0000352 length=WriteLSBLong(file,image->rows);
cristy3d7f8062009-09-24 20:45:35 +0000353 length=fwrite("\027\001\004\000\001\000\000\000\000\000\000\000",1,12,file);
354 offset=(ssize_t) ftell(file)-4;
355 length=fwrite("\032\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000356 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000357 length=fwrite("\033\001\005\000\001\000\000\000",1,8,file);
cristybb503372010-05-27 20:51:26 +0000358 length=WriteLSBLong(file,(size_t) (strip_offset-8));
cristy3d7f8062009-09-24 20:45:35 +0000359 length=fwrite("\050\001\003\000\001\000\000\000\002\000\000\000",1,12,file);
360 length=fwrite("\000\000\000\000",1,4,file);
361 length=WriteLSBLong(file,image->x_resolution);
362 length=WriteLSBLong(file,1);
363 for (length=0; (c=ReadBlobByte(image)) != EOF; length++)
364 (void) fputc(c,file);
cristybb503372010-05-27 20:51:26 +0000365 offset=(ssize_t) fseek(file,(ssize_t) offset,SEEK_SET);
cristy3d7f8062009-09-24 20:45:35 +0000366 length=WriteLSBLong(file,(unsigned int) length);
367 (void) fclose(file);
368 (void) CloseBlob(image);
369 image=DestroyImage(image);
370 /*
371 Read TIFF image.
372 */
cristy14efd992009-09-26 23:46:03 +0000373 read_info=CloneImageInfo((ImageInfo *) NULL);
cristy53a8bc92011-01-10 19:15:17 +0000374 (void) FormatMagickString(read_info->filename,MaxTextExtent,"%s",filename);
cristy3d7f8062009-09-24 20:45:35 +0000375 image=ReadTIFFImage(read_info,exception);
376 read_info=DestroyImageInfo(read_info);
377 if (image != (Image *) NULL)
378 {
379 (void) CopyMagickString(image->filename,image_info->filename,
380 MaxTextExtent);
381 (void) CopyMagickString(image->magick_filename,image_info->filename,
382 MaxTextExtent);
383 (void) CopyMagickString(image->magick,"GROUP4",MaxTextExtent);
384 }
385 (void) RelinquishUniqueFileResource(filename);
386 return(image);
387}
388#endif
389
390#if defined(MAGICKCORE_TIFF_DELEGATE)
391/*
392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393% %
394% %
395% %
cristy3ed852e2009-09-05 21:47:34 +0000396% R e a d T I F F I m a g e %
397% %
398% %
399% %
400%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401%
402% ReadTIFFImage() reads a Tagged image file and returns it. It allocates the
403% memory necessary for the new Image structure and returns a pointer to the
404% new image.
405%
406% The format of the ReadTIFFImage method is:
407%
408% Image *ReadTIFFImage(const ImageInfo *image_info,
409% ExceptionInfo *exception)
410%
411% A description of each parameter follows:
412%
413% o image_info: the image info.
414%
415% o exception: return any errors or warnings in this structure.
416%
417*/
418
419static inline size_t MagickMax(const size_t x,const size_t y)
420{
421 if (x > y)
422 return(x);
423 return(y);
424}
425
cristybb503372010-05-27 20:51:26 +0000426static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +0000427{
428 if (x < y)
429 return(x);
430 return(y);
431}
432
433static MagickBooleanType ReadProfile(Image *image,const char *name,
cristybb503372010-05-27 20:51:26 +0000434 unsigned char *datum,ssize_t length)
cristy3ed852e2009-09-05 21:47:34 +0000435{
436 MagickBooleanType
437 status;
438
cristybb503372010-05-27 20:51:26 +0000439 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000440 i;
441
442 StringInfo
443 *profile;
444
445 if (length < 4)
446 return(MagickFalse);
447 i=0;
448 if ((LocaleCompare(name,"icc") != 0) && (LocaleCompare(name,"xmp") != 0))
449 {
450 for (i=0; i < (length-4); i+=2)
451 if (LocaleNCompare((char *) (datum+i),"8BIM",4) == 0)
452 break;
453 if (i == length)
454 length-=i;
455 else
456 i=0;
457 if (length < 4)
458 return(MagickFalse);
459 }
460 profile=AcquireStringInfo((size_t) length);
461 SetStringInfoDatum(profile,datum+i);
462 status=SetImageProfile(image,name,profile);
463 profile=DestroyStringInfo(profile);
464 if (status == MagickFalse)
465 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
466 image->filename);
467 return(MagickTrue);
468}
469
470#if defined(__cplusplus) || defined(c_plusplus)
471extern "C" {
472#endif
473
474static int TIFFCloseBlob(thandle_t image)
475{
476 (void) CloseBlob((Image *) image);
477 return(0);
478}
479
480static void TIFFErrors(const char *module,const char *format,va_list error)
481{
482 char
483 message[MaxTextExtent];
484
485 ExceptionInfo
486 *exception;
487
488#if defined(MAGICKCORE_HAVE_VSNPRINTF)
489 (void) vsnprintf(message,MaxTextExtent,format,error);
490#else
491 (void) vsprintf(message,format,error);
492#endif
493 (void) ConcatenateMagickString(message,".",MaxTextExtent);
494 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
495 if (exception != (ExceptionInfo *) NULL)
cristyb1db6232010-07-26 17:31:48 +0000496 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,message,
497 "`%s'",module);
cristy3ed852e2009-09-05 21:47:34 +0000498}
499
500static void TIFFGetProfiles(TIFF *tiff,Image *image)
501{
502 uint32
503 length;
504
505 unsigned char
506 *profile;
507
508#if defined(TIFFTAG_ICCPROFILE)
509 length=0;
510 if (TIFFGetField(tiff,TIFFTAG_ICCPROFILE,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000511 (void) ReadProfile(image,"icc",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000512#endif
513#if defined(TIFFTAG_PHOTOSHOP)
514 length=0;
515 if (TIFFGetField(tiff,TIFFTAG_PHOTOSHOP,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000516 (void) ReadProfile(image,"8bim",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000517#endif
518#if defined(TIFFTAG_RICHTIFFIPTC)
519 length=0;
520 if (TIFFGetField(tiff,TIFFTAG_RICHTIFFIPTC,&length,&profile) == 1)
521 {
522 if (TIFFIsByteSwapped(tiff) != 0)
cristybb503372010-05-27 20:51:26 +0000523 TIFFSwabArrayOfLong((uint32 *) profile,(size_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000524 (void) ReadProfile(image,"iptc",profile,4L*length);
525 }
526#endif
527#if defined(TIFFTAG_XMLPACKET)
528 length=0;
529 if (TIFFGetField(tiff,TIFFTAG_XMLPACKET,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000530 (void) ReadProfile(image,"xmp",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000531#endif
532 length=0;
533 if (TIFFGetField(tiff,37724,&length,&profile) == 1)
cristybb503372010-05-27 20:51:26 +0000534 (void) ReadProfile(image,"tiff:37724",profile,(ssize_t) length);
cristy3ed852e2009-09-05 21:47:34 +0000535}
536
537static void TIFFGetProperties(TIFF *tiff,Image *image)
538{
539 char
540 *text;
541
542 if (TIFFGetField(tiff,TIFFTAG_ARTIST,&text) == 1)
543 (void) SetImageProperty(image,"tiff:artist",text);
544 if (TIFFGetField(tiff,TIFFTAG_DATETIME,&text) == 1)
545 (void) SetImageProperty(image,"tiff:timestamp",text);
546 if (TIFFGetField(tiff,TIFFTAG_SOFTWARE,&text) == 1)
547 (void) SetImageProperty(image,"tiff:software",text);
548 if (TIFFGetField(tiff,TIFFTAG_HOSTCOMPUTER,&text) == 1)
549 (void) SetImageProperty(image,"tiff:hostcomputer",text);
550 if (TIFFGetField(tiff,TIFFTAG_DOCUMENTNAME,&text) == 1)
551 (void) SetImageProperty(image,"tiff:document",text);
552 if (TIFFGetField(tiff,TIFFTAG_MAKE,&text) == 1)
553 (void) SetImageProperty(image,"tiff:make",text);
554 if (TIFFGetField(tiff,TIFFTAG_MODEL,&text) == 1)
555 (void) SetImageProperty(image,"tiff:model",text);
556 if (TIFFGetField(tiff,33432,&text) == 1)
557 (void) SetImageProperty(image,"tiff:copyright",text);
558 if (TIFFGetField(tiff,TIFFTAG_PAGENAME,&text) == 1)
559 (void) SetImageProperty(image,"label",text);
560 if (TIFFGetField(tiff,TIFFTAG_IMAGEDESCRIPTION,&text) == 1)
561 (void) SetImageProperty(image,"comment",text);
562}
563
564static void TIFFGetEXIFProperties(TIFF *tiff,Image *image)
565{
566#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
567 char
568 value[MaxTextExtent];
569
cristybb503372010-05-27 20:51:26 +0000570 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000571 i;
572
573 tdir_t
574 directory;
575
576 uint32
577 offset;
578
579 /*
580 Read EXIF properties.
581 */
582 if (TIFFGetField(tiff,TIFFTAG_EXIFIFD,&offset) == 0)
583 return;
584 directory=TIFFCurrentDirectory(tiff);
585 if (TIFFReadEXIFDirectory(tiff,offset) == 0)
586 return;
587 for (i=0; exif_info[i].tag != 0; i++)
588 {
589 *value='\0';
590 switch (exif_info[i].type)
591 {
592 case TIFF_ASCII:
593 {
594 char
595 *ascii;
596
597 if (TIFFGetField(tiff,exif_info[i].tag,&ascii) != 0)
598 (void) CopyMagickMemory(value,ascii,MaxTextExtent);
599 break;
600 }
601 case TIFF_SHORT:
602 {
603 uint16
604 shorty;
605
606 if (TIFFGetField(tiff,exif_info[i].tag,&shorty) != 0)
607 (void) FormatMagickString(value,MaxTextExtent,"%d",shorty);
608 break;
609 }
610 case TIFF_LONG:
611 {
612 uint32
cristybb503372010-05-27 20:51:26 +0000613 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +0000614
cristybb503372010-05-27 20:51:26 +0000615 if (TIFFGetField(tiff,exif_info[i].tag,&ssize_ty) != 0)
616 (void) FormatMagickString(value,MaxTextExtent,"%d",ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +0000617 break;
618 }
619 case TIFF_RATIONAL:
620 case TIFF_SRATIONAL:
621 {
622 float
623 rational;
624
625 if (TIFFGetField(tiff,exif_info[i].tag,&rational) != 0)
cristye7f51092010-01-17 00:39:37 +0000626 (void) FormatMagickString(value,MaxTextExtent,"%g",rational);
cristy3ed852e2009-09-05 21:47:34 +0000627 break;
628 }
629 default:
630 break;
631 }
632 if (*value != '\0')
633 (void) SetImageProperty(image,exif_info[i].property,value);
634 }
635 TIFFSetDirectory(tiff,directory);
636#else
637 (void) tiff;
638 (void) image;
639#endif
640}
641
642static int TIFFMapBlob(thandle_t image,tdata_t *base,toff_t *size)
643{
644 *base=(tdata_t *) GetBlobStreamData((Image *) image);
645 if (*base != (tdata_t *) NULL)
646 *size=(toff_t) GetBlobSize((Image *) image);
647 if (*base != (tdata_t *) NULL)
648 return(1);
649 return(0);
650}
651
652static tsize_t TIFFReadBlob(thandle_t image,tdata_t data,tsize_t size)
653{
654 tsize_t
655 count;
656
657 count=(tsize_t) ReadBlob((Image *) image,(size_t) size,
658 (unsigned char *) data);
659 return(count);
660}
661
cristybb503372010-05-27 20:51:26 +0000662static int32 TIFFReadPixels(TIFF *tiff,size_t bits_per_sample,
663 tsample_t sample,ssize_t row,tdata_t scanline)
cristy3ed852e2009-09-05 21:47:34 +0000664{
665 int32
666 status;
667
668 (void) bits_per_sample;
669 status=TIFFReadScanline(tiff,scanline,(uint32) row,sample);
670 return(status);
671}
672
673static toff_t TIFFSeekBlob(thandle_t image,toff_t offset,int whence)
674{
675 return((toff_t) SeekBlob((Image *) image,(MagickOffsetType) offset,whence));
676}
677
678static toff_t TIFFGetBlobSize(thandle_t image)
679{
680 return((toff_t) GetBlobSize((Image *) image));
681}
682
683static void TIFFUnmapBlob(thandle_t image,tdata_t base,toff_t size)
684{
685 (void) image;
686 (void) base;
687 (void) size;
688}
689
690static void TIFFWarnings(const char *module,const char *format,va_list warning)
691{
692 char
693 message[MaxTextExtent];
694
695 ExceptionInfo
696 *exception;
697
698#if defined(MAGICKCORE_HAVE_VSNPRINTF)
699 (void) vsnprintf(message,MaxTextExtent,format,warning);
700#else
701 (void) vsprintf(message,format,warning);
702#endif
703 (void) ConcatenateMagickString(message,".",MaxTextExtent);
704 exception=(ExceptionInfo *) MagickGetThreadValue(tiff_exception);
705 if (exception != (ExceptionInfo *) NULL)
706 (void) ThrowMagickException(exception,GetMagickModule(),CoderWarning,
707 message,"`%s'",module);
708}
709
710static tsize_t TIFFWriteBlob(thandle_t image,tdata_t data,tsize_t size)
711{
712 tsize_t
713 count;
714
715 count=(tsize_t) WriteBlob((Image *) image,(size_t) size,
716 (unsigned char *) data);
717 return(count);
718}
719
720#if defined(__cplusplus) || defined(c_plusplus)
721}
722#endif
723
724static Image *ReadTIFFImage(const ImageInfo *image_info,
725 ExceptionInfo *exception)
726{
727 typedef enum
728 {
729 ReadSingleSampleMethod,
730 ReadRGBAMethod,
731 ReadCMYKAMethod,
732 ReadStripMethod,
733 ReadTileMethod,
734 ReadGenericMethod
735 } TIFFMethodType;
736
737 const char
738 *option;
739
740 float
741 *chromaticity,
742 x_position,
743 y_position,
744 x_resolution,
745 y_resolution;
746
747 Image
748 *image;
749
cristybb503372010-05-27 20:51:26 +0000750 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000751 y;
752
753 MagickBooleanType
cristy891dc792010-03-04 01:47:16 +0000754 associated_alpha,
cristy3ed852e2009-09-05 21:47:34 +0000755 debug,
756 status;
757
758 MagickSizeType
759 number_pixels;
760
761 QuantumInfo
762 *quantum_info;
763
764 QuantumType
765 quantum_type;
766
cristybb503372010-05-27 20:51:26 +0000767 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000768 i;
769
770 size_t
771 length,
772 pad;
773
774 TIFF
775 *tiff;
776
777 TIFFErrorHandler
778 error_handler,
779 warning_handler;
780
781 TIFFMethodType
782 method;
783
784 uint16
785 compress_tag,
786 bits_per_sample,
cristyf2687ca2010-06-29 16:32:38 +0000787 endian,
cristy3ed852e2009-09-05 21:47:34 +0000788 extra_samples,
789 interlace,
790 max_sample_value,
791 min_sample_value,
792 orientation,
793 pages,
794 photometric,
795 *sample_info,
796 sample_format,
797 samples_per_pixel,
798 units,
799 value;
800
801 uint32
802 height,
803 rows_per_strip,
804 width;
805
806 unsigned char
807 *pixels;
808
cristyf2687ca2010-06-29 16:32:38 +0000809 size_t
810 lsb_first;
811
cristy3ed852e2009-09-05 21:47:34 +0000812 /*
813 Open image.
814 */
815 assert(image_info != (const ImageInfo *) NULL);
816 assert(image_info->signature == MagickSignature);
817 if (image_info->debug != MagickFalse)
818 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
819 image_info->filename);
820 assert(exception != (ExceptionInfo *) NULL);
821 assert(exception->signature == MagickSignature);
822 image=AcquireImage(image_info);
823 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
824 if (status == MagickFalse)
825 {
826 image=DestroyImageList(image);
827 return((Image *) NULL);
828 }
829 (void) MagickSetThreadValue(tiff_exception,exception);
830 error_handler=TIFFSetErrorHandler(TIFFErrors);
831 warning_handler=TIFFSetWarningHandler(TIFFWarnings);
832 tiff=TIFFClientOpen(image->filename,"r",(thandle_t) image,TIFFReadBlob,
833 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
834 TIFFUnmapBlob);
835 if (tiff == (TIFF *) NULL)
836 {
837 (void) TIFFSetWarningHandler(warning_handler);
838 (void) TIFFSetErrorHandler(error_handler);
839 image=DestroyImageList(image);
840 return((Image *) NULL);
841 }
842 debug=IsEventLogging();
843 if (image_info->number_scenes != 0)
844 {
845 /*
846 Generate blank images for subimage specification (e.g. image.tif[4].
847 */
cristybb503372010-05-27 20:51:26 +0000848 for (i=0; i < (ssize_t) image_info->scene; i++)
cristy3ed852e2009-09-05 21:47:34 +0000849 {
850 (void) TIFFReadDirectory(tiff);
851 AcquireNextImage(image_info,image);
852 if (GetNextImageInList(image) == (Image *) NULL)
853 {
854 image=DestroyImageList(image);
855 return((Image *) NULL);
856 }
857 image=SyncNextImageInList(image);
858 }
859 }
860 do
861 {
862 if (0 && (image_info->verbose != MagickFalse))
863 TIFFPrintDirectory(tiff,stdout,MagickFalse);
cristy6b032822010-06-29 16:52:32 +0000864 (void) SetImageProperty(image,"tiff:endian",TIFFIsBigEndian(tiff) == 0 ?
865 "lsb" : "msb");
cristy3ed852e2009-09-05 21:47:34 +0000866 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_COMPRESSION,&compress_tag);
867 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_ORIENTATION,&orientation);
868 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGEWIDTH,&width);
869 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_IMAGELENGTH,&height);
cristyf2687ca2010-06-29 16:32:38 +0000870 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
cristy3ed852e2009-09-05 21:47:34 +0000871 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PLANARCONFIG,&interlace);
872 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,&bits_per_sample);
873 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLEFORMAT,&sample_format);
874 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MINSAMPLEVALUE,&min_sample_value);
875 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_MAXSAMPLEVALUE,&max_sample_value);
876 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PHOTOMETRIC,&photometric);
877 switch (photometric)
878 {
879 case PHOTOMETRIC_MINISBLACK:
880 {
881 (void) SetImageProperty(image,"tiff:photometric","min-is-black");
882 break;
883 }
884 case PHOTOMETRIC_MINISWHITE:
885 {
886 (void) SetImageProperty(image,"tiff:photometric","min-is-white");
887 break;
888 }
889 case PHOTOMETRIC_PALETTE:
890 {
891 (void) SetImageProperty(image,"tiff:photometric","palette");
892 break;
893 }
894 case PHOTOMETRIC_RGB:
895 {
896 (void) SetImageProperty(image,"tiff:photometric","RGB");
897 break;
898 }
899 case PHOTOMETRIC_CIELAB:
900 {
901 (void) SetImageProperty(image,"tiff:photometric","CIELAB");
902 break;
903 }
904 case PHOTOMETRIC_SEPARATED:
905 {
906 (void) SetImageProperty(image,"tiff:photometric","separated");
907 break;
908 }
cristy79e5dad2010-09-16 19:48:33 +0000909 case PHOTOMETRIC_YCBCR:
910 {
911 (void) SetImageProperty(image,"tiff:photometric","YCBCR");
912 break;
913 }
cristy3ed852e2009-09-05 21:47:34 +0000914 default:
915 {
916 (void) SetImageProperty(image,"tiff:photometric","unknown");
917 break;
918 }
919 }
920 if (image->debug != MagickFalse)
921 {
922 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Geometry: %ux%u",
923 (unsigned int) width,(unsigned int) height);
924 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Interlace: %u",
925 interlace);
926 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
927 "Bits per sample: %u",bits_per_sample);
928 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
929 "Min sample value: %u",min_sample_value);
930 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
931 "Max sample value: %u",max_sample_value);
932 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Photometric "
933 "interpretation: %s",GetImageProperty(image,"tiff:photometric"));
934 }
cristybb503372010-05-27 20:51:26 +0000935 image->columns=(size_t) width;
936 image->rows=(size_t) height;
937 image->depth=(size_t) bits_per_sample;
cristy3ed852e2009-09-05 21:47:34 +0000938 if (image->debug != MagickFalse)
cristye8c25f92010-06-03 00:53:06 +0000939 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"Image depth: %.20g",
940 (double) image->depth);
cristyf2687ca2010-06-29 16:32:38 +0000941 lsb_first=1;
942 image->endian=MSBEndian;
943 if ((int) (*(char *) &lsb_first) != 0)
944 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +0000945 if (photometric == PHOTOMETRIC_SEPARATED)
946 image->colorspace=CMYKColorspace;
947 if (photometric == PHOTOMETRIC_CIELAB)
948 image->colorspace=LabColorspace;
949 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
950 &samples_per_pixel);
951 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_RESOLUTIONUNIT,&units);
952 x_resolution=(float) image->x_resolution;
953 y_resolution=(float) image->y_resolution;
954 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XRESOLUTION,&x_resolution);
955 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YRESOLUTION,&y_resolution);
956 image->x_resolution=x_resolution;
957 image->y_resolution=y_resolution;
958 x_position=(float) image->page.x/x_resolution;
959 y_position=(float) image->page.y/y_resolution;
960 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_XPOSITION,&x_position);
961 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YPOSITION,&y_position);
cristybb503372010-05-27 20:51:26 +0000962 image->page.x=(ssize_t) ceil(x_position*x_resolution-0.5);
963 image->page.y=(ssize_t) ceil(y_position*y_resolution-0.5);
cristy3ed852e2009-09-05 21:47:34 +0000964 image->orientation=(OrientationType) orientation;
965 chromaticity=(float *) NULL;
966 (void) TIFFGetField(tiff,TIFFTAG_WHITEPOINT,&chromaticity);
967 if (chromaticity != (float *) NULL)
968 {
969 image->chromaticity.white_point.x=chromaticity[0];
970 image->chromaticity.white_point.y=chromaticity[1];
971 }
972 chromaticity=(float *) NULL;
973 (void) TIFFGetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,&chromaticity);
974 if (chromaticity != (float *) NULL)
975 {
976 image->chromaticity.red_primary.x=chromaticity[0];
977 image->chromaticity.red_primary.y=chromaticity[1];
978 image->chromaticity.green_primary.x=chromaticity[2];
979 image->chromaticity.green_primary.y=chromaticity[3];
980 image->chromaticity.blue_primary.x=chromaticity[4];
981 image->chromaticity.blue_primary.y=chromaticity[5];
982 }
983 TIFFGetProperties(tiff,image);
984 option=GetImageOption(image_info,"tiff:exif");
985 if ((option != (const char *) NULL) &&
986 (IsMagickTrue(option) != MagickFalse))
987 TIFFGetEXIFProperties(tiff,image);
988 TIFFGetProfiles(tiff,image);
989 /*
990 Allocate memory for the image and pixel buffer.
991 */
992#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
993 if ((compress_tag != COMPRESSION_NONE) &&
994 (TIFFIsCODECConfigured(compress_tag) == 0))
995 {
996 TIFFClose(tiff);
997 ThrowReaderException(CoderError,"CompressNotSupported");
998 }
999#endif
1000 switch (compress_tag)
1001 {
1002 case COMPRESSION_NONE: image->compression=NoCompression; break;
1003 case COMPRESSION_CCITTFAX3: image->compression=FaxCompression; break;
1004 case COMPRESSION_CCITTFAX4: image->compression=Group4Compression; break;
1005 case COMPRESSION_JPEG:
1006 {
1007 image->compression=JPEGCompression;
1008#if defined(JPEG_SUPPORT)
1009 {
1010 char
1011 sampling_factor[MaxTextExtent];
1012
1013 uint16
1014 horizontal,
1015 vertical;
1016
1017 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_YCBCRSUBSAMPLING,
1018 &horizontal,&vertical);
1019 (void) FormatMagickString(sampling_factor,MaxTextExtent,"%dx%d",
1020 horizontal,vertical);
1021 (void) SetImageProperty(image,"jpeg:sampling-factor",
1022 sampling_factor);
1023 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1024 "Sampling Factors: %s",sampling_factor);
1025 if ((samples_per_pixel > 1) && (photometric == PHOTOMETRIC_YCBCR))
1026 {
1027 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,
1028 JPEGCOLORMODE_RGB);
1029 photometric=PHOTOMETRIC_RGB;
1030 }
1031 }
1032#endif
1033 break;
1034 }
1035 case COMPRESSION_OJPEG: image->compression=JPEGCompression; break;
cristyfbb0ef02010-12-19 02:32:11 +00001036#if defined(COMPRESSION_LZMA)
1037 case COMPRESSION_LZMA: image->compression=LZMACompression; break;
1038#endif
cristy3ed852e2009-09-05 21:47:34 +00001039 case COMPRESSION_LZW: image->compression=LZWCompression; break;
1040 case COMPRESSION_DEFLATE: image->compression=ZipCompression; break;
1041 case COMPRESSION_ADOBE_DEFLATE: image->compression=ZipCompression; break;
1042 default: image->compression=RLECompression; break;
1043 }
1044 quantum_info=AcquireQuantumInfo(image_info,image);
1045 if (quantum_info == (QuantumInfo *) NULL)
cristy8d137bf2010-04-21 23:52:04 +00001046 {
1047 TIFFClose(tiff);
1048 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1049 }
cristy3ed852e2009-09-05 21:47:34 +00001050 if (sample_format == SAMPLEFORMAT_UINT)
1051 status=SetQuantumFormat(image,quantum_info,UnsignedQuantumFormat);
1052 if (sample_format == SAMPLEFORMAT_INT)
1053 status=SetQuantumFormat(image,quantum_info,SignedQuantumFormat);
1054 if (sample_format == SAMPLEFORMAT_IEEEFP)
1055 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
1056 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001057 {
1058 TIFFClose(tiff);
1059 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1060 }
cristy3ed852e2009-09-05 21:47:34 +00001061 status=MagickTrue;
1062 switch (photometric)
1063 {
1064 case PHOTOMETRIC_MINISBLACK:
1065 {
1066 quantum_info->min_is_white=MagickFalse;
1067 break;
1068 }
1069 case PHOTOMETRIC_MINISWHITE:
1070 {
1071 quantum_info->min_is_white=MagickTrue;
1072 break;
1073 }
1074 default:
1075 break;
1076 }
cristy891dc792010-03-04 01:47:16 +00001077 associated_alpha=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001078 extra_samples=0;
1079 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_EXTRASAMPLES,&extra_samples,
1080 &sample_info);
1081 if (extra_samples == 0)
1082 {
1083 if ((samples_per_pixel == 4) && (photometric == PHOTOMETRIC_RGB))
1084 image->matte=MagickTrue;
1085 }
1086 else
cristy6c207342010-02-19 17:49:50 +00001087 for (i=0; i < extra_samples; i++)
cristy3ed852e2009-09-05 21:47:34 +00001088 {
cristy7cf69862010-03-03 13:57:24 +00001089 image->matte=MagickTrue;
cristy6c207342010-02-19 17:49:50 +00001090 if (sample_info[i] == EXTRASAMPLE_ASSOCALPHA)
cristy891dc792010-03-04 01:47:16 +00001091 SetQuantumAlphaType(quantum_info,DisassociatedQuantumAlpha);
cristy3ed852e2009-09-05 21:47:34 +00001092 }
cristy891dc792010-03-04 01:47:16 +00001093 option=GetImageOption(image_info,"tiff:alpha");
1094 if (option != (const char *) NULL)
1095 associated_alpha=LocaleCompare(option,"associated") == 0 ? MagickTrue :
1096 MagickFalse;
1097 if (image->matte != MagickFalse)
1098 (void) SetImageProperty(image,"tiff:alpha",
1099 associated_alpha != MagickFalse ? "associated" : "unassociated");
cristy3ed852e2009-09-05 21:47:34 +00001100 if ((photometric == PHOTOMETRIC_PALETTE) &&
1101 (pow(2.0,1.0*bits_per_sample) <= MaxColormapSize))
1102 {
cristybb503372010-05-27 20:51:26 +00001103 size_t
cristy3ed852e2009-09-05 21:47:34 +00001104 colors;
1105
cristybb503372010-05-27 20:51:26 +00001106 colors=(size_t) GetQuantumRange(bits_per_sample)+1;
cristy3ed852e2009-09-05 21:47:34 +00001107 if (AcquireImageColormap(image,colors) == MagickFalse)
1108 {
1109 TIFFClose(tiff);
1110 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1111 }
1112 }
1113 if (units == RESUNIT_INCH)
1114 image->units=PixelsPerInchResolution;
1115 if (units == RESUNIT_CENTIMETER)
1116 image->units=PixelsPerCentimeterResolution;
1117 value=(unsigned short) image->scene;
1118 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_PAGENUMBER,&value,&pages);
1119 image->scene=value;
1120 if (image_info->ping != MagickFalse)
1121 {
1122 if (image_info->number_scenes != 0)
1123 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1124 break;
1125 goto next_tiff_frame;
1126 }
1127 method=ReadGenericMethod;
1128 if (TIFFGetField(tiff,TIFFTAG_ROWSPERSTRIP,&rows_per_strip) != 0)
1129 {
1130 char
1131 value[MaxTextExtent];
1132
1133 method=ReadStripMethod;
1134 (void) FormatMagickString(value,MaxTextExtent,"%u",
1135 (unsigned int) rows_per_strip);
1136 (void) SetImageProperty(image,"tiff:rows-per-strip",value);
1137 }
1138 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_CONTIG))
1139 method=ReadRGBAMethod;
1140 if ((samples_per_pixel >= 2) && (interlace == PLANARCONFIG_SEPARATE))
1141 method=ReadCMYKAMethod;
1142 if ((photometric != PHOTOMETRIC_RGB) &&
1143 (photometric != PHOTOMETRIC_CIELAB) &&
1144 (photometric != PHOTOMETRIC_SEPARATED))
1145 method=ReadGenericMethod;
1146 if (image->storage_class == PseudoClass)
1147 method=ReadSingleSampleMethod;
1148 if ((photometric == PHOTOMETRIC_MINISBLACK) ||
1149 (photometric == PHOTOMETRIC_MINISWHITE))
1150 method=ReadSingleSampleMethod;
1151 if ((photometric != PHOTOMETRIC_SEPARATED) &&
1152 (interlace == PLANARCONFIG_SEPARATE))
1153 method=ReadGenericMethod;
cristye978e2c2010-09-15 14:02:22 +00001154 if (image->compression == JPEGCompression)
1155 method=ReadGenericMethod;
cristy3ed852e2009-09-05 21:47:34 +00001156 if (TIFFIsTiled(tiff) != MagickFalse)
1157 method=ReadTileMethod;
1158 quantum_type=RGBQuantum;
1159 pixels=GetQuantumPixels(quantum_info);
1160 switch (method)
1161 {
1162 case ReadSingleSampleMethod:
1163 {
1164 /*
1165 Convert TIFF image to PseudoClass MIFF image.
1166 */
1167 if ((image->storage_class == PseudoClass) &&
1168 (photometric == PHOTOMETRIC_PALETTE))
1169 {
cristybb503372010-05-27 20:51:26 +00001170 size_t
cristy3ed852e2009-09-05 21:47:34 +00001171 range;
1172
1173 uint16
1174 *blue_colormap,
1175 *green_colormap,
1176 *red_colormap;
1177
1178 /*
1179 Initialize colormap.
1180 */
1181 (void) TIFFGetField(tiff,TIFFTAG_COLORMAP,&red_colormap,
1182 &green_colormap,&blue_colormap);
1183 range=255; /* might be old style 8-bit colormap */
cristybb503372010-05-27 20:51:26 +00001184 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001185 if ((red_colormap[i] >= 256) || (green_colormap[i] >= 256) ||
1186 (blue_colormap[i] >= 256))
1187 {
1188 range=65535;
1189 break;
1190 }
cristybb503372010-05-27 20:51:26 +00001191 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00001192 {
cristyce70c172010-01-07 17:15:30 +00001193 image->colormap[i].red=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001194 red_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001195 image->colormap[i].green=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001196 green_colormap[i])/range);
cristyce70c172010-01-07 17:15:30 +00001197 image->colormap[i].blue=ClampToQuantum(((double) QuantumRange*
cristy3ed852e2009-09-05 21:47:34 +00001198 blue_colormap[i])/range);
1199 }
1200 }
1201 quantum_type=IndexQuantum;
1202 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1203 if (image->matte != MagickFalse)
1204 {
1205 if (image->storage_class != PseudoClass)
1206 {
1207 quantum_type=samples_per_pixel == 1 ? AlphaQuantum :
1208 GrayAlphaQuantum;
1209 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1210 }
1211 else
1212 {
1213 quantum_type=IndexAlphaQuantum;
1214 pad=(size_t) MagickMax((size_t) samples_per_pixel-2,0);
1215 }
1216 }
1217 else
1218 if (image->storage_class != PseudoClass)
1219 {
1220 quantum_type=GrayQuantum;
1221 pad=(size_t) MagickMax((size_t) samples_per_pixel-1,0);
1222 }
1223 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1224 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001225 {
1226 TIFFClose(tiff);
1227 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1228 }
cristy3ed852e2009-09-05 21:47:34 +00001229 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001230 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001231 {
1232 int
1233 status;
1234
1235 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001236 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001237
1238 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1239 if (status == -1)
1240 break;
1241 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1242 if (q == (PixelPacket *) NULL)
1243 break;
1244 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1245 quantum_type,pixels,exception);
1246 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1247 break;
1248 if (image->previous == (Image *) NULL)
1249 {
cristycee97112010-05-28 00:44:52 +00001250 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1251 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001252 if (status == MagickFalse)
1253 break;
1254 }
1255 }
1256 break;
1257 }
1258 case ReadRGBAMethod:
1259 {
1260 /*
1261 Convert TIFF image to DirectClass MIFF image.
1262 */
1263 pad=(size_t) MagickMax((size_t) samples_per_pixel-3,0);
1264 quantum_type=RGBQuantum;
1265 if (image->matte != MagickFalse)
1266 {
1267 quantum_type=RGBAQuantum;
1268 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1269 }
1270 if (image->colorspace == CMYKColorspace)
1271 {
1272 pad=(size_t) MagickMax((size_t) samples_per_pixel-4,0);
1273 quantum_type=CMYKQuantum;
1274 if (image->matte != MagickFalse)
1275 {
1276 quantum_type=CMYKAQuantum;
1277 pad=(size_t) MagickMax((size_t) samples_per_pixel-5,0);
1278 }
1279 }
1280 status=SetQuantumPad(image,quantum_info,pad*((bits_per_sample+7) >> 3));
1281 if (status == MagickFalse)
cristy8d137bf2010-04-21 23:52:04 +00001282 {
1283 TIFFClose(tiff);
1284 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1285 }
cristy3ed852e2009-09-05 21:47:34 +00001286 pixels=GetQuantumPixels(quantum_info);
cristybb503372010-05-27 20:51:26 +00001287 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001288 {
1289 int
1290 status;
1291
1292 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001293 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001294
1295 status=TIFFReadPixels(tiff,bits_per_sample,0,y,(char *) pixels);
1296 if (status == -1)
1297 break;
1298 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1299 if (q == (PixelPacket *) NULL)
1300 break;
cristy0b334892010-03-15 02:26:46 +00001301 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1302 quantum_type,pixels,exception);
cristy3ed852e2009-09-05 21:47:34 +00001303 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1304 break;
1305 if (image->previous == (Image *) NULL)
1306 {
cristycee97112010-05-28 00:44:52 +00001307 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1308 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001309 if (status == MagickFalse)
1310 break;
1311 }
1312 }
1313 break;
1314 }
1315 case ReadCMYKAMethod:
1316 {
1317 /*
1318 Convert TIFF image to DirectClass MIFF image.
1319 */
cristybb503372010-05-27 20:51:26 +00001320 for (i=0; i < (ssize_t) samples_per_pixel; i++)
cristy3ed852e2009-09-05 21:47:34 +00001321 {
cristybb503372010-05-27 20:51:26 +00001322 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001323 {
cristy6d9f12f2009-11-03 14:50:26 +00001324 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001325 *restrict q;
cristy6d9f12f2009-11-03 14:50:26 +00001326
cristy3ed852e2009-09-05 21:47:34 +00001327 int
1328 status;
1329
1330 status=TIFFReadPixels(tiff,bits_per_sample,(tsample_t) i,y,(char *)
1331 pixels);
1332 if (status == -1)
1333 break;
1334 q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
1335 if (q == (PixelPacket *) NULL)
1336 break;
1337 if (image->colorspace != CMYKColorspace)
1338 switch (i)
1339 {
1340 case 0: quantum_type=RedQuantum; break;
1341 case 1: quantum_type=GreenQuantum; break;
1342 case 2: quantum_type=BlueQuantum; break;
1343 case 3: quantum_type=AlphaQuantum; break;
1344 default: quantum_type=UndefinedQuantum; break;
1345 }
cristy6d9f12f2009-11-03 14:50:26 +00001346 else
cristy3ed852e2009-09-05 21:47:34 +00001347 switch (i)
1348 {
1349 case 0: quantum_type=CyanQuantum; break;
1350 case 1: quantum_type=MagentaQuantum; break;
1351 case 2: quantum_type=YellowQuantum; break;
1352 case 3: quantum_type=BlackQuantum; break;
1353 case 4: quantum_type=AlphaQuantum; break;
1354 default: quantum_type=UndefinedQuantum; break;
1355 }
1356 length=ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1357 quantum_type,pixels,exception);
1358 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1359 break;
1360 }
1361 if (image->previous == (Image *) NULL)
1362 {
cristycee97112010-05-28 00:44:52 +00001363 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1364 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001365 if (status == MagickFalse)
1366 break;
1367 }
1368 }
1369 break;
1370 }
1371 case ReadStripMethod:
1372 {
1373 register unsigned char
1374 *p;
1375
1376 /*
1377 Convert stripped TIFF image to DirectClass MIFF image.
1378 */
1379 i=0;
1380 p=(unsigned char *) NULL;
cristybb503372010-05-27 20:51:26 +00001381 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001382 {
cristybb503372010-05-27 20:51:26 +00001383 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001384 x;
1385
1386 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001387 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001388
1389 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1390 if (q == (PixelPacket *) NULL)
1391 break;
1392 if (i == 0)
1393 {
1394 if (TIFFReadRGBAStrip(tiff,(tstrip_t) y,(uint32 *) pixels) == 0)
1395 break;
cristyeaedf062010-05-29 22:36:02 +00001396 i=(ssize_t) MagickMin((ssize_t) rows_per_strip,(ssize_t)
1397 image->rows-y);
cristy3ed852e2009-09-05 21:47:34 +00001398 }
1399 i--;
1400 p=pixels+image->columns*i;
cristybb503372010-05-27 20:51:26 +00001401 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001402 {
1403 q->red=ScaleCharToQuantum((unsigned char) (TIFFGetR(*p)));
1404 q->green=ScaleCharToQuantum((unsigned char) (TIFFGetG(*p)));
1405 q->blue=ScaleCharToQuantum((unsigned char) (TIFFGetB(*p)));
1406 if (image->matte != MagickFalse)
1407 q->opacity=ScaleCharToQuantum((unsigned char) (TIFFGetA(*p)));
1408 p++;
1409 q++;
1410 }
1411 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1412 break;
1413 if (image->previous == (Image *) NULL)
1414 {
cristycee97112010-05-28 00:44:52 +00001415 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1416 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001417 if (status == MagickFalse)
1418 break;
1419 }
1420 }
1421 break;
1422 }
1423 case ReadTileMethod:
1424 {
1425 register uint32
1426 *p;
1427
1428 uint32
1429 *tile_pixels,
1430 columns,
1431 rows;
1432
cristybb503372010-05-27 20:51:26 +00001433 size_t
cristy3ed852e2009-09-05 21:47:34 +00001434 number_pixels;
1435
1436 /*
1437 Convert tiled TIFF image to DirectClass MIFF image.
1438 */
1439 if ((TIFFGetField(tiff,TIFFTAG_TILEWIDTH,&columns) == 0) ||
1440 (TIFFGetField(tiff,TIFFTAG_TILELENGTH,&rows) == 0))
1441 {
1442 TIFFClose(tiff);
1443 ThrowReaderException(CoderError,"ImageIsNotTiled");
1444 }
1445 (void) SetImageStorageClass(image,DirectClass);
1446 number_pixels=columns*rows;
1447 tile_pixels=(uint32 *) AcquireQuantumMemory((size_t) columns*rows,
1448 sizeof(*tile_pixels));
1449 if (tile_pixels == (uint32 *) NULL)
1450 {
1451 TIFFClose(tiff);
1452 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1453 }
cristybb503372010-05-27 20:51:26 +00001454 for (y=0; y < (ssize_t) image->rows; y+=rows)
cristy3ed852e2009-09-05 21:47:34 +00001455 {
1456 PixelPacket
1457 *tile;
1458
cristybb503372010-05-27 20:51:26 +00001459 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001460 x;
1461
1462 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001463 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001464
cristybb503372010-05-27 20:51:26 +00001465 size_t
cristy3ed852e2009-09-05 21:47:34 +00001466 columns_remaining,
1467 rows_remaining;
1468
1469 rows_remaining=image->rows-y;
cristybb503372010-05-27 20:51:26 +00001470 if ((ssize_t) (y+rows) < (ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +00001471 rows_remaining=rows;
1472 tile=QueueAuthenticPixels(image,0,y,image->columns,rows_remaining,
1473 exception);
1474 if (tile == (PixelPacket *) NULL)
1475 break;
cristybb503372010-05-27 20:51:26 +00001476 for (x=0; x < (ssize_t) image->columns; x+=columns)
cristy3ed852e2009-09-05 21:47:34 +00001477 {
cristybb503372010-05-27 20:51:26 +00001478 size_t
cristy3ed852e2009-09-05 21:47:34 +00001479 column,
1480 row;
1481
1482 if (TIFFReadRGBATile(tiff,(uint32) x,(uint32) y,tile_pixels) == 0)
1483 break;
1484 columns_remaining=image->columns-x;
cristybb503372010-05-27 20:51:26 +00001485 if ((ssize_t) (x+columns) < (ssize_t) image->columns)
cristy3ed852e2009-09-05 21:47:34 +00001486 columns_remaining=columns;
1487 p=tile_pixels+(rows-rows_remaining)*columns;
1488 q=tile+(image->columns*(rows_remaining-1)+x);
1489 for (row=rows_remaining; row > 0; row--)
1490 {
1491 if (image->matte != MagickFalse)
1492 for (column=columns_remaining; column > 0; column--)
1493 {
1494 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1495 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1496 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1497 q->opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
1498 (unsigned char) TIFFGetA(*p)));
1499 q++;
1500 p++;
1501 }
1502 else
1503 for (column=columns_remaining; column > 0; column--)
1504 {
1505 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1506 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1507 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1508 q++;
1509 p++;
1510 }
1511 p+=columns-columns_remaining;
1512 q-=(image->columns+columns_remaining);
1513 }
1514 }
1515 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1516 break;
1517 if (image->previous == (Image *) NULL)
1518 {
cristycee97112010-05-28 00:44:52 +00001519 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1520 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001521 if (status == MagickFalse)
1522 break;
1523 }
1524 }
1525 tile_pixels=(uint32 *) RelinquishMagickMemory(tile_pixels);
1526 break;
1527 }
1528 case ReadGenericMethod:
1529 default:
1530 {
1531 register uint32
1532 *p;
1533
1534 uint32
1535 *pixels;
1536
1537 /*
1538 Convert TIFF image to DirectClass MIFF image.
1539 */
1540 number_pixels=(MagickSizeType) image->columns*image->rows;
1541 if ((number_pixels*sizeof(uint32)) != (MagickSizeType) ((size_t)
1542 (number_pixels*sizeof(uint32))))
1543 {
1544 TIFFClose(tiff);
1545 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1546 }
1547 pixels=(uint32 *) AcquireQuantumMemory(image->columns,image->rows*
1548 sizeof(uint32));
1549 if (pixels == (uint32 *) NULL)
1550 {
1551 TIFFClose(tiff);
1552 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1553 }
1554 (void) TIFFReadRGBAImage(tiff,(uint32) image->columns,
1555 (uint32) image->rows,(uint32 *) pixels,0);
1556 /*
1557 Convert image to DirectClass pixel packets.
1558 */
1559 p=pixels+number_pixels-1;
cristybb503372010-05-27 20:51:26 +00001560 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001561 {
cristybb503372010-05-27 20:51:26 +00001562 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001563 x;
1564
1565 register PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00001566 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001567
1568 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
1569 if (q == (PixelPacket *) NULL)
1570 break;
1571 q+=image->columns-1;
cristybb503372010-05-27 20:51:26 +00001572 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001573 {
1574 q->red=ScaleCharToQuantum((unsigned char) TIFFGetR(*p));
1575 q->green=ScaleCharToQuantum((unsigned char) TIFFGetG(*p));
1576 q->blue=ScaleCharToQuantum((unsigned char) TIFFGetB(*p));
1577 if (image->matte != MagickFalse)
cristycc768f42010-08-19 16:00:09 +00001578 q->opacity=(Quantum) (QuantumRange-
1579 ScaleCharToQuantum((unsigned char) TIFFGetA(*p)));
cristy3ed852e2009-09-05 21:47:34 +00001580 p--;
1581 q--;
1582 }
1583 if (SyncAuthenticPixels(image,exception) == MagickFalse)
1584 break;
1585 if (image->previous == (Image *) NULL)
1586 {
cristycee97112010-05-28 00:44:52 +00001587 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
1588 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001589 if (status == MagickFalse)
1590 break;
1591 }
1592 }
1593 pixels=(uint32 *) RelinquishMagickMemory(pixels);
1594 break;
1595 }
1596 }
1597 SetQuantumImageType(image,quantum_type);
1598 next_tiff_frame:
1599 if ((photometric == PHOTOMETRIC_LOGL) ||
1600 (photometric == PHOTOMETRIC_MINISBLACK) ||
1601 (photometric == PHOTOMETRIC_MINISWHITE))
1602 {
1603 image->type=GrayscaleType;
1604 if (bits_per_sample == 1)
1605 image->type=BilevelType;
1606 }
1607 if (image->storage_class == PseudoClass)
1608 image->depth=GetImageDepth(image,exception);
cristyf2687ca2010-06-29 16:32:38 +00001609 image->endian=MSBEndian;
1610 if (endian == FILLORDER_LSB2MSB)
1611 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00001612 if ((photometric == PHOTOMETRIC_LOGL) ||
1613 (photometric == PHOTOMETRIC_MINISBLACK) ||
1614 (photometric == PHOTOMETRIC_MINISWHITE))
1615 {
1616 image->type=GrayscaleType;
1617 if (bits_per_sample == 1)
1618 image->type=BilevelType;
1619 }
1620 /*
1621 Proceed to next image.
1622 */
1623 if (image_info->number_scenes != 0)
1624 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1625 break;
1626 status=TIFFReadDirectory(tiff) != 0 ? MagickTrue : MagickFalse;
1627 if (status == MagickTrue)
1628 {
1629 /*
1630 Allocate next image structure.
1631 */
1632 AcquireNextImage(image_info,image);
1633 if (GetNextImageInList(image) == (Image *) NULL)
1634 {
1635 image=DestroyImageList(image);
1636 return((Image *) NULL);
1637 }
1638 image=SyncNextImageInList(image);
1639 status=SetImageProgress(image,LoadImagesTag,image->scene-1,
1640 image->scene);
1641 if (status == MagickFalse)
1642 break;
1643 }
1644 quantum_info=DestroyQuantumInfo(quantum_info);
1645 } while (status == MagickTrue);
1646 (void) TIFFSetWarningHandler(warning_handler);
1647 (void) TIFFSetErrorHandler(error_handler);
1648 TIFFClose(tiff);
1649 return(GetFirstImageInList(image));
1650}
1651#endif
1652
1653/*
1654%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1655% %
1656% %
1657% %
1658% R e g i s t e r T I F F I m a g e %
1659% %
1660% %
1661% %
1662%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1663%
1664% RegisterTIFFImage() adds properties for the TIFF image format to
1665% the list of supported formats. The properties include the image format
1666% tag, a method to read and/or write the format, whether the format
1667% supports the saving of more than one frame to the same file or blob,
1668% whether the format supports native in-memory I/O, and a brief
1669% description of the format.
1670%
1671% The format of the RegisterTIFFImage method is:
1672%
cristybb503372010-05-27 20:51:26 +00001673% size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001674%
1675*/
cristybb503372010-05-27 20:51:26 +00001676ModuleExport size_t RegisterTIFFImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001677{
1678#define TIFFDescription "Tagged Image File Format"
1679
1680 char
1681 version[MaxTextExtent];
1682
1683 MagickInfo
1684 *entry;
cristy6b032822010-06-29 16:52:32 +00001685
cristy18b17442009-10-25 18:36:48 +00001686 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1687 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001688 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001689 if (instantiate_key == MagickFalse)
1690 {
1691 if (MagickCreateThreadKey(&tiff_exception) == MagickFalse)
1692 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1693 instantiate_key=MagickTrue;
1694 }
cristyf84a1932010-01-03 18:00:18 +00001695 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001696 *version='\0';
1697#if defined(TIFF_VERSION)
1698 (void) FormatMagickString(version,MaxTextExtent,"%d",TIFF_VERSION);
1699#endif
1700#if defined(MAGICKCORE_TIFF_DELEGATE)
1701 {
1702 const char
1703 *p;
1704
cristybb503372010-05-27 20:51:26 +00001705 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001706 i;
1707
1708 p=TIFFGetVersion();
1709 for (i=0; (i < (MaxTextExtent-1)) && (*p != 0) && (*p != '\n'); i++)
1710 version[i]=(*p++);
1711 version[i]='\0';
1712 }
1713#endif
1714
cristy3d7f8062009-09-24 20:45:35 +00001715 entry=SetMagickInfo("GROUP4");
1716#if defined(MAGICKCORE_TIFF_DELEGATE)
1717 entry->decoder=(DecodeImageHandler *) ReadGROUP4Image;
1718 entry->encoder=(EncodeImageHandler *) WriteGROUP4Image;
1719#endif
1720 entry->raw=MagickTrue;
1721 entry->endian_support=MagickTrue;
cristy2d6ccc32009-09-25 03:18:25 +00001722 entry->adjoin=MagickFalse;
cristy38a69f12010-07-26 01:03:12 +00001723 entry->format_type=ImplicitFormatType;
cristy3d7f8062009-09-24 20:45:35 +00001724 entry->seekable_stream=MagickTrue;
1725 entry->thread_support=NoThreadSupport;
1726 entry->description=ConstantString("Raw CCITT Group4");
1727 entry->module=ConstantString("TIFF");
1728 (void) RegisterMagickInfo(entry);
cristy3ed852e2009-09-05 21:47:34 +00001729 entry=SetMagickInfo("PTIF");
1730#if defined(MAGICKCORE_TIFF_DELEGATE)
1731 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1732 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1733#endif
1734 entry->endian_support=MagickTrue;
1735 entry->seekable_stream=MagickTrue;
1736 entry->thread_support=NoThreadSupport;
1737 entry->description=ConstantString("Pyramid encoded TIFF");
1738 entry->module=ConstantString("TIFF");
1739 (void) RegisterMagickInfo(entry);
1740 entry=SetMagickInfo("TIF");
1741#if defined(MAGICKCORE_TIFF_DELEGATE)
1742 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1743 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1744#endif
1745 entry->endian_support=MagickTrue;
1746 entry->seekable_stream=MagickTrue;
1747 entry->stealth=MagickTrue;
1748 entry->thread_support=NoThreadSupport;
1749 entry->description=ConstantString(TIFFDescription);
1750 if (*version != '\0')
1751 entry->version=ConstantString(version);
1752 entry->module=ConstantString("TIFF");
1753 (void) RegisterMagickInfo(entry);
1754 entry=SetMagickInfo("TIFF");
1755#if defined(MAGICKCORE_TIFF_DELEGATE)
1756 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1757 entry->encoder=(EncodeImageHandler *) WriteTIFFImage;
1758#endif
1759 entry->magick=(IsImageFormatHandler *) IsTIFF;
1760 entry->endian_support=MagickTrue;
1761 entry->seekable_stream=MagickTrue;
1762 entry->thread_support=NoThreadSupport;
1763 entry->description=ConstantString(TIFFDescription);
1764 if (*version != '\0')
1765 entry->version=ConstantString(version);
1766 entry->module=ConstantString("TIFF");
1767 (void) RegisterMagickInfo(entry);
1768 entry=SetMagickInfo("TIFF64");
1769#if defined(TIFF_VERSION_BIG)
1770 entry->decoder=(DecodeImageHandler *) ReadTIFFImage;
1771 entry->encoder=(EncodeImageHandler *) WritePTIFImage;
1772#endif
1773 entry->adjoin=MagickFalse;
1774 entry->endian_support=MagickTrue;
1775 entry->seekable_stream=MagickTrue;
1776 entry->thread_support=NoThreadSupport;
1777 entry->description=ConstantString("Tagged Image File Format (64-bit)");
1778 if (*version != '\0')
1779 entry->version=ConstantString(version);
1780 entry->module=ConstantString("TIFF");
1781 (void) RegisterMagickInfo(entry);
1782 return(MagickImageCoderSignature);
1783}
1784
1785/*
1786%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1787% %
1788% %
1789% %
1790% U n r e g i s t e r T I F F I m a g e %
1791% %
1792% %
1793% %
1794%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1795%
1796% UnregisterTIFFImage() removes format registrations made by the TIFF module
1797% from the list of supported formats.
1798%
1799% The format of the UnregisterTIFFImage method is:
1800%
1801% UnregisterTIFFImage(void)
1802%
1803*/
1804ModuleExport void UnregisterTIFFImage(void)
1805{
cristy3d7f8062009-09-24 20:45:35 +00001806 (void) UnregisterMagickInfo("RAWGROUP4");
cristy3ed852e2009-09-05 21:47:34 +00001807 (void) UnregisterMagickInfo("PTIF");
1808 (void) UnregisterMagickInfo("TIF");
1809 (void) UnregisterMagickInfo("TIFF");
1810 (void) UnregisterMagickInfo("TIFF64");
cristy514e9e72009-11-20 02:12:08 +00001811 if (tiff_semaphore == (SemaphoreInfo *) NULL)
1812 tiff_semaphore=AllocateSemaphoreInfo();
cristyf84a1932010-01-03 18:00:18 +00001813 LockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001814 if (instantiate_key != MagickFalse)
1815 if (MagickDeleteThreadKey(tiff_exception) == MagickFalse)
1816 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1817 instantiate_key=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001818 UnlockSemaphoreInfo(tiff_semaphore);
cristy3ed852e2009-09-05 21:47:34 +00001819 DestroySemaphoreInfo(&tiff_semaphore);
1820}
1821
1822#if defined(MAGICKCORE_TIFF_DELEGATE)
1823/*
1824%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1825% %
1826% %
1827% %
cristy3d7f8062009-09-24 20:45:35 +00001828% W r i t e G R O U P 4 I m a g e %
1829% %
1830% %
1831% %
1832%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1833%
1834% WriteGROUP4Image() writes an image in the raw CCITT Group 4 image format.
1835%
1836% The format of the WriteGROUP4Image method is:
1837%
1838% MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1839% Image *image)
1840%
1841% A description of each parameter follows:
1842%
1843% o image_info: the image info.
1844%
1845% o image: The image.
1846%
1847*/
1848static MagickBooleanType WriteGROUP4Image(const ImageInfo *image_info,
1849 Image *image)
1850{
1851 char
1852 filename[MaxTextExtent];
1853
1854 FILE
1855 *file;
1856
1857 Image
1858 *huffman_image;
1859
1860 ImageInfo
1861 *write_info;
1862
1863 int
1864 unique_file;
1865
1866 MagickBooleanType
1867 status;
1868
cristybb503372010-05-27 20:51:26 +00001869 register ssize_t
cristy3d7f8062009-09-24 20:45:35 +00001870 i;
1871
1872 ssize_t
1873 count;
1874
1875 TIFF
1876 *tiff;
1877
cristy94c8fe42009-10-06 01:57:36 +00001878 toff_t
cristy3d7f8062009-09-24 20:45:35 +00001879 *byte_count,
1880 strip_size;
1881
1882 unsigned char
1883 *buffer;
1884
1885 /*
1886 Write image as CCITT Group4 TIFF image to a temporary file.
1887 */
1888 assert(image_info != (const ImageInfo *) NULL);
1889 assert(image_info->signature == MagickSignature);
1890 assert(image != (Image *) NULL);
1891 assert(image->signature == MagickSignature);
1892 if (image->debug != MagickFalse)
1893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1894 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
1895 if (status == MagickFalse)
1896 return(status);
1897 huffman_image=CloneImage(image,0,0,MagickTrue,&image->exception);
1898 if (huffman_image == (Image *) NULL)
1899 {
1900 (void) CloseBlob(image);
1901 return(MagickFalse);
1902 }
cristy94c8fe42009-10-06 01:57:36 +00001903 huffman_image->endian=MSBEndian;
cristy3d7f8062009-09-24 20:45:35 +00001904 file=(FILE *) NULL;
1905 unique_file=AcquireUniqueFileResource(filename);
1906 if (unique_file != -1)
cristy0accf6e2009-10-01 13:14:28 +00001907 file=fdopen(unique_file,"wb");
cristy3d7f8062009-09-24 20:45:35 +00001908 if ((unique_file == -1) || (file == (FILE *) NULL))
1909 {
1910 ThrowFileException(&image->exception,FileOpenError,
1911 "UnableToCreateTemporaryFile",filename);
1912 return(MagickFalse);
1913 }
1914 (void) FormatMagickString(huffman_image->filename,MaxTextExtent,"tiff:%s",
1915 filename);
1916 (void) SetImageType(huffman_image,BilevelType);
cristy14efd992009-09-26 23:46:03 +00001917 write_info=CloneImageInfo((ImageInfo *) NULL);
cristy3d7f8062009-09-24 20:45:35 +00001918 SetImageInfoFile(write_info,file);
1919 write_info->compression=Group4Compression;
1920 write_info->type=BilevelType;
1921 (void) SetImageOption(write_info,"quantum:polarity","min-is-white");
1922 status=WriteTIFFImage(write_info,huffman_image);
1923 (void) fflush(file);
1924 write_info=DestroyImageInfo(write_info);
1925 if (status == MagickFalse)
1926 {
1927 InheritException(&image->exception,&huffman_image->exception);
1928 huffman_image=DestroyImage(huffman_image);
1929 (void) fclose(file);
1930 (void) RelinquishUniqueFileResource(filename);
1931 return(MagickFalse);
1932 }
1933 tiff=TIFFOpen(filename,"rb");
1934 if (tiff == (TIFF *) NULL)
1935 {
1936 huffman_image=DestroyImage(huffman_image);
1937 (void) fclose(file);
1938 (void) RelinquishUniqueFileResource(filename);
1939 ThrowFileException(&image->exception,FileOpenError,"UnableToOpenFile",
1940 image_info->filename);
1941 return(MagickFalse);
1942 }
1943 /*
1944 Allocate raw strip buffer.
1945 */
cristy94c8fe42009-10-06 01:57:36 +00001946 if (TIFFGetField(tiff,TIFFTAG_STRIPBYTECOUNTS,&byte_count) != 1)
1947 {
1948 TIFFClose(tiff);
1949 huffman_image=DestroyImage(huffman_image);
1950 (void) fclose(file);
1951 (void) RelinquishUniqueFileResource(filename);
1952 return(MagickFalse);
1953 }
cristy3d7f8062009-09-24 20:45:35 +00001954 strip_size=byte_count[0];
cristybb503372010-05-27 20:51:26 +00001955 for (i=1; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001956 if (byte_count[i] > strip_size)
1957 strip_size=byte_count[i];
1958 buffer=(unsigned char *) AcquireQuantumMemory((size_t) strip_size,
1959 sizeof(*buffer));
1960 if (buffer == (unsigned char *) NULL)
1961 {
1962 TIFFClose(tiff);
1963 huffman_image=DestroyImage(huffman_image);
1964 (void) fclose(file);
1965 (void) RelinquishUniqueFileResource(filename);
1966 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1967 image_info->filename);
1968 }
1969 /*
1970 Compress runlength encoded to 2D Huffman pixels.
1971 */
cristybb503372010-05-27 20:51:26 +00001972 for (i=0; i < (ssize_t) TIFFNumberOfStrips(tiff); i++)
cristy3d7f8062009-09-24 20:45:35 +00001973 {
cristy94c8fe42009-10-06 01:57:36 +00001974 count=(ssize_t) TIFFReadRawStrip(tiff,(uint32) i,buffer,strip_size);
cristy3d7f8062009-09-24 20:45:35 +00001975 if (WriteBlob(image,(size_t) count,buffer) != count)
1976 status=MagickFalse;
1977 }
1978 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
1979 TIFFClose(tiff);
1980 huffman_image=DestroyImage(huffman_image);
1981 (void) fclose(file);
1982 (void) RelinquishUniqueFileResource(filename);
1983 (void) CloseBlob(image);
1984 return(status);
1985}
1986#endif
1987
1988#if defined(MAGICKCORE_TIFF_DELEGATE)
1989/*
1990%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1991% %
1992% %
1993% %
cristy3ed852e2009-09-05 21:47:34 +00001994% W r i t e P T I F I m a g e %
1995% %
1996% %
1997% %
1998%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1999%
2000% WritePTIFImage() writes an image in the pyrimid-encoded Tagged image file
2001% format.
2002%
2003% The format of the WritePTIFImage method is:
2004%
2005% MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2006% Image *image)
2007%
2008% A description of each parameter follows:
2009%
2010% o image_info: the image info.
2011%
2012% o image: The image.
2013%
2014*/
2015static MagickBooleanType WritePTIFImage(const ImageInfo *image_info,
2016 Image *image)
2017{
2018 Image
2019 *images,
2020 *next,
2021 *pyramid_image;
2022
2023 ImageInfo
2024 *write_info;
2025
2026 MagickBooleanType
2027 status;
2028
cristybb503372010-05-27 20:51:26 +00002029 size_t
cristy3ed852e2009-09-05 21:47:34 +00002030 columns,
2031 rows;
2032
2033 /*
2034 Create pyramid-encoded TIFF image.
2035 */
2036 images=NewImageList();
2037 for (next=image; next != (Image *) NULL; next=GetNextImageInList(next))
2038 {
2039 AppendImageToList(&images,CloneImage(next,0,0,MagickFalse,
2040 &image->exception));
2041 columns=next->columns;
2042 rows=next->rows;
2043 while ((columns > 64) && (rows > 64))
2044 {
2045 columns/=2;
2046 rows/=2;
2047 pyramid_image=ResizeImage(next,columns,rows,UndefinedFilter,image->blur,
2048 &image->exception);
2049 AppendImageToList(&images,pyramid_image);
2050 }
2051 }
2052 /*
2053 Write pyramid-encoded TIFF image.
2054 */
cristy58567532009-09-26 01:13:32 +00002055 write_info=CloneImageInfo(image_info);
cristy3ed852e2009-09-05 21:47:34 +00002056 *write_info->magick='\0';
2057 write_info->adjoin=MagickTrue;
2058 status=WriteTIFFImage(write_info,GetFirstImageInList(images));
2059 images=DestroyImageList(images);
2060 write_info=DestroyImageInfo(write_info);
2061 return(status);
2062}
2063#endif
2064
2065#if defined(MAGICKCORE_TIFF_DELEGATE)
2066/*
2067%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2068% %
2069% %
2070% W r i t e T I F F I m a g e %
2071% %
2072% %
2073% %
2074%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2075%
2076% WriteTIFFImage() writes an image in the Tagged image file format.
2077%
2078% The format of the WriteTIFFImage method is:
2079%
2080% MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2081% Image *image)
2082%
2083% A description of each parameter follows:
2084%
2085% o image_info: the image info.
2086%
2087% o image: The image.
2088%
2089*/
2090
2091typedef struct _TIFFInfo
2092{
2093 RectangleInfo
2094 tile_geometry;
2095
2096 unsigned char
2097 *scanline,
2098 *scanlines,
2099 *pixels;
2100} TIFFInfo;
2101
2102static void DestroyTIFFInfo(TIFFInfo *tiff_info)
2103{
2104 assert(tiff_info != (TIFFInfo *) NULL);
2105 if (tiff_info->scanlines != (unsigned char *) NULL)
2106 tiff_info->scanlines=(unsigned char *) RelinquishMagickMemory(
2107 tiff_info->scanlines);
2108 if (tiff_info->pixels != (unsigned char *) NULL)
2109 tiff_info->pixels=(unsigned char *) RelinquishMagickMemory(
2110 tiff_info->pixels);
2111}
2112
2113static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,TIFF *tiff,
2114 TIFFInfo *tiff_info)
2115{
2116 const char
2117 *option;
2118
2119 MagickStatusType
2120 flags;
2121
cristy79e5dad2010-09-16 19:48:33 +00002122 uint32
2123 tile_columns,
2124 tile_rows;
2125
cristy3ed852e2009-09-05 21:47:34 +00002126 assert(tiff_info != (TIFFInfo *) NULL);
2127 (void) ResetMagickMemory(tiff_info,0,sizeof(*tiff_info));
2128 option=GetImageOption(image_info,"tiff:tile-geometry");
2129 if (option == (const char *) NULL)
2130 return(MagickTrue);
2131 flags=ParseAbsoluteGeometry(option,&tiff_info->tile_geometry);
2132 if ((flags & HeightValue) == 0)
2133 tiff_info->tile_geometry.height=tiff_info->tile_geometry.width;
cristydf6d6d42010-09-18 02:15:37 +00002134 tile_columns=(uint32) tiff_info->tile_geometry.width;
2135 tile_rows=(uint32) tiff_info->tile_geometry.height;
cristy79e5dad2010-09-16 19:48:33 +00002136 TIFFDefaultTileSize(tiff,&tile_columns,&tile_rows);
2137 (void) TIFFSetField(tiff,TIFFTAG_TILEWIDTH,tile_columns);
2138 (void) TIFFSetField(tiff,TIFFTAG_TILELENGTH,tile_rows);
2139 tiff_info->tile_geometry.width=tile_columns;
2140 tiff_info->tile_geometry.height=tile_rows;
cristy3ed852e2009-09-05 21:47:34 +00002141 tiff_info->scanlines=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002142 tile_rows*TIFFScanlineSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002143 tiff_info->pixels=(unsigned char *) AcquireQuantumMemory((size_t)
cristy79e5dad2010-09-16 19:48:33 +00002144 tile_rows*TIFFTileSize(tiff),sizeof(*tiff_info->scanlines));
cristy3ed852e2009-09-05 21:47:34 +00002145 if ((tiff_info->scanlines == (unsigned char *) NULL) ||
2146 (tiff_info->pixels == (unsigned char *) NULL))
2147 {
2148 DestroyTIFFInfo(tiff_info);
2149 return(MagickFalse);
2150 }
2151 return(MagickTrue);
2152}
2153
cristybb503372010-05-27 20:51:26 +00002154static int32 TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
cristy3ed852e2009-09-05 21:47:34 +00002155 tsample_t sample,Image *image)
2156{
2157 int32
2158 status;
2159
cristybb503372010-05-27 20:51:26 +00002160 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002161 bytes_per_pixel,
2162 j,
2163 k,
2164 l;
2165
cristybb503372010-05-27 20:51:26 +00002166 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002167 i;
2168
2169 register unsigned char
2170 *p,
2171 *q;
2172
cristybb503372010-05-27 20:51:26 +00002173 size_t
cristy3ed852e2009-09-05 21:47:34 +00002174 number_tiles,
2175 tile_width;
2176
2177 if (TIFFIsTiled(tiff) == 0)
2178 return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
2179 /*
2180 Fill scanlines to tile height.
2181 */
cristybb503372010-05-27 20:51:26 +00002182 i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
cristy3ed852e2009-09-05 21:47:34 +00002183 (void) CopyMagickMemory(tiff_info->scanlines+i,(char *) tiff_info->scanline,
2184 (size_t) TIFFScanlineSize(tiff));
cristybb503372010-05-27 20:51:26 +00002185 if (((size_t) (row % tiff_info->tile_geometry.height) !=
2186 (tiff_info->tile_geometry.height-1)) && (row != (ssize_t) (image->rows-1)))
cristy3ed852e2009-09-05 21:47:34 +00002187 return(0);
2188 /*
2189 Write tile to TIFF image.
2190 */
2191 status=0;
cristybb503372010-05-27 20:51:26 +00002192 bytes_per_pixel=TIFFTileSize(tiff)/(ssize_t) (tiff_info->tile_geometry.height*
cristy3ed852e2009-09-05 21:47:34 +00002193 tiff_info->tile_geometry.width);
2194 number_tiles=(image->columns+tiff_info->tile_geometry.width)/
2195 tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002196 for (i=0; i < (ssize_t) number_tiles; i++)
cristy3ed852e2009-09-05 21:47:34 +00002197 {
cristybb503372010-05-27 20:51:26 +00002198 tile_width=(i == (ssize_t) (number_tiles-1)) ? image->columns-(i*
cristy3ed852e2009-09-05 21:47:34 +00002199 tiff_info->tile_geometry.width) : tiff_info->tile_geometry.width;
cristybb503372010-05-27 20:51:26 +00002200 for (j=0; j < (ssize_t) ((row % tiff_info->tile_geometry.height)+1); j++)
2201 for (k=0; k < (ssize_t) tile_width; k++)
cristy3ed852e2009-09-05 21:47:34 +00002202 {
2203 if (bytes_per_pixel == 0)
2204 {
2205 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2206 tiff_info->tile_geometry.width+k)/8);
2207 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k/8);
2208 *q++=(*p++);
2209 continue;
2210 }
2211 p=tiff_info->scanlines+(j*TIFFScanlineSize(tiff)+(i*
2212 tiff_info->tile_geometry.width+k)*bytes_per_pixel);
2213 q=tiff_info->pixels+(j*TIFFTileRowSize(tiff)+k*bytes_per_pixel);
2214 for (l=0; l < bytes_per_pixel; l++)
2215 *q++=(*p++);
2216 }
cristydaff8092010-04-22 14:50:53 +00002217 if ((i*tiff_info->tile_geometry.width) != image->columns)
2218 status=TIFFWriteTile(tiff,tiff_info->pixels,(uint32) (i*
2219 tiff_info->tile_geometry.width),(uint32) ((row/
2220 tiff_info->tile_geometry.height)*tiff_info->tile_geometry.height),0,
2221 sample);
cristy3ed852e2009-09-05 21:47:34 +00002222 if (status < 0)
2223 break;
2224 }
2225 return(status);
2226}
2227
2228static void TIFFSetProfiles(TIFF *tiff,Image *image)
2229{
2230 const char
2231 *name;
2232
2233 const StringInfo
2234 *profile;
2235
2236 if (image->profiles == (void *) NULL)
2237 return;
2238 ResetImageProfileIterator(image);
2239 for (name=GetNextImageProfile(image); name != (const char *) NULL; )
2240 {
2241 profile=GetImageProfile(image,name);
2242#if defined(TIFFTAG_XMLPACKET)
2243 if (LocaleCompare(name,"xmp") == 0)
2244 (void) TIFFSetField(tiff,TIFFTAG_XMLPACKET,(uint32) GetStringInfoLength(
2245 profile),GetStringInfoDatum(profile));
2246#endif
2247#if defined(TIFFTAG_ICCPROFILE)
2248 if (LocaleCompare(name,"icc") == 0)
2249 (void) TIFFSetField(tiff,TIFFTAG_ICCPROFILE,(uint32) GetStringInfoLength(
2250 profile),GetStringInfoDatum(profile));
2251#endif
2252 if (LocaleCompare(name,"iptc") == 0)
2253 {
2254 size_t
2255 length;
2256
2257 StringInfo
2258 *iptc_profile;
2259
2260 iptc_profile=CloneStringInfo(profile);
2261 length=GetStringInfoLength(profile)+4-(GetStringInfoLength(profile) &
2262 0x03);
2263 SetStringInfoLength(iptc_profile,length);
2264 if (TIFFIsByteSwapped(tiff))
2265 TIFFSwabArrayOfLong((uint32 *) GetStringInfoDatum(iptc_profile),
cristyf6fe0a12010-05-30 00:44:47 +00002266 (unsigned long) (length/4));
cristy3ed852e2009-09-05 21:47:34 +00002267 (void) TIFFSetField(tiff,TIFFTAG_RICHTIFFIPTC,(uint32)
2268 GetStringInfoLength(iptc_profile)/4,GetStringInfoDatum(iptc_profile));
2269 iptc_profile=DestroyStringInfo(iptc_profile);
2270 }
2271#if defined(TIFFTAG_PHOTOSHOP)
2272 if (LocaleCompare(name,"8bim") == 0)
2273 (void) TIFFSetField(tiff,TIFFTAG_PHOTOSHOP,(uint32)
2274 GetStringInfoLength(profile),GetStringInfoDatum(profile));
2275#endif
2276 if (LocaleCompare(name,"tiff:37724") == 0)
2277 (void) TIFFSetField(tiff,37724,(uint32)GetStringInfoLength(profile),
2278 GetStringInfoDatum(profile));
2279 name=GetNextImageProfile(image);
2280 }
2281}
2282
2283static void TIFFSetProperties(TIFF *tiff,Image *image)
2284{
2285 const char
2286 *value;
2287
cristy15d8d212010-09-17 01:56:30 +00002288 (void) TIFFSetField(tiff,TIFFTAG_DOCUMENTNAME,image->filename);
cristy3ed852e2009-09-05 21:47:34 +00002289 value=GetImageProperty(image,"tiff:hostcomputer");
2290 if (value != (const char *) NULL)
2291 (void) TIFFSetField(tiff,TIFFTAG_HOSTCOMPUTER,value);
2292 value=GetImageProperty(image,"tiff:artist");
2293 if (value != (const char *) NULL)
2294 (void) TIFFSetField(tiff,TIFFTAG_ARTIST,value);
2295 value=GetImageProperty(image,"tiff:timestamp");
2296 if (value != (const char *) NULL)
2297 (void) TIFFSetField(tiff,TIFFTAG_DATETIME,value);
2298 value=GetImageProperty(image,"tiff:make");
2299 if (value != (const char *) NULL)
2300 (void) TIFFSetField(tiff,TIFFTAG_MAKE,value);
2301 value=GetImageProperty(image,"tiff:model");
2302 if (value != (const char *) NULL)
2303 (void) TIFFSetField(tiff,TIFFTAG_MODEL,value);
cristy15d8d212010-09-17 01:56:30 +00002304 value=GetImageProperty(image,"tiff:software");
2305 if (value != (const char *) NULL)
2306 (void) TIFFSetField(tiff,TIFFTAG_SOFTWARE,value);
cristy3ed852e2009-09-05 21:47:34 +00002307 value=GetImageProperty(image,"tiff:copyright");
2308 if (value != (const char *) NULL)
2309 (void) TIFFSetField(tiff,33432,value);
2310 value=GetImageProperty(image,"kodak-33423");
2311 if (value != (const char *) NULL)
2312 (void) TIFFSetField(tiff,33423,value);
2313 value=GetImageProperty(image,"kodak-36867");
2314 if (value != (const char *) NULL)
2315 (void) TIFFSetField(tiff,36867,value);
2316 value=GetImageProperty(image,"label");
2317 if (value != (const char *) NULL)
2318 (void) TIFFSetField(tiff,TIFFTAG_PAGENAME,value);
2319 value=GetImageProperty(image,"comment");
2320 if (value != (const char *) NULL)
2321 (void) TIFFSetField(tiff,TIFFTAG_IMAGEDESCRIPTION,value);
2322}
2323
2324static void TIFFSetEXIFProperties(TIFF *tiff,Image *image)
2325{
2326#if defined(MAGICKCORE_HAVE_TIFFREADEXIFDIRECTORY)
2327 const char
2328 *value;
2329
cristybb503372010-05-27 20:51:26 +00002330 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002331 i;
2332
2333 uint32
2334 offset;
2335
2336 /*
2337 Write EXIF properties.
2338 */
2339 offset=0;
2340 (void) TIFFSetField(tiff,TIFFTAG_SUBIFD,1,&offset);
2341 for (i=0; exif_info[i].tag != 0; i++)
2342 {
2343 value=GetImageProperty(image,exif_info[i].property);
2344 if (value == (const char *) NULL)
2345 continue;
2346 switch (exif_info[i].type)
2347 {
2348 case TIFF_ASCII:
2349 {
2350 (void) TIFFSetField(tiff,exif_info[i].tag,value);
2351 break;
2352 }
2353 case TIFF_SHORT:
2354 {
2355 uint16
2356 shorty;
2357
cristyf2f27272009-12-17 14:48:46 +00002358 shorty=(uint16) StringToLong(value);
cristy3ed852e2009-09-05 21:47:34 +00002359 (void) TIFFSetField(tiff,exif_info[i].tag,shorty);
2360 break;
2361 }
2362 case TIFF_LONG:
2363 {
2364 uint16
cristybb503372010-05-27 20:51:26 +00002365 ssize_ty;
cristy3ed852e2009-09-05 21:47:34 +00002366
cristybb503372010-05-27 20:51:26 +00002367 ssize_ty=(uint16) StringToLong(value);
2368 (void) TIFFSetField(tiff,exif_info[i].tag,ssize_ty);
cristy3ed852e2009-09-05 21:47:34 +00002369 break;
2370 }
2371 case TIFF_RATIONAL:
2372 case TIFF_SRATIONAL:
2373 {
2374 float
2375 rational;
2376
cristyf2f27272009-12-17 14:48:46 +00002377 rational=StringToDouble(value);
cristy3ed852e2009-09-05 21:47:34 +00002378 (void) TIFFSetField(tiff,exif_info[i].tag,rational);
2379 break;
2380 }
2381 default:
2382 break;
2383 }
2384 }
2385 /* (void) TIFFSetField(tiff,TIFFTAG_EXIFIFD,offset); */
2386#else
2387 (void) tiff;
2388 (void) image;
2389#endif
2390}
2391
2392static MagickBooleanType WriteTIFFImage(const ImageInfo *image_info,
2393 Image *image)
2394{
2395#if !defined(TIFFDefaultStripSize)
2396#define TIFFDefaultStripSize(tiff,request) (8192UL/TIFFScanlineSize(tiff))
2397#endif
2398
2399 const char
2400 *mode,
cristy949bf5b2010-05-08 02:47:03 +00002401 *option;
cristy3ed852e2009-09-05 21:47:34 +00002402
2403 CompressionType
2404 compression;
2405
cristy6b032822010-06-29 16:52:32 +00002406 EndianType
2407 endian_type;
2408
cristybb503372010-05-27 20:51:26 +00002409 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002410 y;
2411
2412 MagickBooleanType
2413 debug,
2414 status;
2415
2416 MagickOffsetType
2417 scene;
2418
2419 QuantumInfo
2420 *quantum_info;
2421
2422 QuantumType
2423 quantum_type;
2424
cristybb503372010-05-27 20:51:26 +00002425 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002426 i;
2427
2428 size_t
2429 length;
2430
2431 TIFF
2432 *tiff;
2433
2434 TIFFErrorHandler
2435 error_handler,
2436 warning_handler;
2437
2438 TIFFInfo
2439 tiff_info;
2440
2441 uint16
2442 bits_per_sample,
2443 compress_tag,
cristyf2687ca2010-06-29 16:32:38 +00002444 endian,
cristy3ed852e2009-09-05 21:47:34 +00002445 photometric;
2446
2447 uint32
2448 rows_per_strip;
2449
2450 unsigned char
2451 *pixels;
2452
cristyf2687ca2010-06-29 16:32:38 +00002453 size_t
2454 lsb_first;
2455
cristy3ed852e2009-09-05 21:47:34 +00002456 /*
2457 Open TIFF file.
2458 */
2459 assert(image_info != (const ImageInfo *) NULL);
2460 assert(image_info->signature == MagickSignature);
2461 assert(image != (Image *) NULL);
2462 assert(image->signature == MagickSignature);
2463 if (image->debug != MagickFalse)
2464 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2465 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
2466 if (status == MagickFalse)
2467 return(status);
2468 (void) MagickSetThreadValue(tiff_exception,&image->exception);
2469 error_handler=TIFFSetErrorHandler((TIFFErrorHandler) TIFFErrors);
2470 warning_handler=TIFFSetWarningHandler((TIFFErrorHandler) TIFFWarnings);
cristy6b032822010-06-29 16:52:32 +00002471 endian_type=UndefinedEndian;
2472 option=GetImageOption(image_info,"tiff:endian");
2473 if (option != (const char *) NULL)
2474 {
2475 if (LocaleNCompare(option,"msb",3) == 0)
2476 endian_type=MSBEndian;
2477 if (LocaleNCompare(option,"lsb",3) == 0)
2478 endian_type=LSBEndian;;
2479 }
2480 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002481 {
2482 case LSBEndian: mode="wl"; break;
2483 case MSBEndian: mode="wb"; break;
2484 default: mode="w"; break;
2485 }
2486#if defined(TIFF_VERSION_BIG)
2487 if (LocaleCompare(image_info->magick,"TIFF64") == 0)
cristy6b032822010-06-29 16:52:32 +00002488 switch (endian_type)
cristy3ed852e2009-09-05 21:47:34 +00002489 {
2490 case LSBEndian: mode="wl8"; break;
2491 case MSBEndian: mode="wb8"; break;
2492 default: mode="w8"; break;
2493 }
2494#endif
2495 tiff=TIFFClientOpen(image->filename,mode,(thandle_t) image,TIFFReadBlob,
2496 TIFFWriteBlob,TIFFSeekBlob,TIFFCloseBlob,TIFFGetBlobSize,TIFFMapBlob,
2497 TIFFUnmapBlob);
2498 if (tiff == (TIFF *) NULL)
2499 {
2500 (void) TIFFSetWarningHandler(warning_handler);
2501 (void) TIFFSetErrorHandler(error_handler);
2502 return(MagickFalse);
2503 }
2504 scene=0;
2505 debug=IsEventLogging();
2506 do
2507 {
2508 /*
2509 Initialize TIFF fields.
2510 */
cristy5f1c1ff2010-12-23 21:38:06 +00002511 if ((image_info->type != UndefinedType) &&
cristy3ed852e2009-09-05 21:47:34 +00002512 (image_info->type != OptimizeType))
2513 (void) SetImageType(image,image_info->type);
2514 quantum_info=AcquireQuantumInfo(image_info,image);
2515 if (quantum_info == (QuantumInfo *) NULL)
2516 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2517 if ((image->storage_class != PseudoClass) && (image->depth >= 32) &&
2518 (quantum_info->format == UndefinedQuantumFormat) &&
2519 (IsHighDynamicRangeImage(image,&image->exception) != MagickFalse))
2520 {
2521 status=SetQuantumFormat(image,quantum_info,FloatingPointQuantumFormat);
2522 if (status == MagickFalse)
2523 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
2524 }
2525 if ((LocaleCompare(image_info->magick,"PTIF") == 0) &&
2526 (GetPreviousImageInList(image) != (Image *) NULL))
2527 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_REDUCEDIMAGE);
2528 if ((image->columns != (uint32) image->columns) ||
2529 (image->rows != (uint32) image->rows))
2530 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
2531 (void) TIFFSetField(tiff,TIFFTAG_IMAGELENGTH,(uint32) image->rows);
2532 (void) TIFFSetField(tiff,TIFFTAG_IMAGEWIDTH,(uint32) image->columns);
2533 compression=image->compression;
2534 if (image_info->compression != UndefinedCompression)
2535 compression=image_info->compression;
2536 switch (compression)
2537 {
2538 case FaxCompression:
2539 {
2540 compress_tag=COMPRESSION_CCITTFAX3;
2541 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2542 break;
2543 }
2544 case Group4Compression:
2545 {
2546 compress_tag=COMPRESSION_CCITTFAX4;
2547 SetQuantumMinIsWhite(quantum_info,MagickTrue);
2548 break;
2549 }
2550 case JPEGCompression:
2551 {
2552 compress_tag=COMPRESSION_JPEG;
2553 break;
2554 }
cristyfbb0ef02010-12-19 02:32:11 +00002555#if defined(COMPRESSION_LZMA)
2556 case LZMACompression:
2557 {
2558 compress_tag=COMPRESSION_LZMA;
2559 break;
2560 }
2561#endif
cristy3ed852e2009-09-05 21:47:34 +00002562 case LZWCompression:
2563 {
2564 compress_tag=COMPRESSION_LZW;
2565 break;
2566 }
2567 case RLECompression:
2568 {
2569 compress_tag=COMPRESSION_PACKBITS;
2570 break;
2571 }
2572 case ZipCompression:
2573 {
2574 compress_tag=COMPRESSION_ADOBE_DEFLATE;
2575 break;
2576 }
2577 case NoCompression:
2578 default:
2579 {
2580 compress_tag=COMPRESSION_NONE;
2581 break;
2582 }
2583 }
cristy5b675872009-10-21 13:12:00 +00002584#if defined(MAGICKCORE_HAVE_TIFFISCODECCONFIGURED) || (TIFFLIB_VERSION > 20040919)
2585 if ((compress_tag != COMPRESSION_NONE) &&
2586 (TIFFIsCODECConfigured(compress_tag) == 0))
cristy3ed852e2009-09-05 21:47:34 +00002587 {
cristy5b675872009-10-21 13:12:00 +00002588 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2589 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002590 MagickCompressOptions,(ssize_t) compression));
cristy5b675872009-10-21 13:12:00 +00002591 compress_tag=COMPRESSION_NONE;
2592 compression=NoCompression;
cristy3ed852e2009-09-05 21:47:34 +00002593 }
2594#else
2595 switch (compress_tag)
2596 {
2597#if defined(CCITT_SUPPORT)
2598 case COMPRESSION_CCITTFAX3:
2599 case COMPRESSION_CCITTFAX4:
2600#endif
2601#if defined(YCBCR_SUPPORT) && defined(JPEG_SUPPORT)
2602 case COMPRESSION_JPEG:
2603#endif
cristyfbb0ef02010-12-19 02:32:11 +00002604#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2605 case COMPRESSION_LZMA:
2606#endif
cristy3ed852e2009-09-05 21:47:34 +00002607#if defined(LZW_SUPPORT)
2608 case COMPRESSION_LZW:
2609#endif
2610#if defined(PACKBITS_SUPPORT)
2611 case COMPRESSION_PACKBITS:
2612#endif
2613#if defined(ZIP_SUPPORT)
2614 case COMPRESSION_ADOBE_DEFLATE:
2615#endif
2616 case COMPRESSION_NONE:
2617 break;
2618 default:
2619 {
2620 (void) ThrowMagickException(&image->exception,GetMagickModule(),
2621 CoderError,"CompressionNotSupported","`%s'",MagickOptionToMnemonic(
cristybb503372010-05-27 20:51:26 +00002622 MagickCompressOptions,(ssize_t) compression));
cristy3ed852e2009-09-05 21:47:34 +00002623 compress_tag=COMPRESSION_NONE;
2624 compression=NoCompression;
2625 break;
2626 }
2627 }
2628#endif
2629 switch (compression)
2630 {
2631 case FaxCompression:
2632 case Group4Compression:
2633 {
2634 (void) SetImageType(image,BilevelType);
2635 break;
2636 }
2637 case JPEGCompression:
2638 {
2639 (void) SetImageStorageClass(image,DirectClass);
2640 (void) SetImageDepth(image,8);
2641 break;
2642 }
2643 default:
2644 break;
2645 }
cristy3ed852e2009-09-05 21:47:34 +00002646 if (image->colorspace == CMYKColorspace)
2647 {
2648 photometric=PHOTOMETRIC_SEPARATED;
2649 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,4);
2650 (void) TIFFSetField(tiff,TIFFTAG_INKSET,INKSET_CMYK);
2651 }
2652 else
2653 {
2654 /*
2655 Full color TIFF raster.
2656 */
2657 if (image->colorspace == LabColorspace)
2658 photometric=PHOTOMETRIC_CIELAB;
2659 else
2660 if (image->colorspace == YCbCrColorspace)
2661 {
2662 photometric=PHOTOMETRIC_YCBCR;
2663 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,1,1);
cristy79e5dad2010-09-16 19:48:33 +00002664 (void) SetImageStorageClass(image,DirectClass);
cristy3ed852e2009-09-05 21:47:34 +00002665 (void) SetImageDepth(image,8);
2666 }
2667 else
2668 {
2669 if (image->colorspace != RGBColorspace)
2670 (void) TransformImageColorspace(image,RGBColorspace);
2671 photometric=PHOTOMETRIC_RGB;
2672 }
2673 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,3);
2674 if ((image_info->type != TrueColorType) &&
2675 (image_info->type != TrueColorMatteType))
2676 {
cristy94c8fe42009-10-06 01:57:36 +00002677 if ((image_info->type != PaletteType) &&
2678 (IsGrayImage(image,&image->exception) != MagickFalse))
cristy3ed852e2009-09-05 21:47:34 +00002679 {
cristy94c8fe42009-10-06 01:57:36 +00002680 photometric=(uint16) (quantum_info->min_is_white !=
2681 MagickFalse ? PHOTOMETRIC_MINISWHITE :
2682 PHOTOMETRIC_MINISBLACK);
cristy3ed852e2009-09-05 21:47:34 +00002683 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
nicolasc150a902010-11-08 20:44:53 +00002684 if ((image_info->depth == 0) && (image->matte == MagickFalse) &&
cristy94c8fe42009-10-06 01:57:36 +00002685 (IsMonochromeImage(image,&image->exception) != MagickFalse))
2686 {
2687 status=SetQuantumDepth(image,quantum_info,1);
2688 if (status == MagickFalse)
2689 ThrowWriterException(ResourceLimitError,
2690 "MemoryAllocationFailed");
2691 }
cristy3ed852e2009-09-05 21:47:34 +00002692 }
2693 else
cristy94c8fe42009-10-06 01:57:36 +00002694 if (image->storage_class == PseudoClass)
cristy3ed852e2009-09-05 21:47:34 +00002695 {
cristybb503372010-05-27 20:51:26 +00002696 size_t
cristy94c8fe42009-10-06 01:57:36 +00002697 depth;
2698
2699 /*
2700 Colormapped TIFF raster.
2701 */
cristy3ed852e2009-09-05 21:47:34 +00002702 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,1);
cristy94c8fe42009-10-06 01:57:36 +00002703 photometric=PHOTOMETRIC_PALETTE;
2704 depth=1;
2705 while ((GetQuantumRange(depth)+1) < image->colors)
2706 depth<<=1;
2707 status=SetQuantumDepth(image,quantum_info,depth);
2708 if (status == MagickFalse)
2709 ThrowWriterException(ResourceLimitError,
2710 "MemoryAllocationFailed");
cristy3ed852e2009-09-05 21:47:34 +00002711 }
2712 }
2713 }
cristyf2687ca2010-06-29 16:32:38 +00002714 switch (image->endian)
2715 {
2716 case LSBEndian:
2717 {
2718 endian=FILLORDER_LSB2MSB;
2719 break;
2720 }
2721 case MSBEndian:
2722 {
2723 endian=FILLORDER_MSB2LSB;
2724 break;
2725 }
2726 case UndefinedEndian:
2727 default:
2728 {
2729 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_FILLORDER,&endian);
2730 break;
2731 }
2732 }
2733 lsb_first=1;
2734 image->endian=MSBEndian;
2735 if ((int) (*(char *) &lsb_first) != 0)
2736 image->endian=LSBEndian;
cristydaff8092010-04-22 14:50:53 +00002737 if ((compress_tag == COMPRESSION_CCITTFAX3) &&
2738 (photometric != PHOTOMETRIC_MINISWHITE))
2739 {
2740 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002741 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002742 }
cristy0accf6e2009-10-01 13:14:28 +00002743 else
cristydaff8092010-04-22 14:50:53 +00002744 if ((compress_tag == COMPRESSION_CCITTFAX4) &&
2745 (photometric != PHOTOMETRIC_MINISWHITE))
2746 {
2747 compress_tag=COMPRESSION_NONE;
cristyf2687ca2010-06-29 16:32:38 +00002748 endian=FILLORDER_MSB2LSB;
cristydaff8092010-04-22 14:50:53 +00002749 }
cristy62e282b2010-06-29 01:27:13 +00002750 option=GetImageProperty(image,"tiff:fill-order");
2751 if (option != (const char *) NULL)
2752 {
2753 if (LocaleNCompare(option,"msb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002754 endian=FILLORDER_MSB2LSB;
cristy62e282b2010-06-29 01:27:13 +00002755 if (LocaleNCompare(option,"lsb",3) == 0)
cristyf2687ca2010-06-29 16:32:38 +00002756 endian=FILLORDER_LSB2MSB;
cristy62e282b2010-06-29 01:27:13 +00002757 }
cristyf2687ca2010-06-29 16:32:38 +00002758 (void) TIFFSetField(tiff,TIFFTAG_COMPRESSION,compress_tag);
2759 (void) TIFFSetField(tiff,TIFFTAG_FILLORDER,endian);
cristy3ed852e2009-09-05 21:47:34 +00002760 (void) TIFFSetField(tiff,TIFFTAG_BITSPERSAMPLE,quantum_info->depth);
2761 if (image->matte != MagickFalse)
2762 {
2763 uint16
2764 extra_samples,
2765 sample_info[1],
2766 samples_per_pixel;
2767
2768 /*
2769 TIFF has a matte channel.
2770 */
2771 extra_samples=1;
2772 sample_info[0]=EXTRASAMPLE_UNASSALPHA;
2773 option=GetImageProperty(image,"tiff:alpha");
2774 if ((option != (const char *) NULL) &&
2775 (LocaleCompare(option,"associated") == 0))
2776 sample_info[0]=EXTRASAMPLE_ASSOCALPHA;
2777 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_SAMPLESPERPIXEL,
2778 &samples_per_pixel);
2779 (void) TIFFSetField(tiff,TIFFTAG_SAMPLESPERPIXEL,samples_per_pixel+1);
2780 (void) TIFFSetField(tiff,TIFFTAG_EXTRASAMPLES,extra_samples,
2781 &sample_info);
2782 if (sample_info[0] == EXTRASAMPLE_ASSOCALPHA)
2783 SetQuantumAlphaType(quantum_info,AssociatedQuantumAlpha);
2784 }
2785 (void) TIFFSetField(tiff,TIFFTAG_PHOTOMETRIC,photometric);
2786 switch (quantum_info->format)
2787 {
2788 case FloatingPointQuantumFormat:
2789 {
2790 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_IEEEFP);
2791 (void) TIFFSetField(tiff,TIFFTAG_SMINSAMPLEVALUE,quantum_info->minimum);
2792 (void) TIFFSetField(tiff,TIFFTAG_SMAXSAMPLEVALUE,quantum_info->maximum);
2793 break;
2794 }
2795 case SignedQuantumFormat:
2796 {
2797 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_INT);
2798 break;
2799 }
2800 case UnsignedQuantumFormat:
2801 {
2802 (void) TIFFSetField(tiff,TIFFTAG_SAMPLEFORMAT,SAMPLEFORMAT_UINT);
2803 break;
2804 }
2805 default:
2806 break;
2807 }
cristy3ed852e2009-09-05 21:47:34 +00002808 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,ORIENTATION_TOPLEFT);
2809 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
2810 if (photometric == PHOTOMETRIC_RGB)
2811 if ((image_info->interlace == PlaneInterlace) ||
2812 (image_info->interlace == PartitionInterlace))
2813 (void) TIFFSetField(tiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_SEPARATE);
2814 rows_per_strip=1;
2815 if (TIFFScanlineSize(tiff) != 0)
cristyf6fe0a12010-05-30 00:44:47 +00002816 rows_per_strip=(uint32) MagickMax((size_t) TIFFDefaultStripSize(tiff,0),
2817 1);
cristy3ed852e2009-09-05 21:47:34 +00002818 option=GetImageOption(image_info,"tiff:rows-per-strip");
2819 if (option != (const char *) NULL)
cristybb503372010-05-27 20:51:26 +00002820 rows_per_strip=(size_t) strtol(option,(char **) NULL,10);
cristy3ed852e2009-09-05 21:47:34 +00002821 switch (compress_tag)
2822 {
2823 case COMPRESSION_JPEG:
2824 {
2825#if defined(JPEG_SUPPORT)
2826 const char
2827 *sampling_factor;
2828
2829 GeometryInfo
2830 geometry_info;
2831
2832 MagickStatusType
2833 flags;
2834
2835 rows_per_strip+=(16-(rows_per_strip % 16));
2836 if (image->quality != 0)
2837 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image->quality);
2838 if (image_info->quality != UndefinedCompressionQuality)
2839 (void) TIFFSetField(tiff,TIFFTAG_JPEGQUALITY,image_info->quality);
2840 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RAW);
2841 if (image->colorspace == RGBColorspace)
2842 {
cristy949bf5b2010-05-08 02:47:03 +00002843 const char
2844 *value;
2845
cristy3ed852e2009-09-05 21:47:34 +00002846 (void) TIFFSetField(tiff,TIFFTAG_JPEGCOLORMODE,JPEGCOLORMODE_RGB);
2847 sampling_factor=(const char *) NULL;
2848 value=GetImageProperty(image,"jpeg:sampling-factor");
2849 if (value != (char *) NULL)
2850 {
2851 sampling_factor=value;
2852 if (image->debug != MagickFalse)
2853 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2854 " Input sampling-factors=%s",sampling_factor);
2855 }
2856 if (image_info->sampling_factor != (char *) NULL)
2857 sampling_factor=image_info->sampling_factor;
2858 if (sampling_factor != (const char *) NULL)
2859 {
2860 flags=ParseGeometry(sampling_factor,&geometry_info);
2861 if ((flags & SigmaValue) == 0)
2862 geometry_info.sigma=geometry_info.rho;
cristy79e5dad2010-09-16 19:48:33 +00002863 if (image->colorspace == YCbCrColorspace)
2864 (void) TIFFSetField(tiff,TIFFTAG_YCBCRSUBSAMPLING,(uint16)
2865 geometry_info.rho,(uint16) geometry_info.sigma);
cristy3ed852e2009-09-05 21:47:34 +00002866 }
2867 }
2868 if (bits_per_sample == 12)
2869 (void) TIFFSetField(tiff,TIFFTAG_JPEGTABLESMODE,JPEGTABLESMODE_QUANT);
2870#endif
2871 break;
2872 }
2873 case COMPRESSION_ADOBE_DEFLATE:
2874 {
cristyf6fe0a12010-05-30 00:44:47 +00002875 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002876 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2877 &bits_per_sample);
2878 if (((photometric == PHOTOMETRIC_RGB) ||
2879 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2880 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002881 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristyf6fe0a12010-05-30 00:44:47 +00002882 (void) TIFFSetField(tiff,TIFFTAG_ZIPQUALITY,(long) (
2883 image_info->quality == UndefinedCompressionQuality ? 7 :
cristy0b29b252010-05-30 01:59:46 +00002884 MagickMin((ssize_t) image_info->quality/10,9)));
cristy3ed852e2009-09-05 21:47:34 +00002885 break;
2886 }
2887 case COMPRESSION_CCITTFAX3:
2888 {
2889 /*
2890 Byte-aligned EOL.
2891 */
cristyeaedf062010-05-29 22:36:02 +00002892 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002893 (void) TIFFSetField(tiff,TIFFTAG_GROUP3OPTIONS,4);
2894 break;
2895 }
2896 case COMPRESSION_CCITTFAX4:
2897 {
cristyeaedf062010-05-29 22:36:02 +00002898 rows_per_strip=(uint32) image->rows;
cristy3ed852e2009-09-05 21:47:34 +00002899 break;
2900 }
cristyef8f26b2010-12-19 20:29:16 +00002901#if defined(LZMA_SUPPORT) && defined(COMPRESSION_LZMA)
2902 case COMPRESSION_LZMA:
2903 {
2904 if (((photometric == PHOTOMETRIC_RGB) ||
2905 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2906 ((bits_per_sample == 8) || (bits_per_sample == 16)))
2907 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
2908 (void) TIFFSetField(tiff,TIFFTAG_LZMAPRESET,(long) (
2909 image_info->quality == UndefinedCompressionQuality ? 7 :
2910 MagickMin((ssize_t) image_info->quality/10,9)));
2911 break;
2912 }
2913#endif
cristy3ed852e2009-09-05 21:47:34 +00002914 case COMPRESSION_LZW:
2915 {
2916 (void) TIFFGetFieldDefaulted(tiff,TIFFTAG_BITSPERSAMPLE,
2917 &bits_per_sample);
2918 if (((photometric == PHOTOMETRIC_RGB) ||
2919 (photometric == PHOTOMETRIC_MINISBLACK)) &&
2920 ((bits_per_sample == 8) || (bits_per_sample == 16)))
cristyef8f26b2010-12-19 20:29:16 +00002921 (void) TIFFSetField(tiff,TIFFTAG_PREDICTOR,PREDICTOR_HORIZONTAL);
cristy3ed852e2009-09-05 21:47:34 +00002922 break;
2923 }
2924 default:
2925 break;
2926 }
cristy79e5dad2010-09-16 19:48:33 +00002927 option=GetImageOption(image_info,"tiff:tile-geometry");
2928 if (option == (const char *) NULL)
2929 (void) TIFFSetField(tiff,TIFFTAG_ROWSPERSTRIP,rows_per_strip);
cristy3ed852e2009-09-05 21:47:34 +00002930 if ((image->x_resolution != 0.0) && (image->y_resolution != 0.0))
2931 {
2932 unsigned short
2933 units;
2934
2935 /*
2936 Set image resolution.
2937 */
2938 units=RESUNIT_NONE;
2939 if (image->units == PixelsPerInchResolution)
2940 units=RESUNIT_INCH;
2941 if (image->units == PixelsPerCentimeterResolution)
2942 units=RESUNIT_CENTIMETER;
2943 (void) TIFFSetField(tiff,TIFFTAG_RESOLUTIONUNIT,(uint16) units);
2944 (void) TIFFSetField(tiff,TIFFTAG_XRESOLUTION,image->x_resolution);
2945 (void) TIFFSetField(tiff,TIFFTAG_YRESOLUTION,image->y_resolution);
2946 if ((image->page.x != 0) || (image->page.y != 0))
2947 {
2948 /*
2949 Set image position.
2950 */
2951 (void) TIFFSetField(tiff,TIFFTAG_XPOSITION,(float) image->page.x/
2952 image->x_resolution);
2953 (void) TIFFSetField(tiff,TIFFTAG_YPOSITION,(float) image->page.y/
2954 image->y_resolution);
2955 }
2956 }
2957 if (image->chromaticity.white_point.x != 0.0)
2958 {
2959 float
2960 chromaticity[6];
2961
2962 /*
2963 Set image chromaticity.
2964 */
2965 chromaticity[0]=(float) image->chromaticity.red_primary.x;
2966 chromaticity[1]=(float) image->chromaticity.red_primary.y;
2967 chromaticity[2]=(float) image->chromaticity.green_primary.x;
2968 chromaticity[3]=(float) image->chromaticity.green_primary.y;
2969 chromaticity[4]=(float) image->chromaticity.blue_primary.x;
2970 chromaticity[5]=(float) image->chromaticity.blue_primary.y;
2971 (void) TIFFSetField(tiff,TIFFTAG_PRIMARYCHROMATICITIES,chromaticity);
2972 chromaticity[0]=(float) image->chromaticity.white_point.x;
2973 chromaticity[1]=(float) image->chromaticity.white_point.y;
2974 (void) TIFFSetField(tiff,TIFFTAG_WHITEPOINT,chromaticity);
2975 }
2976 if ((image_info->adjoin != MagickFalse) && (GetImageListLength(image) > 1))
2977 {
2978 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2979 if (image->scene != 0)
2980 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,(uint16) image->scene,
2981 GetImageListLength(image));
2982 }
2983 if (image->orientation != UndefinedOrientation)
2984 (void) TIFFSetField(tiff,TIFFTAG_ORIENTATION,(uint16) image->orientation);
2985 (void) TIFFSetProfiles(tiff,image);
2986 {
2987 uint16
2988 page,
2989 pages;
2990
2991 page=(uint16) scene;
cristyeaedf062010-05-29 22:36:02 +00002992 pages=(uint16) GetImageListLength(image);
cristy3ed852e2009-09-05 21:47:34 +00002993 if ((image_info->adjoin != MagickFalse) && (pages > 1))
2994 (void) TIFFSetField(tiff,TIFFTAG_SUBFILETYPE,FILETYPE_PAGE);
2995 (void) TIFFSetField(tiff,TIFFTAG_PAGENUMBER,page,pages);
2996 }
2997 (void) TIFFSetProperties(tiff,image);
2998 if (0)
2999 (void) TIFFSetEXIFProperties(tiff,image);
3000 /*
3001 Write image scanlines.
3002 */
3003 if (GetTIFFInfo(image_info,tiff,&tiff_info) == MagickFalse)
3004 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3005 pixels=GetQuantumPixels(quantum_info);
3006 tiff_info.scanline=GetQuantumPixels(quantum_info);
3007 switch (photometric)
3008 {
3009 case PHOTOMETRIC_CIELAB:
3010 case PHOTOMETRIC_YCBCR:
3011 case PHOTOMETRIC_RGB:
3012 {
3013 /*
3014 RGB TIFF image.
3015 */
3016 switch (image_info->interlace)
3017 {
3018 case NoInterlace:
3019 default:
3020 {
3021 quantum_type=RGBQuantum;
3022 if (image->matte != MagickFalse)
3023 quantum_type=RGBAQuantum;
cristybb503372010-05-27 20:51:26 +00003024 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003025 {
3026 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003027 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003028
3029 p=GetVirtualPixels(image,0,y,image->columns,1,
3030 &image->exception);
3031 if (p == (const PixelPacket *) NULL)
3032 break;
3033 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3034 quantum_info,quantum_type,pixels,&image->exception);
3035 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3036 break;
3037 if (image->previous == (Image *) NULL)
3038 {
cristy2837bcc2010-08-07 23:57:39 +00003039 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType)
3040 y,image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003041 if (status == MagickFalse)
3042 break;
3043 }
3044 }
3045 break;
3046 }
3047 case PlaneInterlace:
3048 case PartitionInterlace:
3049 {
3050 /*
3051 Plane interlacing: RRRRRR...GGGGGG...BBBBBB...
3052 */
cristybb503372010-05-27 20:51:26 +00003053 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003054 {
3055 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003056 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003057
3058 p=GetVirtualPixels(image,0,y,image->columns,1,
3059 &image->exception);
3060 if (p == (const PixelPacket *) NULL)
3061 break;
3062 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3063 quantum_info,RedQuantum,pixels,&image->exception);
3064 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3065 break;
3066 }
3067 if (image->previous == (Image *) NULL)
3068 {
3069 status=SetImageProgress(image,SaveImageTag,100,400);
3070 if (status == MagickFalse)
3071 break;
3072 }
cristybb503372010-05-27 20:51:26 +00003073 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003074 {
3075 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003076 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003077
3078 p=GetVirtualPixels(image,0,y,image->columns,1,
3079 &image->exception);
3080 if (p == (const PixelPacket *) NULL)
3081 break;
3082 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3083 quantum_info,GreenQuantum,pixels,&image->exception);
3084 if (TIFFWritePixels(tiff,&tiff_info,y,1,image) == -1)
3085 break;
3086 }
3087 if (image->previous == (Image *) NULL)
3088 {
3089 status=SetImageProgress(image,SaveImageTag,200,400);
3090 if (status == MagickFalse)
3091 break;
3092 }
cristybb503372010-05-27 20:51:26 +00003093 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003094 {
3095 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003096 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003097
3098 p=GetVirtualPixels(image,0,y,image->columns,1,
3099 &image->exception);
3100 if (p == (const PixelPacket *) NULL)
3101 break;
3102 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3103 quantum_info,BlueQuantum,pixels,&image->exception);
3104 if (TIFFWritePixels(tiff,&tiff_info,y,2,image) == -1)
3105 break;
3106 }
3107 if (image->previous == (Image *) NULL)
3108 {
3109 status=SetImageProgress(image,SaveImageTag,300,400);
3110 if (status == MagickFalse)
3111 break;
3112 }
3113 if (image->matte != MagickFalse)
cristybb503372010-05-27 20:51:26 +00003114 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003115 {
3116 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003117 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003118
3119 p=GetVirtualPixels(image,0,y,image->columns,1,
3120 &image->exception);
3121 if (p == (const PixelPacket *) NULL)
3122 break;
3123 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3124 quantum_info,AlphaQuantum,pixels,&image->exception);
3125 if (TIFFWritePixels(tiff,&tiff_info,y,3,image) == -1)
3126 break;
3127 }
3128 if (image->previous == (Image *) NULL)
3129 {
3130 status=SetImageProgress(image,SaveImageTag,400,400);
3131 if (status == MagickFalse)
3132 break;
3133 }
3134 break;
3135 }
3136 }
3137 break;
3138 }
3139 case PHOTOMETRIC_SEPARATED:
3140 {
3141 /*
3142 CMYK TIFF image.
3143 */
3144 quantum_type=CMYKQuantum;
3145 if (image->matte != MagickFalse)
3146 quantum_type=CMYKAQuantum;
3147 if (image->colorspace != CMYKColorspace)
3148 (void) TransformImageColorspace(image,CMYKColorspace);
cristybb503372010-05-27 20:51:26 +00003149 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003150 {
3151 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003152 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003153
3154 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3155 if (p == (const PixelPacket *) NULL)
3156 break;
3157 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3158 quantum_info,quantum_type,pixels,&image->exception);
3159 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3160 break;
3161 if (image->previous == (Image *) NULL)
3162 {
cristycee97112010-05-28 00:44:52 +00003163 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3164 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003165 if (status == MagickFalse)
3166 break;
3167 }
3168 }
3169 break;
3170 }
3171 case PHOTOMETRIC_PALETTE:
3172 {
3173 uint16
3174 *blue,
3175 *green,
3176 *red;
3177
3178 /*
3179 Colormapped TIFF image.
3180 */
3181 red=(uint16 *) AcquireQuantumMemory(65536,sizeof(*red));
3182 green=(uint16 *) AcquireQuantumMemory(65536,sizeof(*green));
3183 blue=(uint16 *) AcquireQuantumMemory(65536,sizeof(*blue));
3184 if ((red == (uint16 *) NULL) || (green == (uint16 *) NULL) ||
3185 (blue == (uint16 *) NULL))
3186 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
3187 /*
3188 Initialize TIFF colormap.
3189 */
3190 (void) ResetMagickMemory(red,0,65536*sizeof(*red));
3191 (void) ResetMagickMemory(green,0,65536*sizeof(*green));
3192 (void) ResetMagickMemory(blue,0,65536*sizeof(*blue));
cristybb503372010-05-27 20:51:26 +00003193 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003194 {
3195 red[i]=ScaleQuantumToShort(image->colormap[i].red);
3196 green[i]=ScaleQuantumToShort(image->colormap[i].green);
3197 blue[i]=ScaleQuantumToShort(image->colormap[i].blue);
3198 }
3199 (void) TIFFSetField(tiff,TIFFTAG_COLORMAP,red,green,blue);
3200 red=(uint16 *) RelinquishMagickMemory(red);
3201 green=(uint16 *) RelinquishMagickMemory(green);
3202 blue=(uint16 *) RelinquishMagickMemory(blue);
3203 }
3204 default:
3205 {
3206 /*
3207 Convert PseudoClass packets to contiguous grayscale scanlines.
3208 */
3209 quantum_type=IndexQuantum;
3210 if (image->matte != MagickFalse)
3211 {
3212 if (photometric != PHOTOMETRIC_PALETTE)
3213 quantum_type=GrayAlphaQuantum;
3214 else
3215 quantum_type=IndexAlphaQuantum;
3216 }
3217 else
3218 if (photometric != PHOTOMETRIC_PALETTE)
3219 quantum_type=GrayQuantum;
cristybb503372010-05-27 20:51:26 +00003220 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003221 {
3222 register const PixelPacket
cristyc47d1f82009-11-26 01:44:43 +00003223 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00003224
3225 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
3226 if (p == (const PixelPacket *) NULL)
3227 break;
3228 length=ExportQuantumPixels(image,(const CacheView *) NULL,
3229 quantum_info,quantum_type,pixels,&image->exception);
3230 if (TIFFWritePixels(tiff,&tiff_info,y,0,image) == -1)
3231 break;
3232 if (image->previous == (Image *) NULL)
3233 {
cristycee97112010-05-28 00:44:52 +00003234 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
3235 image->rows);
cristy3ed852e2009-09-05 21:47:34 +00003236 if (status == MagickFalse)
3237 break;
3238 }
3239 }
3240 break;
3241 }
3242 }
3243 quantum_info=DestroyQuantumInfo(quantum_info);
3244 DestroyTIFFInfo(&tiff_info);
3245 if (0 && (image_info->verbose == MagickTrue))
3246 TIFFPrintDirectory(tiff,stdout,MagickFalse);
3247 (void) TIFFWriteDirectory(tiff);
cristyf2687ca2010-06-29 16:32:38 +00003248 image->endian=MSBEndian;
3249 if (endian == FILLORDER_LSB2MSB)
3250 image->endian=LSBEndian;
cristy3ed852e2009-09-05 21:47:34 +00003251 image=SyncNextImageInList(image);
3252 if (image == (Image *) NULL)
3253 break;
3254 status=SetImageProgress(image,SaveImagesTag,scene++,
3255 GetImageListLength(image));
3256 if (status == MagickFalse)
3257 break;
3258 } while (image_info->adjoin != MagickFalse);
3259 (void) TIFFSetWarningHandler(warning_handler);
3260 (void) TIFFSetErrorHandler(error_handler);
3261 TIFFClose(tiff);
3262 return(MagickTrue);
3263}
3264#endif