]> granicus.if.org Git - imagemagick/blobdiff - coders/tga.c
(no commit message)
[imagemagick] / coders / tga.c
index f032171549d72f501b598be582450abbd927519f..cf4539609638775126e5d1c5e6912a3fe2894846 100644 (file)
 %                    Read/Write Truevision Targa Image Format                 %
 %                                                                             %
 %                              Software Design                                %
-%                                John Cristy                                  %
+%                                   Cristy                                    %
 %                                 July 1992                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
 /*
   Include declarations.
 */
-#include "magick/studio.h"
-#include "magick/blob.h"
-#include "magick/blob-private.h"
-#include "magick/cache.h"
-#include "magick/color-private.h"
-#include "magick/colormap-private.h"
-#include "magick/colorspace.h"
-#include "magick/exception.h"
-#include "magick/exception-private.h"
-#include "magick/image.h"
-#include "magick/image-private.h"
-#include "magick/list.h"
-#include "magick/magick.h"
-#include "magick/memory_.h"
-#include "magick/monitor.h"
-#include "magick/monitor-private.h"
-#include "magick/property.h"
-#include "magick/quantum-private.h"
-#include "magick/static.h"
-#include "magick/string_.h"
-#include "magick/module.h"
+#include "MagickCore/studio.h"
+#include "MagickCore/attribute.h"
+#include "MagickCore/blob.h"
+#include "MagickCore/blob-private.h"
+#include "MagickCore/cache.h"
+#include "MagickCore/color-private.h"
+#include "MagickCore/colormap.h"
+#include "MagickCore/colormap-private.h"
+#include "MagickCore/colorspace.h"
+#include "MagickCore/colorspace-private.h"
+#include "MagickCore/exception.h"
+#include "MagickCore/exception-private.h"
+#include "MagickCore/image.h"
+#include "MagickCore/image-private.h"
+#include "MagickCore/list.h"
+#include "MagickCore/magick.h"
+#include "MagickCore/memory_.h"
+#include "MagickCore/monitor.h"
+#include "MagickCore/monitor-private.h"
+#include "MagickCore/pixel-accessor.h"
+#include "MagickCore/property.h"
+#include "MagickCore/quantum-private.h"
+#include "MagickCore/static.h"
+#include "MagickCore/string_.h"
+#include "MagickCore/module.h"
 \f
 /*
   Forward declarations.
 */
 static MagickBooleanType
