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