2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Truevision Targa Image Format %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color-private.h"
48 #include "MagickCore/colormap.h"
49 #include "MagickCore/colormap-private.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/magick.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/property.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/module.h"
71 static MagickBooleanType
72 WriteTGAImage(const ImageInfo *,Image *,ExceptionInfo *);
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 % R e a d T G A I m a g e %
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 % ReadTGAImage() reads a Truevision TGA image file and returns it.
86 % It allocates the memory necessary for the new Image structure and returns
87 % a pointer to the new image.
89 % The format of the ReadTGAImage method is:
91 % Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
93 % A description of each parameter follows:
95 % o image_info: the image info.
97 % o exception: return any errors or warnings in this structure.
100 static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
102 #define TGAColormap 1
104 #define TGAMonochrome 3
105 #define TGARLEColormap 9
107 #define TGARLEMonochrome 11
109 typedef struct _TGAInfo
176 assert(image_info != (const ImageInfo *) NULL);
177 assert(image_info->signature == MagickSignature);
178 if (image_info->debug != MagickFalse)
179 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
180 image_info->filename);
181 assert(exception != (ExceptionInfo *) NULL);
182 assert(exception->signature == MagickSignature);
183 image=AcquireImage(image_info,exception);
184 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
185 if (status == MagickFalse)
187 image=DestroyImageList(image);
188 return((Image *) NULL);
191 Read TGA header information.
193 count=ReadBlob(image,1,&tga_info.id_length);
194 tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
195 tga_info.image_type=(unsigned char) ReadBlobByte(image);
197 ((tga_info.image_type != TGAColormap) &&
198 (tga_info.image_type != TGARGB) &&
199 (tga_info.image_type != TGAMonochrome) &&
200 (tga_info.image_type != TGARLEColormap) &&
201 (tga_info.image_type != TGARLERGB) &&
202 (tga_info.image_type != TGARLEMonochrome)) ||
203 (((tga_info.image_type == TGAColormap) ||
204 (tga_info.image_type == TGARLEColormap)) &&
205 (tga_info.colormap_type == 0)))
206 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
207 tga_info.colormap_index=ReadBlobLSBShort(image);
208 tga_info.colormap_length=ReadBlobLSBShort(image);
209 tga_info.colormap_size=(unsigned char) ReadBlobByte(image);
210 tga_info.x_origin=ReadBlobLSBShort(image);
211 tga_info.y_origin=ReadBlobLSBShort(image);
212 tga_info.width=(unsigned short) ReadBlobLSBShort(image);
213 tga_info.height=(unsigned short) ReadBlobLSBShort(image);
214 tga_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
215 tga_info.attributes=(unsigned char) ReadBlobByte(image);
216 if (EOFBlob(image) != MagickFalse)
217 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
218 if ((((tga_info.bits_per_pixel <= 1) || (tga_info.bits_per_pixel >= 17)) &&
219 (tga_info.bits_per_pixel != 24) && (tga_info.bits_per_pixel != 32)))
220 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
222 Initialize image structure.
224 image->columns=tga_info.width;
225 image->rows=tga_info.height;
226 image->matte=(tga_info.attributes & 0x0FU) != 0 ? MagickTrue : MagickFalse;
227 if ((tga_info.image_type != TGAColormap) &&
228 (tga_info.image_type != TGARLEColormap))
229 image->depth=(size_t) ((tga_info.bits_per_pixel <= 8) ? 8 :
230 (tga_info.bits_per_pixel <= 16) ? 5 :
231 (tga_info.bits_per_pixel == 24) ? 8 :
232 (tga_info.bits_per_pixel == 32) ? 8 : 8);
234 image->depth=(size_t) ((tga_info.colormap_size <= 8) ? 8 :
235 (tga_info.colormap_size <= 16) ? 5 :
236 (tga_info.colormap_size == 24) ? 8 :
237 (tga_info.colormap_size == 32) ? 8 : 8);
238 if ((tga_info.image_type == TGAColormap) ||
239 (tga_info.image_type == TGAMonochrome) ||
240 (tga_info.image_type == TGARLEColormap) ||
241 (tga_info.image_type == TGARLEMonochrome))
242 image->storage_class=PseudoClass;
243 image->compression=NoCompression;
244 if ((tga_info.image_type == TGARLEColormap) ||
245 (tga_info.image_type == TGARLEMonochrome))
246 image->compression=RLECompression;
247 if (image->storage_class == PseudoClass)
249 if (tga_info.colormap_type != 0)
250 image->colors=tga_info.colormap_length;
257 image->colors=one << tga_info.bits_per_pixel;
258 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
259 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
262 if (tga_info.id_length != 0)
273 length=(size_t) tga_info.id_length;
274 comment=(char *) NULL;
275 if (~length >= (MaxTextExtent-1))
276 comment=(char *) AcquireQuantumMemory(length+MaxTextExtent,
278 if (comment == (char *) NULL)
279 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
280 count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
281 comment[tga_info.id_length]='\0';
282 (void) SetImageProperty(image,"comment",comment,exception);
283 comment=DestroyString(comment);
285 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
286 pixel.alpha=(Quantum) OpaqueAlpha;
287 if (tga_info.colormap_type != 0)
290 Read TGA raster colormap.
292 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
293 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
294 for (i=0; i < (ssize_t) image->colors; i++)
296 switch (tga_info.colormap_size)
304 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
305 pixel.green=pixel.red;
306 pixel.blue=pixel.red;
316 5 bits each of red green and blue.
318 j=(unsigned char) ReadBlobByte(image);
319 k=(unsigned char) ReadBlobByte(image);
320 range=GetQuantumRange(5UL);
321 pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
322 pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
323 (1UL*(j & 0xe0) >> 5),range);
324 pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
331 8 bits each of blue, green and red.
333 pixel.blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
334 pixel.green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
335 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
339 image->colormap[i]=pixel;
343 Convert TGA pixels to pixel packets.
352 for (y=0; y < (ssize_t) image->rows; y++)
355 if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
356 real=image->rows-real-1;
357 q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
358 if (q == (Quantum *) NULL)
360 for (x=0; x < (ssize_t) image->columns; x++)
362 if ((tga_info.image_type == TGARLEColormap) ||
363 (tga_info.image_type == TGARLERGB) ||
364 (tga_info.image_type == TGARLEMonochrome))
373 count=ReadBlob(image,1,&runlength);
375 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
376 flag=runlength & 0x80;
382 if (skip == MagickFalse)
383 switch (tga_info.bits_per_pixel)
391 index=(Quantum) ReadBlobByte(image);
392 if (tga_info.colormap_type != 0)
393 pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
394 1UL*index,exception)];
397 pixel.red=ScaleCharToQuantum((unsigned char) index);
398 pixel.green=ScaleCharToQuantum((unsigned char) index);
399 pixel.blue=ScaleCharToQuantum((unsigned char) index);
412 if (ReadBlob(image,2,pixels) != 2)
413 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
416 range=GetQuantumRange(5UL);
417 pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
418 pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
419 (1UL*(j & 0xe0) >> 5),range);
420 pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
421 if (image->matte != MagickFalse)
422 pixel.alpha=(k & 0x80) == 0 ? (Quantum) OpaqueAlpha : (Quantum)
424 if (image->storage_class == PseudoClass)
425 index=ConstrainColormapIndex(image,((size_t) k << 8)+j,exception);
433 if (ReadBlob(image,3,pixels) != 3)
434 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
435 pixel.blue=ScaleCharToQuantum(pixels[0]);
436 pixel.green=ScaleCharToQuantum(pixels[1]);
437 pixel.red=ScaleCharToQuantum(pixels[2]);
445 if (ReadBlob(image,4,pixels) != 4)
446 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
447 pixel.blue=ScaleCharToQuantum(pixels[0]);
448 pixel.green=ScaleCharToQuantum(pixels[1]);
449 pixel.red=ScaleCharToQuantum(pixels[2]);
450 pixel.alpha=ScaleCharToQuantum(pixels[3]);
454 if (status == MagickFalse)
455 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
456 if (image->storage_class == PseudoClass)
457 SetPixelIndex(image,index,q);
458 SetPixelRed(image,pixel.red,q);
459 SetPixelGreen(image,pixel.green,q);
460 SetPixelBlue(image,pixel.blue,q);
461 if (image->matte != MagickFalse)
462 SetPixelAlpha(image,pixel.alpha,q);
463 q+=GetPixelChannels(image);
465 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
468 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
472 if (offset >= image->rows)
477 if (SyncAuthenticPixels(image,exception) == MagickFalse)
479 if (image->previous == (Image *) NULL)
481 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
483 if (status == MagickFalse)
487 if (EOFBlob(image) != MagickFalse)
488 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
490 (void) CloseBlob(image);
491 return(GetFirstImageInList(image));
495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499 % R e g i s t e r T G A I m a g e %
503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505 % RegisterTGAImage() adds properties for the TGA image format to
506 % the list of supported formats. The properties include the image format
507 % tag, a method to read and/or write the format, whether the format
508 % supports the saving of more than one frame to the same file or blob,
509 % whether the format supports native in-memory I/O, and a brief
510 % description of the format.
512 % The format of the RegisterTGAImage method is:
514 % size_t RegisterTGAImage(void)
517 ModuleExport size_t RegisterTGAImage(void)
522 entry=SetMagickInfo("ICB");
523 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
524 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
525 entry->adjoin=MagickFalse;
526 entry->description=ConstantString("Truevision Targa image");
527 entry->module=ConstantString("TGA");
528 (void) RegisterMagickInfo(entry);
529 entry=SetMagickInfo("TGA");
530 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
531 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
532 entry->adjoin=MagickFalse;
533 entry->description=ConstantString("Truevision Targa image");
534 entry->module=ConstantString("TGA");
535 (void) RegisterMagickInfo(entry);
536 entry=SetMagickInfo("VDA");
537 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
538 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
539 entry->adjoin=MagickFalse;
540 entry->description=ConstantString("Truevision Targa image");
541 entry->module=ConstantString("TGA");
542 (void) RegisterMagickInfo(entry);
543 entry=SetMagickInfo("VST");
544 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
545 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
546 entry->adjoin=MagickFalse;
547 entry->description=ConstantString("Truevision Targa image");
548 entry->module=ConstantString("TGA");
549 (void) RegisterMagickInfo(entry);
550 return(MagickImageCoderSignature);
554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
558 % U n r e g i s t e r T G A I m a g e %
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
564 % UnregisterTGAImage() removes format registrations made by the
565 % TGA module from the list of supported formats.
567 % The format of the UnregisterTGAImage method is:
569 % UnregisterTGAImage(void)
572 ModuleExport void UnregisterTGAImage(void)
574 (void) UnregisterMagickInfo("ICB");
575 (void) UnregisterMagickInfo("TGA");
576 (void) UnregisterMagickInfo("VDA");
577 (void) UnregisterMagickInfo("VST");
581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585 % W r i t e T G A I m a g e %
589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591 % WriteTGAImage() writes a image in the Truevision Targa rasterfile
594 % The format of the WriteTGAImage method is:
596 % MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
597 % Image *image,ExceptionInfo *exception)
599 % A description of each parameter follows.
601 % o image_info: the image info.
603 % o image: The image.
607 static inline size_t MagickMin(const size_t x,const size_t y)
614 static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
615 ExceptionInfo *exception)
617 #define TargaColormap 1
619 #define TargaMonochrome 3
620 #define TargaRLEColormap 9
621 #define TargaRLERGB 10
622 #define TargaRLEMonochrome 11
624 typedef struct _TargaInfo
655 register const Quantum
664 register unsigned char
678 Open output image file.
680 assert(image_info != (const ImageInfo *) NULL);
681 assert(image_info->signature == MagickSignature);
682 assert(image != (Image *) NULL);
683 assert(image->signature == MagickSignature);
684 if (image->debug != MagickFalse)
685 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
686 assert(exception != (ExceptionInfo *) NULL);
687 assert(exception->signature == MagickSignature);
688 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
689 if (status == MagickFalse)
692 Initialize TGA raster file header.
694 if ((image->columns > 65535L) || (image->rows > 65535L))
695 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
696 if (IsRGBColorspace(image->colorspace) == MagickFalse)
697 (void) TransformImageColorspace(image,RGBColorspace,exception);
698 targa_info.id_length=0;
699 value=GetImageProperty(image,"comment",exception);
700 if (value != (const char *) NULL)
701 targa_info.id_length=(unsigned char) MagickMin(strlen(value),255);
702 targa_info.colormap_type=0;
703 targa_info.colormap_index=0;
704 targa_info.colormap_length=0;
705 targa_info.colormap_size=0;
706 targa_info.x_origin=0;
707 targa_info.y_origin=0;
708 targa_info.width=(unsigned short) image->columns;
709 targa_info.height=(unsigned short) image->rows;
710 targa_info.bits_per_pixel=8;
711 targa_info.attributes=0;
712 if ((image_info->type != TrueColorType) &&
713 (image_info->type != TrueColorMatteType) &&
714 (image_info->type != PaletteType) &&
715 (image->matte == MagickFalse) &&
716 (IsImageGray(image,exception) != MagickFalse))
717 targa_info.image_type=TargaMonochrome;
719 if ((image->storage_class == DirectClass) || (image->colors > 256))
722 Full color TGA raster.
724 targa_info.image_type=TargaRGB;
725 targa_info.bits_per_pixel=24;
726 if (image->matte != MagickFalse)
728 targa_info.bits_per_pixel=32;
729 targa_info.attributes=8; /* # of alpha bits */
735 Colormapped TGA raster.
737 targa_info.image_type=TargaColormap;
738 targa_info.colormap_type=1;
739 targa_info.colormap_length=(unsigned short) image->colors;
740 targa_info.colormap_size=24;
745 (void) WriteBlobByte(image,targa_info.id_length);
746 (void) WriteBlobByte(image,targa_info.colormap_type);
747 (void) WriteBlobByte(image,targa_info.image_type);
748 (void) WriteBlobLSBShort(image,targa_info.colormap_index);
749 (void) WriteBlobLSBShort(image,targa_info.colormap_length);
750 (void) WriteBlobByte(image,targa_info.colormap_size);
751 (void) WriteBlobLSBShort(image,targa_info.x_origin);
752 (void) WriteBlobLSBShort(image,targa_info.y_origin);
753 (void) WriteBlobLSBShort(image,targa_info.width);
754 (void) WriteBlobLSBShort(image,targa_info.height);
755 (void) WriteBlobByte(image,targa_info.bits_per_pixel);
756 (void) WriteBlobByte(image,targa_info.attributes);
757 if (targa_info.id_length != 0)
758 (void) WriteBlob(image,targa_info.id_length,(unsigned char *)
760 if (targa_info.image_type == TargaColormap)
766 Dump colormap to file (blue, green, red byte order).
768 targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
769 targa_info.colormap_length,3UL*sizeof(*targa_colormap));
770 if (targa_colormap == (unsigned char *) NULL)
771 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
773 for (i=0; i < (ssize_t) image->colors; i++)
775 *q++=ScaleQuantumToChar(image->colormap[i].blue);
776 *q++=ScaleQuantumToChar(image->colormap[i].green);
777 *q++=ScaleQuantumToChar(image->colormap[i].red);
779 (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length),
781 targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
784 Convert MIFF to TGA raster pixels.
786 count=(ssize_t) (targa_info.bits_per_pixel*targa_info.width)/8;
787 targa_pixels=(unsigned char *) AcquireQuantumMemory((size_t) count,
788 sizeof(*targa_pixels));
789 if (targa_pixels == (unsigned char *) NULL)
790 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
791 for (y=(ssize_t) (image->rows-1); y >= 0; y--)
793 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
794 if (p == (const Quantum *) NULL)
797 for (x=0; x < (ssize_t) image->columns; x++)
799 if (targa_info.image_type == TargaColormap)
800 *q++=(unsigned char) GetPixelIndex(image,p);
802 if (targa_info.image_type == TargaMonochrome)
803 *q++=(unsigned char) ScaleQuantumToChar(GetPixelIntensity(image,p));
806 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
807 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
808 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
809 if (image->matte != MagickFalse)
810 *q++=(unsigned char) ScaleQuantumToChar(GetPixelAlpha(image,p));
812 p+=GetPixelChannels(image);
814 (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
815 if (image->previous == (Image *) NULL)
817 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
819 if (status == MagickFalse)
823 targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
824 (void) CloseBlob(image);