wpg.c revision 06b627a07ff44e1ff93ef1288c9f428066ded10d
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%                                                                             %
19b56bb24a985ca4366713bcd8ffdfacbb48a98a2fcristy%  Copyright 1999-2015 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;
320bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy        for (x=0; x < ((ssize_t) image->columns-1); x+=2)
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);
3373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            p++;
338ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
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);
3463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            if ((image->columns % 4) >= 1)
3473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              {
349c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
3504c08aed51c5899665ade97263692328eea4af106cristy                SetPixelIndex(image,index,q);
35111a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy                SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
352ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy                q+=GetPixelChannels(image);
3533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                if ((image->columns % 4) >= 2)
3543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  {
356c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                    index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,
357c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                      exception);
3584c08aed51c5899665ade97263692328eea4af106cristy                    SetPixelIndex(image,index,q);
35911a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy                    SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
360ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy                    q+=GetPixelChannels(image);
3613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  }
3623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              }
3633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            p++;
3643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
3653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (SyncAuthenticPixels(image,exception) == MagickFalse)
3663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
3673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        break;
3683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
3693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
3703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 4:  /* Convert PseudoColor scanline. */
3713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
3723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
373acd2ed254c18c254a0ab5aafa06d1645e5d079d8cristy        if (q == (Quantum *) NULL)
3743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
375bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy        for (x=0; x < ((ssize_t) image->columns-1); x+=2)
3763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
377c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
3784c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
37911a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
380ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
381c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,(*p) & 0x0f,exception);
3824c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
38311a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
3843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            p++;
385ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
3863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
3873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if ((image->columns % 2) != 0)
3883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
389c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
3904c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
39111a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
3923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            p++;
393ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
3943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
3953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (SyncAuthenticPixels(image,exception) == MagickFalse)
3963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
3973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        break;
3983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
3993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 8: /* Convert PseudoColor scanline. */
4003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
4013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
402acd2ed254c18c254a0ab5aafa06d1645e5d079d8cristy        if (q == (Quantum *) NULL) break;
4033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
404bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy        for (x=0; x < (ssize_t) image->columns; x++)
4053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
406c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy            index=ConstrainColormapIndex(image,*p,exception);
4074c08aed51c5899665ade97263692328eea4af106cristy            SetPixelIndex(image,index,q);
40811a06d3f2cac0f17af7963e83bc6e9ebd2a377c0cristy            SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
4093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            p++;
410ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy            q+=GetPixelChannels(image);
4113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
4123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if (SyncAuthenticPixels(image,exception) == MagickFalse)
4133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          break;
4143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
4153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      break;
4163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 24:     /*  Convert DirectColor scanline.  */
4183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
419acd2ed254c18c254a0ab5aafa06d1645e5d079d8cristy      if (q == (Quantum *) NULL)
4203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        break;
421bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy      for (x=0; x < (ssize_t) image->columns; x++)
4223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
4234c08aed51c5899665ade97263692328eea4af106cristy          SetPixelRed(image,ScaleCharToQuantum(*p++),q);
4244c08aed51c5899665ade97263692328eea4af106cristy          SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
4254c08aed51c5899665ade97263692328eea4af106cristy          SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
426ed2315769b26818ed9d0c1291dc0457f0d8da0a4cristy          q+=GetPixelChannels(image);
4273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
4283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      if (!SyncAuthenticPixels(image,exception))
4293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        break;
4303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      break;
4313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
4323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
4333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/* Helper for WPG1 raster reader. */
4363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy#define InsertByte(b) \
4373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{ \
4383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  BImgBuff[x]=b; \
4393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x++; \
440bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  if((ssize_t) x>=ldblk) \
4413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  { \
442c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy    InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception); \
4433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    x=0; \
4443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    y++; \
4453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    } \
4463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
4473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/* WPG1 raster reader. */
448c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristystatic int UnpackWPGRaster(Image *image,int bpp,ExceptionInfo *exception)
4493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
4503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  int
4513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    x,
4523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    y,
4533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    i;
4543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned char
4563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    bbuf,
4573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *BImgBuff,
4583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    RunCount;
4593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
460bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  ssize_t
4613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ldblk;
4623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=0;
4643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  y=0;
4653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
466bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  ldblk=(ssize_t) ((bpp*image->columns+7)/8);
4673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
468f432c635c526259b858c9aad3d409c5c44545686cristy    8*sizeof(*BImgBuff));
4693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(BImgBuff==NULL) return(-2);
4703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
471bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  while(y<(ssize_t) image->rows)
4723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
4734693836b52ec11c0348f9687b8c1f92ce27f991fcristy      int
4744693836b52ec11c0348f9687b8c1f92ce27f991fcristy        c;
4753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
4764693836b52ec11c0348f9687b8c1f92ce27f991fcristy      c=ReadBlobByte(image);
4774693836b52ec11c0348f9687b8c1f92ce27f991fcristy      if (c == EOF)
4784693836b52ec11c0348f9687b8c1f92ce27f991fcristy        break;
4794693836b52ec11c0348f9687b8c1f92ce27f991fcristy      bbuf=(unsigned char) c;
4803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      RunCount=bbuf & 0x7F;
4813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      if(bbuf & 0x80)
4823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
4833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(RunCount)  /* repeat next byte runcount * */
4843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
4853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              bbuf=ReadBlobByte(image);
4863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              for(i=0;i<(int) RunCount;i++) InsertByte(bbuf);
4873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
4883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          else {  /* read next byte as RunCount; repeat 0xFF runcount* */
4891d4a6951ace4a184f51cfd8c25b1586c6453be96cristy            c=ReadBlobByte(image);
4901d4a6951ace4a184f51cfd8c25b1586c6453be96cristy            if (c < 0)
4911d4a6951ace4a184f51cfd8c25b1586c6453be96cristy              break;
4921d4a6951ace4a184f51cfd8c25b1586c6453be96cristy            RunCount=(unsigned char) c;
4933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            for(i=0;i<(int) RunCount;i++) InsertByte(0xFF);
4943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
4953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
4963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      else {
4973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if(RunCount)   /* next runcount byte are readed directly */
4983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          {
4993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            for(i=0;i < (int) RunCount;i++)
5003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              {
5013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                bbuf=ReadBlobByte(image);
5023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                InsertByte(bbuf);
5033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              }
5043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
5053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        else {  /* repeat previous line runcount* */
5061d4a6951ace4a184f51cfd8c25b1586c6453be96cristy          c=ReadBlobByte(image);
5071d4a6951ace4a184f51cfd8c25b1586c6453be96cristy          if (c < 0)
5081d4a6951ace4a184f51cfd8c25b1586c6453be96cristy            break;
5091d4a6951ace4a184f51cfd8c25b1586c6453be96cristy          RunCount=(unsigned char) c;
5103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(x) {    /* attempt to duplicate row from x position: */
5113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            /* I do not know what to do here */
5123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
5133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            return(-3);
5143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          }
5153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          for(i=0;i < (int) RunCount;i++)
5163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
5173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              x=0;
5183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              y++;    /* Here I need to duplicate previous row RUNCOUNT* */
5193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(y<2) continue;
520bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy              if(y>(ssize_t) image->rows)
5213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
5223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
5233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  return(-4);
5243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
525c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy              InsertRow(image,BImgBuff,y-1,bpp,exception);
5263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
5273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
5283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
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        }
6673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
6683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
6693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(0);
6703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
6713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6733ed852eea50f9d4cd633efb8c2b054b8e33c253cristytypedef float tCTM[3][3];
6743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6753ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic unsigned LoadWPG2Flags(Image *image,char Precision,float *Angle,tCTM *CTM)
6763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
6773ed852eea50f9d4cd633efb8c2b054b8e33c253cristyconst unsigned char TPR=1,TRN=2,SKW=4,SCL=8,ROT=0x10,OID=0x20,LCK=0x80;
678bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristyssize_t x;
6793ed852eea50f9d4cd633efb8c2b054b8e33c253cristyunsigned DenX;
6803ed852eea50f9d4cd633efb8c2b054b8e33c253cristyunsigned Flags;
6813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (void) memset(*CTM,0,sizeof(*CTM));     /*CTM.erase();CTM.resize(3,3);*/
6833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (*CTM)[0][0]=1;
6843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (*CTM)[1][1]=1;
6853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy (*CTM)[2][2]=1;
6863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
6873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Flags=ReadBlobLSBShort(image);
6883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & LCK) x=ReadBlobLSBLong(image);  /*Edit lock*/
6893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & OID)
6903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
6913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(Precision==0)
6923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {x=ReadBlobLSBShort(image);}  /*ObjectID*/
6933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  else
6943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {x=ReadBlobLSBLong(image);}  /*ObjectID (Double precision)*/
6953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
6963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & ROT)
6973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
6983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image);  /*Rot Angle*/
6993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(Angle) *Angle=x/65536.0;
7003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
7013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & (ROT|SCL))
7023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
7033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image);  /*Sx*cos()*/
7043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[0][0] = (float)x/0x10000;
7053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image);  /*Sy*cos()*/
7063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[1][1] = (float)x/0x10000;
7073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
7083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & (ROT|SKW))
7093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
7103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image);       /*Kx*sin()*/
7113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[1][0] = (float)x/0x10000;
7123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image);       /*Ky*sin()*/
7133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[0][1] = (float)x/0x10000;
7143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
7153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & TRN)
7163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
7173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image);  /*Tx*/
7183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if(x>=0) (*CTM)[0][2] = (float)x+(float)DenX/0x10000;
7193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            else (*CTM)[0][2] = (float)x-(float)DenX/0x10000;
7203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image);  /*Ty*/
7213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[1][2]=(float)x + ((x>=0)?1:-1)*(float)DenX/0x10000;
7223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if(x>=0) (*CTM)[1][2] = (float)x+(float)DenX/0x10000;
7233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            else (*CTM)[1][2] = (float)x-(float)DenX/0x10000;
7243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
7253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy if(Flags & TPR)
7263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
7273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image);  /*Px*/
7283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[2][0] = x + (float)DenX/0x10000;;
7293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  x=ReadBlobLSBShort(image);  DenX=ReadBlobLSBShort(image); /*Py*/
7303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (*CTM)[2][1] = x + (float)DenX/0x10000;
7313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
7323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy return(Flags);
7333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
7343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7363ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic Image *ExtractPostscript(Image *image,const ImageInfo *image_info,
737bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  MagickOffsetType PS_Offset,ssize_t PS_Size,ExceptionInfo *exception)
7383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
7393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  char
7403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    postscript_file[MaxTextExtent];
7413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  const MagicInfo
7433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *magic_info;
7443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  FILE
7463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *ps_file;
7473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  ImageInfo
7493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *clone_info;
7503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Image
7523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *image2;
7533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned char
7553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    magick[2*MaxTextExtent];
7563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if ((clone_info=CloneImageInfo(image_info)) == NULL)
7593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    return(image);
7603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  clone_info->blob=(void *) NULL;
7613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  clone_info->length=0;
7623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /* Obtain temporary file */
7644d0ca34912f861b25e5ed95e3f624048cb180358cristy  (void) AcquireUniqueFilename(postscript_file);
76518c6c27bc513a8c73a5bc4a2c157afb19c3e24b9cristy  ps_file=fopen_utf8(postscript_file,"wb");
7663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (ps_file == (FILE *) NULL)
7673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    goto FINISH;
7683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /* Copy postscript to temporary file */
7703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) SeekBlob(image,PS_Offset,SEEK_SET);
7713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) ReadBlob(image, 2*MaxTextExtent, magick);
7723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) SeekBlob(image,PS_Offset,SEEK_SET);
7743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  while(PS_Size-- > 0)
7753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
7763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      (void) fputc(ReadBlobByte(image),ps_file);
7773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
7783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) fclose(ps_file);
7793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* Detect file format - Check magic.mgk configuration file. */
7813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  magic_info=GetMagicInfo(magick,2*MaxTextExtent,exception);
7823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(magic_info == (const MagicInfo *) NULL) goto FINISH_UNL;
7833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*     printf("Detected:%s  \n",magic_info->name); */
7843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(exception->severity != UndefinedException) goto FINISH_UNL;
7853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if(magic_info->name == (char *) NULL) goto FINISH_UNL;
7863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) CopyMagickMemory(clone_info->magick,magic_info->name,MaxTextExtent);
7883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /* Read nested image */
79053a8bc9b0c80f3027e032dcd8e77d8987c53bd6fcristy  /*FormatString(clone_info->filename,"%s:%s",magic_info->name,postscript_file);*/
791b51dff5c0d16a4c1b69ff683e786cb3b4c467694cristy  FormatLocaleString(clone_info->filename,MaxTextExtent,"%s",postscript_file);
7923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image2=ReadImage(clone_info,exception);
7933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (!image2)
7953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    goto FINISH_UNL;
7963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
7973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
7983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Replace current image with new image while copying base image
7993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    attributes.
8003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
8013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) CopyMagickMemory(image2->filename,image->filename,MaxTextExtent);
8023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) CopyMagickMemory(image2->magick_filename,image->magick_filename,MaxTextExtent);
8033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) CopyMagickMemory(image2->magick,image->magick,MaxTextExtent);
8043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image2->depth=image->depth;
8053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  DestroyBlob(image2);
8063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image2->blob=ReferenceBlob(image->blob);
8073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if ((image->rows == 0) || (image->columns == 0))
8093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    DeleteImageFromList(&image);
8103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  AppendImageToList(&image,image2);
8123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy FINISH_UNL:
8143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) RelinquishUniqueFileResource(postscript_file);
8153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy FINISH:
8163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  DestroyImageInfo(clone_info);
8173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(image);
8183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
8193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
8213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   R e a d W P G I m a g e                                                   %
8263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
8293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
8303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  Method ReadWPGImage reads an WPG X image file and returns it.  It
8323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  allocates the memory necessary for the new Image structure and returns a
8333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  pointer to the new image.
8343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the ReadWPGImage method is:
8363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
8383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  A description of each parameter follows:
8403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o image:  Method ReadWPGImage returns a pointer to the image after
8423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      reading. A null image is returned if there is a memory shortage or if
8433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      the image cannot be read.
8443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o image_info: Specifies a pointer to a ImageInfo structure.
8463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%    o exception: return any errors or warnings in this structure.
8483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
8493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
8503ed852eea50f9d4cd633efb8c2b054b8e33c253cristystatic Image *ReadWPGImage(const ImageInfo *image_info,
8513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  ExceptionInfo *exception)
8523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
8533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
855bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    size_t FileId;
8563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    MagickOffsetType DataOffset;
8573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int ProductType;
8583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int FileType;
8593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char MajorVersion;
8603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char MinorVersion;
8613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int EncryptKey;
8623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Reserved;
8633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGHeader;
8643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
8673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char RecType;
868bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    size_t RecordLength;
8693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGRecord;
8703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
8733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char Class;
8743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char RecType;
875bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    size_t Extension;
876bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    size_t RecordLength;
8773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPG2Record;
8783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
8813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned  HorizontalUnits;
8823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned  VerticalUnits;
8833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char PosSizePrecision;
8843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPG2Start;
8853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
8883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Width;
88974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy    unsigned int Height;
8903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Depth;
8913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int HorzRes;
8923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int VertRes;
8933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGBitmapType1;
8943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
8953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
8963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
8973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Width;
89874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy    unsigned int Height;
8993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char Depth;
9003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned char Compression;
9013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPG2BitmapType1;
9023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
9043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
9053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int RotAngle;
9063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int LowLeftX;
9073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int LowLeftY;
9083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int UpRightX;
9093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int UpRightY;
9103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Width;
91174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy    unsigned int Height;
9123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int Depth;
9133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int HorzRes;
9143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int VertRes;
9153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGBitmapType2;
9163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct
9183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
9193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int StartIndex;
9203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int NumOfEntries;
9213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGColorMapRec;
9223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
923b0de93fdedaac769cb08e15b3ec176d4c9078907cristy  /*
9243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  typedef struct {
925bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    size_t PS_unknown1;
9263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int PS_unknown2;
9273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    unsigned int PS_unknown3;
9283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  } WPGPSl1Record;
929b0de93fdedaac769cb08e15b3ec176d4c9078907cristy  */
9303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Image
93274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy    *image;
9333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned int
9353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    status;
9363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPGHeader
9383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Header;
9393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPGRecord
9413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Rec;
9423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPG2Record
9443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Rec2;
9453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPG2Start StartWPG;
9473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPGBitmapType1
9493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    BitmapHeader1;
9503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPG2BitmapType1
9523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Bitmap2Header1;
9533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPGBitmapType2
9553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    BitmapHeader2;
9563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  WPGColorMapRec
9583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    WPG_Palette;
9593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  int
9613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    i,
9623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    bpp,
9633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    WPG2Flags;
9643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
965bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy  ssize_t
9663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ldblk;
9673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
968eaedf06777741da32408da72c1e512975c600c48cristy  size_t
969eaedf06777741da32408da72c1e512975c600c48cristy    one;
970eaedf06777741da32408da72c1e512975c600c48cristy
9713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  unsigned char
9723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *BImgBuff;
9733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  tCTM CTM;         /*current transform matrix*/
9753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
9763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
9773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Open image file.
9783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
9793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(image_info != (const ImageInfo *) NULL);
9803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(image_info->signature == MagickSignature);
9813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(exception != (ExceptionInfo *) NULL);
9823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  assert(exception->signature == MagickSignature);
983eaedf06777741da32408da72c1e512975c600c48cristy  one=1;
9849950d57e1124b73f684fb5946e206994cefda628cristy  image=AcquireImage(image_info,exception);
9853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image->depth=8;
9863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
9873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (status == MagickFalse)
9883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
9893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      image=DestroyImageList(image);
9903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      return((Image *) NULL);
9913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    }
9923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  /*
9933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Read WPG image.
9943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  */
9953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.FileId=ReadBlobLSBLong(image);
9963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.DataOffset=(MagickOffsetType) ReadBlobLSBLong(image);
9973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.ProductType=ReadBlobLSBShort(image);
9983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.FileType=ReadBlobLSBShort(image);
9993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.MajorVersion=ReadBlobByte(image);
10003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.MinorVersion=ReadBlobByte(image);
10013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.EncryptKey=ReadBlobLSBShort(image);
10023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  Header.Reserved=ReadBlobLSBShort(image);
10033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (Header.FileId!=0x435057FF || (Header.ProductType>>8)!=0x16)
10053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
10063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (Header.EncryptKey!=0)
10073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ThrowReaderException(CoderError,"EncryptedWPGImageFileNotSupported");
10083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image->columns = 1;
10103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image->rows = 1;
10113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  image->colors = 0;
10123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  bpp=0;
10133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  BitmapHeader2.RotAngle=0;
10143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  switch(Header.FileType)
10163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    {
10173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 1:     /* WPG level 1 */
10183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      while(!EOFBlob(image)) /* object parser loop */
10193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
10203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
10213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(EOFBlob(image))
10223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
10233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rec.RecType=(i=ReadBlobByte(image));
10253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(i==EOF)
10263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
10273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rd_WP_DWORD(image,&Rec.RecordLength);
10283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(EOFBlob(image))
10293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
10303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Header.DataOffset=TellBlob(image)+Rec.RecordLength;
10323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          switch(Rec.RecType)
10343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
10353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x0B: /* bitmap type 1 */
10363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader1.Width=ReadBlobLSBShort(image);
103774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              BitmapHeader1.Height=ReadBlobLSBShort(image);
103874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              if ((BitmapHeader1.Width == 0) || (BitmapHeader1.Height == 0))
103974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                ThrowReaderException(CorruptImageError,"ImproperImageHeader");
10403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader1.Depth=ReadBlobLSBShort(image);
10413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader1.HorzRes=ReadBlobLSBShort(image);
10423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader1.VertRes=ReadBlobLSBShort(image);
10433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(BitmapHeader1.HorzRes && BitmapHeader1.VertRes)
10453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
10463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->units=PixelsPerCentimeterResolution;
10472a11befa48257796843468409d77bb8cfb129cdccristy                  image->resolution.x=BitmapHeader1.HorzRes/470.0;
10482a11befa48257796843468409d77bb8cfb129cdccristy                  image->resolution.y=BitmapHeader1.VertRes/470.0;
10493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
10503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->columns=BitmapHeader1.Width;
105174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              image->rows=BitmapHeader1.Height;
10523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              bpp=BitmapHeader1.Depth;
10533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              goto UnpackRaster;
10553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x0E:  /*Color palette */
10573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              WPG_Palette.StartIndex=ReadBlobLSBShort(image);
10583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
10593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->colors=WPG_Palette.NumOfEntries;
1061018f07f7333b25743d0afff892450cebdb905c1acristy              if (!AcquireImageColormap(image,image->colors,exception))
10623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                goto NoMemory;
10633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              for (i=WPG_Palette.StartIndex;
10643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                   i < (int)WPG_Palette.NumOfEntries; i++)
10653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
10663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].red=ScaleCharToQuantum((unsigned char)
10673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
10683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].green=ScaleCharToQuantum((unsigned char)
10693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
10703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
10713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
10723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
10733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
10743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x11:  /* Start PS l1 */
10763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(Rec.RecordLength > 8)
10773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                image=ExtractPostscript(image,image_info,
10783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  TellBlob(image)+8,   /* skip PS header in the wpg */
1079bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy                  (ssize_t) Rec.RecordLength-8,exception);
10803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
10813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x14:  /* bitmap type 2 */
10833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.RotAngle=ReadBlobLSBShort(image);
10843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.LowLeftX=ReadBlobLSBShort(image);
10853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.LowLeftY=ReadBlobLSBShort(image);
10863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.UpRightX=ReadBlobLSBShort(image);
10873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.UpRightY=ReadBlobLSBShort(image);
10883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.Width=ReadBlobLSBShort(image);
108974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              BitmapHeader2.Height=ReadBlobLSBShort(image);
109074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              if ((BitmapHeader2.Width == 0) || (BitmapHeader2.Height == 0))
109174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                ThrowReaderException(CorruptImageError,"ImproperImageHeader");
10923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.Depth=ReadBlobLSBShort(image);
10933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.HorzRes=ReadBlobLSBShort(image);
10943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              BitmapHeader2.VertRes=ReadBlobLSBShort(image);
10953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
10963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->units=PixelsPerCentimeterResolution;
10973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->page.width=(unsigned int)
10983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightX)/470.0);
10993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->page.height=(unsigned int)
11003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightY)/470.0);
11013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->page.x=(int) (BitmapHeader2.LowLeftX/470.0);
11023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->page.y=(int) (BitmapHeader2.LowLeftX/470.0);
11033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(BitmapHeader2.HorzRes && BitmapHeader2.VertRes)
11043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
11052a11befa48257796843468409d77bb8cfb129cdccristy                  image->resolution.x=BitmapHeader2.HorzRes/470.0;
11062a11befa48257796843468409d77bb8cfb129cdccristy                  image->resolution.y=BitmapHeader2.VertRes/470.0;
11073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
11083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->columns=BitmapHeader2.Width;
110974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              image->rows=BitmapHeader2.Height;
11103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              bpp=BitmapHeader2.Depth;
11113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
11123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            UnpackRaster:
11133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if ((image->colors == 0) && (bpp != 24))
11143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
1115eaedf06777741da32408da72c1e512975c600c48cristy                  image->colors=one << bpp;
1116018f07f7333b25743d0afff892450cebdb905c1acristy                  if (!AcquireImageColormap(image,image->colors,exception))
11173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {
11183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    NoMemory:
11193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      ThrowReaderException(ResourceLimitError,
11203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                        "MemoryAllocationFailed");
11213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
11223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  /* printf("Load default colormap \n"); */
11233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  for (i=0; (i < (int) image->colors) && (i < 256); i++)
11243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {
11253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      image->colormap[i].red=ScaleCharToQuantum(WPG1_Palette[i].Red);
11263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      image->colormap[i].green=ScaleCharToQuantum(WPG1_Palette[i].Green);
11273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      image->colormap[i].blue=ScaleCharToQuantum(WPG1_Palette[i].Blue);
11283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
11293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
11303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              else
11313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
11323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if (bpp < 24)
1133eaedf06777741da32408da72c1e512975c600c48cristy                    if ( (image->colors < (one << bpp)) && (bpp != 24) )
1134101ab708b0574518ac5715da4d3915400e9df79acristy                      image->colormap=(PixelInfo *) ResizeQuantumMemory(
1135eaedf06777741da32408da72c1e512975c600c48cristy                        image->colormap,(size_t) (one << bpp),
11363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                        sizeof(*image->colormap));
11373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
11383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
11393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if (bpp == 1)
11403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
11413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if(image->colormap[0].red==0 &&
11423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     image->colormap[0].green==0 &&
11433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     image->colormap[0].blue==0 &&
11443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     image->colormap[1].red==0 &&
11453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     image->colormap[1].green==0 &&
11463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     image->colormap[1].blue==0)
11473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {  /* fix crippled monochrome palette */
11483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      image->colormap[1].red =
11493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                        image->colormap[1].green =
11503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                        image->colormap[1].blue = QuantumRange;
11513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
11523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
11533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1154c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy              if(UnpackWPGRaster(image,bpp,exception) < 0)
11553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                /* The raster cannot be unpacked */
11563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
11573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                DecompressionFailed:
11583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  ThrowReaderException(CoderError,"UnableToDecompressImage");
11593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
11603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
11613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(Rec.RecType==0x14 && BitmapHeader2.RotAngle!=0 && !image_info->ping)
11623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
11633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  /* flop command */
11643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if(BitmapHeader2.RotAngle & 0x8000)
11653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {
116674f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      Image
116774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        *flop_image;
116874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy
116974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      flop_image = FlopImage(image, exception);
117074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      if (flop_image != (Image *) NULL) {
117174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        DuplicateBlob(flop_image,image);
117274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        (void) RemoveLastImageFromList(&image);
117374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        AppendImageToList(&image,flop_image);
117474f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      }
11753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
11763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  /* flip command */
11773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if(BitmapHeader2.RotAngle & 0x2000)
11783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {
117974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      Image
118074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        *flip_image;
118174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy
118274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      flip_image = FlipImage(image, exception);
118374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      if (flip_image != (Image *) NULL) {
118474f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        DuplicateBlob(flip_image,image);
118574f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        (void) RemoveLastImageFromList(&image);
118674f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        AppendImageToList(&image,flip_image);
118774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      }
11883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
11893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
11903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      /* rotate command */
11913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if(BitmapHeader2.RotAngle & 0x0FFF)
11923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    {
119374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      Image
119474f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        *rotate_image;
119574f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy
119674f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      rotate_image=RotateImage(image,(BitmapHeader2.RotAngle &
119774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        0x0FFF), exception);
119874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      if (rotate_image != (Image *) NULL) {
119974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        DuplicateBlob(rotate_image,image);
120074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        (void) RemoveLastImageFromList(&image);
120174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                        AppendImageToList(&image,rotate_image);
120274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                      }
12033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    }
12043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
12053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              /* Allocate next image structure. */
12079950d57e1124b73f684fb5946e206994cefda628cristy              AcquireNextImage(image_info,image,exception);
12083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->depth=8;
12093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if (image->next == (Image *) NULL)
12103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                goto Finish;
12113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image=SyncNextImageInList(image);
12123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->columns=image->rows=0;
12133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->colors=0;
12143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
12153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x1B:  /* Postscript l2 */
12173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(Rec.RecordLength>0x3C)
12183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                image=ExtractPostscript(image,image_info,
12193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  TellBlob(image)+0x3C,   /* skip PS l2 header in the wpg */
1220bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy                  (ssize_t) Rec.RecordLength-0x3C,exception);
12213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
12223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
12233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
12243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      break;
12253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    case 2:  /* WPG level 2 */
12273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      (void) memset(CTM,0,sizeof(CTM));
12283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      StartWPG.PosSizePrecision = 0;
12293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      while(!EOFBlob(image)) /* object parser loop */
12303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        {
12313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
12323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(EOFBlob(image))
12333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
12343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rec2.Class=(i=ReadBlobByte(image));
12363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(i==EOF)
12373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
12383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rec2.RecType=(i=ReadBlobByte(image));
12393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(i==EOF)
12403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
12413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rd_WP_DWORD(image,&Rec2.Extension);
12423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Rd_WP_DWORD(image,&Rec2.RecordLength);
12433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          if(EOFBlob(image))
12443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            break;
12453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          Header.DataOffset=TellBlob(image)+Rec2.RecordLength;
12473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          switch(Rec2.RecType)
12493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            {
12503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      case 1:
12513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              StartWPG.HorizontalUnits=ReadBlobLSBShort(image);
12523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              StartWPG.VerticalUnits=ReadBlobLSBShort(image);
12533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              StartWPG.PosSizePrecision=ReadBlobByte(image);
12543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
12553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x0C:    /* Color palette */
12563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              WPG_Palette.StartIndex=ReadBlobLSBShort(image);
12573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
12583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12593ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->colors=WPG_Palette.NumOfEntries;
1260018f07f7333b25743d0afff892450cebdb905c1acristy              if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
12613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                ThrowReaderException(ResourceLimitError,
12623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  "MemoryAllocationFailed");
12633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              for (i=WPG_Palette.StartIndex;
12643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                   i < (int)WPG_Palette.NumOfEntries; i++)
12653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
12663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].red=ScaleCharToQuantum((char)
12673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
12683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].green=ScaleCharToQuantum((char)
12693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
12703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  image->colormap[i].blue=ScaleCharToQuantum((char)
12713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    ReadBlobByte(image));
12723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  (void) ReadBlobByte(image);   /*Opacity??*/
12733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
12743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
12753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x0E:
12763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              Bitmap2Header1.Width=ReadBlobLSBShort(image);
127774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              Bitmap2Header1.Height=ReadBlobLSBShort(image);
127874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              if ((Bitmap2Header1.Width == 0) || (Bitmap2Header1.Height == 0))
127974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                ThrowReaderException(CorruptImageError,"ImproperImageHeader");
12803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              Bitmap2Header1.Depth=ReadBlobByte(image);
12813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              Bitmap2Header1.Compression=ReadBlobByte(image);
12823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
12833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(Bitmap2Header1.Compression > 1)
12843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                continue; /*Unknown compression method */
12853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              switch(Bitmap2Header1.Depth)
12863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
12873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 1:
12883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  bpp=1;
12893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
12903ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 2:
12913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  bpp=2;
12923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
12933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 3:
12943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  bpp=4;
12953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
12963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 4:
12973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  bpp=8;
12983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
12993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 8:
13003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  bpp=24;
13013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  break;
13023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                default:
13033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  continue;  /*Ignore raster with unknown depth*/
13043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
13053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->columns=Bitmap2Header1.Width;
130674f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              image->rows=Bitmap2Header1.Height;
13073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if ((image->colors == 0) && (bpp != 24))
13093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
13100b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy                  size_t
13110b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy                    one;
13120b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy
13130b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy                  one=1;
13140b29b25525c4612c77b1b3c8abcc40685d0aa33fcristy                  image->colors=one << bpp;
1315018f07f7333b25743d0afff892450cebdb905c1acristy                  if (!AcquireImageColormap(image,image->colors,exception))
13163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    goto NoMemory;
13173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
13183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              else
13193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
13203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  if(bpp < 24)
1321eaedf06777741da32408da72c1e512975c600c48cristy                    if( image->colors<(one << bpp) && bpp!=24 )
1322101ab708b0574518ac5715da4d3915400e9df79acristy                      image->colormap=(PixelInfo *) ResizeQuantumMemory(
1323eaedf06777741da32408da72c1e512975c600c48cristy                       image->colormap,(size_t) (one << bpp),
13243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                       sizeof(*image->colormap));
13253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
13263ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13273ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              switch(Bitmap2Header1.Compression)
13293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                {
13303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 0:    /*Uncompressed raster*/
13313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  {
1332bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy                    ldblk=(ssize_t) ((bpp*image->columns+7)/8);
13333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t)
13343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      ldblk,sizeof(*BImgBuff));
13353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    if (BImgBuff == (unsigned char *) NULL)
13363ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      goto NoMemory;
13373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1338bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy                    for(i=0; i< (ssize_t) image->rows; i++)
13393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      {
13403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                        (void) ReadBlob(image,ldblk,BImgBuff);
1341c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                        InsertRow(image,BImgBuff,i,bpp,exception);
13423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      }
13433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    if(BImgBuff)
13453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);;
13463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    break;
13473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  }
13483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                case 1:    /*RLE for WPG2 */
13493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  {
1350c82a27bb8e3138ff9bbf0f696663bdf3e704cedecristy                    if( UnpackWPG2Raster(image,bpp,exception) < 0)
13513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                      goto DecompressionFailed;
13523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                    break;
13533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  }
13543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
13553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(CTM[0][0]<0 && !image_info->ping)
135774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                {    /*?? RotAngle=360-RotAngle;*/
135874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                  Image
135974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    *flop_image;
136074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy
136174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                  flop_image = FlopImage(image, exception);
136274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                  if (flop_image != (Image *) NULL) {
136374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    DuplicateBlob(flop_image,image);
136474f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    (void) RemoveLastImageFromList(&image);
136574f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    AppendImageToList(&image,flop_image);
136674f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                  }
13673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  /* Try to change CTM according to Flip - I am not sure, must be checked.
13683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,0)=-1;      Tx(1,0)=0;   Tx(2,0)=0;
13693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,1)= 0;      Tx(1,1)=1;   Tx(2,1)=0;
13703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,2)=(WPG._2Rect.X_ur+WPG._2Rect.X_ll);
13713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(1,2)=0;   Tx(2,2)=1; */
13723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                }
13733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(CTM[1][1]<0 && !image_info->ping)
137474f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                {    /*?? RotAngle=360-RotAngle;*/
137574f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                  Image
137674f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    *flip_image;
137774f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy
137874f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                   flip_image = FlipImage(image, exception);
137974f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                   if (flip_image != (Image *) NULL) {
138074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                     DuplicateBlob(flip_image,image);
138174f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                     (void) RemoveLastImageFromList(&image);
138274f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                     AppendImageToList(&image,flip_image);
138374f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy                    }
13843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  /* Try to change CTM according to Flip - I am not sure, must be checked.
13853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     float_matrix Tx(3,3);
13863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,0)= 1;   Tx(1,0)= 0;   Tx(2,0)=0;
13873ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,1)= 0;   Tx(1,1)=-1;   Tx(2,1)=0;
13883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(0,2)= 0;   Tx(1,2)=(WPG._2Rect.Y_ur+WPG._2Rect.Y_ll);
13893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                     Tx(2,2)=1; */
139074f7d4186c9e083042541fe6c6fd6d2e6b8d73aecristy              }
13913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
13933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              /* Allocate next image structure. */
13949950d57e1124b73f684fb5946e206994cefda628cristy              AcquireNextImage(image_info,image,exception);
13953ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->depth=8;
13963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if (image->next == (Image *) NULL)
13973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                goto Finish;
13983ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image=SyncNextImageInList(image);
13993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->columns=image->rows=1;
14003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              image->colors=0;
14013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
14023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            case 0x12:  /* Postscript WPG2*/
14043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        i=ReadBlobLSBShort(image);
14053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              if(Rec2.RecordLength > (unsigned int) i)
14063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                image=ExtractPostscript(image,image_info,
14073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy                  TellBlob(image)+i,    /*skip PS header in the wpg2*/
1408bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy                  (ssize_t) (Rec2.RecordLength-i-2),exception);
14093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
14103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      case 0x1B:          /*bitmap rectangle*/
14123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              WPG2Flags = LoadWPG2Flags(image,StartWPG.PosSizePrecision,NULL,&CTM);
1413da16f16767eb31921af855f17bda465fffc4e000cristy              (void) WPG2Flags;
14143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy              break;
14153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy            }
14163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
14173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      break;
14193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    default:
14213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
14223ed852eea50f9d4cd633efb8c2b054b8e33c253cristy         ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
14233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
14243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy   }
1425acabb847a592ca5e430c1c0949d03acfc0b78bb9cristy  status=SetImageExtent(image,image->columns,image->rows,exception);
1426acabb847a592ca5e430c1c0949d03acfc0b78bb9cristy  if (status == MagickFalse)
1427acabb847a592ca5e430c1c0949d03acfc0b78bb9cristy    return(DestroyImageList(image));
14283ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14293ed852eea50f9d4cd633efb8c2b054b8e33c253cristy Finish:
14303ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) CloseBlob(image);
14313ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14323ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  {
14333ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    Image
14343ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      *p;
14353ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
1436bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy    ssize_t
14373ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      scene=0;
14383ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14393ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /*
14403ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      Rewind list, removing any empty images while rewinding.
14413ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    */
14423ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    p=image;
14433ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    image=NULL;
14443ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    while (p != (Image *) NULL)
14453ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      {
14463ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        Image *tmp=p;
14473ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        if ((p->rows == 0) || (p->columns == 0)) {
14483ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          p=p->previous;
14493ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          DeleteImageFromList(&tmp);
14503ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        } else {
14513ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          image=p;
14523ed852eea50f9d4cd633efb8c2b054b8e33c253cristy          p=p->previous;
14533ed852eea50f9d4cd633efb8c2b054b8e33c253cristy        }
14543ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      }
14553ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    /*
14563ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      Fix scene numbers.
14573ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    */
14583ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    for (p=image; p != (Image *) NULL; p=p->next)
1459bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy      p->scene=(size_t) scene++;
14603ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  }
14613ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  if (image == (Image *) NULL)
14623ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    ThrowReaderException(CorruptImageError,
14633ed852eea50f9d4cd633efb8c2b054b8e33c253cristy      "ImageFileDoesNotContainAnyImageData");
14643ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(image);
14653ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
14663ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
14673ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
14683ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14693ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14703ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14713ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14723ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   R e g i s t e r W P G I m a g e                                           %
14733ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14743ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14753ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
14763ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14773ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
14783ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  Method RegisterWPGImage adds attributes for the WPG image format to
14793ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  the list of supported formats.  The attributes include the image format
14803ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  tag, a method to read and/or write the format, whether the format
14813ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  supports the saving of more than one frame to the same file or blob,
14823ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  whether the format supports native in-memory I/O, and a brief
14833ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  description of the format.
14843ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
14853ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the RegisterWPGImage method is:
14863ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
1487bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristy%      size_t RegisterWPGImage(void)
14883ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
14893ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
1490bb50337b2a8a16ca7e903cc04ab195ff0fd47ae6cristyModuleExport size_t RegisterWPGImage(void)
14913ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
14923ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  MagickInfo
14933ed852eea50f9d4cd633efb8c2b054b8e33c253cristy    *entry;
14943ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
149506b627a07ff44e1ff93ef1288c9f428066ded10ddirk  entry=AcquireMagickInfo("WPG","WPG","Word Perfect Graphics");
14963ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->decoder=(DecodeImageHandler *) ReadWPGImage;
14973ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  entry->magick=(IsImageFormatHandler *) IsWPG;
149808e9a113db499034abb5ad8d59b42f8eca3c641cdirk  entry->flags|=CoderSeekableStreamFlag;
14993ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) RegisterMagickInfo(entry);
15003ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  return(MagickImageCoderSignature);
15013ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
15023ed852eea50f9d4cd633efb8c2b054b8e33c253cristy
15033ed852eea50f9d4cd633efb8c2b054b8e33c253cristy/*
15043ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15053ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15063ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15073ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15083ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%   U n r e g i s t e r W P G I m a g e                                       %
15093ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15103ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15113ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%                                                                             %
15123ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
15133ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
15143ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  Method UnregisterWPGImage removes format registrations made by the
15153ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  WPG module from the list of supported formats.
15163ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
15173ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%  The format of the UnregisterWPGImage method is:
15183ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
15193ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%      UnregisterWPGImage(void)
15203ed852eea50f9d4cd633efb8c2b054b8e33c253cristy%
15213ed852eea50f9d4cd633efb8c2b054b8e33c253cristy*/
15223ed852eea50f9d4cd633efb8c2b054b8e33c253cristyModuleExport void UnregisterWPGImage(void)
15233ed852eea50f9d4cd633efb8c2b054b8e33c253cristy{
15243ed852eea50f9d4cd633efb8c2b054b8e33c253cristy  (void) UnregisterMagickInfo("WPG");
15253ed852eea50f9d4cd633efb8c2b054b8e33c253cristy}
1526