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 % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
Cristy | 7ce65e7 | 2015-12-12 18:03:16 -0500 | [diff] [blame] | 20 | % Copyright 1999-2016 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" |
dirk | 9ac1955 | 2015-09-13 09:30:49 +0200 | [diff] [blame] | 43 | #include "MagickCore/artifact.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 44 | #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" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 52 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 53 | #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" |
dirk | 9ac1955 | 2015-09-13 09:30:49 +0200 | [diff] [blame] | 62 | #include "MagickCore/option.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 63 | #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" |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | Enumerated declaractions. |
| 72 | */ |
| 73 | typedef 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 | */ |
| 86 | typedef 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; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | Forward declarations. |
| 115 | */ |
| 116 | static MagickBooleanType |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 117 | WriteTGAImage(const ImageInfo *,Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 118 | |
| 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 | */ |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 145 | static Image *ReadTGAImage(const ImageInfo *image_info, |
| 146 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 147 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 148 | Image |
| 149 | *image; |
| 150 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 151 | MagickBooleanType |
| 152 | status; |
| 153 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 154 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 155 | pixel; |
| 156 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 157 | Quantum |
| 158 | index; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 159 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 160 | register Quantum |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 161 | *q; |
| 162 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 163 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 164 | i, |
| 165 | x; |
| 166 | |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 167 | size_t |
| 168 | base, |
| 169 | flag, |
| 170 | offset, |
| 171 | real, |
| 172 | skip; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 173 | |
| 174 | ssize_t |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 175 | count, |
| 176 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 177 | |
| 178 | TGAInfo |
| 179 | tga_info; |
| 180 | |
| 181 | unsigned char |
| 182 | j, |
| 183 | k, |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 184 | pixels[4], |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 185 | runlength; |
| 186 | |
cristy | 18307f1 | 2011-12-30 01:20:16 +0000 | [diff] [blame] | 187 | unsigned int |
| 188 | alpha_bits; |
| 189 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 190 | /* |
| 191 | Open image file. |
| 192 | */ |
| 193 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 194 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 195 | if (image_info->debug != MagickFalse) |
| 196 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s", |
| 197 | image_info->filename); |
| 198 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 199 | assert(exception->signature == MagickCoreSignature); |
cristy | 9950d57 | 2011-10-01 18:22:35 +0000 | [diff] [blame] | 200 | image=AcquireImage(image_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 201 | 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); |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 212 | tga_info.image_type=(TGAImageType) ReadBlobByte(image); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 213 | 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; |
cristy | 18307f1 | 2011-12-30 01:20:16 +0000 | [diff] [blame] | 243 | alpha_bits=(tga_info.attributes & 0x0FU); |
cristy | ba21340 | 2013-06-28 14:05:12 +0000 | [diff] [blame] | 244 | image->alpha_trait=(alpha_bits > 0) || (tga_info.bits_per_pixel == 32) || |
| 245 | (tga_info.colormap_size == 32) ? BlendPixelTrait : UndefinedPixelTrait; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 246 | 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) || |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 258 | (tga_info.image_type == TGAMonochrome) || |
| 259 | (tga_info.image_type == TGARLEColormap) || |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 260 | (tga_info.image_type == TGARLEMonochrome)) |
| 261 | image->storage_class=PseudoClass; |
| 262 | image->compression=NoCompression; |
| 263 | if ((tga_info.image_type == TGARLEColormap) || |
dirk | 58ee16a | 2015-05-08 19:43:09 +0000 | [diff] [blame] | 264 | (tga_info.image_type == TGARLEMonochrome) || |
| 265 | (tga_info.image_type == TGARLERGB)) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 266 | image->compression=RLECompression; |
| 267 | if (image->storage_class == PseudoClass) |
| 268 | { |
| 269 | if (tga_info.colormap_type != 0) |
cristy | c9eb04c | 2014-12-20 20:34:41 +0000 | [diff] [blame] | 270 | image->colors=tga_info.colormap_index+tga_info.colormap_length; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 271 | else |
| 272 | { |
| 273 | size_t |
| 274 | one; |
| 275 | |
| 276 | one=1; |
| 277 | image->colors=one << tga_info.bits_per_pixel; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 278 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 279 | 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; |
cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 295 | if (~length >= (MagickPathExtent-1)) |
| 296 | comment=(char *) AcquireQuantumMemory(length+MagickPathExtent, |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 297 | 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'; |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 302 | (void) SetImageProperty(image,"comment",comment,exception); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 303 | comment=DestroyString(comment); |
| 304 | } |
dirk | 9ac1955 | 2015-09-13 09:30:49 +0200 | [diff] [blame] | 305 | 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 | } |
dirk | e1b0388 | 2014-05-13 11:03:42 +0000 | [diff] [blame] | 319 | if (image_info->ping != MagickFalse) |
| 320 | { |
| 321 | (void) CloseBlob(image); |
| 322 | return(image); |
| 323 | } |
cristy | acabb84 | 2014-12-14 23:36:33 +0000 | [diff] [blame] | 324 | status=SetImageExtent(image,image->columns,image->rows,exception); |
| 325 | if (status == MagickFalse) |
| 326 | return(DestroyImageList(image)); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 327 | (void) ResetMagickMemory(&pixel,0,sizeof(pixel)); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 328 | pixel.alpha=(MagickRealType) OpaqueAlpha; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 329 | if (tga_info.colormap_type != 0) |
| 330 | { |
| 331 | /* |
| 332 | Read TGA raster colormap. |
| 333 | */ |
Cristy | 4f68e96 | 2015-08-31 16:19:40 -0400 | [diff] [blame] | 334 | if (image->colors < tga_info.colormap_index) |
| 335 | image->colors=tga_info.colormap_index; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 336 | if (AcquireImageColormap(image,image->colors,exception) == MagickFalse) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 337 | ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed"); |
cristy | c9eb04c | 2014-12-20 20:34:41 +0000 | [diff] [blame] | 338 | for (i=0; i < (ssize_t) tga_info.colormap_index; i++) |
| 339 | image->colormap[i]=pixel; |
| 340 | for ( ; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 341 | { |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 342 | switch (tga_info.colormap_size) |
| 343 | { |
| 344 | case 8: |
| 345 | default: |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 346 | { |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 347 | /* |
| 348 | Gray scale. |
| 349 | */ |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 350 | pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char) |
| 351 | ReadBlobByte(image)); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 352 | pixel.green=pixel.red; |
| 353 | pixel.blue=pixel.red; |
| 354 | break; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 355 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 356 | 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); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 368 | 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); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 373 | break; |
| 374 | } |
| 375 | case 24: |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 376 | { |
| 377 | /* |
| 378 | 8 bits each of blue, green and red. |
| 379 | */ |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 380 | 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)); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 386 | break; |
| 387 | } |
cristy | ba21340 | 2013-06-28 14:05:12 +0000 | [diff] [blame] | 388 | 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 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 403 | } |
| 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; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 414 | index=0; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 415 | 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); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 423 | if (q == (Quantum *) NULL) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 424 | break; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 425 | 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); |
cristy | 771c884 | 2015-01-09 12:13:22 +0000 | [diff] [blame] | 439 | if (count != 1) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 440 | 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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 456 | index=(Quantum) ReadBlobByte(image); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 457 | if (tga_info.colormap_type != 0) |
| 458 | pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image, |
cristy | c9eb04c | 2014-12-20 20:34:41 +0000 | [diff] [blame] | 459 | (ssize_t) index,exception)]; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 460 | else |
| 461 | { |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 462 | 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); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 468 | } |
| 469 | break; |
| 470 | } |
| 471 | case 15: |
| 472 | case 16: |
| 473 | { |
| 474 | QuantumAny |
| 475 | range; |
| 476 | |
| 477 | /* |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 478 | 5 bits each of RGB. |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 479 | */ |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 480 | if (ReadBlob(image,2,pixels) != 2) |
| 481 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
| 482 | j=pixels[0]; |
| 483 | k=pixels[1]; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 484 | range=GetQuantumRange(5UL); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 485 | pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2, |
| 486 | range); |
cristy | dfc3f85 | 2015-04-11 12:37:16 +0000 | [diff] [blame] | 487 | pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL* |
| 488 | (k & 0x03) << 3)+(1UL*(j & 0xe0) >> 5),range); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 489 | pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range); |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 490 | if (image->alpha_trait != UndefinedPixelTrait) |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 491 | pixel.alpha=(MagickRealType) ((k & 0x80) == 0 ? (Quantum) |
dirk | 0cfa9e9 | 2015-05-03 15:00:25 +0000 | [diff] [blame] | 492 | TransparentAlpha : (Quantum) OpaqueAlpha); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 493 | if (image->storage_class == PseudoClass) |
cristy | c9eb04c | 2014-12-20 20:34:41 +0000 | [diff] [blame] | 494 | index=(Quantum) ConstrainColormapIndex(image,((ssize_t) (k << 8))+ |
| 495 | j,exception); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 496 | break; |
| 497 | } |
| 498 | case 24: |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 499 | { |
| 500 | /* |
| 501 | BGR pixels. |
| 502 | */ |
| 503 | if (ReadBlob(image,3,pixels) != 3) |
| 504 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 505 | pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]); |
| 506 | pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]); |
| 507 | pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]); |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 508 | break; |
| 509 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 510 | case 32: |
| 511 | { |
| 512 | /* |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 513 | BGRA pixels. |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 514 | */ |
cristy | 5dd83a5 | 2011-11-24 01:54:17 +0000 | [diff] [blame] | 515 | if (ReadBlob(image,4,pixels) != 4) |
| 516 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 517 | 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]); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | } |
| 524 | if (status == MagickFalse) |
| 525 | ThrowReaderException(CorruptImageError,"UnableToReadImageData"); |
| 526 | if (image->storage_class == PseudoClass) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 527 | SetPixelIndex(image,index,q); |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 528 | SetPixelRed(image,ClampToQuantum(pixel.red),q); |
| 529 | SetPixelGreen(image,ClampToQuantum(pixel.green),q); |
| 530 | SetPixelBlue(image,ClampToQuantum(pixel.blue),q); |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 531 | if (image->alpha_trait != UndefinedPixelTrait) |
cristy | 77b33ba | 2013-03-28 13:23:13 +0000 | [diff] [blame] | 532 | SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 533 | q+=GetPixelChannels(image); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 534 | } |
cristy | cd56a38 | 2015-06-27 15:39:21 +0000 | [diff] [blame] | 535 | /* |
| 536 | if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4) |
| 537 | offset+=4; |
| 538 | else |
| 539 | */ |
cristy | ccdef73 | 2015-05-31 14:14:16 +0000 | [diff] [blame] | 540 | if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 541 | 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); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 555 | if (status == MagickFalse) |
| 556 | break; |
| 557 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 558 | } |
| 559 | if (EOFBlob(image) != MagickFalse) |
| 560 | ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile", |
| 561 | image->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 562 | (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 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 586 | % size_t RegisterTGAImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 587 | % |
| 588 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 589 | ModuleExport size_t RegisterTGAImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 590 | { |
| 591 | MagickInfo |
| 592 | *entry; |
| 593 | |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 594 | entry=AcquireMagickInfo("TGA","ICB","Truevision Targa image"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 595 | entry->decoder=(DecodeImageHandler *) ReadTGAImage; |
| 596 | entry->encoder=(EncodeImageHandler *) WriteTGAImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 597 | entry->flags^=CoderAdjoinFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 598 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 599 | entry=AcquireMagickInfo("TGA","TGA","Truevision Targa image"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 600 | entry->decoder=(DecodeImageHandler *) ReadTGAImage; |
| 601 | entry->encoder=(EncodeImageHandler *) WriteTGAImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 602 | entry->flags^=CoderAdjoinFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 603 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 604 | entry=AcquireMagickInfo("TGA","VDA","Truevision Targa image"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 605 | entry->decoder=(DecodeImageHandler *) ReadTGAImage; |
| 606 | entry->encoder=(EncodeImageHandler *) WriteTGAImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 607 | entry->flags^=CoderAdjoinFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 608 | (void) RegisterMagickInfo(entry); |
dirk | 06b627a | 2015-04-06 18:59:17 +0000 | [diff] [blame] | 609 | entry=AcquireMagickInfo("TGA","VST","Truevision Targa image"); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 610 | entry->decoder=(DecodeImageHandler *) ReadTGAImage; |
| 611 | entry->encoder=(EncodeImageHandler *) WriteTGAImage; |
dirk | 08e9a11 | 2015-02-22 01:51:41 +0000 | [diff] [blame] | 612 | entry->flags^=CoderAdjoinFlag; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 613 | (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 | */ |
| 636 | ModuleExport 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 | % |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 660 | % MagickBooleanType WriteTGAImage(const ImageInfo *image_info, |
| 661 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 662 | % |
| 663 | % A description of each parameter follows. |
| 664 | % |
| 665 | % o image_info: the image info. |
| 666 | % |
| 667 | % o image: The image. |
| 668 | % |
| 669 | */ |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 670 | static inline void WriteTGAPixel(Image *image,TGAImageType image_type, |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 671 | const Quantum *p,const QuantumAny range,const double midpoint) |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 672 | { |
| 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 |
cristy | dfc3f85 | 2015-04-11 12:37:16 +0000 | [diff] [blame] | 681 | 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) && |
dirk | 0cfa9e9 | 2015-05-03 15:00:25 +0000 | [diff] [blame] | 693 | ((double) GetPixelAlpha(image,p) > midpoint)) ? 0x80 : 0) | |
cristy | dfc3f85 | 2015-04-11 12:37:16 +0000 | [diff] [blame] | 694 | ((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 | } |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 713 | static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image, |
| 714 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 715 | { |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 716 | CompressionType |
| 717 | compression; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 718 | |
| 719 | const char |
| 720 | *value; |
| 721 | |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 722 | const double |
cristy | dfc3f85 | 2015-04-11 12:37:16 +0000 | [diff] [blame] | 723 | midpoint = QuantumRange/2.0; |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 724 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 725 | MagickBooleanType |
| 726 | status; |
| 727 | |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 728 | QuantumAny |
| 729 | range; |
| 730 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 731 | register const Quantum |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 732 | *p; |
| 733 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 734 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 735 | x; |
| 736 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 737 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 738 | i; |
| 739 | |
| 740 | register unsigned char |
| 741 | *q; |
| 742 | |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 743 | size_t |
| 744 | channels; |
| 745 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 746 | ssize_t |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 747 | count, |
| 748 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 749 | |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 750 | TGAInfo |
| 751 | tga_info; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 752 | |
| 753 | /* |
| 754 | Open output image file. |
| 755 | */ |
| 756 | assert(image_info != (const ImageInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 757 | assert(image_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 758 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 759 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 760 | if (image->debug != MagickFalse) |
| 761 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 762 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 763 | assert(exception->signature == MagickCoreSignature); |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 764 | status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 765 | if (status == MagickFalse) |
| 766 | return(status); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 767 | /* |
| 768 | Initialize TGA raster file header. |
| 769 | */ |
| 770 | if ((image->columns > 65535L) || (image->rows > 65535L)) |
| 771 | ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit"); |
cristy | af8d391 | 2014-02-21 14:50:33 +0000 | [diff] [blame] | 772 | (void) TransformImageColorspace(image,sRGBColorspace,exception); |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 773 | compression=image->compression; |
| 774 | if (image_info->compression != UndefinedCompression) |
| 775 | compression=image_info->compression; |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 776 | range=GetQuantumRange(5UL); |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 777 | tga_info.id_length=0; |
cristy | d15e659 | 2011-10-15 00:13:06 +0000 | [diff] [blame] | 778 | value=GetImageProperty(image,"comment",exception); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 779 | if (value != (const char *) NULL) |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 780 | 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; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 791 | if ((image_info->type != TrueColorType) && |
cristy | def23e5 | 2015-01-22 11:52:01 +0000 | [diff] [blame] | 792 | (image_info->type != TrueColorAlphaType) && |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 793 | (image_info->type != PaletteType) && |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 794 | (image->alpha_trait == UndefinedPixelTrait) && |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 795 | (SetImageGray(image,exception) != MagickFalse)) |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 796 | tga_info.image_type=compression == RLECompression ? TGARLEMonochrome : |
| 797 | TGAMonochrome; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 798 | else |
| 799 | if ((image->storage_class == DirectClass) || (image->colors > 256)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 800 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 801 | /* |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 802 | Full color TGA raster. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 803 | */ |
cristy | dfc3f85 | 2015-04-11 12:37:16 +0000 | [diff] [blame] | 804 | tga_info.image_type=compression == RLECompression ? TGARLERGB : TGARGB; |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 805 | if (image_info->depth == 5) |
dirk | 0cfa9e9 | 2015-05-03 15:00:25 +0000 | [diff] [blame] | 806 | { |
| 807 | tga_info.bits_per_pixel=16; |
| 808 | if (image->alpha_trait != UndefinedPixelTrait) |
| 809 | tga_info.attributes=1; /* # of alpha bits */ |
| 810 | } |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 811 | else |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 812 | { |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 813 | tga_info.bits_per_pixel=24; |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 814 | if (image->alpha_trait != UndefinedPixelTrait) |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 815 | { |
| 816 | tga_info.bits_per_pixel=32; |
| 817 | tga_info.attributes=8; /* # of alpha bits */ |
| 818 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 819 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 820 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 821 | else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 822 | { |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 823 | /* |
| 824 | Colormapped TGA raster. |
| 825 | */ |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 826 | tga_info.image_type=compression == RLECompression ? TGARLEColormap : |
| 827 | TGAColormap; |
| 828 | tga_info.colormap_type=1; |
| 829 | tga_info.colormap_length=(unsigned short) image->colors; |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 830 | if (image_info->depth == 5) |
| 831 | tga_info.colormap_size=16; |
| 832 | else |
| 833 | tga_info.colormap_size=24; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 834 | } |
dirk | 9ac1955 | 2015-09-13 09:30:49 +0200 | [diff] [blame] | 835 | 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 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 848 | /* |
| 849 | Write TGA header. |
| 850 | */ |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 851 | (void) WriteBlobByte(image,tga_info.id_length); |
| 852 | (void) WriteBlobByte(image,tga_info.colormap_type); |
dirk | 2a2a3e9 | 2014-05-16 08:46:18 +0000 | [diff] [blame] | 853 | (void) WriteBlobByte(image,(unsigned char) tga_info.image_type); |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 854 | (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) |
cristy | dfc3f85 | 2015-04-11 12:37:16 +0000 | [diff] [blame] | 864 | (void) WriteBlob(image,tga_info.id_length,(unsigned char *) value); |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 865 | if (tga_info.colormap_type != 0) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 866 | { |
| 867 | unsigned char |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 868 | green, |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 869 | *targa_colormap; |
| 870 | |
| 871 | /* |
| 872 | Dump colormap to file (blue, green, red byte order). |
| 873 | */ |
| 874 | targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t) |
cristy | dfc3f85 | 2015-04-11 12:37:16 +0000 | [diff] [blame] | 875 | tga_info.colormap_length,(tga_info.colormap_size/8)* |
| 876 | sizeof(*targa_colormap)); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 877 | 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 | { |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 882 | 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); |
cristy | c9eb04c | 2014-12-20 20:34:41 +0000 | [diff] [blame] | 888 | *q++=(((image->alpha_trait != UndefinedPixelTrait) && ((double) |
dirk | 0cfa9e9 | 2015-05-03 15:00:25 +0000 | [diff] [blame] | 889 | ClampToQuantum(image->colormap[i].alpha) > midpoint)) ? 0x80 : 0) | |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 890 | ((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 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 899 | } |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 900 | (void) WriteBlob(image,(size_t) ((tga_info.colormap_size/8)* |
| 901 | tga_info.colormap_length),targa_colormap); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 902 | targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 903 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 904 | /* |
| 905 | Convert MIFF to TGA raster pixels. |
| 906 | */ |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 907 | channels=GetPixelChannels(image); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 908 | for (y=(ssize_t) (image->rows-1); y >= 0; y--) |
| 909 | { |
cristy | 3a37efd | 2011-08-28 20:31:03 +0000 | [diff] [blame] | 910 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 911 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 912 | break; |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 913 | 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)) |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 922 | { |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 923 | 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; |
cristy | 17f11b0 | 2014-12-20 19:37:04 +0000 | [diff] [blame] | 944 | if ((image->alpha_trait != UndefinedPixelTrait) && |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 945 | (GetPixelAlpha(image,p+(i*channels)) != |
| 946 | GetPixelAlpha(image,p+(i-1)*channels))) |
| 947 | break; |
| 948 | } |
| 949 | i++; |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 950 | } |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 951 | 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 | { |
cristy | 43254d1 | 2014-05-24 23:30:21 +0000 | [diff] [blame] | 961 | (void) WriteBlobByte(image,(unsigned char) (--count)); |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 962 | while (count >= 0) |
| 963 | { |
| 964 | WriteTGAPixel(image,tga_info.image_type,p-((count+1)* |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 965 | channels),range,midpoint); |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 966 | count--; |
| 967 | } |
| 968 | count=0; |
| 969 | } |
| 970 | } |
| 971 | if (i >= 3) |
| 972 | { |
cristy | c9eb04c | 2014-12-20 20:34:41 +0000 | [diff] [blame] | 973 | (void) WriteBlobByte(image,(unsigned char) ((i-1) | 0x80)); |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 974 | WriteTGAPixel(image,tga_info.image_type,p,range,midpoint); |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 975 | p+=(i*channels); |
| 976 | } |
| 977 | x+=i; |
| 978 | } |
| 979 | } |
| 980 | else |
| 981 | { |
| 982 | for (x=0; x < (ssize_t) image->columns; x++) |
| 983 | { |
dirk | f142f1f | 2014-12-16 09:48:03 +0000 | [diff] [blame] | 984 | WriteTGAPixel(image,tga_info.image_type,p,range,midpoint); |
dirk | 75652a8 | 2014-05-12 19:05:56 +0000 | [diff] [blame] | 985 | p+=channels; |
| 986 | } |
| 987 | } |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 988 | if (image->previous == (Image *) NULL) |
| 989 | { |
| 990 | status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y, |
cristy | c6da28e | 2011-04-28 01:41:35 +0000 | [diff] [blame] | 991 | image->rows); |
cristy | 9814091 | 2011-04-20 14:11:45 +0000 | [diff] [blame] | 992 | if (status == MagickFalse) |
| 993 | break; |
| 994 | } |
| 995 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 996 | (void) CloseBlob(image); |
| 997 | return(MagickTrue); |
| 998 | } |