]> granicus.if.org Git - imagemagick/blob - coders/tga.c
(no commit message)
[imagemagick] / coders / tga.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            TTTTT   GGGG   AAA                               %
7 %                              T    G      A   A                              %
8 %                              T    G  GG  AAAAA                              %
9 %                              T    G   G  A   A                              %
10 %                              T     GGG   A   A                              %
11 %                                                                             %
12 %                                                                             %
13 %                    Read/Write Truevision Targa Image Format                 %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 July 1992                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    http://www.imagemagick.org/script/license.php                            %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 \f
39 /*
40   Include declarations.
41 */
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"
64 \f
65 /*
66   Forward declarations.
67 */
68 static MagickBooleanType
69   WriteTGAImage(const ImageInfo *,Image *);
70 \f
71 /*
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %                                                                             %
74 %                                                                             %
75 %                                                                             %
76 %   R e a d T G A I m a g e                                                   %
77 %                                                                             %
78 %                                                                             %
79 %                                                                             %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %
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.
85 %
86 %  The format of the ReadTGAImage method is:
87 %
88 %      Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
89 %
90 %  A description of each parameter follows:
91 %
92 %    o image_info: the image info.
93 %
94 %    o exception: return any errors or warnings in this structure.
95 %
96 */
97 static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
98 {
99 #define TGAColormap 1
100 #define TGARGB 2
101 #define TGAMonochrome 3
102 #define TGARLEColormap  9
103 #define TGARLERGB  10
104 #define TGARLEMonochrome  11
105
106   typedef struct _TGAInfo
107   {
108     unsigned char
109       id_length,
110       colormap_type,
111       image_type;
112
113     unsigned short
114       colormap_index,
115       colormap_length;
116
117     unsigned char
118       colormap_size;
119
120     unsigned short
121       x_origin,
122       y_origin,
123       width,
124       height;
125
126     unsigned char
127       bits_per_pixel,
128       attributes;
129   } TGAInfo;
130
131   Image
132     *image;
133
134   IndexPacket
135     index;
136
137   MagickBooleanType
138     status;
139
140   PixelPacket
141     pixel;
142
143   register IndexPacket
144     *indexes;
145
146   register PixelPacket
147     *q;
148
149   register ssize_t
150     i,
151     x;
152
153   size_t
154     base,
155     flag,
156     offset,
157     real,
158     skip;
159
160   ssize_t
161     count,
162     y;
163
164   TGAInfo
165     tga_info;
166
167   unsigned char
168     j,
169     k,
170     runlength;
171
172   /*
173     Open image file.
174   */
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)
185     {
186       image=DestroyImageList(image);
187       return((Image *) NULL);
188     }
189   /*
190     Read TGA header information.
191   */
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);
195   if ((count != 1) ||
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");
220   /*
221     Initialize image structure.
222   */
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);
232   else
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)
247     {
248       if (tga_info.colormap_type != 0)
249         image->colors=tga_info.colormap_length;
250       else
251         {
252           size_t
253             one;
254
255           one=1;
256           image->colors=one << tga_info.bits_per_pixel;
257           if (AcquireImageColormap(image,image->colors) == MagickFalse)
258             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
259         }
260     }
261   if (tga_info.id_length != 0)
262     {
263       char
264         *comment;
265
266       size_t
267         length;
268
269       /*
270         TGA image comment.
271       */
272       length=(size_t) tga_info.id_length;
273       comment=(char *) NULL;
274       if (~length >= MaxTextExtent)
275         comment=(char *) AcquireQuantumMemory(length+MaxTextExtent,
276           sizeof(*comment));
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);
283     }
284   (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
285   pixel.opacity=(Quantum) OpaqueOpacity;
286   if (tga_info.colormap_type != 0)
287     {
288       /*
289         Read TGA raster colormap.
290       */
291       if (AcquireImageColormap(image,image->colors) == MagickFalse)
292         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
293       for (i=0; i < (ssize_t) image->colors; i++)
294       {
295         switch (tga_info.colormap_size)
296         {
297           case 8:
298           default:
299           {
300             /*
301               Gray scale.
302             */
303             pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
304             pixel.green=pixel.red;
305             pixel.blue=pixel.red;
306             break;
307           }
308           case 15:
309           case 16:
310           {
311             QuantumAny
312               range;
313
314             /*
315               5 bits each of red green and blue.
316             */
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);
324             break;
325           }
326           case 24:
327           case 32:
328           {
329             /*
330               8 bits each of blue, green and red.
331             */
332             pixel.blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
333             pixel.green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
334             pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
335             break;
336           }
337         }
338         image->colormap[i]=pixel;
339       }
340     }
341   /*
342     Convert TGA pixels to pixel packets.
343   */
344   base=0;
345   flag=0;
346   skip=MagickFalse;
347   real=0;
348   index=(IndexPacket) 0;
349   runlength=0;
350   offset=0;
351   for (y=0; y < (ssize_t) image->rows; y++)
352   {
353     real=offset;
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)
358       break;
359     indexes=GetAuthenticIndexQueue(image);
360     for (x=0; x < (ssize_t) image->columns; x++)
361     {
362       if ((tga_info.image_type == TGARLEColormap) ||
363           (tga_info.image_type == TGARLERGB) ||
364           (tga_info.image_type == TGARLEMonochrome))
365         {
366           if (runlength != 0)
367             {
368               runlength--;
369               skip=flag != 0;
370             }
371           else
372             {
373               count=ReadBlob(image,1,&runlength);
374               if (count == 0)
375                 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
376               flag=runlength & 0x80;
377               if (flag != 0)
378                 runlength-=128;
379               skip=MagickFalse;
380             }
381         }
382       if (skip == MagickFalse)
383         switch (tga_info.bits_per_pixel)
384         {
385           case 8:
386           default:
387           {
388             /*
389               Gray scale.
390             */
391             index=(IndexPacket) ReadBlobByte(image);
392             if (tga_info.colormap_type != 0)
393               pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
394                 1UL*index)];
395             else
396               {
397                 pixel.red=ScaleCharToQuantum((unsigned char) index);
398                 pixel.green=ScaleCharToQuantum((unsigned char) index);
399                 pixel.blue=ScaleCharToQuantum((unsigned char) index);
400               }
401             break;
402           }
403           case 15:
404           case 16:
405           {
406             QuantumAny
407               range;
408
409             /*
410               5 bits each of red green and blue.
411             */
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);
424             break;
425           }
426           case 24:
427           case 32:
428           {
429             /*
430               8 bits each of blue green and red.
431             */
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)));
438             break;
439           }
440         }
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);
450       q++;
451     }
452     if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
453       offset+=4;
454     else
455       if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
456         offset+=2;
457       else
458         offset++;
459     if (offset >= image->rows)
460       {
461         base++;
462         offset=base;
463       }
464     if (SyncAuthenticPixels(image,exception) == MagickFalse)
465       break;
466     if (image->previous == (Image *) NULL)
467       {
468         status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
469           image->rows);
470         if (status == MagickFalse)
471           break;
472       }
473   }
474   if (EOFBlob(image) != MagickFalse)
475     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
476       image->filename);
477   (void) CloseBlob(image);
478   return(GetFirstImageInList(image));
479 }
480 \f
481 /*
482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
483 %                                                                             %
484 %                                                                             %
485 %                                                                             %
486 %   R e g i s t e r T G A I m a g e                                           %
487 %                                                                             %
488 %                                                                             %
489 %                                                                             %
490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
491 %
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.
498 %
499 %  The format of the RegisterTGAImage method is:
500 %
501 %      size_t RegisterTGAImage(void)
502 %
503 */
504 ModuleExport size_t RegisterTGAImage(void)
505 {
506   MagickInfo
507     *entry;
508
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);
538 }
539 \f
540 /*
541 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
542 %                                                                             %
543 %                                                                             %
544 %                                                                             %
545 %   U n r e g i s t e r T G A I m a g e                                       %
546 %                                                                             %
547 %                                                                             %
548 %                                                                             %
549 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550 %
551 %  UnregisterTGAImage() removes format registrations made by the
552 %  TGA module from the list of supported formats.
553 %
554 %  The format of the UnregisterTGAImage method is:
555 %
556 %      UnregisterTGAImage(void)
557 %
558 */
559 ModuleExport void UnregisterTGAImage(void)
560 {
561   (void) UnregisterMagickInfo("ICB");
562   (void) UnregisterMagickInfo("TGA");
563   (void) UnregisterMagickInfo("VDA");
564   (void) UnregisterMagickInfo("VST");
565 }
566 \f
567 /*
568 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
569 %                                                                             %
570 %                                                                             %
571 %                                                                             %
572 %   W r i t e T G A I m a g e                                                 %
573 %                                                                             %
574 %                                                                             %
575 %                                                                             %
576 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
577 %
578 %  WriteTGAImage() writes a image in the Truevision Targa rasterfile
579 %  format.
580 %
581 %  The format of the WriteTGAImage method is:
582 %
583 %      MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
584 %
585 %  A description of each parameter follows.
586 %
587 %    o image_info: the image info.
588 %
589 %    o image:  The image.
590 %
591 */
592
593 static inline size_t MagickMin(const size_t x,const size_t y)
594 {
595   if (x < y)
596     return(x);
597   return(y);
598 }
599
600 static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
601 {
602 #define TargaColormap 1
603 #define TargaRGB 2
604 #define TargaMonochrome 3
605 #define TargaRLEColormap  9
606 #define TargaRLERGB  10
607 #define TargaRLEMonochrome  11
608
609   typedef struct _TargaInfo
610   {
611     unsigned char
612       id_length,
613       colormap_type,
614       image_type;
615
616     unsigned short
617       colormap_index,
618       colormap_length;
619
620     unsigned char
621       colormap_size;
622
623     unsigned short
624       x_origin,
625       y_origin,
626       width,
627       height;
628
629     unsigned char
630       bits_per_pixel,
631       attributes;
632   } TargaInfo;
633
634   const char
635     *value;
636
637   MagickBooleanType
638     status;
639
640   register const IndexPacket
641     *indexes;
642
643   register const PixelPacket
644     *p;
645
646   register ssize_t
647     x;
648
649   register ssize_t
650     i;
651
652   register unsigned char
653     *q;
654
655   ssize_t
656     count,
657     y;
658
659   TargaInfo
660     targa_info;
661
662   unsigned char
663     *targa_pixels;
664
665   /*
666     Open output image file.
667   */
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)
676     return(status);
677   /*
678     Initialize TGA raster file header.
679   */
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;
704   else
705     if ((image->storage_class == DirectClass) || (image->colors > 256))
706       {
707         /*
708           Full color TGA raster.
709         */
710         targa_info.image_type=TargaRGB;
711         targa_info.bits_per_pixel=24;
712         if (image->matte != MagickFalse)
713           {
714             targa_info.bits_per_pixel=32;
715             targa_info.attributes=8;  /* # of alpha bits */
716           }
717       }
718     else
719       {
720         /*
721           Colormapped TGA raster.
722         */
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;
727       }
728   /*
729     Write TGA header.
730   */
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 *)
745       value);
746   if (targa_info.image_type == TargaColormap)
747     {
748       unsigned char
749         *targa_colormap;
750
751       /*
752         Dump colormap to file (blue, green, red byte order).
753       */
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");
758       q=targa_colormap;
759       for (i=0; i < (ssize_t) image->colors; i++)
760       {
761         *q++=ScaleQuantumToChar(image->colormap[i].blue);
762         *q++=ScaleQuantumToChar(image->colormap[i].green);
763         *q++=ScaleQuantumToChar(image->colormap[i].red);
764       }
765       (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length),
766         targa_colormap);
767       targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
768     }
769   /*
770     Convert MIFF to TGA raster pixels.
771   */
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--)
778   {
779     p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
780     if (p == (const PixelPacket *) NULL)
781       break;
782     q=targa_pixels;
783     indexes=GetVirtualIndexQueue(image);
784     for (x=0; x < (ssize_t) image->columns; x++)
785     {
786       if (targa_info.image_type == TargaColormap)
787         *q++=(unsigned char) GetIndexPixelComponent(indexes+x);
788       else
789         if (targa_info.image_type == TargaMonochrome)
790           *q++=(unsigned char) ScaleQuantumToChar(PixelIntensityToQuantum(p));
791         else
792           {
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]);
801           }
802       p++;
803     }
804     (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
805     if (image->previous == (Image *) NULL)
806       {
807         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
808           image->rows);
809         if (status == MagickFalse)
810           break;
811       }
812   }
813   targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
814   (void) CloseBlob(image);
815   return(MagickTrue);
816 }