13ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
23ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
43ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
53ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                            W   W  PPPP    GGGG                              %
63ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                            W   W  P   P  G                                  %
73ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                            W W W  PPPP   G GGG                              %
83ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                            WW WW  P      G   G                              %
93ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                            W   W  P       GGG                               %
103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                       Read WordPerfect Image Format                         %
133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                              Software Design                                %
153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                              Jaroslav Fojtik                                %
163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                 June 2000                                   %
173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
197ce65e7125a4e1df1a274ce373c537a9df9c16cdCristy%  Copyright 1999-2016 ImageMagick Studio LLC, a non-profit organization      %
203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  dedicated to making software imaging solutions freely available.           %
213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  You may not use this file except in compliance with the License.  You may  %
233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  obtain a copy of the License at                                            %
243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    http://www.imagemagick.org/script/license.php                            %
263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  Unless required by applicable law or agreed to in writing, software        %
283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  distributed under the License is distributed on an "AS IS" BASIS,          %
293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  See the License for the specific language governing permissions and        %
313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  limitations under the License.                                             %
323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Include declarations.
403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
414c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/studio.h"
424c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/blob.h"
434c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/blob-private.h"
444c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/color-private.h"
454c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/colormap.h"
464c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/colormap-private.h"
474c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/constitute.h"
484c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/exception.h"
494c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/exception-private.h"
504c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/cache.h"
51c53413df5789700e14e1a67e6cc2716d1716a387cristy#include "MagickCore/distort.h"
524c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/image.h"
534c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/image-private.h"
544c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/list.h"
554c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/magic.h"
564c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/magick.h"
574c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/memory_.h"
584c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/resource_.h"
594c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/pixel-accessor.h"
604c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/quantum-private.h"
614c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/static.h"
624c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/string_.h"
634c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/module.h"
644c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/transform.h"
654c08aed51c5899665ade97263692328eea4af106cristy#include "MagickCore/utility.h"
6618c6c27bc513a8c73a5bc4a2c157afb19c3e24b9cristy#include "MagickCore/utility-private.h"
673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
683ed852eea50f9d4cd633efb8c2b054b8e33c253cristytypedef struct
693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy   {
703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy   unsigned char Red;
713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy   unsigned char Blue;
723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy   unsigned char Green;
733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy   } RGB_Record;
743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/* Default palette for WPG level 1 */
764189984c711febcbd1a99f01663b62cadee43a5acristystatic const RGB_Record WPG1_Palette[256]={
773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{  0,  0,  0},    {  0,  0,168},
783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{  0,168,  0},    {  0,168,168},
793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{168,  0,  0},    {168,  0,168},
803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{168, 84,  0},    {168,168,168},
813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{ 84, 84, 84},    { 84, 84,252},
823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{ 84,252, 84},    { 84,252,252},
833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252, 84, 84},    {252, 84,252},
843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,252, 84},    {252,252,252},  /*16*/
853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{  0,  0,  0},    { 20, 20, 20},
863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{ 32, 32, 32},    { 44, 44, 44},
873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{ 56, 56, 56},    { 68, 68, 68},
883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{ 80, 80, 80},    { 96, 96, 96},
893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,112,112},    {128,128,128},
903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{144,144,144},    {160,160,160},
913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{180,180,180},    {200,200,200},
923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{224,224,224},    {252,252,252},  /*32*/
933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{  0,  0,252},    { 64,  0,252},
943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{124,  0,252},    {188,  0,252},
953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,  0,252},    {252,  0,188},
963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,  0,124},    {252,  0, 64},
973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,  0,  0},    {252, 64,  0},
983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,124,  0},    {252,188,  0},
993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,252,  0},    {188,252,  0},
1003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{124,252,  0},    { 64,252,  0},  /*48*/
1013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{  0,252,  0},    {  0,252, 64},
1023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{  0,252,124},    {  0,252,188},
1033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{  0,252,252},    {  0,188,252},
1043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{  0,124,252},    {  0, 64,252},
1053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{124,124,252},    {156,124,252},
1063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{188,124,252},    {220,124,252},
1073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,124,252},    {252,124,220},
1083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,124,188},    {252,124,156},  /*64*/
1093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,124,124},    {252,156,124},
1103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,188,124},    {252,220,124},
1113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,252,124},    {220,252,124},
1123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{188,252,124},    {156,252,124},
1133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{124,252,124},    {124,252,156},
1143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{124,252,188},    {124,252,220},
1153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{124,252,252},    {124,220,252},
1163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{124,188,252},    {124,156,252},  /*80*/
1173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{180,180,252},    {196,180,252},
1183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{216,180,252},    {232,180,252},
1193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,180,252},    {252,180,232},
1203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,180,216},    {252,180,196},
1213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,180,180},    {252,196,180},
1223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,216,180},    {252,232,180},
1233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{252,252,180},    {232,252,180},
1243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{216,252,180},    {196,252,180},  /*96*/
1253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{180,220,180},    {180,252,196},
1263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{180,252,216},    {180,252,232},
1273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{180,252,252},    {180,232,252},
1283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{180,216,252},    {180,196,252},
1293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,0,112},    {28,0,112},
1303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{56,0,112},    {84,0,112},
1313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,0,112},    {112,0,84},
1323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,0,56},    {112,0,28},  /*112*/
1333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,0,0},    {112,28,0},
1343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,56,0},    {112,84,0},
1353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,112,0},    {84,112,0},
1363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{56,112,0},    {28,112,0},
1373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,112,0},    {0,112,28},
1383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,112,56},    {0,112,84},
1393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,112,112},    {0,84,112},
1403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,56,112},    {0,28,112},   /*128*/
1413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{56,56,112},    {68,56,112},
1423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{84,56,112},    {96,56,112},
1433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,56,112},    {112,56,96},
1443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,56,84},    {112,56,68},
1453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,56,56},    {112,68,56},
1463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,84,56},    {112,96,56},
1473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,112,56},    {96,112,56},
1483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{84,112,56},    {68,112,56},  /*144*/
1493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{56,112,56},    {56,112,69},
1503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{56,112,84},    {56,112,96},
1513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{56,112,112},    {56,96,112},
1523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{56,84,112},    {56,68,112},
1533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{80,80,112},    {88,80,112},
1543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{96,80,112},    {104,80,112},
1553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,80,112},    {112,80,104},
1563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,80,96},    {112,80,88},  /*160*/
1573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,80,80},    {112,88,80},
1583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,96,80},    {112,104,80},
1593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{112,112,80},    {104,112,80},
1603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{96,112,80},    {88,112,80},
1613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{80,112,80},    {80,112,88},
1623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{80,112,96},    {80,112,104},
1633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{80,112,112},    {80,114,112},
1643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{80,96,112},    {80,88,112},  /*176*/
1653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,0,64},    {16,0,64},
1663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{32,0,64},    {48,0,64},
1673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,0,64},    {64,0,48},
1683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,0,32},    {64,0,16},
1693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,0,0},    {64,16,0},
1703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,32,0},    {64,48,0},
1713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,64,0},    {48,64,0},
1723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{32,64,0},    {16,64,0},  /*192*/
1733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,64,0},    {0,64,16},
1743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,64,32},    {0,64,48},
1753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,64,64},    {0,48,64},
1763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,32,64},    {0,16,64},
1773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{32,32,64},    {40,32,64},
1783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{48,32,64},    {56,32,64},
1793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,32,64},    {64,32,56},
1803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,32,48},    {64,32,40},  /*208*/
1813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,32,32},    {64,40,32},
1823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,48,32},    {64,56,32},
1833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,64,32},    {56,64,32},
1843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{48,64,32},    {40,64,32},
1853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{32,64,32},    {32,64,40},
1863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{32,64,48},    {32,64,56},
1873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{32,64,64},    {32,56,64},
1883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{32,48,64},    {32,40,64},  /*224*/
1893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{44,44,64},    {48,44,64},
1903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{52,44,64},    {60,44,64},
1913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,44,64},    {64,44,60},
1923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,44,52},    {64,44,48},
1933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,44,44},    {64,48,44},
1943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,52,44},    {64,60,44},
1953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{64,64,44},    {60,64,44},
1963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{52,64,44},    {48,64,44},  /*240*/
1973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{44,64,44},    {44,64,48},
1983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{44,64,52},    {44,64,60},
1993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{44,64,64},    {44,60,64},
2003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{44,55,64},    {44,48,64},
2013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,0,0},    {0,0,0},
2023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,0,0},    {0,0,0},
2033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,0,0},    {0,0,0},
2043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{0,0,0},    {0,0,0}    /*256*/
2053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy};
2063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
2073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
2083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
2103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
2113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
2123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   I s W P G                                                                 %
2133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
2143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
2153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
2163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
2183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  IsWPG() returns True if the image format type, identified by the magick
2193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  string, is WPG.
2203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
2213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the IsWPG method is:
2223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
2233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      unsigned int IsWPG(const unsigned char *magick,const size_t length)
2243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
2253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  A description of each parameter follows:
2263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
2273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o status:  Method IsWPG returns True if the image format type is WPG.
2283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
2293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o magick: compare image format pattern against these bytes.
2303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
2313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o length: Specifies the length of the magick string.
2323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
2333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
2343ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic unsigned int IsWPG(const unsigned char *magick,const size_t length)
2353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
2363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (length < 4)
2373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return(MagickFalse);
2383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (memcmp(magick,"\377WPC",4) == 0)
2393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return(MagickTrue);
2403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(MagickFalse);
2413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
2423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
2433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
244bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristystatic void Rd_WP_DWORD(Image *image,size_t *d)
2453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
2463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned char
2473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    b;
2483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
2493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  b=ReadBlobByte(image);
2503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  *d=b;
2513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (b < 0xFFU)
2523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return;
2533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  b=ReadBlobByte(image);
254bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  *d=(size_t) b;
2553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  b=ReadBlobByte(image);
256bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  *d+=(size_t) b*256l;
2573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (*d < 0x8000)
2583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return;
2593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  *d=(*d & 0x7FFF) << 16;
2603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  b=ReadBlobByte(image);
261bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  *d+=(size_t) b;
2623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  b=ReadBlobByte(image);
263bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  *d+=(size_t) b*256l;
2643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return;
2653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
2663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
267c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristystatic void InsertRow(Image *image,unsigned char *p,ssize_t y,int bpp,
268c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy  ExceptionInfo *exception)
2693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
2703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  int
2713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    bit;
2723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
2734c08aed51c5899665ade97263692328eea4af106cristy  Quantum
2744c08aed51c5899665ade97263692328eea4af106cristy    index;
2753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
2764c08aed51c5899665ade97263692328eea4af106cristy  register Quantum
2773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *q;
2783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
2794c08aed51c5899665ade97263692328eea4af106cristy  ssize_t
2804c08aed51c5899665ade97263692328eea4af106cristy    x;
2813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
2823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  switch (bpp)
2833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
2843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 1:  /* Convert bitmap scanline. */
2853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
2863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
287acd2ed254c18c254a0ab5aafa06d1645e5d079d8cristy        if (q == (Quantum *) NULL)
2883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
289bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy        for (x=0; x < ((ssize_t) image->columns-7); x+=8)
2904c08aed51c5899665ade97263692328eea4af106cristy        {
2914c08aed51c5899665ade97263692328eea4af106cristy          for (bit=0; bit < 8; bit++)
2923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
2934c08aed51c5899665ade97263692328eea4af106cristy            index=((*p) & (0x80 >> bit) ? 0x01 : 0x00);
2944c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
29511a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
296ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
2973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
2984c08aed51c5899665ade97263692328eea4af106cristy          p++;
2994c08aed51c5899665ade97263692328eea4af106cristy        }
3003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if ((image->columns % 8) != 0)
3013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
302bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy            for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
3034c08aed51c5899665ade97263692328eea4af106cristy            {
3044c08aed51c5899665ade97263692328eea4af106cristy              index=((*p) & (0x80 >> bit) ? 0x01 : 0x00);
3054c08aed51c5899665ade97263692328eea4af106cristy              SetPixelIndex(image,index,q);
30611a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy              SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
307ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy              q+=GetPixelChannels(image);
3084c08aed51c5899665ade97263692328eea4af106cristy            }
3093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            p++;
3103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
3113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (!SyncAuthenticPixels(image,exception))
3123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
3133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        break;
3143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
3153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 2:  /* Convert PseudoColor scanline. */
3163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
3173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
318acd2ed254c18c254a0ab5aafa06d1645e5d079d8cristy        if (q == (Quantum *) NULL)
3193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
320998c687fb83993c13fa711d75f59a95b38ceab77dirk        for (x=0; x < ((ssize_t) image->columns-3); x+=4)
3213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
322c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
3234c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
32411a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
325ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
326c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
3274c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
32811a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
329ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
330c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,exception);
3314c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
33211a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
333ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
334c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,(*p) & 0x3,exception);
3354c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
33611a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
337ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
338998c687fb83993c13fa711d75f59a95b38ceab77dirk            p++;
3393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
3403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy       if ((image->columns % 4) != 0)
3413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
342c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
3434c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
34411a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
345ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
346998c687fb83993c13fa711d75f59a95b38ceab77dirk            if ((image->columns % 4) > 1)
3473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              {
348c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
3494c08aed51c5899665ade97263692328eea4af106cristy                SetPixelIndex(image,index,q);
35011a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy                SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
351ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy                q+=GetPixelChannels(image);
352998c687fb83993c13fa711d75f59a95b38ceab77dirk                if ((image->columns % 4) > 2)
3533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  {
354c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                    index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,
355c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                      exception);
3564c08aed51c5899665ade97263692328eea4af106cristy                    SetPixelIndex(image,index,q);
35711a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy                    SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
358ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy                    q+=GetPixelChannels(image);
3593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  }
3603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              }
3613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            p++;
3623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
3633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (SyncAuthenticPixels(image,exception) == MagickFalse)
3643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
3653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        break;
3663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
3673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 4:  /* Convert PseudoColor scanline. */
3693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
3703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
371acd2ed254c18c254a0ab5aafa06d1645e5d079d8cristy        if (q == (Quantum *) NULL)
3723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
373bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy        for (x=0; x < ((ssize_t) image->columns-1); x+=2)
3743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
375c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
3764c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
37711a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
378ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
379c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,(*p) & 0x0f,exception);
3804c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
38111a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
3823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            p++;
383ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
3843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
3853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if ((image->columns % 2) != 0)
3863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
387c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
3884c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
38911a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
3903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            p++;
391ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
3923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
3933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (SyncAuthenticPixels(image,exception) == MagickFalse)
3943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
3953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        break;
3963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
3973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 8: /* Convert PseudoColor scanline. */
3983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
3993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
400acd2ed254c18c254a0ab5aafa06d1645e5d079d8cristy        if (q == (Quantum *) NULL) break;
4013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
402bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy        for (x=0; x < (ssize_t) image->columns; x++)
4033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
404c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,*p,exception);
4054c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
40611a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
4073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            p++;
408ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
4093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
4103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (SyncAuthenticPixels(image,exception) == MagickFalse)
4113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
4123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
4133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      break;
4143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 24:     /*  Convert DirectColor scanline.  */
4163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
417acd2ed254c18c254a0ab5aafa06d1645e5d079d8cristy      if (q == (Quantum *) NULL)
4183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        break;
419bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy      for (x=0; x < (ssize_t) image->columns; x++)
4203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
4214c08aed51c5899665ade97263692328eea4af106cristy          SetPixelRed(image,ScaleCharToQuantum(*p++),q);
4224c08aed51c5899665ade97263692328eea4af106cristy          SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
4234c08aed51c5899665ade97263692328eea4af106cristy          SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
424ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy          q+=GetPixelChannels(image);
4253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
4263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      if (!SyncAuthenticPixels(image,exception))
4273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        break;
428998c687fb83993c13fa711d75f59a95b38ceab77dirk      break;
4293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
4303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
4313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/* Helper for WPG1 raster reader. */
4343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#define InsertByte(b) \
4353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{ \
4363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  BImgBuff[x]=b; \
4373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x++; \
438bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  if((ssize_t) x>=ldblk) \
4393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  { \
440c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy    InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception); \
4413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    x=0; \
4423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    y++; \
4433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    } \
4443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
4453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/* WPG1 raster reader. */
446c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristystatic int UnpackWPGRaster(Image *image,int bpp,ExceptionInfo *exception)
4473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
4483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  int
4493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    x,
4503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    y,
4513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    i;
4523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned char
4543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    bbuf,
4553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *BImgBuff,
4563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    RunCount;
4573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
458bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  ssize_t
4593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ldblk;
4603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=0;
4623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  y=0;
4633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
464bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  ldblk=(ssize_t) ((bpp*image->columns+7)/8);
4653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
466f432c635c526259b858c9aad3d409c5c44545686cristy    8*sizeof(*BImgBuff));
4673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(BImgBuff==NULL) return(-2);
4683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
469bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  while(y<(ssize_t) image->rows)
4703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
4714693836b52ec11c0348f9687b8c1f92ce27f991fcristy      int
4724693836b52ec11c0348f9687b8c1f92ce27f991fcristy        c;
4733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4744693836b52ec11c0348f9687b8c1f92ce27f991fcristy      c=ReadBlobByte(image);
4754693836b52ec11c0348f9687b8c1f92ce27f991fcristy      if (c == EOF)
4764693836b52ec11c0348f9687b8c1f92ce27f991fcristy        break;
4774693836b52ec11c0348f9687b8c1f92ce27f991fcristy      bbuf=(unsigned char) c;
4783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      RunCount=bbuf & 0x7F;
4793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      if(bbuf & 0x80)
4803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
4813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(RunCount)  /* repeat next byte runcount * */
4823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
4833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              bbuf=ReadBlobByte(image);
4843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              for(i=0;i<(int) RunCount;i++) InsertByte(bbuf);
4853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
4863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          else {  /* read next byte as RunCount; repeat 0xFF runcount* */
4871d4a6951ace4a184f51cfd8c25b1586c6453be96cristy            c=ReadBlobByte(image);
4881d4a6951ace4a184f51cfd8c25b1586c6453be96cristy            if (c < 0)
4891d4a6951ace4a184f51cfd8c25b1586c6453be96cristy              break;
4901d4a6951ace4a184f51cfd8c25b1586c6453be96cristy            RunCount=(unsigned char) c;
4913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            for(i=0;i<(int) RunCount;i++) InsertByte(0xFF);
4923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
4933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
4943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      else {
4953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if(RunCount)   /* next runcount byte are readed directly */
4963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
4973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            for(i=0;i < (int) RunCount;i++)
4983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              {
4993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                bbuf=ReadBlobByte(image);
5003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                InsertByte(bbuf);
5013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              }
5023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
5033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        else {  /* repeat previous line runcount* */
5041d4a6951ace4a184f51cfd8c25b1586c6453be96cristy          c=ReadBlobByte(image);
5051d4a6951ace4a184f51cfd8c25b1586c6453be96cristy          if (c < 0)
5061d4a6951ace4a184f51cfd8c25b1586c6453be96cristy            break;
5071d4a6951ace4a184f51cfd8c25b1586c6453be96cristy          RunCount=(unsigned char) c;
5083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(x) {    /* attempt to duplicate row from x position: */
5093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            /* I do not know what to do here */
5103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
5113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            return(-3);
5123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
5133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          for(i=0;i < (int) RunCount;i++)
5143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
5153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              x=0;
5163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              y++;    /* Here I need to duplicate previous row RUNCOUNT* */
5173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(y<2) continue;
518bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy              if(y>(ssize_t) image->rows)
5193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
5203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
5213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  return(-4);
5223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
523c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy              InsertRow(image,BImgBuff,y-1,bpp,exception);
5243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
5253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
5263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
527fcd84b40725bdcf5fdc90de1cdf7add3f325a286Cristy      if (EOFBlob(image) != MagickFalse)
528fcd84b40725bdcf5fdc90de1cdf7add3f325a286Cristy        break;
5293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
5303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
5314693836b52ec11c0348f9687b8c1f92ce27f991fcristy  return(y <(ssize_t) image->rows ? -5 : 0);
5323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
5333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
5343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
5353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/* Helper for WPG2 reader. */
5363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#define InsertByte6(b) \
5373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{ \
53893b02b797c4127ce2b06dbd3b2d75ecc33fca759dirkDisableMSCWarning(4310) \
5393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(XorMe)\
5403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    BImgBuff[x] = (unsigned char)~b;\
5413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  else\
5423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    BImgBuff[x] = b;\
54393b02b797c4127ce2b06dbd3b2d75ecc33fca759dirkRestoreMSCWarning \
5443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x++; \
545bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  if((ssize_t) x >= ldblk) \
5463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  { \
547c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy    InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception); \
5483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    x=0; \
5493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    y++; \
5503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy   } \
5513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
5523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/* WPG2 raster reader. */
553c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristystatic int UnpackWPG2Raster(Image *image,int bpp,ExceptionInfo *exception)
5543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
5558418c7e51974060a1c724e25d700d72fb437174bcristy  int
5568418c7e51974060a1c724e25d700d72fb437174bcristy    RunCount,
5578418c7e51974060a1c724e25d700d72fb437174bcristy    XorMe = 0;
5588418c7e51974060a1c724e25d700d72fb437174bcristy
5596a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy  size_t
5606a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy    x,
5616a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy    y;
5626a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy
5636a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy  ssize_t
564f2a82ee6e25411cb280db708ff26ab55cece1945cristy    i,
5656a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy    ldblk;
5666a1c5a9375dfad129bc7d6ae8f3eadea60cebe46cristy
5673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned int
5683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    SampleSize=1;
5693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
5703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned char
5713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    bbuf,
5723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *BImgBuff,
5738a1e4af723536f27e896029456130a6603923f61cristy    SampleBuffer[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
5743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
5753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=0;
5763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  y=0;
577bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  ldblk=(ssize_t) ((bpp*image->columns+7)/8);
5783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
5793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    sizeof(*BImgBuff));
5803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(BImgBuff==NULL)
5813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return(-2);
5823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
5833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  while( y< image->rows)
5843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
5853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      bbuf=ReadBlobByte(image);
5863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
5873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      switch(bbuf)
5883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
5893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        case 0x7D:
5903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          SampleSize=ReadBlobByte(image);  /* DSZ */
5913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(SampleSize>8)
5923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            return(-2);
5933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(SampleSize<1)
5943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            return(-2);
5953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
5963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        case 0x7E:
5971e604812fad85bb96f757a2393015ae3d061c39acristy          (void) FormatLocaleFile(stderr,
5981e604812fad85bb96f757a2393015ae3d061c39acristy            "\nUnsupported WPG token XOR, please report!");
5991e604812fad85bb96f757a2393015ae3d061c39acristy          XorMe=!XorMe;
6003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
6013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        case 0x7F:
6023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          RunCount=ReadBlobByte(image);   /* BLK */
6031d4a6951ace4a184f51cfd8c25b1586c6453be96cristy          if (RunCount < 0)
6046e394043e0ab26815d8ca88d4bf07a33f9c83b49cristy            break;
6053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          for(i=0; i < SampleSize*(RunCount+1); i++)
6063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
6073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              InsertByte6(0);
6083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
6093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
6103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        case 0xFD:
6111e604812fad85bb96f757a2393015ae3d061c39acristy          RunCount=ReadBlobByte(image);   /* EXT */
6121d4a6951ace4a184f51cfd8c25b1586c6453be96cristy          if (RunCount < 0)
6136e394043e0ab26815d8ca88d4bf07a33f9c83b49cristy            break;
6141e604812fad85bb96f757a2393015ae3d061c39acristy          for(i=0; i<= RunCount;i++)
6153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            for(bbuf=0; bbuf < SampleSize; bbuf++)
6163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              InsertByte6(SampleBuffer[bbuf]);
6173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
6183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        case 0xFE:
6193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          RunCount=ReadBlobByte(image);  /* RST */
6201d4a6951ace4a184f51cfd8c25b1586c6453be96cristy          if (RunCount < 0)
6216e394043e0ab26815d8ca88d4bf07a33f9c83b49cristy            break;
6223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(x!=0)
6233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
624b51dff5c0d16a4c1b69ff683e786cb3b4c467694cristy              (void) FormatLocaleFile(stderr,
6251e604812fad85bb96f757a2393015ae3d061c39acristy                "\nUnsupported WPG2 unaligned token RST x=%.20g, please report!\n"
6261e604812fad85bb96f757a2393015ae3d061c39acristy                ,(double) x);
6273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              return(-3);
6283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
6293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
6303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            /* duplicate the previous row RunCount x */
6313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            for(i=0;i<=RunCount;i++)
6323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              {
633c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                InsertRow(image,BImgBuff,(ssize_t) (image->rows >= y ? y : image->rows-1),
634c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                          bpp,exception);
6353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                y++;
6363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              }
6373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
6383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
6393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        case 0xFF:
6403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          RunCount=ReadBlobByte(image);   /* WHT */
6411d4a6951ace4a184f51cfd8c25b1586c6453be96cristy          if (RunCount < 0)
6426e394043e0ab26815d8ca88d4bf07a33f9c83b49cristy            break;
6433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          for(i=0; i < SampleSize*(RunCount+1); i++)
6443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
6453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              InsertByte6(0xFF);
6463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
6473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
6483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        default:
6493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          RunCount=bbuf & 0x7F;
6503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(bbuf & 0x80)     /* REP */
6523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
6533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              for(i=0; i < SampleSize; i++)
6543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                SampleBuffer[i]=ReadBlobByte(image);
6553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              for(i=0;i<=RunCount;i++)
6563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                for(bbuf=0;bbuf<SampleSize;bbuf++)
6573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  InsertByte6(SampleBuffer[bbuf]);
6583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
6593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          else {      /* NRP */
6603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            for(i=0; i< SampleSize*(RunCount+1);i++)
6613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              {
6623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                bbuf=ReadBlobByte(image);
6633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                InsertByte6(bbuf);
6643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              }
6653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
6663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
667fcd84b40725bdcf5fdc90de1cdf7add3f325a286Cristy      if (EOFBlob(image) != MagickFalse)
668fcd84b40725bdcf5fdc90de1cdf7add3f325a286Cristy        break;
6693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
6703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
6713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(0);
6723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
6733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6753ed852eea50f9d4cd633efb8c2b054b8e33c253cristytypedef float tCTM[3][3];
6763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6773ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic unsigned LoadWPG2Flags(Image *image,char Precision,float *Angle,tCTM *CTM)
6783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
6793ed852eea50f9d4cd633efb8c2b054b8e33c253cristyconst unsigned char TPR=1,TRN=2,SKW=4,SCL=8,ROT=0x10,OID=0x20,LCK=0x80;
680bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristyssize_t x;
6813ed852eea50f9d4cd633efb8c2b054b8e33c253cristyunsigned DenX;
6823ed852eea50f9d4cd633efb8c2b054b8e33c253cristyunsigned Flags;
6833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (void) memset(*CTM,0,sizeof(*CTM));     /*CTM.erase();CTM.resize(3,3);*/
6853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (*CTM)[0][0]=1;
6863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (*CTM)[1][1]=1;
6873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (*CTM)[2][2]=1;
6883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Flags=ReadBlobLSBShort(image);
690657749bd88b461303ede5dc0840eda51ac9e70a1cristy if(Flags & LCK) (void) ReadBlobLSBLong(image);  /*Edit lock*/
6913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & OID)
6923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
6933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(Precision==0)
694657749bd88b461303ede5dc0840eda51ac9e70a1cristy    {(void) ReadBlobLSBShort(image);}  /*ObjectID*/
6953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  else
696657749bd88b461303ede5dc0840eda51ac9e70a1cristy    {(void) ReadBlobLSBLong(image);}  /*ObjectID (Double precision)*/
6973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
6983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & ROT)
6993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
7003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image);  /*Rot Angle*/
7013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(Angle) *Angle=x/65536.0;
7023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
7033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & (ROT|SCL))
7043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
7053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image);  /*Sx*cos()*/
7063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[0][0] = (float)x/0x10000;
7073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image);  /*Sy*cos()*/
7083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[1][1] = (float)x/0x10000;
7093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
7103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & (ROT|SKW))
7113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
7123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image);       /*Kx*sin()*/
7133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[1][0] = (float)x/0x10000;
7143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image);       /*Ky*sin()*/
7153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[0][1] = (float)x/0x10000;
7163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
7173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & TRN)
7183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
7193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image);  /*Tx*/
7203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if(x>=0) (*CTM)[0][2] = (float)x+(float)DenX/0x10000;
7213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            else (*CTM)[0][2] = (float)x-(float)DenX/0x10000;
7223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image);  /*Ty*/
7233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[1][2]=(float)x + ((x>=0)?1:-1)*(float)DenX/0x10000;
7243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if(x>=0) (*CTM)[1][2] = (float)x+(float)DenX/0x10000;
7253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            else (*CTM)[1][2] = (float)x-(float)DenX/0x10000;
7263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
7273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & TPR)
7283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
7293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image);  /*Px*/
7303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[2][0] = x + (float)DenX/0x10000;;
7313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBShort(image);  DenX=ReadBlobLSBShort(image); /*Py*/
7323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[2][1] = x + (float)DenX/0x10000;
7333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
7343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return(Flags);
7353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
7363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7383ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic Image *ExtractPostscript(Image *image,const ImageInfo *image_info,
739bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  MagickOffsetType PS_Offset,ssize_t PS_Size,ExceptionInfo *exception)
7403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
7413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  char
742151b66dffc9e3c2e8c4f8cdaca37ff987ca0f497cristy    postscript_file[MagickPathExtent];
7433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  const MagicInfo
7453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *magic_info;
7463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  FILE
7483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *ps_file;
7493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  ImageInfo
7513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *clone_info;
7523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Image
7543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *image2;
7553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned char
757151b66dffc9e3c2e8c4f8cdaca37ff987ca0f497cristy    magick[2*MagickPathExtent];
7583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if ((clone_info=CloneImageInfo(image_info)) == NULL)
7613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return(image);
7623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  clone_info->blob=(void *) NULL;
7633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  clone_info->length=0;
7643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /* Obtain temporary file */
7664d0ca34912f861b25e5ed95e3f624048cb180358cristy  (void) AcquireUniqueFilename(postscript_file);
76718c6c27bc513a8c73a5bc4a2c157afb19c3e24b9cristy  ps_file=fopen_utf8(postscript_file,"wb");
7683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (ps_file == (FILE *) NULL)
7693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    goto FINISH;
7703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /* Copy postscript to temporary file */
7723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) SeekBlob(image,PS_Offset,SEEK_SET);
773151b66dffc9e3c2e8c4f8cdaca37ff987ca0f497cristy  (void) ReadBlob(image, 2*MagickPathExtent, magick);
7743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) SeekBlob(image,PS_Offset,SEEK_SET);
7763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  while(PS_Size-- > 0)
7773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
7783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      (void) fputc(ReadBlobByte(image),ps_file);
7793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
7803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) fclose(ps_file);
7813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* Detect file format - Check magic.mgk configuration file. */
783151b66dffc9e3c2e8c4f8cdaca37ff987ca0f497cristy  magic_info=GetMagicInfo(magick,2*MagickPathExtent,exception);
7843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(magic_info == (const MagicInfo *) NULL) goto FINISH_UNL;
7853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*     printf("Detected:%s  \n",magic_info->name); */
7863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(exception->severity != UndefinedException) goto FINISH_UNL;
7873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(magic_info->name == (char *) NULL) goto FINISH_UNL;
7883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
789b3dd69b23e9338806891c708a0cc8a82c0d1872aCristy  (void) strncpy(clone_info->magick,magic_info->name,MagickPathExtent);
7903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* Read nested image */
79253a8bc9b0c80f3027e032dcd8e77d8987c53bd6fcristy  /*FormatString(clone_info->filename,"%s:%s",magic_info->name,postscript_file);*/
793151b66dffc9e3c2e8c4f8cdaca37ff987ca0f497cristy  FormatLocaleString(clone_info->filename,MagickPathExtent,"%s",postscript_file);
7943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image2=ReadImage(clone_info,exception);
7953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (!image2)
7973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    goto FINISH_UNL;
7983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
8003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Replace current image with new image while copying base image
8013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    attributes.
8023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
8038b86c8143fb3785683e92098c2e477c144be1895Cristy  (void) CopyMagickString(image2->filename,image->filename,MagickPathExtent);
8048b86c8143fb3785683e92098c2e477c144be1895Cristy  (void) CopyMagickString(image2->magick_filename,image->magick_filename,MagickPathExtent);
8058b86c8143fb3785683e92098c2e477c144be1895Cristy  (void) CopyMagickString(image2->magick,image->magick,MagickPathExtent);
8063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image2->depth=image->depth;
8073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  DestroyBlob(image2);
8083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image2->blob=ReferenceBlob(image->blob);
8093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if ((image->rows == 0) || (image->columns == 0))
8113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    DeleteImageFromList(&image);
8123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  AppendImageToList(&image,image2);
8143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy FINISH_UNL:
8163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) RelinquishUniqueFileResource(postscript_file);
8173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy FINISH:
8183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  DestroyImageInfo(clone_info);
8193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(image);
8203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
8213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
8233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   R e a d W P G I m a g e                                                   %
8283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  Method ReadWPGImage reads an WPG X image file and returns it.  It
8343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  allocates the memory necessary for the new Image structure and returns a
8353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  pointer to the new image.
8363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the ReadWPGImage method is:
8383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
8403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  A description of each parameter follows:
8423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o image:  Method ReadWPGImage returns a pointer to the image after
8443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      reading. A null image is returned if there is a memory shortage or if
8453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      the image cannot be read.
8463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o image_info: Specifies a pointer to a ImageInfo structure.
8483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o exception: return any errors or warnings in this structure.
8503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
8523ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic Image *ReadWPGImage(const ImageInfo *image_info,
8533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  ExceptionInfo *exception)
8543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
8553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
857bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    size_t FileId;
8583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    MagickOffsetType DataOffset;
8593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int ProductType;
8603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int FileType;
8613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char MajorVersion;
8623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char MinorVersion;
8633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int EncryptKey;
8643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Reserved;
8653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGHeader;
8663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
8693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char RecType;
870bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    size_t RecordLength;
8713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGRecord;
8723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
8753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char Class;
8763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char RecType;
877bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    size_t Extension;
878bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    size_t RecordLength;
8793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPG2Record;
8803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
8833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned  HorizontalUnits;
8843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned  VerticalUnits;
8853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char PosSizePrecision;
8863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPG2Start;
8873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
8903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Width;
89174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy    unsigned int Height;
8923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Depth;
8933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int HorzRes;
8943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int VertRes;
8953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGBitmapType1;
8963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
8993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Width;
90074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy    unsigned int Height;
9013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char Depth;
9023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char Compression;
9033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPG2BitmapType1;
9043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
9063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
9073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int RotAngle;
9083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int LowLeftX;
9093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int LowLeftY;
9103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int UpRightX;
9113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int UpRightY;
9123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Width;
91374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy    unsigned int Height;
9143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Depth;
9153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int HorzRes;
9163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int VertRes;
9173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGBitmapType2;
9183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
9203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
9213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int StartIndex;
9223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int NumOfEntries;
9233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGColorMapRec;
9243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
925b0de93fdedaac769cb08e15b3ec176d4c9078907cristy  /*
9263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct {
927bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    size_t PS_unknown1;
9283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int PS_unknown2;
9293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int PS_unknown3;
9303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGPSl1Record;
931b0de93fdedaac769cb08e15b3ec176d4c9078907cristy  */
9323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Image
93474f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy    *image;
9353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned int
9373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    status;
9383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPGHeader
9403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Header;
9413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPGRecord
9433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Rec;
9443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPG2Record
9463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Rec2;
9473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPG2Start StartWPG;
9493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPGBitmapType1
9513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    BitmapHeader1;
9523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPG2BitmapType1
9543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Bitmap2Header1;
9553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPGBitmapType2
9573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    BitmapHeader2;
9583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPGColorMapRec
9603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    WPG_Palette;
9613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  int
9633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    i,
9643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    bpp,
9653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    WPG2Flags;
9663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
967bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  ssize_t
9683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ldblk;
9693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
970eaedf06777741da32408da72c1e512975c600c48cristy  size_t
971eaedf06777741da32408da72c1e512975c600c48cristy    one;
972eaedf06777741da32408da72c1e512975c600c48cristy
9733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned char
9743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *BImgBuff;
9753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  tCTM CTM;         /*current transform matrix*/
9773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
9793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Open image file.
9803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
9813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(image_info != (const ImageInfo *) NULL);
982e1c94d9d25db6b0dd7a5028ffee31d1057855d73cristy  assert(image_info->signature == MagickCoreSignature);
9833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(exception != (ExceptionInfo *) NULL);
984e1c94d9d25db6b0dd7a5028ffee31d1057855d73cristy  assert(exception->signature == MagickCoreSignature);
985eaedf06777741da32408da72c1e512975c600c48cristy  one=1;
9869950d57e1124b73f684fb5946e206994cefda628cristy  image=AcquireImage(image_info,exception);
9873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image->depth=8;
9883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
9893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (status == MagickFalse)
9903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
9913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      image=DestroyImageList(image);
9923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      return((Image *) NULL);
9933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
9943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
9953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Read WPG image.
9963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
9973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.FileId=ReadBlobLSBLong(image);
9983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.DataOffset=(MagickOffsetType) ReadBlobLSBLong(image);
9993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.ProductType=ReadBlobLSBShort(image);
10003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.FileType=ReadBlobLSBShort(image);
10013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.MajorVersion=ReadBlobByte(image);
10023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.MinorVersion=ReadBlobByte(image);
10033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.EncryptKey=ReadBlobLSBShort(image);
10043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.Reserved=ReadBlobLSBShort(image);
10053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (Header.FileId!=0x435057FF || (Header.ProductType>>8)!=0x16)
10073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
10083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (Header.EncryptKey!=0)
10093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ThrowReaderException(CoderError,"EncryptedWPGImageFileNotSupported");
10103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image->columns = 1;
10123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image->rows = 1;
10133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image->colors = 0;
10143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  bpp=0;
10153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  BitmapHeader2.RotAngle=0;
10163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  switch(Header.FileType)
10183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
10193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 1:     /* WPG level 1 */
10203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      while(!EOFBlob(image)) /* object parser loop */
10213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
10223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
10233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(EOFBlob(image))
10243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
10253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rec.RecType=(i=ReadBlobByte(image));
10273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(i==EOF)
10283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
10293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rd_WP_DWORD(image,&Rec.RecordLength);
10303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(EOFBlob(image))
10313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
10323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Header.DataOffset=TellBlob(image)+Rec.RecordLength;
10343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          switch(Rec.RecType)
10363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
10373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x0B: /* bitmap type 1 */
10383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader1.Width=ReadBlobLSBShort(image);
103974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              BitmapHeader1.Height=ReadBlobLSBShort(image);
104074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              if ((BitmapHeader1.Width == 0) || (BitmapHeader1.Height == 0))
104174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                ThrowReaderException(CorruptImageError,"ImproperImageHeader");
10423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader1.Depth=ReadBlobLSBShort(image);
10433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader1.HorzRes=ReadBlobLSBShort(image);
10443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader1.VertRes=ReadBlobLSBShort(image);
10453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(BitmapHeader1.HorzRes && BitmapHeader1.VertRes)
10473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
10483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->units=PixelsPerCentimeterResolution;
10492a11befa48257796843468409d77bb8cfb129cdccristy                  image->resolution.x=BitmapHeader1.HorzRes/470.0;
10502a11befa48257796843468409d77bb8cfb129cdccristy                  image->resolution.y=BitmapHeader1.VertRes/470.0;
10513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
10523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->columns=BitmapHeader1.Width;
105374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              image->rows=BitmapHeader1.Height;
10543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              bpp=BitmapHeader1.Depth;
10553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              goto UnpackRaster;
10573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x0E:  /*Color palette */
10593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              WPG_Palette.StartIndex=ReadBlobLSBShort(image);
10603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
10613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->colors=WPG_Palette.NumOfEntries;
1063018f07f7333b25743d0afff892450cebdb905c1acristy              if (!AcquireImageColormap(image,image->colors,exception))
10643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                goto NoMemory;
10653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              for (i=WPG_Palette.StartIndex;
10663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                   i < (int)WPG_Palette.NumOfEntries; i++)
10673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
10683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].red=ScaleCharToQuantum((unsigned char)
10693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
10703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].green=ScaleCharToQuantum((unsigned char)
10713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
10723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
10733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
10743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
10753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
10763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x11:  /* Start PS l1 */
10783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(Rec.RecordLength > 8)
10793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                image=ExtractPostscript(image,image_info,
10803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  TellBlob(image)+8,   /* skip PS header in the wpg */
1081bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy                  (ssize_t) Rec.RecordLength-8,exception);
10823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
10833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x14:  /* bitmap type 2 */
10853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.RotAngle=ReadBlobLSBShort(image);
10863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.LowLeftX=ReadBlobLSBShort(image);
10873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.LowLeftY=ReadBlobLSBShort(image);
10883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.UpRightX=ReadBlobLSBShort(image);
10893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.UpRightY=ReadBlobLSBShort(image);
10903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.Width=ReadBlobLSBShort(image);
109174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              BitmapHeader2.Height=ReadBlobLSBShort(image);
109274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              if ((BitmapHeader2.Width == 0) || (BitmapHeader2.Height == 0))
109374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                ThrowReaderException(CorruptImageError,"ImproperImageHeader");
10943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.Depth=ReadBlobLSBShort(image);
10953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.HorzRes=ReadBlobLSBShort(image);
10963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.VertRes=ReadBlobLSBShort(image);
10973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->units=PixelsPerCentimeterResolution;
10993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->page.width=(unsigned int)
11003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightX)/470.0);
11013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->page.height=(unsigned int)
11023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightY)/470.0);
11033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->page.x=(int) (BitmapHeader2.LowLeftX/470.0);
11043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->page.y=(int) (BitmapHeader2.LowLeftX/470.0);
11053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(BitmapHeader2.HorzRes && BitmapHeader2.VertRes)
11063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
11072a11befa48257796843468409d77bb8cfb129cdccristy                  image->resolution.x=BitmapHeader2.HorzRes/470.0;
11082a11befa48257796843468409d77bb8cfb129cdccristy                  image->resolution.y=BitmapHeader2.VertRes/470.0;
11093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
11103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->columns=BitmapHeader2.Width;
111174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              image->rows=BitmapHeader2.Height;
11123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              bpp=BitmapHeader2.Depth;
11133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
11143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            UnpackRaster:
1115fc43974d34318c834fbf78570ca1a3764ed8c7d7Cristy              status=SetImageExtent(image,image->columns,image->rows,exception);
1116fc43974d34318c834fbf78570ca1a3764ed8c7d7Cristy              if (status == MagickFalse)
1117fc43974d34318c834fbf78570ca1a3764ed8c7d7Cristy                break;
11183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if ((image->colors == 0) && (bpp != 24))
11193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
1120eaedf06777741da32408da72c1e512975c600c48cristy                  image->colors=one << bpp;
1121018f07f7333b25743d0afff892450cebdb905c1acristy                  if (!AcquireImageColormap(image,image->colors,exception))
11223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {
11233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    NoMemory:
11243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      ThrowReaderException(ResourceLimitError,
11253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                        "MemoryAllocationFailed");
11263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
11273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  /* printf("Load default colormap \n"); */
11283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  for (i=0; (i < (int) image->colors) && (i < 256); i++)
11293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {
11303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      image->colormap[i].red=ScaleCharToQuantum(WPG1_Palette[i].Red);
11313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      image->colormap[i].green=ScaleCharToQuantum(WPG1_Palette[i].Green);
11323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      image->colormap[i].blue=ScaleCharToQuantum(WPG1_Palette[i].Blue);
11333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
11343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
11353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              else
11363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
11373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if (bpp < 24)
1138eaedf06777741da32408da72c1e512975c600c48cristy                    if ( (image->colors < (one << bpp)) && (bpp != 24) )
1139101ab708b0574518ac5715da4d3915400e9df79acristy                      image->colormap=(PixelInfo *) ResizeQuantumMemory(
1140eaedf06777741da32408da72c1e512975c600c48cristy                        image->colormap,(size_t) (one << bpp),
11413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                        sizeof(*image->colormap));
11423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
11433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
11443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if (bpp == 1)
11453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
11463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if(image->colormap[0].red==0 &&
11473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     image->colormap[0].green==0 &&
11483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     image->colormap[0].blue==0 &&
11493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     image->colormap[1].red==0 &&
11503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     image->colormap[1].green==0 &&
11513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     image->colormap[1].blue==0)
11523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {  /* fix crippled monochrome palette */
11533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      image->colormap[1].red =
11543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                        image->colormap[1].green =
11553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                        image->colormap[1].blue = QuantumRange;
11563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
11573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
11583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1159c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy              if(UnpackWPGRaster(image,bpp,exception) < 0)
11603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                /* The raster cannot be unpacked */
11613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
11623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                DecompressionFailed:
11633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  ThrowReaderException(CoderError,"UnableToDecompressImage");
11643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
11653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
11663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(Rec.RecType==0x14 && BitmapHeader2.RotAngle!=0 && !image_info->ping)
11673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
11683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  /* flop command */
11693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if(BitmapHeader2.RotAngle & 0x8000)
11703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {
117174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      Image
117274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        *flop_image;
117374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy
117474f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      flop_image = FlopImage(image, exception);
117574f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      if (flop_image != (Image *) NULL) {
117674f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        DuplicateBlob(flop_image,image);
117774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        (void) RemoveLastImageFromList(&image);
117874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        AppendImageToList(&image,flop_image);
117974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      }
11803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
11813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  /* flip command */
11823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if(BitmapHeader2.RotAngle & 0x2000)
11833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {
118474f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      Image
118574f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        *flip_image;
118674f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy
118774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      flip_image = FlipImage(image, exception);
118874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      if (flip_image != (Image *) NULL) {
118974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        DuplicateBlob(flip_image,image);
119074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        (void) RemoveLastImageFromList(&image);
1191998c687fb83993c13fa711d75f59a95b38ceab77dirk                        AppendImageToList(&image,flip_image);
119274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      }
11933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
1194998c687fb83993c13fa711d75f59a95b38ceab77dirk                  /* rotate command */
11953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if(BitmapHeader2.RotAngle & 0x0FFF)
11963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {
119774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      Image
119874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        *rotate_image;
119974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy
120074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      rotate_image=RotateImage(image,(BitmapHeader2.RotAngle &
120174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        0x0FFF), exception);
120274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      if (rotate_image != (Image *) NULL) {
120374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        DuplicateBlob(rotate_image,image);
120474f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        (void) RemoveLastImageFromList(&image);
1205998c687fb83993c13fa711d75f59a95b38ceab77dirk                        AppendImageToList(&image,rotate_image);
120674f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      }
1207998c687fb83993c13fa711d75f59a95b38ceab77dirk                    }
12083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
12093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              /* Allocate next image structure. */
12119950d57e1124b73f684fb5946e206994cefda628cristy              AcquireNextImage(image_info,image,exception);
12123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->depth=8;
12133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if (image->next == (Image *) NULL)
12143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                goto Finish;
12153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image=SyncNextImageInList(image);
1216998c687fb83993c13fa711d75f59a95b38ceab77dirk              image->columns=image->rows=1;
12173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->colors=0;
12183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
12193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x1B:  /* Postscript l2 */
12213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(Rec.RecordLength>0x3C)
12223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                image=ExtractPostscript(image,image_info,
12233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  TellBlob(image)+0x3C,   /* skip PS l2 header in the wpg */
1224bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy                  (ssize_t) Rec.RecordLength-0x3C,exception);
12253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
12263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
12273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
12283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      break;
12293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 2:  /* WPG level 2 */
12313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      (void) memset(CTM,0,sizeof(CTM));
12323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      StartWPG.PosSizePrecision = 0;
12333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      while(!EOFBlob(image)) /* object parser loop */
12343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
12353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
12363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(EOFBlob(image))
12373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
12383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rec2.Class=(i=ReadBlobByte(image));
12403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(i==EOF)
12413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
12423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rec2.RecType=(i=ReadBlobByte(image));
12433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(i==EOF)
12443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
12453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rd_WP_DWORD(image,&Rec2.Extension);
12463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rd_WP_DWORD(image,&Rec2.RecordLength);
12473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(EOFBlob(image))
12483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
12493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Header.DataOffset=TellBlob(image)+Rec2.RecordLength;
12513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          switch(Rec2.RecType)
12533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
12543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      case 1:
12553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              StartWPG.HorizontalUnits=ReadBlobLSBShort(image);
12563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              StartWPG.VerticalUnits=ReadBlobLSBShort(image);
12573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              StartWPG.PosSizePrecision=ReadBlobByte(image);
12583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
12593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x0C:    /* Color palette */
12603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              WPG_Palette.StartIndex=ReadBlobLSBShort(image);
12613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
12623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->colors=WPG_Palette.NumOfEntries;
1264018f07f7333b25743d0afff892450cebdb905c1acristy              if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
12653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                ThrowReaderException(ResourceLimitError,
12663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  "MemoryAllocationFailed");
12673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              for (i=WPG_Palette.StartIndex;
12683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                   i < (int)WPG_Palette.NumOfEntries; i++)
12693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
12703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].red=ScaleCharToQuantum((char)
12713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
12723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].green=ScaleCharToQuantum((char)
12733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
12743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].blue=ScaleCharToQuantum((char)
12753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
12763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  (void) ReadBlobByte(image);   /*Opacity??*/
12773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
12783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
12793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x0E:
12803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              Bitmap2Header1.Width=ReadBlobLSBShort(image);
128174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              Bitmap2Header1.Height=ReadBlobLSBShort(image);
128274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              if ((Bitmap2Header1.Width == 0) || (Bitmap2Header1.Height == 0))
128374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                ThrowReaderException(CorruptImageError,"ImproperImageHeader");
12843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              Bitmap2Header1.Depth=ReadBlobByte(image);
12853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              Bitmap2Header1.Compression=ReadBlobByte(image);
12863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(Bitmap2Header1.Compression > 1)
12883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                continue; /*Unknown compression method */
12893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              switch(Bitmap2Header1.Depth)
12903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
12913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 1:
12923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  bpp=1;
12933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
12943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 2:
12953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  bpp=2;
12963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
12973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 3:
12983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  bpp=4;
12993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
13003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 4:
13013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  bpp=8;
13023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
13033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 8:
13043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  bpp=24;
13053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
13063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                default:
13073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  continue;  /*Ignore raster with unknown depth*/
13083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
13093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->columns=Bitmap2Header1.Width;
1310998c687fb83993c13fa711d75f59a95b38ceab77dirk              image->rows=Bitmap2Header1.Height;
1311fc43974d34318c834fbf78570ca1a3764ed8c7d7Cristy              status=SetImageExtent(image,image->columns,image->rows,exception);
1312fc43974d34318c834fbf78570ca1a3764ed8c7d7Cristy              if (status == MagickFalse)
1313fc43974d34318c834fbf78570ca1a3764ed8c7d7Cristy                break;
13143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if ((image->colors == 0) && (bpp != 24))
13153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
13160b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy                  size_t
13170b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy                    one;
13180b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy
13190b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy                  one=1;
13200b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy                  image->colors=one << bpp;
1321018f07f7333b25743d0afff892450cebdb905c1acristy                  if (!AcquireImageColormap(image,image->colors,exception))
13223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    goto NoMemory;
13233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
13243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              else
13253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
13263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if(bpp < 24)
1327eaedf06777741da32408da72c1e512975c600c48cristy                    if( image->colors<(one << bpp) && bpp!=24 )
1328101ab708b0574518ac5715da4d3915400e9df79acristy                      image->colormap=(PixelInfo *) ResizeQuantumMemory(
1329eaedf06777741da32408da72c1e512975c600c48cristy                       image->colormap,(size_t) (one << bpp),
13303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                       sizeof(*image->colormap));
13313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
13323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              switch(Bitmap2Header1.Compression)
13353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
13363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 0:    /*Uncompressed raster*/
13373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  {
1338bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy                    ldblk=(ssize_t) ((bpp*image->columns+7)/8);
13393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t)
1340bef1e4f637d8f665bc133a9c6d30df08d983bc3aCristy                      ldblk+1,sizeof(*BImgBuff));
13413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    if (BImgBuff == (unsigned char *) NULL)
13423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      goto NoMemory;
13433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1344bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy                    for(i=0; i< (ssize_t) image->rows; i++)
13453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      {
13463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                        (void) ReadBlob(image,ldblk,BImgBuff);
1347c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                        InsertRow(image,BImgBuff,i,bpp,exception);
13483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      }
13493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    if(BImgBuff)
1351998c687fb83993c13fa711d75f59a95b38ceab77dirk                      BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
13523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    break;
13533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  }
13543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 1:    /*RLE for WPG2 */
13553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  {
1356c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                    if( UnpackWPG2Raster(image,bpp,exception) < 0)
13573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      goto DecompressionFailed;
13583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    break;
13593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  }
13603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
13613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(CTM[0][0]<0 && !image_info->ping)
136374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                {    /*?? RotAngle=360-RotAngle;*/
136474f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                  Image
136574f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    *flop_image;
136674f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy
136774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                  flop_image = FlopImage(image, exception);
136874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                  if (flop_image != (Image *) NULL) {
136974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    DuplicateBlob(flop_image,image);
137074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    (void) RemoveLastImageFromList(&image);
137174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    AppendImageToList(&image,flop_image);
137274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                  }
1373998c687fb83993c13fa711d75f59a95b38ceab77dirk                  /* Try to change CTM according to Flip - I am not sure, must be checked.
13743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,0)=-1;      Tx(1,0)=0;   Tx(2,0)=0;
13753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,1)= 0;      Tx(1,1)=1;   Tx(2,1)=0;
13763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,2)=(WPG._2Rect.X_ur+WPG._2Rect.X_ll);
1377998c687fb83993c13fa711d75f59a95b38ceab77dirk                     Tx(1,2)=0;   Tx(2,2)=1; */
13783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
13793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(CTM[1][1]<0 && !image_info->ping)
138074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                {    /*?? RotAngle=360-RotAngle;*/
138174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                  Image
138274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    *flip_image;
138374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy
138474f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                   flip_image = FlipImage(image, exception);
138574f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                   if (flip_image != (Image *) NULL) {
138674f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                     DuplicateBlob(flip_image,image);
138774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                     (void) RemoveLastImageFromList(&image);
138874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                     AppendImageToList(&image,flip_image);
138974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    }
13903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  /* Try to change CTM according to Flip - I am not sure, must be checked.
13913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     float_matrix Tx(3,3);
13923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,0)= 1;   Tx(1,0)= 0;   Tx(2,0)=0;
13933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,1)= 0;   Tx(1,1)=-1;   Tx(2,1)=0;
13943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,2)= 0;   Tx(1,2)=(WPG._2Rect.Y_ur+WPG._2Rect.Y_ll);
1395998c687fb83993c13fa711d75f59a95b38ceab77dirk                     Tx(2,2)=1; */
1396998c687fb83993c13fa711d75f59a95b38ceab77dirk              }
1397998c687fb83993c13fa711d75f59a95b38ceab77dirk
13983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              /* Allocate next image structure. */
14009950d57e1124b73f684fb5946e206994cefda628cristy              AcquireNextImage(image_info,image,exception);
14013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->depth=8;
14023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if (image->next == (Image *) NULL)
14033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                goto Finish;
14043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image=SyncNextImageInList(image);
14053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->columns=image->rows=1;
14063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->colors=0;
14073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
14083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x12:  /* Postscript WPG2*/
14103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        i=ReadBlobLSBShort(image);
14113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(Rec2.RecordLength > (unsigned int) i)
14123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                image=ExtractPostscript(image,image_info,
14133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  TellBlob(image)+i,    /*skip PS header in the wpg2*/
1414bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy                  (ssize_t) (Rec2.RecordLength-i-2),exception);
14153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
14163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      case 0x1B:          /*bitmap rectangle*/
14183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              WPG2Flags = LoadWPG2Flags(image,StartWPG.PosSizePrecision,NULL,&CTM);
1419da16f16767eb31921af855f17bda465fffc4e000cristy              (void) WPG2Flags;
14203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
14213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
14223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
14233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      break;
14253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    default:
14273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
14283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy         ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
14293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
14303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy   }
14313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Finish:
14333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) CloseBlob(image);
14343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
14363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Image
14373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      *p;
14383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1439bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    ssize_t
14403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      scene=0;
14413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /*
14433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      Rewind list, removing any empty images while rewinding.
14443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    */
14453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    p=image;
14463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    image=NULL;
14473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    while (p != (Image *) NULL)
14483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
14493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        Image *tmp=p;
14503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if ((p->rows == 0) || (p->columns == 0)) {
14513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          p=p->previous;
14523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          DeleteImageFromList(&tmp);
14533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        } else {
14543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          image=p;
14553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          p=p->previous;
14563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
14573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
14583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /*
14593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      Fix scene numbers.
14603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    */
14613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    for (p=image; p != (Image *) NULL; p=p->next)
1462bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy      p->scene=(size_t) scene++;
14633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
14643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (image == (Image *) NULL)
14653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ThrowReaderException(CorruptImageError,
14663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      "ImageFileDoesNotContainAnyImageData");
14673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(image);
14683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
14693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
14713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   R e g i s t e r W P G I m a g e                                           %
14763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
14813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  Method RegisterWPGImage adds attributes for the WPG image format to
14823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  the list of supported formats.  The attributes include the image format
14833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  tag, a method to read and/or write the format, whether the format
14843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  supports the saving of more than one frame to the same file or blob,
14853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  whether the format supports native in-memory I/O, and a brief
14863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  description of the format.
14873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
14883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the RegisterWPGImage method is:
14893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
1490bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy%      size_t RegisterWPGImage(void)
14913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
14923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
1493bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristyModuleExport size_t RegisterWPGImage(void)
14943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
14953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  MagickInfo
14963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *entry;
14973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
149806b627a07ff44e1ff93ef1288c9f428066ded10ddirk  entry=AcquireMagickInfo("WPG","WPG","Word Perfect Graphics");
14993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->decoder=(DecodeImageHandler *) ReadWPGImage;
15003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->magick=(IsImageFormatHandler *) IsWPG;
150108e9a113db499034abb5ad8d59b42f8eca3c641cdirk  entry->flags|=CoderSeekableStreamFlag;
15023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) RegisterMagickInfo(entry);
15033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(MagickImageCoderSignature);
15043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
15053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
15063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
15073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   U n r e g i s t e r W P G I m a g e                                       %
15123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
15173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  Method UnregisterWPGImage removes format registrations made by the
15183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  WPG module from the list of supported formats.
15193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
15203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the UnregisterWPGImage method is:
15213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
15223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      UnregisterWPGImage(void)
15233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
15243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
15253ed852eea50f9d4cd633efb8c2b054b8e33c253cristyModuleExport void UnregisterWPGImage(void)
15263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
15273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) UnregisterMagickInfo("WPG");
15283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
1529