13ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
23ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
43ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
53ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
63ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                            RRRR   L       AAA                               %
73ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                            R   R  L      A   A                              %
83ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                            RRRR   L      AAAAA                              %
93ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                            R R    L      A   A                              %
103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                            R  R   LLLLL  A   A                              %
113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                      Read Alias/Wavefront 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"
434c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/property.h"
444c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/blob.h"
454c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/blob-private.h"
464c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/cache.h"
474c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/exception.h"
484c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/exception-private.h"
494c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/image.h"
504c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/image-private.h"
514c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/list.h"
524c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/magick.h"
534c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/memory_.h"
544c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/monitor.h"
554c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/monitor-private.h"
564c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/pixel-accessor.h"
574c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/quantum-private.h"
584c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/static.h"
594c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/string_.h"
604c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/module.h"
613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   R e a d R L A I m a g e                                                   %
683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  ReadRLAImage() reads a run-length encoded Wavefront RLA image file
743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  and returns it.  It allocates the memory necessary for the new Image
753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  structure and returns a pointer to the new image.
763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  Note:  This module was contributed by Lester Vecsey (master@internexus.net).
783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the ReadRLAImage method is:
803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      Image *ReadRLAImage(const ImageInfo *image_info,ExceptionInfo *exception)
823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  A description of each parameter follows:
843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o image_info: the image info.
863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o exception: return any errors or warnings in this structure.
883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
903ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic Image *ReadRLAImage(const ImageInfo *image_info,ExceptionInfo *exception)
913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct _WindowFrame
933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    short
953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      left,
963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      right,
973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      bottom,
983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      top;
993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WindowFrame;
1003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct _RLAInfo
1023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
1033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    WindowFrame
1043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      window,
1053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      active_window;
1063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    short
1083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      frame,
1093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      storage_type,
1103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      number_channels,
1113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      number_matte_channels,
1123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      number_auxiliary_channels,
1133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      revision;
1143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    char
116952a6063b767d87c452b00435088197d38916dd8cristy      gamma[16+1],
117952a6063b767d87c452b00435088197d38916dd8cristy      red_primary[24+1],
118952a6063b767d87c452b00435088197d38916dd8cristy      green_primary[24+1],
119952a6063b767d87c452b00435088197d38916dd8cristy      blue_primary[24+1],
120952a6063b767d87c452b00435088197d38916dd8cristy      white_point[24+1];
1213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
122952a6063b767d87c452b00435088197d38916dd8cristy    int
1233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      job_number;
1243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    char
126952a6063b767d87c452b00435088197d38916dd8cristy      name[128+1],
127952a6063b767d87c452b00435088197d38916dd8cristy      description[128+1],
128952a6063b767d87c452b00435088197d38916dd8cristy      program[64+1],
129952a6063b767d87c452b00435088197d38916dd8cristy      machine[32+1],
130952a6063b767d87c452b00435088197d38916dd8cristy      user[32+1],
131952a6063b767d87c452b00435088197d38916dd8cristy      date[20+1],
132952a6063b767d87c452b00435088197d38916dd8cristy      aspect[24+1],
133952a6063b767d87c452b00435088197d38916dd8cristy      aspect_ratio[8+1],
134952a6063b767d87c452b00435088197d38916dd8cristy      chan[32+1];
1353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    short
1373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      field;
1383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    char
1403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      time[12],
1413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      filter[32];
1423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    short
1443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      bits_per_channel,
1453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      matte_type,
1463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      matte_bits,
1473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      auxiliary_type,
1483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      auxiliary_bits;
1493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    char
151952a6063b767d87c452b00435088197d38916dd8cristy      auxiliary[32+1],
152952a6063b767d87c452b00435088197d38916dd8cristy      space[36+1];
1533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
154952a6063b767d87c452b00435088197d38916dd8cristy    int
1553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      next;
1563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } RLAInfo;
1573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Image
1593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *image;
1603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  int
1623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    channel,
1633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    length,
1643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    runlength;
1653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  MagickBooleanType
1673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    status;
1683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  MagickOffsetType
170952a6063b767d87c452b00435088197d38916dd8cristy    offset,
171952a6063b767d87c452b00435088197d38916dd8cristy    *scanlines;
1723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
173bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  register ssize_t
1743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    i,
1753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    x;
1763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1774c08aed51c5899665ade97263692328eea4af106cristy  register Quantum
1783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *q;
1793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  ssize_t
181c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    count,
182c6da28e61bb609d2b2cfdcc7752106c973415edbcristy    y;
1833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  RLAInfo
1853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    rla_info;
1863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned char
1883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    byte;
1893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
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    }
207952a6063b767d87c452b00435088197d38916dd8cristy  (void) ResetMagickMemory(&rla_info,0,sizeof(rla_info));
2083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.window.left=(short) ReadBlobMSBShort(image);
2093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.window.right=(short) ReadBlobMSBShort(image);
2103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.window.bottom=(short) ReadBlobMSBShort(image);
2113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.window.top=(short) ReadBlobMSBShort(image);
2123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.active_window.left=(short) ReadBlobMSBShort(image);
2133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.active_window.right=(short) ReadBlobMSBShort(image);
2143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.active_window.bottom=(short) ReadBlobMSBShort(image);
2153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.active_window.top=(short) ReadBlobMSBShort(image);
2163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.frame=(short) ReadBlobMSBShort(image);
2173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.storage_type=(short) ReadBlobMSBShort(image);
2183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.number_channels=(short) ReadBlobMSBShort(image);
2193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.number_matte_channels=(short) ReadBlobMSBShort(image);
2203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (rla_info.number_channels == 0)
2213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    rla_info.number_channels=3;
2223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.number_channels+=rla_info.number_matte_channels;
2233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.number_auxiliary_channels=(short) ReadBlobMSBShort(image);
2243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.revision=(short) ReadBlobMSBShort(image);
2253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,16,(unsigned char *) rla_info.gamma);
2263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,24,(unsigned char *) rla_info.red_primary);
2273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,24,(unsigned char *) rla_info.green_primary);
2283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,24,(unsigned char *) rla_info.blue_primary);
2293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,24,(unsigned char *) rla_info.white_point);
2301fe0b879964fa7797e3d68574d297922a47c4034Cristy  rla_info.job_number=ReadBlobMSBSignedLong(image);
2313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,128,(unsigned char *) rla_info.name);
2323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,128,(unsigned char *) rla_info.description);
233f50886b34832135cf6e2e1458f2a324bb1704191cristy  rla_info.description[127]='\0';
2343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,64,(unsigned char *) rla_info.program);
2353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,32,(unsigned char *) rla_info.machine);
2363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,32,(unsigned char *) rla_info.user);
2373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,20,(unsigned char *) rla_info.date);
2383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,24,(unsigned char *) rla_info.aspect);
2393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,8,(unsigned char *) rla_info.aspect_ratio);
2403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,32,(unsigned char *) rla_info.chan);
2413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.field=(short) ReadBlobMSBShort(image);
2423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,12,(unsigned char *) rla_info.time);
2433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,32,(unsigned char *) rla_info.filter);
2443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.bits_per_channel=(short) ReadBlobMSBShort(image);
2453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.matte_type=(short) ReadBlobMSBShort(image);
2463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.matte_bits=(short) ReadBlobMSBShort(image);
2473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.auxiliary_type=(short) ReadBlobMSBShort(image);
2483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  rla_info.auxiliary_bits=(short) ReadBlobMSBShort(image);
2493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,32,(unsigned char *) rla_info.auxiliary);
2503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  count=ReadBlob(image,36,(unsigned char *) rla_info.space);
2513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if ((size_t) count != 36)
2523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ThrowReaderException(CorruptImageError,"UnableToReadImageData");
2531fe0b879964fa7797e3d68574d297922a47c4034Cristy  rla_info.next=ReadBlobMSBSignedLong(image);
2543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
2553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Initialize image structure.
2563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
257b0a657e13c4aefba39c51292005427b47277869dcristy  image->alpha_trait=rla_info.number_matte_channels != 0 ? BlendPixelTrait :
258b0a657e13c4aefba39c51292005427b47277869dcristy    UndefinedPixelTrait;
259952a6063b767d87c452b00435088197d38916dd8cristy  image->columns=(size_t) (rla_info.active_window.right-
260952a6063b767d87c452b00435088197d38916dd8cristy    rla_info.active_window.left+1);
261952a6063b767d87c452b00435088197d38916dd8cristy  image->rows=(size_t) (rla_info.active_window.top-
262952a6063b767d87c452b00435088197d38916dd8cristy    rla_info.active_window.bottom+1);
2633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (image_info->ping != MagickFalse)
2643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
2653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      (void) CloseBlob(image);
2663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      return(GetFirstImageInList(image));
2673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
268acabb847a592ca5e430c1c0949d03acfc0b78bb9cristy  status=SetImageExtent(image,image->columns,image->rows,exception);
269acabb847a592ca5e430c1c0949d03acfc0b78bb9cristy  if (status == MagickFalse)
270acabb847a592ca5e430c1c0949d03acfc0b78bb9cristy    return(DestroyImageList(image));
271952a6063b767d87c452b00435088197d38916dd8cristy  scanlines=(MagickOffsetType *) AcquireQuantumMemory(image->rows,
272952a6063b767d87c452b00435088197d38916dd8cristy    sizeof(*scanlines));
273952a6063b767d87c452b00435088197d38916dd8cristy  if (scanlines == (MagickOffsetType *) NULL)
2743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (*rla_info.description != '\0')
276d15e65928aec551b7388c2863de3e3e628e2e0ddcristy    (void) SetImageProperty(image,"comment",rla_info.description,exception);
2773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
2783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Read offsets to each scanline data.
2793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
280bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  for (i=0; i < (ssize_t) image->rows; i++)
2811fe0b879964fa7797e3d68574d297922a47c4034Cristy    scanlines[i]=(MagickOffsetType) ReadBlobMSBSignedLong(image);
2823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
2833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Read image data.
2843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
2853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=0;
286bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  for (y=0; y < (ssize_t) image->rows; y++)
2873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
2883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    offset=SeekBlob(image,scanlines[image->rows-y-1],SEEK_SET);
2893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    if (offset < 0)
2903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      ThrowReaderException(CorruptImageError,"ImproperImageHeader");
2913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    for (channel=0; channel < (int) rla_info.number_channels; channel++)
2923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
2931fe0b879964fa7797e3d68574d297922a47c4034Cristy      length=ReadBlobMSBSignedShort(image);
2943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      while (length > 0)
2953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
2963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        byte=(unsigned char) ReadBlobByte(image);
2973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        runlength=byte;
2983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (byte > 127)
2993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          runlength=byte-256;
3003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        length--;
3013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (length == 0)
3023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
3033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (runlength < 0)
3043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
3053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            while (runlength < 0)
3063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
307bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy              q=GetAuthenticPixels(image,(ssize_t) (x % image->columns),
308bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy                (ssize_t) (y % image->rows),1,1,exception);
309acd2ed254c18c254a0ab5aafa06d1645e5d079d8cristy              if (q == (Quantum *) NULL)
3103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                break;
3113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              byte=(unsigned char) ReadBlobByte(image);
3123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              length--;
3133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              switch (channel)
3143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              {
3153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 0:
3163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
3174c08aed51c5899665ade97263692328eea4af106cristy                  SetPixelRed(image,ScaleCharToQuantum(byte),q);
3183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
3193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
3203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 1:
3213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
3224c08aed51c5899665ade97263692328eea4af106cristy                  SetPixelGreen(image,ScaleCharToQuantum(byte),q);
3233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
3243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
3253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 2:
3263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
3274c08aed51c5899665ade97263692328eea4af106cristy                  SetPixelBlue(image,ScaleCharToQuantum(byte),q);
3283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
3293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
3303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 3:
3313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                default:
3323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
3334c08aed51c5899665ade97263692328eea4af106cristy                  SetPixelAlpha(image,ScaleCharToQuantum(byte),q);
3343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
3353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
3363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              }
3373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if (SyncAuthenticPixels(image,exception) == MagickFalse)
3383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                break;
3393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              x++;
3403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              runlength++;
3413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
3423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            continue;
3433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
3443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        byte=(unsigned char) ReadBlobByte(image);
3453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        length--;
3463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        runlength++;
3473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        do
3483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
349bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy          q=GetAuthenticPixels(image,(ssize_t) (x % image->columns),
350bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy            (ssize_t) (y % image->rows),1,1,exception);
351acd2ed254c18c254a0ab5aafa06d1645e5d079d8cristy          if (q == (Quantum *) NULL)
3523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
3533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          switch (channel)
3543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
3553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0:
3563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
3574c08aed51c5899665ade97263692328eea4af106cristy              SetPixelRed(image,ScaleCharToQuantum(byte),q);
3583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
3593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
3603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 1:
3613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
3624c08aed51c5899665ade97263692328eea4af106cristy              SetPixelGreen(image,ScaleCharToQuantum(byte),q);
3633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
3643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
3653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 2:
3663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
3674c08aed51c5899665ade97263692328eea4af106cristy              SetPixelBlue(image,ScaleCharToQuantum(byte),q);
3683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
3693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
3703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 3:
3713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            default:
3723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
3734c08aed51c5899665ade97263692328eea4af106cristy              SetPixelAlpha(image,ScaleCharToQuantum(byte),q);
3743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
3753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
3763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
3773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if (SyncAuthenticPixels(image,exception) == MagickFalse)
3783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
3793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          x++;
3803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          runlength--;
3813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
3823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        while (runlength > 0);
3833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
3843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
385cee9711bbc334b5677d5ec4ea1cc70340d35ee35cristy    status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
386c6da28e61bb609d2b2cfdcc7752106c973415edbcristy      image->rows);
3873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    if (status == MagickFalse)
3883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      break;
3893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
3903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (EOFBlob(image) != MagickFalse)
3913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ThrowFileException(exception,CorruptImageError,"UnexpectedEndOfFile",
3923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      image->filename);
393952a6063b767d87c452b00435088197d38916dd8cristy  scanlines=(MagickOffsetType *) RelinquishMagickMemory(scanlines);
3943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) CloseBlob(image);
3953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(GetFirstImageInList(image));
3963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
3973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
3993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   R e g i s t e r R L A I m a g e                                           %
4043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
4093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  RegisterRLAImage() adds attributes for the RLA image format to
4103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  the list of supported formats.  The attributes include the image format
4113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  tag, a method to read and/or write the format, whether the format
4123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  supports the saving of more than one frame to the same file or blob,
4133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  whether the format supports native in-memory I/O, and a brief
4143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  description of the format.
4153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
4163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the RegisterRLAImage method is:
4173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
418bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy%      size_t RegisterRLAImage(void)
4193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
4203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
421bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristyModuleExport size_t RegisterRLAImage(void)
4223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
4233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  MagickInfo
4243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *entry;
4253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
42606b627a07ff44e1ff93ef1288c9f428066ded10ddirk  entry=AcquireMagickInfo("RLA","RLA","Alias/Wavefront image");
4273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->decoder=(DecodeImageHandler *) ReadRLAImage;
42808e9a113db499034abb5ad8d59b42f8eca3c641cdirk  entry->flags^=CoderAdjoinFlag;
42908e9a113db499034abb5ad8d59b42f8eca3c641cdirk  entry->flags|=CoderSeekableStreamFlag;
4303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) RegisterMagickInfo(entry);
4313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(MagickImageCoderSignature);
4323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
4333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
4353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   U n r e g i s t e r R L A I m a g e                                       %
4403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
4433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
4453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  UnregisterRLAImage() removes format registrations made by the
4463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  RLA module from the list of supported formats.
4473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
4483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the UnregisterRLAImage method is:
4493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
4503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      UnregisterRLAImage(void)
4513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
4523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
4533ed852eea50f9d4cd633efb8c2b054b8e33c253cristyModuleExport void UnregisterRLAImage(void)
4543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
4553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) UnregisterMagickInfo("RLA");
4563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
457