-  WriteTGAImage(const ImageInfo *,Image *);
+  WriteTGAImage(const ImageInfo *,Image *,ExceptionInfo *);
 \f
 /*
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -130,30 +134,32 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
   Image
     *image;
 
-  IndexPacket
-    index;
-
-  long
-    y;
-
   MagickBooleanType
     status;
 
-  PixelPacket
+  PixelInfo
     pixel;
 
-  register IndexPacket
-    *indexes;
+  Quantum
+    index;
+
+  register Quantum
+    *q;
 
-  register long
+  register ssize_t
     i,
     x;
 
-  register PixelPacket
-    *q;
+  size_t
+    base,
+    flag,
+    offset,
+    real,
+    skip;
 
   ssize_t
-    count;
+    count,
+    y;
 
   TGAInfo
     tga_info;
@@ -161,14 +167,11 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
   unsigned char
     j,
     k,
+    pixels[4],
     runlength;
 
-  unsigned long
-    base,
-    flag,
-    offset,
-    real,
-    skip;
+  unsigned int
+    alpha_bits;
 
   /*
     Open image file.
@@ -180,7 +183,7 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
       image_info->filename);
   assert(exception != (ExceptionInfo *) NULL);
   assert(exception->signature == MagickSignature);
-  image=AcquireImage(image_info);
+  image=AcquireImage(image_info,exception);
   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
   if (status == MagickFalse)
     {
@@ -193,327 +196,325 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
   count=ReadBlob(image,1,&tga_info.id_length);
   tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
   tga_info.image_type=(unsigned char) ReadBlobByte(image);
-  do
-  {
-    if ((count != 1) ||
-        ((tga_info.image_type != TGAColormap) &&
-         (tga_info.image_type != TGARGB) &&
-         (tga_info.image_type != TGAMonochrome) &&
-         (tga_info.image_type != TGARLEColormap) &&
-         (tga_info.image_type != TGARLERGB) &&
-         (tga_info.image_type != TGARLEMonochrome)) ||
-        (((tga_info.image_type == TGAColormap) ||
-         (tga_info.image_type == TGARLEColormap)) &&
-         (tga_info.colormap_type == 0)))
-      ThrowReaderException(CorruptImageError,"ImproperImageHeader");
-    tga_info.colormap_index=ReadBlobLSBShort(image);
-    tga_info.colormap_length=ReadBlobLSBShort(image);
-    tga_info.colormap_size=(unsigned char) ReadBlobByte(image);
-    tga_info.x_origin=ReadBlobLSBShort(image);
-    tga_info.y_origin=ReadBlobLSBShort(image);
-    tga_info.width=(unsigned short) ReadBlobLSBShort(image);
-    tga_info.height=(unsigned short) ReadBlobLSBShort(image);
-    tga_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
-    tga_info.attributes=(unsigned char) ReadBlobByte(image);
-    if (EOFBlob(image) != MagickFalse)
-      ThrowReaderException(CorruptImageError,"UnableToReadImageData");
-    /*
-      Initialize image structure.
-    */
-    image->columns=tga_info.width;
-    image->rows=tga_info.height;
-    image->matte=tga_info.bits_per_pixel == 32 ? MagickTrue : MagickFalse;
-    if ((tga_info.image_type != TGAColormap) &&
-        (tga_info.image_type != TGARLEColormap))
-      image->depth=(unsigned long) ((tga_info.bits_per_pixel <= 8) ? 8 :
-        (tga_info.bits_per_pixel <= 16) ? 5 :
-        (tga_info.bits_per_pixel == 24) ? 8 :
-        (tga_info.bits_per_pixel == 32) ? 8 : 8);
-    else
-      image->depth=(unsigned long) ((tga_info.colormap_size <= 8) ? 8 :
-        (tga_info.colormap_size <= 16) ? 5 :
-        (tga_info.colormap_size == 24) ? 8 :
-        (tga_info.colormap_size == 32) ? 8 : 8);
-    if ((tga_info.image_type == TGAColormap) ||
-        (tga_info.image_type == TGAMonochrome) ||
-        (tga_info.image_type == TGARLEColormap) ||
-        (tga_info.image_type == TGARLEMonochrome))
-      image->storage_class=PseudoClass;
-    image->compression=NoCompression;
-    if ((tga_info.image_type == TGARLEColormap) ||
-        (tga_info.image_type == TGARLEMonochrome))
-      image->compression=RLECompression;
-    if (image->storage_class == PseudoClass)
+  if ((count != 1) ||
+      ((tga_info.image_type != TGAColormap) &&
+       (tga_info.image_type != TGARGB) &&
+       (tga_info.image_type != TGAMonochrome) &&
+       (tga_info.image_type != TGARLEColormap) &&
+       (tga_info.image_type != TGARLERGB) &&
+       (tga_info.image_type != TGARLEMonochrome)) ||
+      (((tga_info.image_type == TGAColormap) ||
+       (tga_info.image_type == TGARLEColormap)) &&
+       (tga_info.colormap_type == 0)))
+    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+  tga_info.colormap_index=ReadBlobLSBShort(image);
+  tga_info.colormap_length=ReadBlobLSBShort(image);
+  tga_info.colormap_size=(unsigned char) ReadBlobByte(image);
+  tga_info.x_origin=ReadBlobLSBShort(image);
+  tga_info.y_origin=ReadBlobLSBShort(image);
+  tga_info.width=(unsigned short) ReadBlobLSBShort(image);
+  tga_info.height=(unsigned short) ReadBlobLSBShort(image);
+  tga_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
+  tga_info.attributes=(unsigned char) ReadBlobByte(image);
+  if (EOFBlob(image) != MagickFalse)
+    ThrowReaderException(CorruptImageError,"UnableToReadImageData");
+  if ((((tga_info.bits_per_pixel <= 1) || (tga_info.bits_per_pixel >= 17)) &&
+       (tga_info.bits_per_pixel != 24) && (tga_info.bits_per_pixel != 32)))
+    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+  /*
+    Initialize image structure.
+  */
+  image->columns=tga_info.width;
+  image->rows=tga_info.height;
+  alpha_bits=(tga_info.attributes & 0x0FU);
+  image->alpha_trait=(alpha_bits > 0) || (tga_info.bits_per_pixel == 32) ||
+    (tga_info.colormap_size == 32) ?  BlendPixelTrait : UndefinedPixelTrait;
+  if ((tga_info.image_type != TGAColormap) &&
+      (tga_info.image_type != TGARLEColormap))
+    image->depth=(size_t) ((tga_info.bits_per_pixel <= 8) ? 8 :
+      (tga_info.bits_per_pixel <= 16) ? 5 :
+      (tga_info.bits_per_pixel == 24) ? 8 :
+      (tga_info.bits_per_pixel == 32) ? 8 : 8);
+  else
+    image->depth=(size_t) ((tga_info.colormap_size <= 8) ? 8 :
+      (tga_info.colormap_size <= 16) ? 5 :
+      (tga_info.colormap_size == 24) ? 8 :
+      (tga_info.colormap_size == 32) ? 8 : 8);
+  if ((tga_info.image_type == TGAColormap) ||
+      (tga_info.image_type == TGAMonochrome) ||
+      (tga_info.image_type == TGARLEColormap) ||
+      (tga_info.image_type == TGARLEMonochrome))
+    image->storage_class=PseudoClass;
+  image->compression=NoCompression;
+  if ((tga_info.image_type == TGARLEColormap) ||
+      (tga_info.image_type == TGARLEMonochrome))
+    image->compression=RLECompression;
+  if (image->storage_class == PseudoClass)
+    {
+      if (tga_info.colormap_type != 0)
+        image->colors=tga_info.colormap_length;
+      else
+        {
+          size_t
+            one;
+
+          one=1;
+          image->colors=one << tga_info.bits_per_pixel;
+          if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
+            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+        }
+    }
+  if (tga_info.id_length != 0)
+    {
+      char
+        *comment;
+
+      size_t
+        length;
+
+      /*
+        TGA image comment.
+      */
+      length=(size_t) tga_info.id_length;
+      comment=(char *) NULL;
+      if (~length >= (MaxTextExtent-1))
+        comment=(char *) AcquireQuantumMemory(length+MaxTextExtent,
+          sizeof(*comment));
+      if (comment == (char *) NULL)
+        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
+      comment[tga_info.id_length]='\0';
+      (void) SetImageProperty(image,"comment",comment,exception);
+      comment=DestroyString(comment);
+    }
+  (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
+  pixel.alpha=(MagickRealType) OpaqueAlpha;
+  if (tga_info.colormap_type != 0)
+    {
+      /*
+        Read TGA raster colormap.
+      */
+      if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
+        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+      for (i=0; i < (ssize_t) image->colors; i++)
       {
-        if (tga_info.colormap_type != 0)
-          image->colors=tga_info.colormap_length;
-        else
+        switch (tga_info.colormap_size)
+        {
+          case 8:
+          default:
           {
-            image->colors=0x01U << tga_info.bits_per_pixel;
-            if (AcquireImageColormap(image,image->colors) == MagickFalse)
-              ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+            /*
+              Gray scale.
+            */
+            pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
+              ReadBlobByte(image));
+            pixel.green=pixel.red;
+            pixel.blue=pixel.red;
+            break;
           }
+          case 15:
+          case 16:
+          {
+            QuantumAny
+              range;
+
+            /*
+              5 bits each of red green and blue.
+            */
+            j=(unsigned char) ReadBlobByte(image);
+            k=(unsigned char) ReadBlobByte(image);
+            range=GetQuantumRange(5UL);
+            pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
+              range);
+            pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*(k & 0x03)
+              << 3)+(1UL*(j & 0xe0) >> 5),range);
+            pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
+            break;
+          }
+          case 24:
+          {
+            /*
+              8 bits each of blue, green and red.
+            */
+            pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
+              ReadBlobByte(image));
+            pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
+              ReadBlobByte(image));
+            pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
+              ReadBlobByte(image));
+            break;
+          }
+          case 32:
+          {
+            /*
+              8 bits each of blue, green, red, and alpha.
+            */
+            pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
+              ReadBlobByte(image));
+            pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
+              ReadBlobByte(image));
+            pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
+              ReadBlobByte(image));
+            pixel.alpha=(MagickRealType) ScaleCharToQuantum((unsigned char)
+              ReadBlobByte(image));
+            break;
+          }
+        }
+        image->colormap[i]=pixel;
       }
