]> granicus.if.org Git - imagemagick/blob - coders/tga.c
c737794a490a6e3709a6f70bad74d4501ad37032
[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 "MagickCore/studio.h"
43 #include "MagickCore/attribute.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/cache.h"
47 #include "MagickCore/color-private.h"
48 #include "MagickCore/colormap.h"
49 #include "MagickCore/colormap-private.h"
50 #include "MagickCore/colorspace.h"
51 #include "MagickCore/colorspace-private.h"
52 #include "MagickCore/exception.h"
53 #include "MagickCore/exception-private.h"
54 #include "MagickCore/image.h"
55 #include "MagickCore/image-private.h"
56 #include "MagickCore/list.h"
57 #include "MagickCore/magick.h"
58 #include "MagickCore/memory_.h"
59 #include "MagickCore/monitor.h"
60 #include "MagickCore/monitor-private.h"
61 #include "MagickCore/pixel-accessor.h"
62 #include "MagickCore/property.h"
63 #include "MagickCore/quantum-private.h"
64 #include "MagickCore/static.h"
65 #include "MagickCore/string_.h"
66 #include "MagickCore/module.h"
67 \f
68 /*
69   Forward declarations.
70 */
71 static MagickBooleanType
72   WriteTGAImage(const ImageInfo *,Image *,ExceptionInfo *);
73 \f
74 /*
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %                                                                             %
77 %                                                                             %
78 %                                                                             %
79 %   R e a d T G A I m a g e                                                   %
80 %                                                                             %
81 %                                                                             %
82 %                                                                             %
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %
85 %  ReadTGAImage() reads a Truevision TGA image file and returns it.
86 %  It allocates the memory necessary for the new Image structure and returns
87 %  a pointer to the new image.
88 %
89 %  The format of the ReadTGAImage method is:
90 %
91 %      Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
92 %
93 %  A description of each parameter follows:
94 %
95 %    o image_info: the image info.
96 %
97 %    o exception: return any errors or warnings in this structure.
98 %
99 */
100 static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
101 {
102 #define TGAColormap 1
103 #define TGARGB 2
104 #define TGAMonochrome 3
105 #define TGARLEColormap  9
106 #define TGARLERGB  10
107 #define TGARLEMonochrome  11
108
109   typedef struct _TGAInfo
110   {
111     unsigned char
112       id_length,
113       colormap_type,
114       image_type;
115
116     unsigned short
117       colormap_index,
118       colormap_length;
119
120     unsigned char
121       colormap_size;
122
123     unsigned short
124       x_origin,
125       y_origin,
126       width,
127       height;
128
129     unsigned char
130       bits_per_pixel,
131       attributes;
132   } TGAInfo;
133
134   Image
135     *image;
136
137   MagickBooleanType
138     status;
139
140   PixelPacket
141     pixel;
142
143   Quantum
144     index;
145
146   register Quantum
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,exception) == 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-1))
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.alpha=(Quantum) OpaqueAlpha;
286   if (tga_info.colormap_type != 0)
287     {
288       /*
289         Read TGA raster colormap.
290       */
291       if (AcquireImageColormap(image,image->colors,exception) == 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=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 == (Quantum *) NULL)
358       break;
359     for (x=0; x < (ssize_t) image->columns; x++)
360     {
361       if ((tga_info.image_type == TGARLEColormap) ||
362           (tga_info.image_type == TGARLERGB) ||
363           (tga_info.image_type == TGARLEMonochrome))
364         {
365           if (runlength != 0)
366             {
367               runlength--;
368               skip=flag != 0;
369             }
370           else
371             {
372               count=ReadBlob(image,1,&runlength);
373               if (count == 0)
374                 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
375               flag=runlength & 0x80;
376               if (flag != 0)
377                 runlength-=128;
378               skip=MagickFalse;
379             }
380         }
381       if (skip == MagickFalse)
382         switch (tga_info.bits_per_pixel)
383         {
384           case 8:
385           default:
386           {
387             /*
388               Gray scale.
389             */
390             index=(Quantum) ReadBlobByte(image);
391             if (tga_info.colormap_type != 0)
392               pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
393                 1UL*index)];
394             else
395               {
396                 pixel.red=ScaleCharToQuantum((unsigned char) index);
397                 pixel.green=ScaleCharToQuantum((unsigned char) index);
398                 pixel.blue=ScaleCharToQuantum((unsigned char) index);
399               }
400             break;
401           }
402           case 15:
403           case 16:
404           {
405             QuantumAny
406               range;
407
408             /*
409               5 bits each of red green and blue.
410             */
411             j=(unsigned char) ReadBlobByte(image);
412             k=(unsigned char) ReadBlobByte(image);
413             range=GetQuantumRange(5UL);
414             pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
415             pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
416               (1UL*(j & 0xe0) >> 5),range);
417             pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
418             if (image->matte != MagickFalse)
419               pixel.alpha=(k & 0x80) == 0 ? (Quantum) OpaqueAlpha :
420                 (Quantum) TransparentAlpha; 
421             if (image->storage_class == PseudoClass)
422               index=ConstrainColormapIndex(image,((size_t) k << 8)+j);
423             break;
424           }
425           case 24:
426           case 32:
427           {
428             /*
429               8 bits each of blue green and red.
430             */
431             pixel.blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
432             pixel.green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
433             pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
434             if (tga_info.bits_per_pixel == 32)
435               pixel.alpha=ScaleCharToQuantum((unsigned char)
436                 ReadBlobByte(image));
437             break;
438           }
439         }
440       if (status == MagickFalse)
441         ThrowReaderException(CorruptImageError,"UnableToReadImageData");
442       if (image->storage_class == PseudoClass)
443         SetPixelIndex(image,index,q);
444       SetPixelRed(image,pixel.red,q);
445       SetPixelGreen(image,pixel.green,q);
446       SetPixelBlue(image,pixel.blue,q);
447       if (image->matte != MagickFalse)
448         SetPixelAlpha(image,pixel.alpha,q);
449       q+=GetPixelChannels(image);
450     }
451     if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
452       offset+=4;
453     else
454       if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
455         offset+=2;
456       else
457         offset++;
458     if (offset >= image->rows)
459       {
460         base++;
461         offset=base;
462       }
463     if (SyncAuthenticPixels(image,exception) == MagickFalse)
464       break;
465     if (image->previous == (Image *) NULL)
466       {
467         status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
468           image->rows);
469         if (status == MagickFalse)
470           break;
471       }
472   }
473   if (EOFBlob(image) != MagickFalse)
474     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
475       image->filename);
476   (void) CloseBlob(image);
477   return(GetFirstImageInList(image));
478 }
479 \f
480 /*
481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
482 %                                                                             %
483 %                                                                             %
484 %                                                                             %
485 %   R e g i s t e r T G A I m a g e                                           %
486 %                                                                             %
487 %                                                                             %
488 %                                                                             %
489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490 %
491 %  RegisterTGAImage() adds properties for the TGA image format to
492 %  the list of supported formats.  The properties include the image format
493 %  tag, a method to read and/or write the format, whether the format
494 %  supports the saving of more than one frame to the same file or blob,
495 %  whether the format supports native in-memory I/O, and a brief
496 %  description of the format.
497 %
498 %  The format of the RegisterTGAImage method is:
499 %
500 %      size_t RegisterTGAImage(void)
501 %
502 */
503 ModuleExport size_t RegisterTGAImage(void)
504 {
505   MagickInfo
506     *entry;
507
508   entry=SetMagickInfo("ICB");
509   entry->decoder=(DecodeImageHandler *) ReadTGAImage;
510   entry->encoder=(EncodeImageHandler *) WriteTGAImage;
511   entry->adjoin=MagickFalse;
512   entry->description=ConstantString("Truevision Targa image");
513   entry->module=ConstantString("TGA");
514   (void) RegisterMagickInfo(entry);
515   entry=SetMagickInfo("TGA");
516   entry->decoder=(DecodeImageHandler *) ReadTGAImage;
517   entry->encoder=(EncodeImageHandler *) WriteTGAImage;
518   entry->adjoin=MagickFalse;
519   entry->description=ConstantString("Truevision Targa image");
520   entry->module=ConstantString("TGA");
521   (void) RegisterMagickInfo(entry);
522   entry=SetMagickInfo("VDA");
523   entry->decoder=(DecodeImageHandler *) ReadTGAImage;
524   entry->encoder=(EncodeImageHandler *) WriteTGAImage;
525   entry->adjoin=MagickFalse;
526   entry->description=ConstantString("Truevision Targa image");
527   entry->module=ConstantString("TGA");
528   (void) RegisterMagickInfo(entry);
529   entry=SetMagickInfo("VST");
530   entry->decoder=(DecodeImageHandler *) ReadTGAImage;
531   entry->encoder=(EncodeImageHandler *) WriteTGAImage;
532   entry->adjoin=MagickFalse;
533   entry->description=ConstantString("Truevision Targa image");
534   entry->module=ConstantString("TGA");
535   (void) RegisterMagickInfo(entry);
536   return(MagickImageCoderSignature);
537 }
538 \f
539 /*
540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
541 %                                                                             %
542 %                                                                             %
543 %                                                                             %
544 %   U n r e g i s t e r T G A I m a g e                                       %
545 %                                                                             %
546 %                                                                             %
547 %                                                                             %
548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549 %
550 %  UnregisterTGAImage() removes format registrations made by the
551 %  TGA module from the list of supported formats.
552 %
553 %  The format of the UnregisterTGAImage method is:
554 %
555 %      UnregisterTGAImage(void)
556 %
557 */
558 ModuleExport void UnregisterTGAImage(void)
559 {
560   (void) UnregisterMagickInfo("ICB");
561   (void) UnregisterMagickInfo("TGA");
562   (void) UnregisterMagickInfo("VDA");
563   (void) UnregisterMagickInfo("VST");
564 }
565 \f
566 /*
567 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568 %                                                                             %
569 %                                                                             %
570 %                                                                             %
571 %   W r i t e T G A I m a g e                                                 %
572 %                                                                             %
573 %                                                                             %
574 %                                                                             %
575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576 %
577 %  WriteTGAImage() writes a image in the Truevision Targa rasterfile
578 %  format.
579 %
580 %  The format of the WriteTGAImage method is:
581 %
582 %      MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
583 %        Image *image,ExceptionInfo *exception)
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   ExceptionInfo *exception)
602 {
603 #define TargaColormap 1
604 #define TargaRGB 2
605 #define TargaMonochrome 3
606 #define TargaRLEColormap  9
607 #define TargaRLERGB  10
608 #define TargaRLEMonochrome  11
609
610   typedef struct _TargaInfo
611   {
612     unsigned char
613       id_length,
614       colormap_type,
615       image_type;
616
617     unsigned short
618       colormap_index,
619       colormap_length;
620
621     unsigned char
622       colormap_size;
623
624     unsigned short
625       x_origin,
626       y_origin,
627       width,
628       height;
629
630     unsigned char
631       bits_per_pixel,
632       attributes;
633   } TargaInfo;
634
635   const char
636     *value;
637
638   MagickBooleanType
639     status;
640
641   register const Quantum
642     *p;
643
644   register ssize_t
645     x;
646
647   register ssize_t
648     i;
649
650   register unsigned char
651     *q;
652
653   ssize_t
654     count,
655     y;
656
657   TargaInfo
658     targa_info;
659
660   unsigned char
661     *targa_pixels;
662
663   /*
664     Open output image file.
665   */
666   assert(image_info != (const ImageInfo *) NULL);
667   assert(image_info->signature == MagickSignature);
668   assert(image != (Image *) NULL);
669   assert(image->signature == MagickSignature);
670   if (image->debug != MagickFalse)
671     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
672   assert(exception != (ExceptionInfo *) NULL);
673   assert(exception->signature == MagickSignature);
674   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
675   if (status == MagickFalse)
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 (IsRGBColorspace(image->colorspace) == MagickFalse)
683     (void) TransformImageColorspace(image,RGBColorspace);
684   targa_info.id_length=0;
685   value=GetImageProperty(image,"comment");
686   if (value != (const char *) NULL)
687     targa_info.id_length=(unsigned char) MagickMin(strlen(value),255);
688   targa_info.colormap_type=0;
689   targa_info.colormap_index=0;
690   targa_info.colormap_length=0;
691   targa_info.colormap_size=0;
692   targa_info.x_origin=0;
693   targa_info.y_origin=0;
694   targa_info.width=(unsigned short) image->columns;
695   targa_info.height=(unsigned short) image->rows;
696   targa_info.bits_per_pixel=8;
697   targa_info.attributes=0;
698   if ((image_info->type != TrueColorType) &&
699       (image_info->type != TrueColorMatteType) &&
700       (image_info->type != PaletteType) &&
701       (image->matte == MagickFalse) &&
702       (IsImageGray(image,exception) != MagickFalse))
703     targa_info.image_type=TargaMonochrome;
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,exception);
780     if (p == (const Quantum *) NULL)
781       break;
782     q=targa_pixels;
783     for (x=0; x < (ssize_t) image->columns; x++)
784     {
785       if (targa_info.image_type == TargaColormap)
786         *q++=(unsigned char) GetPixelIndex(image,p);
787       else
788         if (targa_info.image_type == TargaMonochrome)
789           *q++=(unsigned char) ScaleQuantumToChar(GetPixelIntensity(image,p));
790         else
791           {
792             *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
793             *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
794             *q++=ScaleQuantumToChar(GetPixelRed(image,p));
795             if (image->matte != MagickFalse)
796               *q++=(unsigned char) ScaleQuantumToChar(GetPixelAlpha(image,p));
797           }
798       p+=GetPixelChannels(image);
799     }
800     (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
801     if (image->previous == (Image *) NULL)
802       {
803         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
804           image->rows);
805         if (status == MagickFalse)
806           break;
807       }
808   }
809   targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
810   (void) CloseBlob(image);
811   return(MagickTrue);
812 }