blob: a9e82b00e4d9575df73da5e5c3bd03be83905e5c [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 %
cristyde984cd2013-12-01 14:49:27 +000016% Cristy %
cristy3ed852e2009-09-05 21:47:34 +000017% July 1992 %
18% %
19% %
Cristy7ce65e72015-12-12 18:03:16 -050020% Copyright 1999-2016 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"
dirk9ac19552015-09-13 09:30:49 +020043#include "MagickCore/artifact.h"
cristy4c08aed2011-07-01 19:47:50 +000044#include "MagickCore/attribute.h"
45#include "MagickCore/blob.h"
46#include "MagickCore/blob-private.h"
47#include "MagickCore/cache.h"
48#include "MagickCore/color-private.h"
49#include "MagickCore/colormap.h"
50#include "MagickCore/colormap-private.h"
51#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +000052#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +000053#include "MagickCore/exception.h"
54#include "MagickCore/exception-private.h"
55#include "MagickCore/image.h"
56#include "MagickCore/image-private.h"
57#include "MagickCore/list.h"
58#include "MagickCore/magick.h"
59#include "MagickCore/memory_.h"
60#include "MagickCore/monitor.h"
61#include "MagickCore/monitor-private.h"
dirk9ac19552015-09-13 09:30:49 +020062#include "MagickCore/option.h"
cristy4c08aed2011-07-01 19:47:50 +000063#include "MagickCore/pixel-accessor.h"
64#include "MagickCore/property.h"
65#include "MagickCore/quantum-private.h"
66#include "MagickCore/static.h"
67#include "MagickCore/string_.h"
68#include "MagickCore/module.h"
dirk75652a82014-05-12 19:05:56 +000069
70/*
71 Enumerated declaractions.
72*/
73typedef enum
74{
75 TGAColormap = 1,
76 TGARGB = 2,
77 TGAMonochrome = 3,
78 TGARLEColormap = 9,
79 TGARLERGB = 10,
80 TGARLEMonochrome = 11
81} TGAImageType;
82
83/*
84 Typedef declaractions.
85*/
86typedef struct _TGAInfo
87{
88 TGAImageType
89 image_type;
90
91 unsigned char
92 id_length,
93 colormap_type;
94
95 unsigned short
96 colormap_index,
97 colormap_length;
98
99 unsigned char
100 colormap_size;
101
102 unsigned short
103 x_origin,
104 y_origin,
105 width,
106 height;
107
108 unsigned char
109 bits_per_pixel,
110 attributes;
111} TGAInfo;
cristy3ed852e2009-09-05 21:47:34 +0000112
113/*
114 Forward declarations.
115*/
116static MagickBooleanType
cristy3a37efd2011-08-28 20:31:03 +0000117 WriteTGAImage(const ImageInfo *,Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000118
119/*
120%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121% %
122% %
123% %
124% R e a d T G A I m a g e %
125% %
126% %
127% %
128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129%
130% ReadTGAImage() reads a Truevision TGA image file and returns it.
131% It allocates the memory necessary for the new Image structure and returns
132% a pointer to the new image.
133%
134% The format of the ReadTGAImage method is:
135%
136% Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
137%
138% A description of each parameter follows:
139%
140% o image_info: the image info.
141%
142% o exception: return any errors or warnings in this structure.
143%
144*/
dirk75652a82014-05-12 19:05:56 +0000145static Image *ReadTGAImage(const ImageInfo *image_info,
146 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000147{
cristy3ed852e2009-09-05 21:47:34 +0000148 Image
149 *image;
150
cristy3ed852e2009-09-05 21:47:34 +0000151 MagickBooleanType
152 status;
153
cristy101ab702011-10-13 13:06:32 +0000154 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000155 pixel;
156
cristy4c08aed2011-07-01 19:47:50 +0000157 Quantum
158 index;
cristy3ed852e2009-09-05 21:47:34 +0000159
cristy4c08aed2011-07-01 19:47:50 +0000160 register Quantum
cristyc6da28e2011-04-28 01:41:35 +0000161 *q;
162
cristybb503372010-05-27 20:51:26 +0000163 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000164 i,
165 x;
166
cristyc6da28e2011-04-28 01:41:35 +0000167 size_t
168 base,
169 flag,
170 offset,
171 real,
172 skip;
cristy3ed852e2009-09-05 21:47:34 +0000173
174 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000175 count,
176 y;
cristy3ed852e2009-09-05 21:47:34 +0000177
178 TGAInfo
179 tga_info;
180
181 unsigned char
182 j,
183 k,
cristy5dd83a52011-11-24 01:54:17 +0000184 pixels[4],
cristy3ed852e2009-09-05 21:47:34 +0000185 runlength;
186
cristy18307f12011-12-30 01:20:16 +0000187 unsigned int
188 alpha_bits;
189
cristy3ed852e2009-09-05 21:47:34 +0000190 /*
191 Open image file.
192 */
193 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000194 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000195 if (image_info->debug != MagickFalse)
196 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
197 image_info->filename);
198 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000199 assert(exception->signature == MagickCoreSignature);
cristy9950d572011-10-01 18:22:35 +0000200 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000201 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
202 if (status == MagickFalse)
203 {
204 image=DestroyImageList(image);
205 return((Image *) NULL);
206 }
207 /*
208 Read TGA header information.
209 */
210 count=ReadBlob(image,1,&tga_info.id_length);
211 tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
dirk75652a82014-05-12 19:05:56 +0000212 tga_info.image_type=(TGAImageType) ReadBlobByte(image);
cristy98140912011-04-20 14:11:45 +0000213 if ((count != 1) ||
214 ((tga_info.image_type != TGAColormap) &&
215 (tga_info.image_type != TGARGB) &&
216 (tga_info.image_type != TGAMonochrome) &&
217 (tga_info.image_type != TGARLEColormap) &&
218 (tga_info.image_type != TGARLERGB) &&
219 (tga_info.image_type != TGARLEMonochrome)) ||
220 (((tga_info.image_type == TGAColormap) ||
221 (tga_info.image_type == TGARLEColormap)) &&
222 (tga_info.colormap_type == 0)))
223 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
224 tga_info.colormap_index=ReadBlobLSBShort(image);
225 tga_info.colormap_length=ReadBlobLSBShort(image);
226 tga_info.colormap_size=(unsigned char) ReadBlobByte(image);
227 tga_info.x_origin=ReadBlobLSBShort(image);
228 tga_info.y_origin=ReadBlobLSBShort(image);
229 tga_info.width=(unsigned short) ReadBlobLSBShort(image);
230 tga_info.height=(unsigned short) ReadBlobLSBShort(image);
231 tga_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
232 tga_info.attributes=(unsigned char) ReadBlobByte(image);
233 if (EOFBlob(image) != MagickFalse)
234 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
235 if ((((tga_info.bits_per_pixel <= 1) || (tga_info.bits_per_pixel >= 17)) &&
236 (tga_info.bits_per_pixel != 24) && (tga_info.bits_per_pixel != 32)))
237 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
238 /*
239 Initialize image structure.
240 */
241 image->columns=tga_info.width;
242 image->rows=tga_info.height;
cristy18307f12011-12-30 01:20:16 +0000243 alpha_bits=(tga_info.attributes & 0x0FU);
cristyba213402013-06-28 14:05:12 +0000244 image->alpha_trait=(alpha_bits > 0) || (tga_info.bits_per_pixel == 32) ||
245 (tga_info.colormap_size == 32) ? BlendPixelTrait : UndefinedPixelTrait;
cristy98140912011-04-20 14:11:45 +0000246 if ((tga_info.image_type != TGAColormap) &&
247 (tga_info.image_type != TGARLEColormap))
248 image->depth=(size_t) ((tga_info.bits_per_pixel <= 8) ? 8 :
249 (tga_info.bits_per_pixel <= 16) ? 5 :
250 (tga_info.bits_per_pixel == 24) ? 8 :
251 (tga_info.bits_per_pixel == 32) ? 8 : 8);
252 else
253 image->depth=(size_t) ((tga_info.colormap_size <= 8) ? 8 :
254 (tga_info.colormap_size <= 16) ? 5 :
255 (tga_info.colormap_size == 24) ? 8 :
256 (tga_info.colormap_size == 32) ? 8 : 8);
257 if ((tga_info.image_type == TGAColormap) ||
cristy3ed852e2009-09-05 21:47:34 +0000258 (tga_info.image_type == TGAMonochrome) ||
259 (tga_info.image_type == TGARLEColormap) ||
cristy98140912011-04-20 14:11:45 +0000260 (tga_info.image_type == TGARLEMonochrome))
261 image->storage_class=PseudoClass;
262 image->compression=NoCompression;
263 if ((tga_info.image_type == TGARLEColormap) ||
dirk58ee16a2015-05-08 19:43:09 +0000264 (tga_info.image_type == TGARLEMonochrome) ||
265 (tga_info.image_type == TGARLERGB))
cristy98140912011-04-20 14:11:45 +0000266 image->compression=RLECompression;
267 if (image->storage_class == PseudoClass)
268 {
269 if (tga_info.colormap_type != 0)
cristyc9eb04c2014-12-20 20:34:41 +0000270 image->colors=tga_info.colormap_index+tga_info.colormap_length;
cristy98140912011-04-20 14:11:45 +0000271 else
272 {
273 size_t
274 one;
275
276 one=1;
277 image->colors=one << tga_info.bits_per_pixel;
cristy018f07f2011-09-04 21:15:19 +0000278 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy98140912011-04-20 14:11:45 +0000279 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
280 }
281 }
282 if (tga_info.id_length != 0)
283 {
284 char
285 *comment;
286
287 size_t
288 length;
289
290 /*
291 TGA image comment.
292 */
293 length=(size_t) tga_info.id_length;
294 comment=(char *) NULL;
cristy151b66d2015-04-15 10:50:31 +0000295 if (~length >= (MagickPathExtent-1))
296 comment=(char *) AcquireQuantumMemory(length+MagickPathExtent,
cristy98140912011-04-20 14:11:45 +0000297 sizeof(*comment));
298 if (comment == (char *) NULL)
299 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
300 count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
301 comment[tga_info.id_length]='\0';
cristyd15e6592011-10-15 00:13:06 +0000302 (void) SetImageProperty(image,"comment",comment,exception);
cristy98140912011-04-20 14:11:45 +0000303 comment=DestroyString(comment);
304 }
dirk9ac19552015-09-13 09:30:49 +0200305 if (tga_info.attributes & (1UL << 4))
306 {
307 if (tga_info.attributes & (1UL << 5))
308 SetImageArtifact(image,"tga:image-origin","TopRight");
309 else
310 SetImageArtifact(image,"tga:image-origin","BottomRight");
311 }
312 else
313 {
314 if (tga_info.attributes & (1UL << 5))
315 SetImageArtifact(image,"tga:image-origin","TopLeft");
316 else
317 SetImageArtifact(image,"tga:image-origin","BottomLeft");
318 }
dirke1b03882014-05-13 11:03:42 +0000319 if (image_info->ping != MagickFalse)
320 {
321 (void) CloseBlob(image);
322 return(image);
323 }
cristyacabb842014-12-14 23:36:33 +0000324 status=SetImageExtent(image,image->columns,image->rows,exception);
325 if (status == MagickFalse)
326 return(DestroyImageList(image));
cristy98140912011-04-20 14:11:45 +0000327 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
cristy77b33ba2013-03-28 13:23:13 +0000328 pixel.alpha=(MagickRealType) OpaqueAlpha;
cristy98140912011-04-20 14:11:45 +0000329 if (tga_info.colormap_type != 0)
330 {
331 /*
332 Read TGA raster colormap.
333 */
Cristy4f68e962015-08-31 16:19:40 -0400334 if (image->colors < tga_info.colormap_index)
335 image->colors=tga_info.colormap_index;
cristy018f07f2011-09-04 21:15:19 +0000336 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy98140912011-04-20 14:11:45 +0000337 ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
cristyc9eb04c2014-12-20 20:34:41 +0000338 for (i=0; i < (ssize_t) tga_info.colormap_index; i++)
339 image->colormap[i]=pixel;
340 for ( ; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000341 {
cristy98140912011-04-20 14:11:45 +0000342 switch (tga_info.colormap_size)
343 {
344 case 8:
345 default:
cristy3ed852e2009-09-05 21:47:34 +0000346 {
cristy98140912011-04-20 14:11:45 +0000347 /*
348 Gray scale.
349 */
cristy77b33ba2013-03-28 13:23:13 +0000350 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
351 ReadBlobByte(image));
cristy98140912011-04-20 14:11:45 +0000352 pixel.green=pixel.red;
353 pixel.blue=pixel.red;
354 break;
cristy3ed852e2009-09-05 21:47:34 +0000355 }
cristy98140912011-04-20 14:11:45 +0000356 case 15:
357 case 16:
358 {
359 QuantumAny
360 range;
361
362 /*
363 5 bits each of red green and blue.
364 */
365 j=(unsigned char) ReadBlobByte(image);
366 k=(unsigned char) ReadBlobByte(image);
367 range=GetQuantumRange(5UL);
cristy77b33ba2013-03-28 13:23:13 +0000368 pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
369 range);
370 pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*(k & 0x03)
371 << 3)+(1UL*(j & 0xe0) >> 5),range);
372 pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
cristy98140912011-04-20 14:11:45 +0000373 break;
374 }
375 case 24:
cristy98140912011-04-20 14:11:45 +0000376 {
377 /*
378 8 bits each of blue, green and red.
379 */
cristy77b33ba2013-03-28 13:23:13 +0000380 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
381 ReadBlobByte(image));
382 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
383 ReadBlobByte(image));
384 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
385 ReadBlobByte(image));
cristy98140912011-04-20 14:11:45 +0000386 break;
387 }
cristyba213402013-06-28 14:05:12 +0000388 case 32:
389 {
390 /*
391 8 bits each of blue, green, red, and alpha.
392 */
393 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
394 ReadBlobByte(image));
395 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
396 ReadBlobByte(image));
397 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
398 ReadBlobByte(image));
399 pixel.alpha=(MagickRealType) ScaleCharToQuantum((unsigned char)
400 ReadBlobByte(image));
401 break;
402 }
cristy98140912011-04-20 14:11:45 +0000403 }
404 image->colormap[i]=pixel;
405 }
406 }
407 /*
408 Convert TGA pixels to pixel packets.
409 */
410 base=0;
411 flag=0;
412 skip=MagickFalse;
413 real=0;
cristy4c08aed2011-07-01 19:47:50 +0000414 index=0;
cristy98140912011-04-20 14:11:45 +0000415 runlength=0;
416 offset=0;
417 for (y=0; y < (ssize_t) image->rows; y++)
418 {
419 real=offset;
420 if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
421 real=image->rows-real-1;
422 q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000423 if (q == (Quantum *) NULL)
cristy98140912011-04-20 14:11:45 +0000424 break;
cristy98140912011-04-20 14:11:45 +0000425 for (x=0; x < (ssize_t) image->columns; x++)
426 {
427 if ((tga_info.image_type == TGARLEColormap) ||
428 (tga_info.image_type == TGARLERGB) ||
429 (tga_info.image_type == TGARLEMonochrome))
430 {
431 if (runlength != 0)
432 {
433 runlength--;
434 skip=flag != 0;
435 }
436 else
437 {
438 count=ReadBlob(image,1,&runlength);
cristy771c8842015-01-09 12:13:22 +0000439 if (count != 1)
cristy98140912011-04-20 14:11:45 +0000440 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
441 flag=runlength & 0x80;
442 if (flag != 0)
443 runlength-=128;
444 skip=MagickFalse;
445 }
446 }
447 if (skip == MagickFalse)
448 switch (tga_info.bits_per_pixel)
449 {
450 case 8:
451 default:
452 {
453 /*
454 Gray scale.
455 */
cristy4c08aed2011-07-01 19:47:50 +0000456 index=(Quantum) ReadBlobByte(image);
cristy98140912011-04-20 14:11:45 +0000457 if (tga_info.colormap_type != 0)
458 pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
cristyc9eb04c2014-12-20 20:34:41 +0000459 (ssize_t) index,exception)];
cristy98140912011-04-20 14:11:45 +0000460 else
461 {
cristy77b33ba2013-03-28 13:23:13 +0000462 pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
463 index);
464 pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
465 index);
466 pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
467 index);
cristy98140912011-04-20 14:11:45 +0000468 }
469 break;
470 }
471 case 15:
472 case 16:
473 {
474 QuantumAny
475 range;
476
477 /*
cristy5dd83a52011-11-24 01:54:17 +0000478 5 bits each of RGB.
cristy98140912011-04-20 14:11:45 +0000479 */
cristy5dd83a52011-11-24 01:54:17 +0000480 if (ReadBlob(image,2,pixels) != 2)
481 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
482 j=pixels[0];
483 k=pixels[1];
cristy98140912011-04-20 14:11:45 +0000484 range=GetQuantumRange(5UL);
cristy77b33ba2013-03-28 13:23:13 +0000485 pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
486 range);
cristydfc3f852015-04-11 12:37:16 +0000487 pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*
488 (k & 0x03) << 3)+(1UL*(j & 0xe0) >> 5),range);
cristy77b33ba2013-03-28 13:23:13 +0000489 pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
cristy17f11b02014-12-20 19:37:04 +0000490 if (image->alpha_trait != UndefinedPixelTrait)
cristy77b33ba2013-03-28 13:23:13 +0000491 pixel.alpha=(MagickRealType) ((k & 0x80) == 0 ? (Quantum)
dirk0cfa9e92015-05-03 15:00:25 +0000492 TransparentAlpha : (Quantum) OpaqueAlpha);
cristy98140912011-04-20 14:11:45 +0000493 if (image->storage_class == PseudoClass)
cristyc9eb04c2014-12-20 20:34:41 +0000494 index=(Quantum) ConstrainColormapIndex(image,((ssize_t) (k << 8))+
495 j,exception);
cristy98140912011-04-20 14:11:45 +0000496 break;
497 }
498 case 24:
cristy5dd83a52011-11-24 01:54:17 +0000499 {
500 /*
501 BGR pixels.
502 */
503 if (ReadBlob(image,3,pixels) != 3)
504 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
cristy77b33ba2013-03-28 13:23:13 +0000505 pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
506 pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
507 pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
cristy5dd83a52011-11-24 01:54:17 +0000508 break;
509 }
cristy98140912011-04-20 14:11:45 +0000510 case 32:
511 {
512 /*
cristy5dd83a52011-11-24 01:54:17 +0000513 BGRA pixels.
cristy98140912011-04-20 14:11:45 +0000514 */
cristy5dd83a52011-11-24 01:54:17 +0000515 if (ReadBlob(image,4,pixels) != 4)
516 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
cristy77b33ba2013-03-28 13:23:13 +0000517 pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
518 pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
519 pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
520 pixel.alpha=(MagickRealType) ScaleCharToQuantum(pixels[3]);
cristy98140912011-04-20 14:11:45 +0000521 break;
522 }
523 }
524 if (status == MagickFalse)
525 ThrowReaderException(CorruptImageError,"UnableToReadImageData");
526 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000527 SetPixelIndex(image,index,q);
cristy77b33ba2013-03-28 13:23:13 +0000528 SetPixelRed(image,ClampToQuantum(pixel.red),q);
529 SetPixelGreen(image,ClampToQuantum(pixel.green),q);
530 SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
cristy17f11b02014-12-20 19:37:04 +0000531 if (image->alpha_trait != UndefinedPixelTrait)
cristy77b33ba2013-03-28 13:23:13 +0000532 SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
cristyed231572011-07-14 02:18:59 +0000533 q+=GetPixelChannels(image);
cristy98140912011-04-20 14:11:45 +0000534 }
cristycd56a382015-06-27 15:39:21 +0000535 /*
536 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
537 offset+=4;
538 else
539 */
cristyccdef732015-05-31 14:14:16 +0000540 if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
cristy98140912011-04-20 14:11:45 +0000541 offset+=2;
542 else
543 offset++;
544 if (offset >= image->rows)
545 {
546 base++;
547 offset=base;
548 }
549 if (SyncAuthenticPixels(image,exception) == MagickFalse)
550 break;
551 if (image->previous == (Image *) NULL)
552 {
553 status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
554 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000555 if (status == MagickFalse)
556 break;
557 }
cristy98140912011-04-20 14:11:45 +0000558 }
559 if (EOFBlob(image) != MagickFalse)
560 ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
561 image->filename);
cristy3ed852e2009-09-05 21:47:34 +0000562 (void) CloseBlob(image);
563 return(GetFirstImageInList(image));
564}
565
566/*
567%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568% %
569% %
570% %
571% R e g i s t e r T G A I m a g e %
572% %
573% %
574% %
575%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
576%
577% RegisterTGAImage() adds properties for the TGA image format to
578% the list of supported formats. The properties include the image format
579% tag, a method to read and/or write the format, whether the format
580% supports the saving of more than one frame to the same file or blob,
581% whether the format supports native in-memory I/O, and a brief
582% description of the format.
583%
584% The format of the RegisterTGAImage method is:
585%
cristybb503372010-05-27 20:51:26 +0000586% size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000587%
588*/
cristybb503372010-05-27 20:51:26 +0000589ModuleExport size_t RegisterTGAImage(void)
cristy3ed852e2009-09-05 21:47:34 +0000590{
591 MagickInfo
592 *entry;
593
dirk06b627a2015-04-06 18:59:17 +0000594 entry=AcquireMagickInfo("TGA","ICB","Truevision Targa image");
cristy3ed852e2009-09-05 21:47:34 +0000595 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
596 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
dirk08e9a112015-02-22 01:51:41 +0000597 entry->flags^=CoderAdjoinFlag;
cristy3ed852e2009-09-05 21:47:34 +0000598 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000599 entry=AcquireMagickInfo("TGA","TGA","Truevision Targa image");
cristy3ed852e2009-09-05 21:47:34 +0000600 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
601 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
dirk08e9a112015-02-22 01:51:41 +0000602 entry->flags^=CoderAdjoinFlag;
cristy3ed852e2009-09-05 21:47:34 +0000603 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000604 entry=AcquireMagickInfo("TGA","VDA","Truevision Targa image");
cristy3ed852e2009-09-05 21:47:34 +0000605 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
606 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
dirk08e9a112015-02-22 01:51:41 +0000607 entry->flags^=CoderAdjoinFlag;
cristy3ed852e2009-09-05 21:47:34 +0000608 (void) RegisterMagickInfo(entry);
dirk06b627a2015-04-06 18:59:17 +0000609 entry=AcquireMagickInfo("TGA","VST","Truevision Targa image");
cristy3ed852e2009-09-05 21:47:34 +0000610 entry->decoder=(DecodeImageHandler *) ReadTGAImage;
611 entry->encoder=(EncodeImageHandler *) WriteTGAImage;
dirk08e9a112015-02-22 01:51:41 +0000612 entry->flags^=CoderAdjoinFlag;
cristy3ed852e2009-09-05 21:47:34 +0000613 (void) RegisterMagickInfo(entry);
614 return(MagickImageCoderSignature);
615}
616
617/*
618%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
619% %
620% %
621% %
622% U n r e g i s t e r T G A I m a g e %
623% %
624% %
625% %
626%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
627%
628% UnregisterTGAImage() removes format registrations made by the
629% TGA module from the list of supported formats.
630%
631% The format of the UnregisterTGAImage method is:
632%
633% UnregisterTGAImage(void)
634%
635*/
636ModuleExport void UnregisterTGAImage(void)
637{
638 (void) UnregisterMagickInfo("ICB");
639 (void) UnregisterMagickInfo("TGA");
640 (void) UnregisterMagickInfo("VDA");
641 (void) UnregisterMagickInfo("VST");
642}
643
644/*
645%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646% %
647% %
648% %
649% W r i t e T G A I m a g e %
650% %
651% %
652% %
653%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
654%
655% WriteTGAImage() writes a image in the Truevision Targa rasterfile
656% format.
657%
658% The format of the WriteTGAImage method is:
659%
cristy3a37efd2011-08-28 20:31:03 +0000660% MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
661% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000662%
663% A description of each parameter follows.
664%
665% o image_info: the image info.
666%
667% o image: The image.
668%
669*/
dirk75652a82014-05-12 19:05:56 +0000670static inline void WriteTGAPixel(Image *image,TGAImageType image_type,
dirkf142f1f2014-12-16 09:48:03 +0000671 const Quantum *p,const QuantumAny range,const double midpoint)
dirk75652a82014-05-12 19:05:56 +0000672{
673 if (image_type == TGAColormap || image_type == TGARLEColormap)
674 (void) WriteBlobByte(image,(unsigned char) GetPixelIndex(image,p));
675 else
676 {
677 if (image_type == TGAMonochrome || image_type == TGARLEMonochrome)
678 (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
679 GetPixelLuma(image,p))));
680 else
cristydfc3f852015-04-11 12:37:16 +0000681 if (image->depth == 5)
682 {
683 unsigned char
684 green,
685 value;
686
687 green=(unsigned char) ScaleQuantumToAny(GetPixelGreen(image,p),
688 range);
689 value=((unsigned char) ScaleQuantumToAny(GetPixelBlue(image,p),
690 range)) | ((green & 0x07) << 5);
691 (void) WriteBlobByte(image,value);
692 value=(((image->alpha_trait != UndefinedPixelTrait) &&
dirk0cfa9e92015-05-03 15:00:25 +0000693 ((double) GetPixelAlpha(image,p) > midpoint)) ? 0x80 : 0) |
cristydfc3f852015-04-11 12:37:16 +0000694 ((unsigned char) ScaleQuantumToAny(GetPixelRed(image,p),range) <<
695 2) | ((green & 0x18) >> 3);
696 (void) WriteBlobByte(image,value);
697 }
698 else
699 {
700 (void) WriteBlobByte(image,ScaleQuantumToChar(
701 GetPixelBlue(image,p)));
702 (void) WriteBlobByte(image,ScaleQuantumToChar(
703 GetPixelGreen(image,p)));
704 (void) WriteBlobByte(image,ScaleQuantumToChar(
705 GetPixelRed(image,p)));
706 if (image->alpha_trait != UndefinedPixelTrait)
707 (void) WriteBlobByte(image,ScaleQuantumToChar(
708 GetPixelAlpha(image,p)));
709 }
dirk75652a82014-05-12 19:05:56 +0000710 }
711}
712
cristy3a37efd2011-08-28 20:31:03 +0000713static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
714 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000715{
dirk75652a82014-05-12 19:05:56 +0000716 CompressionType
717 compression;
cristy3ed852e2009-09-05 21:47:34 +0000718
719 const char
720 *value;
721
dirkf142f1f2014-12-16 09:48:03 +0000722 const double
cristydfc3f852015-04-11 12:37:16 +0000723 midpoint = QuantumRange/2.0;
dirkf142f1f2014-12-16 09:48:03 +0000724
cristy3ed852e2009-09-05 21:47:34 +0000725 MagickBooleanType
726 status;
727
dirkf142f1f2014-12-16 09:48:03 +0000728 QuantumAny
729 range;
730
cristy4c08aed2011-07-01 19:47:50 +0000731 register const Quantum
cristy3ed852e2009-09-05 21:47:34 +0000732 *p;
733
cristybb503372010-05-27 20:51:26 +0000734 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000735 x;
736
cristybb503372010-05-27 20:51:26 +0000737 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000738 i;
739
740 register unsigned char
741 *q;
742
dirk75652a82014-05-12 19:05:56 +0000743 size_t
744 channels;
745
cristy3ed852e2009-09-05 21:47:34 +0000746 ssize_t
cristyc6da28e2011-04-28 01:41:35 +0000747 count,
748 y;
cristy3ed852e2009-09-05 21:47:34 +0000749
dirk75652a82014-05-12 19:05:56 +0000750 TGAInfo
751 tga_info;
cristy3ed852e2009-09-05 21:47:34 +0000752
753 /*
754 Open output image file.
755 */
756 assert(image_info != (const ImageInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000757 assert(image_info->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000758 assert(image != (Image *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000759 assert(image->signature == MagickCoreSignature);
cristy3ed852e2009-09-05 21:47:34 +0000760 if (image->debug != MagickFalse)
761 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy3a37efd2011-08-28 20:31:03 +0000762 assert(exception != (ExceptionInfo *) NULL);
cristye1c94d92015-06-28 12:16:33 +0000763 assert(exception->signature == MagickCoreSignature);
cristy3a37efd2011-08-28 20:31:03 +0000764 status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
cristy3ed852e2009-09-05 21:47:34 +0000765 if (status == MagickFalse)
766 return(status);
cristy98140912011-04-20 14:11:45 +0000767 /*
768 Initialize TGA raster file header.
769 */
770 if ((image->columns > 65535L) || (image->rows > 65535L))
771 ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
cristyaf8d3912014-02-21 14:50:33 +0000772 (void) TransformImageColorspace(image,sRGBColorspace,exception);
dirk75652a82014-05-12 19:05:56 +0000773 compression=image->compression;
774 if (image_info->compression != UndefinedCompression)
775 compression=image_info->compression;
dirkf142f1f2014-12-16 09:48:03 +0000776 range=GetQuantumRange(5UL);
dirk75652a82014-05-12 19:05:56 +0000777 tga_info.id_length=0;
cristyd15e6592011-10-15 00:13:06 +0000778 value=GetImageProperty(image,"comment",exception);
cristy98140912011-04-20 14:11:45 +0000779 if (value != (const char *) NULL)
dirk75652a82014-05-12 19:05:56 +0000780 tga_info.id_length=(unsigned char) MagickMin(strlen(value),255);
781 tga_info.colormap_type=0;
782 tga_info.colormap_index=0;
783 tga_info.colormap_length=0;
784 tga_info.colormap_size=0;
785 tga_info.x_origin=0;
786 tga_info.y_origin=0;
787 tga_info.width=(unsigned short) image->columns;
788 tga_info.height=(unsigned short) image->rows;
789 tga_info.bits_per_pixel=8;
790 tga_info.attributes=0;
cristy98140912011-04-20 14:11:45 +0000791 if ((image_info->type != TrueColorType) &&
cristydef23e52015-01-22 11:52:01 +0000792 (image_info->type != TrueColorAlphaType) &&
cristy98140912011-04-20 14:11:45 +0000793 (image_info->type != PaletteType) &&
cristy17f11b02014-12-20 19:37:04 +0000794 (image->alpha_trait == UndefinedPixelTrait) &&
dirkf1d85482015-04-06 00:36:00 +0000795 (SetImageGray(image,exception) != MagickFalse))
dirk75652a82014-05-12 19:05:56 +0000796 tga_info.image_type=compression == RLECompression ? TGARLEMonochrome :
797 TGAMonochrome;
cristy98140912011-04-20 14:11:45 +0000798 else
799 if ((image->storage_class == DirectClass) || (image->colors > 256))
cristy3ed852e2009-09-05 21:47:34 +0000800 {
cristy3ed852e2009-09-05 21:47:34 +0000801 /*
cristy98140912011-04-20 14:11:45 +0000802 Full color TGA raster.
cristy3ed852e2009-09-05 21:47:34 +0000803 */
cristydfc3f852015-04-11 12:37:16 +0000804 tga_info.image_type=compression == RLECompression ? TGARLERGB : TGARGB;
dirkf142f1f2014-12-16 09:48:03 +0000805 if (image_info->depth == 5)
dirk0cfa9e92015-05-03 15:00:25 +0000806 {
807 tga_info.bits_per_pixel=16;
808 if (image->alpha_trait != UndefinedPixelTrait)
809 tga_info.attributes=1; /* # of alpha bits */
810 }
dirkf142f1f2014-12-16 09:48:03 +0000811 else
cristy98140912011-04-20 14:11:45 +0000812 {
dirkf142f1f2014-12-16 09:48:03 +0000813 tga_info.bits_per_pixel=24;
cristy17f11b02014-12-20 19:37:04 +0000814 if (image->alpha_trait != UndefinedPixelTrait)
dirkf142f1f2014-12-16 09:48:03 +0000815 {
816 tga_info.bits_per_pixel=32;
817 tga_info.attributes=8; /* # of alpha bits */
818 }
cristy98140912011-04-20 14:11:45 +0000819 }
cristy3ed852e2009-09-05 21:47:34 +0000820 }
cristy98140912011-04-20 14:11:45 +0000821 else
cristy3ed852e2009-09-05 21:47:34 +0000822 {
cristy98140912011-04-20 14:11:45 +0000823 /*
824 Colormapped TGA raster.
825 */
dirk75652a82014-05-12 19:05:56 +0000826 tga_info.image_type=compression == RLECompression ? TGARLEColormap :
827 TGAColormap;
828 tga_info.colormap_type=1;
829 tga_info.colormap_length=(unsigned short) image->colors;
dirkf142f1f2014-12-16 09:48:03 +0000830 if (image_info->depth == 5)
831 tga_info.colormap_size=16;
832 else
833 tga_info.colormap_size=24;
cristy3ed852e2009-09-05 21:47:34 +0000834 }
dirk9ac19552015-09-13 09:30:49 +0200835 value=GetImageArtifact(image,"tga:image-origin");
836 if (value != (const char *) NULL)
837 {
838 OrientationType
839 origin;
840
841 origin=(OrientationType) ParseCommandOption(MagickOrientationOptions,
842 MagickFalse,value);
843 if (origin == BottomRightOrientation || origin == TopRightOrientation)
844 tga_info.attributes|=(1UL << 4);
845 if (origin == TopLeftOrientation || origin == TopRightOrientation)
846 tga_info.attributes|=(1UL << 5);
847 }
cristy98140912011-04-20 14:11:45 +0000848 /*
849 Write TGA header.
850 */
dirk75652a82014-05-12 19:05:56 +0000851 (void) WriteBlobByte(image,tga_info.id_length);
852 (void) WriteBlobByte(image,tga_info.colormap_type);
dirk2a2a3e92014-05-16 08:46:18 +0000853 (void) WriteBlobByte(image,(unsigned char) tga_info.image_type);
dirk75652a82014-05-12 19:05:56 +0000854 (void) WriteBlobLSBShort(image,tga_info.colormap_index);
855 (void) WriteBlobLSBShort(image,tga_info.colormap_length);
856 (void) WriteBlobByte(image,tga_info.colormap_size);
857 (void) WriteBlobLSBShort(image,tga_info.x_origin);
858 (void) WriteBlobLSBShort(image,tga_info.y_origin);
859 (void) WriteBlobLSBShort(image,tga_info.width);
860 (void) WriteBlobLSBShort(image,tga_info.height);
861 (void) WriteBlobByte(image,tga_info.bits_per_pixel);
862 (void) WriteBlobByte(image,tga_info.attributes);
863 if (tga_info.id_length != 0)
cristydfc3f852015-04-11 12:37:16 +0000864 (void) WriteBlob(image,tga_info.id_length,(unsigned char *) value);
dirk75652a82014-05-12 19:05:56 +0000865 if (tga_info.colormap_type != 0)
cristy98140912011-04-20 14:11:45 +0000866 {
867 unsigned char
dirkf142f1f2014-12-16 09:48:03 +0000868 green,
cristy98140912011-04-20 14:11:45 +0000869 *targa_colormap;
870
871 /*
872 Dump colormap to file (blue, green, red byte order).
873 */
874 targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
cristydfc3f852015-04-11 12:37:16 +0000875 tga_info.colormap_length,(tga_info.colormap_size/8)*
876 sizeof(*targa_colormap));
cristy98140912011-04-20 14:11:45 +0000877 if (targa_colormap == (unsigned char *) NULL)
878 ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
879 q=targa_colormap;
880 for (i=0; i < (ssize_t) image->colors; i++)
881 {
dirkf142f1f2014-12-16 09:48:03 +0000882 if (image_info->depth == 5)
883 {
884 green=(unsigned char) ScaleQuantumToAny(ClampToQuantum(
885 image->colormap[i].green),range);
886 *q++=((unsigned char) ScaleQuantumToAny(ClampToQuantum(
887 image->colormap[i].blue),range)) | ((green & 0x07) << 5);
cristyc9eb04c2014-12-20 20:34:41 +0000888 *q++=(((image->alpha_trait != UndefinedPixelTrait) && ((double)
dirk0cfa9e92015-05-03 15:00:25 +0000889 ClampToQuantum(image->colormap[i].alpha) > midpoint)) ? 0x80 : 0) |
dirkf142f1f2014-12-16 09:48:03 +0000890 ((unsigned char) ScaleQuantumToAny(ClampToQuantum(
891 image->colormap[i].red),range) << 2) | ((green & 0x18) >> 3);
892 }
893 else
894 {
895 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
896 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
897 *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
898 }
cristy98140912011-04-20 14:11:45 +0000899 }
dirkf142f1f2014-12-16 09:48:03 +0000900 (void) WriteBlob(image,(size_t) ((tga_info.colormap_size/8)*
901 tga_info.colormap_length),targa_colormap);
cristy98140912011-04-20 14:11:45 +0000902 targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
cristy3ed852e2009-09-05 21:47:34 +0000903 }
cristy98140912011-04-20 14:11:45 +0000904 /*
905 Convert MIFF to TGA raster pixels.
906 */
dirk75652a82014-05-12 19:05:56 +0000907 channels=GetPixelChannels(image);
cristy98140912011-04-20 14:11:45 +0000908 for (y=(ssize_t) (image->rows-1); y >= 0; y--)
909 {
cristy3a37efd2011-08-28 20:31:03 +0000910 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000911 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000912 break;
dirk75652a82014-05-12 19:05:56 +0000913 if (compression == RLECompression)
914 {
915 x=0;
916 count=0;
917 while (x < (ssize_t) image->columns)
918 {
919 i=1;
920 while ((i < 128) && (count + i < 128) &&
921 ((x + i) < (ssize_t) image->columns))
cristy98140912011-04-20 14:11:45 +0000922 {
dirk75652a82014-05-12 19:05:56 +0000923 if (tga_info.image_type == TGARLEColormap)
924 {
925 if (GetPixelIndex(image,p+(i*channels)) !=
926 GetPixelIndex(image,p+((i-1)*channels)))
927 break;
928 }
929 else if (tga_info.image_type == TGARLEMonochrome)
930 {
931 if (GetPixelLuma(image,p+(i*channels)) !=
932 GetPixelLuma(image,p+((i-1)*channels)))
933 break;
934 }
935 else
936 {
937 if ((GetPixelBlue(image,p+(i*channels)) !=
938 GetPixelBlue(image,p+((i-1)*channels))) ||
939 (GetPixelGreen(image,p+(i*channels)) !=
940 GetPixelGreen(image,p+((i-1)*channels))) ||
941 (GetPixelRed(image,p+(i*channels)) !=
942 GetPixelRed(image,p+((i-1)*channels))))
943 break;
cristy17f11b02014-12-20 19:37:04 +0000944 if ((image->alpha_trait != UndefinedPixelTrait) &&
dirk75652a82014-05-12 19:05:56 +0000945 (GetPixelAlpha(image,p+(i*channels)) !=
946 GetPixelAlpha(image,p+(i-1)*channels)))
947 break;
948 }
949 i++;
cristy98140912011-04-20 14:11:45 +0000950 }
dirk75652a82014-05-12 19:05:56 +0000951 if (i < 3)
952 {
953 count+=i;
954 p+=(i*channels);
955 }
956 if ((i >= 3) || (count == 128) ||
957 ((x + i) == (ssize_t) image->columns))
958 {
959 if (count > 0)
960 {
cristy43254d12014-05-24 23:30:21 +0000961 (void) WriteBlobByte(image,(unsigned char) (--count));
dirk75652a82014-05-12 19:05:56 +0000962 while (count >= 0)
963 {
964 WriteTGAPixel(image,tga_info.image_type,p-((count+1)*
dirkf142f1f2014-12-16 09:48:03 +0000965 channels),range,midpoint);
dirk75652a82014-05-12 19:05:56 +0000966 count--;
967 }
968 count=0;
969 }
970 }
971 if (i >= 3)
972 {
cristyc9eb04c2014-12-20 20:34:41 +0000973 (void) WriteBlobByte(image,(unsigned char) ((i-1) | 0x80));
dirkf142f1f2014-12-16 09:48:03 +0000974 WriteTGAPixel(image,tga_info.image_type,p,range,midpoint);
dirk75652a82014-05-12 19:05:56 +0000975 p+=(i*channels);
976 }
977 x+=i;
978 }
979 }
980 else
981 {
982 for (x=0; x < (ssize_t) image->columns; x++)
983 {
dirkf142f1f2014-12-16 09:48:03 +0000984 WriteTGAPixel(image,tga_info.image_type,p,range,midpoint);
dirk75652a82014-05-12 19:05:56 +0000985 p+=channels;
986 }
987 }
cristy98140912011-04-20 14:11:45 +0000988 if (image->previous == (Image *) NULL)
989 {
990 status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
cristyc6da28e2011-04-28 01:41:35 +0000991 image->rows);
cristy98140912011-04-20 14:11:45 +0000992 if (status == MagickFalse)
993 break;
994 }
995 }
cristy3ed852e2009-09-05 21:47:34 +0000996 (void) CloseBlob(image);
997 return(MagickTrue);
998}