-    if (tga_info.id_length != 0)
-      {
-        char
-          *comment;
-
-        size_t
-          length;
-
-        /*
-          TGA image comment.
-        */
-        length=(size_t) tga_info.id_length;
-        comment=(char *) NULL;
-        if (~length >= MaxTextExtent)
-          comment=(char *) AcquireQuantumMemory(length+MaxTextExtent,
-            sizeof(*comment));
-        if (comment == (char *) NULL)
-          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-        count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
-        comment[tga_info.id_length]='\0';
-        (void) SetImageProperty(image,"comment",comment);
-        comment=DestroyString(comment);
-      }
-    (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
-    pixel.opacity=(Quantum) OpaqueOpacity;
-    if (tga_info.colormap_type != 0)
-      {
-        /*
-          Read TGA raster colormap.
-        */
-        if (AcquireImageColormap(image,image->colors) == MagickFalse)
-          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
-        for (i=0; i < (long) image->colors; i++)
+    }
+  /*
+    Convert TGA pixels to pixel packets.
+  */
+  base=0;
+  flag=0;
+  skip=MagickFalse;
+  real=0;
+  index=0;
+  runlength=0;
+  offset=0;
+  for (y=0; y < (ssize_t) image->rows; y++)
+  {
+    real=offset;
+    if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
+      real=image->rows-real-1;
+    q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
+    if (q == (Quantum *) NULL)
+      break;
+    for (x=0; x < (ssize_t) image->columns; x++)
+    {
+      if ((tga_info.image_type == TGARLEColormap) ||
+          (tga_info.image_type == TGARLERGB) ||
+          (tga_info.image_type == TGARLEMonochrome))
         {
-          switch (tga_info.colormap_size)
-          {
-            case 8:
-            default:
-            {
-              /*
-                Gray scale.
-              */
-              pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
-              pixel.green=pixel.red;
-              pixel.blue=pixel.red;
-              break;
-            }
-            case 15:
-            case 16:
+          if (runlength != 0)
             {
-              QuantumAny
-                range;
-
-              /*
-                5 bits each of red green and blue.
-              */
-              j=(unsigned char) ReadBlobByte(image);
-              k=(unsigned char) ReadBlobByte(image);
-              range=GetQuantumRange(5UL);
-              pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
-              pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
-                (1UL*(j & 0xe0) >> 5),range);
-              pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
-              break;
+              runlength--;
+              skip=flag != 0;
             }
-            case 24:
-            case 32:
+          else
             {
-              /*
-                8 bits each of blue, green and red.
-              */
-              pixel.blue=ScaleCharToQuantum((unsigned char)
-                ReadBlobByte(image));
-              pixel.green=ScaleCharToQuantum((unsigned char)
-                ReadBlobByte(image));
-              pixel.red=ScaleCharToQuantum((unsigned char)
-                ReadBlobByte(image));
-              break;
+              count=ReadBlob(image,1,&runlength);
+              if (count == 0)
+                ThrowReaderException(CorruptImageError,"UnableToReadImageData");
+              flag=runlength & 0x80;
+              if (flag != 0)
+                runlength-=128;
+              skip=MagickFalse;
             }
-          }
-          image->colormap[i]=pixel;
         }
-      }
-    if ((image_info->ping != MagickFalse) && (image_info->number_scenes != 0))
-      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
-        break;
-    /*
-      Convert TGA pixels to pixel packets.
-    */
-    base=0;
-    flag=0;
-    skip=MagickFalse;
-    real=0;
-    index=(IndexPacket) 0;
-    runlength=0;
-    offset=0;
-    for (y=0; y < (long) image->rows; y++)
-    {
-      real=offset;
-      if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
-        real=image->rows-real-1;
-      q=QueueAuthenticPixels(image,0,(long) real,image->columns,1,exception);
-      if (q == (PixelPacket *) NULL)
-        break;
-      indexes=GetAuthenticIndexQueue(image);
-      for (x=0; x < (long) image->columns; x++)
-      {
-        if ((tga_info.image_type == TGARLEColormap) ||
-            (tga_info.image_type == TGARLERGB) ||
-            (tga_info.image_type == TGARLEMonochrome))
+      if (skip == MagickFalse)
+        switch (tga_info.bits_per_pixel)
+        {
+          case 8:
+          default:
           {
-            if (runlength != 0)
-              {
-                runlength--;
-                skip=flag != 0;
-              }
+            /*
+              Gray scale.
+            */
+            index=(Quantum) ReadBlobByte(image);
+            if (tga_info.colormap_type != 0)
+              pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
+                1UL*index,exception)];
             else
               {
-                count=ReadBlob(image,1,&runlength);
-                if (count == 0)
-                  ThrowReaderException(CorruptImageError,
-                    "UnableToReadImageData");
-                flag=runlength & 0x80;
-                if (flag != 0)
-                  runlength-=128;
-                skip=MagickFalse;
+                pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
+                  index);
+                pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
+                  index);
+                pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
+                  index);
               }
