blob: 9f7332cdb2ec4917c27c739686c227eefa7d2697 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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,
cristy5dd83a52011-11-24 01:54:17 +0000170 pixels[4],
cristy3ed852e2009-09-05 21:47:34 +0000171 runlength;
172
cristy18307f12011-12-30 01:20:16 +0000173 unsigned int
174 alpha_bits;
175
cristy3ed852e2009-09-05 21:47:34 +0000176 /*
177 Open image file.
178 */
179 assert(image_info != (const ImageInfo *) NULL);
180 assert(image_info->signature == MagickSignature);
181 if (image_info->debug != MagickFalse)
182 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
183 image_info->filename);
184 assert(exception != (ExceptionInfo *) NULL);
185 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000186 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000187 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
188 if (status == MagickFalse)
189 {
190 image=DestroyImageList(image);
191 return((Image *) NULL);
192 }
193 /*
194 Read TGA header information.
195 */
196 count=ReadBlob(image,1,&tga_info.id_length);
197 tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
198 tga_info.image_type=(unsigned char) ReadBlobByte(image);
cristy98140912011-04-20 14:11:45 +0000199 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 if ((((tga_info.bits_per_pixel <= 1) || (tga_info.bits_per_pixel >= 17)) &&
222 (tga_info.bits_per_pixel != 24) && (tga_info.bits_per_pixel != 32)))
223 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
224 /*
225 Initialize image structure.
226 */
227 image->columns=tga_info.width;
228 image->rows=tga_info.height;
cristy18307f12011-12-30 01:20:16 +0000229 alpha_bits=(tga_info.attributes & 0x0FU);
230 image->matte=(alpha_bits > 0) || (tga_info.bits_per_pixel == 32) ?
231 MagickTrue : MagickFalse;
cristy98140912011-04-20 14:11:45 +0000232 if ((tga_info.image_type != TGAColormap) &&
233 (tga_info.image_type != TGARLEColormap))
234 image->depth=(size_t) ((tga_info.bits_per_pixel <= 8) ? 8 :
235 (tga_info.bits_per_pixel <= 16) ? 5 :
236 (tga_info.bits_per_pixel == 24) ? 8 :
237 (tga_info.bits_per_pixel == 32) ? 8 : 8);
238 else
239 image->depth=(size_t) ((tga_info.colormap_size <= 8) ? 8 :
240 (tga_info.colormap_size <= 16) ? 5 :
241 (tga_info.colormap_size == 24) ? 8 :
242 (tga_info.colormap_size == 32) ? 8 : 8);
243 if ((tga_info.image_type == TGAColormap) ||
cristy3ed852e2009-09-05 21:47:34 +0000244 (tga_info.image_type == TGAMonochrome) ||
245 (tga_info.image_type == TGARLEColormap) ||
cristy98140912011-04-20 14:11:45 +0000246 (tga_info.image_type == TGARLEMonochrome))
247 image->storage_class=PseudoClass;
248 image->compression=NoCompression;
249 if ((tga_info.image_type == TGARLEColormap) ||
250 (tga_info.image_type == TGARLEMonochrome))
251 image->compression=RLECompression;
252 if (image->storage_class == PseudoClass)
253 {
254 if (tga_info.colormap_type != 0)
255 image->colors=tga_info.colormap_length;
256 else
257 {
258 size_t
259 one;
260
261 one=1;
262 image->colors=one << tga_info.bits_per_pixel;
cristy018f07f2011-09-04 21:15:19 +0000263 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy98140912011-04-20 14:11:45 +0000264 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
265 }
266 }
267 if (tga_info.id_length != 0)
268 {
269 char
270 *comment;
271
272 size_t
273 length;
274
275 /*
276 TGA image comment.
277 */
278 length=(size_t) tga_info.id_length;
279 comment=(char *) NULL;
cristy37e0b382011-06-07 13:31:21 +0000280 if (~length >= (MaxTextExtent-1))
cristy98140912011-04-20 14:11:45 +0000281 comment=(char *) AcquireQuantumMemory(length+MaxTextExtent,
282 sizeof(*comment));
283 if (comment == (char *) NULL)
284 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
285 count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
286 comment[tga_info.id_length]='\0';
cristyd15e6592011-10-15 00:13:06 +0000287 (void) SetImageProperty(image,"comment",comment,exception);
cristy98140912011-04-20 14:11:45 +0000288 comment=DestroyString(comment);
289 }
290 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
cristy4c08aed2011-07-01 19:47:50 +0000291 pixel.alpha=(Quantum) OpaqueAlpha;
cristy98140912011-04-20 14:11:45 +0000292 if (tga_info.colormap_type != 0)
293 {
294 /*
295 Read TGA raster colormap.
296 */
cristy018f07f2011-09-04 21:15:19 +0000297 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy98140912011-04-20 14:11:45 +0000298 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
299 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000300 {
cristy98140912011-04-20 14:11:45 +0000301 switch (tga_info.colormap_size)
302 {
303 case 8:
304 default:
cristy3ed852e2009-09-05 21:47:34 +0000305 {
cristy98140912011-04-20 14:11:45 +0000306 /*
307 Gray scale.
308 */
309 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
310 pixel.green=pixel.red;
311 pixel.blue=pixel.red;
312 break;
cristy3ed852e2009-09-05 21:47:34 +0000313 }
cristy98140912011-04-20 14:11:45 +0000314 case 15:
315 case 16:
316 {
317 QuantumAny
318 range;
319
320 /*
321 5 bits each of red green and blue.
322 */
323 j=(unsigned char) ReadBlobByte(image);
324 k=(unsigned char) ReadBlobByte(image);
325 range=GetQuantumRange(5UL);
326 pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
327 pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
328 (1UL*(j & 0xe0) >> 5),range);
329 pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
330 break;
331 }
332 case 24:
333 case 32:
334 {
335 /*
336 8 bits each of blue, green and red.
337 */
338 pixel.blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
339 pixel.green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
340 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
341 break;
342 }
343 }
344 image->colormap[i]=pixel;
345 }
346 }
347 /*
348 Convert TGA pixels to pixel packets.
349 */
350 base=0;
351 flag=0;
352 skip=MagickFalse;
353 real=0;
cristy4c08aed2011-07-01 19:47:50 +0000354 index=0;
cristy98140912011-04-20 14:11:45 +0000355 runlength=0;
356 offset=0;
357 for (y=0; y < (ssize_t) image->rows; y++)
358 {
359 real=offset;
360 if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
361 real=image->rows-real-1;
362 q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000363 if (q == (Quantum *) NULL)
cristy98140912011-04-20 14:11:45 +0000364 break;
cristy98140912011-04-20 14:11:45 +0000365 for (x=0; x < (ssize_t) image->columns; x++)
366 {
367 if ((tga_info.image_type == TGARLEColormap) ||
368 (tga_info.image_type == TGARLERGB) ||
369 (tga_info.image_type == TGARLEMonochrome))
370 {
371 if (runlength != 0)
372 {
373 runlength--;
374 skip=flag != 0;
375 }
376 else
377 {
378 count=ReadBlob(image,1,&runlength);
379 if (count == 0)
380 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
381 flag=runlength & 0x80;
382 if (flag != 0)
383 runlength-=128;
384 skip=MagickFalse;
385 }
386 }
387 if (skip == MagickFalse)
388 switch (tga_info.bits_per_pixel)
389 {
390 case 8:
391 default:
392 {
393 /*
394 Gray scale.
395 */
cristy4c08aed2011-07-01 19:47:50 +0000396 index=(Quantum) ReadBlobByte(image);
cristy98140912011-04-20 14:11:45 +0000397 if (tga_info.colormap_type != 0)
398 pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
cristyc82a27b2011-10-21 01:07:16 +0000399 1UL*index,exception)];
cristy98140912011-04-20 14:11:45 +0000400 else
401 {
402 pixel.red=ScaleCharToQuantum((unsigned char) index);
403 pixel.green=ScaleCharToQuantum((unsigned char) index);
404 pixel.blue=ScaleCharToQuantum((unsigned char) index);
405 }
406 break;
407 }
408 case 15:
409 case 16:
410 {
411 QuantumAny
412 range;
413
414 /*
cristy5dd83a52011-11-24 01:54:17 +0000415 5 bits each of RGB.
cristy98140912011-04-20 14:11:45 +0000416 */
cristy5dd83a52011-11-24 01:54:17 +0000417 if (ReadBlob(image,2,pixels) != 2)
418 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
419 j=pixels[0];
420 k=pixels[1];
cristy98140912011-04-20 14:11:45 +0000421 range=GetQuantumRange(5UL);
422 pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
423 pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
424 (1UL*(j & 0xe0) >> 5),range);
425 pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
426 if (image->matte != MagickFalse)
cristy5dd83a52011-11-24 01:54:17 +0000427 pixel.alpha=(k & 0x80) == 0 ? (Quantum) OpaqueAlpha : (Quantum)
428 TransparentAlpha;
cristy98140912011-04-20 14:11:45 +0000429 if (image->storage_class == PseudoClass)
cristyc82a27b2011-10-21 01:07:16 +0000430 index=ConstrainColormapIndex(image,((size_t) k << 8)+j,exception);
cristy98140912011-04-20 14:11:45 +0000431 break;
432 }
433 case 24:
cristy5dd83a52011-11-24 01:54:17 +0000434 {
435 /*
436 BGR pixels.
437 */
438 if (ReadBlob(image,3,pixels) != 3)
439 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
440 pixel.blue=ScaleCharToQuantum(pixels[0]);
441 pixel.green=ScaleCharToQuantum(pixels[1]);
442 pixel.red=ScaleCharToQuantum(pixels[2]);
443 break;
444 }
cristy98140912011-04-20 14:11:45 +0000445 case 32:
446 {
447 /*
cristy5dd83a52011-11-24 01:54:17 +0000448 BGRA pixels.
cristy98140912011-04-20 14:11:45 +0000449 */
cristy5dd83a52011-11-24 01:54:17 +0000450 if (ReadBlob(image,4,pixels) != 4)
451 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
452 pixel.blue=ScaleCharToQuantum(pixels[0]);
453 pixel.green=ScaleCharToQuantum(pixels[1]);
454 pixel.red=ScaleCharToQuantum(pixels[2]);
455 pixel.alpha=ScaleCharToQuantum(pixels[3]);
cristy98140912011-04-20 14:11:45 +0000456 break;
457 }
458 }
459 if (status == MagickFalse)
460 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
461 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000462 SetPixelIndex(image,index,q);
463 SetPixelRed(image,pixel.red,q);
464 SetPixelGreen(image,pixel.green,q);
465 SetPixelBlue(image,pixel.blue,q);
cristy98140912011-04-20 14:11:45 +0000466 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000467 SetPixelAlpha(image,pixel.alpha,q);
cristyed231572011-07-14 02:18:59 +0000468 q+=GetPixelChannels(image);
cristy98140912011-04-20 14:11:45 +0000469 }
470 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
471 offset+=4;
472 else
473 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
474 offset+=2;
475 else
476 offset++;
477 if (offset >= image->rows)
478 {
479 base++;
480 offset=base;
481 }
482 if (SyncAuthenticPixels(image,exception) == MagickFalse)
483 break;
484 if (image->previous == (Image *) NULL)
485 {
486 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
487 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000488 if (status == MagickFalse)
489 break;
490 }
cristy98140912011-04-20 14:11:45 +0000491 }
492 if (EOFBlob(image) != MagickFalse)
493 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
494 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000495 (void) CloseBlob(image);
496 return(GetFirstImageInList(image));
497}
498
499/*
500%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
501% %
502% %
503% %
504% R e g i s t e r T G A I m a g e %
505% %
506% %
507% %
508%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
509%
510% RegisterTGAImage() adds properties for the TGA image format to
511% the list of supported formats. The properties include the image format
512% tag, a method to read and/or write the format, whether the format
513% supports the saving of more than one frame to the same file or blob,
514% whether the format supports native in-memory I/O, and a brief
515% description of the format.
516%
517% The format of the RegisterTGAImage method is:
518%
cristybb503372010-05-27 20:51:26 +0000519% size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000520%
521*/
cristybb503372010-05-27 20:51:26 +0000522ModuleExport size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000523{
524 MagickInfo
525 *entry;
526
527 entry=SetMagickInfo("ICB");
528 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
529 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
cristy98140912011-04-20 14:11:45 +0000530 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000531 entry->description=ConstantString("Truevision Targa image");
532 entry->module=ConstantString("TGA");
533 (void) RegisterMagickInfo(entry);
534 entry=SetMagickInfo("TGA");
535 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
536 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
cristy98140912011-04-20 14:11:45 +0000537 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000538 entry->description=ConstantString("Truevision Targa image");
539 entry->module=ConstantString("TGA");
540 (void) RegisterMagickInfo(entry);
541 entry=SetMagickInfo("VDA");
542 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
543 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
cristy98140912011-04-20 14:11:45 +0000544 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000545 entry->description=ConstantString("Truevision Targa image");
546 entry->module=ConstantString("TGA");
547 (void) RegisterMagickInfo(entry);
548 entry=SetMagickInfo("VST");
549 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
550 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
cristy98140912011-04-20 14:11:45 +0000551 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000552 entry->description=ConstantString("Truevision Targa image");
553 entry->module=ConstantString("TGA");
554 (void) RegisterMagickInfo(entry);
555 return(MagickImageCoderSignature);
556}
557
558/*
559%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560% %
561% %
562% %
563% U n r e g i s t e r T G A I m a g e %
564% %
565% %
566% %
567%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568%
569% UnregisterTGAImage() removes format registrations made by the
570% TGA module from the list of supported formats.
571%
572% The format of the UnregisterTGAImage method is:
573%
574% UnregisterTGAImage(void)
575%
576*/
577ModuleExport void UnregisterTGAImage(void)
578{
579 (void) UnregisterMagickInfo("ICB");
580 (void) UnregisterMagickInfo("TGA");
581 (void) UnregisterMagickInfo("VDA");
582 (void) UnregisterMagickInfo("VST");
583}
584
585/*
586%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587% %
588% %
589% %
590% W r i t e T G A I m a g e %
591% %
592% %
593% %
594%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
595%
596% WriteTGAImage() writes a image in the Truevision Targa rasterfile
597% format.
598%
599% The format of the WriteTGAImage method is:
600%
cristy3a37efd2011-08-28 20:31:03 +0000601% MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
602% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000603%
604% A description of each parameter follows.
605%
606% o image_info: the image info.
607%
608% o image: The image.
609%
610*/
611
612static inline size_t MagickMin(const size_t x,const size_t y)
613{
614 if (x < y)
615 return(x);
616 return(y);
617}
618
cristy3a37efd2011-08-28 20:31:03 +0000619static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
620 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000621{
622#define TargaColormap 1
623#define TargaRGB 2
624#define TargaMonochrome 3
625#define TargaRLEColormap 9
626#define TargaRLERGB 10
627#define TargaRLEMonochrome 11
628
629 typedef struct _TargaInfo
630 {
631 unsigned char
632 id_length,
633 colormap_type,
634 image_type;
635
636 unsigned short
637 colormap_index,
638 colormap_length;
639
640 unsigned char
641 colormap_size;
642
643 unsigned short
644 x_origin,
645 y_origin,
646 width,
647 height;
648
649 unsigned char
650 bits_per_pixel,
651 attributes;
652 } TargaInfo;
653
654 const char
655 *value;
656
cristy3ed852e2009-09-05 21:47:34 +0000657 MagickBooleanType
658 status;
659
cristy4c08aed2011-07-01 19:47:50 +0000660 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000661 *p;
662
cristybb503372010-05-27 20:51:26 +0000663 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000664 x;
665
cristybb503372010-05-27 20:51:26 +0000666 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000667 i;
668
669 register unsigned char
670 *q;
671
672 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000673 count,
674 y;
cristy3ed852e2009-09-05 21:47:34 +0000675
676 TargaInfo
677 targa_info;
678
679 unsigned char
680 *targa_pixels;
681
682 /*
683 Open output image file.
684 */
685 assert(image_info != (const ImageInfo *) NULL);
686 assert(image_info->signature == MagickSignature);
687 assert(image != (Image *) NULL);
688 assert(image->signature == MagickSignature);
689 if (image->debug != MagickFalse)
690 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000691 assert(exception != (ExceptionInfo *) NULL);
692 assert(exception->signature == MagickSignature);
693 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000694 if (status == MagickFalse)
695 return(status);
cristy98140912011-04-20 14:11:45 +0000696 /*
697 Initialize TGA raster file header.
698 */
699 if ((image->columns > 65535L) || (image->rows > 65535L))
700 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
cristy82e0dca2012-06-17 16:20:37 +0000701 if ((IssRGBColorspace(image->colorspace) == MagickFalse) &&
702 (IsImageGray(image,exception) == MagickFalse))
cristy8d951092012-02-08 18:54:56 +0000703 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy98140912011-04-20 14:11:45 +0000704 targa_info.id_length=0;
cristyd15e6592011-10-15 00:13:06 +0000705 value=GetImageProperty(image,"comment",exception);
cristy98140912011-04-20 14:11:45 +0000706 if (value != (const char *) NULL)
707 targa_info.id_length=(unsigned char) MagickMin(strlen(value),255);
708 targa_info.colormap_type=0;
709 targa_info.colormap_index=0;
710 targa_info.colormap_length=0;
711 targa_info.colormap_size=0;
712 targa_info.x_origin=0;
713 targa_info.y_origin=0;
714 targa_info.width=(unsigned short) image->columns;
715 targa_info.height=(unsigned short) image->rows;
716 targa_info.bits_per_pixel=8;
717 targa_info.attributes=0;
718 if ((image_info->type != TrueColorType) &&
719 (image_info->type != TrueColorMatteType) &&
720 (image_info->type != PaletteType) &&
721 (image->matte == MagickFalse) &&
cristy3a37efd2011-08-28 20:31:03 +0000722 (IsImageGray(image,exception) != MagickFalse))
cristy98140912011-04-20 14:11:45 +0000723 targa_info.image_type=TargaMonochrome;
724 else
725 if ((image->storage_class == DirectClass) || (image->colors > 256))
cristy3ed852e2009-09-05 21:47:34 +0000726 {
cristy3ed852e2009-09-05 21:47:34 +0000727 /*
cristy98140912011-04-20 14:11:45 +0000728 Full color TGA raster.
cristy3ed852e2009-09-05 21:47:34 +0000729 */
cristy98140912011-04-20 14:11:45 +0000730 targa_info.image_type=TargaRGB;
731 targa_info.bits_per_pixel=24;
732 if (image->matte != MagickFalse)
733 {
734 targa_info.bits_per_pixel=32;
735 targa_info.attributes=8; /* # of alpha bits */
736 }
cristy3ed852e2009-09-05 21:47:34 +0000737 }
cristy98140912011-04-20 14:11:45 +0000738 else
cristy3ed852e2009-09-05 21:47:34 +0000739 {
cristy98140912011-04-20 14:11:45 +0000740 /*
741 Colormapped TGA raster.
742 */
743 targa_info.image_type=TargaColormap;
744 targa_info.colormap_type=1;
745 targa_info.colormap_length=(unsigned short) image->colors;
746 targa_info.colormap_size=24;
cristy3ed852e2009-09-05 21:47:34 +0000747 }
cristy98140912011-04-20 14:11:45 +0000748 /*
749 Write TGA header.
750 */
751 (void) WriteBlobByte(image,targa_info.id_length);
752 (void) WriteBlobByte(image,targa_info.colormap_type);
753 (void) WriteBlobByte(image,targa_info.image_type);
754 (void) WriteBlobLSBShort(image,targa_info.colormap_index);
755 (void) WriteBlobLSBShort(image,targa_info.colormap_length);
756 (void) WriteBlobByte(image,targa_info.colormap_size);
757 (void) WriteBlobLSBShort(image,targa_info.x_origin);
758 (void) WriteBlobLSBShort(image,targa_info.y_origin);
759 (void) WriteBlobLSBShort(image,targa_info.width);
760 (void) WriteBlobLSBShort(image,targa_info.height);
761 (void) WriteBlobByte(image,targa_info.bits_per_pixel);
762 (void) WriteBlobByte(image,targa_info.attributes);
763 if (targa_info.id_length != 0)
764 (void) WriteBlob(image,targa_info.id_length,(unsigned char *)
765 value);
766 if (targa_info.image_type == TargaColormap)
767 {
768 unsigned char
769 *targa_colormap;
770
771 /*
772 Dump colormap to file (blue, green, red byte order).
773 */
774 targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
775 targa_info.colormap_length,3UL*sizeof(*targa_colormap));
776 if (targa_colormap == (unsigned char *) NULL)
777 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
778 q=targa_colormap;
779 for (i=0; i < (ssize_t) image->colors; i++)
780 {
781 *q++=ScaleQuantumToChar(image->colormap[i].blue);
782 *q++=ScaleQuantumToChar(image->colormap[i].green);
783 *q++=ScaleQuantumToChar(image->colormap[i].red);
784 }
785 (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length),
786 targa_colormap);
787 targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
cristy3ed852e2009-09-05 21:47:34 +0000788 }
cristy98140912011-04-20 14:11:45 +0000789 /*
790 Convert MIFF to TGA raster pixels.
791 */
792 count=(ssize_t) (targa_info.bits_per_pixel*targa_info.width)/8;
793 targa_pixels=(unsigned char *) AcquireQuantumMemory((size_t) count,
794 sizeof(*targa_pixels));
795 if (targa_pixels == (unsigned char *) NULL)
796 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
797 for (y=(ssize_t) (image->rows-1); y >= 0; y--)
798 {
cristy3a37efd2011-08-28 20:31:03 +0000799 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000800 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000801 break;
cristy98140912011-04-20 14:11:45 +0000802 q=targa_pixels;
cristy98140912011-04-20 14:11:45 +0000803 for (x=0; x < (ssize_t) image->columns; x++)
804 {
805 if (targa_info.image_type == TargaColormap)
cristy4c08aed2011-07-01 19:47:50 +0000806 *q++=(unsigned char) GetPixelIndex(image,p);
cristy98140912011-04-20 14:11:45 +0000807 else
808 if (targa_info.image_type == TargaMonochrome)
cristy4c08aed2011-07-01 19:47:50 +0000809 *q++=(unsigned char) ScaleQuantumToChar(GetPixelIntensity(image,p));
cristy98140912011-04-20 14:11:45 +0000810 else
811 {
cristy4c08aed2011-07-01 19:47:50 +0000812 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
813 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
814 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
cristy98140912011-04-20 14:11:45 +0000815 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000816 *q++=(unsigned char) ScaleQuantumToChar(GetPixelAlpha(image,p));
cristy98140912011-04-20 14:11:45 +0000817 }
cristyed231572011-07-14 02:18:59 +0000818 p+=GetPixelChannels(image);
cristy98140912011-04-20 14:11:45 +0000819 }
820 (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
821 if (image->previous == (Image *) NULL)
822 {
823 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000824 image->rows);
cristy98140912011-04-20 14:11:45 +0000825 if (status == MagickFalse)
826 break;
827 }
828 }
829 targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000830 (void) CloseBlob(image);
831 return(MagickTrue);
832}