blob: fa23ceb93b68489d0484800403d809ed6fa15b7c [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
cristybb503372010-05-27 20:51:26 +0000137 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000138 y;
139
140 MagickBooleanType
141 status;
142
143 PixelPacket
144 pixel;
145
146 register IndexPacket
147 *indexes;
148
cristybb503372010-05-27 20:51:26 +0000149 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000150 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
cristybb503372010-05-27 20:51:26 +0000167 size_t
cristy3ed852e2009-09-05 21:47:34 +0000168 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))
cristybb503372010-05-27 20:51:26 +0000229 image->depth=(size_t) ((tga_info.bits_per_pixel <= 8) ? 8 :
cristy3ed852e2009-09-05 21:47:34 +0000230 (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
cristybb503372010-05-27 20:51:26 +0000234 image->depth=(size_t) ((tga_info.colormap_size <= 8) ? 8 :
cristy3ed852e2009-09-05 21:47:34 +0000235 (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");
cristybb503372010-05-27 20:51:26 +0000290 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000291 {
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;
cristybb503372010-05-27 20:51:26 +0000354 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000355 {
356 real=offset;
357 if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
358 real=image->rows-real-1;
cristybb503372010-05-27 20:51:26 +0000359 q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
cristy3ed852e2009-09-05 21:47:34 +0000360 if (q == (PixelPacket *) NULL)
361 break;
362 indexes=GetAuthenticIndexQueue(image);
cristybb503372010-05-27 20:51:26 +0000363 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000364 {
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)
cristybb503372010-05-27 20:51:26 +0000397 pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
cristy3ed852e2009-09-05 21:47:34 +0000398 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)
cristybb503372010-05-27 20:51:26 +0000427 index=ConstrainColormapIndex(image,((size_t) k << 8)+j);
cristy3ed852e2009-09-05 21:47:34 +0000428 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 {
cristycee97112010-05-28 00:44:52 +0000474 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
475 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000476 if (status == MagickFalse)
477 break;
478 }
479 }
480 if (EOFBlob(image) != MagickFalse)
481 {
482 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
483 image->filename);
484 break;
485 }
486 /*
487 Proceed to next image.
488 */
489 if (image_info->number_scenes != 0)
490 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
491 break;
492 count=ReadBlob(image,1,&tga_info.id_length);
493 tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
494 tga_info.image_type=(unsigned char) ReadBlobByte(image);
495 status=((tga_info.image_type == TGAColormap) ||
496 (tga_info.image_type == TGARGB) ||
497 (tga_info.image_type == TGAMonochrome) ||
498 (tga_info.image_type == TGARLEColormap) ||
499 (tga_info.image_type == TGARLERGB) ||
500 (tga_info.image_type == TGARLEMonochrome)) ? MagickTrue : MagickFalse;
501 if (status == MagickTrue)
502 {
503 /*
504 Allocate next image structure.
505 */
506 AcquireNextImage(image_info,image);
507 if (GetNextImageInList(image) == (Image *) NULL)
508 {
509 image=DestroyImageList(image);
510 return((Image *) NULL);
511 }
512 image=SyncNextImageInList(image);
513 status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
514 GetBlobSize(image));
515 if (status == MagickFalse)
516 break;
517 }
518 } while (status == MagickTrue);
519 (void) CloseBlob(image);
520 return(GetFirstImageInList(image));
521}
522
523/*
524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525% %
526% %
527% %
528% R e g i s t e r T G A I m a g e %
529% %
530% %
531% %
532%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533%
534% RegisterTGAImage() adds properties for the TGA image format to
535% the list of supported formats. The properties include the image format
536% tag, a method to read and/or write the format, whether the format
537% supports the saving of more than one frame to the same file or blob,
538% whether the format supports native in-memory I/O, and a brief
539% description of the format.
540%
541% The format of the RegisterTGAImage method is:
542%
cristybb503372010-05-27 20:51:26 +0000543% size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000544%
545*/
cristybb503372010-05-27 20:51:26 +0000546ModuleExport size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000547{
548 MagickInfo
549 *entry;
550
551 entry=SetMagickInfo("ICB");
552 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
553 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
554 entry->description=ConstantString("Truevision Targa image");
555 entry->module=ConstantString("TGA");
556 (void) RegisterMagickInfo(entry);
557 entry=SetMagickInfo("TGA");
558 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
559 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
560 entry->description=ConstantString("Truevision Targa image");
561 entry->module=ConstantString("TGA");
562 (void) RegisterMagickInfo(entry);
563 entry=SetMagickInfo("VDA");
564 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
565 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
566 entry->description=ConstantString("Truevision Targa image");
567 entry->module=ConstantString("TGA");
568 (void) RegisterMagickInfo(entry);
569 entry=SetMagickInfo("VST");
570 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
571 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
572 entry->description=ConstantString("Truevision Targa image");
573 entry->module=ConstantString("TGA");
574 (void) RegisterMagickInfo(entry);
575 return(MagickImageCoderSignature);
576}
577
578/*
579%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
580% %
581% %
582% %
583% U n r e g i s t e r T G A I m a g e %
584% %
585% %
586% %
587%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
588%
589% UnregisterTGAImage() removes format registrations made by the
590% TGA module from the list of supported formats.
591%
592% The format of the UnregisterTGAImage method is:
593%
594% UnregisterTGAImage(void)
595%
596*/
597ModuleExport void UnregisterTGAImage(void)
598{
599 (void) UnregisterMagickInfo("ICB");
600 (void) UnregisterMagickInfo("TGA");
601 (void) UnregisterMagickInfo("VDA");
602 (void) UnregisterMagickInfo("VST");
603}
604
605/*
606%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607% %
608% %
609% %
610% W r i t e T G A I m a g e %
611% %
612% %
613% %
614%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
615%
616% WriteTGAImage() writes a image in the Truevision Targa rasterfile
617% format.
618%
619% The format of the WriteTGAImage method is:
620%
621% MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
622%
623% A description of each parameter follows.
624%
625% o image_info: the image info.
626%
627% o image: The image.
628%
629*/
630
631static inline size_t MagickMin(const size_t x,const size_t y)
632{
633 if (x < y)
634 return(x);
635 return(y);
636}
637
638static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
639{
640#define TargaColormap 1
641#define TargaRGB 2
642#define TargaMonochrome 3
643#define TargaRLEColormap 9
644#define TargaRLERGB 10
645#define TargaRLEMonochrome 11
646
647 typedef struct _TargaInfo
648 {
649 unsigned char
650 id_length,
651 colormap_type,
652 image_type;
653
654 unsigned short
655 colormap_index,
656 colormap_length;
657
658 unsigned char
659 colormap_size;
660
661 unsigned short
662 x_origin,
663 y_origin,
664 width,
665 height;
666
667 unsigned char
668 bits_per_pixel,
669 attributes;
670 } TargaInfo;
671
672 const char
673 *value;
674
cristybb503372010-05-27 20:51:26 +0000675 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000676 y;
677
678 MagickBooleanType
679 status;
680
681 MagickOffsetType
682 scene;
683
684 register const IndexPacket
685 *indexes;
686
687 register const PixelPacket
688 *p;
689
cristybb503372010-05-27 20:51:26 +0000690 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000691 x;
692
cristybb503372010-05-27 20:51:26 +0000693 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000694 i;
695
696 register unsigned char
697 *q;
698
699 ssize_t
700 count;
701
702 TargaInfo
703 targa_info;
704
705 unsigned char
706 *targa_pixels;
707
708 /*
709 Open output image file.
710 */
711 assert(image_info != (const ImageInfo *) NULL);
712 assert(image_info->signature == MagickSignature);
713 assert(image != (Image *) NULL);
714 assert(image->signature == MagickSignature);
715 if (image->debug != MagickFalse)
716 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
717 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
718 if (status == MagickFalse)
719 return(status);
720 scene=0;
721 do
722 {
723 /*
724 Initialize TGA raster file header.
725 */
726 if ((image->columns > 65535L) || (image->rows > 65535L))
727 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
728 if (image->colorspace != RGBColorspace)
729 (void) TransformImageColorspace(image,RGBColorspace);
730 targa_info.id_length=0;
731 value=GetImageProperty(image,"comment");
732 if (value != (const char *) NULL)
733 targa_info.id_length=(unsigned char) MagickMin(strlen(value),255);
734 targa_info.colormap_type=0;
735 targa_info.colormap_index=0;
736 targa_info.colormap_length=0;
737 targa_info.colormap_size=0;
738 targa_info.x_origin=0;
739 targa_info.y_origin=0;
740 targa_info.width=(unsigned short) image->columns;
741 targa_info.height=(unsigned short) image->rows;
742 targa_info.bits_per_pixel=8;
743 targa_info.attributes=0;
744 if ((image_info->type != TrueColorType) &&
745 (image_info->type != TrueColorMatteType) &&
746 (image_info->type != PaletteType) &&
747 (image->matte == MagickFalse) &&
748 (IsGrayImage(image,&image->exception) != MagickFalse))
749 targa_info.image_type=TargaMonochrome;
750 else
751 if ((image->storage_class == DirectClass) || (image->colors > 256))
752 {
753 /*
754 Full color TGA raster.
755 */
756 targa_info.image_type=TargaRGB;
757 targa_info.bits_per_pixel=24;
758 if (image->matte != MagickFalse)
759 {
760 targa_info.bits_per_pixel=32;
761 targa_info.attributes=8; /* # of alpha bits */
762 }
763 }
764 else
765 {
766 /*
767 Colormapped TGA raster.
768 */
769 targa_info.image_type=TargaColormap;
770 targa_info.colormap_type=1;
771 targa_info.colormap_length=(unsigned short) image->colors;
772 targa_info.colormap_size=24;
773 }
774 /*
775 Write TGA header.
776 */
777 (void) WriteBlobByte(image,targa_info.id_length);
778 (void) WriteBlobByte(image,targa_info.colormap_type);
779 (void) WriteBlobByte(image,targa_info.image_type);
780 (void) WriteBlobLSBShort(image,targa_info.colormap_index);
781 (void) WriteBlobLSBShort(image,targa_info.colormap_length);
782 (void) WriteBlobByte(image,targa_info.colormap_size);
783 (void) WriteBlobLSBShort(image,targa_info.x_origin);
784 (void) WriteBlobLSBShort(image,targa_info.y_origin);
785 (void) WriteBlobLSBShort(image,targa_info.width);
786 (void) WriteBlobLSBShort(image,targa_info.height);
787 (void) WriteBlobByte(image,targa_info.bits_per_pixel);
788 (void) WriteBlobByte(image,targa_info.attributes);
789 if (targa_info.id_length != 0)
790 (void) WriteBlob(image,targa_info.id_length,(unsigned char *)
791 value);
792 if (targa_info.image_type == TargaColormap)
793 {
794 unsigned char
795 *targa_colormap;
796
797 /*
798 Dump colormap to file (blue, green, red byte order).
799 */
800 targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
801 targa_info.colormap_length,3UL*sizeof(*targa_colormap));
802 if (targa_colormap == (unsigned char *) NULL)
803 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
804 q=targa_colormap;
cristybb503372010-05-27 20:51:26 +0000805 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000806 {
807 *q++=ScaleQuantumToChar(image->colormap[i].blue);
808 *q++=ScaleQuantumToChar(image->colormap[i].green);
809 *q++=ScaleQuantumToChar(image->colormap[i].red);
810 }
811 (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length),
812 targa_colormap);
813 targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
814 }
815 /*
816 Convert MIFF to TGA raster pixels.
817 */
818 count=(ssize_t) (targa_info.bits_per_pixel*targa_info.width)/8;
819 targa_pixels=(unsigned char *) AcquireQuantumMemory((size_t) count,
820 sizeof(*targa_pixels));
821 if (targa_pixels == (unsigned char *) NULL)
822 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
cristybb503372010-05-27 20:51:26 +0000823 for (y=(ssize_t) (image->rows-1); y >= 0; y--)
cristy3ed852e2009-09-05 21:47:34 +0000824 {
825 p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
826 if (p == (const PixelPacket *) NULL)
827 break;
828 q=targa_pixels;
829 indexes=GetVirtualIndexQueue(image);
cristybb503372010-05-27 20:51:26 +0000830 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000831 {
832 if (targa_info.image_type == TargaColormap)
833 *q++=(unsigned char) indexes[x];
834 else
835 if (targa_info.image_type == TargaMonochrome)
836 *q++=(unsigned char) ScaleQuantumToChar(PixelIntensityToQuantum(p));
837 else
838 {
cristyce70c172010-01-07 17:15:30 +0000839 *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
840 *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
841 *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
cristy3ed852e2009-09-05 21:47:34 +0000842 if (image->matte != MagickFalse)
843 *q++=(unsigned char) ScaleQuantumToChar((Quantum)
cristy46f08202010-01-10 04:04:21 +0000844 (GetAlphaPixelComponent(p)));
cristy3ed852e2009-09-05 21:47:34 +0000845 if (image->colorspace == CMYKColorspace)
846 *q++=ScaleQuantumToChar(indexes[x]);
847 }
848 p++;
849 }
850 (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
851 if (image->previous == (Image *) NULL)
852 {
cristycee97112010-05-28 00:44:52 +0000853 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
854 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000855 if (status == MagickFalse)
856 break;
857 }
858 }
859 targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
860 if (GetNextImageInList(image) == (Image *) NULL)
861 break;
862 image=SyncNextImageInList(image);
863 status=SetImageProgress(image,SaveImagesTag,scene++,
864 GetImageListLength(image));
865 if (status == MagickFalse)
866 break;
867 } while (image_info->adjoin != MagickFalse);
868 (void) CloseBlob(image);
869 return(MagickTrue);
870}