+            break;
           }
-        if (skip == MagickFalse)
-          switch (tga_info.bits_per_pixel)
+          case 15:
+          case 16:
           {
-            case 8:
-            default:
-            {
-              /*
-                Gray scale.
-              */
-              index=(IndexPacket) ReadBlobByte(image);
-              if (tga_info.colormap_type != 0)
-                pixel=image->colormap[(long) ConstrainColormapIndex(image,
-                  1UL*index)];
-              else
-                {
-                  pixel.red=ScaleCharToQuantum((unsigned char) index);
-                  pixel.green=ScaleCharToQuantum((unsigned char) index);
-                  pixel.blue=ScaleCharToQuantum((unsigned char) index);
-                }
-              break;
-            }
-            case 15:
-            case 16:
-            {
-              QuantumAny
-                range;
-
-              /*
-                5 bits each of red green and blue.
-              */
-              j=(unsigned char) ReadBlobByte(image);
-              k=(unsigned char) ReadBlobByte(image);
-              range=GetQuantumRange(5UL);
-              pixel.red=ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,range);
-              pixel.green=ScaleAnyToQuantum((1UL*(k & 0x03) << 3)+
-                (1UL*(j & 0xe0) >> 5),range);
-              pixel.blue=ScaleAnyToQuantum(1UL*(j & 0x1f),range);
-              if (image->matte != MagickFalse)
-                pixel.opacity=(k & 0x80) != 0 ? (Quantum) OpaqueOpacity :
-                  (Quantum) TransparentOpacity; 
-              if (image->storage_class == PseudoClass)
-                index=ConstrainColormapIndex(image,((unsigned long) k << 8)+j);
-              break;
-            }
-            case 24:
-            case 32:
-            {
-              /*
-                8 bits each of blue green and red.
-              */
-              pixel.blue=ScaleCharToQuantum((unsigned char)
-                ReadBlobByte(image));
-              pixel.green=ScaleCharToQuantum((unsigned char)
-                ReadBlobByte(image));
-              pixel.red=ScaleCharToQuantum((unsigned char) ReadBlobByte(image));
-              if (tga_info.bits_per_pixel == 32)
-                pixel.opacity=(Quantum) (QuantumRange-ScaleCharToQuantum(
-                  (unsigned char) ReadBlobByte(image)));
-              break;
-            }
+            QuantumAny
+              range;
+
+            /*
+              5 bits each of RGB.
+            */
+            if (ReadBlob(image,2,pixels) != 2)
+              ThrowReaderException(CorruptImageError,"UnableToReadImageData");
+            j=pixels[0];
+            k=pixels[1];
+            range=GetQuantumRange(5UL);
+            pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
+              range);
+            pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*(k & 0x03)
+              << 3)+(1UL*(j & 0xe0) >> 5),range);
+            pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
+            if (image->alpha_trait == BlendPixelTrait)
+              pixel.alpha=(MagickRealType) ((k & 0x80) == 0 ? (Quantum)
+                OpaqueAlpha : (Quantum) TransparentAlpha); 
+            if (image->storage_class == PseudoClass)
+              index=ConstrainColormapIndex(image,((size_t) k << 8)+j,exception);
+            break;
           }
