2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Read/Write Truevision Targa Image Format %
20 % Copyright 1999-2017 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/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 (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
275 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
278 if (tga_info.id_length != 0)
289 length=(size_t) tga_info.id_length;
290 comment=(char *) NULL;
291 if (~length >= (MagickPathExtent-1))
292 comment=(char *) AcquireQuantumMemory(length+MagickPathExtent,
294 if (comment == (char *) NULL)
295 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
296 count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
297 comment[tga_info.id_length]='\0';
298 (void) SetImageProperty(image,"comment",comment,exception);
299 comment=DestroyString(comment);
301 if (tga_info.attributes & (1UL << 4))
303 if (tga_info.attributes & (1UL << 5))
304 SetImageArtifact(image,"tga:image-origin","TopRight");
306 SetImageArtifact(image,"tga:image-origin","BottomRight");
310 if (tga_info.attributes & (1UL << 5))
311 SetImageArtifact(image,"tga:image-origin","TopLeft");
313 SetImageArtifact(image,"tga:image-origin","BottomLeft");
315 if (image_info->ping != MagickFalse)
317 (void) CloseBlob(image);
320 status=SetImageExtent(image,image->columns,image->rows,exception);
321 if (status == MagickFalse)
322 return(DestroyImageList(image));
323 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
324 pixel.alpha=(MagickRealType) OpaqueAlpha;
325 if (tga_info.colormap_type != 0)
328 Read TGA raster colormap.
330 if (image->colors < tga_info.colormap_index)
331 image->colors=tga_info.colormap_index;
332 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
333 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
334 for (i=0; i < (ssize_t) tga_info.colormap_index; i++)
335 image->colormap[i]=pixel;
336 for ( ; i < (ssize_t) image->colors; i++)
338 switch (tga_info.colormap_size)
346 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
347 ReadBlobByte(image));
348 pixel.green=pixel.red;
349 pixel.blue=pixel.red;
359 5 bits each of red green and blue.
361 j=(unsigned char) ReadBlobByte(image);
362 k=(unsigned char) ReadBlobByte(image);
363 range=GetQuantumRange(5UL);
364 pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
366 pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*(k & 0x03)
367 << 3)+(1UL*(j & 0xe0) >> 5),range);
368 pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
374 8 bits each of blue, green and red.
376 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
377 ReadBlobByte(image));
378 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
379 ReadBlobByte(image));
380 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
381 ReadBlobByte(image));
387 8 bits each of blue, green, red, and alpha.
389 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
390 ReadBlobByte(image));
391 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
392 ReadBlobByte(image));
393 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
394 ReadBlobByte(image));
395 pixel.alpha=(MagickRealType) ScaleCharToQuantum((unsigned char)
396 ReadBlobByte(image));
400 image->colormap[i]=pixel;
404 Convert TGA pixels to pixel packets.
413 for (y=0; y < (ssize_t) image->rows; y++)
416 if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
417 real=image->rows-real-1;
418 q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
419 if (q == (Quantum *) NULL)
421 for (x=0; x < (ssize_t) image->columns; x++)
423 if ((tga_info.image_type == TGARLEColormap) ||
424 (tga_info.image_type == TGARLERGB) ||
425 (tga_info.image_type == TGARLEMonochrome))
434 count=ReadBlob(image,1,&runlength);
436 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
437 flag=runlength & 0x80;
443 if (skip == MagickFalse)
444 switch (tga_info.bits_per_pixel)
452 index=(Quantum) ReadBlobByte(image);
453 if (tga_info.colormap_type != 0)
454 pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
455 (ssize_t) index,exception)];
458 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
460 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
462 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
476 if (ReadBlob(image,2,pixels) != 2)
477 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
480 range=GetQuantumRange(5UL);
481 pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
483 pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*
484 (k & 0x03) << 3)+(1UL*(j & 0xe0) >> 5),range);
485 pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
486 if (image->alpha_trait != UndefinedPixelTrait)
487 pixel.alpha=(MagickRealType) ((k & 0x80) == 0 ? (Quantum)
488 TransparentAlpha : (Quantum) OpaqueAlpha);
489 if (image->storage_class == PseudoClass)
490 index=(Quantum) ConstrainColormapIndex(image,((ssize_t) (k << 8))+
499 if (ReadBlob(image,3,pixels) != 3)
500 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
501 pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
502 pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
503 pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
511 if (ReadBlob(image,4,pixels) != 4)
512 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
513 pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
514 pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
515 pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
516 pixel.alpha=(MagickRealType) ScaleCharToQuantum(pixels[3]);
520 if (status == MagickFalse)
521 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
522 if (image->storage_class == PseudoClass)
523 SetPixelIndex(image,index,q);
524 SetPixelRed(image,ClampToQuantum(pixel.red),q);
525 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
526 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
527 if (image->alpha_trait != UndefinedPixelTrait)
528 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
529 q+=GetPixelChannels(image);
532 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
536 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
540 if (offset >= image->rows)
545 if (SyncAuthenticPixels(image,exception) == MagickFalse)
547 if (image->previous == (Image *) NULL)
549 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
551 if (status == MagickFalse)
555 if (EOFBlob(image) != MagickFalse)
556 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
558 (void) CloseBlob(image);
559 return(GetFirstImageInList(image));
563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
567 % R e g i s t e r T G A I m a g e %
571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
573 % RegisterTGAImage() adds properties for the TGA image format to
574 % the list of supported formats. The properties include the image format
575 % tag, a method to read and/or write the format, whether the format
576 % supports the saving of more than one frame to the same file or blob,
577 % whether the format supports native in-memory I/O, and a brief
578 % description of the format.
580 % The format of the RegisterTGAImage method is:
582 % size_t RegisterTGAImage(void)
585 ModuleExport size_t RegisterTGAImage(void)
590 entry=AcquireMagickInfo("TGA","ICB","Truevision Targa image");
591 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
592 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
593 entry->flags^=CoderAdjoinFlag;
594 (void) RegisterMagickInfo(entry);
595 entry=AcquireMagickInfo("TGA","TGA","Truevision Targa image");
596 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
597 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
598 entry->flags^=CoderAdjoinFlag;
599 (void) RegisterMagickInfo(entry);
600 entry=AcquireMagickInfo("TGA","VDA","Truevision Targa image");
601 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
602 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
603 entry->flags^=CoderAdjoinFlag;
604 (void) RegisterMagickInfo(entry);
605 entry=AcquireMagickInfo("TGA","VST","Truevision Targa image");
606 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
607 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
608 entry->flags^=CoderAdjoinFlag;
609 (void) RegisterMagickInfo(entry);
610 return(MagickImageCoderSignature);
614 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
618 % U n r e g i s t e r T G A I m a g e %
622 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624 % UnregisterTGAImage() removes format registrations made by the
625 % TGA module from the list of supported formats.
627 % The format of the UnregisterTGAImage method is:
629 % UnregisterTGAImage(void)
632 ModuleExport void UnregisterTGAImage(void)
634 (void) UnregisterMagickInfo("ICB");
635 (void) UnregisterMagickInfo("TGA");
636 (void) UnregisterMagickInfo("VDA");
637 (void) UnregisterMagickInfo("VST");
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 % W r i t e T G A I m a g e %
649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 % WriteTGAImage() writes a image in the Truevision Targa rasterfile
654 % The format of the WriteTGAImage method is:
656 % MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
657 % Image *image,ExceptionInfo *exception)
659 % A description of each parameter follows.
661 % o image_info: the image info.
663 % o image: The image.
666 static inline void WriteTGAPixel(Image *image,TGAImageType image_type,
667 const Quantum *p,const QuantumAny range,const double midpoint)
669 if (image_type == TGAColormap || image_type == TGARLEColormap)
670 (void) WriteBlobByte(image,(unsigned char) GetPixelIndex(image,p));
673 if (image_type == TGAMonochrome || image_type == TGARLEMonochrome)
674 (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
675 GetPixelLuma(image,p))));
677 if (image->depth == 5)
683 green=(unsigned char) ScaleQuantumToAny(GetPixelGreen(image,p),
685 value=((unsigned char) ScaleQuantumToAny(GetPixelBlue(image,p),
686 range)) | ((green & 0x07) << 5);
687 (void) WriteBlobByte(image,value);
688 value=(((image->alpha_trait != UndefinedPixelTrait) &&
689 ((double) GetPixelAlpha(image,p) > midpoint)) ? 0x80 : 0) |
690 ((unsigned char) ScaleQuantumToAny(GetPixelRed(image,p),range) <<
691 2) | ((green & 0x18) >> 3);
692 (void) WriteBlobByte(image,value);
696 (void) WriteBlobByte(image,ScaleQuantumToChar(
697 GetPixelBlue(image,p)));
698 (void) WriteBlobByte(image,ScaleQuantumToChar(
699 GetPixelGreen(image,p)));
700 (void) WriteBlobByte(image,ScaleQuantumToChar(
701 GetPixelRed(image,p)));
702 if (image->alpha_trait != UndefinedPixelTrait)
703 (void) WriteBlobByte(image,ScaleQuantumToChar(
704 GetPixelAlpha(image,p)));
709 static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
710 ExceptionInfo *exception)
719 midpoint = QuantumRange/2.0;
727 register const Quantum
736 register unsigned char
750 Open output image file.
752 assert(image_info != (const ImageInfo *) NULL);
753 assert(image_info->signature == MagickCoreSignature);
754 assert(image != (Image *) NULL);
755 assert(image->signature == MagickCoreSignature);
756 if (image->debug != MagickFalse)
757 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
758 assert(exception != (ExceptionInfo *) NULL);
759 assert(exception->signature == MagickCoreSignature);
760 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
761 if (status == MagickFalse)
764 Initialize TGA raster file header.
766 if ((image->columns > 65535L) || (image->rows > 65535L))
767 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
768 (void) TransformImageColorspace(image,sRGBColorspace,exception);
769 compression=image->compression;
770 if (image_info->compression != UndefinedCompression)
771 compression=image_info->compression;
772 range=GetQuantumRange(5UL);
773 tga_info.id_length=0;
774 value=GetImageProperty(image,"comment",exception);
775 if (value != (const char *) NULL)
776 tga_info.id_length=(unsigned char) MagickMin(strlen(value),255);
777 tga_info.colormap_type=0;
778 tga_info.colormap_index=0;
779 tga_info.colormap_length=0;
780 tga_info.colormap_size=0;
783 tga_info.width=(unsigned short) image->columns;
784 tga_info.height=(unsigned short) image->rows;
785 tga_info.bits_per_pixel=8;
786 tga_info.attributes=0;
787 if ((image_info->type != TrueColorType) &&
788 (image_info->type != TrueColorAlphaType) &&
789 (image_info->type != PaletteType) &&
790 (image->alpha_trait == UndefinedPixelTrait) &&
791 (SetImageGray(image,exception) != MagickFalse))
792 tga_info.image_type=compression == RLECompression ? TGARLEMonochrome :
795 if ((image->storage_class == DirectClass) || (image->colors > 256))
798 Full color TGA raster.
800 tga_info.image_type=compression == RLECompression ? TGARLERGB : TGARGB;
801 if (image_info->depth == 5)
803 tga_info.bits_per_pixel=16;
804 if (image->alpha_trait != UndefinedPixelTrait)
805 tga_info.attributes=1; /* # of alpha bits */
809 tga_info.bits_per_pixel=24;
810 if (image->alpha_trait != UndefinedPixelTrait)
812 tga_info.bits_per_pixel=32;
813 tga_info.attributes=8; /* # of alpha bits */
820 Colormapped TGA raster.
822 tga_info.image_type=compression == RLECompression ? TGARLEColormap :
824 tga_info.colormap_type=1;
825 tga_info.colormap_length=(unsigned short) image->colors;
826 if (image_info->depth == 5)
827 tga_info.colormap_size=16;
829 tga_info.colormap_size=24;
831 value=GetImageArtifact(image,"tga:image-origin");
832 if (value != (const char *) NULL)
837 origin=(OrientationType) ParseCommandOption(MagickOrientationOptions,
839 if (origin == BottomRightOrientation || origin == TopRightOrientation)
840 tga_info.attributes|=(1UL << 4);
841 if (origin == TopLeftOrientation || origin == TopRightOrientation)
842 tga_info.attributes|=(1UL << 5);
847 (void) WriteBlobByte(image,tga_info.id_length);
848 (void) WriteBlobByte(image,tga_info.colormap_type);
849 (void) WriteBlobByte(image,(unsigned char) tga_info.image_type);
850 (void) WriteBlobLSBShort(image,tga_info.colormap_index);
851 (void) WriteBlobLSBShort(image,tga_info.colormap_length);
852 (void) WriteBlobByte(image,tga_info.colormap_size);
853 (void) WriteBlobLSBShort(image,tga_info.x_origin);
854 (void) WriteBlobLSBShort(image,tga_info.y_origin);
855 (void) WriteBlobLSBShort(image,tga_info.width);
856 (void) WriteBlobLSBShort(image,tga_info.height);
857 (void) WriteBlobByte(image,tga_info.bits_per_pixel);
858 (void) WriteBlobByte(image,tga_info.attributes);
859 if (tga_info.id_length != 0)
860 (void) WriteBlob(image,tga_info.id_length,(unsigned char *) value);
861 if (tga_info.colormap_type != 0)
868 Dump colormap to file (blue, green, red byte order).
870 targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
871 tga_info.colormap_length,(tga_info.colormap_size/8)*
872 sizeof(*targa_colormap));
873 if (targa_colormap == (unsigned char *) NULL)
874 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
876 for (i=0; i < (ssize_t) image->colors; i++)
878 if (image_info->depth == 5)
880 green=(unsigned char) ScaleQuantumToAny(ClampToQuantum(
881 image->colormap[i].green),range);
882 *q++=((unsigned char) ScaleQuantumToAny(ClampToQuantum(
883 image->colormap[i].blue),range)) | ((green & 0x07) << 5);
884 *q++=(((image->alpha_trait != UndefinedPixelTrait) && ((double)
885 ClampToQuantum(image->colormap[i].alpha) > midpoint)) ? 0x80 : 0) |
886 ((unsigned char) ScaleQuantumToAny(ClampToQuantum(
887 image->colormap[i].red),range) << 2) | ((green & 0x18) >> 3);
891 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
892 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
893 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
896 (void) WriteBlob(image,(size_t) ((tga_info.colormap_size/8)*
897 tga_info.colormap_length),targa_colormap);
898 targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
901 Convert MIFF to TGA raster pixels.
903 channels=GetPixelChannels(image);
904 for (y=(ssize_t) (image->rows-1); y >= 0; y--)
906 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
907 if (p == (const Quantum *) NULL)
909 if (compression == RLECompression)
913 while (x < (ssize_t) image->columns)
916 while ((i < 128) && (count + i < 128) &&
917 ((x + i) < (ssize_t) image->columns))
919 if (tga_info.image_type == TGARLEColormap)
921 if (GetPixelIndex(image,p+(i*channels)) !=
922 GetPixelIndex(image,p+((i-1)*channels)))
925 else if (tga_info.image_type == TGARLEMonochrome)
927 if (GetPixelLuma(image,p+(i*channels)) !=
928 GetPixelLuma(image,p+((i-1)*channels)))
933 if ((GetPixelBlue(image,p+(i*channels)) !=
934 GetPixelBlue(image,p+((i-1)*channels))) ||
935 (GetPixelGreen(image,p+(i*channels)) !=
936 GetPixelGreen(image,p+((i-1)*channels))) ||
937 (GetPixelRed(image,p+(i*channels)) !=
938 GetPixelRed(image,p+((i-1)*channels))))
940 if ((image->alpha_trait != UndefinedPixelTrait) &&
941 (GetPixelAlpha(image,p+(i*channels)) !=
942 GetPixelAlpha(image,p+(i-1)*channels)))
952 if ((i >= 3) || (count == 128) ||
953 ((x + i) == (ssize_t) image->columns))
957 (void) WriteBlobByte(image,(unsigned char) (--count));
960 WriteTGAPixel(image,tga_info.image_type,p-((count+1)*
961 channels),range,midpoint);
969 (void) WriteBlobByte(image,(unsigned char) ((i-1) | 0x80));
970 WriteTGAPixel(image,tga_info.image_type,p,range,midpoint);
978 for (x=0; x < (ssize_t) image->columns; x++)
980 WriteTGAPixel(image,tga_info.image_type,p,range,midpoint);
984 if (image->previous == (Image *) NULL)
986 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
988 if (status == MagickFalse)
992 (void) CloseBlob(image);