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 "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
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,exception);
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,exception) == 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-1))
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.alpha=(Quantum) OpaqueAlpha;
286 if (tga_info.colormap_type != 0)
289 Read TGA raster colormap.
291 if (AcquireImageColormap(image,image->colors,exception) == 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.
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 == (Quantum *) NULL)
359 for (x=0; x < (ssize_t) image->columns; x++)
361 if ((tga_info.image_type == TGARLEColormap) ||
362 (tga_info.image_type == TGARLERGB) ||
363 (tga_info.image_type == TGARLEMonochrome))
372 count=ReadBlob(image,1,&runlength);
374 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
375 flag=runlength & 0x80;
381 if (skip == MagickFalse)
382 switch (tga_info.bits_per_pixel)
390 index=(Quantum) ReadBlobByte(image);
391 if (tga_info.colormap_type != 0)
392 pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
396 pixel.red=ScaleCharToQuantum((unsigned char) index);
397 pixel.green=ScaleCharToQuantum((unsigned char) index);
398 pixel.blue=ScaleCharToQuantum((unsigned char) index);
409 5 bits each of red green and blue.
411 j=(unsigned char) ReadBlobByte(image);
412 k=(unsigned char) ReadBlobByte(image);
413 range=GetQuantumRange(5UL);
414 pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
415 pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
416 (1UL*(j & 0xe0) >> 5),range);
417 pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
418 if (image->matte != MagickFalse)
419 pixel.alpha=(k & 0x80) == 0 ? (Quantum) OpaqueAlpha :
420 (Quantum) TransparentAlpha;
421 if (image->storage_class == PseudoClass)
422 index=ConstrainColormapIndex(image,((size_t) k << 8)+j);
429 8 bits each of blue green and red.
431 pixel.blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
432 pixel.green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
433 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
434 if (tga_info.bits_per_pixel == 32)
435 pixel.alpha=ScaleCharToQuantum((unsigned char)
436 ReadBlobByte(image));
440 if (status == MagickFalse)
441 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
442 if (image->storage_class == PseudoClass)
443 SetPixelIndex(image,index,q);
444 SetPixelRed(image,pixel.red,q);
445 SetPixelGreen(image,pixel.green,q);
446 SetPixelBlue(image,pixel.blue,q);
447 if (image->matte != MagickFalse)
448 SetPixelAlpha(image,pixel.alpha,q);
449 q+=GetPixelChannels(image);
451 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
454 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
458 if (offset >= image->rows)
463 if (SyncAuthenticPixels(image,exception) == MagickFalse)
465 if (image->previous == (Image *) NULL)
467 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
469 if (status == MagickFalse)
473 if (EOFBlob(image) != MagickFalse)
474 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
476 (void) CloseBlob(image);
477 return(GetFirstImageInList(image));
481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
485 % R e g i s t e r T G A I m a g e %
489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
491 % RegisterTGAImage() adds properties for the TGA image format to
492 % the list of supported formats. The properties include the image format
493 % tag, a method to read and/or write the format, whether the format
494 % supports the saving of more than one frame to the same file or blob,
495 % whether the format supports native in-memory I/O, and a brief
496 % description of the format.
498 % The format of the RegisterTGAImage method is:
500 % size_t RegisterTGAImage(void)
503 ModuleExport size_t RegisterTGAImage(void)
508 entry=SetMagickInfo("ICB");
509 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
510 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
511 entry->adjoin=MagickFalse;
512 entry->description=ConstantString("Truevision Targa image");
513 entry->module=ConstantString("TGA");
514 (void) RegisterMagickInfo(entry);
515 entry=SetMagickInfo("TGA");
516 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
517 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
518 entry->adjoin=MagickFalse;
519 entry->description=ConstantString("Truevision Targa image");
520 entry->module=ConstantString("TGA");
521 (void) RegisterMagickInfo(entry);
522 entry=SetMagickInfo("VDA");
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("VST");
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 return(MagickImageCoderSignature);
540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
544 % U n r e g i s t e r T G A I m a g e %
548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550 % UnregisterTGAImage() removes format registrations made by the
551 % TGA module from the list of supported formats.
553 % The format of the UnregisterTGAImage method is:
555 % UnregisterTGAImage(void)
558 ModuleExport void UnregisterTGAImage(void)
560 (void) UnregisterMagickInfo("ICB");
561 (void) UnregisterMagickInfo("TGA");
562 (void) UnregisterMagickInfo("VDA");
563 (void) UnregisterMagickInfo("VST");
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
571 % W r i t e T G A I m a g e %
575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
577 % WriteTGAImage() writes a image in the Truevision Targa rasterfile
580 % The format of the WriteTGAImage method is:
582 % MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
583 % Image *image,ExceptionInfo *exception)
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,
601 ExceptionInfo *exception)
603 #define TargaColormap 1
605 #define TargaMonochrome 3
606 #define TargaRLEColormap 9
607 #define TargaRLERGB 10
608 #define TargaRLEMonochrome 11
610 typedef struct _TargaInfo
641 register const Quantum
650 register unsigned char
664 Open output image file.
666 assert(image_info != (const ImageInfo *) NULL);
667 assert(image_info->signature == MagickSignature);
668 assert(image != (Image *) NULL);
669 assert(image->signature == MagickSignature);
670 if (image->debug != MagickFalse)
671 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
672 assert(exception != (ExceptionInfo *) NULL);
673 assert(exception->signature == MagickSignature);
674 status=OpenBlob(image_info,image,WriteBinaryBlobMode,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 (IsRGBColorspace(image->colorspace) == MagickFalse)
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 (IsImageGray(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,exception);
780 if (p == (const Quantum *) NULL)
783 for (x=0; x < (ssize_t) image->columns; x++)
785 if (targa_info.image_type == TargaColormap)
786 *q++=(unsigned char) GetPixelIndex(image,p);
788 if (targa_info.image_type == TargaMonochrome)
789 *q++=(unsigned char) ScaleQuantumToChar(GetPixelIntensity(image,p));
792 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
793 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
794 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
795 if (image->matte != MagickFalse)
796 *q++=(unsigned char) ScaleQuantumToChar(GetPixelAlpha(image,p));
798 p+=GetPixelChannels(image);
800 (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
801 if (image->previous == (Image *) NULL)
803 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
805 if (status == MagickFalse)
809 targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
810 (void) CloseBlob(image);