-        if (status == MagickFalse)
-          ThrowReaderException(CorruptImageError,"UnableToReadImageData");
-        if (image->storage_class == PseudoClass)
-          indexes[x]=index;
-        q->red=pixel.red;
-        q->green=pixel.green;
-        q->blue=pixel.blue;
-        if (image->matte != MagickFalse)
-          q->opacity=pixel.opacity;
-        q++;
-      }
-      if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
-        offset+=4;
-      else
-        if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
-          offset+=2;
-        else
-          offset++;
-      if (offset >= image->rows)
-        {
-          base++;
-          offset=base;
-        }
-      if (SyncAuthenticPixels(image,exception) == MagickFalse)
-        break;
-      if (image->previous == (Image *) NULL)
-        {
-          status=SetImageProgress(image,LoadImageTag,y,image->rows);
-          if (status == MagickFalse)
+          case 24:
+          {
+            /*
+              BGR pixels.
+            */
+            if (ReadBlob(image,3,pixels) != 3)
+              ThrowReaderException(CorruptImageError,"UnableToReadImageData");
+            pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
+            pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
+            pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
             break;
+          }
+          case 32:
+          {
+            /*
+              BGRA pixels.
+            */
+            if (ReadBlob(image,4,pixels) != 4)
+              ThrowReaderException(CorruptImageError,"UnableToReadImageData");
+            pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
+            pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
+            pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
+            pixel.alpha=(MagickRealType) ScaleCharToQuantum(pixels[3]);
+            break;
+          }
         }
+      if (status == MagickFalse)
+        ThrowReaderException(CorruptImageError,"UnableToReadImageData");
+      if (image->storage_class == PseudoClass)
+        SetPixelIndex(image,index,q);
+      SetPixelRed(image,ClampToQuantum(pixel.red),q);
+      SetPixelGreen(image,ClampToQuantum(pixel.green),q);
+      SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
+      if (image->alpha_trait == BlendPixelTrait)
+        SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
+      q+=GetPixelChannels(image);
     }
-    if (EOFBlob(image) != MagickFalse)
+    if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
+      offset+=4;
+    else
+      if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
+        offset+=2;
+      else
+        offset++;
+    if (offset >= image->rows)
       {
-        ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
-          image->filename);
-        break;
+        base++;
+        offset=base;
       }
-    /*
-      Proceed to next image.
-    */
-    if (image_info->number_scenes != 0)
-      if (image->scene >= (image_info->scene+image_info->number_scenes-1))
-        break;
-    count=ReadBlob(image,1,&tga_info.id_length);
-    tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
-    tga_info.image_type=(unsigned char) ReadBlobByte(image);
-    status=((tga_info.image_type == TGAColormap) ||
-      (tga_info.image_type == TGARGB) ||
-      (tga_info.image_type == TGAMonochrome) ||
-      (tga_info.image_type == TGARLEColormap) ||
-      (tga_info.image_type == TGARLERGB) ||
-      (tga_info.image_type == TGARLEMonochrome)) ? MagickTrue : MagickFalse;
-    if (status == MagickTrue)
+    if (SyncAuthenticPixels(image,exception) == MagickFalse)
+      break;
+    if (image->previous == (Image *) NULL)
       {
-        /*
-          Allocate next image structure.
-        */
-        AcquireNextImage(image_info,image);
-        if (GetNextImageInList(image) == (Image *) NULL)
-          {
-            image=DestroyImageList(image);
-            return((Image *) NULL);
-          }
-        image=SyncNextImageInList(image);
-        status=SetImageProgress(image,LoadImagesTag,TellBlob(image),
-          GetBlobSize(image));
+        status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
+          image->rows);
         if (status == MagickFalse)
           break;
       }
