blob: bcd0e4dab2ef30631b4b96f9df4b395ea465a2fc [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% %
cristyfe676ee2013-11-18 13:03:38 +000020% Copyright 1999-2014 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);
cristyba213402013-06-28 14:05:12 +0000230 image->alpha_trait=(alpha_bits > 0) || (tga_info.bits_per_pixel == 32) ||
231 (tga_info.colormap_size == 32) ? BlendPixelTrait : UndefinedPixelTrait;
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));
cristy77b33ba2013-03-28 13:23:13 +0000291 pixel.alpha=(MagickRealType) 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 */
cristy77b33ba2013-03-28 13:23:13 +0000309 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
310 ReadBlobByte(image));
cristy98140912011-04-20 14:11:45 +0000311 pixel.green=pixel.red;
312 pixel.blue=pixel.red;
313 break;
cristy3ed852e2009-09-05 21:47:34 +0000314 }
cristy98140912011-04-20 14:11:45 +0000315 case 15:
316 case 16:
317 {
318 QuantumAny
319 range;
320
321 /*
322 5 bits each of red green and blue.
323 */
324 j=(unsigned char) ReadBlobByte(image);
325 k=(unsigned char) ReadBlobByte(image);
326 range=GetQuantumRange(5UL);
cristy77b33ba2013-03-28 13:23:13 +0000327 pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
328 range);
329 pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*(k & 0x03)
330 << 3)+(1UL*(j & 0xe0) >> 5),range);
331 pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
cristy98140912011-04-20 14:11:45 +0000332 break;
333 }
334 case 24:
cristy98140912011-04-20 14:11:45 +0000335 {
336 /*
337 8 bits each of blue, green and red.
338 */
cristy77b33ba2013-03-28 13:23:13 +0000339 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
340 ReadBlobByte(image));
341 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
342 ReadBlobByte(image));
343 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
344 ReadBlobByte(image));
cristy98140912011-04-20 14:11:45 +0000345 break;
346 }
cristyba213402013-06-28 14:05:12 +0000347 case 32:
348 {
349 /*
350 8 bits each of blue, green, red, and alpha.
351 */
352 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
353 ReadBlobByte(image));
354 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
355 ReadBlobByte(image));
356 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
357 ReadBlobByte(image));
358 pixel.alpha=(MagickRealType) ScaleCharToQuantum((unsigned char)
359 ReadBlobByte(image));
360 break;
361 }
cristy98140912011-04-20 14:11:45 +0000362 }
363 image->colormap[i]=pixel;
364 }
365 }
366 /*
367 Convert TGA pixels to pixel packets.
368 */
369 base=0;
370 flag=0;
371 skip=MagickFalse;
372 real=0;
cristy4c08aed2011-07-01 19:47:50 +0000373 index=0;
cristy98140912011-04-20 14:11:45 +0000374 runlength=0;
375 offset=0;
376 for (y=0; y < (ssize_t) image->rows; y++)
377 {
378 real=offset;
379 if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
380 real=image->rows-real-1;
381 q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000382 if (q == (Quantum *) NULL)
cristy98140912011-04-20 14:11:45 +0000383 break;
cristy98140912011-04-20 14:11:45 +0000384 for (x=0; x < (ssize_t) image->columns; x++)
385 {
386 if ((tga_info.image_type == TGARLEColormap) ||
387 (tga_info.image_type == TGARLERGB) ||
388 (tga_info.image_type == TGARLEMonochrome))
389 {
390 if (runlength != 0)
391 {
392 runlength--;
393 skip=flag != 0;
394 }
395 else
396 {
397 count=ReadBlob(image,1,&runlength);
398 if (count == 0)
399 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
400 flag=runlength & 0x80;
401 if (flag != 0)
402 runlength-=128;
403 skip=MagickFalse;
404 }
405 }
406 if (skip == MagickFalse)
407 switch (tga_info.bits_per_pixel)
408 {
409 case 8:
410 default:
411 {
412 /*
413 Gray scale.
414 */
cristy4c08aed2011-07-01 19:47:50 +0000415 index=(Quantum) ReadBlobByte(image);
cristy98140912011-04-20 14:11:45 +0000416 if (tga_info.colormap_type != 0)
417 pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
cristyc82a27b2011-10-21 01:07:16 +0000418 1UL*index,exception)];
cristy98140912011-04-20 14:11:45 +0000419 else
420 {
cristy77b33ba2013-03-28 13:23:13 +0000421 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
422 index);
423 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
424 index);
425 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
426 index);
cristy98140912011-04-20 14:11:45 +0000427 }
428 break;
429 }
430 case 15:
431 case 16:
432 {
433 QuantumAny
434 range;
435
436 /*
cristy5dd83a52011-11-24 01:54:17 +0000437 5 bits each of RGB.
cristy98140912011-04-20 14:11:45 +0000438 */
cristy5dd83a52011-11-24 01:54:17 +0000439 if (ReadBlob(image,2,pixels) != 2)
440 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
441 j=pixels[0];
442 k=pixels[1];
cristy98140912011-04-20 14:11:45 +0000443 range=GetQuantumRange(5UL);
cristy77b33ba2013-03-28 13:23:13 +0000444 pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
445 range);
446 pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*(k & 0x03)
447 << 3)+(1UL*(j & 0xe0) >> 5),range);
448 pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
cristy8a46d822012-08-28 23:32:39 +0000449 if (image->alpha_trait == BlendPixelTrait)
cristy77b33ba2013-03-28 13:23:13 +0000450 pixel.alpha=(MagickRealType) ((k & 0x80) == 0 ? (Quantum)
451 OpaqueAlpha : (Quantum) TransparentAlpha);
cristy98140912011-04-20 14:11:45 +0000452 if (image->storage_class == PseudoClass)
cristyc82a27b2011-10-21 01:07:16 +0000453 index=ConstrainColormapIndex(image,((size_t) k << 8)+j,exception);
cristy98140912011-04-20 14:11:45 +0000454 break;
455 }
456 case 24:
cristy5dd83a52011-11-24 01:54:17 +0000457 {
458 /*
459 BGR pixels.
460 */
461 if (ReadBlob(image,3,pixels) != 3)
462 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
cristy77b33ba2013-03-28 13:23:13 +0000463 pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
464 pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
465 pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
cristy5dd83a52011-11-24 01:54:17 +0000466 break;
467 }
cristy98140912011-04-20 14:11:45 +0000468 case 32:
469 {
470 /*
cristy5dd83a52011-11-24 01:54:17 +0000471 BGRA pixels.
cristy98140912011-04-20 14:11:45 +0000472 */
cristy5dd83a52011-11-24 01:54:17 +0000473 if (ReadBlob(image,4,pixels) != 4)
474 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
cristy77b33ba2013-03-28 13:23:13 +0000475 pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
476 pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
477 pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
478 pixel.alpha=(MagickRealType) ScaleCharToQuantum(pixels[3]);
cristy98140912011-04-20 14:11:45 +0000479 break;
480 }
481 }
482 if (status == MagickFalse)
483 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
484 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000485 SetPixelIndex(image,index,q);
cristy77b33ba2013-03-28 13:23:13 +0000486 SetPixelRed(image,ClampToQuantum(pixel.red),q);
487 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
488 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
cristy8a46d822012-08-28 23:32:39 +0000489 if (image->alpha_trait == BlendPixelTrait)
cristy77b33ba2013-03-28 13:23:13 +0000490 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +0000491 q+=GetPixelChannels(image);
cristy98140912011-04-20 14:11:45 +0000492 }
493 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
494 offset+=4;
495 else
496 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
497 offset+=2;
498 else
499 offset++;
500 if (offset >= image->rows)
501 {
502 base++;
503 offset=base;
504 }
505 if (SyncAuthenticPixels(image,exception) == MagickFalse)
506 break;
507 if (image->previous == (Image *) NULL)
508 {
509 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
510 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000511 if (status == MagickFalse)
512 break;
513 }
cristy98140912011-04-20 14:11:45 +0000514 }
515 if (EOFBlob(image) != MagickFalse)
516 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
517 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000518 (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%
cristybb503372010-05-27 20:51:26 +0000542% size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000543%
544*/
cristybb503372010-05-27 20:51:26 +0000545ModuleExport size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000546{
547 MagickInfo
548 *entry;
549
550 entry=SetMagickInfo("ICB");
551 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
552 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
cristy98140912011-04-20 14:11:45 +0000553 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000554 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;
cristy98140912011-04-20 14:11:45 +0000560 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000561 entry->description=ConstantString("Truevision Targa image");
562 entry->module=ConstantString("TGA");
563 (void) RegisterMagickInfo(entry);
564 entry=SetMagickInfo("VDA");
565 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
566 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
cristy98140912011-04-20 14:11:45 +0000567 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000568 entry->description=ConstantString("Truevision Targa image");
569 entry->module=ConstantString("TGA");
570 (void) RegisterMagickInfo(entry);
571 entry=SetMagickInfo("VST");
572 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
573 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
cristy98140912011-04-20 14:11:45 +0000574 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000575 entry->description=ConstantString("Truevision Targa image");
576 entry->module=ConstantString("TGA");
577 (void) RegisterMagickInfo(entry);
578 return(MagickImageCoderSignature);
579}
580
581/*
582%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583% %
584% %
585% %
586% U n r e g i s t e r T G A I m a g e %
587% %
588% %
589% %
590%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
591%
592% UnregisterTGAImage() removes format registrations made by the
593% TGA module from the list of supported formats.
594%
595% The format of the UnregisterTGAImage method is:
596%
597% UnregisterTGAImage(void)
598%
599*/
600ModuleExport void UnregisterTGAImage(void)
601{
602 (void) UnregisterMagickInfo("ICB");
603 (void) UnregisterMagickInfo("TGA");
604 (void) UnregisterMagickInfo("VDA");
605 (void) UnregisterMagickInfo("VST");
606}
607
608/*
609%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
610% %
611% %
612% %
613% W r i t e T G A I m a g e %
614% %
615% %
616% %
617%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
618%
619% WriteTGAImage() writes a image in the Truevision Targa rasterfile
620% format.
621%
622% The format of the WriteTGAImage method is:
623%
cristy3a37efd2011-08-28 20:31:03 +0000624% MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
625% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000626%
627% A description of each parameter follows.
628%
629% o image_info: the image info.
630%
631% o image: The image.
632%
633*/
634
635static inline size_t MagickMin(const size_t x,const size_t y)
636{
637 if (x < y)
638 return(x);
639 return(y);
640}
641
cristy3a37efd2011-08-28 20:31:03 +0000642static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
643 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000644{
645#define TargaColormap 1
646#define TargaRGB 2
647#define TargaMonochrome 3
648#define TargaRLEColormap 9
649#define TargaRLERGB 10
650#define TargaRLEMonochrome 11
651
652 typedef struct _TargaInfo
653 {
654 unsigned char
655 id_length,
656 colormap_type,
657 image_type;
658
659 unsigned short
660 colormap_index,
661 colormap_length;
662
663 unsigned char
664 colormap_size;
665
666 unsigned short
667 x_origin,
668 y_origin,
669 width,
670 height;
671
672 unsigned char
673 bits_per_pixel,
674 attributes;
675 } TargaInfo;
676
677 const char
678 *value;
679
cristy3ed852e2009-09-05 21:47:34 +0000680 MagickBooleanType
681 status;
682
cristy4c08aed2011-07-01 19:47:50 +0000683 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000684 *p;
685
cristybb503372010-05-27 20:51:26 +0000686 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000687 x;
688
cristybb503372010-05-27 20:51:26 +0000689 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000690 i;
691
692 register unsigned char
693 *q;
694
695 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000696 count,
697 y;
cristy3ed852e2009-09-05 21:47:34 +0000698
699 TargaInfo
700 targa_info;
701
702 unsigned char
703 *targa_pixels;
704
705 /*
706 Open output image file.
707 */
708 assert(image_info != (const ImageInfo *) NULL);
709 assert(image_info->signature == MagickSignature);
710 assert(image != (Image *) NULL);
711 assert(image->signature == MagickSignature);
712 if (image->debug != MagickFalse)
713 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000714 assert(exception != (ExceptionInfo *) NULL);
715 assert(exception->signature == MagickSignature);
716 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000717 if (status == MagickFalse)
718 return(status);
cristy98140912011-04-20 14:11:45 +0000719 /*
720 Initialize TGA raster file header.
721 */
722 if ((image->columns > 65535L) || (image->rows > 65535L))
723 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
cristy3d9f5ba2012-06-26 13:37:31 +0000724 if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
cristy8d951092012-02-08 18:54:56 +0000725 (void) TransformImageColorspace(image,sRGBColorspace,exception);
cristy98140912011-04-20 14:11:45 +0000726 targa_info.id_length=0;
cristyd15e6592011-10-15 00:13:06 +0000727 value=GetImageProperty(image,"comment",exception);
cristy98140912011-04-20 14:11:45 +0000728 if (value != (const char *) NULL)
729 targa_info.id_length=(unsigned char) MagickMin(strlen(value),255);
730 targa_info.colormap_type=0;
731 targa_info.colormap_index=0;
732 targa_info.colormap_length=0;
733 targa_info.colormap_size=0;
734 targa_info.x_origin=0;
735 targa_info.y_origin=0;
736 targa_info.width=(unsigned short) image->columns;
737 targa_info.height=(unsigned short) image->rows;
738 targa_info.bits_per_pixel=8;
739 targa_info.attributes=0;
740 if ((image_info->type != TrueColorType) &&
741 (image_info->type != TrueColorMatteType) &&
742 (image_info->type != PaletteType) &&
cristy8a46d822012-08-28 23:32:39 +0000743 (image->alpha_trait != BlendPixelTrait) &&
cristy3a37efd2011-08-28 20:31:03 +0000744 (IsImageGray(image,exception) != MagickFalse))
cristy98140912011-04-20 14:11:45 +0000745 targa_info.image_type=TargaMonochrome;
746 else
747 if ((image->storage_class == DirectClass) || (image->colors > 256))
cristy3ed852e2009-09-05 21:47:34 +0000748 {
cristy3ed852e2009-09-05 21:47:34 +0000749 /*
cristy98140912011-04-20 14:11:45 +0000750 Full color TGA raster.
cristy3ed852e2009-09-05 21:47:34 +0000751 */
cristy98140912011-04-20 14:11:45 +0000752 targa_info.image_type=TargaRGB;
753 targa_info.bits_per_pixel=24;
cristy8a46d822012-08-28 23:32:39 +0000754 if (image->alpha_trait == BlendPixelTrait)
cristy98140912011-04-20 14:11:45 +0000755 {
756 targa_info.bits_per_pixel=32;
757 targa_info.attributes=8; /* # of alpha bits */
758 }
cristy3ed852e2009-09-05 21:47:34 +0000759 }
cristy98140912011-04-20 14:11:45 +0000760 else
cristy3ed852e2009-09-05 21:47:34 +0000761 {
cristy98140912011-04-20 14:11:45 +0000762 /*
763 Colormapped TGA raster.
764 */
765 targa_info.image_type=TargaColormap;
766 targa_info.colormap_type=1;
767 targa_info.colormap_length=(unsigned short) image->colors;
768 targa_info.colormap_size=24;
cristy3ed852e2009-09-05 21:47:34 +0000769 }
cristy98140912011-04-20 14:11:45 +0000770 /*
771 Write TGA header.
772 */
773 (void) WriteBlobByte(image,targa_info.id_length);
774 (void) WriteBlobByte(image,targa_info.colormap_type);
775 (void) WriteBlobByte(image,targa_info.image_type);
776 (void) WriteBlobLSBShort(image,targa_info.colormap_index);
777 (void) WriteBlobLSBShort(image,targa_info.colormap_length);
778 (void) WriteBlobByte(image,targa_info.colormap_size);
779 (void) WriteBlobLSBShort(image,targa_info.x_origin);
780 (void) WriteBlobLSBShort(image,targa_info.y_origin);
781 (void) WriteBlobLSBShort(image,targa_info.width);
782 (void) WriteBlobLSBShort(image,targa_info.height);
783 (void) WriteBlobByte(image,targa_info.bits_per_pixel);
784 (void) WriteBlobByte(image,targa_info.attributes);
785 if (targa_info.id_length != 0)
786 (void) WriteBlob(image,targa_info.id_length,(unsigned char *)
787 value);
788 if (targa_info.image_type == TargaColormap)
789 {
790 unsigned char
791 *targa_colormap;
792
793 /*
794 Dump colormap to file (blue, green, red byte order).
795 */
796 targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
797 targa_info.colormap_length,3UL*sizeof(*targa_colormap));
798 if (targa_colormap == (unsigned char *) NULL)
799 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
800 q=targa_colormap;
801 for (i=0; i < (ssize_t) image->colors; i++)
802 {
cristy77b33ba2013-03-28 13:23:13 +0000803 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
804 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
805 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
cristy98140912011-04-20 14:11:45 +0000806 }
807 (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length),
808 targa_colormap);
809 targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
cristy3ed852e2009-09-05 21:47:34 +0000810 }
cristy98140912011-04-20 14:11:45 +0000811 /*
812 Convert MIFF to TGA raster pixels.
813 */
814 count=(ssize_t) (targa_info.bits_per_pixel*targa_info.width)/8;
815 targa_pixels=(unsigned char *) AcquireQuantumMemory((size_t) count,
816 sizeof(*targa_pixels));
817 if (targa_pixels == (unsigned char *) NULL)
818 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
819 for (y=(ssize_t) (image->rows-1); y >= 0; y--)
820 {
cristy3a37efd2011-08-28 20:31:03 +0000821 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000822 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000823 break;
cristy98140912011-04-20 14:11:45 +0000824 q=targa_pixels;
cristy98140912011-04-20 14:11:45 +0000825 for (x=0; x < (ssize_t) image->columns; x++)
826 {
827 if (targa_info.image_type == TargaColormap)
cristy4c08aed2011-07-01 19:47:50 +0000828 *q++=(unsigned char) GetPixelIndex(image,p);
cristy98140912011-04-20 14:11:45 +0000829 else
830 if (targa_info.image_type == TargaMonochrome)
cristy77b33ba2013-03-28 13:23:13 +0000831 *q++=(unsigned char) ScaleQuantumToChar((ClampToQuantum(
cristyd0323222013-04-07 16:13:21 +0000832 GetPixelLuma(image,p))));
cristy98140912011-04-20 14:11:45 +0000833 else
834 {
cristy4c08aed2011-07-01 19:47:50 +0000835 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
836 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
837 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
cristy8a46d822012-08-28 23:32:39 +0000838 if (image->alpha_trait == BlendPixelTrait)
cristy4c08aed2011-07-01 19:47:50 +0000839 *q++=(unsigned char) ScaleQuantumToChar(GetPixelAlpha(image,p));
cristy98140912011-04-20 14:11:45 +0000840 }
cristyed231572011-07-14 02:18:59 +0000841 p+=GetPixelChannels(image);
cristy98140912011-04-20 14:11:45 +0000842 }
843 (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
844 if (image->previous == (Image *) NULL)
845 {
846 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000847 image->rows);
cristy98140912011-04-20 14:11:45 +0000848 if (status == MagickFalse)
849 break;
850 }
851 }
852 targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000853 (void) CloseBlob(image);
854 return(MagickTrue);
855}