2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Truevision Targa Image Format %
20 % Copyright 1999-2011 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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/blob-private.h"
45 #include "magick/cache.h"
46 #include "magick/color-private.h"
47 #include "magick/colormap.h"
48 #include "magick/colormap-private.h"
49 #include "magick/colorspace.h"
50 #include "magick/exception.h"
51 #include "magick/exception-private.h"
52 #include "magick/image.h"
53 #include "magick/image-private.h"
54 #include "magick/list.h"
55 #include "magick/magick.h"
56 #include "magick/memory_.h"
57 #include "magick/monitor.h"
58 #include "magick/monitor-private.h"
59 #include "magick/property.h"
60 #include "magick/quantum-private.h"
61 #include "magick/static.h"
62 #include "magick/string_.h"
63 #include "magick/module.h"
68 static MagickBooleanType
69 WriteTGAImage(const ImageInfo *,Image *);
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 % R e a d T G A I m a g e %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % ReadTGAImage() reads a Truevision TGA image file and returns it.
83 % It allocates the memory necessary for the new Image structure and returns
84 % a pointer to the new image.
86 % The format of the ReadTGAImage method is:
88 % Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
90 % A description of each parameter follows:
92 % o image_info: the image info.
94 % o exception: return any errors or warnings in this structure.
97 static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
101 #define TGAMonochrome 3
102 #define TGARLEColormap 9
104 #define TGARLEMonochrome 11
106 typedef struct _TGAInfo
175 assert(image_info != (const ImageInfo *) NULL);
176 assert(image_info->signature == MagickSignature);
177 if (image_info->debug != MagickFalse)
178 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
179 image_info->filename);
180 assert(exception != (ExceptionInfo *) NULL);
181 assert(exception->signature == MagickSignature);
182 image=AcquireImage(image_info);
183 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
184 if (status == MagickFalse)
186 image=DestroyImageList(image);
187 return((Image *) NULL);
190 Read TGA header information.
192 count=ReadBlob(image,1,&tga_info.id_length);
193 tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
194 tga_info.image_type=(unsigned char) ReadBlobByte(image);
196 ((tga_info.image_type != TGAColormap) &&
197 (tga_info.image_type != TGARGB) &&
198 (tga_info.image_type != TGAMonochrome) &&
199 (tga_info.image_type != TGARLEColormap) &&
200 (tga_info.image_type != TGARLERGB) &&
201 (tga_info.image_type != TGARLEMonochrome)) ||
202 (((tga_info.image_type == TGAColormap) ||
203 (tga_info.image_type == TGARLEColormap)) &&
204 (tga_info.colormap_type == 0)))
205 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
206 tga_info.colormap_index=ReadBlobLSBShort(image);
207 tga_info.colormap_length=ReadBlobLSBShort(image);
208 tga_info.colormap_size=(unsigned char) ReadBlobByte(image);
209 tga_info.x_origin=ReadBlobLSBShort(image);
210 tga_info.y_origin=ReadBlobLSBShort(image);
211 tga_info.width=(unsigned short) ReadBlobLSBShort(image);
212 tga_info.height=(unsigned short) ReadBlobLSBShort(image);
213 tga_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
214 tga_info.attributes=(unsigned char) ReadBlobByte(image);
215 if (EOFBlob(image) != MagickFalse)
216 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
217 if ((((tga_info.bits_per_pixel <= 1) || (tga_info.bits_per_pixel >= 17)) &&
218 (tga_info.bits_per_pixel != 24) && (tga_info.bits_per_pixel != 32)))
219 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
221 Initialize image structure.
223 image->columns=tga_info.width;
224 image->rows=tga_info.height;
225 image->matte=(tga_info.attributes & 0x0FU) != 0 ? MagickTrue : MagickFalse;
226 if ((tga_info.image_type != TGAColormap) &&
227 (tga_info.image_type != TGARLEColormap))
228 image->depth=(size_t) ((tga_info.bits_per_pixel <= 8) ? 8 :
229 (tga_info.bits_per_pixel <= 16) ? 5 :
230 (tga_info.bits_per_pixel == 24) ? 8 :
231 (tga_info.bits_per_pixel == 32) ? 8 : 8);
233 image->depth=(size_t) ((tga_info.colormap_size <= 8) ? 8 :
234 (tga_info.colormap_size <= 16) ? 5 :
235 (tga_info.colormap_size == 24) ? 8 :
236 (tga_info.colormap_size == 32) ? 8 : 8);
237 if ((tga_info.image_type == TGAColormap) ||
238 (tga_info.image_type == TGAMonochrome) ||
239 (tga_info.image_type == TGARLEColormap) ||
240 (tga_info.image_type == TGARLEMonochrome))
241 image->storage_class=PseudoClass;
242 image->compression=NoCompression;
243 if ((tga_info.image_type == TGARLEColormap) ||
244 (tga_info.image_type == TGARLEMonochrome))
245 image->compression=RLECompression;
246 if (image->storage_class == PseudoClass)
248 if (tga_info.colormap_type != 0)
249 image->colors=tga_info.colormap_length;
256 image->colors=one << tga_info.bits_per_pixel;
257 if (AcquireImageColormap(image,image->colors) == MagickFalse)
258 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
261 if (tga_info.id_length != 0)
272 length=(size_t) tga_info.id_length;
273 comment=(char *) NULL;
274 if (~length >= MaxTextExtent)
275 comment=(char *) AcquireQuantumMemory(length+MaxTextExtent,
277 if (comment == (char *) NULL)
278 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
279 count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
280 comment[tga_info.id_length]='\0';
281 (void) SetImageProperty(image,"comment",comment);
282 comment=DestroyString(comment);
284 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
285 pixel.opacity=(Quantum) OpaqueOpacity;
286 if (tga_info.colormap_type != 0)
289 Read TGA raster colormap.
291 if (AcquireImageColormap(image,image->colors) == MagickFalse)
292 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
293 for (i=0; i < (ssize_t) image->colors; i++)
295 switch (tga_info.colormap_size)
303 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
304 pixel.green=pixel.red;
305 pixel.blue=pixel.red;
315 5 bits each of red green and blue.
317 j=(unsigned char) ReadBlobByte(image);
318 k=(unsigned char) ReadBlobByte(image);
319 range=GetQuantumRange(5UL);
320 pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
321 pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
322 (1UL*(j & 0xe0) >> 5),range);
323 pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
330 8 bits each of blue, green and red.
332 pixel.blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
333 pixel.green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
334 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
338 image->colormap[i]=pixel;
342 Convert TGA pixels to pixel packets.
348 index=(IndexPacket) 0;
351 for (y=0; y < (ssize_t) image->rows; y++)
354 if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
355 real=image->rows-real-1;
356 q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
357 if (q == (PixelPacket *) NULL)
359 indexes=GetAuthenticIndexQueue(image);
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=(IndexPacket) ReadBlobByte(image);
392 if (tga_info.colormap_type != 0)
393 pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
397 pixel.red=ScaleCharToQuantum((unsigned char) index);
398 pixel.green=ScaleCharToQuantum((unsigned char) index);
399 pixel.blue=ScaleCharToQuantum((unsigned char) index);
410 5 bits each of red green and blue.
412 j=(unsigned char) ReadBlobByte(image);
413 k=(unsigned char) ReadBlobByte(image);
414 range=GetQuantumRange(5UL);
415 pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
416 pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
417 (1UL*(j & 0xe0) >> 5),range);
418 pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
419 if (image->matte != MagickFalse)
420 pixel.opacity=(k & 0x80) == 0 ? (Quantum) OpaqueOpacity :
421 (Quantum) TransparentOpacity;
422 if (image->storage_class == PseudoClass)
423 index=ConstrainColormapIndex(image,((size_t) k << 8)+j);
430 8 bits each of blue green and red.
432 pixel.blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
433 pixel.green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
434 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
435 if (tga_info.bits_per_pixel == 32)
436 pixel.opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
437 (unsigned char) ReadBlobByte(image)));
441 if (status == MagickFalse)
442 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
443 if (image->storage_class == PseudoClass)
444 SetIndexPixelComponent(indexes+x,index);
445 SetRedPixelComponent(q,pixel.red);
446 SetGreenPixelComponent(q,pixel.green);
447 SetBluePixelComponent(q,pixel.blue);
448 if (image->matte != MagickFalse)
449 SetOpacityPixelComponent(q,pixel.opacity);
452 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
455 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
459 if (offset >= image->rows)
464 if (SyncAuthenticPixels(image,exception) == MagickFalse)
466 if (image->previous == (Image *) NULL)
468 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
470 if (status == MagickFalse)
474 if (EOFBlob(image) != MagickFalse)
475 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
477 (void) CloseBlob(image);
478 return(GetFirstImageInList(image));
482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
486 % R e g i s t e r T G A I m a g e %
490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 % RegisterTGAImage() adds properties for the TGA image format to
493 % the list of supported formats. The properties include the image format
494 % tag, a method to read and/or write the format, whether the format
495 % supports the saving of more than one frame to the same file or blob,
496 % whether the format supports native in-memory I/O, and a brief
497 % description of the format.
499 % The format of the RegisterTGAImage method is:
501 % size_t RegisterTGAImage(void)
504 ModuleExport size_t RegisterTGAImage(void)
509 entry=SetMagickInfo("ICB");
510 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
511 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
512 entry->adjoin=MagickFalse;
513 entry->description=ConstantString("Truevision Targa image");
514 entry->module=ConstantString("TGA");
515 (void) RegisterMagickInfo(entry);
516 entry=SetMagickInfo("TGA");
517 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
518 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
519 entry->adjoin=MagickFalse;
520 entry->description=ConstantString("Truevision Targa image");
521 entry->module=ConstantString("TGA");
522 (void) RegisterMagickInfo(entry);
523 entry=SetMagickInfo("VDA");
524 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
525 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
526 entry->adjoin=MagickFalse;
527 entry->description=ConstantString("Truevision Targa image");
528 entry->module=ConstantString("TGA");
529 (void) RegisterMagickInfo(entry);
530 entry=SetMagickInfo("VST");
531 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
532 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
533 entry->adjoin=MagickFalse;
534 entry->description=ConstantString("Truevision Targa image");
535 entry->module=ConstantString("TGA");
536 (void) RegisterMagickInfo(entry);
537 return(MagickImageCoderSignature);
541 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
545 % U n r e g i s t e r T G A I m a g e %
549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
551 % UnregisterTGAImage() removes format registrations made by the
552 % TGA module from the list of supported formats.
554 % The format of the UnregisterTGAImage method is:
556 % UnregisterTGAImage(void)
559 ModuleExport void UnregisterTGAImage(void)
561 (void) UnregisterMagickInfo("ICB");
562 (void) UnregisterMagickInfo("TGA");
563 (void) UnregisterMagickInfo("VDA");
564 (void) UnregisterMagickInfo("VST");
568 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
572 % W r i t e T G A I m a g e %
576 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578 % WriteTGAImage() writes a image in the Truevision Targa rasterfile
581 % The format of the WriteTGAImage method is:
583 % MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
585 % A description of each parameter follows.
587 % o image_info: the image info.
589 % o image: The image.
593 static inline size_t MagickMin(const size_t x,const size_t y)
600 static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
602 #define TargaColormap 1
604 #define TargaMonochrome 3
605 #define TargaRLEColormap 9
606 #define TargaRLERGB 10
607 #define TargaRLEMonochrome 11
609 typedef struct _TargaInfo
640 register const IndexPacket
643 register const PixelPacket
652 register unsigned char
666 Open output image file.
668 assert(image_info != (const ImageInfo *) NULL);
669 assert(image_info->signature == MagickSignature);
670 assert(image != (Image *) NULL);
671 assert(image->signature == MagickSignature);
672 if (image->debug != MagickFalse)
673 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
674 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
675 if (status == MagickFalse)
678 Initialize TGA raster file header.
680 if ((image->columns > 65535L) || (image->rows > 65535L))
681 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
682 if (image->colorspace != RGBColorspace)
683 (void) TransformImageColorspace(image,RGBColorspace);
684 targa_info.id_length=0;
685 value=GetImageProperty(image,"comment");
686 if (value != (const char *) NULL)
687 targa_info.id_length=(unsigned char) MagickMin(strlen(value),255);
688 targa_info.colormap_type=0;
689 targa_info.colormap_index=0;
690 targa_info.colormap_length=0;
691 targa_info.colormap_size=0;
692 targa_info.x_origin=0;
693 targa_info.y_origin=0;
694 targa_info.width=(unsigned short) image->columns;
695 targa_info.height=(unsigned short) image->rows;
696 targa_info.bits_per_pixel=8;
697 targa_info.attributes=0;
698 if ((image_info->type != TrueColorType) &&
699 (image_info->type != TrueColorMatteType) &&
700 (image_info->type != PaletteType) &&
701 (image->matte == MagickFalse) &&
702 (IsGrayImage(image,&image->exception) != MagickFalse))
703 targa_info.image_type=TargaMonochrome;
705 if ((image->storage_class == DirectClass) || (image->colors > 256))
708 Full color TGA raster.
710 targa_info.image_type=TargaRGB;
711 targa_info.bits_per_pixel=24;
712 if (image->matte != MagickFalse)
714 targa_info.bits_per_pixel=32;
715 targa_info.attributes=8; /* # of alpha bits */
721 Colormapped TGA raster.
723 targa_info.image_type=TargaColormap;
724 targa_info.colormap_type=1;
725 targa_info.colormap_length=(unsigned short) image->colors;
726 targa_info.colormap_size=24;
731 (void) WriteBlobByte(image,targa_info.id_length);
732 (void) WriteBlobByte(image,targa_info.colormap_type);
733 (void) WriteBlobByte(image,targa_info.image_type);
734 (void) WriteBlobLSBShort(image,targa_info.colormap_index);
735 (void) WriteBlobLSBShort(image,targa_info.colormap_length);
736 (void) WriteBlobByte(image,targa_info.colormap_size);
737 (void) WriteBlobLSBShort(image,targa_info.x_origin);
738 (void) WriteBlobLSBShort(image,targa_info.y_origin);
739 (void) WriteBlobLSBShort(image,targa_info.width);
740 (void) WriteBlobLSBShort(image,targa_info.height);
741 (void) WriteBlobByte(image,targa_info.bits_per_pixel);
742 (void) WriteBlobByte(image,targa_info.attributes);
743 if (targa_info.id_length != 0)
744 (void) WriteBlob(image,targa_info.id_length,(unsigned char *)
746 if (targa_info.image_type == TargaColormap)
752 Dump colormap to file (blue, green, red byte order).
754 targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
755 targa_info.colormap_length,3UL*sizeof(*targa_colormap));
756 if (targa_colormap == (unsigned char *) NULL)
757 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
759 for (i=0; i < (ssize_t) image->colors; i++)
761 *q++=ScaleQuantumToChar(image->colormap[i].blue);
762 *q++=ScaleQuantumToChar(image->colormap[i].green);
763 *q++=ScaleQuantumToChar(image->colormap[i].red);
765 (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length),
767 targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
770 Convert MIFF to TGA raster pixels.
772 count=(ssize_t) (targa_info.bits_per_pixel*targa_info.width)/8;
773 targa_pixels=(unsigned char *) AcquireQuantumMemory((size_t) count,
774 sizeof(*targa_pixels));
775 if (targa_pixels == (unsigned char *) NULL)
776 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
777 for (y=(ssize_t) (image->rows-1); y >= 0; y--)
779 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
780 if (p == (const PixelPacket *) NULL)
783 indexes=GetVirtualIndexQueue(image);
784 for (x=0; x < (ssize_t) image->columns; x++)
786 if (targa_info.image_type == TargaColormap)
787 *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
789 if (targa_info.image_type == TargaMonochrome)
790 *q++=(unsigned char) ScaleQuantumToChar(PixelIntensityToQuantum(p));
793 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
794 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
795 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
796 if (image->matte != MagickFalse)
797 *q++=(unsigned char) ScaleQuantumToChar(
798 GetAlphaPixelComponent(p));
799 if (image->colorspace == CMYKColorspace)
800 *q++=ScaleQuantumToChar(indexes[x]);
804 (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
805 if (image->previous == (Image *) NULL)
807 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
809 if (status == MagickFalse)
813 targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
814 (void) CloseBlob(image);