-  } while (status == MagickTrue);
+  }
+  if (EOFBlob(image) != MagickFalse)
+    ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
+      image->filename);
   (void) CloseBlob(image);
   return(GetFirstImageInList(image));
 }
@@ -538,10 +539,10 @@ static Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
 %
 %  The format of the RegisterTGAImage method is:
 %
-%      unsigned long RegisterTGAImage(void)
+%      size_t RegisterTGAImage(void)
 %
 */
-ModuleExport unsigned long RegisterTGAImage(void)
+ModuleExport size_t RegisterTGAImage(void)
 {
   MagickInfo
     *entry;
@@ -549,24 +550,28 @@ ModuleExport unsigned long RegisterTGAImage(void)
   entry=SetMagickInfo("ICB");
   entry->decoder=(DecodeImageHandler *) ReadTGAImage;
   entry->encoder=(EncodeImageHandler *) WriteTGAImage;
+  entry->adjoin=MagickFalse;
   entry->description=ConstantString("Truevision Targa image");
   entry->module=ConstantString("TGA");
   (void) RegisterMagickInfo(entry);
   entry=SetMagickInfo("TGA");
   entry->decoder=(DecodeImageHandler *) ReadTGAImage;
   entry->encoder=(EncodeImageHandler *) WriteTGAImage;
+  entry->adjoin=MagickFalse;
   entry->description=ConstantString("Truevision Targa image");
   entry->module=ConstantString("TGA");
   (void) RegisterMagickInfo(entry);
   entry=SetMagickInfo("VDA");
   entry->decoder=(DecodeImageHandler *) ReadTGAImage;
   entry->encoder=(EncodeImageHandler *) WriteTGAImage;
+  entry->adjoin=MagickFalse;
   entry->description=ConstantString("Truevision Targa image");
   entry->module=ConstantString("TGA");
   (void) RegisterMagickInfo(entry);
   entry=SetMagickInfo("VST");
   entry->decoder=(DecodeImageHandler *) ReadTGAImage;
   entry->encoder=(EncodeImageHandler *) WriteTGAImage;
+  entry->adjoin=MagickFalse;
   entry->description=ConstantString("Truevision Targa image");
   entry->module=ConstantString("TGA");
   (void) RegisterMagickInfo(entry);
@@ -616,7 +621,8 @@ ModuleExport void UnregisterTGAImage(void)
 %
 %  The format of the WriteTGAImage method is:
 %
-%      MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
+%      MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
+%        Image *image,ExceptionInfo *exception)
 %
 %  A description of each parameter follows.
 %
@@ -633,7 +639,8 @@ static inline size_t MagickMin(const size_t x,const size_t y)
   return(y);
 }
 
-static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
+static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
+  ExceptionInfo *exception)
 {
 #define TargaColormap 1
 #define TargaRGB 2
@@ -670,32 +677,24 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
   const char
     *value;
 
-  long
-    y;
-
   MagickBooleanType
     status;
 
-  MagickOffsetType
-    scene;
-
-  register const IndexPacket
-    *indexes;
-
-  register const PixelPacket
+  register const Quantum
     *p;
 
-  register long
+  register ssize_t
     x;
 
-  register long
+  register ssize_t
     i;
 
   register unsigned char
     *q;
 
   ssize_t
-    count;
+    count,
+    y;
 
   TargaInfo
     targa_info;
@@ -712,156 +711,144 @@ static MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image)
   assert(image->signature == MagickSignature);
   if (image->debug != MagickFalse)
     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
-  status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
+  assert(exception != (ExceptionInfo *) NULL);
+  assert(exception->signature == MagickSignature);
+  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
   if (status == MagickFalse)
     return(status);
