blob: 9eefab9699bf09fcd0f551834dbf994c729f63dd [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
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% %
cristy16af1cb2009-12-11 21:38:29 +000020% Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% 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
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"
cristye7e40552010-04-24 21:34:22 +000047#include "magick/colormap.h"
cristy316d5172009-09-17 19:31:25 +000048#include "magick/colormap-private.h"
cristy3ed852e2009-09-05 21:47:34 +000049#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
65/*
66 Forward declarations.
67*/
68static MagickBooleanType
69 WriteTGAImage(const ImageInfo *,Image *);
70
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*/
97static 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 long
138 y;
139
140 MagickBooleanType
141 status;
142
143 PixelPacket
144 pixel;
145
146 register IndexPacket
147 *indexes;
148
149 register long
150 i,
151 x;
152
153 register PixelPacket
154 *q;
155
156 ssize_t
157 count;
158
159 TGAInfo
160 tga_info;
161
162 unsigned char
163 j,
164 k,
165 runlength;
166
167 unsigned long
168 base,
169 flag,
170 offset,
171 real,
172 skip;
173
174 /*
175 Open image file.
176 */
177 assert(image_info != (const ImageInfo *) NULL);
178 assert(image_info->signature == MagickSignature);
179 if (image_info->debug != MagickFalse)
180 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
181 image_info->filename);
182 assert(exception != (ExceptionInfo *) NULL);
183 assert(exception->signature == MagickSignature);
184 image=AcquireImage(image_info);
185 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
186 if (status == MagickFalse)
187 {
188 image=DestroyImageList(image);
189 return((Image *) NULL);
190 }
191 /*
192 Read TGA header information.
193 */
194 count=ReadBlob(image,1,&tga_info.id_length);
195 tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
196 tga_info.image_type=(unsigned char) ReadBlobByte(image);
197 do
198 {
199 if ((count != 1) ||
200 ((tga_info.image_type != TGAColormap) &&
201 (tga_info.image_type != TGARGB) &&
202 (tga_info.image_type != TGAMonochrome) &&
203 (tga_info.image_type != TGARLEColormap) &&
204 (tga_info.image_type != TGARLERGB) &&
205 (tga_info.image_type != TGARLEMonochrome)) ||
206 (((tga_info.image_type == TGAColormap) ||
207 (tga_info.image_type == TGARLEColormap)) &&
208 (tga_info.colormap_type == 0)))
209 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
210 tga_info.colormap_index=ReadBlobLSBShort(image);
211 tga_info.colormap_length=ReadBlobLSBShort(image);
212 tga_info.colormap_size=(unsigned char) ReadBlobByte(image);
213 tga_info.x_origin=ReadBlobLSBShort(image);
214 tga_info.y_origin=ReadBlobLSBShort(image);
215 tga_info.width=(unsigned short) ReadBlobLSBShort(image);
216 tga_info.height=(unsigned short) ReadBlobLSBShort(image);
217 tga_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
218 tga_info.attributes=(unsigned char) ReadBlobByte(image);
219 if (EOFBlob(image) != MagickFalse)
220 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
221 /*
222 Initialize image structure.
223 */
224 image->columns=tga_info.width;
225 image->rows=tga_info.height;
226 image->matte=tga_info.bits_per_pixel == 32 ? MagickTrue : MagickFalse;
227 if ((tga_info.image_type != TGAColormap) &&
228 (tga_info.image_type != TGARLEColormap))
229 image->depth=(unsigned long) ((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=(unsigned long) ((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 image->colors=0x01U << tga_info.bits_per_pixel;
254 if (AcquireImageColormap(image,image->colors) == MagickFalse)
255 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
256 }
257 }
258 if (tga_info.id_length != 0)
259 {
260 char
261 *comment;
262
263 size_t
264 length;
265
266 /*
267 TGA image comment.
268 */
269 length=(size_t) tga_info.id_length;
270 comment=(char *) NULL;
271 if (~length >= MaxTextExtent)
272 comment=(char *) AcquireQuantumMemory(length+MaxTextExtent,
273 sizeof(*comment));
274 if (comment == (char *) NULL)
275 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
276 count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
277 comment[tga_info.id_length]='\0';
278 (void) SetImageProperty(image,"comment",comment);
279 comment=DestroyString(comment);
280 }
281 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
282 pixel.opacity=(Quantum) OpaqueOpacity;
283 if (tga_info.colormap_type != 0)
284 {
285 /*
286 Read TGA raster colormap.
287 */
288 if (AcquireImageColormap(image,image->colors) == MagickFalse)
289 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
290 for (i=0; i < (long) image->colors; i++)
291 {
292 switch (tga_info.colormap_size)
293 {
294 case 8:
295 default:
296 {
297 /*
298 Gray scale.
299 */
300 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
301 pixel.green=pixel.red;
302 pixel.blue=pixel.red;
303 break;
304 }
305 case 15:
306 case 16:
307 {
308 QuantumAny
309 range;
310
311 /*
312 5 bits each of red green and blue.
313 */
314 j=(unsigned char) ReadBlobByte(image);
315 k=(unsigned char) ReadBlobByte(image);
316 range=GetQuantumRange(5UL);
317 pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
318 pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
319 (1UL*(j & 0xe0) >> 5),range);
320 pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
321 break;
322 }
323 case 24:
324 case 32:
325 {
326 /*
327 8 bits each of blue, green and red.
328 */
329 pixel.blue=ScaleCharToQuantum((unsigned char)
330 ReadBlobByte(image));
331 pixel.green=ScaleCharToQuantum((unsigned char)
332 ReadBlobByte(image));
333 pixel.red=ScaleCharToQuantum((unsigned char)
334 ReadBlobByte(image));
335 break;
336 }
337 }
338 image->colormap[i]=pixel;
339 }
340 }
341 if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
342 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
343 break;
344 /*
345 Convert TGA pixels to pixel packets.
346 */
347 base=0;
348 flag=0;
349 skip=MagickFalse;
350 real=0;
351 index=(IndexPacket) 0;
352 runlength=0;
353 offset=0;
354 for (y=0; y < (long) image->rows; y++)
355 {
356 real=offset;
357 if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
358 real=image->rows-real-1;
359 q=QueueAuthenticPixels(image,0,(long) real,image->columns,1,exception);
360 if (q == (PixelPacket *) NULL)
361 break;
362 indexes=GetAuthenticIndexQueue(image);
363 for (x=0; x < (long) image->columns; x++)
364 {
365 if ((tga_info.image_type == TGARLEColormap) ||
366 (tga_info.image_type == TGARLERGB) ||
367 (tga_info.image_type == TGARLEMonochrome))
368 {
369 if (runlength != 0)
370 {
371 runlength--;
372 skip=flag != 0;
373 }
374 else
375 {
376 count=ReadBlob(image,1,&runlength);
377 if (count == 0)
378 ThrowReaderException(CorruptImageError,
379 "UnableToReadImageData");
380 flag=runlength & 0x80;
381 if (flag != 0)
382 runlength-=128;
383 skip=MagickFalse;
384 }
385 }
386 if (skip == MagickFalse)
387 switch (tga_info.bits_per_pixel)
388 {
389 case 8:
390 default:
391 {
392 /*
393 Gray scale.
394 */
395 index=(IndexPacket) ReadBlobByte(image);
396 if (tga_info.colormap_type != 0)
397 pixel=image->colormap[(long) ConstrainColormapIndex(image,
398 1UL*index)];
399 else
400 {
401 pixel.red=ScaleCharToQuantum((unsigned char) index);
402 pixel.green=ScaleCharToQuantum((unsigned char) index);
403 pixel.blue=ScaleCharToQuantum((unsigned char) index);
404 }
405 break;
406 }
407 case 15:
408 case 16:
409 {
410 QuantumAny
411 range;
412
413 /*
414 5 bits each of red green and blue.
415 */
416 j=(unsigned char) ReadBlobByte(image);
417 k=(unsigned char) ReadBlobByte(image);
418 range=GetQuantumRange(5UL);
419 pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
420 pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
421 (1UL*(j & 0xe0) >> 5),range);
422 pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
423 if (image->matte != MagickFalse)
424 pixel.opacity=(k & 0x80) != 0 ? (Quantum) OpaqueOpacity :
425 (Quantum) TransparentOpacity;
426 if (image->storage_class == PseudoClass)
427 index=ConstrainColormapIndex(image,((unsigned long) k << 8)+j);
428 break;
429 }
430 case 24:
431 case 32:
432 {
433 /*
434 8 bits each of blue green and red.
435 */
436 pixel.blue=ScaleCharToQuantum((unsigned char)
437 ReadBlobByte(image));
438 pixel.green=ScaleCharToQuantum((unsigned char)
439 ReadBlobByte(image));
440 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
441 if (tga_info.bits_per_pixel == 32)
442 pixel.opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
443 (unsigned char) ReadBlobByte(image)));
444 break;
445 }
446 }
447 if (status == MagickFalse)
448 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
449 if (image->storage_class == PseudoClass)
450 indexes[x]=index;
451 q->red=pixel.red;
452 q->green=pixel.green;
453 q->blue=pixel.blue;
454 if (image->matte != MagickFalse)
455 q->opacity=pixel.opacity;
456 q++;
457 }
458 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
459 offset+=4;
460 else
461 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
462 offset+=2;
463 else
464 offset++;
465 if (offset >= image->rows)
466 {
467 base++;
468 offset=base;
469 }
470 if (SyncAuthenticPixels(image,exception) == MagickFalse)
471 break;
472 if (image->previous == (Image *) NULL)
473 {
474 status=SetImageProgress(image,LoadImageTag,y,image->rows);
475 if (status == MagickFalse)
476 break;
477 }
478 }
479 if (EOFBlob(image) != MagickFalse)
480 {
481 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
482 image->filename);
483 break;
484 }
485 /*
486 Proceed to next image.
487 */
488 if (image_info->number_scenes != 0)
489 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
490 break;
491 count=ReadBlob(image,1,&tga_info.id_length);
492 tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
493 tga_info.image_type=(unsigned char) ReadBlobByte(image);
494 status=((tga_info.image_type == TGAColormap) ||
495 (tga_info.image_type == TGARGB) ||
496 (tga_info.image_type == TGAMonochrome) ||
497 (tga_info.image_type == TGARLEColormap) ||
498 (tga_info.image_type == TGARLERGB) ||
499 (tga_info.image_type == TGARLEMonochrome)) ? MagickTrue : MagickFalse;
500 if (status == MagickTrue)
501 {
502 /*
503 Allocate next image structure.
504 */
505 AcquireNextImage(image_info,image);
506 if (GetNextImageInList(image) == (Image *) NULL)
507 {
508 image=DestroyImageList(image);
509 return((Image *) NULL);
510 }
511 image=SyncNextImageInList(image);
512 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
513 GetBlobSize(image));
514 if (status == MagickFalse)
515 break;
516 }
517 } while (status == MagickTrue);
518 (void) CloseBlob(image);
519 return(GetFirstImageInList(image));
520}
521
522/*
523%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524% %
525% %
526% %
527% R e g i s t e r T G A I m a g e %
528% %
529% %
530% %
531%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
532%
533% RegisterTGAImage() adds properties for the TGA image format to
534% the list of supported formats. The properties include the image format
535% tag, a method to read and/or write the format, whether the format
536% supports the saving of more than one frame to the same file or blob,
537% whether the format supports native in-memory I/O, and a brief
538% description of the format.
539%
540% The format of the RegisterTGAImage method is:
541%
542% unsigned long RegisterTGAImage(void)
543%
544*/
545ModuleExport unsigned long RegisterTGAImage(void)
546{
547 MagickInfo
548 *entry;
549
550 entry=SetMagickInfo("ICB");
551 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
552 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
553 entry->description=ConstantString("Truevision Targa image");
554 entry->module=ConstantString("TGA");
555 (void) RegisterMagickInfo(entry);
556 entry=SetMagickInfo("TGA");
557 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
558 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
559 entry->description=ConstantString("Truevision Targa image");
560 entry->module=ConstantString("TGA");
561 (void) RegisterMagickInfo(entry);
562 entry=SetMagickInfo("VDA");
563 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
564 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
565 entry->description=ConstantString("Truevision Targa image");
566 entry->module=ConstantString("TGA");
567 (void) RegisterMagickInfo(entry);
568 entry=SetMagickInfo("VST");
569 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
570 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
571 entry->description=ConstantString("Truevision Targa image");
572 entry->module=ConstantString("TGA");
573 (void) RegisterMagickInfo(entry);
574 return(MagickImageCoderSignature);
575}
576
577/*
578%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579% %
580% %
581% %
582% U n r e g i s t e r T G A I m a g e %
583% %
584% %
585% %
586%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587%
588% UnregisterTGAImage() removes format registrations made by the
589% TGA module from the list of supported formats.
590%
591% The format of the UnregisterTGAImage method is:
592%
593% UnregisterTGAImage(void)
594%
595*/
596ModuleExport void UnregisterTGAImage(void)
597{
598 (void) UnregisterMagickInfo("ICB");
599 (void) UnregisterMagickInfo("TGA");
600 (void) UnregisterMagickInfo("VDA");
601 (void) UnregisterMagickInfo("VST");
602}
603
604/*
605%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606% %
607% %
608% %
609% W r i t e T G A I m a g e %
610% %
611% %
612% %
613%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
614%
615% WriteTGAImage() writes a image in the Truevision Targa rasterfile
616% format.
617%
618% The format of the WriteTGAImage method is:
619%
620% MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
621%
622% A description of each parameter follows.
623%
624% o image_info: the image info.
625%
626% o image: The image.
627%
628*/
629
630static inline size_t MagickMin(const size_t x,const size_t y)
631{
632 if (x < y)
633 return(x);
634 return(y);
635}
636
637static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
638{
639#define TargaColormap 1
640#define TargaRGB 2
641#define TargaMonochrome 3
642#define TargaRLEColormap 9
643#define TargaRLERGB 10
644#define TargaRLEMonochrome 11
645
646 typedef struct _TargaInfo
647 {
648 unsigned char
649 id_length,
650 colormap_type,
651 image_type;
652
653 unsigned short
654 colormap_index,
655 colormap_length;
656
657 unsigned char
658 colormap_size;
659
660 unsigned short
661 x_origin,
662 y_origin,
663 width,
664 height;
665
666 unsigned char
667 bits_per_pixel,
668 attributes;
669 } TargaInfo;
670
671 const char
672 *value;
673
674 long
675 y;
676
677 MagickBooleanType
678 status;
679
680 MagickOffsetType
681 scene;
682
683 register const IndexPacket
684 *indexes;
685
686 register const PixelPacket
687 *p;
688
689 register long
690 x;
691
692 register long
693 i;
694
695 register unsigned char
696 *q;
697
698 ssize_t
699 count;
700
701 TargaInfo
702 targa_info;
703
704 unsigned char
705 *targa_pixels;
706
707 /*
708 Open output image file.
709 */
710 assert(image_info != (const ImageInfo *) NULL);
711 assert(image_info->signature == MagickSignature);
712 assert(image != (Image *) NULL);
713 assert(image->signature == MagickSignature);
714 if (image->debug != MagickFalse)
715 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
716 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
717 if (status == MagickFalse)
718 return(status);
719 scene=0;
720 do
721 {
722 /*
723 Initialize TGA raster file header.
724 */
725 if ((image->columns > 65535L) || (image->rows > 65535L))
726 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
727 if (image->colorspace != RGBColorspace)
728 (void) TransformImageColorspace(image,RGBColorspace);
729 targa_info.id_length=0;
730 value=GetImageProperty(image,"comment");
731 if (value != (const char *) NULL)
732 targa_info.id_length=(unsigned char) MagickMin(strlen(value),255);
733 targa_info.colormap_type=0;
734 targa_info.colormap_index=0;
735 targa_info.colormap_length=0;
736 targa_info.colormap_size=0;
737 targa_info.x_origin=0;
738 targa_info.y_origin=0;
739 targa_info.width=(unsigned short) image->columns;
740 targa_info.height=(unsigned short) image->rows;
741 targa_info.bits_per_pixel=8;
742 targa_info.attributes=0;
743 if ((image_info->type != TrueColorType) &&
744 (image_info->type != TrueColorMatteType) &&
745 (image_info->type != PaletteType) &&
746 (image->matte == MagickFalse) &&
747 (IsGrayImage(image,&image->exception) != MagickFalse))
748 targa_info.image_type=TargaMonochrome;
749 else
750 if ((image->storage_class == DirectClass) || (image->colors > 256))
751 {
752 /*
753 Full color TGA raster.
754 */
755 targa_info.image_type=TargaRGB;
756 targa_info.bits_per_pixel=24;
757 if (image->matte != MagickFalse)
758 {
759 targa_info.bits_per_pixel=32;
760 targa_info.attributes=8; /* # of alpha bits */
761 }
762 }
763 else
764 {
765 /*
766 Colormapped TGA raster.
767 */
768 targa_info.image_type=TargaColormap;
769 targa_info.colormap_type=1;
770 targa_info.colormap_length=(unsigned short) image->colors;
771 targa_info.colormap_size=24;
772 }
773 /*
774 Write TGA header.
775 */
776 (void) WriteBlobByte(image,targa_info.id_length);
777 (void) WriteBlobByte(image,targa_info.colormap_type);
778 (void) WriteBlobByte(image,targa_info.image_type);
779 (void) WriteBlobLSBShort(image,targa_info.colormap_index);
780 (void) WriteBlobLSBShort(image,targa_info.colormap_length);
781 (void) WriteBlobByte(image,targa_info.colormap_size);
782 (void) WriteBlobLSBShort(image,targa_info.x_origin);
783 (void) WriteBlobLSBShort(image,targa_info.y_origin);
784 (void) WriteBlobLSBShort(image,targa_info.width);
785 (void) WriteBlobLSBShort(image,targa_info.height);
786 (void) WriteBlobByte(image,targa_info.bits_per_pixel);
787 (void) WriteBlobByte(image,targa_info.attributes);
788 if (targa_info.id_length != 0)
789 (void) WriteBlob(image,targa_info.id_length,(unsigned char *)
790 value);
791 if (targa_info.image_type == TargaColormap)
792 {
793 unsigned char
794 *targa_colormap;
795
796 /*
797 Dump colormap to file (blue, green, red byte order).
798 */
799 targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
800 targa_info.colormap_length,3UL*sizeof(*targa_colormap));
801 if (targa_colormap == (unsigned char *) NULL)
802 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
803 q=targa_colormap;
804 for (i=0; i < (long) image->colors; i++)
805 {
806 *q++=ScaleQuantumToChar(image->colormap[i].blue);
807 *q++=ScaleQuantumToChar(image->colormap[i].green);
808 *q++=ScaleQuantumToChar(image->colormap[i].red);
809 }
810 (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length),
811 targa_colormap);
812 targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
813 }
814 /*
815 Convert MIFF to TGA raster pixels.
816 */
817 count=(ssize_t) (targa_info.bits_per_pixel*targa_info.width)/8;
818 targa_pixels=(unsigned char *) AcquireQuantumMemory((size_t) count,
819 sizeof(*targa_pixels));
820 if (targa_pixels == (unsigned char *) NULL)
821 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
822 for (y=(long) (image->rows-1); y >= 0; y--)
823 {
824 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
825 if (p == (const PixelPacket *) NULL)
826 break;
827 q=targa_pixels;
828 indexes=GetVirtualIndexQueue(image);
829 for (x=0; x < (long) image->columns; x++)
830 {
831 if (targa_info.image_type == TargaColormap)
832 *q++=(unsigned char) indexes[x];
833 else
834 if (targa_info.image_type == TargaMonochrome)
835 *q++=(unsigned char) ScaleQuantumToChar(PixelIntensityToQuantum(p));
836 else
837 {
cristyce70c172010-01-07 17:15:30 +0000838 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
839 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
840 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000841 if (image->matte != MagickFalse)
842 *q++=(unsigned char) ScaleQuantumToChar((Quantum)
cristy46f08202010-01-10 04:04:21 +0000843 (GetAlphaPixelComponent(p)));
cristy3ed852e2009-09-05 21:47:34 +0000844 if (image->colorspace == CMYKColorspace)
845 *q++=ScaleQuantumToChar(indexes[x]);
846 }
847 p++;
848 }
849 (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
850 if (image->previous == (Image *) NULL)
851 {
852 status=SetImageProgress(image,SaveImageTag,y,image->rows);
853 if (status == MagickFalse)
854 break;
855 }
856 }
857 targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
858 if (GetNextImageInList(image) == (Image *) NULL)
859 break;
860 image=SyncNextImageInList(image);
861 status=SetImageProgress(image,SaveImagesTag,scene++,
862 GetImageListLength(image));
863 if (status == MagickFalse)
864 break;
865 } while (image_info->adjoin != MagickFalse);
866 (void) CloseBlob(image);
867 return(MagickTrue);
868}