13ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
23ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
43ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
53ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
63ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                            TTTTT   GGGG   AAA                               %
73ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                              T    G      A   A                              %
83ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                              T    G  GG  AAAAA                              %
93ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                              T    G   G  A   A                              %
103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                              T     GGG   A   A                              %
113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                    Read/Write Truevision Targa Image Format                 %
143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                              Software Design                                %
16de984cdc3631106b1cbbb8d3972b76a0fc27e8e8cristy%                                   Cristy                                    %
173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                 July 1992                                   %
183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
207ce65e7125a4e1df1a274ce373c537a9df9c16cdCristy%  Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization      %
213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  dedicated to making software imaging solutions freely available.           %
223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  You may not use this file except in compliance with the License.  You may  %
243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  obtain a copy of the License at                                            %
253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    http://www.imagemagick.org/script/license.php                            %
273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  Unless required by applicable law or agreed to in writing, software        %
293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  distributed under the License is distributed on an "AS IS" BASIS,          %
303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  See the License for the specific language governing permissions and        %
323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  limitations under the License.                                             %
333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Include declarations.
413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
424c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/studio.h"
439ac195506bdd2a46b0489d24f772a989be6ea46cdirk#include "MagickCore/artifact.h"
444c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/attribute.h"
454c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/blob.h"
464c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/blob-private.h"
474c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/cache.h"
484c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/color-private.h"
494c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/colormap.h"
504c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/colormap-private.h"
514c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/colorspace.h"
52510d06a3f7063e91993e13d546d5685048248074cristy#include "MagickCore/colorspace-private.h"
534c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/exception.h"
544c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/exception-private.h"
554c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/image.h"
564c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/image-private.h"
574c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/list.h"
584c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/magick.h"
594c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/memory_.h"
604c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/monitor.h"
614c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/monitor-private.h"
629ac195506bdd2a46b0489d24f772a989be6ea46cdirk#include "MagickCore/option.h"
634c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/pixel-accessor.h"
644c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/property.h"
654c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/quantum-private.h"
664c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/static.h"
674c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/string_.h"
684c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/module.h"
6975652a86d1cd6e8bdd2967145fcd003415004219dirk
7075652a86d1cd6e8bdd2967145fcd003415004219dirk/*
7175652a86d1cd6e8bdd2967145fcd003415004219dirk  Enumerated declaractions.
7275652a86d1cd6e8bdd2967145fcd003415004219dirk*/
7375652a86d1cd6e8bdd2967145fcd003415004219dirktypedef enum
7475652a86d1cd6e8bdd2967145fcd003415004219dirk{
7575652a86d1cd6e8bdd2967145fcd003415004219dirk  TGAColormap = 1,
7675652a86d1cd6e8bdd2967145fcd003415004219dirk  TGARGB = 2,
7775652a86d1cd6e8bdd2967145fcd003415004219dirk  TGAMonochrome = 3,
7875652a86d1cd6e8bdd2967145fcd003415004219dirk  TGARLEColormap = 9,
7975652a86d1cd6e8bdd2967145fcd003415004219dirk  TGARLERGB = 10,
8075652a86d1cd6e8bdd2967145fcd003415004219dirk  TGARLEMonochrome = 11
8175652a86d1cd6e8bdd2967145fcd003415004219dirk} TGAImageType;
8275652a86d1cd6e8bdd2967145fcd003415004219dirk
8375652a86d1cd6e8bdd2967145fcd003415004219dirk/*
8475652a86d1cd6e8bdd2967145fcd003415004219dirk  Typedef declaractions.
8575652a86d1cd6e8bdd2967145fcd003415004219dirk*/
8675652a86d1cd6e8bdd2967145fcd003415004219dirktypedef struct _TGAInfo
8775652a86d1cd6e8bdd2967145fcd003415004219dirk{
8875652a86d1cd6e8bdd2967145fcd003415004219dirk  TGAImageType
8975652a86d1cd6e8bdd2967145fcd003415004219dirk    image_type;
9075652a86d1cd6e8bdd2967145fcd003415004219dirk
9175652a86d1cd6e8bdd2967145fcd003415004219dirk  unsigned char
9275652a86d1cd6e8bdd2967145fcd003415004219dirk    id_length,
9375652a86d1cd6e8bdd2967145fcd003415004219dirk    colormap_type;
9475652a86d1cd6e8bdd2967145fcd003415004219dirk
9575652a86d1cd6e8bdd2967145fcd003415004219dirk  unsigned short
9675652a86d1cd6e8bdd2967145fcd003415004219dirk    colormap_index,
9775652a86d1cd6e8bdd2967145fcd003415004219dirk    colormap_length;
9875652a86d1cd6e8bdd2967145fcd003415004219dirk
9975652a86d1cd6e8bdd2967145fcd003415004219dirk  unsigned char
10075652a86d1cd6e8bdd2967145fcd003415004219dirk    colormap_size;
10175652a86d1cd6e8bdd2967145fcd003415004219dirk
10275652a86d1cd6e8bdd2967145fcd003415004219dirk  unsigned short
10375652a86d1cd6e8bdd2967145fcd003415004219dirk    x_origin,
10475652a86d1cd6e8bdd2967145fcd003415004219dirk    y_origin,
10575652a86d1cd6e8bdd2967145fcd003415004219dirk    width,
10675652a86d1cd6e8bdd2967145fcd003415004219dirk    height;
10775652a86d1cd6e8bdd2967145fcd003415004219dirk
10875652a86d1cd6e8bdd2967145fcd003415004219dirk  unsigned char
10975652a86d1cd6e8bdd2967145fcd003415004219dirk    bits_per_pixel,
11075652a86d1cd6e8bdd2967145fcd003415004219dirk    attributes;
11175652a86d1cd6e8bdd2967145fcd003415004219dirk} TGAInfo;
1123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
1143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Forward declarations.
1153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
1163ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic MagickBooleanType
1173a37efd7ece979e9c63dc8f2f2d3816bab8b1156cristy  WriteTGAImage(const ImageInfo *,Image *,ExceptionInfo *);
1183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
1203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
1223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
1233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
1243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   R e a d T G A I m a g e                                                   %
1253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
1263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
1273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
1283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
1303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  ReadTGAImage() reads a Truevision TGA image file and returns it.
1313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  It allocates the memory necessary for the new Image structure and returns
1323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  a pointer to the new image.
1333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
1343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the ReadTGAImage method is:
1353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
1363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      Image *ReadTGAImage(const ImageInfo *image_info,ExceptionInfo *exception)
1373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
1383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  A description of each parameter follows:
1393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
1403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o image_info: the image info.
1413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
1423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o exception: return any errors or warnings in this structure.
1433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
1443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
14575652a86d1cd6e8bdd2967145fcd003415004219dirkstatic Image *ReadTGAImage(const ImageInfo *image_info,
14675652a86d1cd6e8bdd2967145fcd003415004219dirk  ExceptionInfo *exception)
1473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
1483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Image
1493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *image;
1503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  MagickBooleanType
1523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    status;
1533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
154101ab708b0574518ac5715da4d3915400e9df79acristy  PixelInfo
1553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    pixel;
1563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1574c08aed51c5899665ade97263692328eea4af106cristy  Quantum
1584c08aed51c5899665ade97263692328eea4af106cristy    index;
1593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1604c08aed51c5899665ade97263692328eea4af106cristy  register Quantum
161c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    *q;
162c6da28e61bb609d2b2cfdcc7752106c973415edbcristy
163bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  register ssize_t
1643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    i,
1653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    x;
1663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
167c6da28e61bb609d2b2cfdcc7752106c973415edbcristy  size_t
168c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    base,
169c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    flag,
170c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    offset,
171c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    real,
172c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    skip;
1733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  ssize_t
175c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    count,
176c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    y;
1773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  TGAInfo
1793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    tga_info;
1803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned char
1823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    j,
1833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    k,
1845dd83a5b3ade92272cda240514df53206cd3f8b7cristy    pixels[4],
1853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    runlength;
1863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
18718307f17b92df2cdaefc13f8ea486b16da9d0d2dcristy  unsigned int
18818307f17b92df2cdaefc13f8ea486b16da9d0d2dcristy    alpha_bits;
18918307f17b92df2cdaefc13f8ea486b16da9d0d2dcristy
1903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
1913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Open image file.
1923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
1933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(image_info != (const ImageInfo *) NULL);
194e1c94d9d25db6b0dd7a5028ffee31d1057855d73cristy  assert(image_info->signature == MagickCoreSignature);
1953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (image_info->debug != MagickFalse)
1963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
1973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      image_info->filename);
1983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(exception != (ExceptionInfo *) NULL);
199e1c94d9d25db6b0dd7a5028ffee31d1057855d73cristy  assert(exception->signature == MagickCoreSignature);
2009950d57e1124b73f684fb5946e206994cefda628cristy  image=AcquireImage(image_info,exception);
2013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
2023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (status == MagickFalse)
2033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
2043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      image=DestroyImageList(image);
2053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      return((Image *) NULL);
2063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
2073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
2083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Read TGA header information.
2093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
2103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,1,&tga_info.id_length);
2113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  tga_info.colormap_type=(unsigned char) ReadBlobByte(image);
21275652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.image_type=(TGAImageType) ReadBlobByte(image);
2139814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if ((count != 1) ||
2149814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      ((tga_info.image_type != TGAColormap) &&
2159814091f6402f77ef31f82a48d6bdbb53ae6c087cristy       (tga_info.image_type != TGARGB) &&
2169814091f6402f77ef31f82a48d6bdbb53ae6c087cristy       (tga_info.image_type != TGAMonochrome) &&
2179814091f6402f77ef31f82a48d6bdbb53ae6c087cristy       (tga_info.image_type != TGARLEColormap) &&
2189814091f6402f77ef31f82a48d6bdbb53ae6c087cristy       (tga_info.image_type != TGARLERGB) &&
2199814091f6402f77ef31f82a48d6bdbb53ae6c087cristy       (tga_info.image_type != TGARLEMonochrome)) ||
2209814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (((tga_info.image_type == TGAColormap) ||
2219814091f6402f77ef31f82a48d6bdbb53ae6c087cristy       (tga_info.image_type == TGARLEColormap)) &&
2229814091f6402f77ef31f82a48d6bdbb53ae6c087cristy       (tga_info.colormap_type == 0)))
2239814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
2249814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  tga_info.colormap_index=ReadBlobLSBShort(image);
2259814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  tga_info.colormap_length=ReadBlobLSBShort(image);
2269814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  tga_info.colormap_size=(unsigned char) ReadBlobByte(image);
2279814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  tga_info.x_origin=ReadBlobLSBShort(image);
2289814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  tga_info.y_origin=ReadBlobLSBShort(image);
2299814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  tga_info.width=(unsigned short) ReadBlobLSBShort(image);
2309814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  tga_info.height=(unsigned short) ReadBlobLSBShort(image);
2319814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  tga_info.bits_per_pixel=(unsigned char) ReadBlobByte(image);
2329814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  tga_info.attributes=(unsigned char) ReadBlobByte(image);
2339814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if (EOFBlob(image) != MagickFalse)
2349814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    ThrowReaderException(CorruptImageError,"UnableToReadImageData");
2359814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if ((((tga_info.bits_per_pixel <= 1) || (tga_info.bits_per_pixel >= 17)) &&
2369814091f6402f77ef31f82a48d6bdbb53ae6c087cristy       (tga_info.bits_per_pixel != 24) && (tga_info.bits_per_pixel != 32)))
2379814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
2389814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  /*
2399814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    Initialize image structure.
2409814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  */
2419814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  image->columns=tga_info.width;
2429814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  image->rows=tga_info.height;
24318307f17b92df2cdaefc13f8ea486b16da9d0d2dcristy  alpha_bits=(tga_info.attributes & 0x0FU);
244ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy  image->alpha_trait=(alpha_bits > 0) || (tga_info.bits_per_pixel == 32) ||
245ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy    (tga_info.colormap_size == 32) ?  BlendPixelTrait : UndefinedPixelTrait;
2469814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if ((tga_info.image_type != TGAColormap) &&
2479814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (tga_info.image_type != TGARLEColormap))
2489814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    image->depth=(size_t) ((tga_info.bits_per_pixel <= 8) ? 8 :
2499814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (tga_info.bits_per_pixel <= 16) ? 5 :
2509814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (tga_info.bits_per_pixel == 24) ? 8 :
2519814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (tga_info.bits_per_pixel == 32) ? 8 : 8);
2529814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  else
2539814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    image->depth=(size_t) ((tga_info.colormap_size <= 8) ? 8 :
2549814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (tga_info.colormap_size <= 16) ? 5 :
2559814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (tga_info.colormap_size == 24) ? 8 :
2569814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (tga_info.colormap_size == 32) ? 8 : 8);
2579814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if ((tga_info.image_type == TGAColormap) ||
2589814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (tga_info.image_type == TGAMonochrome) ||
2599814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (tga_info.image_type == TGARLEColormap) ||
2609814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (tga_info.image_type == TGARLEMonochrome))
2619814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    image->storage_class=PseudoClass;
2629814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  image->compression=NoCompression;
2639814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if ((tga_info.image_type == TGARLEColormap) ||
26458ee16ae537f3029ba79bd3d9848f6ec31381fe0dirk      (tga_info.image_type == TGARLEMonochrome) ||
26558ee16ae537f3029ba79bd3d9848f6ec31381fe0dirk      (tga_info.image_type == TGARLERGB))
2669814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    image->compression=RLECompression;
2679814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if (image->storage_class == PseudoClass)
2689814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    {
2699814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      if (tga_info.colormap_type != 0)
270c9eb04ca2174d8500f59e7e3e32148069610223ecristy        image->colors=tga_info.colormap_index+tga_info.colormap_length;
2719814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      else
2729814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        {
2739814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          size_t
2749814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            one;
2759814091f6402f77ef31f82a48d6bdbb53ae6c087cristy
2769814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          one=1;
2779814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          image->colors=one << tga_info.bits_per_pixel;
278018f07f7333b25743d0afff892450cebdb905c1acristy          if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
2799814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2809814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        }
2819814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    }
2829814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if (tga_info.id_length != 0)
2839814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    {
2849814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      char
2859814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        *comment;
2869814091f6402f77ef31f82a48d6bdbb53ae6c087cristy
2879814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      size_t
2889814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        length;
2899814091f6402f77ef31f82a48d6bdbb53ae6c087cristy
2909814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      /*
2919814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        TGA image comment.
2929814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      */
2939814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      length=(size_t) tga_info.id_length;
2949814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      comment=(char *) NULL;
295151b66dffc9e3c2e8c4f8cdaca37ff987ca0f497cristy      if (~length >= (MagickPathExtent-1))
296151b66dffc9e3c2e8c4f8cdaca37ff987ca0f497cristy        comment=(char *) AcquireQuantumMemory(length+MagickPathExtent,
2979814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          sizeof(*comment));
2989814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      if (comment == (char *) NULL)
2999814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3009814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      count=ReadBlob(image,tga_info.id_length,(unsigned char *) comment);
3019814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      comment[tga_info.id_length]='\0';
302d15e65928aec551b7388c2863de3e3e628e2e0ddcristy      (void) SetImageProperty(image,"comment",comment,exception);
3039814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      comment=DestroyString(comment);
3049814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    }
3059ac195506bdd2a46b0489d24f772a989be6ea46cdirk  if (tga_info.attributes & (1UL << 4))
3069ac195506bdd2a46b0489d24f772a989be6ea46cdirk    {
3079ac195506bdd2a46b0489d24f772a989be6ea46cdirk      if (tga_info.attributes & (1UL << 5))
3089ac195506bdd2a46b0489d24f772a989be6ea46cdirk        SetImageArtifact(image,"tga:image-origin","TopRight");
3099ac195506bdd2a46b0489d24f772a989be6ea46cdirk      else
3109ac195506bdd2a46b0489d24f772a989be6ea46cdirk        SetImageArtifact(image,"tga:image-origin","BottomRight");
3119ac195506bdd2a46b0489d24f772a989be6ea46cdirk    }
3129ac195506bdd2a46b0489d24f772a989be6ea46cdirk  else
3139ac195506bdd2a46b0489d24f772a989be6ea46cdirk    {
3149ac195506bdd2a46b0489d24f772a989be6ea46cdirk      if (tga_info.attributes & (1UL << 5))
3159ac195506bdd2a46b0489d24f772a989be6ea46cdirk        SetImageArtifact(image,"tga:image-origin","TopLeft");
3169ac195506bdd2a46b0489d24f772a989be6ea46cdirk      else
3179ac195506bdd2a46b0489d24f772a989be6ea46cdirk        SetImageArtifact(image,"tga:image-origin","BottomLeft");
3189ac195506bdd2a46b0489d24f772a989be6ea46cdirk    }
319e1b038801e940dd2540e779b9f9859b9abd1f289dirk  if (image_info->ping != MagickFalse)
320e1b038801e940dd2540e779b9f9859b9abd1f289dirk    {
321e1b038801e940dd2540e779b9f9859b9abd1f289dirk      (void) CloseBlob(image);
322e1b038801e940dd2540e779b9f9859b9abd1f289dirk      return(image);
323e1b038801e940dd2540e779b9f9859b9abd1f289dirk    }
324acabb847a592ca5e430c1c0949d03acfc0b78bb9cristy  status=SetImageExtent(image,image->columns,image->rows,exception);
325acabb847a592ca5e430c1c0949d03acfc0b78bb9cristy  if (status == MagickFalse)
326acabb847a592ca5e430c1c0949d03acfc0b78bb9cristy    return(DestroyImageList(image));
3279814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
32877b33baacfae08c3794dd19df1862fb645ebe265cristy  pixel.alpha=(MagickRealType) OpaqueAlpha;
3299814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if (tga_info.colormap_type != 0)
3309814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    {
3319814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      /*
3329814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        Read TGA raster colormap.
3339814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      */
3344f68e9661518463fca523c9726bb5d940a2aa6d8Cristy      if (image->colors < tga_info.colormap_index)
3354f68e9661518463fca523c9726bb5d940a2aa6d8Cristy        image->colors=tga_info.colormap_index;
336018f07f7333b25743d0afff892450cebdb905c1acristy      if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
3379814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
338c9eb04ca2174d8500f59e7e3e32148069610223ecristy      for (i=0; i < (ssize_t) tga_info.colormap_index; i++)
339c9eb04ca2174d8500f59e7e3e32148069610223ecristy        image->colormap[i]=pixel;
340c9eb04ca2174d8500f59e7e3e32148069610223ecristy      for ( ; i < (ssize_t) image->colors; i++)
3413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
3429814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        switch (tga_info.colormap_size)
3439814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        {
3449814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          case 8:
3459814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          default:
3463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
3479814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            /*
3489814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              Gray scale.
3499814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            */
35077b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
35177b33baacfae08c3794dd19df1862fb645ebe265cristy              ReadBlobByte(image));
3529814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            pixel.green=pixel.red;
3539814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            pixel.blue=pixel.red;
3549814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            break;
3553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
3569814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          case 15:
3579814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          case 16:
3589814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          {
3599814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            QuantumAny
3609814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              range;
3619814091f6402f77ef31f82a48d6bdbb53ae6c087cristy
3629814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            /*
3639814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              5 bits each of red green and blue.
3649814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            */
3659814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            j=(unsigned char) ReadBlobByte(image);
3669814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            k=(unsigned char) ReadBlobByte(image);
3679814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            range=GetQuantumRange(5UL);
36877b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
36977b33baacfae08c3794dd19df1862fb645ebe265cristy              range);
37077b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*(k & 0x03)
37177b33baacfae08c3794dd19df1862fb645ebe265cristy              << 3)+(1UL*(j & 0xe0) >> 5),range);
37277b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
3739814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            break;
3749814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          }
3759814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          case 24:
3769814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          {
3779814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            /*
3789814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              8 bits each of blue, green and red.
3799814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            */
38077b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
38177b33baacfae08c3794dd19df1862fb645ebe265cristy              ReadBlobByte(image));
38277b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
38377b33baacfae08c3794dd19df1862fb645ebe265cristy              ReadBlobByte(image));
38477b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
38577b33baacfae08c3794dd19df1862fb645ebe265cristy              ReadBlobByte(image));
3869814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            break;
3879814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          }
388ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy          case 32:
389ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy          {
390ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy            /*
391ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy              8 bits each of blue, green, red, and alpha.
392ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy            */
393ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy            pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
394ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy              ReadBlobByte(image));
395ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy            pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
396ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy              ReadBlobByte(image));
397ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy            pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
398ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy              ReadBlobByte(image));
399ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy            pixel.alpha=(MagickRealType) ScaleCharToQuantum((unsigned char)
400ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy              ReadBlobByte(image));
401ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy            break;
402ba21340ff1a7fefd9029dfc9035e3d3246e282b7cristy          }
4039814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        }
4049814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        image->colormap[i]=pixel;
4053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
4069814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    }
4079814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  /*
4089814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    Convert TGA pixels to pixel packets.
4099814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  */
4109814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  base=0;
4119814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  flag=0;
4129814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  skip=MagickFalse;
4139814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  real=0;
4144c08aed51c5899665ade97263692328eea4af106cristy  index=0;
4159814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  runlength=0;
4169814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  offset=0;
4179814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  for (y=0; y < (ssize_t) image->rows; y++)
4189814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  {
4199814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    real=offset;
4209814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    if (((unsigned char) (tga_info.attributes & 0x20) >> 5) == 0)
4219814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      real=image->rows-real-1;
4229814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    q=QueueAuthenticPixels(image,0,(ssize_t) real,image->columns,1,exception);
423acd2ed254c18c254a0ab5aafa06d1645e5d079d8cristy    if (q == (Quantum *) NULL)
4249814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      break;
4259814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    for (x=0; x < (ssize_t) image->columns; x++)
4269814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    {
4279814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      if ((tga_info.image_type == TGARLEColormap) ||
4289814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          (tga_info.image_type == TGARLERGB) ||
4299814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          (tga_info.image_type == TGARLEMonochrome))
4303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
4319814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          if (runlength != 0)
4323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
4339814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              runlength--;
4349814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              skip=flag != 0;
4353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
4369814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          else
4373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
4389814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              count=ReadBlob(image,1,&runlength);
439771c884804218f352efd091f15328cebd1696079cristy              if (count != 1)
4409814091f6402f77ef31f82a48d6bdbb53ae6c087cristy                ThrowReaderException(CorruptImageError,"UnableToReadImageData");
4419814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              flag=runlength & 0x80;
4429814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              if (flag != 0)
4439814091f6402f77ef31f82a48d6bdbb53ae6c087cristy                runlength-=128;
4449814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              skip=MagickFalse;
4453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
4463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
4479814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      if (skip == MagickFalse)
4489814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        switch (tga_info.bits_per_pixel)
4499814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        {
4509814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          case 8:
4519814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          default:
4523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
4539814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            /*
4549814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              Gray scale.
4559814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            */
4564c08aed51c5899665ade97263692328eea4af106cristy            index=(Quantum) ReadBlobByte(image);
4579814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            if (tga_info.colormap_type != 0)
4589814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              pixel=image->colormap[(ssize_t) ConstrainColormapIndex(image,
459c9eb04ca2174d8500f59e7e3e32148069610223ecristy                (ssize_t) index,exception)];
4603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            else
4613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              {
46277b33baacfae08c3794dd19df1862fb645ebe265cristy                pixel.red=(MagickRealType) ScaleCharToQuantum((unsigned char)
46377b33baacfae08c3794dd19df1862fb645ebe265cristy                  index);
46477b33baacfae08c3794dd19df1862fb645ebe265cristy                pixel.green=(MagickRealType) ScaleCharToQuantum((unsigned char)
46577b33baacfae08c3794dd19df1862fb645ebe265cristy                  index);
46677b33baacfae08c3794dd19df1862fb645ebe265cristy                pixel.blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
46777b33baacfae08c3794dd19df1862fb645ebe265cristy                  index);
4683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              }
4699814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            break;
4703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
4719814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          case 15:
4729814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          case 16:
4733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
4749814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            QuantumAny
4759814091f6402f77ef31f82a48d6bdbb53ae6c087cristy              range;
4769814091f6402f77ef31f82a48d6bdbb53ae6c087cristy
4779814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            /*
4785dd83a5b3ade92272cda240514df53206cd3f8b7cristy              5 bits each of RGB.
4799814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            */
4805dd83a5b3ade92272cda240514df53206cd3f8b7cristy            if (ReadBlob(image,2,pixels) != 2)
4815dd83a5b3ade92272cda240514df53206cd3f8b7cristy              ThrowReaderException(CorruptImageError,"UnableToReadImageData");
4825dd83a5b3ade92272cda240514df53206cd3f8b7cristy            j=pixels[0];
4835dd83a5b3ade92272cda240514df53206cd3f8b7cristy            k=pixels[1];
4849814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            range=GetQuantumRange(5UL);
48577b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.red=(MagickRealType) ScaleAnyToQuantum(1UL*(k & 0x7c) >> 2,
48677b33baacfae08c3794dd19df1862fb645ebe265cristy              range);
487dfc3f8522077b58965f6c51d6498e90269e9b306cristy            pixel.green=(MagickRealType) ScaleAnyToQuantum((1UL*
488dfc3f8522077b58965f6c51d6498e90269e9b306cristy              (k & 0x03) << 3)+(1UL*(j & 0xe0) >> 5),range);
48977b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.blue=(MagickRealType) ScaleAnyToQuantum(1UL*(j & 0x1f),range);
49017f11b056210f082a6d0e54ac5d68e6d72fa76b2cristy            if (image->alpha_trait != UndefinedPixelTrait)
49177b33baacfae08c3794dd19df1862fb645ebe265cristy              pixel.alpha=(MagickRealType) ((k & 0x80) == 0 ? (Quantum)
4920cfa9e9cf72324988ee0c2eb60b5bb1b634f344fdirk                TransparentAlpha : (Quantum) OpaqueAlpha);
4939814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            if (image->storage_class == PseudoClass)
494c9eb04ca2174d8500f59e7e3e32148069610223ecristy              index=(Quantum) ConstrainColormapIndex(image,((ssize_t) (k << 8))+
495c9eb04ca2174d8500f59e7e3e32148069610223ecristy                j,exception);
4969814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            break;
4973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
4989814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          case 24:
4995dd83a5b3ade92272cda240514df53206cd3f8b7cristy          {
5005dd83a5b3ade92272cda240514df53206cd3f8b7cristy            /*
5015dd83a5b3ade92272cda240514df53206cd3f8b7cristy              BGR pixels.
5025dd83a5b3ade92272cda240514df53206cd3f8b7cristy            */
5035dd83a5b3ade92272cda240514df53206cd3f8b7cristy            if (ReadBlob(image,3,pixels) != 3)
5045dd83a5b3ade92272cda240514df53206cd3f8b7cristy              ThrowReaderException(CorruptImageError,"UnableToReadImageData");
50577b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
50677b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
50777b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
5085dd83a5b3ade92272cda240514df53206cd3f8b7cristy            break;
5095dd83a5b3ade92272cda240514df53206cd3f8b7cristy          }
5109814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          case 32:
5119814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          {
5129814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            /*
5135dd83a5b3ade92272cda240514df53206cd3f8b7cristy              BGRA pixels.
5149814091f6402f77ef31f82a48d6bdbb53ae6c087cristy            */
5155dd83a5b3ade92272cda240514df53206cd3f8b7cristy            if (ReadBlob(image,4,pixels) != 4)
5165dd83a5b3ade92272cda240514df53206cd3f8b7cristy              ThrowReaderException(CorruptImageError,"UnableToReadImageData");
51777b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.blue=(MagickRealType) ScaleCharToQuantum(pixels[0]);
51877b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.green=(MagickRealType) ScaleCharToQuantum(pixels[1]);
51977b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.red=(MagickRealType) ScaleCharToQuantum(pixels[2]);
52077b33baacfae08c3794dd19df1862fb645ebe265cristy            pixel.alpha=(MagickRealType) ScaleCharToQuantum(pixels[3]);
5213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
5229814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          }
5233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
5249814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      if (status == MagickFalse)
5259814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        ThrowReaderException(CorruptImageError,"UnableToReadImageData");
5269814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      if (image->storage_class == PseudoClass)
5274c08aed51c5899665ade97263692328eea4af106cristy        SetPixelIndex(image,index,q);
52877b33baacfae08c3794dd19df1862fb645ebe265cristy      SetPixelRed(image,ClampToQuantum(pixel.red),q);
52977b33baacfae08c3794dd19df1862fb645ebe265cristy      SetPixelGreen(image,ClampToQuantum(pixel.green),q);
53077b33baacfae08c3794dd19df1862fb645ebe265cristy      SetPixelBlue(image,ClampToQuantum(pixel.blue),q);
53117f11b056210f082a6d0e54ac5d68e6d72fa76b2cristy      if (image->alpha_trait != UndefinedPixelTrait)
53277b33baacfae08c3794dd19df1862fb645ebe265cristy        SetPixelAlpha(image,ClampToQuantum(pixel.alpha),q);
533ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy      q+=GetPixelChannels(image);
5343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
535cd56a387d42af261b6c42dc8f7bc7df4ba7e636bcristy    /*
536cd56a387d42af261b6c42dc8f7bc7df4ba7e636bcristy      if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 4)
537cd56a387d42af261b6c42dc8f7bc7df4ba7e636bcristy        offset+=4;
538cd56a387d42af261b6c42dc8f7bc7df4ba7e636bcristy      else
539cd56a387d42af261b6c42dc8f7bc7df4ba7e636bcristy    */
540ccdef73219f3ef522f21b968ab80ee0f6b26709acristy      if (((unsigned char) (tga_info.attributes & 0xc0) >> 6) == 2)
5419814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        offset+=2;
5429814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      else
5439814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        offset++;
5449814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    if (offset >= image->rows)
5453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
5469814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        base++;
5479814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        offset=base;
5483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
5499814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    if (SyncAuthenticPixels(image,exception) == MagickFalse)
5509814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      break;
5519814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    if (image->previous == (Image *) NULL)
5523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
5539814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
5549814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          image->rows);
5553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (status == MagickFalse)
5563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
5573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
5589814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  }
5599814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if (EOFBlob(image) != MagickFalse)
5609814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
5619814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      image->filename);
5623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) CloseBlob(image);
5633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(GetFirstImageInList(image));
5643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
5653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
5663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
5673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
5693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
5703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
5713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   R e g i s t e r T G A I m a g e                                           %
5723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
5733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
5743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
5753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
5773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  RegisterTGAImage() adds properties for the TGA image format to
5783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  the list of supported formats.  The properties include the image format
5793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  tag, a method to read and/or write the format, whether the format
5803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  supports the saving of more than one frame to the same file or blob,
5813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  whether the format supports native in-memory I/O, and a brief
5823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  description of the format.
5833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
5843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the RegisterTGAImage method is:
5853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
586bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy%      size_t RegisterTGAImage(void)
5873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
5883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
589bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristyModuleExport size_t RegisterTGAImage(void)
5903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
5913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  MagickInfo
5923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *entry;
5933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
59406b627a07ff44e1ff93ef1288c9f428066ded10ddirk  entry=AcquireMagickInfo("TGA","ICB","Truevision Targa image");
5953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->decoder=(DecodeImageHandler *) ReadTGAImage;
5963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->encoder=(EncodeImageHandler *) WriteTGAImage;
59708e9a113db499034abb5ad8d59b42f8eca3c641cdirk  entry->flags^=CoderAdjoinFlag;
5983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) RegisterMagickInfo(entry);
59906b627a07ff44e1ff93ef1288c9f428066ded10ddirk  entry=AcquireMagickInfo("TGA","TGA","Truevision Targa image");
6003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->decoder=(DecodeImageHandler *) ReadTGAImage;
6013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->encoder=(EncodeImageHandler *) WriteTGAImage;
60208e9a113db499034abb5ad8d59b42f8eca3c641cdirk  entry->flags^=CoderAdjoinFlag;
6033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) RegisterMagickInfo(entry);
60406b627a07ff44e1ff93ef1288c9f428066ded10ddirk  entry=AcquireMagickInfo("TGA","VDA","Truevision Targa image");
6053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->decoder=(DecodeImageHandler *) ReadTGAImage;
6063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->encoder=(EncodeImageHandler *) WriteTGAImage;
60708e9a113db499034abb5ad8d59b42f8eca3c641cdirk  entry->flags^=CoderAdjoinFlag;
6083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) RegisterMagickInfo(entry);
60906b627a07ff44e1ff93ef1288c9f428066ded10ddirk  entry=AcquireMagickInfo("TGA","VST","Truevision Targa image");
6103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->decoder=(DecodeImageHandler *) ReadTGAImage;
6113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->encoder=(EncodeImageHandler *) WriteTGAImage;
61208e9a113db499034abb5ad8d59b42f8eca3c641cdirk  entry->flags^=CoderAdjoinFlag;
6133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) RegisterMagickInfo(entry);
6143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(MagickImageCoderSignature);
6153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
6163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
6183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   U n r e g i s t e r T G A I m a g e                                       %
6233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
6283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  UnregisterTGAImage() removes format registrations made by the
6293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  TGA module from the list of supported formats.
6303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
6313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the UnregisterTGAImage method is:
6323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
6333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      UnregisterTGAImage(void)
6343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
6353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
6363ed852eea50f9d4cd633efb8c2b054b8e33c253cristyModuleExport void UnregisterTGAImage(void)
6373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
6383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) UnregisterMagickInfo("ICB");
6393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) UnregisterMagickInfo("TGA");
6403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) UnregisterMagickInfo("VDA");
6413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) UnregisterMagickInfo("VST");
6423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
6433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
6453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   W r i t e T G A I m a g e                                                 %
6503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
6533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
6553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  WriteTGAImage() writes a image in the Truevision Targa rasterfile
6563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  format.
6573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
6583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the WriteTGAImage method is:
6593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
6603a37efd7ece979e9c63dc8f2f2d3816bab8b1156cristy%      MagickBooleanType WriteTGAImage(const ImageInfo *image_info,
6613a37efd7ece979e9c63dc8f2f2d3816bab8b1156cristy%        Image *image,ExceptionInfo *exception)
6623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
6633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  A description of each parameter follows.
6643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
6653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o image_info: the image info.
6663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
6673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o image:  The image.
6683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
6693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
67075652a86d1cd6e8bdd2967145fcd003415004219dirkstatic inline void WriteTGAPixel(Image *image,TGAImageType image_type,
671f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk  const Quantum *p,const QuantumAny range,const double midpoint)
67275652a86d1cd6e8bdd2967145fcd003415004219dirk{
67375652a86d1cd6e8bdd2967145fcd003415004219dirk  if (image_type == TGAColormap || image_type == TGARLEColormap)
67475652a86d1cd6e8bdd2967145fcd003415004219dirk    (void) WriteBlobByte(image,(unsigned char) GetPixelIndex(image,p));
67575652a86d1cd6e8bdd2967145fcd003415004219dirk  else
67675652a86d1cd6e8bdd2967145fcd003415004219dirk    {
67775652a86d1cd6e8bdd2967145fcd003415004219dirk      if (image_type == TGAMonochrome || image_type == TGARLEMonochrome)
67875652a86d1cd6e8bdd2967145fcd003415004219dirk        (void) WriteBlobByte(image,ScaleQuantumToChar(ClampToQuantum(
67975652a86d1cd6e8bdd2967145fcd003415004219dirk          GetPixelLuma(image,p))));
68075652a86d1cd6e8bdd2967145fcd003415004219dirk      else
681dfc3f8522077b58965f6c51d6498e90269e9b306cristy        if (image->depth == 5)
682dfc3f8522077b58965f6c51d6498e90269e9b306cristy          {
683dfc3f8522077b58965f6c51d6498e90269e9b306cristy            unsigned char
684dfc3f8522077b58965f6c51d6498e90269e9b306cristy              green,
685dfc3f8522077b58965f6c51d6498e90269e9b306cristy              value;
686dfc3f8522077b58965f6c51d6498e90269e9b306cristy
687dfc3f8522077b58965f6c51d6498e90269e9b306cristy            green=(unsigned char) ScaleQuantumToAny(GetPixelGreen(image,p),
688dfc3f8522077b58965f6c51d6498e90269e9b306cristy              range);
689dfc3f8522077b58965f6c51d6498e90269e9b306cristy            value=((unsigned char) ScaleQuantumToAny(GetPixelBlue(image,p),
690dfc3f8522077b58965f6c51d6498e90269e9b306cristy              range)) | ((green & 0x07) << 5);
691dfc3f8522077b58965f6c51d6498e90269e9b306cristy            (void) WriteBlobByte(image,value);
692dfc3f8522077b58965f6c51d6498e90269e9b306cristy            value=(((image->alpha_trait != UndefinedPixelTrait) &&
6930cfa9e9cf72324988ee0c2eb60b5bb1b634f344fdirk              ((double) GetPixelAlpha(image,p) > midpoint)) ? 0x80 : 0) |
694dfc3f8522077b58965f6c51d6498e90269e9b306cristy              ((unsigned char) ScaleQuantumToAny(GetPixelRed(image,p),range) <<
695dfc3f8522077b58965f6c51d6498e90269e9b306cristy              2) | ((green & 0x18) >> 3);
696dfc3f8522077b58965f6c51d6498e90269e9b306cristy            (void) WriteBlobByte(image,value);
697dfc3f8522077b58965f6c51d6498e90269e9b306cristy          }
698dfc3f8522077b58965f6c51d6498e90269e9b306cristy        else
699dfc3f8522077b58965f6c51d6498e90269e9b306cristy          {
700dfc3f8522077b58965f6c51d6498e90269e9b306cristy            (void) WriteBlobByte(image,ScaleQuantumToChar(
701dfc3f8522077b58965f6c51d6498e90269e9b306cristy              GetPixelBlue(image,p)));
702dfc3f8522077b58965f6c51d6498e90269e9b306cristy            (void) WriteBlobByte(image,ScaleQuantumToChar(
703dfc3f8522077b58965f6c51d6498e90269e9b306cristy              GetPixelGreen(image,p)));
704dfc3f8522077b58965f6c51d6498e90269e9b306cristy            (void) WriteBlobByte(image,ScaleQuantumToChar(
705dfc3f8522077b58965f6c51d6498e90269e9b306cristy              GetPixelRed(image,p)));
706dfc3f8522077b58965f6c51d6498e90269e9b306cristy            if (image->alpha_trait != UndefinedPixelTrait)
707dfc3f8522077b58965f6c51d6498e90269e9b306cristy              (void) WriteBlobByte(image,ScaleQuantumToChar(
708dfc3f8522077b58965f6c51d6498e90269e9b306cristy                GetPixelAlpha(image,p)));
709dfc3f8522077b58965f6c51d6498e90269e9b306cristy          }
71075652a86d1cd6e8bdd2967145fcd003415004219dirk    }
71175652a86d1cd6e8bdd2967145fcd003415004219dirk}
71275652a86d1cd6e8bdd2967145fcd003415004219dirk
7133a37efd7ece979e9c63dc8f2f2d3816bab8b1156cristystatic MagickBooleanType WriteTGAImage(const ImageInfo *image_info,Image *image,
7143a37efd7ece979e9c63dc8f2f2d3816bab8b1156cristy  ExceptionInfo *exception)
7153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
71675652a86d1cd6e8bdd2967145fcd003415004219dirk  CompressionType
71775652a86d1cd6e8bdd2967145fcd003415004219dirk    compression;
7183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  const char
7203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *value;
7213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
722f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk  const double
723dfc3f8522077b58965f6c51d6498e90269e9b306cristy    midpoint = QuantumRange/2.0;
724f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk
7253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  MagickBooleanType
7263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    status;
7273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
728f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk  QuantumAny
729f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk    range;
730f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk
7314c08aed51c5899665ade97263692328eea4af106cristy  register const Quantum
7323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *p;
7333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
734bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  register ssize_t
7353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    x;
7363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
737bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  register ssize_t
7383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    i;
7393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  register unsigned char
7413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *q;
7423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
74375652a86d1cd6e8bdd2967145fcd003415004219dirk  size_t
74475652a86d1cd6e8bdd2967145fcd003415004219dirk    channels;
74575652a86d1cd6e8bdd2967145fcd003415004219dirk
7463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  ssize_t
747c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    count,
748c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    y;
7493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
75075652a86d1cd6e8bdd2967145fcd003415004219dirk  TGAInfo
75175652a86d1cd6e8bdd2967145fcd003415004219dirk    tga_info;
7523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
7543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Open output image file.
7553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
7563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(image_info != (const ImageInfo *) NULL);
757e1c94d9d25db6b0dd7a5028ffee31d1057855d73cristy  assert(image_info->signature == MagickCoreSignature);
7583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(image != (Image *) NULL);
759e1c94d9d25db6b0dd7a5028ffee31d1057855d73cristy  assert(image->signature == MagickCoreSignature);
7603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (image->debug != MagickFalse)
7613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
7623a37efd7ece979e9c63dc8f2f2d3816bab8b1156cristy  assert(exception != (ExceptionInfo *) NULL);
763e1c94d9d25db6b0dd7a5028ffee31d1057855d73cristy  assert(exception->signature == MagickCoreSignature);
7643a37efd7ece979e9c63dc8f2f2d3816bab8b1156cristy  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
7653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (status == MagickFalse)
7663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return(status);
7679814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  /*
7689814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    Initialize TGA raster file header.
7699814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  */
7709814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if ((image->columns > 65535L) || (image->rows > 65535L))
7719814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
772af8d391906d11f0a1f2bbf4e2adbc4995c852d33cristy  (void) TransformImageColorspace(image,sRGBColorspace,exception);
77375652a86d1cd6e8bdd2967145fcd003415004219dirk  compression=image->compression;
77475652a86d1cd6e8bdd2967145fcd003415004219dirk  if (image_info->compression != UndefinedCompression)
77575652a86d1cd6e8bdd2967145fcd003415004219dirk    compression=image_info->compression;
776f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk  range=GetQuantumRange(5UL);
77775652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.id_length=0;
778d15e65928aec551b7388c2863de3e3e628e2e0ddcristy  value=GetImageProperty(image,"comment",exception);
7799814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if (value != (const char *) NULL)
78075652a86d1cd6e8bdd2967145fcd003415004219dirk    tga_info.id_length=(unsigned char) MagickMin(strlen(value),255);
78175652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.colormap_type=0;
78275652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.colormap_index=0;
78375652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.colormap_length=0;
78475652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.colormap_size=0;
78575652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.x_origin=0;
78675652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.y_origin=0;
78775652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.width=(unsigned short) image->columns;
78875652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.height=(unsigned short) image->rows;
78975652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.bits_per_pixel=8;
79075652a86d1cd6e8bdd2967145fcd003415004219dirk  tga_info.attributes=0;
7919814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  if ((image_info->type != TrueColorType) &&
792def23e5d7331b1a13ed593b6d6aca516da382328cristy      (image_info->type != TrueColorAlphaType) &&
7939814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      (image_info->type != PaletteType) &&
79417f11b056210f082a6d0e54ac5d68e6d72fa76b2cristy      (image->alpha_trait == UndefinedPixelTrait) &&
795f1d8548abecaf5ca89d453fd9fc0cde77d20672bdirk      (SetImageGray(image,exception) != MagickFalse))
79675652a86d1cd6e8bdd2967145fcd003415004219dirk    tga_info.image_type=compression == RLECompression ? TGARLEMonochrome :
79775652a86d1cd6e8bdd2967145fcd003415004219dirk      TGAMonochrome;
7989814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  else
7999814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    if ((image->storage_class == DirectClass) || (image->colors > 256))
8009814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      {
8019814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        /*
8029814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          Full color TGA raster.
8039814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        */
804dfc3f8522077b58965f6c51d6498e90269e9b306cristy        tga_info.image_type=compression == RLECompression ? TGARLERGB : TGARGB;
805f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk        if (image_info->depth == 5)
8060cfa9e9cf72324988ee0c2eb60b5bb1b634f344fdirk          {
8070cfa9e9cf72324988ee0c2eb60b5bb1b634f344fdirk            tga_info.bits_per_pixel=16;
8080cfa9e9cf72324988ee0c2eb60b5bb1b634f344fdirk            if (image->alpha_trait != UndefinedPixelTrait)
8090cfa9e9cf72324988ee0c2eb60b5bb1b634f344fdirk              tga_info.attributes=1;  /* # of alpha bits */
8100cfa9e9cf72324988ee0c2eb60b5bb1b634f344fdirk          }
811f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk        else
8129814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          {
813f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk            tga_info.bits_per_pixel=24;
81417f11b056210f082a6d0e54ac5d68e6d72fa76b2cristy            if (image->alpha_trait != UndefinedPixelTrait)
815f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk              {
816f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk                tga_info.bits_per_pixel=32;
817f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk                tga_info.attributes=8;  /* # of alpha bits */
818f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk              }
8199814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          }
8209814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      }
8213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    else
8223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
8233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        /*
8249814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          Colormapped TGA raster.
8253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        */
82675652a86d1cd6e8bdd2967145fcd003415004219dirk        tga_info.image_type=compression == RLECompression ? TGARLEColormap :
82775652a86d1cd6e8bdd2967145fcd003415004219dirk          TGAColormap;
82875652a86d1cd6e8bdd2967145fcd003415004219dirk        tga_info.colormap_type=1;
82975652a86d1cd6e8bdd2967145fcd003415004219dirk        tga_info.colormap_length=(unsigned short) image->colors;
830f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk        if (image_info->depth == 5)
831f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk          tga_info.colormap_size=16;
832f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk        else
833f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk          tga_info.colormap_size=24;
8343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
8359ac195506bdd2a46b0489d24f772a989be6ea46cdirk  value=GetImageArtifact(image,"tga:image-origin");
8369ac195506bdd2a46b0489d24f772a989be6ea46cdirk  if (value != (const char *) NULL)
8379ac195506bdd2a46b0489d24f772a989be6ea46cdirk    {
8389ac195506bdd2a46b0489d24f772a989be6ea46cdirk      OrientationType
8399ac195506bdd2a46b0489d24f772a989be6ea46cdirk        origin;
8409ac195506bdd2a46b0489d24f772a989be6ea46cdirk
8419ac195506bdd2a46b0489d24f772a989be6ea46cdirk      origin=(OrientationType) ParseCommandOption(MagickOrientationOptions,
8429ac195506bdd2a46b0489d24f772a989be6ea46cdirk        MagickFalse,value);
8439ac195506bdd2a46b0489d24f772a989be6ea46cdirk      if (origin == BottomRightOrientation || origin == TopRightOrientation)
8449ac195506bdd2a46b0489d24f772a989be6ea46cdirk        tga_info.attributes|=(1UL << 4);
8459ac195506bdd2a46b0489d24f772a989be6ea46cdirk      if (origin == TopLeftOrientation || origin == TopRightOrientation)
8469ac195506bdd2a46b0489d24f772a989be6ea46cdirk        tga_info.attributes|=(1UL << 5);
8479ac195506bdd2a46b0489d24f772a989be6ea46cdirk    }
8489814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  /*
8499814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    Write TGA header.
8509814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  */
85175652a86d1cd6e8bdd2967145fcd003415004219dirk  (void) WriteBlobByte(image,tga_info.id_length);
85275652a86d1cd6e8bdd2967145fcd003415004219dirk  (void) WriteBlobByte(image,tga_info.colormap_type);
8532a2a3e99bdb5ce8378e394dea48075177805ebb0dirk  (void) WriteBlobByte(image,(unsigned char) tga_info.image_type);
85475652a86d1cd6e8bdd2967145fcd003415004219dirk  (void) WriteBlobLSBShort(image,tga_info.colormap_index);
85575652a86d1cd6e8bdd2967145fcd003415004219dirk  (void) WriteBlobLSBShort(image,tga_info.colormap_length);
85675652a86d1cd6e8bdd2967145fcd003415004219dirk  (void) WriteBlobByte(image,tga_info.colormap_size);
85775652a86d1cd6e8bdd2967145fcd003415004219dirk  (void) WriteBlobLSBShort(image,tga_info.x_origin);
85875652a86d1cd6e8bdd2967145fcd003415004219dirk  (void) WriteBlobLSBShort(image,tga_info.y_origin);
85975652a86d1cd6e8bdd2967145fcd003415004219dirk  (void) WriteBlobLSBShort(image,tga_info.width);
86075652a86d1cd6e8bdd2967145fcd003415004219dirk  (void) WriteBlobLSBShort(image,tga_info.height);
86175652a86d1cd6e8bdd2967145fcd003415004219dirk  (void) WriteBlobByte(image,tga_info.bits_per_pixel);
86275652a86d1cd6e8bdd2967145fcd003415004219dirk  (void) WriteBlobByte(image,tga_info.attributes);
86375652a86d1cd6e8bdd2967145fcd003415004219dirk  if (tga_info.id_length != 0)
864dfc3f8522077b58965f6c51d6498e90269e9b306cristy    (void) WriteBlob(image,tga_info.id_length,(unsigned char *) value);
86575652a86d1cd6e8bdd2967145fcd003415004219dirk  if (tga_info.colormap_type != 0)
8663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
8679814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      unsigned char
868f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk        green,
8699814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        *targa_colormap;
8709814091f6402f77ef31f82a48d6bdbb53ae6c087cristy
8719814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      /*
8729814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        Dump colormap to file (blue, green, red byte order).
8739814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      */
8749814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      targa_colormap=(unsigned char *) AcquireQuantumMemory((size_t)
875dfc3f8522077b58965f6c51d6498e90269e9b306cristy        tga_info.colormap_length,(tga_info.colormap_size/8)*
876dfc3f8522077b58965f6c51d6498e90269e9b306cristy        sizeof(*targa_colormap));
8779814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      if (targa_colormap == (unsigned char *) NULL)
8789814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
8799814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      q=targa_colormap;
8809814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      for (i=0; i < (ssize_t) image->colors; i++)
8813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
882f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk        if (image_info->depth == 5)
883f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk          {
884f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk            green=(unsigned char) ScaleQuantumToAny(ClampToQuantum(
885f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk              image->colormap[i].green),range);
886f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk            *q++=((unsigned char) ScaleQuantumToAny(ClampToQuantum(
887f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk              image->colormap[i].blue),range)) | ((green & 0x07) << 5);
888c9eb04ca2174d8500f59e7e3e32148069610223ecristy            *q++=(((image->alpha_trait != UndefinedPixelTrait) && ((double)
8890cfa9e9cf72324988ee0c2eb60b5bb1b634f344fdirk              ClampToQuantum(image->colormap[i].alpha) > midpoint)) ? 0x80 : 0) |
890f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk              ((unsigned char) ScaleQuantumToAny(ClampToQuantum(
891f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk              image->colormap[i].red),range) << 2) | ((green & 0x18) >> 3);
892f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk          }
893f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk        else
894f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk          {
895f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk            *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].blue));
896f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk            *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].green));
897f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk            *q++=ScaleQuantumToChar(ClampToQuantum(image->colormap[i].red));
898f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk          }
8993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
900f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk      (void) WriteBlob(image,(size_t) ((tga_info.colormap_size/8)*
901f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk        tga_info.colormap_length),targa_colormap);
9029814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      targa_colormap=(unsigned char *) RelinquishMagickMemory(targa_colormap);
9033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
9049814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  /*
9059814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    Convert MIFF to TGA raster pixels.
9069814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  */
90775652a86d1cd6e8bdd2967145fcd003415004219dirk  channels=GetPixelChannels(image);
9089814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  for (y=(ssize_t) (image->rows-1); y >= 0; y--)
9099814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  {
9103a37efd7ece979e9c63dc8f2f2d3816bab8b1156cristy    p=GetVirtualPixels(image,0,y,image->columns,1,exception);
9114c08aed51c5899665ade97263692328eea4af106cristy    if (p == (const Quantum *) NULL)
9123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      break;
91375652a86d1cd6e8bdd2967145fcd003415004219dirk    if (compression == RLECompression)
91475652a86d1cd6e8bdd2967145fcd003415004219dirk      {
91575652a86d1cd6e8bdd2967145fcd003415004219dirk        x=0;
91675652a86d1cd6e8bdd2967145fcd003415004219dirk        count=0;
91775652a86d1cd6e8bdd2967145fcd003415004219dirk        while (x < (ssize_t) image->columns)
91875652a86d1cd6e8bdd2967145fcd003415004219dirk        {
91975652a86d1cd6e8bdd2967145fcd003415004219dirk          i=1;
92075652a86d1cd6e8bdd2967145fcd003415004219dirk          while ((i < 128) && (count + i < 128) &&
92175652a86d1cd6e8bdd2967145fcd003415004219dirk                 ((x + i) < (ssize_t) image->columns))
9229814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          {
92375652a86d1cd6e8bdd2967145fcd003415004219dirk            if (tga_info.image_type == TGARLEColormap)
92475652a86d1cd6e8bdd2967145fcd003415004219dirk              {
92575652a86d1cd6e8bdd2967145fcd003415004219dirk                if (GetPixelIndex(image,p+(i*channels)) !=
92675652a86d1cd6e8bdd2967145fcd003415004219dirk                    GetPixelIndex(image,p+((i-1)*channels)))
92775652a86d1cd6e8bdd2967145fcd003415004219dirk                  break;
92875652a86d1cd6e8bdd2967145fcd003415004219dirk              }
92975652a86d1cd6e8bdd2967145fcd003415004219dirk            else if (tga_info.image_type == TGARLEMonochrome)
93075652a86d1cd6e8bdd2967145fcd003415004219dirk              {
93175652a86d1cd6e8bdd2967145fcd003415004219dirk                if (GetPixelLuma(image,p+(i*channels)) !=
93275652a86d1cd6e8bdd2967145fcd003415004219dirk                    GetPixelLuma(image,p+((i-1)*channels)))
93375652a86d1cd6e8bdd2967145fcd003415004219dirk                  break;
93475652a86d1cd6e8bdd2967145fcd003415004219dirk              }
93575652a86d1cd6e8bdd2967145fcd003415004219dirk            else
93675652a86d1cd6e8bdd2967145fcd003415004219dirk              {
93775652a86d1cd6e8bdd2967145fcd003415004219dirk                if ((GetPixelBlue(image,p+(i*channels)) !=
93875652a86d1cd6e8bdd2967145fcd003415004219dirk                     GetPixelBlue(image,p+((i-1)*channels))) ||
93975652a86d1cd6e8bdd2967145fcd003415004219dirk                    (GetPixelGreen(image,p+(i*channels)) !=
94075652a86d1cd6e8bdd2967145fcd003415004219dirk                     GetPixelGreen(image,p+((i-1)*channels))) ||
94175652a86d1cd6e8bdd2967145fcd003415004219dirk                    (GetPixelRed(image,p+(i*channels)) !=
94275652a86d1cd6e8bdd2967145fcd003415004219dirk                     GetPixelRed(image,p+((i-1)*channels))))
94375652a86d1cd6e8bdd2967145fcd003415004219dirk                  break;
94417f11b056210f082a6d0e54ac5d68e6d72fa76b2cristy                if ((image->alpha_trait != UndefinedPixelTrait) &&
94575652a86d1cd6e8bdd2967145fcd003415004219dirk                    (GetPixelAlpha(image,p+(i*channels)) !=
94675652a86d1cd6e8bdd2967145fcd003415004219dirk                     GetPixelAlpha(image,p+(i-1)*channels)))
94775652a86d1cd6e8bdd2967145fcd003415004219dirk                  break;
94875652a86d1cd6e8bdd2967145fcd003415004219dirk              }
94975652a86d1cd6e8bdd2967145fcd003415004219dirk            i++;
9509814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          }
95175652a86d1cd6e8bdd2967145fcd003415004219dirk          if (i < 3)
95275652a86d1cd6e8bdd2967145fcd003415004219dirk            {
95375652a86d1cd6e8bdd2967145fcd003415004219dirk              count+=i;
95475652a86d1cd6e8bdd2967145fcd003415004219dirk              p+=(i*channels);
95575652a86d1cd6e8bdd2967145fcd003415004219dirk            }
95675652a86d1cd6e8bdd2967145fcd003415004219dirk          if ((i >= 3) || (count == 128) ||
95775652a86d1cd6e8bdd2967145fcd003415004219dirk              ((x + i) == (ssize_t) image->columns))
95875652a86d1cd6e8bdd2967145fcd003415004219dirk            {
95975652a86d1cd6e8bdd2967145fcd003415004219dirk              if (count > 0)
96075652a86d1cd6e8bdd2967145fcd003415004219dirk                {
96143254d10ab2f9c7088f742b78c754d04103c527fcristy                  (void) WriteBlobByte(image,(unsigned char) (--count));
96275652a86d1cd6e8bdd2967145fcd003415004219dirk                  while (count >= 0)
96375652a86d1cd6e8bdd2967145fcd003415004219dirk                  {
96475652a86d1cd6e8bdd2967145fcd003415004219dirk                    WriteTGAPixel(image,tga_info.image_type,p-((count+1)*
965f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk                      channels),range,midpoint);
96675652a86d1cd6e8bdd2967145fcd003415004219dirk                    count--;
96775652a86d1cd6e8bdd2967145fcd003415004219dirk                  }
96875652a86d1cd6e8bdd2967145fcd003415004219dirk                  count=0;
96975652a86d1cd6e8bdd2967145fcd003415004219dirk                }
97075652a86d1cd6e8bdd2967145fcd003415004219dirk            }
97175652a86d1cd6e8bdd2967145fcd003415004219dirk          if (i >= 3)
97275652a86d1cd6e8bdd2967145fcd003415004219dirk            {
973c9eb04ca2174d8500f59e7e3e32148069610223ecristy              (void) WriteBlobByte(image,(unsigned char) ((i-1) | 0x80));
974f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk              WriteTGAPixel(image,tga_info.image_type,p,range,midpoint);
97575652a86d1cd6e8bdd2967145fcd003415004219dirk              p+=(i*channels);
97675652a86d1cd6e8bdd2967145fcd003415004219dirk            }
97775652a86d1cd6e8bdd2967145fcd003415004219dirk          x+=i;
97875652a86d1cd6e8bdd2967145fcd003415004219dirk        }
97975652a86d1cd6e8bdd2967145fcd003415004219dirk      }
98075652a86d1cd6e8bdd2967145fcd003415004219dirk    else
98175652a86d1cd6e8bdd2967145fcd003415004219dirk      {
98275652a86d1cd6e8bdd2967145fcd003415004219dirk        for (x=0; x < (ssize_t) image->columns; x++)
98375652a86d1cd6e8bdd2967145fcd003415004219dirk          {
984f142f1f2fb88a3ec8189afa56ebb512741c6ff05dirk            WriteTGAPixel(image,tga_info.image_type,p,range,midpoint);
98575652a86d1cd6e8bdd2967145fcd003415004219dirk            p+=channels;
98675652a86d1cd6e8bdd2967145fcd003415004219dirk          }
98775652a86d1cd6e8bdd2967145fcd003415004219dirk      }
9889814091f6402f77ef31f82a48d6bdbb53ae6c087cristy    if (image->previous == (Image *) NULL)
9899814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      {
9909814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
991c6da28e61bb609d2b2cfdcc7752106c973415edbcristy          image->rows);
9929814091f6402f77ef31f82a48d6bdbb53ae6c087cristy        if (status == MagickFalse)
9939814091f6402f77ef31f82a48d6bdbb53ae6c087cristy          break;
9949814091f6402f77ef31f82a48d6bdbb53ae6c087cristy      }
9959814091f6402f77ef31f82a48d6bdbb53ae6c087cristy  }
9963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) CloseBlob(image);
9973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(MagickTrue);
9983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
999