-  scene=0;
-  do
-  {
-    /*
-      Initialize TGA raster file header.
-    */
-    if ((image->columns > 65535L) || (image->rows > 65535L))
-      ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
-    if (image->colorspace != RGBColorspace)
-      (void) TransformImageColorspace(image,RGBColorspace);
-    targa_info.id_length=0;
-    value=GetImageProperty(image,"comment");
-    if (value != (const char *) NULL)
-      targa_info.id_length=(unsigned char) MagickMin(strlen(value),255);
-    targa_info.colormap_type=0;
-    targa_info.colormap_index=0;
-    targa_info.colormap_length=0;
-    targa_info.colormap_size=0;
-    targa_info.x_origin=0;
-    targa_info.y_origin=0;
-    targa_info.width=(unsigned short) image->columns;
-    targa_info.height=(unsigned short) image->rows;
-    targa_info.bits_per_pixel=8;
-    targa_info.attributes=0;
-    if ((image_info->type != TrueColorType) &&
-        (image_info->type != TrueColorMatteType) &&
-        (image_info->type != PaletteType) &&
-        (image->matte == MagickFalse) &&
-        (IsGrayImage(image,&image->exception) != MagickFalse))
-      targa_info.image_type=TargaMonochrome;
+  /*
+    Initialize TGA raster file header.
+  */
+  if ((image->columns > 65535L) || (image->rows > 65535L))
+    ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
+  (void) TransformImageColorspace(image,sRGBColorspace,exception);
+  targa_info.id_length=0;
+  value=GetImageProperty(image,"comment",exception);
+  if (value != (const char *) NULL)
+    targa_info.id_length=(unsigned char) MagickMin(strlen(value),255);
+  targa_info.colormap_type=0;
+  targa_info.colormap_index=0;
+  targa_info.colormap_length=0;
+  targa_info.colormap_size=0;
+  targa_info.x_origin=0;
+  targa_info.y_origin=0;
+  targa_info.width=(unsigned short) image->columns;
+  targa_info.height=(unsigned short) image->rows;
+  targa_info.bits_per_pixel=8;
+  targa_info.attributes=0;
+  if ((image_info->type != TrueColorType) &&
+      (image_info->type != TrueColorMatteType) &&
+      (image_info->type != PaletteType) &&
+      (image->alpha_trait != BlendPixelTrait) &&
+      (IsImageGray(image,exception) != MagickFalse))
+    targa_info.image_type=TargaMonochrome;
+  else
+    if ((image->storage_class == DirectClass) || (image->colors > 256))
+      {
+        /*
+          Full color TGA raster.
+        */
+        targa_info.image_type=TargaRGB;
+        targa_info.bits_per_pixel=24;
+        if (image->alpha_trait == BlendPixelTrait)
+          {
+            targa_info.bits_per_pixel=32;
+            targa_info.attributes=8;  /* # of alpha bits */
+          }
+      }
     else
-      if ((image->storage_class == DirectClass) || (image->colors > 256))
-        {
-          /*
-            Full color TGA raster.
-          */
-          targa_info.image_type=TargaRGB;
-          targa_info.bits_per_pixel=24;
-          if (image->matte != MagickFalse)
-            {
-              targa_info.bits_per_pixel=32;
-              targa_info.attributes=8;  /* # of alpha bits */
-            }
-        }
-      else
-        {
-          /*
-            Colormapped TGA raster.
-          */
-          targa_info.image_type=TargaColormap;
-          targa_info.colormap_type=1;
-          targa_info.colormap_length=(unsigned short) image->colors;
-          targa_info.colormap_size=24;
-        }
-    /*
-      Write TGA header.
-    */
-    (void) WriteBlobByte(image,targa_info.id_length);
-    (void) WriteBlobByte(image,targa_info.colormap_type);
-    (void) WriteBlobByte(image,targa_info.image_type);
-    (void) WriteBlobLSBShort(image,targa_info.colormap_index);
-    (void) WriteBlobLSBShort(image,targa_info.colormap_length);
-    (void) WriteBlobByte(image,targa_info.colormap_size);
-    (void) WriteBlobLSBShort(image,targa_info.x_origin);
-    (void) WriteBlobLSBShort(image,targa_info.y_origin);
-    (void) WriteBlobLSBShort(image,targa_info.width);
-    (void) WriteBlobLSBShort(image,targa_info.height);
-    (void) WriteBlobByte(image,targa_info.bits_per_pixel);
-    (void) WriteBlobByte(image,targa_info.attributes);
-    if (targa_info.id_length != 0)
-      (void) WriteBlob(image,targa_info.id_length,(unsigned char *)
-        value);
-    if (targa_info.image_type == TargaColormap)
       {
-        unsigned char
-          *targa_colormap;
-
         /*
-          Dump colormap to file (blue, green, red byte order).
+          Colormapped TGA raster.
         */
-        targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
-          targa_info.colormap_length,3UL*sizeof(*targa_colormap));
-        if (targa_colormap == (unsigned char *) NULL)
-          ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
-        q=targa_colormap;
-        for (i=0; i < (long) image->colors; i++)
-        {
-          *q++=ScaleQuantumToChar(image->colormap[i].blue);
-          *q++=ScaleQuantumToChar(image->colormap[i].green);
-          *q++=ScaleQuantumToChar(image->colormap[i].red);
-        }
-        (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length),
-          targa_colormap);
-        targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
+        targa_info.image_type=TargaColormap;
+        targa_info.colormap_type=1;
+        targa_info.colormap_length=(unsigned short) image->colors;
+        targa_info.colormap_size=24;
       }
