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,length,(unsigned char *) comment);
300 if (count == (ssize_t) length)
302 comment[length]='\0';
303 (void) SetImageProperty(image,"comment",comment,exception);
305 comment=DestroyString(comment);
307 if (tga_info.attributes & (1UL << 4))
309 if (tga_info.attributes & (1UL << 5))
310 image->orientation=TopRightOrientation;
312 image->orientation=BottomRightOrientation;
316 if (tga_info.attributes & (1UL << 5))
317 image->orientation=TopLeftOrientation;
319 image->orientation=BottomLeftOrientation;
321 if (image_info->ping != MagickFalse)
323 (void) CloseBlob(image);
326 status=SetImageExtent(image,image->columns,image->rows,exception);
327 if (status == MagickFalse)
328 return(DestroyImageList(image));
329 (void) memset(&pixel,0,sizeof(pixel));
330 pixel.alpha=(MagickRealType) OpaqueAlpha;
331 if (tga_info.colormap_type != 0)
334 Read TGA raster colormap.
336 if (image->colors < tga_info.colormap_index)
337 image->colors=tga_info.colormap_index;
338 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
339 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
340 for (i=0; i < (ssize_t) tga_info.colormap_index; i++)
341 image->colormap[i]=pixel;
342 for ( ; i < (ssize_t) image->colors; i++)
344 switch (tga_info.colormap_size)
352 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
353 ReadBlobByte(image));
354 pixel.green=pixel.red;
355 pixel.blue=pixel.red;
365 5 bits each of red green and blue.
367 j=(unsigned char) ReadBlobByte(image);
368 k=(unsigned char) ReadBlobByte(image);
369 range=GetQuantumRange(5UL);
370 pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
372 pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*(k & 0x03)
373 << 3)+(1UL*(j & 0xe0) >> 5),range);
374 pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
380 8 bits each of blue, green and red.
382 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
383 ReadBlobByte(image));
384 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
385 ReadBlobByte(image));
386 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
387 ReadBlobByte(image));
393 8 bits each of blue, green, red, and alpha.
395 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
396 ReadBlobByte(image));
397 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
398 ReadBlobByte(image));
399 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
400 ReadBlobByte(image));
401 pixel.alpha=(MagickRealType) ScaleCharToQuantum((unsigned char)
402 ReadBlobByte(image));
406 image->colormap[i]=pixel;
410 Convert TGA pixels to pixel packets.
419 for (y=0; y < (ssize_t) image->rows; y++)
422 if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
423 real=image->rows-real-1;
424 q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
425 if (q == (Quantum *) NULL)
427 for (x=0; x < (ssize_t) image->columns; x++)
429 if ((tga_info.image_type == TGARLEColormap) ||
430 (tga_info.image_type == TGARLERGB) ||
431 (tga_info.image_type == TGARLEMonochrome))
440 count=ReadBlob(image,1,&runlength);
442 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
443 flag=runlength & 0x80;
449 if (skip == MagickFalse)
450 switch (tga_info.bits_per_pixel)
458 if (ReadBlob(image,1,pixels) != 1)
459 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
460 index=(Quantum) pixels[0];
461 if (tga_info.colormap_type != 0)
462 pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
463 (ssize_t) index,exception)];
466 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
468 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
470 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
484 if (ReadBlob(image,2,pixels) != 2)
485 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
488 range=GetQuantumRange(5UL);
489 pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
491 pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*
492 (k & 0x03) << 3)+(1UL*(j & 0xe0) >> 5),range);
493 pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
494 if (image->alpha_trait != UndefinedPixelTrait)
495 pixel.alpha=(MagickRealType) ((k & 0x80) == 0 ? (Quantum)
496 TransparentAlpha : (Quantum) OpaqueAlpha);
497 if (image->storage_class == PseudoClass)
498 index=(Quantum) ConstrainColormapIndex(image,((ssize_t) (k << 8))+
507 if (ReadBlob(image,3,pixels) != 3)
508 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
509 pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
510 pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
511 pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
519 if (ReadBlob(image,4,pixels) != 4)
520 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
521 pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
522 pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
523 pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
524 pixel.alpha=(MagickRealType) ScaleCharToQuantum(pixels[3]);
528 if (status == MagickFalse)
529 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
530 if (image->storage_class == PseudoClass)
531 SetPixelIndex(image,index,q);
532 SetPixelRed(image,ClampToQuantum(pixel.red),q);
533 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
534 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
535 if (image->alpha_trait != UndefinedPixelTrait)
536 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
537 q+=GetPixelChannels(image);
540 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
544 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
548 if (offset >= image->rows)
553 if (SyncAuthenticPixels(image,exception) == MagickFalse)
555 if (image->previous == (Image *) NULL)
557 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
559 if (status == MagickFalse)
563 if (EOFBlob(image) != MagickFalse)
564 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
566 (void) CloseBlob(image);
567 return(GetFirstImageInList(image));
571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
575 % R e g i s t e r T G A I m a g e %
579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
581 % RegisterTGAImage() adds properties for the TGA image format to
582 % the list of supported formats. The properties include the image format
583 % tag, a method to read and/or write the format, whether the format
584 % supports the saving of more than one frame to the same file or blob,
585 % whether the format supports native in-memory I/O, and a brief
586 % description of the format.
588 % The format of the RegisterTGAImage method is:
590 % size_t RegisterTGAImage(void)
593 ModuleExport size_t RegisterTGAImage(void)
598 entry=AcquireMagickInfo("TGA","ICB","Truevision Targa image");
599 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
600 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
601 entry->flags|=CoderDecoderSeekableStreamFlag;
602 entry->flags^=CoderAdjoinFlag;
603 (void) RegisterMagickInfo(entry);
604 entry=AcquireMagickInfo("TGA","TGA","Truevision Targa image");
605 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
606 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
607 entry->flags|=CoderDecoderSeekableStreamFlag;
608 entry->flags^=CoderAdjoinFlag;
609 (void) RegisterMagickInfo(entry);
610 entry=AcquireMagickInfo("TGA","VDA","Truevision Targa image");
611 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
612 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
613 entry->flags|=CoderDecoderSeekableStreamFlag;
614 entry->flags^=CoderAdjoinFlag;
615 (void) RegisterMagickInfo(entry);
616 entry=AcquireMagickInfo("TGA","VST","Truevision Targa image");
617 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
618 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
619 entry->flags|=CoderDecoderSeekableStreamFlag;
620 entry->flags^=CoderAdjoinFlag;
621 (void) RegisterMagickInfo(entry);
622 return(MagickImageCoderSignature);
626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 % U n r e g i s t e r T G A I m a g e %
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 % UnregisterTGAImage() removes format registrations made by the
637 % TGA module from the list of supported formats.
639 % The format of the UnregisterTGAImage method is:
641 % UnregisterTGAImage(void)
644 ModuleExport void UnregisterTGAImage(void)
646 (void) UnregisterMagickInfo("ICB");
647 (void) UnregisterMagickInfo("TGA");
648 (void) UnregisterMagickInfo("VDA");
649 (void) UnregisterMagickInfo("VST");
653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
657 % W r i t e T G A I m a g e %
661 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
663 % WriteTGAImage() writes a image in the Truevision Targa rasterfile
666 % The format of the WriteTGAImage method is:
668 % MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
669 % Image *image,ExceptionInfo *exception)
671 % A description of each parameter follows.
673 % o image_info: the image info.
675 % o image: The image.
678 static inline void WriteTGAPixel(Image *image,TGAImageType image_type,
679 const Quantum *p,const QuantumAny range,const double midpoint)
681 if (image_type == TGAColormap || image_type == TGARLEColormap)
682 (void) WriteBlobByte(image,(unsigned char) GetPixelIndex(image,p));
685 if (image_type == TGAMonochrome || image_type == TGARLEMonochrome)
686 (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
687 GetPixelLuma(image,p))));
689 if (image->depth == 5)
695 green=(unsigned char) ScaleQuantumToAny(GetPixelGreen(image,p),
697 value=((unsigned char) ScaleQuantumToAny(GetPixelBlue(image,p),
698 range)) | ((green & 0x07) << 5);
699 (void) WriteBlobByte(image,value);
700 value=(((image->alpha_trait != UndefinedPixelTrait) &&
701 ((double) GetPixelAlpha(image,p) > midpoint)) ? 0x80 : 0) |
702 ((unsigned char) ScaleQuantumToAny(GetPixelRed(image,p),range) <<
703 2) | ((green & 0x18) >> 3);
704 (void) WriteBlobByte(image,value);
708 (void) WriteBlobByte(image,ScaleQuantumToChar(
709 GetPixelBlue(image,p)));
710 (void) WriteBlobByte(image,ScaleQuantumToChar(
711 GetPixelGreen(image,p)));
712 (void) WriteBlobByte(image,ScaleQuantumToChar(
713 GetPixelRed(image,p)));
714 if (image->alpha_trait != UndefinedPixelTrait)
715 (void) WriteBlobByte(image,ScaleQuantumToChar(
716 GetPixelAlpha(image,p)));
721 static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
722 ExceptionInfo *exception)
731 midpoint = QuantumRange/2.0;
739 register const Quantum
748 register unsigned char
762 Open output image file.
764 assert(image_info != (const ImageInfo *) NULL);
765 assert(image_info->signature == MagickCoreSignature);
766 assert(image != (Image *) NULL);
767 assert(image->signature == MagickCoreSignature);
768 if (image->debug != MagickFalse)
769 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
770 assert(exception != (ExceptionInfo *) NULL);
771 assert(exception->signature == MagickCoreSignature);
772 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
773 if (status == MagickFalse)
776 Initialize TGA raster file header.
778 if ((image->columns > 65535L) || (image->rows > 65535L))
779 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
780 (void) TransformImageColorspace(image,sRGBColorspace,exception);
781 compression=image->compression;
782 if (image_info->compression != UndefinedCompression)
783 compression=image_info->compression;
784 range=GetQuantumRange(5UL);
785 tga_info.id_length=0;
786 comment=GetImageProperty(image,"comment",exception);
787 if (comment != (const char *) NULL)
788 tga_info.id_length=(unsigned char) MagickMin(strlen(comment),255);
789 tga_info.colormap_type=0;
790 tga_info.colormap_index=0;
791 tga_info.colormap_length=0;
792 tga_info.colormap_size=0;
795 tga_info.width=(unsigned short) image->columns;
796 tga_info.height=(unsigned short) image->rows;
797 tga_info.bits_per_pixel=8;
798 tga_info.attributes=0;
799 if ((image_info->type != TrueColorType) &&
800 (image_info->type != TrueColorAlphaType) &&
801 (image_info->type != PaletteType) &&
802 (image->alpha_trait == UndefinedPixelTrait) &&
803 (SetImageGray(image,exception) != MagickFalse))
804 tga_info.image_type=compression == RLECompression ? TGARLEMonochrome :
807 if ((image->storage_class == DirectClass) || (image->colors > 256))
810 Full color TGA raster.
812 tga_info.image_type=compression == RLECompression ? TGARLERGB : TGARGB;
813 if (image_info->depth == 5)
815 tga_info.bits_per_pixel=16;
816 if (image->alpha_trait != UndefinedPixelTrait)
817 tga_info.attributes=1; /* # of alpha bits */
821 tga_info.bits_per_pixel=24;
822 if (image->alpha_trait != UndefinedPixelTrait)
824 tga_info.bits_per_pixel=32;
825 tga_info.attributes=8; /* # of alpha bits */
832 Colormapped TGA raster.
834 tga_info.image_type=compression == RLECompression ? TGARLEColormap :
836 tga_info.colormap_type=1;
837 tga_info.colormap_length=(unsigned short) image->colors;
838 if (image_info->depth == 5)
839 tga_info.colormap_size=16;
841 tga_info.colormap_size=24;
843 if ((image->orientation == BottomRightOrientation) ||
844 (image->orientation == TopRightOrientation))
845 tga_info.attributes|=(1UL << 4);
846 if ((image->orientation == TopLeftOrientation) ||
847 (image->orientation == TopRightOrientation))
848 tga_info.attributes|=(1UL << 5);
852 (void) WriteBlobByte(image,tga_info.id_length);
853 (void) WriteBlobByte(image,tga_info.colormap_type);
854 (void) WriteBlobByte(image,(unsigned char) tga_info.image_type);
855 (void) WriteBlobLSBShort(image,tga_info.colormap_index);
856 (void) WriteBlobLSBShort(image,tga_info.colormap_length);
857 (void) WriteBlobByte(image,tga_info.colormap_size);
858 (void) WriteBlobLSBShort(image,tga_info.x_origin);
859 (void) WriteBlobLSBShort(image,tga_info.y_origin);
860 (void) WriteBlobLSBShort(image,tga_info.width);
861 (void) WriteBlobLSBShort(image,tga_info.height);
862 (void) WriteBlobByte(image,tga_info.bits_per_pixel);
863 (void) WriteBlobByte(image,tga_info.attributes);
864 if (tga_info.id_length != 0)
865 (void) WriteBlob(image,tga_info.id_length,(unsigned char *) comment);
866 if (tga_info.colormap_type != 0)
873 Dump colormap to file (blue, green, red byte order).
875 targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
876 tga_info.colormap_length,(tga_info.colormap_size/8)*
877 sizeof(*targa_colormap));
878 if (targa_colormap == (unsigned char *) NULL)
879 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
881 for (i=0; i < (ssize_t) image->colors; i++)
883 if (image_info->depth == 5)
885 green=(unsigned char) ScaleQuantumToAny(ClampToQuantum(
886 image->colormap[i].green),range);
887 *q++=((unsigned char) ScaleQuantumToAny(ClampToQuantum(
888 image->colormap[i].blue),range)) | ((green & 0x07) << 5);
889 *q++=(((image->alpha_trait != UndefinedPixelTrait) && ((double)
890 ClampToQuantum(image->colormap[i].alpha) > midpoint)) ? 0x80 : 0) |
891 ((unsigned char) ScaleQuantumToAny(ClampToQuantum(
892 image->colormap[i].red),range) << 2) | ((green & 0x18) >> 3);
896 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
897 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
898 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
901 (void) WriteBlob(image,(size_t) ((tga_info.colormap_size/8)*
902 tga_info.colormap_length),targa_colormap);
903 targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
906 Convert MIFF to TGA raster pixels.
908 channels=GetPixelChannels(image);
909 for (y=(ssize_t) (image->rows-1); y >= 0; y--)
911 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
912 if (p == (const Quantum *) NULL)
914 if (compression == RLECompression)
918 while (x < (ssize_t) image->columns)
921 while ((i < 128) && (count + i < 128) &&
922 ((x + i) < (ssize_t) image->columns))
924 if (tga_info.image_type == TGARLEColormap)
926 if (GetPixelIndex(image,p+(i*channels)) !=
927 GetPixelIndex(image,p+((i-1)*channels)))
930 else if (tga_info.image_type == TGARLEMonochrome)
932 if (GetPixelLuma(image,p+(i*channels)) !=
933 GetPixelLuma(image,p+((i-1)*channels)))
938 if ((GetPixelBlue(image,p+(i*channels)) !=
939 GetPixelBlue(image,p+((i-1)*channels))) ||
940 (GetPixelGreen(image,p+(i*channels)) !=
941 GetPixelGreen(image,p+((i-1)*channels))) ||
942 (GetPixelRed(image,p+(i*channels)) !=
943 GetPixelRed(image,p+((i-1)*channels))))
945 if ((image->alpha_trait != UndefinedPixelTrait) &&
946 (GetPixelAlpha(image,p+(i*channels)) !=
947 GetPixelAlpha(image,p+(i-1)*channels)))
957 if ((i >= 3) || (count == 128) ||
958 ((x + i) == (ssize_t) image->columns))
962 (void) WriteBlobByte(image,(unsigned char) (--count));
965 WriteTGAPixel(image,tga_info.image_type,p-((count+1)*
966 channels),range,midpoint);
974 (void) WriteBlobByte(image,(unsigned char) ((i-1) | 0x80));
975 WriteTGAPixel(image,tga_info.image_type,p,range,midpoint);
983 for (x=0; x < (ssize_t) image->columns; x++)
985 WriteTGAPixel(image,tga_info.image_type,p,range,midpoint);
989 if (image->previous == (Image *) NULL)
991 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
993 if (status == MagickFalse)
997 (void) CloseBlob(image);