blob: c2cbbfaefbf0258e9281f5a09fe08701a13f588d [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,
cristy5dd83a52011-11-24 01:54:17 +0000170 pixels[4],
cristy3ed852e2009-09-05 21:47:34 +0000171 runlength;
172
cristy3ed852e2009-09-05 21:47:34 +0000173 /*
174 Open image file.
175 */
176 assert(image_info != (const ImageInfo *) NULL);
177 assert(image_info->signature == MagickSignature);
178 if (image_info->debug != MagickFalse)
179 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
180 image_info->filename);
181 assert(exception != (ExceptionInfo *) NULL);
182 assert(exception->signature == MagickSignature);
cristy9950d572011-10-01 18:22:35 +0000183 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000184 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
185 if (status == MagickFalse)
186 {
187 image=DestroyImageList(image);
188 return((Image *) NULL);
189 }
190 /*
191 Read TGA header information.
192 */
193 count=ReadBlob(image,1,&tga_info.id_length);
194 tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
195 tga_info.image_type=(unsigned char) ReadBlobByte(image);
cristy98140912011-04-20 14:11:45 +0000196 if ((count != 1) ||
197 ((tga_info.image_type != TGAColormap) &&
198 (tga_info.image_type != TGARGB) &&
199 (tga_info.image_type != TGAMonochrome) &&
200 (tga_info.image_type != TGARLEColormap) &&
201 (tga_info.image_type != TGARLERGB) &&
202 (tga_info.image_type != TGARLEMonochrome)) ||
203 (((tga_info.image_type == TGAColormap) ||
204 (tga_info.image_type == TGARLEColormap)) &&
205 (tga_info.colormap_type == 0)))
206 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
207 tga_info.colormap_index=ReadBlobLSBShort(image);
208 tga_info.colormap_length=ReadBlobLSBShort(image);
209 tga_info.colormap_size=(unsigned char) ReadBlobByte(image);
210 tga_info.x_origin=ReadBlobLSBShort(image);
211 tga_info.y_origin=ReadBlobLSBShort(image);
212 tga_info.width=(unsigned short) ReadBlobLSBShort(image);
213 tga_info.height=(unsigned short) ReadBlobLSBShort(image);
214 tga_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
215 tga_info.attributes=(unsigned char) ReadBlobByte(image);
216 if (EOFBlob(image) != MagickFalse)
217 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
218 if ((((tga_info.bits_per_pixel <= 1) || (tga_info.bits_per_pixel >= 17)) &&
219 (tga_info.bits_per_pixel != 24) && (tga_info.bits_per_pixel != 32)))
220 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
221 /*
222 Initialize image structure.
223 */
224 image->columns=tga_info.width;
225 image->rows=tga_info.height;
226 image->matte=(tga_info.attributes & 0x0FU) != 0 ? MagickTrue : MagickFalse;
227 if ((tga_info.image_type != TGAColormap) &&
228 (tga_info.image_type != TGARLEColormap))
229 image->depth=(size_t) ((tga_info.bits_per_pixel <= 8) ? 8 :
230 (tga_info.bits_per_pixel <= 16) ? 5 :
231 (tga_info.bits_per_pixel == 24) ? 8 :
232 (tga_info.bits_per_pixel == 32) ? 8 : 8);
233 else
234 image->depth=(size_t) ((tga_info.colormap_size <= 8) ? 8 :
235 (tga_info.colormap_size <= 16) ? 5 :
236 (tga_info.colormap_size == 24) ? 8 :
237 (tga_info.colormap_size == 32) ? 8 : 8);
238 if ((tga_info.image_type == TGAColormap) ||
cristy3ed852e2009-09-05 21:47:34 +0000239 (tga_info.image_type == TGAMonochrome) ||
240 (tga_info.image_type == TGARLEColormap) ||
cristy98140912011-04-20 14:11:45 +0000241 (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 size_t
254 one;
255
256 one=1;
257 image->colors=one << tga_info.bits_per_pixel;
cristy018f07f2011-09-04 21:15:19 +0000258 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy98140912011-04-20 14:11:45 +0000259 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
260 }
261 }
262 if (tga_info.id_length != 0)
263 {
264 char
265 *comment;
266
267 size_t
268 length;
269
270 /*
271 TGA image comment.
272 */
273 length=(size_t) tga_info.id_length;
274 comment=(char *) NULL;
cristy37e0b382011-06-07 13:31:21 +0000275 if (~length >= (MaxTextExtent-1))
cristy98140912011-04-20 14:11:45 +0000276 comment=(char *) AcquireQuantumMemory(length+MaxTextExtent,
277 sizeof(*comment));
278 if (comment == (char *) NULL)
279 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
280 count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
281 comment[tga_info.id_length]='\0';
cristyd15e6592011-10-15 00:13:06 +0000282 (void) SetImageProperty(image,"comment",comment,exception);
cristy98140912011-04-20 14:11:45 +0000283 comment=DestroyString(comment);
284 }
285 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
cristy4c08aed2011-07-01 19:47:50 +0000286 pixel.alpha=(Quantum) OpaqueAlpha;
cristy98140912011-04-20 14:11:45 +0000287 if (tga_info.colormap_type != 0)
288 {
289 /*
290 Read TGA raster colormap.
291 */
cristy018f07f2011-09-04 21:15:19 +0000292 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy98140912011-04-20 14:11:45 +0000293 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
294 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000295 {
cristy98140912011-04-20 14:11:45 +0000296 switch (tga_info.colormap_size)
297 {
298 case 8:
299 default:
cristy3ed852e2009-09-05 21:47:34 +0000300 {
cristy98140912011-04-20 14:11:45 +0000301 /*
302 Gray scale.
303 */
304 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
305 pixel.green=pixel.red;
306 pixel.blue=pixel.red;
307 break;
cristy3ed852e2009-09-05 21:47:34 +0000308 }
cristy98140912011-04-20 14:11:45 +0000309 case 15:
310 case 16:
311 {
312 QuantumAny
313 range;
314
315 /*
316 5 bits each of red green and blue.
317 */
318 j=(unsigned char) ReadBlobByte(image);
319 k=(unsigned char) ReadBlobByte(image);
320 range=GetQuantumRange(5UL);
321 pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
322 pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
323 (1UL*(j & 0xe0) >> 5),range);
324 pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
325 break;
326 }
327 case 24:
328 case 32:
329 {
330 /*
331 8 bits each of blue, green and red.
332 */
333 pixel.blue=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
334 pixel.green=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
335 pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
336 break;
337 }
338 }
339 image->colormap[i]=pixel;
340 }
341 }
342 /*
343 Convert TGA pixels to pixel packets.
344 */
345 base=0;
346 flag=0;
347 skip=MagickFalse;
348 real=0;
cristy4c08aed2011-07-01 19:47:50 +0000349 index=0;
cristy98140912011-04-20 14:11:45 +0000350 runlength=0;
351 offset=0;
352 for (y=0; y < (ssize_t) image->rows; y++)
353 {
354 real=offset;
355 if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
356 real=image->rows-real-1;
357 q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000358 if (q == (Quantum *) NULL)
cristy98140912011-04-20 14:11:45 +0000359 break;
cristy98140912011-04-20 14:11:45 +0000360 for (x=0; x < (ssize_t) image->columns; x++)
361 {
362 if ((tga_info.image_type == TGARLEColormap) ||
363 (tga_info.image_type == TGARLERGB) ||
364 (tga_info.image_type == TGARLEMonochrome))
365 {
366 if (runlength != 0)
367 {
368 runlength--;
369 skip=flag != 0;
370 }
371 else
372 {
373 count=ReadBlob(image,1,&runlength);
374 if (count == 0)
375 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
376 flag=runlength & 0x80;
377 if (flag != 0)
378 runlength-=128;
379 skip=MagickFalse;
380 }
381 }
382 if (skip == MagickFalse)
383 switch (tga_info.bits_per_pixel)
384 {
385 case 8:
386 default:
387 {
388 /*
389 Gray scale.
390 */
cristy4c08aed2011-07-01 19:47:50 +0000391 index=(Quantum) ReadBlobByte(image);
cristy98140912011-04-20 14:11:45 +0000392 if (tga_info.colormap_type != 0)
393 pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
cristyc82a27b2011-10-21 01:07:16 +0000394 1UL*index,exception)];
cristy98140912011-04-20 14:11:45 +0000395 else
396 {
397 pixel.red=ScaleCharToQuantum((unsigned char) index);
398 pixel.green=ScaleCharToQuantum((unsigned char) index);
399 pixel.blue=ScaleCharToQuantum((unsigned char) index);
400 }
401 break;
402 }
403 case 15:
404 case 16:
405 {
406 QuantumAny
407 range;
408
409 /*
cristy5dd83a52011-11-24 01:54:17 +0000410 5 bits each of RGB.
cristy98140912011-04-20 14:11:45 +0000411 */
cristy5dd83a52011-11-24 01:54:17 +0000412 if (ReadBlob(image,2,pixels) != 2)
413 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
414 j=pixels[0];
415 k=pixels[1];
cristy98140912011-04-20 14:11:45 +0000416 range=GetQuantumRange(5UL);
417 pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
418 pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
419 (1UL*(j & 0xe0) >> 5),range);
420 pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
421 if (image->matte != MagickFalse)
cristy5dd83a52011-11-24 01:54:17 +0000422 pixel.alpha=(k & 0x80) == 0 ? (Quantum) OpaqueAlpha : (Quantum)
423 TransparentAlpha;
cristy98140912011-04-20 14:11:45 +0000424 if (image->storage_class == PseudoClass)
cristyc82a27b2011-10-21 01:07:16 +0000425 index=ConstrainColormapIndex(image,((size_t) k << 8)+j,exception);
cristy98140912011-04-20 14:11:45 +0000426 break;
427 }
428 case 24:
cristy5dd83a52011-11-24 01:54:17 +0000429 {
430 /*
431 BGR pixels.
432 */
433 if (ReadBlob(image,3,pixels) != 3)
434 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
435 pixel.blue=ScaleCharToQuantum(pixels[0]);
436 pixel.green=ScaleCharToQuantum(pixels[1]);
437 pixel.red=ScaleCharToQuantum(pixels[2]);
438 break;
439 }
cristy98140912011-04-20 14:11:45 +0000440 case 32:
441 {
442 /*
cristy5dd83a52011-11-24 01:54:17 +0000443 BGRA pixels.
cristy98140912011-04-20 14:11:45 +0000444 */
cristy5dd83a52011-11-24 01:54:17 +0000445 if (ReadBlob(image,4,pixels) != 4)
446 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
447 pixel.blue=ScaleCharToQuantum(pixels[0]);
448 pixel.green=ScaleCharToQuantum(pixels[1]);
449 pixel.red=ScaleCharToQuantum(pixels[2]);
450 pixel.alpha=ScaleCharToQuantum(pixels[3]);
cristy98140912011-04-20 14:11:45 +0000451 break;
452 }
453 }
454 if (status == MagickFalse)
455 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
456 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000457 SetPixelIndex(image,index,q);
458 SetPixelRed(image,pixel.red,q);
459 SetPixelGreen(image,pixel.green,q);
460 SetPixelBlue(image,pixel.blue,q);
cristy98140912011-04-20 14:11:45 +0000461 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000462 SetPixelAlpha(image,pixel.alpha,q);
cristyed231572011-07-14 02:18:59 +0000463 q+=GetPixelChannels(image);
cristy98140912011-04-20 14:11:45 +0000464 }
465 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
466 offset+=4;
467 else
468 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
469 offset+=2;
470 else
471 offset++;
472 if (offset >= image->rows)
473 {
474 base++;
475 offset=base;
476 }
477 if (SyncAuthenticPixels(image,exception) == MagickFalse)
478 break;
479 if (image->previous == (Image *) NULL)
480 {
481 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
482 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000483 if (status == MagickFalse)
484 break;
485 }
cristy98140912011-04-20 14:11:45 +0000486 }
487 if (EOFBlob(image) != MagickFalse)
488 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
489 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000490 (void) CloseBlob(image);
491 return(GetFirstImageInList(image));
492}
493
494/*
495%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496% %
497% %
498% %
499% R e g i s t e r T G A I m a g e %
500% %
501% %
502% %
503%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504%
505% RegisterTGAImage() adds properties for the TGA image format to
506% the list of supported formats. The properties include the image format
507% tag, a method to read and/or write the format, whether the format
508% supports the saving of more than one frame to the same file or blob,
509% whether the format supports native in-memory I/O, and a brief
510% description of the format.
511%
512% The format of the RegisterTGAImage method is:
513%
cristybb503372010-05-27 20:51:26 +0000514% size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000515%
516*/
cristybb503372010-05-27 20:51:26 +0000517ModuleExport size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000518{
519 MagickInfo
520 *entry;
521
522 entry=SetMagickInfo("ICB");
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("TGA");
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 entry=SetMagickInfo("VDA");
537 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
538 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
cristy98140912011-04-20 14:11:45 +0000539 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000540 entry->description=ConstantString("Truevision Targa image");
541 entry->module=ConstantString("TGA");
542 (void) RegisterMagickInfo(entry);
543 entry=SetMagickInfo("VST");
544 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
545 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
cristy98140912011-04-20 14:11:45 +0000546 entry->adjoin=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000547 entry->description=ConstantString("Truevision Targa image");
548 entry->module=ConstantString("TGA");
549 (void) RegisterMagickInfo(entry);
550 return(MagickImageCoderSignature);
551}
552
553/*
554%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
555% %
556% %
557% %
558% U n r e g i s t e r T G A I m a g e %
559% %
560% %
561% %
562%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
563%
564% UnregisterTGAImage() removes format registrations made by the
565% TGA module from the list of supported formats.
566%
567% The format of the UnregisterTGAImage method is:
568%
569% UnregisterTGAImage(void)
570%
571*/
572ModuleExport void UnregisterTGAImage(void)
573{
574 (void) UnregisterMagickInfo("ICB");
575 (void) UnregisterMagickInfo("TGA");
576 (void) UnregisterMagickInfo("VDA");
577 (void) UnregisterMagickInfo("VST");
578}
579
580/*
581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
582% %
583% %
584% %
585% W r i t e T G A I m a g e %
586% %
587% %
588% %
589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
590%
591% WriteTGAImage() writes a image in the Truevision Targa rasterfile
592% format.
593%
594% The format of the WriteTGAImage method is:
595%
cristy3a37efd2011-08-28 20:31:03 +0000596% MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
597% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000598%
599% A description of each parameter follows.
600%
601% o image_info: the image info.
602%
603% o image: The image.
604%
605*/
606
607static inline size_t MagickMin(const size_t x,const size_t y)
608{
609 if (x < y)
610 return(x);
611 return(y);
612}
613
cristy3a37efd2011-08-28 20:31:03 +0000614static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
615 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000616{
617#define TargaColormap 1
618#define TargaRGB 2
619#define TargaMonochrome 3
620#define TargaRLEColormap 9
621#define TargaRLERGB 10
622#define TargaRLEMonochrome 11
623
624 typedef struct _TargaInfo
625 {
626 unsigned char
627 id_length,
628 colormap_type,
629 image_type;
630
631 unsigned short
632 colormap_index,
633 colormap_length;
634
635 unsigned char
636 colormap_size;
637
638 unsigned short
639 x_origin,
640 y_origin,
641 width,
642 height;
643
644 unsigned char
645 bits_per_pixel,
646 attributes;
647 } TargaInfo;
648
649 const char
650 *value;
651
cristy3ed852e2009-09-05 21:47:34 +0000652 MagickBooleanType
653 status;
654
cristy4c08aed2011-07-01 19:47:50 +0000655 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000656 *p;
657
cristybb503372010-05-27 20:51:26 +0000658 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000659 x;
660
cristybb503372010-05-27 20:51:26 +0000661 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000662 i;
663
664 register unsigned char
665 *q;
666
667 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000668 count,
669 y;
cristy3ed852e2009-09-05 21:47:34 +0000670
671 TargaInfo
672 targa_info;
673
674 unsigned char
675 *targa_pixels;
676
677 /*
678 Open output image file.
679 */
680 assert(image_info != (const ImageInfo *) NULL);
681 assert(image_info->signature == MagickSignature);
682 assert(image != (Image *) NULL);
683 assert(image->signature == MagickSignature);
684 if (image->debug != MagickFalse)
685 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000686 assert(exception != (ExceptionInfo *) NULL);
687 assert(exception->signature == MagickSignature);
688 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000689 if (status == MagickFalse)
690 return(status);
cristy98140912011-04-20 14:11:45 +0000691 /*
692 Initialize TGA raster file header.
693 */
694 if ((image->columns > 65535L) || (image->rows > 65535L))
695 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
cristy510d06a2011-07-06 23:43:54 +0000696 if (IsRGBColorspace(image->colorspace) == MagickFalse)
cristye941a752011-10-15 01:52:48 +0000697 (void) TransformImageColorspace(image,RGBColorspace,exception);
cristy98140912011-04-20 14:11:45 +0000698 targa_info.id_length=0;
cristyd15e6592011-10-15 00:13:06 +0000699 value=GetImageProperty(image,"comment",exception);
cristy98140912011-04-20 14:11:45 +0000700 if (value != (const char *) NULL)
701 targa_info.id_length=(unsigned char) MagickMin(strlen(value),255);
702 targa_info.colormap_type=0;
703 targa_info.colormap_index=0;
704 targa_info.colormap_length=0;
705 targa_info.colormap_size=0;
706 targa_info.x_origin=0;
707 targa_info.y_origin=0;
708 targa_info.width=(unsigned short) image->columns;
709 targa_info.height=(unsigned short) image->rows;
710 targa_info.bits_per_pixel=8;
711 targa_info.attributes=0;
712 if ((image_info->type != TrueColorType) &&
713 (image_info->type != TrueColorMatteType) &&
714 (image_info->type != PaletteType) &&
715 (image->matte == MagickFalse) &&
cristy3a37efd2011-08-28 20:31:03 +0000716 (IsImageGray(image,exception) != MagickFalse))
cristy98140912011-04-20 14:11:45 +0000717 targa_info.image_type=TargaMonochrome;
718 else
719 if ((image->storage_class == DirectClass) || (image->colors > 256))
cristy3ed852e2009-09-05 21:47:34 +0000720 {
cristy3ed852e2009-09-05 21:47:34 +0000721 /*
cristy98140912011-04-20 14:11:45 +0000722 Full color TGA raster.
cristy3ed852e2009-09-05 21:47:34 +0000723 */
cristy98140912011-04-20 14:11:45 +0000724 targa_info.image_type=TargaRGB;
725 targa_info.bits_per_pixel=24;
726 if (image->matte != MagickFalse)
727 {
728 targa_info.bits_per_pixel=32;
729 targa_info.attributes=8; /* # of alpha bits */
730 }
cristy3ed852e2009-09-05 21:47:34 +0000731 }
cristy98140912011-04-20 14:11:45 +0000732 else
cristy3ed852e2009-09-05 21:47:34 +0000733 {
cristy98140912011-04-20 14:11:45 +0000734 /*
735 Colormapped TGA raster.
736 */
737 targa_info.image_type=TargaColormap;
738 targa_info.colormap_type=1;
739 targa_info.colormap_length=(unsigned short) image->colors;
740 targa_info.colormap_size=24;
cristy3ed852e2009-09-05 21:47:34 +0000741 }
cristy98140912011-04-20 14:11:45 +0000742 /*
743 Write TGA header.
744 */
745 (void) WriteBlobByte(image,targa_info.id_length);
746 (void) WriteBlobByte(image,targa_info.colormap_type);
747 (void) WriteBlobByte(image,targa_info.image_type);
748 (void) WriteBlobLSBShort(image,targa_info.colormap_index);
749 (void) WriteBlobLSBShort(image,targa_info.colormap_length);
750 (void) WriteBlobByte(image,targa_info.colormap_size);
751 (void) WriteBlobLSBShort(image,targa_info.x_origin);
752 (void) WriteBlobLSBShort(image,targa_info.y_origin);
753 (void) WriteBlobLSBShort(image,targa_info.width);
754 (void) WriteBlobLSBShort(image,targa_info.height);
755 (void) WriteBlobByte(image,targa_info.bits_per_pixel);
756 (void) WriteBlobByte(image,targa_info.attributes);
757 if (targa_info.id_length != 0)
758 (void) WriteBlob(image,targa_info.id_length,(unsigned char *)
759 value);
760 if (targa_info.image_type == TargaColormap)
761 {
762 unsigned char
763 *targa_colormap;
764
765 /*
766 Dump colormap to file (blue, green, red byte order).
767 */
768 targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
769 targa_info.colormap_length,3UL*sizeof(*targa_colormap));
770 if (targa_colormap == (unsigned char *) NULL)
771 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
772 q=targa_colormap;
773 for (i=0; i < (ssize_t) image->colors; i++)
774 {
775 *q++=ScaleQuantumToChar(image->colormap[i].blue);
776 *q++=ScaleQuantumToChar(image->colormap[i].green);
777 *q++=ScaleQuantumToChar(image->colormap[i].red);
778 }
779 (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length),
780 targa_colormap);
781 targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
cristy3ed852e2009-09-05 21:47:34 +0000782 }
cristy98140912011-04-20 14:11:45 +0000783 /*
784 Convert MIFF to TGA raster pixels.
785 */
786 count=(ssize_t) (targa_info.bits_per_pixel*targa_info.width)/8;
787 targa_pixels=(unsigned char *) AcquireQuantumMemory((size_t) count,
788 sizeof(*targa_pixels));
789 if (targa_pixels == (unsigned char *) NULL)
790 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
791 for (y=(ssize_t) (image->rows-1); y >= 0; y--)
792 {
cristy3a37efd2011-08-28 20:31:03 +0000793 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000794 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000795 break;
cristy98140912011-04-20 14:11:45 +0000796 q=targa_pixels;
cristy98140912011-04-20 14:11:45 +0000797 for (x=0; x < (ssize_t) image->columns; x++)
798 {
799 if (targa_info.image_type == TargaColormap)
cristy4c08aed2011-07-01 19:47:50 +0000800 *q++=(unsigned char) GetPixelIndex(image,p);
cristy98140912011-04-20 14:11:45 +0000801 else
802 if (targa_info.image_type == TargaMonochrome)
cristy4c08aed2011-07-01 19:47:50 +0000803 *q++=(unsigned char) ScaleQuantumToChar(GetPixelIntensity(image,p));
cristy98140912011-04-20 14:11:45 +0000804 else
805 {
cristy4c08aed2011-07-01 19:47:50 +0000806 *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
807 *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
808 *q++=ScaleQuantumToChar(GetPixelRed(image,p));
cristy98140912011-04-20 14:11:45 +0000809 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000810 *q++=(unsigned char) ScaleQuantumToChar(GetPixelAlpha(image,p));
cristy98140912011-04-20 14:11:45 +0000811 }
cristyed231572011-07-14 02:18:59 +0000812 p+=GetPixelChannels(image);
cristy98140912011-04-20 14:11:45 +0000813 }
814 (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
815 if (image->previous == (Image *) NULL)
816 {
817 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000818 image->rows);
cristy98140912011-04-20 14:11:45 +0000819 if (status == MagickFalse)
820 break;
821 }
822 }
823 targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
cristy3ed852e2009-09-05 21:47:34 +0000824 (void) CloseBlob(image);
825 return(MagickTrue);
826}