-    /*
-      Convert MIFF to TGA raster pixels.
-    */
-    count=(ssize_t) (targa_info.bits_per_pixel*targa_info.width)/8;
-    targa_pixels=(unsigned char *) AcquireQuantumMemory((size_t) count,
-      sizeof(*targa_pixels));
-    if (targa_pixels == (unsigned char *) NULL)
-      ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
-    for (y=(long) (image->rows-1); y >= 0; y--)
+  /*
+    Write TGA header.
+  */
+  (void) WriteBlobByte(image,targa_info.id_length);
+  (void) WriteBlobByte(image,targa_info.colormap_type);
+  (void) WriteBlobByte(image,targa_info.image_type);
+  (void) WriteBlobLSBShort(image,targa_info.colormap_index);
+  (void) WriteBlobLSBShort(image,targa_info.colormap_length);
+  (void) WriteBlobByte(image,targa_info.colormap_size);
+  (void) WriteBlobLSBShort(image,targa_info.x_origin);
+  (void) WriteBlobLSBShort(image,targa_info.y_origin);
+  (void) WriteBlobLSBShort(image,targa_info.width);
+  (void) WriteBlobLSBShort(image,targa_info.height);
+  (void) WriteBlobByte(image,targa_info.bits_per_pixel);
+  (void) WriteBlobByte(image,targa_info.attributes);
+  if (targa_info.id_length != 0)
+    (void) WriteBlob(image,targa_info.id_length,(unsigned char *)
+      value);
+  if (targa_info.image_type == TargaColormap)
     {
-      p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
-      if (p == (const PixelPacket *) NULL)
-        break;
-      q=targa_pixels;
-      indexes=GetVirtualIndexQueue(image);
-      for (x=0; x < (long) image->columns; x++)
+      unsigned char
+        *targa_colormap;
+
+      /*
+        Dump colormap to file (blue, green, red byte order).
+      */
+      targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
+        targa_info.colormap_length,3UL*sizeof(*targa_colormap));
+      if (targa_colormap == (unsigned char *) NULL)
+        ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
+      q=targa_colormap;
+      for (i=0; i < (ssize_t) image->colors; i++)
       {
-        if (targa_info.image_type == TargaColormap)
-          *q++=(unsigned char) indexes[x];
-        else
-          if (targa_info.image_type == TargaMonochrome)
-            *q++=(unsigned char) ScaleQuantumToChar(PixelIntensityToQuantum(p));
-          else
-            {
-              *q++=ScaleQuantumToChar(GetBluePixelComponent(p));
-              *q++=ScaleQuantumToChar(GetGreenPixelComponent(p));
-              *q++=ScaleQuantumToChar(GetRedPixelComponent(p));
-              if (image->matte != MagickFalse)
-                *q++=(unsigned char) ScaleQuantumToChar((Quantum)
-                  (GetAlphaPixelComponent(p)));
-              if (image->colorspace == CMYKColorspace)
-                *q++=ScaleQuantumToChar(indexes[x]);
-            }
-        p++;
+        *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
+        *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
+        *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
       }
-      (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
-      if (image->previous == (Image *) NULL)
-        {
-          status=SetImageProgress(image,SaveImageTag,y,image->rows);
-          if (status == MagickFalse)
-            break;
-        }
+      (void) WriteBlob(image,(size_t) (3*targa_info.colormap_length),
+        targa_colormap);
+      targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
     }
-    targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
-    if (GetNextImageInList(image) == (Image *) NULL)
-      break;
-    image=SyncNextImageInList(image);
-    status=SetImageProgress(image,SaveImagesTag,scene++,
-      GetImageListLength(image));
-    if (status == MagickFalse)
+  /*
+    Convert MIFF to TGA raster pixels.
+  */
+  count=(ssize_t) (targa_info.bits_per_pixel*targa_info.width)/8;
+  targa_pixels=(unsigned char *) AcquireQuantumMemory((size_t) count,
+    sizeof(*targa_pixels));
+  if (targa_pixels == (unsigned char *) NULL)
+    ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
+  for (y=(ssize_t) (image->rows-1); y >= 0; y--)
+  {
+    p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+    if (p == (const Quantum *) NULL)
       break;
-  } while (image_info->adjoin != MagickFalse);
+    q=targa_pixels;
+    for (x=0; x < (ssize_t) image->columns; x++)
+    {
+      if (targa_info.image_type == TargaColormap)
+        *q++=(unsigned char) GetPixelIndex(image,p);
+      else
+        if (targa_info.image_type == TargaMonochrome)
+          *q++=(unsigned char) ScaleQuantumToChar((ClampToQuantum(
+            GetPixelLuma(image,p))));
+        else
+          {
+            *q++=ScaleQuantumToChar(GetPixelBlue(image,p));
+            *q++=ScaleQuantumToChar(GetPixelGreen(image,p));
+            *q++=ScaleQuantumToChar(GetPixelRed(image,p));
+            if (image->alpha_trait == BlendPixelTrait)
+              *q++=(unsigned char) ScaleQuantumToChar(GetPixelAlpha(image,p));
+          }
+      p+=GetPixelChannels(image);
+    }
+    (void) WriteBlob(image,(size_t) (q-targa_pixels),targa_pixels);
+    if (image->previous == (Image *) NULL)
+      {
+        status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+          image->rows);
+        if (status == MagickFalse)
+          break;
+      }
+  }
+  targa_pixels=(unsigned char *) RelinquishMagickMemory(targa_pixels);
   (void) CloseBlob(image);
   return(MagickTrue);
 }