cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 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 | % % |
cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 20 | % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % 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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 42 | #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" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 51 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 52 | #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" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | Forward declarations. |
| 70 | */ |
| 71 | static MagickBooleanType |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 72 | WriteTGAImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 73 | |
| 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 | */ |
| 100 | static 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 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 137 | MagickBooleanType |
| 138 | status; |
| 139 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 140 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 141 | pixel; |
| 142 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 143 | Quantum |
| 144 | index; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 145 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 146 | register Quantum |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 147 | *q; |
| 148 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 149 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 150 | i, |
| 151 | x; |
| 152 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 153 | size_t |
| 154 | base, |
| 155 | flag, |
| 156 | offset, |
| 157 | real, |
| 158 | skip; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 159 | |
| 160 | ssize_t |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 161 | count, |
| 162 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 163 | |
| 164 | TGAInfo |
| 165 | tga_info; |
| 166 | |
| 167 | unsigned char |
| 168 | j, |
| 169 | k, |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 170 | pixels[4], |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 171 | runlength; |
| 172 | |
cristy | 18307f1 | 2011-12-30 01:20:16 +0000 | [diff] [blame] | 173 | unsigned int |
| 174 | alpha_bits; |
| 175 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 176 | /* |
| 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); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 186 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 187 | 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); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 199 | 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; |
cristy | 18307f1 | 2011-12-30 01:20:16 +0000 | [diff] [blame] | 229 | alpha_bits=(tga_info.attributes & 0x0FU); |
cristy | ba21340 | 2013-06-28 14:05:12 +0000 | [diff] [blame] | 230 | image->alpha_trait=(alpha_bits > 0) || (tga_info.bits_per_pixel == 32) || |
| 231 | (tga_info.colormap_size == 32) ? BlendPixelTrait : UndefinedPixelTrait; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 232 | 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) || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 244 | (tga_info.image_type == TGAMonochrome) || |
| 245 | (tga_info.image_type == TGARLEColormap) || |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 246 | (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; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 263 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 264 | 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; |
cristy | 37e0b38 | 2011-06-07 13:31:21 +0000 | [diff] [blame] | 280 | if (~length >= (MaxTextExtent-1)) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 281 | 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'; |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 287 | (void) SetImageProperty(image,"comment",comment,exception); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 288 | comment=DestroyString(comment); |
| 289 | } |
| 290 | (void) ResetMagickMemory(&pixel,0,sizeof(pixel)); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 291 | pixel.alpha=(MagickRealType) OpaqueAlpha; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 292 | if (tga_info.colormap_type != 0) |
| 293 | { |
| 294 | /* |
| 295 | Read TGA raster colormap. |
| 296 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 297 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 298 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
| 299 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 300 | { |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 301 | switch (tga_info.colormap_size) |
| 302 | { |
| 303 | case 8: |
| 304 | default: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 305 | { |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 306 | /* |
| 307 | Gray scale. |
| 308 | */ |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 309 | pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char) |
| 310 | ReadBlobByte(image)); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 311 | pixel.green=pixel.red; |
| 312 | pixel.blue=pixel.red; |
| 313 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 314 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 315 | 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); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 327 | 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); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 332 | break; |
| 333 | } |
| 334 | case 24: |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 335 | { |
| 336 | /* |
| 337 | 8 bits each of blue, green and red. |
| 338 | */ |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 339 | 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)); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 345 | break; |
| 346 | } |
cristy | ba21340 | 2013-06-28 14:05:12 +0000 | [diff] [blame] | 347 | 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 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 362 | } |
| 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; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 373 | index=0; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 374 | 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); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 382 | if (q == (Quantum *) NULL) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 383 | break; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 384 | 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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 415 | index=(Quantum) ReadBlobByte(image); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 416 | if (tga_info.colormap_type != 0) |
| 417 | pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image, |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 418 | 1UL*index,exception)]; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 419 | else |
| 420 | { |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 421 | 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); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 427 | } |
| 428 | break; |
| 429 | } |
| 430 | case 15: |
| 431 | case 16: |
| 432 | { |
| 433 | QuantumAny |
| 434 | range; |
| 435 | |
| 436 | /* |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 437 | 5 bits each of RGB. |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 438 | */ |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 439 | if (ReadBlob(image,2,pixels) != 2) |
| 440 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
| 441 | j=pixels[0]; |
| 442 | k=pixels[1]; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 443 | range=GetQuantumRange(5UL); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 444 | 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); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 449 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 450 | pixel.alpha=(MagickRealType) ((k & 0x80) == 0 ? (Quantum) |
| 451 | OpaqueAlpha : (Quantum) TransparentAlpha); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 452 | if (image->storage_class == PseudoClass) |
cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 453 | index=ConstrainColormapIndex(image,((size_t) k << 8)+j,exception); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 454 | break; |
| 455 | } |
| 456 | case 24: |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 457 | { |
| 458 | /* |
| 459 | BGR pixels. |
| 460 | */ |
| 461 | if (ReadBlob(image,3,pixels) != 3) |
| 462 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 463 | pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]); |
| 464 | pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]); |
| 465 | pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]); |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 466 | break; |
| 467 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 468 | case 32: |
| 469 | { |
| 470 | /* |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 471 | BGRA pixels. |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 472 | */ |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 473 | if (ReadBlob(image,4,pixels) != 4) |
| 474 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 475 | 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]); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 479 | break; |
| 480 | } |
| 481 | } |
| 482 | if (status == MagickFalse) |
| 483 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
| 484 | if (image->storage_class == PseudoClass) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 485 | SetPixelIndex(image,index,q); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 486 | SetPixelRed(image,ClampToQuantum(pixel.red),q); |
| 487 | SetPixelGreen(image,ClampToQuantum(pixel.green),q); |
| 488 | SetPixelBlue(image,ClampToQuantum(pixel.blue),q); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 489 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 490 | SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 491 | q+=GetPixelChannels(image); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 492 | } |
| 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); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 511 | if (status == MagickFalse) |
| 512 | break; |
| 513 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 514 | } |
| 515 | if (EOFBlob(image) != MagickFalse) |
| 516 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 517 | image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 518 | (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 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 542 | % size_t RegisterTGAImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 543 | % |
| 544 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 545 | ModuleExport size_t RegisterTGAImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 546 | { |
| 547 | MagickInfo |
| 548 | *entry; |
| 549 | |
| 550 | entry=SetMagickInfo("ICB"); |
| 551 | entry->decoder=(DecodeImageHandler *) ReadTGAImage; |
| 552 | entry->encoder=(EncodeImageHandler *) WriteTGAImage; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 553 | entry->adjoin=MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 554 | 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; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 560 | entry->adjoin=MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 561 | 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; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 567 | entry->adjoin=MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 568 | 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; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 574 | entry->adjoin=MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 575 | 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 | */ |
| 600 | ModuleExport 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 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 624 | % MagickBooleanType WriteTGAImage(const ImageInfo *image_info, |
| 625 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 626 | % |
| 627 | % A description of each parameter follows. |
| 628 | % |
| 629 | % o image_info: the image info. |
| 630 | % |
| 631 | % o image: The image. |
| 632 | % |
| 633 | */ |
| 634 | |
| 635 | static 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 | |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 642 | static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image, |
| 643 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 644 | { |
| 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 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 680 | MagickBooleanType |
| 681 | status; |
| 682 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 683 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 684 | *p; |
| 685 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 686 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 687 | x; |
| 688 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 689 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 690 | i; |
| 691 | |
| 692 | register unsigned char |
| 693 | *q; |
| 694 | |
| 695 | ssize_t |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 696 | count, |
| 697 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 698 | |
| 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); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 714 | assert(exception != (ExceptionInfo *) NULL); |
| 715 | assert(exception->signature == MagickSignature); |
| 716 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 717 | if (status == MagickFalse) |
| 718 | return(status); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 719 | /* |
| 720 | Initialize TGA raster file header. |
| 721 | */ |
| 722 | if ((image->columns > 65535L) || (image->rows > 65535L)) |
| 723 | ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit"); |
cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 724 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
cristy | 8d95109 | 2012-02-08 18:54:56 +0000 | [diff] [blame] | 725 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 726 | targa_info.id_length=0; |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 727 | value=GetImageProperty(image,"comment",exception); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 728 | 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) && |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 743 | (image->alpha_trait != BlendPixelTrait) && |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 744 | (IsImageGray(image,exception) != MagickFalse)) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 745 | targa_info.image_type=TargaMonochrome; |
| 746 | else |
| 747 | if ((image->storage_class == DirectClass) || (image->colors > 256)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 748 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 749 | /* |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 750 | Full color TGA raster. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 751 | */ |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 752 | targa_info.image_type=TargaRGB; |
| 753 | targa_info.bits_per_pixel=24; |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 754 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 755 | { |
| 756 | targa_info.bits_per_pixel=32; |
| 757 | targa_info.attributes=8; /* # of alpha bits */ |
| 758 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 759 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 760 | else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 761 | { |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 762 | /* |
| 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; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 769 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 770 | /* |
| 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 | { |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 803 | *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue)); |
| 804 | *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green)); |
| 805 | *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red)); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 806 | } |
| 807 | (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length), |
| 808 | targa_colormap); |
| 809 | targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 810 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 811 | /* |
| 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 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 821 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 822 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 823 | break; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 824 | q=targa_pixels; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 825 | for (x=0; x < (ssize_t) image->columns; x++) |
| 826 | { |
| 827 | if (targa_info.image_type == TargaColormap) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 828 | *q++=(unsigned char) GetPixelIndex(image,p); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 829 | else |
| 830 | if (targa_info.image_type == TargaMonochrome) |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 831 | *q++=(unsigned char) ScaleQuantumToChar((ClampToQuantum( |
cristy | d032322 | 2013-04-07 16:13:21 +0000 | [diff] [blame] | 832 | GetPixelLuma(image,p)))); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 833 | else |
| 834 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 835 | *q++=ScaleQuantumToChar(GetPixelBlue(image,p)); |
| 836 | *q++=ScaleQuantumToChar(GetPixelGreen(image,p)); |
| 837 | *q++=ScaleQuantumToChar(GetPixelRed(image,p)); |
cristy | 8a46d82 | 2012-08-28 23:32:39 +0000 | [diff] [blame] | 838 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 839 | *q++=(unsigned char) ScaleQuantumToChar(GetPixelAlpha(image,p)); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 840 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 841 | p+=GetPixelChannels(image); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 842 | } |
| 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, |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 847 | image->rows); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 848 | if (status == MagickFalse) |
| 849 | break; |
| 850 | } |
| 851 | } |
| 852 | targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 853 | (void) CloseBlob(image); |
| 854 | return(MagickTrue); |
| 855 | } |