2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Truevision Targa Image Format %
20 % Copyright 1999-2018 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 % https://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/artifact.h"
44 #include "MagickCore/attribute.h"
45 #include "MagickCore/blob.h"
46 #include "MagickCore/blob-private.h"
47 #include "MagickCore/cache.h"
48 #include "MagickCore/color-private.h"
49 #include "MagickCore/colormap.h"
50 #include "MagickCore/colormap-private.h"
51 #include "MagickCore/colorspace.h"
52 #include "MagickCore/colorspace-private.h"
53 #include "MagickCore/exception.h"
54 #include "MagickCore/exception-private.h"
55 #include "MagickCore/image.h"
56 #include "MagickCore/image-private.h"
57 #include "MagickCore/list.h"
58 #include "MagickCore/magick.h"
59 #include "MagickCore/memory_.h"
60 #include "MagickCore/monitor.h"
61 #include "MagickCore/monitor-private.h"
62 #include "MagickCore/option.h"
63 #include "MagickCore/pixel-accessor.h"
64 #include "MagickCore/property.h"
65 #include "MagickCore/quantum-private.h"
66 #include "MagickCore/static.h"
67 #include "MagickCore/string_.h"
68 #include "MagickCore/module.h"
71 Enumerated declaractions.
84 Typedef declaractions.
86 typedef struct _TGAInfo
114 Forward declarations.
116 static MagickBooleanType
117 WriteTGAImage(const ImageInfo *,Image *,ExceptionInfo *);
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % R e a d T G A I m a g e %
128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 % ReadTGAImage() reads a Truevision TGA image file and returns it.
131 % It allocates the memory necessary for the new Image structure and returns
132 % a pointer to the new image.
134 % The format of the ReadTGAImage method is:
136 % Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
138 % A description of each parameter follows:
140 % o image_info: the image info.
142 % o exception: return any errors or warnings in this structure.
145 static Image *ReadTGAImage(const ImageInfo *image_info,
146 ExceptionInfo *exception)
193 assert(image_info != (const ImageInfo *) NULL);
194 assert(image_info->signature == MagickCoreSignature);
195 if (image_info->debug != MagickFalse)
196 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
197 image_info->filename);
198 assert(exception != (ExceptionInfo *) NULL);
199 assert(exception->signature == MagickCoreSignature);
200 image=AcquireImage(image_info,exception);
201 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
202 if (status == MagickFalse)
204 image=DestroyImageList(image);
205 return((Image *) NULL);
208 Read TGA header information.
210 count=ReadBlob(image,1,&tga_info.id_length);
211 tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
212 tga_info.image_type=(TGAImageType) ReadBlobByte(image);
214 ((tga_info.image_type != TGAColormap) &&
215 (tga_info.image_type != TGARGB) &&
216 (tga_info.image_type != TGAMonochrome) &&
217 (tga_info.image_type != TGARLEColormap) &&
218 (tga_info.image_type != TGARLERGB) &&
219 (tga_info.image_type != TGARLEMonochrome)) ||
220 (((tga_info.image_type == TGAColormap) ||
221 (tga_info.image_type == TGARLEColormap)) &&
222 (tga_info.colormap_type == 0)))
223 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
224 tga_info.colormap_index=ReadBlobLSBShort(image);
225 tga_info.colormap_length=ReadBlobLSBShort(image);
226 tga_info.colormap_size=(unsigned char) ReadBlobByte(image);
227 tga_info.x_origin=ReadBlobLSBShort(image);
228 tga_info.y_origin=ReadBlobLSBShort(image);
229 tga_info.width=(unsigned short) ReadBlobLSBShort(image);
230 tga_info.height=(unsigned short) ReadBlobLSBShort(image);
231 tga_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
232 tga_info.attributes=(unsigned char) ReadBlobByte(image);
233 if (EOFBlob(image) != MagickFalse)
234 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
235 if ((((tga_info.bits_per_pixel <= 1) || (tga_info.bits_per_pixel >= 17)) &&
236 (tga_info.bits_per_pixel != 24) && (tga_info.bits_per_pixel != 32)))
237 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
239 Initialize image structure.
241 image->columns=tga_info.width;
242 image->rows=tga_info.height;
243 alpha_bits=(tga_info.attributes & 0x0FU);
244 image->alpha_trait=(alpha_bits > 0) || (tga_info.bits_per_pixel == 32) ||
245 (tga_info.colormap_size == 32) ? BlendPixelTrait : UndefinedPixelTrait;
246 if ((tga_info.image_type != TGAColormap) &&
247 (tga_info.image_type != TGARLEColormap))
248 image->depth=(size_t) ((tga_info.bits_per_pixel <= 8) ? 8 :
249 (tga_info.bits_per_pixel <= 16) ? 5 : 8);
251 image->depth=(size_t) ((tga_info.colormap_size <= 8) ? 8 :
252 (tga_info.colormap_size <= 16) ? 5 : 8);
253 if ((tga_info.image_type == TGAColormap) ||
254 (tga_info.image_type == TGAMonochrome) ||
255 (tga_info.image_type == TGARLEColormap) ||
256 (tga_info.image_type == TGARLEMonochrome))
257 image->storage_class=PseudoClass;
258 image->compression=NoCompression;
259 if ((tga_info.image_type == TGARLEColormap) ||
260 (tga_info.image_type == TGARLEMonochrome) ||
261 (tga_info.image_type == TGARLERGB))
262 image->compression=RLECompression;
263 if (image->storage_class == PseudoClass)
265 if (tga_info.colormap_type != 0)
266 image->colors=tga_info.colormap_index+tga_info.colormap_length;
273 image->colors=one << tga_info.bits_per_pixel;
274 if (image->colors > GetBlobSize(image))
275 ThrowReaderException(CorruptImageError,
276 "InsufficientImageDataInFile");
277 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
278 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
281 if (tga_info.id_length != 0)
292 length=(size_t) tga_info.id_length;
293 comment=(char *) NULL;
294 if (~length >= (MagickPathExtent-1))
295 comment=(char *) AcquireQuantumMemory(length+MagickPathExtent,
297 if (comment == (char *) NULL)
298 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
299 count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
300 comment[tga_info.id_length]='\0';
301 (void) SetImageProperty(image,"comment",comment,exception);
302 comment=DestroyString(comment);
304 if (tga_info.attributes & (1UL << 4))
306 if (tga_info.attributes & (1UL << 5))
307 SetImageArtifact(image,"tga:image-origin","TopRight");
309 SetImageArtifact(image,"tga:image-origin","BottomRight");
313 if (tga_info.attributes & (1UL << 5))
314 SetImageArtifact(image,"tga:image-origin","TopLeft");
316 SetImageArtifact(image,"tga:image-origin","BottomLeft");
318 if (image_info->ping != MagickFalse)
320 (void) CloseBlob(image);
323 status=SetImageExtent(image,image->columns,image->rows,exception);
324 if (status == MagickFalse)
325 return(DestroyImageList(image));
326 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
327 pixel.alpha=(MagickRealType) OpaqueAlpha;
328 if (tga_info.colormap_type != 0)
331 Read TGA raster colormap.
333 if (image->colors < tga_info.colormap_index)
334 image->colors=tga_info.colormap_index;
335 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
336 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
337 for (i=0; i < (ssize_t) tga_info.colormap_index; i++)
338 image->colormap[i]=pixel;
339 for ( ; i < (ssize_t) image->colors; i++)
341 switch (tga_info.colormap_size)
349 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
350 ReadBlobByte(image));
351 pixel.green=pixel.red;
352 pixel.blue=pixel.red;
362 5 bits each of red green and blue.
364 j=(unsigned char) ReadBlobByte(image);
365 k=(unsigned char) ReadBlobByte(image);
366 range=GetQuantumRange(5UL);
367 pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
369 pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*(k & 0x03)
370 << 3)+(1UL*(j & 0xe0) >> 5),range);
371 pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
377 8 bits each of blue, green and red.
379 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
380 ReadBlobByte(image));
381 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
382 ReadBlobByte(image));
383 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
384 ReadBlobByte(image));
390 8 bits each of blue, green, red, and alpha.
392 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
393 ReadBlobByte(image));
394 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
395 ReadBlobByte(image));
396 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
397 ReadBlobByte(image));
398 pixel.alpha=(MagickRealType) ScaleCharToQuantum((unsigned char)
399 ReadBlobByte(image));
403 image->colormap[i]=pixel;
407 Convert TGA pixels to pixel packets.
416 for (y=0; y < (ssize_t) image->rows; y++)
419 if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
420 real=image->rows-real-1;
421 q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
422 if (q == (Quantum *) NULL)
424 for (x=0; x < (ssize_t) image->columns; x++)
426 if ((tga_info.image_type == TGARLEColormap) ||
427 (tga_info.image_type == TGARLERGB) ||
428 (tga_info.image_type == TGARLEMonochrome))
437 count=ReadBlob(image,1,&runlength);
439 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
440 flag=runlength & 0x80;
446 if (skip == MagickFalse)
447 switch (tga_info.bits_per_pixel)
455 index=(Quantum) ReadBlobByte(image);
456 if (tga_info.colormap_type != 0)
457 pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
458 (ssize_t) index,exception)];
461 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
463 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
465 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
479 if (ReadBlob(image,2,pixels) != 2)
480 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
483 range=GetQuantumRange(5UL);
484 pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
486 pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*
487 (k & 0x03) << 3)+(1UL*(j & 0xe0) >> 5),range);
488 pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
489 if (image->alpha_trait != UndefinedPixelTrait)
490 pixel.alpha=(MagickRealType) ((k & 0x80) == 0 ? (Quantum)
491 TransparentAlpha : (Quantum) OpaqueAlpha);
492 if (image->storage_class == PseudoClass)
493 index=(Quantum) ConstrainColormapIndex(image,((ssize_t) (k << 8))+
502 if (ReadBlob(image,3,pixels) != 3)
503 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
504 pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
505 pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
506 pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
514 if (ReadBlob(image,4,pixels) != 4)
515 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
516 pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
517 pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
518 pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
519 pixel.alpha=(MagickRealType) ScaleCharToQuantum(pixels[3]);
523 if (status == MagickFalse)
524 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
525 if (image->storage_class == PseudoClass)
526 SetPixelIndex(image,index,q);
527 SetPixelRed(image,ClampToQuantum(pixel.red),q);
528 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
529 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
530 if (image->alpha_trait != UndefinedPixelTrait)
531 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
532 q+=GetPixelChannels(image);
535 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
539 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
543 if (offset >= image->rows)
548 if (SyncAuthenticPixels(image,exception) == MagickFalse)
550 if (image->previous == (Image *) NULL)
552 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
554 if (status == MagickFalse)
558 if (EOFBlob(image) != MagickFalse)
559 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
561 (void) CloseBlob(image);
562 return(GetFirstImageInList(image));
566 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
570 % R e g i s t e r T G A I m a g e %
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 % RegisterTGAImage() adds properties for the TGA image format to
577 % the list of supported formats. The properties include the image format
578 % tag, a method to read and/or write the format, whether the format
579 % supports the saving of more than one frame to the same file or blob,
580 % whether the format supports native in-memory I/O, and a brief
581 % description of the format.
583 % The format of the RegisterTGAImage method is:
585 % size_t RegisterTGAImage(void)
588 ModuleExport size_t RegisterTGAImage(void)
593 entry=AcquireMagickInfo("TGA","ICB","Truevision Targa image");
594 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
595 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
596 entry->flags|=CoderDecoderSeekableStreamFlag;
597 entry->flags^=CoderAdjoinFlag;
598 (void) RegisterMagickInfo(entry);
599 entry=AcquireMagickInfo("TGA","TGA","Truevision Targa image");
600 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
601 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
602 entry->flags|=CoderDecoderSeekableStreamFlag;
603 entry->flags^=CoderAdjoinFlag;
604 (void) RegisterMagickInfo(entry);
605 entry=AcquireMagickInfo("TGA","VDA","Truevision Targa image");
606 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
607 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
608 entry->flags|=CoderDecoderSeekableStreamFlag;
609 entry->flags^=CoderAdjoinFlag;
610 (void) RegisterMagickInfo(entry);
611 entry=AcquireMagickInfo("TGA","VST","Truevision Targa image");
612 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
613 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
614 entry->flags|=CoderDecoderSeekableStreamFlag;
615 entry->flags^=CoderAdjoinFlag;
616 (void) RegisterMagickInfo(entry);
617 return(MagickImageCoderSignature);
621 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
625 % U n r e g i s t e r T G A I m a g e %
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631 % UnregisterTGAImage() removes format registrations made by the
632 % TGA module from the list of supported formats.
634 % The format of the UnregisterTGAImage method is:
636 % UnregisterTGAImage(void)
639 ModuleExport void UnregisterTGAImage(void)
641 (void) UnregisterMagickInfo("ICB");
642 (void) UnregisterMagickInfo("TGA");
643 (void) UnregisterMagickInfo("VDA");
644 (void) UnregisterMagickInfo("VST");
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 % W r i t e T G A I m a g e %
656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
658 % WriteTGAImage() writes a image in the Truevision Targa rasterfile
661 % The format of the WriteTGAImage method is:
663 % MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
664 % Image *image,ExceptionInfo *exception)
666 % A description of each parameter follows.
668 % o image_info: the image info.
670 % o image: The image.
673 static inline void WriteTGAPixel(Image *image,TGAImageType image_type,
674 const Quantum *p,const QuantumAny range,const double midpoint)
676 if (image_type == TGAColormap || image_type == TGARLEColormap)
677 (void) WriteBlobByte(image,(unsigned char) GetPixelIndex(image,p));
680 if (image_type == TGAMonochrome || image_type == TGARLEMonochrome)
681 (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
682 GetPixelLuma(image,p))));
684 if (image->depth == 5)
690 green=(unsigned char) ScaleQuantumToAny(GetPixelGreen(image,p),
692 value=((unsigned char) ScaleQuantumToAny(GetPixelBlue(image,p),
693 range)) | ((green & 0x07) << 5);
694 (void) WriteBlobByte(image,value);
695 value=(((image->alpha_trait != UndefinedPixelTrait) &&
696 ((double) GetPixelAlpha(image,p) > midpoint)) ? 0x80 : 0) |
697 ((unsigned char) ScaleQuantumToAny(GetPixelRed(image,p),range) <<
698 2) | ((green & 0x18) >> 3);
699 (void) WriteBlobByte(image,value);
703 (void) WriteBlobByte(image,ScaleQuantumToChar(
704 GetPixelBlue(image,p)));
705 (void) WriteBlobByte(image,ScaleQuantumToChar(
706 GetPixelGreen(image,p)));
707 (void) WriteBlobByte(image,ScaleQuantumToChar(
708 GetPixelRed(image,p)));
709 if (image->alpha_trait != UndefinedPixelTrait)
710 (void) WriteBlobByte(image,ScaleQuantumToChar(
711 GetPixelAlpha(image,p)));
716 static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
717 ExceptionInfo *exception)
727 midpoint = QuantumRange/2.0;
735 register const Quantum
744 register unsigned char
758 Open output image file.
760 assert(image_info != (const ImageInfo *) NULL);
761 assert(image_info->signature == MagickCoreSignature);
762 assert(image != (Image *) NULL);
763 assert(image->signature == MagickCoreSignature);
764 if (image->debug != MagickFalse)
765 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
766 assert(exception != (ExceptionInfo *) NULL);
767 assert(exception->signature == MagickCoreSignature);
768 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
769 if (status == MagickFalse)
772 Initialize TGA raster file header.
774 if ((image->columns > 65535L) || (image->rows > 65535L))
775 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
776 (void) TransformImageColorspace(image,sRGBColorspace,exception);
777 compression=image->compression;
778 if (image_info->compression != UndefinedCompression)
779 compression=image_info->compression;
780 range=GetQuantumRange(5UL);
781 tga_info.id_length=0;
782 comment=GetImageProperty(image,"comment",exception);
783 if (comment != (const char *) NULL)
784 tga_info.id_length=(unsigned char) MagickMin(strlen(comment),255);
785 tga_info.colormap_type=0;
786 tga_info.colormap_index=0;
787 tga_info.colormap_length=0;
788 tga_info.colormap_size=0;
791 tga_info.width=(unsigned short) image->columns;
792 tga_info.height=(unsigned short) image->rows;
793 tga_info.bits_per_pixel=8;
794 tga_info.attributes=0;
795 if ((image_info->type != TrueColorType) &&
796 (image_info->type != TrueColorAlphaType) &&
797 (image_info->type != PaletteType) &&
798 (image->alpha_trait == UndefinedPixelTrait) &&
799 (SetImageGray(image,exception) != MagickFalse))
800 tga_info.image_type=compression == RLECompression ? TGARLEMonochrome :
803 if ((image->storage_class == DirectClass) || (image->colors > 256))
806 Full color TGA raster.
808 tga_info.image_type=compression == RLECompression ? TGARLERGB : TGARGB;
809 if (image_info->depth == 5)
811 tga_info.bits_per_pixel=16;
812 if (image->alpha_trait != UndefinedPixelTrait)
813 tga_info.attributes=1; /* # of alpha bits */
817 tga_info.bits_per_pixel=24;
818 if (image->alpha_trait != UndefinedPixelTrait)
820 tga_info.bits_per_pixel=32;
821 tga_info.attributes=8; /* # of alpha bits */
828 Colormapped TGA raster.
830 tga_info.image_type=compression == RLECompression ? TGARLEColormap :
832 tga_info.colormap_type=1;
833 tga_info.colormap_length=(unsigned short) image->colors;
834 if (image_info->depth == 5)
835 tga_info.colormap_size=16;
837 tga_info.colormap_size=24;
839 value=GetImageArtifact(image,"tga:image-origin");
840 if (value != (const char *) NULL)
845 origin=(OrientationType) ParseCommandOption(MagickOrientationOptions,
847 if (origin == BottomRightOrientation || origin == TopRightOrientation)
848 tga_info.attributes|=(1UL << 4);
849 if (origin == TopLeftOrientation || origin == TopRightOrientation)
850 tga_info.attributes|=(1UL << 5);
855 (void) WriteBlobByte(image,tga_info.id_length);
856 (void) WriteBlobByte(image,tga_info.colormap_type);
857 (void) WriteBlobByte(image,(unsigned char) tga_info.image_type);
858 (void) WriteBlobLSBShort(image,tga_info.colormap_index);
859 (void) WriteBlobLSBShort(image,tga_info.colormap_length);
860 (void) WriteBlobByte(image,tga_info.colormap_size);
861 (void) WriteBlobLSBShort(image,tga_info.x_origin);
862 (void) WriteBlobLSBShort(image,tga_info.y_origin);
863 (void) WriteBlobLSBShort(image,tga_info.width);
864 (void) WriteBlobLSBShort(image,tga_info.height);
865 (void) WriteBlobByte(image,tga_info.bits_per_pixel);
866 (void) WriteBlobByte(image,tga_info.attributes);
867 if (tga_info.id_length != 0)
868 (void) WriteBlob(image,tga_info.id_length,(unsigned char *) comment);
869 if (tga_info.colormap_type != 0)
876 Dump colormap to file (blue, green, red byte order).
878 targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
879 tga_info.colormap_length,(tga_info.colormap_size/8)*
880 sizeof(*targa_colormap));
881 if (targa_colormap == (unsigned char *) NULL)
882 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
884 for (i=0; i < (ssize_t) image->colors; i++)
886 if (image_info->depth == 5)
888 green=(unsigned char) ScaleQuantumToAny(ClampToQuantum(
889 image->colormap[i].green),range);
890 *q++=((unsigned char) ScaleQuantumToAny(ClampToQuantum(
891 image->colormap[i].blue),range)) | ((green & 0x07) << 5);
892 *q++=(((image->alpha_trait != UndefinedPixelTrait) && ((double)
893 ClampToQuantum(image->colormap[i].alpha) > midpoint)) ? 0x80 : 0) |
894 ((unsigned char) ScaleQuantumToAny(ClampToQuantum(
895 image->colormap[i].red),range) << 2) | ((green & 0x18) >> 3);
899 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
900 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
901 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
904 (void) WriteBlob(image,(size_t) ((tga_info.colormap_size/8)*
905 tga_info.colormap_length),targa_colormap);
906 targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
909 Convert MIFF to TGA raster pixels.
911 channels=GetPixelChannels(image);
912 for (y=(ssize_t) (image->rows-1); y >= 0; y--)
914 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
915 if (p == (const Quantum *) NULL)
917 if (compression == RLECompression)
921 while (x < (ssize_t) image->columns)
924 while ((i < 128) && (count + i < 128) &&
925 ((x + i) < (ssize_t) image->columns))
927 if (tga_info.image_type == TGARLEColormap)
929 if (GetPixelIndex(image,p+(i*channels)) !=
930 GetPixelIndex(image,p+((i-1)*channels)))
933 else if (tga_info.image_type == TGARLEMonochrome)
935 if (GetPixelLuma(image,p+(i*channels)) !=
936 GetPixelLuma(image,p+((i-1)*channels)))
941 if ((GetPixelBlue(image,p+(i*channels)) !=
942 GetPixelBlue(image,p+((i-1)*channels))) ||
943 (GetPixelGreen(image,p+(i*channels)) !=
944 GetPixelGreen(image,p+((i-1)*channels))) ||
945 (GetPixelRed(image,p+(i*channels)) !=
946 GetPixelRed(image,p+((i-1)*channels))))
948 if ((image->alpha_trait != UndefinedPixelTrait) &&
949 (GetPixelAlpha(image,p+(i*channels)) !=
950 GetPixelAlpha(image,p+(i-1)*channels)))
960 if ((i >= 3) || (count == 128) ||
961 ((x + i) == (ssize_t) image->columns))
965 (void) WriteBlobByte(image,(unsigned char) (--count));
968 WriteTGAPixel(image,tga_info.image_type,p-((count+1)*
969 channels),range,midpoint);
977 (void) WriteBlobByte(image,(unsigned char) ((i-1) | 0x80));
978 WriteTGAPixel(image,tga_info.image_type,p,range,midpoint);
986 for (x=0; x < (ssize_t) image->columns; x++)
988 WriteTGAPixel(image,tga_info.image_type,p,range,midpoint);
992 if (image->previous == (Image *) NULL)
994 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
996 if (status == MagickFalse)
1000 (void) CloseBlob(image);