blob: 8187ec8e104cbbf32f01f536ad4b23bc0923e97d [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% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 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*/
cristy4c08aed2011-07-01 19:47:50 +000042#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"
cristy510d06a2011-07-06 23:43:54 +000051#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#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"
cristy3ed852e2009-09-05 21:47:34 +000067
68/*
69 Forward declarations.
70*/
71static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +000072 WriteTGAImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +000073
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*/
100static 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
cristy3ed852e2009-09-05 21:47:34 +0000137 MagickBooleanType
138 status;
139
cristy101ab702011-10-13 13:06:32 +0000140 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000141 pixel;
142
cristy4c08aed2011-07-01 19:47:50 +0000143 Quantum
144 index;
cristy3ed852e2009-09-05 21:47:34 +0000145
cristy4c08aed2011-07-01 19:47:50 +0000146 register Quantum
cristyc6da28e2011-04-28 01:41:35 +0000147 *q;
148
cristybb503372010-05-27 20:51:26 +0000149 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000150 i,
151 x;
152
cristyc6da28e2011-04-28 01:41:35 +0000153 size_t
154 base,
155 flag,
156 offset,
157 real,
158 skip;
cristy3ed852e2009-09-05 21:47:34 +0000159
160 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000161 count,
162 y;
cristy3ed852e2009-09-05 21:47:34 +0000163
164 TGAInfo
165 tga_info;
166
167 unsigned char
168 j,
169 k,
170 runlength;
171
cristy3ed852e2009-09-05 21:47:34 +0000172 /*
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);
cristy9950d572011-10-01 18:22:35 +0000182 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000183 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);
cristy98140912011-04-20 14:11:45 +0000195 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) ||
cristy3ed852e2009-09-05 21:47:34 +0000238 (tga_info.image_type == TGAMonochrome) ||
239 (tga_info.image_type == TGARLEColormap) ||
cristy98140912011-04-20 14:11:45 +0000240 (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;
cristy018f07f2011-09-04 21:15:19 +0000257 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy98140912011-04-20 14:11:45 +0000258 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;
cristy37e0b382011-06-07 13:31:21 +0000274 if (~length >= (MaxTextExtent-1))
cristy98140912011-04-20 14:11:45 +0000275 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';
cristyd15e6592011-10-15 00:13:06 +0000281 (void) SetImageProperty(image,"comment",comment,exception);
cristy98140912011-04-20 14:11:45 +0000282 comment=DestroyString(comment);
283 }
284 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
cristy4c08aed2011-07-01 19:47:50 +0000285 pixel.alpha=(Quantum) OpaqueAlpha;
cristy98140912011-04-20 14:11:45 +0000286 if (tga_info.colormap_type != 0)
287 {
288 /*
289 Read TGA raster colormap.
290 */
cristy018f07f2011-09-04 21:15:19 +0000291 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy98140912011-04-20 14:11:45 +0000292 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
293 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000294 {
cristy98140912011-04-20 14:11:45 +0000295 switch (tga_info.colormap_size)
296 {
297 case 8:
298 default:
cristy3ed852e2009-09-05 21:47:34 +0000299 {
cristy98140912011-04-20 14:11:45 +0000300 /*
301 Gray scale.
302 */
303 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
304 pixel.green=pixel.red;
305 pixel.blue=pixel.red;
306 break;
cristy3ed852e2009-09-05 21:47:34 +0000307 }
cristy98140912011-04-20 14:11:45 +0000308 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;
cristy4c08aed2011-07-01 19:47:50 +0000348 index=0;
cristy98140912011-04-20 14:11:45 +0000349 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);
cristyacd2ed22011-08-30 01:44:23 +0000357 if (q == (Quantum *) NULL)
cristy98140912011-04-20 14:11:45 +0000358 break;
cristy98140912011-04-20 14:11:45 +0000359 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 */
cristy4c08aed2011-07-01 19:47:50 +0000390 index=(Quantum) ReadBlobByte(image);
cristy98140912011-04-20 14:11:45 +0000391 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)
cristy4c08aed2011-07-01 19:47:50 +0000419 pixel.alpha=(k & 0x80) == 0 ? (Quantum) OpaqueAlpha :
420 (Quantum) TransparentAlpha;
cristy98140912011-04-20 14:11:45 +0000421 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)
cristy4c08aed2011-07-01 19:47:50 +0000435 pixel.alpha=ScaleCharToQuantum((unsigned char)
436 ReadBlobByte(image));
cristy98140912011-04-20 14:11:45 +0000437 break;
438 }
439 }
440 if (status == MagickFalse)
441 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
442 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000443 SetPixelIndex(image,index,q);
444 SetPixelRed(image,pixel.red,q);
445 SetPixelGreen(image,pixel.green,q);
446 SetPixelBlue(image,pixel.blue,q);
cristy98140912011-04-20 14:11:45 +0000447 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000448 SetPixelAlpha(image,pixel.alpha,q);
cristyed231572011-07-14 02:18:59 +0000449 q+=GetPixelChannels(image);
cristy98140912011-04-20 14:11:45 +0000450 }
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);
cristy3ed852e2009-09-05 21:47:34 +0000469 if (status == MagickFalse)
470 break;
471 }
cristy98140912011-04-20 14:11:45 +0000472 }
473 if (EOFBlob(image) != MagickFalse)
474 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
475 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000476 (void) CloseBlob(image);
477 return(GetFirstImageInList(image));
478}
479
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%
cristybb503372010-05-27 20:51:26 +0000500% size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000501%
502*/
cristybb503372010-05-27 20:51:26 +0000503ModuleExport size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000504{
505 MagickInfo
506 *entry;
507
508 entry=SetMagickInfo("ICB");
509 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
510 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
cristy98140912011-04-20 14:11:45 +0000511 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000512 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;
cristy98140912011-04-20 14:11:45 +0000518 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000519 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;
cristy98140912011-04-20 14:11:45 +0000525 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000526 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;
cristy98140912011-04-20 14:11:45 +0000532 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000533 entry->description=ConstantString("Truevision Targa image");
534 entry->module=ConstantString("TGA");
535 (void) RegisterMagickInfo(entry);
536 return(MagickImageCoderSignature);
537}
538
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*/
558ModuleExport void UnregisterTGAImage(void)
559{
560 (void) UnregisterMagickInfo("ICB");
561 (void) UnregisterMagickInfo("TGA");
562 (void) UnregisterMagickInfo("VDA");
563 (void) UnregisterMagickInfo("VST");
564}
565
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%
cristy3a37efd2011-08-28 20:31:03 +0000582% MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
583% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000584%
585% A description of each parameter follows.
586%
587% o image_info: the image info.
588%
589% o image: The image.
590%
591*/
592
593static 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
cristy3a37efd2011-08-28 20:31:03 +0000600static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
601 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000602{
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
cristy3ed852e2009-09-05 21:47:34 +0000638 MagickBooleanType
639 status;
640
cristy4c08aed2011-07-01 19:47:50 +0000641 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000642 *p;
643
cristybb503372010-05-27 20:51:26 +0000644 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000645 x;
646
cristybb503372010-05-27 20:51:26 +0000647 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000648 i;
649
650 register unsigned char
651 *q;
652
653 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000654 count,
655 y;
cristy3ed852e2009-09-05 21:47:34 +0000656
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);
cristy3a37efd2011-08-28 20:31:03 +0000672 assert(exception != (ExceptionInfo *) NULL);
673 assert(exception->signature == MagickSignature);
674 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000675 if (status == MagickFalse)
676 return(status);
cristy98140912011-04-20 14:11:45 +0000677 /*
678 Initialize TGA raster file header.
679 */
680 if ((image->columns > 65535L) || (image->rows > 65535L))
681 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
cristy510d06a2011-07-06 23:43:54 +0000682 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristy98140912011-04-20 14:11:45 +0000683 (void) TransformImageColorspace(image,RGBColorspace);
684 targa_info.id_length=0;
cristyd15e6592011-10-15 00:13:06 +0000685 value=GetImageProperty(image,"comment",exception);
cristy98140912011-04-20 14:11:45 +0000686 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) &&
cristy3a37efd2011-08-28 20:31:03 +0000702 (IsImageGray(image,exception) != MagickFalse))
cristy98140912011-04-20 14:11:45 +0000703 targa_info.image_type=TargaMonochrome;
704 else
705 if ((image->storage_class == DirectClass) || (image->colors > 256))
cristy3ed852e2009-09-05 21:47:34 +0000706 {
cristy3ed852e2009-09-05 21:47:34 +0000707 /*
cristy98140912011-04-20 14:11:45 +0000708 Full color TGA raster.
cristy3ed852e2009-09-05 21:47:34 +0000709 */
cristy98140912011-04-20 14:11:45 +0000710 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 }
cristy3ed852e2009-09-05 21:47:34 +0000717 }
cristy98140912011-04-20 14:11:45 +0000718 else
cristy3ed852e2009-09-05 21:47:34 +0000719 {
cristy98140912011-04-20 14:11:45 +0000720 /*
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;
cristy3ed852e2009-09-05 21:47:34 +0000727 }
cristy98140912011-04-20 14:11:45 +0000728 /*
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);
cristy3ed852e2009-09-05 21:47:34 +0000768 }
cristy98140912011-04-20 14:11:45 +0000769 /*
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 {
cristy3a37efd2011-08-28 20:31:03 +0000779 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000780 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000781 break;
cristy98140912011-04-20 14:11:45 +0000782 q=targa_pixels;
cristy98140912011-04-20 14:11:45 +0000783 for (x=0; x < (ssize_t) image->columns; x++)
784 {
785 if (targa_info.image_type == TargaColormap)
cristy4c08aed2011-07-01 19:47:50 +0000786 *q++=(unsigned char) GetPixelIndex(image,p);
cristy98140912011-04-20 14:11:45 +0000787 else
788 if (targa_info.image_type == TargaMonochrome)
cristy4c08aed2011-07-01 19:47:50 +0000789 *q++=(unsigned char) ScaleQuantumToChar(GetPixelIntensity(image,p));
cristy98140912011-04-20 14:11:45 +0000790 else
791 {
cristy4c08aed2011-07-01 19:47:50 +0000792 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
793 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
794 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
cristy98140912011-04-20 14:11:45 +0000795 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000796 *q++=(unsigned char) ScaleQuantumToChar(GetPixelAlpha(image,p));
cristy98140912011-04-20 14:11:45 +0000797 }
cristyed231572011-07-14 02:18:59 +0000798 p+=GetPixelChannels(image);
cristy98140912011-04-20 14:11:45 +0000799 }
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,
cristyc6da28e2011-04-28 01:41:35 +0000804 image->rows);
cristy98140912011-04-20 14:11:45 +0000805 if (status == MagickFalse)
806 break;
807 }
808 }
809 targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000810 (void) CloseBlob(image);
811 return(MagickTrue);
812}