]> 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-2012 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   PixelInfo
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     pixels[4],
171     runlength;
172
173   /*
174     Open image file.
175   */
176   assert(image_info != (const ImageInfo *) NULL);
177   assert(image_info->signature == MagickSignature);
178   if (image_info->debug != MagickFalse)
179     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
180       image_info->filename);
181   assert(exception != (ExceptionInfo *) NULL);
182   assert(exception->signature == MagickSignature);
183   image=AcquireImage(image_info,exception);
184   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
185   if (status == MagickFalse)
186     {
187       image=DestroyImageList(image);
188       return((Image *) NULL);
189     }
190   /*
191     Read TGA header information.
192   */
193   count=ReadBlob(image,1,&tga_info.id_length);
194   tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
195   tga_info.image_type=(unsigned char) ReadBlobByte(image);
196   if ((count != 1) ||
197       ((tga_info.image_type != TGAColormap) &&
198        (tga_info.image_type != TGARGB) &&
199        (tga_info.image_type != TGAMonochrome) &&
200        (tga_info.image_type != TGARLEColormap) &&
201        (tga_info.image_type != TGARLERGB) &&
202        (tga_info.image_type != TGARLEMonochrome)) ||
203       (((tga_info.image_type == TGAColormap) ||
204        (tga_info.image_type == TGARLEColormap)) &&
205        (tga_info.colormap_type == 0)))
206     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
207   tga_info.colormap_index=ReadBlobLSBShort(image);
208   tga_info.colormap_length=ReadBlobLSBShort(image);
209   tga_info.colormap_size=(unsigned char) ReadBlobByte(image);
210   tga_info.x_origin=ReadBlobLSBShort(image);
211   tga_info.y_origin=ReadBlobLSBShort(image);
212   tga_info.width=(unsigned short) ReadBlobLSBShort(image);
213   tga_info.height=(unsigned short) ReadBlobLSBShort(image);
214   tga_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
215   tga_info.attributes=(unsigned char) ReadBlobByte(image);
216   if (EOFBlob(image) != MagickFalse)
217     ThrowReaderException(CorruptImageError,"UnableToReadImageData");
218   if ((((tga_info.bits_per_pixel <= 1) || (tga_info.bits_per_pixel >= 17)) &&
219        (tga_info.bits_per_pixel != 24) && (tga_info.bits_per_pixel != 32)))
220     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
221   /*
222     Initialize image structure.
223   */
224   image->columns=tga_info.width;
225   image->rows=tga_info.height;
226   image->matte=(tga_info.attributes & 0x0FU) != 0 ? MagickTrue : MagickFalse;
227   if ((tga_info.image_type != TGAColormap) &&
228       (tga_info.image_type != TGARLEColormap))
229     image->depth=(size_t) ((tga_info.bits_per_pixel <= 8) ? 8 :
230       (tga_info.bits_per_pixel <= 16) ? 5 :
231       (tga_info.bits_per_pixel == 24) ? 8 :
232       (tga_info.bits_per_pixel == 32) ? 8 : 8);
233   else
234     image->depth=(size_t) ((tga_info.colormap_size <= 8) ? 8 :
235       (tga_info.colormap_size <= 16) ? 5 :
236       (tga_info.colormap_size == 24) ? 8 :
237       (tga_info.colormap_size == 32) ? 8 : 8);
238   if ((tga_info.image_type == TGAColormap) ||
239       (tga_info.image_type == TGAMonochrome) ||
240       (tga_info.image_type == TGARLEColormap) ||
241       (tga_info.image_type == TGARLEMonochrome))
242     image->storage_class=PseudoClass;
243   image->compression=NoCompression;
244   if ((tga_info.image_type == TGARLEColormap) ||
245       (tga_info.image_type == TGARLEMonochrome))
246     image->compression=RLECompression;
247   if (image->storage_class == PseudoClass)
248     {
249       if (tga_info.colormap_type != 0)
250         image->colors=tga_info.colormap_length;
251       else
252         {
253           size_t
254             one;
255
256           one=1;
257           image->colors=one << tga_info.bits_per_pixel;
258           if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
259             ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
260         }
261     }
262   if (tga_info.id_length != 0)
263     {
264       char
265         *comment;
266
267       size_t
268         length;
269
270       /*
271         TGA image comment.
272       */
273       length=(size_t) tga_info.id_length;
274       comment=(char *) NULL;
275       if (~length >= (MaxTextExtent-1))
276         comment=(char *) AcquireQuantumMemory(length+MaxTextExtent,
277           sizeof(*comment));
278       if (comment == (char *) NULL)
279         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
280       count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
281       comment[tga_info.id_length]='\0';
282       (void) SetImageProperty(image,"comment",comment,exception);
283       comment=DestroyString(comment);
284     }
285   (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
286   pixel.alpha=(Quantum) OpaqueAlpha;
287   if (tga_info.colormap_type != 0)
288     {
289       /*
290         Read TGA raster colormap.
291       */
292       if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
293         ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
294       for (i=0; i < (ssize_t) image->colors; i++)
295       {
296         switch (tga_info.colormap_size)
297         {
298           case 8:
299           default:
300           {
301             /*
302               Gray scale.
303             */
304             pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
305             pixel.green=pixel.red;
306             pixel.blue=pixel.red;
307             break;
308           }
309           case 15:
310           case 16:
311           {
312             QuantumAny
313               range;
314
315             /*
316               5 bits each of red green and blue.
317             */
318             j=(unsigned char) ReadBlobByte(image);
319             k=(unsigned char) ReadBlobByte(image);
320             range=GetQuantumRange(5UL);
321             pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
322             pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
323               (1UL*(j & 0xe0) >> 5),range);
324             pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
325             break;
326           }
327           case 24:
328           case 32:
329           {
330             /*
331               8 bits each of blue, green and red.
332             */
333             pixel.blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
334             pixel.green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
335             pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
336             break;
337           }
338         }
339         image->colormap[i]=pixel;
340       }
341     }
342   /*
343     Convert TGA pixels to pixel packets.
344   */
345   base=0;
346   flag=0;
347   skip=MagickFalse;
348   real=0;
349   index=0;
350   runlength=0;
351   offset=0;
352   for (y=0; y < (ssize_t) image->rows; y++)
353   {
354     real=offset;
355     if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
356       real=image->rows-real-1;
357     q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
358     if (q == (Quantum *) NULL)
359       break;
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=(Quantum) ReadBlobByte(image);
392             if (tga_info.colormap_type != 0)
393               pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
394                 1UL*index,exception)];
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 RGB.
411             */
412             if (ReadBlob(image,2,pixels) != 2)
413               ThrowReaderException(CorruptImageError,"UnableToReadImageData");
414             j=pixels[0];
415             k=pixels[1];
416             range=GetQuantumRange(5UL);
417             pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
418             pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
419               (1UL*(j & 0xe0) >> 5),range);
420             pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
421             if (image->matte != MagickFalse)
422               pixel.alpha=(k & 0x80) == 0 ? (Quantum) OpaqueAlpha : (Quantum)
423                 TransparentAlpha; 
424             if (image->storage_class == PseudoClass)
425               index=ConstrainColormapIndex(image,((size_t) k << 8)+j,exception);
426             break;
427           }
428           case 24:
429           {
430             /*
431               BGR pixels.
432             */
433             if (ReadBlob(image,3,pixels) != 3)
434               ThrowReaderException(CorruptImageError,"UnableToReadImageData");
435             pixel.blue=ScaleCharToQuantum(pixels[0]);
436             pixel.green=ScaleCharToQuantum(pixels[1]);
437             pixel.red=ScaleCharToQuantum(pixels[2]);
438             break;
439           }
440           case 32:
441           {
442             /*
443               BGRA pixels.
444             */
445             if (ReadBlob(image,4,pixels) != 4)
446               ThrowReaderException(CorruptImageError,"UnableToReadImageData");
447             pixel.blue=ScaleCharToQuantum(pixels[0]);
448             pixel.green=ScaleCharToQuantum(pixels[1]);
449             pixel.red=ScaleCharToQuantum(pixels[2]);
450             pixel.alpha=ScaleCharToQuantum(pixels[3]);
451             break;
452           }
453         }
454       if (status == MagickFalse)
455         ThrowReaderException(CorruptImageError,"UnableToReadImageData");
456       if (image->storage_class == PseudoClass)
457         SetPixelIndex(image,index,q);
458       SetPixelRed(image,pixel.red,q);
459       SetPixelGreen(image,pixel.green,q);
460       SetPixelBlue(image,pixel.blue,q);
461       if (image->matte != MagickFalse)
462         SetPixelAlpha(image,pixel.alpha,q);
463       q+=GetPixelChannels(image);
464     }
465     if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
466       offset+=4;
467     else
468       if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
469         offset+=2;
470       else
471         offset++;
472     if (offset >= image->rows)
473       {
474         base++;
475         offset=base;
476       }
477     if (SyncAuthenticPixels(image,exception) == MagickFalse)
478       break;
479     if (image->previous == (Image *) NULL)
480       {
481         status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
482           image->rows);
483         if (status == MagickFalse)
484           break;
485       }
486   }
487   if (EOFBlob(image) != MagickFalse)
488     ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
489       image->filename);
490   (void) CloseBlob(image);
491   return(GetFirstImageInList(image));
492 }
493 \f
494 /*
495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496 %                                                                             %
497 %                                                                             %
498 %                                                                             %
499 %   R e g i s t e r T G A I m a g e                                           %
500 %                                                                             %
501 %                                                                             %
502 %                                                                             %
503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504 %
505 %  RegisterTGAImage() adds properties for the TGA image format to
506 %  the list of supported formats.  The properties include the image format
507 %  tag, a method to read and/or write the format, whether the format
508 %  supports the saving of more than one frame to the same file or blob,
509 %  whether the format supports native in-memory I/O, and a brief
510 %  description of the format.
511 %
512 %  The format of the RegisterTGAImage method is:
513 %
514 %      size_t RegisterTGAImage(void)
515 %
516 */
517 ModuleExport size_t RegisterTGAImage(void)
518 {
519   MagickInfo
520     *entry;
521
522   entry=SetMagickInfo("ICB");
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("TGA");
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   entry=SetMagickInfo("VDA");
537   entry->decoder=(DecodeImageHandler *) ReadTGAImage;
538   entry->encoder=(EncodeImageHandler *) WriteTGAImage;
539   entry->adjoin=MagickFalse;
540   entry->description=ConstantString("Truevision Targa image");
541   entry->module=ConstantString("TGA");
542   (void) RegisterMagickInfo(entry);
543   entry=SetMagickInfo("VST");
544   entry->decoder=(DecodeImageHandler *) ReadTGAImage;
545   entry->encoder=(EncodeImageHandler *) WriteTGAImage;
546   entry->adjoin=MagickFalse;
547   entry->description=ConstantString("Truevision Targa image");
548   entry->module=ConstantString("TGA");
549   (void) RegisterMagickInfo(entry);
550   return(MagickImageCoderSignature);
551 }
552 \f
553 /*
554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555 %                                                                             %
556 %                                                                             %
557 %                                                                             %
558 %   U n r e g i s t e r T G A I m a g e                                       %
559 %                                                                             %
560 %                                                                             %
561 %                                                                             %
562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563 %
564 %  UnregisterTGAImage() removes format registrations made by the
565 %  TGA module from the list of supported formats.
566 %
567 %  The format of the UnregisterTGAImage method is:
568 %
569 %      UnregisterTGAImage(void)
570 %
571 */
572 ModuleExport void UnregisterTGAImage(void)
573 {
574   (void) UnregisterMagickInfo("ICB");
575   (void) UnregisterMagickInfo("TGA");
576   (void) UnregisterMagickInfo("VDA");
577   (void) UnregisterMagickInfo("VST");
578 }
579 \f
580 /*
581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582 %                                                                             %
583 %                                                                             %
584 %                                                                             %
585 %   W r i t e T G A I m a g e                                                 %
586 %                                                                             %
587 %                                                                             %
588 %                                                                             %
589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
590 %
591 %  WriteTGAImage() writes a image in the Truevision Targa rasterfile
592 %  format.
593 %
594 %  The format of the WriteTGAImage method is:
595 %
596 %      MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
597 %        Image *image,ExceptionInfo *exception)
598 %
599 %  A description of each parameter follows.
600 %
601 %    o image_info: the image info.
602 %
603 %    o image:  The image.
604 %
605 */
606
607 static inline size_t MagickMin(const size_t x,const size_t y)
608 {
609   if (x < y)
610     return(x);
611   return(y);
612 }
613
614 static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
615   ExceptionInfo *exception)
616 {
617 #define TargaColormap 1
618 #define TargaRGB 2
619 #define TargaMonochrome 3
620 #define TargaRLEColormap  9
621 #define TargaRLERGB  10
622 #define TargaRLEMonochrome  11
623
624   typedef struct _TargaInfo
625   {
626     unsigned char
627       id_length,
628       colormap_type,
629       image_type;
630
631     unsigned short
632       colormap_index,
633       colormap_length;
634
635     unsigned char
636       colormap_size;
637
638     unsigned short
639       x_origin,
640       y_origin,
641       width,
642       height;
643
644     unsigned char
645       bits_per_pixel,
646       attributes;
647   } TargaInfo;
648
649   const char
650     *value;
651
652   MagickBooleanType
653     status;
654
655   register const Quantum
656     *p;
657
658   register ssize_t
659     x;
660
661   register ssize_t
662     i;
663
664   register unsigned char
665     *q;
666
667   ssize_t
668     count,
669     y;
670
671   TargaInfo
672     targa_info;
673
674   unsigned char
675     *targa_pixels;
676
677   /*
678     Open output image file.
679   */
680   assert(image_info != (const ImageInfo *) NULL);
681   assert(image_info->signature == MagickSignature);
682   assert(image != (Image *) NULL);
683   assert(image->signature == MagickSignature);
684   if (image->debug != MagickFalse)
685     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
686   assert(exception != (ExceptionInfo *) NULL);
687   assert(exception->signature == MagickSignature);
688   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
689   if (status == MagickFalse)
690     return(status);
691   /*
692     Initialize TGA raster file header.
693   */
694   if ((image->columns > 65535L) || (image->rows > 65535L))
695     ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
696   if (IsRGBColorspace(image->colorspace) == MagickFalse)
697     (void) TransformImageColorspace(image,RGBColorspace,exception);
698   targa_info.id_length=0;
699   value=GetImageProperty(image,"comment",exception);
700   if (value != (const char *) NULL)
701     targa_info.id_length=(unsigned char) MagickMin(strlen(value),255);
702   targa_info.colormap_type=0;
703   targa_info.colormap_index=0;
704   targa_info.colormap_length=0;
705   targa_info.colormap_size=0;
706   targa_info.x_origin=0;
707   targa_info.y_origin=0;
708   targa_info.width=(unsigned short) image->columns;
709   targa_info.height=(unsigned short) image->rows;
710   targa_info.bits_per_pixel=8;
711   targa_info.attributes=0;
712   if ((image_info->type != TrueColorType) &&
713       (image_info->type != TrueColorMatteType) &&
714       (image_info->type != PaletteType) &&
715       (image->matte == MagickFalse) &&
716       (IsImageGray(image,exception) != MagickFalse))
717     targa_info.image_type=TargaMonochrome;
718   else
719     if ((image->storage_class == DirectClass) || (image->colors > 256))
720       {
721         /*
722           Full color TGA raster.
723         */
724         targa_info.image_type=TargaRGB;
725         targa_info.bits_per_pixel=24;
726         if (image->matte != MagickFalse)
727           {
728             targa_info.bits_per_pixel=32;
729             targa_info.attributes=8;  /* # of alpha bits */
730           }
731       }
732     else
733       {
734         /*
735           Colormapped TGA raster.
736         */
737         targa_info.image_type=TargaColormap;
738         targa_info.colormap_type=1;
739         targa_info.colormap_length=(unsigned short) image->colors;
740         targa_info.colormap_size=24;
741       }
742   /*
743     Write TGA header.
744   */
745   (void) WriteBlobByte(image,targa_info.id_length);
746   (void) WriteBlobByte(image,targa_info.colormap_type);
747   (void) WriteBlobByte(image,targa_info.image_type);
748   (void) WriteBlobLSBShort(image,targa_info.colormap_index);
749   (void) WriteBlobLSBShort(image,targa_info.colormap_length);
750   (void) WriteBlobByte(image,targa_info.colormap_size);
751   (void) WriteBlobLSBShort(image,targa_info.x_origin);
752   (void) WriteBlobLSBShort(image,targa_info.y_origin);
753   (void) WriteBlobLSBShort(image,targa_info.width);
754   (void) WriteBlobLSBShort(image,targa_info.height);
755   (void) WriteBlobByte(image,targa_info.bits_per_pixel);
756   (void) WriteBlobByte(image,targa_info.attributes);
757   if (targa_info.id_length != 0)
758     (void) WriteBlob(image,targa_info.id_length,(unsigned char *)
759       value);
760   if (targa_info.image_type == TargaColormap)
761     {
762       unsigned char
763         *targa_colormap;
764
765       /*
766         Dump colormap to file (blue, green, red byte order).
767       */
768       targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
769         targa_info.colormap_length,3UL*sizeof(*targa_colormap));
770       if (targa_colormap == (unsigned char *) NULL)
771         ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
772       q=targa_colormap;
773       for (i=0; i < (ssize_t) image->colors; i++)
774       {
775         *q++=ScaleQuantumToChar(image->colormap[i].blue);
776         *q++=ScaleQuantumToChar(image->colormap[i].green);
777         *q++=ScaleQuantumToChar(image->colormap[i].red);
778       }
779       (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length),
780         targa_colormap);
781       targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
782     }
783   /*
784     Convert MIFF to TGA raster pixels.
785   */
786   count=(ssize_t) (targa_info.bits_per_pixel*targa_info.width)/8;
787   targa_pixels=(unsigned char *) AcquireQuantumMemory((size_t) count,
788     sizeof(*targa_pixels));
789   if (targa_pixels == (unsigned char *) NULL)
790     ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
791   for (y=(ssize_t) (image->rows-1); y >= 0; y--)
792   {
793     p=GetVirtualPixels(image,0,y,image->columns,1,exception);
794     if (p == (const Quantum *) NULL)
795       break;
796     q=targa_pixels;
797     for (x=0; x < (ssize_t) image->columns; x++)
798     {
799       if (targa_info.image_type == TargaColormap)
800         *q++=(unsigned char) GetPixelIndex(image,p);
801       else
802         if (targa_info.image_type == TargaMonochrome)
803           *q++=(unsigned char) ScaleQuantumToChar(GetPixelIntensity(image,p));
804         else
805           {
806             *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
807             *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
808             *q++=ScaleQuantumToChar(GetPixelRed(image,p));
809             if (image->matte != MagickFalse)
810               *q++=(unsigned char) ScaleQuantumToChar(GetPixelAlpha(image,p));
811           }
812       p+=GetPixelChannels(image);
813     }
814     (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
815     if (image->previous == (Image *) NULL)
816       {
817         status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
818           image->rows);
819         if (status == MagickFalse)
820           break;
821       }
822   }
823   targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
824   (void) CloseBlob(image);
825   return(MagickTrue);
826 }