blob: 04ff0c2b4f58b3c73972d4b6864e8aac096937c1 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% W W PPPP GGGG %
6% W W P P G %
7% W W W PPPP G GGG %
8% WW WW P G G %
9% W W P GGG %
10% %
11% %
12% Read WordPerfect Image Format %
13% %
14% Software Design %
15% Jaroslav Fojtik %
16% June 2000 %
17% %
18% %
cristy1454be72011-12-19 01:52:48 +000019% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000020% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
25% http://www.imagemagick.org/script/license.php %
26% %
27% Unless required by applicable law or agreed to in writing, software %
28% distributed under the License is distributed on an "AS IS" BASIS, %
29% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30% See the License for the specific language governing permissions and %
31% limitations under the License. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34%
35%
36*/
37
38/*
39 Include declarations.
40*/
cristy4c08aed2011-07-01 19:47:50 +000041#include "MagickCore/studio.h"
42#include "MagickCore/blob.h"
43#include "MagickCore/blob-private.h"
44#include "MagickCore/color-private.h"
45#include "MagickCore/colormap.h"
46#include "MagickCore/colormap-private.h"
47#include "MagickCore/constitute.h"
48#include "MagickCore/exception.h"
49#include "MagickCore/exception-private.h"
50#include "MagickCore/cache.h"
cristyc53413d2011-11-17 13:04:26 +000051#include "MagickCore/distort.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/image.h"
53#include "MagickCore/image-private.h"
54#include "MagickCore/list.h"
55#include "MagickCore/magic.h"
56#include "MagickCore/magick.h"
57#include "MagickCore/memory_.h"
58#include "MagickCore/resource_.h"
59#include "MagickCore/pixel-accessor.h"
60#include "MagickCore/quantum-private.h"
cristy4c08aed2011-07-01 19:47:50 +000061#include "MagickCore/static.h"
62#include "MagickCore/string_.h"
63#include "MagickCore/module.h"
64#include "MagickCore/transform.h"
65#include "MagickCore/utility.h"
cristy18c6c272011-09-23 14:40:37 +000066#include "MagickCore/utility-private.h"
cristy3ed852e2009-09-05 21:47:34 +000067
68typedef struct
69 {
70 unsigned char Red;
71 unsigned char Blue;
72 unsigned char Green;
73 } RGB_Record;
74
75/* Default palette for WPG level 1 */
76const RGB_Record WPG1_Palette[256]={
77{ 0, 0, 0}, { 0, 0,168},
78{ 0,168, 0}, { 0,168,168},
79{168, 0, 0}, {168, 0,168},
80{168, 84, 0}, {168,168,168},
81{ 84, 84, 84}, { 84, 84,252},
82{ 84,252, 84}, { 84,252,252},
83{252, 84, 84}, {252, 84,252},
84{252,252, 84}, {252,252,252}, /*16*/
85{ 0, 0, 0}, { 20, 20, 20},
86{ 32, 32, 32}, { 44, 44, 44},
87{ 56, 56, 56}, { 68, 68, 68},
88{ 80, 80, 80}, { 96, 96, 96},
89{112,112,112}, {128,128,128},
90{144,144,144}, {160,160,160},
91{180,180,180}, {200,200,200},
92{224,224,224}, {252,252,252}, /*32*/
93{ 0, 0,252}, { 64, 0,252},
94{124, 0,252}, {188, 0,252},
95{252, 0,252}, {252, 0,188},
96{252, 0,124}, {252, 0, 64},
97{252, 0, 0}, {252, 64, 0},
98{252,124, 0}, {252,188, 0},
99{252,252, 0}, {188,252, 0},
100{124,252, 0}, { 64,252, 0}, /*48*/
101{ 0,252, 0}, { 0,252, 64},
102{ 0,252,124}, { 0,252,188},
103{ 0,252,252}, { 0,188,252},
104{ 0,124,252}, { 0, 64,252},
105{124,124,252}, {156,124,252},
106{188,124,252}, {220,124,252},
107{252,124,252}, {252,124,220},
108{252,124,188}, {252,124,156}, /*64*/
109{252,124,124}, {252,156,124},
110{252,188,124}, {252,220,124},
111{252,252,124}, {220,252,124},
112{188,252,124}, {156,252,124},
113{124,252,124}, {124,252,156},
114{124,252,188}, {124,252,220},
115{124,252,252}, {124,220,252},
116{124,188,252}, {124,156,252}, /*80*/
117{180,180,252}, {196,180,252},
118{216,180,252}, {232,180,252},
119{252,180,252}, {252,180,232},
120{252,180,216}, {252,180,196},
121{252,180,180}, {252,196,180},
122{252,216,180}, {252,232,180},
123{252,252,180}, {232,252,180},
124{216,252,180}, {196,252,180}, /*96*/
125{180,220,180}, {180,252,196},
126{180,252,216}, {180,252,232},
127{180,252,252}, {180,232,252},
128{180,216,252}, {180,196,252},
129{0,0,112}, {28,0,112},
130{56,0,112}, {84,0,112},
131{112,0,112}, {112,0,84},
132{112,0,56}, {112,0,28}, /*112*/
133{112,0,0}, {112,28,0},
134{112,56,0}, {112,84,0},
135{112,112,0}, {84,112,0},
136{56,112,0}, {28,112,0},
137{0,112,0}, {0,112,28},
138{0,112,56}, {0,112,84},
139{0,112,112}, {0,84,112},
140{0,56,112}, {0,28,112}, /*128*/
141{56,56,112}, {68,56,112},
142{84,56,112}, {96,56,112},
143{112,56,112}, {112,56,96},
144{112,56,84}, {112,56,68},
145{112,56,56}, {112,68,56},
146{112,84,56}, {112,96,56},
147{112,112,56}, {96,112,56},
148{84,112,56}, {68,112,56}, /*144*/
149{56,112,56}, {56,112,69},
150{56,112,84}, {56,112,96},
151{56,112,112}, {56,96,112},
152{56,84,112}, {56,68,112},
153{80,80,112}, {88,80,112},
154{96,80,112}, {104,80,112},
155{112,80,112}, {112,80,104},
156{112,80,96}, {112,80,88}, /*160*/
157{112,80,80}, {112,88,80},
158{112,96,80}, {112,104,80},
159{112,112,80}, {104,112,80},
160{96,112,80}, {88,112,80},
161{80,112,80}, {80,112,88},
162{80,112,96}, {80,112,104},
163{80,112,112}, {80,114,112},
164{80,96,112}, {80,88,112}, /*176*/
165{0,0,64}, {16,0,64},
166{32,0,64}, {48,0,64},
167{64,0,64}, {64,0,48},
168{64,0,32}, {64,0,16},
169{64,0,0}, {64,16,0},
170{64,32,0}, {64,48,0},
171{64,64,0}, {48,64,0},
172{32,64,0}, {16,64,0}, /*192*/
173{0,64,0}, {0,64,16},
174{0,64,32}, {0,64,48},
175{0,64,64}, {0,48,64},
176{0,32,64}, {0,16,64},
177{32,32,64}, {40,32,64},
178{48,32,64}, {56,32,64},
179{64,32,64}, {64,32,56},
180{64,32,48}, {64,32,40}, /*208*/
181{64,32,32}, {64,40,32},
182{64,48,32}, {64,56,32},
183{64,64,32}, {56,64,32},
184{48,64,32}, {40,64,32},
185{32,64,32}, {32,64,40},
186{32,64,48}, {32,64,56},
187{32,64,64}, {32,56,64},
188{32,48,64}, {32,40,64}, /*224*/
189{44,44,64}, {48,44,64},
190{52,44,64}, {60,44,64},
191{64,44,64}, {64,44,60},
192{64,44,52}, {64,44,48},
193{64,44,44}, {64,48,44},
194{64,52,44}, {64,60,44},
195{64,64,44}, {60,64,44},
196{52,64,44}, {48,64,44}, /*240*/
197{44,64,44}, {44,64,48},
198{44,64,52}, {44,64,60},
199{44,64,64}, {44,60,64},
200{44,55,64}, {44,48,64},
201{0,0,0}, {0,0,0},
202{0,0,0}, {0,0,0},
203{0,0,0}, {0,0,0},
204{0,0,0}, {0,0,0} /*256*/
205};
206
207/*
208%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209% %
210% %
211% %
212% I s W P G %
213% %
214% %
215% %
216%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217%
218% IsWPG() returns True if the image format type, identified by the magick
219% string, is WPG.
220%
221% The format of the IsWPG method is:
222%
223% unsigned int IsWPG(const unsigned char *magick,const size_t length)
224%
225% A description of each parameter follows:
226%
227% o status: Method IsWPG returns True if the image format type is WPG.
228%
229% o magick: compare image format pattern against these bytes.
230%
231% o length: Specifies the length of the magick string.
232%
233*/
234static unsigned int IsWPG(const unsigned char *magick,const size_t length)
235{
236 if (length < 4)
237 return(MagickFalse);
238 if (memcmp(magick,"\377WPC",4) == 0)
239 return(MagickTrue);
240 return(MagickFalse);
241}
242
243
cristybb503372010-05-27 20:51:26 +0000244static void Rd_WP_DWORD(Image *image,size_t *d)
cristy3ed852e2009-09-05 21:47:34 +0000245{
246 unsigned char
247 b;
248
249 b=ReadBlobByte(image);
250 *d=b;
251 if (b < 0xFFU)
252 return;
253 b=ReadBlobByte(image);
cristybb503372010-05-27 20:51:26 +0000254 *d=(size_t) b;
cristy3ed852e2009-09-05 21:47:34 +0000255 b=ReadBlobByte(image);
cristybb503372010-05-27 20:51:26 +0000256 *d+=(size_t) b*256l;
cristy3ed852e2009-09-05 21:47:34 +0000257 if (*d < 0x8000)
258 return;
259 *d=(*d & 0x7FFF) << 16;
260 b=ReadBlobByte(image);
cristybb503372010-05-27 20:51:26 +0000261 *d+=(size_t) b;
cristy3ed852e2009-09-05 21:47:34 +0000262 b=ReadBlobByte(image);
cristybb503372010-05-27 20:51:26 +0000263 *d+=(size_t) b*256l;
cristy3ed852e2009-09-05 21:47:34 +0000264 return;
265}
266
cristyc82a27b2011-10-21 01:07:16 +0000267static void InsertRow(Image *image,unsigned char *p,ssize_t y,int bpp,
268 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000269{
cristy3ed852e2009-09-05 21:47:34 +0000270 int
271 bit;
272
cristy4c08aed2011-07-01 19:47:50 +0000273 Quantum
cristy3ed852e2009-09-05 21:47:34 +0000274 index;
275
cristy4c08aed2011-07-01 19:47:50 +0000276 register Quantum
277 *q;
278
279 ssize_t
280 x;
cristy3ed852e2009-09-05 21:47:34 +0000281
cristy3ed852e2009-09-05 21:47:34 +0000282 switch (bpp)
283 {
284 case 1: /* Convert bitmap scanline. */
285 {
286 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000287 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000288 break;
cristybb503372010-05-27 20:51:26 +0000289 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
cristy4c08aed2011-07-01 19:47:50 +0000290 {
291 for (bit=0; bit < 8; bit++)
cristy3ed852e2009-09-05 21:47:34 +0000292 {
cristy4c08aed2011-07-01 19:47:50 +0000293 index=((*p) & (0x80 >> bit) ? 0x01 : 0x00);
294 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000295 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristyed231572011-07-14 02:18:59 +0000296 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000297 }
cristy4c08aed2011-07-01 19:47:50 +0000298 p++;
299 }
cristy3ed852e2009-09-05 21:47:34 +0000300 if ((image->columns % 8) != 0)
301 {
cristybb503372010-05-27 20:51:26 +0000302 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
cristy4c08aed2011-07-01 19:47:50 +0000303 {
304 index=((*p) & (0x80 >> bit) ? 0x01 : 0x00);
305 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000306 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristyed231572011-07-14 02:18:59 +0000307 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +0000308 }
cristy3ed852e2009-09-05 21:47:34 +0000309 p++;
310 }
311 if (!SyncAuthenticPixels(image,exception))
312 break;
313 break;
314 }
315 case 2: /* Convert PseudoColor scanline. */
316 {
317 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000318 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000319 break;
cristybb503372010-05-27 20:51:26 +0000320 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
cristy3ed852e2009-09-05 21:47:34 +0000321 {
cristyc82a27b2011-10-21 01:07:16 +0000322 index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
cristy4c08aed2011-07-01 19:47:50 +0000323 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000324 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristyed231572011-07-14 02:18:59 +0000325 q+=GetPixelChannels(image);
cristyc82a27b2011-10-21 01:07:16 +0000326 index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
cristy4c08aed2011-07-01 19:47:50 +0000327 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000328 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristyed231572011-07-14 02:18:59 +0000329 q+=GetPixelChannels(image);
cristyc82a27b2011-10-21 01:07:16 +0000330 index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,exception);
cristy4c08aed2011-07-01 19:47:50 +0000331 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000332 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristyed231572011-07-14 02:18:59 +0000333 q+=GetPixelChannels(image);
cristyc82a27b2011-10-21 01:07:16 +0000334 index=ConstrainColormapIndex(image,(*p) & 0x3,exception);
cristy4c08aed2011-07-01 19:47:50 +0000335 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000336 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristy3ed852e2009-09-05 21:47:34 +0000337 p++;
cristyed231572011-07-14 02:18:59 +0000338 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000339 }
340 if ((image->columns % 4) != 0)
341 {
cristyc82a27b2011-10-21 01:07:16 +0000342 index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
cristy4c08aed2011-07-01 19:47:50 +0000343 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000344 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristyed231572011-07-14 02:18:59 +0000345 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000346 if ((image->columns % 4) >= 1)
347
348 {
cristyc82a27b2011-10-21 01:07:16 +0000349 index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
cristy4c08aed2011-07-01 19:47:50 +0000350 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000351 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristyed231572011-07-14 02:18:59 +0000352 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000353 if ((image->columns % 4) >= 2)
354
355 {
cristyc82a27b2011-10-21 01:07:16 +0000356 index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,
357 exception);
cristy4c08aed2011-07-01 19:47:50 +0000358 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000359 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristyed231572011-07-14 02:18:59 +0000360 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000361 }
362 }
363 p++;
364 }
365 if (SyncAuthenticPixels(image,exception) == MagickFalse)
366 break;
367 break;
368 }
369
370 case 4: /* Convert PseudoColor scanline. */
371 {
372 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000373 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000374 break;
cristybb503372010-05-27 20:51:26 +0000375 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
cristy3ed852e2009-09-05 21:47:34 +0000376 {
cristyc82a27b2011-10-21 01:07:16 +0000377 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
cristy4c08aed2011-07-01 19:47:50 +0000378 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000379 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristyed231572011-07-14 02:18:59 +0000380 q+=GetPixelChannels(image);
cristyc82a27b2011-10-21 01:07:16 +0000381 index=ConstrainColormapIndex(image,(*p) & 0x0f,exception);
cristy4c08aed2011-07-01 19:47:50 +0000382 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000383 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristy3ed852e2009-09-05 21:47:34 +0000384 p++;
cristyed231572011-07-14 02:18:59 +0000385 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000386 }
387 if ((image->columns % 2) != 0)
388 {
cristyc82a27b2011-10-21 01:07:16 +0000389 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
cristy4c08aed2011-07-01 19:47:50 +0000390 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000391 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristy3ed852e2009-09-05 21:47:34 +0000392 p++;
cristyed231572011-07-14 02:18:59 +0000393 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000394 }
395 if (SyncAuthenticPixels(image,exception) == MagickFalse)
396 break;
397 break;
398 }
399 case 8: /* Convert PseudoColor scanline. */
400 {
401 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000402 if (q == (Quantum *) NULL) break;
cristy3ed852e2009-09-05 21:47:34 +0000403
cristybb503372010-05-27 20:51:26 +0000404 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000405 {
cristyc82a27b2011-10-21 01:07:16 +0000406 index=ConstrainColormapIndex(image,*p,exception);
cristy4c08aed2011-07-01 19:47:50 +0000407 SetPixelIndex(image,index,q);
cristy803640d2011-11-17 02:11:32 +0000408 SetPixelInfoPixel(image,image->colormap+(ssize_t) index,q);
cristy3ed852e2009-09-05 21:47:34 +0000409 p++;
cristyed231572011-07-14 02:18:59 +0000410 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000411 }
412 if (SyncAuthenticPixels(image,exception) == MagickFalse)
413 break;
414 }
415 break;
416
417 case 24: /* Convert DirectColor scanline. */
418 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +0000419 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000420 break;
cristybb503372010-05-27 20:51:26 +0000421 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +0000422 {
cristy4c08aed2011-07-01 19:47:50 +0000423 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
424 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
425 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
cristyed231572011-07-14 02:18:59 +0000426 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000427 }
428 if (!SyncAuthenticPixels(image,exception))
429 break;
430 break;
431 }
432}
433
434
435/* Helper for WPG1 raster reader. */
436#define InsertByte(b) \
437{ \
438 BImgBuff[x]=b; \
439 x++; \
cristybb503372010-05-27 20:51:26 +0000440 if((ssize_t) x>=ldblk) \
cristy3ed852e2009-09-05 21:47:34 +0000441 { \
cristyc82a27b2011-10-21 01:07:16 +0000442 InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception); \
cristy3ed852e2009-09-05 21:47:34 +0000443 x=0; \
444 y++; \
445 } \
446}
447/* WPG1 raster reader. */
cristyc82a27b2011-10-21 01:07:16 +0000448static int UnpackWPGRaster(Image *image,int bpp,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000449{
450 int
451 x,
452 y,
453 i;
454
455 unsigned char
456 bbuf,
457 *BImgBuff,
458 RunCount;
459
cristybb503372010-05-27 20:51:26 +0000460 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000461 ldblk;
462
463 x=0;
464 y=0;
465
cristybb503372010-05-27 20:51:26 +0000466 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
cristy3ed852e2009-09-05 21:47:34 +0000467 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
468 sizeof(*BImgBuff));
469 if(BImgBuff==NULL) return(-2);
470
cristybb503372010-05-27 20:51:26 +0000471 while(y<(ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +0000472 {
473 bbuf=ReadBlobByte(image);
474
475 RunCount=bbuf & 0x7F;
476 if(bbuf & 0x80)
477 {
478 if(RunCount) /* repeat next byte runcount * */
479 {
480 bbuf=ReadBlobByte(image);
481 for(i=0;i<(int) RunCount;i++) InsertByte(bbuf);
482 }
483 else { /* read next byte as RunCount; repeat 0xFF runcount* */
484 RunCount=ReadBlobByte(image);
485 for(i=0;i<(int) RunCount;i++) InsertByte(0xFF);
486 }
487 }
488 else {
489 if(RunCount) /* next runcount byte are readed directly */
490 {
491 for(i=0;i < (int) RunCount;i++)
492 {
493 bbuf=ReadBlobByte(image);
494 InsertByte(bbuf);
495 }
496 }
497 else { /* repeat previous line runcount* */
498 RunCount=ReadBlobByte(image);
499 if(x) { /* attempt to duplicate row from x position: */
500 /* I do not know what to do here */
501 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
502 return(-3);
503 }
504 for(i=0;i < (int) RunCount;i++)
505 {
506 x=0;
507 y++; /* Here I need to duplicate previous row RUNCOUNT* */
508 if(y<2) continue;
cristybb503372010-05-27 20:51:26 +0000509 if(y>(ssize_t) image->rows)
cristy3ed852e2009-09-05 21:47:34 +0000510 {
511 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
512 return(-4);
513 }
cristyc82a27b2011-10-21 01:07:16 +0000514 InsertRow(image,BImgBuff,y-1,bpp,exception);
cristy3ed852e2009-09-05 21:47:34 +0000515 }
516 }
517 }
518 }
519 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
520 return(0);
521}
522
523
524/* Helper for WPG2 reader. */
525#define InsertByte6(b) \
526{ \
527 if(XorMe)\
528 BImgBuff[x] = (unsigned char)~b;\
529 else\
530 BImgBuff[x] = b;\
531 x++; \
cristybb503372010-05-27 20:51:26 +0000532 if((ssize_t) x >= ldblk) \
cristy3ed852e2009-09-05 21:47:34 +0000533 { \
cristyc82a27b2011-10-21 01:07:16 +0000534 InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception); \
cristy3ed852e2009-09-05 21:47:34 +0000535 x=0; \
536 y++; \
537 } \
538}
539/* WPG2 raster reader. */
cristyc82a27b2011-10-21 01:07:16 +0000540static int UnpackWPG2Raster(Image *image,int bpp,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000541{
cristy6a1c5a92011-04-28 01:57:04 +0000542 size_t
543 x,
544 y;
545
546 ssize_t
547 ldblk;
548
549 int XorMe = 0;
550
cristy3ed852e2009-09-05 21:47:34 +0000551 unsigned int
552 SampleSize=1;
553
554 unsigned char
555 bbuf,
556 *BImgBuff,
557 RunCount,
558 SampleBuffer[8];
559
cristy3ed852e2009-09-05 21:47:34 +0000560 unsigned int
561 i;
562
cristy3ed852e2009-09-05 21:47:34 +0000563 x=0;
564 y=0;
cristybb503372010-05-27 20:51:26 +0000565 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
cristy3ed852e2009-09-05 21:47:34 +0000566 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
567 sizeof(*BImgBuff));
568 if(BImgBuff==NULL)
569 return(-2);
570
571 while( y< image->rows)
572 {
573 bbuf=ReadBlobByte(image);
574
575 switch(bbuf)
576 {
577 case 0x7D:
578 SampleSize=ReadBlobByte(image); /* DSZ */
579 if(SampleSize>8)
580 return(-2);
581 if(SampleSize<1)
582 return(-2);
583 break;
584 case 0x7E:
cristy1e604812011-05-19 18:07:50 +0000585 (void) FormatLocaleFile(stderr,
586 "\nUnsupported WPG token XOR, please report!");
587 XorMe=!XorMe;
cristy3ed852e2009-09-05 21:47:34 +0000588 break;
589 case 0x7F:
590 RunCount=ReadBlobByte(image); /* BLK */
591 for(i=0; i < SampleSize*(RunCount+1); i++)
592 {
593 InsertByte6(0);
594 }
595 break;
596 case 0xFD:
cristy1e604812011-05-19 18:07:50 +0000597 RunCount=ReadBlobByte(image); /* EXT */
598 for(i=0; i<= RunCount;i++)
cristy3ed852e2009-09-05 21:47:34 +0000599 for(bbuf=0; bbuf < SampleSize; bbuf++)
600 InsertByte6(SampleBuffer[bbuf]);
601 break;
602 case 0xFE:
603 RunCount=ReadBlobByte(image); /* RST */
604 if(x!=0)
605 {
cristyb51dff52011-05-19 16:55:47 +0000606 (void) FormatLocaleFile(stderr,
cristy1e604812011-05-19 18:07:50 +0000607 "\nUnsupported WPG2 unaligned token RST x=%.20g, please report!\n"
608 ,(double) x);
cristy3ed852e2009-09-05 21:47:34 +0000609 return(-3);
610 }
611 {
612 /* duplicate the previous row RunCount x */
613 for(i=0;i<=RunCount;i++)
614 {
cristyc82a27b2011-10-21 01:07:16 +0000615 InsertRow(image,BImgBuff,(ssize_t) (image->rows >= y ? y : image->rows-1),
616 bpp,exception);
cristy3ed852e2009-09-05 21:47:34 +0000617 y++;
618 }
619 }
620 break;
621 case 0xFF:
622 RunCount=ReadBlobByte(image); /* WHT */
623 for(i=0; i < SampleSize*(RunCount+1); i++)
624 {
625 InsertByte6(0xFF);
626 }
627 break;
628 default:
629 RunCount=bbuf & 0x7F;
630
631 if(bbuf & 0x80) /* REP */
632 {
633 for(i=0; i < SampleSize; i++)
634 SampleBuffer[i]=ReadBlobByte(image);
635 for(i=0;i<=RunCount;i++)
636 for(bbuf=0;bbuf<SampleSize;bbuf++)
637 InsertByte6(SampleBuffer[bbuf]);
638 }
639 else { /* NRP */
640 for(i=0; i< SampleSize*(RunCount+1);i++)
641 {
642 bbuf=ReadBlobByte(image);
643 InsertByte6(bbuf);
644 }
645 }
646 }
647 }
648 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
649 return(0);
650}
651
652
653typedef float tCTM[3][3];
654
655static unsigned LoadWPG2Flags(Image *image,char Precision,float *Angle,tCTM *CTM)
656{
657const unsigned char TPR=1,TRN=2,SKW=4,SCL=8,ROT=0x10,OID=0x20,LCK=0x80;
cristybb503372010-05-27 20:51:26 +0000658ssize_t x;
cristy3ed852e2009-09-05 21:47:34 +0000659unsigned DenX;
660unsigned Flags;
661
662 (void) memset(*CTM,0,sizeof(*CTM)); /*CTM.erase();CTM.resize(3,3);*/
663 (*CTM)[0][0]=1;
664 (*CTM)[1][1]=1;
665 (*CTM)[2][2]=1;
666
667 Flags=ReadBlobLSBShort(image);
668 if(Flags & LCK) x=ReadBlobLSBLong(image); /*Edit lock*/
669 if(Flags & OID)
670 {
671 if(Precision==0)
672 {x=ReadBlobLSBShort(image);} /*ObjectID*/
673 else
674 {x=ReadBlobLSBLong(image);} /*ObjectID (Double precision)*/
675 }
676 if(Flags & ROT)
677 {
678 x=ReadBlobLSBLong(image); /*Rot Angle*/
679 if(Angle) *Angle=x/65536.0;
680 }
681 if(Flags & (ROT|SCL))
682 {
683 x=ReadBlobLSBLong(image); /*Sx*cos()*/
684 (*CTM)[0][0] = (float)x/0x10000;
685 x=ReadBlobLSBLong(image); /*Sy*cos()*/
686 (*CTM)[1][1] = (float)x/0x10000;
687 }
688 if(Flags & (ROT|SKW))
689 {
690 x=ReadBlobLSBLong(image); /*Kx*sin()*/
691 (*CTM)[1][0] = (float)x/0x10000;
692 x=ReadBlobLSBLong(image); /*Ky*sin()*/
693 (*CTM)[0][1] = (float)x/0x10000;
694 }
695 if(Flags & TRN)
696 {
697 x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image); /*Tx*/
698 if(x>=0) (*CTM)[0][2] = (float)x+(float)DenX/0x10000;
699 else (*CTM)[0][2] = (float)x-(float)DenX/0x10000;
700 x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image); /*Ty*/
701 (*CTM)[1][2]=(float)x + ((x>=0)?1:-1)*(float)DenX/0x10000;
702 if(x>=0) (*CTM)[1][2] = (float)x+(float)DenX/0x10000;
703 else (*CTM)[1][2] = (float)x-(float)DenX/0x10000;
704 }
705 if(Flags & TPR)
706 {
707 x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image); /*Px*/
708 (*CTM)[2][0] = x + (float)DenX/0x10000;;
709 x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image); /*Py*/
710 (*CTM)[2][1] = x + (float)DenX/0x10000;
711 }
712 return(Flags);
713}
714
715
716static Image *ExtractPostscript(Image *image,const ImageInfo *image_info,
cristybb503372010-05-27 20:51:26 +0000717 MagickOffsetType PS_Offset,ssize_t PS_Size,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000718{
719 char
720 postscript_file[MaxTextExtent];
721
722 const MagicInfo
723 *magic_info;
724
725 FILE
726 *ps_file;
727
728 ImageInfo
729 *clone_info;
730
731 Image
732 *image2;
733
734 unsigned char
735 magick[2*MaxTextExtent];
736
737
738 if ((clone_info=CloneImageInfo(image_info)) == NULL)
739 return(image);
740 clone_info->blob=(void *) NULL;
741 clone_info->length=0;
742
743 /* Obtain temporary file */
744 AcquireUniqueFilename(postscript_file);
cristy18c6c272011-09-23 14:40:37 +0000745 ps_file=fopen_utf8(postscript_file,"wb");
cristy3ed852e2009-09-05 21:47:34 +0000746 if (ps_file == (FILE *) NULL)
747 goto FINISH;
748
749 /* Copy postscript to temporary file */
750 (void) SeekBlob(image,PS_Offset,SEEK_SET);
751 (void) ReadBlob(image, 2*MaxTextExtent, magick);
752
753 (void) SeekBlob(image,PS_Offset,SEEK_SET);
754 while(PS_Size-- > 0)
755 {
756 (void) fputc(ReadBlobByte(image),ps_file);
757 }
758 (void) fclose(ps_file);
759
760 /* Detect file format - Check magic.mgk configuration file. */
761 magic_info=GetMagicInfo(magick,2*MaxTextExtent,exception);
762 if(magic_info == (const MagicInfo *) NULL) goto FINISH_UNL;
763 /* printf("Detected:%s \n",magic_info->name); */
764 if(exception->severity != UndefinedException) goto FINISH_UNL;
765 if(magic_info->name == (char *) NULL) goto FINISH_UNL;
766
767 (void) CopyMagickMemory(clone_info->magick,magic_info->name,MaxTextExtent);
768
769 /* Read nested image */
cristy53a8bc92011-01-10 19:15:17 +0000770 /*FormatString(clone_info->filename,"%s:%s",magic_info->name,postscript_file);*/
cristyb51dff52011-05-19 16:55:47 +0000771 FormatLocaleString(clone_info->filename,MaxTextExtent,"%s",postscript_file);
cristy3ed852e2009-09-05 21:47:34 +0000772 image2=ReadImage(clone_info,exception);
773
774 if (!image2)
775 goto FINISH_UNL;
776
777 /*
778 Replace current image with new image while copying base image
779 attributes.
780 */
781 (void) CopyMagickMemory(image2->filename,image->filename,MaxTextExtent);
782 (void) CopyMagickMemory(image2->magick_filename,image->magick_filename,MaxTextExtent);
783 (void) CopyMagickMemory(image2->magick,image->magick,MaxTextExtent);
784 image2->depth=image->depth;
785 DestroyBlob(image2);
786 image2->blob=ReferenceBlob(image->blob);
787
788 if ((image->rows == 0) || (image->columns == 0))
789 DeleteImageFromList(&image);
790
791 AppendImageToList(&image,image2);
792
793 FINISH_UNL:
794 (void) RelinquishUniqueFileResource(postscript_file);
795 FINISH:
796 DestroyImageInfo(clone_info);
797 return(image);
798}
799
800/*
801%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802% %
803% %
804% %
805% R e a d W P G I m a g e %
806% %
807% %
808% %
809%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
810%
811% Method ReadWPGImage reads an WPG X image file and returns it. It
812% allocates the memory necessary for the new Image structure and returns a
813% pointer to the new image.
814%
815% The format of the ReadWPGImage method is:
816%
817% Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
818%
819% A description of each parameter follows:
820%
821% o image: Method ReadWPGImage returns a pointer to the image after
822% reading. A null image is returned if there is a memory shortage or if
823% the image cannot be read.
824%
825% o image_info: Specifies a pointer to a ImageInfo structure.
826%
827% o exception: return any errors or warnings in this structure.
828%
829*/
830static Image *ReadWPGImage(const ImageInfo *image_info,
831 ExceptionInfo *exception)
832{
833 typedef struct
834 {
cristybb503372010-05-27 20:51:26 +0000835 size_t FileId;
cristy3ed852e2009-09-05 21:47:34 +0000836 MagickOffsetType DataOffset;
837 unsigned int ProductType;
838 unsigned int FileType;
839 unsigned char MajorVersion;
840 unsigned char MinorVersion;
841 unsigned int EncryptKey;
842 unsigned int Reserved;
843 } WPGHeader;
844
845 typedef struct
846 {
847 unsigned char RecType;
cristybb503372010-05-27 20:51:26 +0000848 size_t RecordLength;
cristy3ed852e2009-09-05 21:47:34 +0000849 } WPGRecord;
850
851 typedef struct
852 {
853 unsigned char Class;
854 unsigned char RecType;
cristybb503372010-05-27 20:51:26 +0000855 size_t Extension;
856 size_t RecordLength;
cristy3ed852e2009-09-05 21:47:34 +0000857 } WPG2Record;
858
859 typedef struct
860 {
861 unsigned HorizontalUnits;
862 unsigned VerticalUnits;
863 unsigned char PosSizePrecision;
864 } WPG2Start;
865
866 typedef struct
867 {
868 unsigned int Width;
869 unsigned int Heigth;
870 unsigned int Depth;
871 unsigned int HorzRes;
872 unsigned int VertRes;
873 } WPGBitmapType1;
874
875 typedef struct
876 {
877 unsigned int Width;
878 unsigned int Heigth;
879 unsigned char Depth;
880 unsigned char Compression;
881 } WPG2BitmapType1;
882
883 typedef struct
884 {
885 unsigned int RotAngle;
886 unsigned int LowLeftX;
887 unsigned int LowLeftY;
888 unsigned int UpRightX;
889 unsigned int UpRightY;
890 unsigned int Width;
891 unsigned int Heigth;
892 unsigned int Depth;
893 unsigned int HorzRes;
894 unsigned int VertRes;
895 } WPGBitmapType2;
896
897 typedef struct
898 {
899 unsigned int StartIndex;
900 unsigned int NumOfEntries;
901 } WPGColorMapRec;
902
903 typedef struct {
cristybb503372010-05-27 20:51:26 +0000904 size_t PS_unknown1;
cristy3ed852e2009-09-05 21:47:34 +0000905 unsigned int PS_unknown2;
906 unsigned int PS_unknown3;
907 } WPGPSl1Record;
908
909 Image
910 *image,
911 *rotated_image;
912
913 unsigned int
914 status;
915
916 WPGHeader
917 Header;
918
919 WPGRecord
920 Rec;
921
922 WPG2Record
923 Rec2;
924
925 WPG2Start StartWPG;
926
927 WPGBitmapType1
928 BitmapHeader1;
929
930 WPG2BitmapType1
931 Bitmap2Header1;
932
933 WPGBitmapType2
934 BitmapHeader2;
935
936 WPGColorMapRec
937 WPG_Palette;
938
939 int
940 i,
941 bpp,
942 WPG2Flags;
943
cristybb503372010-05-27 20:51:26 +0000944 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000945 ldblk;
946
cristyeaedf062010-05-29 22:36:02 +0000947 size_t
948 one;
949
cristy3ed852e2009-09-05 21:47:34 +0000950 unsigned char
951 *BImgBuff;
952
953 tCTM CTM; /*current transform matrix*/
954
955 /*
956 Open image file.
957 */
958 assert(image_info != (const ImageInfo *) NULL);
959 assert(image_info->signature == MagickSignature);
960 assert(exception != (ExceptionInfo *) NULL);
961 assert(exception->signature == MagickSignature);
cristyeaedf062010-05-29 22:36:02 +0000962 one=1;
cristy9950d572011-10-01 18:22:35 +0000963 image=AcquireImage(image_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000964 image->depth=8;
965 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
966 if (status == MagickFalse)
967 {
968 image=DestroyImageList(image);
969 return((Image *) NULL);
970 }
971 /*
972 Read WPG image.
973 */
974 Header.FileId=ReadBlobLSBLong(image);
975 Header.DataOffset=(MagickOffsetType) ReadBlobLSBLong(image);
976 Header.ProductType=ReadBlobLSBShort(image);
977 Header.FileType=ReadBlobLSBShort(image);
978 Header.MajorVersion=ReadBlobByte(image);
979 Header.MinorVersion=ReadBlobByte(image);
980 Header.EncryptKey=ReadBlobLSBShort(image);
981 Header.Reserved=ReadBlobLSBShort(image);
982
983 if (Header.FileId!=0x435057FF || (Header.ProductType>>8)!=0x16)
984 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
985 if (Header.EncryptKey!=0)
986 ThrowReaderException(CoderError,"EncryptedWPGImageFileNotSupported");
987
988 image->columns = 1;
989 image->rows = 1;
990 image->colors = 0;
991 bpp=0;
992 BitmapHeader2.RotAngle=0;
993
994 switch(Header.FileType)
995 {
996 case 1: /* WPG level 1 */
997 while(!EOFBlob(image)) /* object parser loop */
998 {
999 (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
1000 if(EOFBlob(image))
1001 break;
1002
1003 Rec.RecType=(i=ReadBlobByte(image));
1004 if(i==EOF)
1005 break;
1006 Rd_WP_DWORD(image,&Rec.RecordLength);
1007 if(EOFBlob(image))
1008 break;
1009
1010 Header.DataOffset=TellBlob(image)+Rec.RecordLength;
1011
1012 switch(Rec.RecType)
1013 {
1014 case 0x0B: /* bitmap type 1 */
1015 BitmapHeader1.Width=ReadBlobLSBShort(image);
1016 BitmapHeader1.Heigth=ReadBlobLSBShort(image);
1017 BitmapHeader1.Depth=ReadBlobLSBShort(image);
1018 BitmapHeader1.HorzRes=ReadBlobLSBShort(image);
1019 BitmapHeader1.VertRes=ReadBlobLSBShort(image);
1020
1021 if(BitmapHeader1.HorzRes && BitmapHeader1.VertRes)
1022 {
1023 image->units=PixelsPerCentimeterResolution;
cristy2a11bef2011-10-28 18:33:11 +00001024 image->resolution.x=BitmapHeader1.HorzRes/470.0;
1025 image->resolution.y=BitmapHeader1.VertRes/470.0;
cristy3ed852e2009-09-05 21:47:34 +00001026 }
1027 image->columns=BitmapHeader1.Width;
1028 image->rows=BitmapHeader1.Heigth;
1029 bpp=BitmapHeader1.Depth;
1030
1031 goto UnpackRaster;
1032
1033 case 0x0E: /*Color palette */
1034 WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1035 WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1036
1037 image->colors=WPG_Palette.NumOfEntries;
cristy018f07f2011-09-04 21:15:19 +00001038 if (!AcquireImageColormap(image,image->colors,exception))
cristy3ed852e2009-09-05 21:47:34 +00001039 goto NoMemory;
1040 for (i=WPG_Palette.StartIndex;
1041 i < (int)WPG_Palette.NumOfEntries; i++)
1042 {
1043 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1044 ReadBlobByte(image));
1045 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1046 ReadBlobByte(image));
1047 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1048 ReadBlobByte(image));
1049 }
1050 break;
1051
1052 case 0x11: /* Start PS l1 */
1053 if(Rec.RecordLength > 8)
1054 image=ExtractPostscript(image,image_info,
1055 TellBlob(image)+8, /* skip PS header in the wpg */
cristybb503372010-05-27 20:51:26 +00001056 (ssize_t) Rec.RecordLength-8,exception);
cristy3ed852e2009-09-05 21:47:34 +00001057 break;
1058
1059 case 0x14: /* bitmap type 2 */
1060 BitmapHeader2.RotAngle=ReadBlobLSBShort(image);
1061 BitmapHeader2.LowLeftX=ReadBlobLSBShort(image);
1062 BitmapHeader2.LowLeftY=ReadBlobLSBShort(image);
1063 BitmapHeader2.UpRightX=ReadBlobLSBShort(image);
1064 BitmapHeader2.UpRightY=ReadBlobLSBShort(image);
1065 BitmapHeader2.Width=ReadBlobLSBShort(image);
1066 BitmapHeader2.Heigth=ReadBlobLSBShort(image);
1067 BitmapHeader2.Depth=ReadBlobLSBShort(image);
1068 BitmapHeader2.HorzRes=ReadBlobLSBShort(image);
1069 BitmapHeader2.VertRes=ReadBlobLSBShort(image);
1070
1071 image->units=PixelsPerCentimeterResolution;
1072 image->page.width=(unsigned int)
1073 ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightX)/470.0);
1074 image->page.height=(unsigned int)
1075 ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightY)/470.0);
1076 image->page.x=(int) (BitmapHeader2.LowLeftX/470.0);
1077 image->page.y=(int) (BitmapHeader2.LowLeftX/470.0);
1078 if(BitmapHeader2.HorzRes && BitmapHeader2.VertRes)
1079 {
cristy2a11bef2011-10-28 18:33:11 +00001080 image->resolution.x=BitmapHeader2.HorzRes/470.0;
1081 image->resolution.y=BitmapHeader2.VertRes/470.0;
cristy3ed852e2009-09-05 21:47:34 +00001082 }
1083 image->columns=BitmapHeader2.Width;
1084 image->rows=BitmapHeader2.Heigth;
1085 bpp=BitmapHeader2.Depth;
1086
1087 UnpackRaster:
1088 if ((image->colors == 0) && (bpp != 24))
1089 {
cristyeaedf062010-05-29 22:36:02 +00001090 image->colors=one << bpp;
cristy018f07f2011-09-04 21:15:19 +00001091 if (!AcquireImageColormap(image,image->colors,exception))
cristy3ed852e2009-09-05 21:47:34 +00001092 {
1093 NoMemory:
1094 ThrowReaderException(ResourceLimitError,
1095 "MemoryAllocationFailed");
1096 }
1097 /* printf("Load default colormap \n"); */
1098 for (i=0; (i < (int) image->colors) && (i < 256); i++)
1099 {
1100 image->colormap[i].red=ScaleCharToQuantum(WPG1_Palette[i].Red);
1101 image->colormap[i].green=ScaleCharToQuantum(WPG1_Palette[i].Green);
1102 image->colormap[i].blue=ScaleCharToQuantum(WPG1_Palette[i].Blue);
1103 }
1104 }
1105 else
1106 {
1107 if (bpp < 24)
cristyeaedf062010-05-29 22:36:02 +00001108 if ( (image->colors < (one << bpp)) && (bpp != 24) )
cristy101ab702011-10-13 13:06:32 +00001109 image->colormap=(PixelInfo *) ResizeQuantumMemory(
cristyeaedf062010-05-29 22:36:02 +00001110 image->colormap,(size_t) (one << bpp),
cristy3ed852e2009-09-05 21:47:34 +00001111 sizeof(*image->colormap));
1112 }
1113
1114 if (bpp == 1)
1115 {
1116 if(image->colormap[0].red==0 &&
1117 image->colormap[0].green==0 &&
1118 image->colormap[0].blue==0 &&
1119 image->colormap[1].red==0 &&
1120 image->colormap[1].green==0 &&
1121 image->colormap[1].blue==0)
1122 { /* fix crippled monochrome palette */
1123 image->colormap[1].red =
1124 image->colormap[1].green =
1125 image->colormap[1].blue = QuantumRange;
1126 }
1127 }
1128
cristyc82a27b2011-10-21 01:07:16 +00001129 if(UnpackWPGRaster(image,bpp,exception) < 0)
cristy3ed852e2009-09-05 21:47:34 +00001130 /* The raster cannot be unpacked */
1131 {
1132 DecompressionFailed:
1133 ThrowReaderException(CoderError,"UnableToDecompressImage");
1134 }
1135
1136 if(Rec.RecType==0x14 && BitmapHeader2.RotAngle!=0 && !image_info->ping)
1137 {
1138 /* flop command */
1139 if(BitmapHeader2.RotAngle & 0x8000)
1140 {
1141 rotated_image = FlopImage(image, exception);
1142 rotated_image->blob = image->blob;
1143 DuplicateBlob(rotated_image,image);
1144 (void) RemoveLastImageFromList(&image);
1145 AppendImageToList(&image,rotated_image);
1146 }
1147 /* flip command */
1148 if(BitmapHeader2.RotAngle & 0x2000)
1149 {
1150 rotated_image = FlipImage(image, exception);
1151 rotated_image->blob = image->blob;
1152 DuplicateBlob(rotated_image,image);
1153 (void) RemoveLastImageFromList(&image);
1154 AppendImageToList(&image,rotated_image);
1155 }
1156
1157 /* rotate command */
1158 if(BitmapHeader2.RotAngle & 0x0FFF)
1159 {
1160 rotated_image = RotateImage(image, (BitmapHeader2.RotAngle & 0x0FFF), exception);
1161 rotated_image->blob = image->blob;
1162 DuplicateBlob(rotated_image,image);
1163 (void) RemoveLastImageFromList(&image);
1164 AppendImageToList(&image,rotated_image);
1165 }
1166 }
1167
1168 /* Allocate next image structure. */
cristy9950d572011-10-01 18:22:35 +00001169 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001170 image->depth=8;
1171 if (image->next == (Image *) NULL)
1172 goto Finish;
1173 image=SyncNextImageInList(image);
1174 image->columns=image->rows=0;
1175 image->colors=0;
1176 break;
1177
1178 case 0x1B: /* Postscript l2 */
1179 if(Rec.RecordLength>0x3C)
1180 image=ExtractPostscript(image,image_info,
1181 TellBlob(image)+0x3C, /* skip PS l2 header in the wpg */
cristybb503372010-05-27 20:51:26 +00001182 (ssize_t) Rec.RecordLength-0x3C,exception);
cristy3ed852e2009-09-05 21:47:34 +00001183 break;
1184 }
1185 }
1186 break;
1187
1188 case 2: /* WPG level 2 */
1189 (void) memset(CTM,0,sizeof(CTM));
1190 StartWPG.PosSizePrecision = 0;
1191 while(!EOFBlob(image)) /* object parser loop */
1192 {
1193 (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
1194 if(EOFBlob(image))
1195 break;
1196
1197 Rec2.Class=(i=ReadBlobByte(image));
1198 if(i==EOF)
1199 break;
1200 Rec2.RecType=(i=ReadBlobByte(image));
1201 if(i==EOF)
1202 break;
1203 Rd_WP_DWORD(image,&Rec2.Extension);
1204 Rd_WP_DWORD(image,&Rec2.RecordLength);
1205 if(EOFBlob(image))
1206 break;
1207
1208 Header.DataOffset=TellBlob(image)+Rec2.RecordLength;
1209
1210 switch(Rec2.RecType)
1211 {
1212 case 1:
1213 StartWPG.HorizontalUnits=ReadBlobLSBShort(image);
1214 StartWPG.VerticalUnits=ReadBlobLSBShort(image);
1215 StartWPG.PosSizePrecision=ReadBlobByte(image);
1216 break;
1217 case 0x0C: /* Color palette */
1218 WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1219 WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1220
1221 image->colors=WPG_Palette.NumOfEntries;
cristy018f07f2011-09-04 21:15:19 +00001222 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001223 ThrowReaderException(ResourceLimitError,
1224 "MemoryAllocationFailed");
1225 for (i=WPG_Palette.StartIndex;
1226 i < (int)WPG_Palette.NumOfEntries; i++)
1227 {
1228 image->colormap[i].red=ScaleCharToQuantum((char)
1229 ReadBlobByte(image));
1230 image->colormap[i].green=ScaleCharToQuantum((char)
1231 ReadBlobByte(image));
1232 image->colormap[i].blue=ScaleCharToQuantum((char)
1233 ReadBlobByte(image));
1234 (void) ReadBlobByte(image); /*Opacity??*/
1235 }
1236 break;
1237 case 0x0E:
1238 Bitmap2Header1.Width=ReadBlobLSBShort(image);
1239 Bitmap2Header1.Heigth=ReadBlobLSBShort(image);
1240 Bitmap2Header1.Depth=ReadBlobByte(image);
1241 Bitmap2Header1.Compression=ReadBlobByte(image);
1242
1243 if(Bitmap2Header1.Compression > 1)
1244 continue; /*Unknown compression method */
1245 switch(Bitmap2Header1.Depth)
1246 {
1247 case 1:
1248 bpp=1;
1249 break;
1250 case 2:
1251 bpp=2;
1252 break;
1253 case 3:
1254 bpp=4;
1255 break;
1256 case 4:
1257 bpp=8;
1258 break;
1259 case 8:
1260 bpp=24;
1261 break;
1262 default:
1263 continue; /*Ignore raster with unknown depth*/
1264 }
1265 image->columns=Bitmap2Header1.Width;
1266 image->rows=Bitmap2Header1.Heigth;
1267
1268 if ((image->colors == 0) && (bpp != 24))
1269 {
cristy0b29b252010-05-30 01:59:46 +00001270 size_t
1271 one;
1272
1273 one=1;
1274 image->colors=one << bpp;
cristy018f07f2011-09-04 21:15:19 +00001275 if (!AcquireImageColormap(image,image->colors,exception))
cristy3ed852e2009-09-05 21:47:34 +00001276 goto NoMemory;
1277 }
1278 else
1279 {
1280 if(bpp < 24)
cristyeaedf062010-05-29 22:36:02 +00001281 if( image->colors<(one << bpp) && bpp!=24 )
cristy101ab702011-10-13 13:06:32 +00001282 image->colormap=(PixelInfo *) ResizeQuantumMemory(
cristyeaedf062010-05-29 22:36:02 +00001283 image->colormap,(size_t) (one << bpp),
cristy3ed852e2009-09-05 21:47:34 +00001284 sizeof(*image->colormap));
1285 }
1286
1287
1288 switch(Bitmap2Header1.Compression)
1289 {
1290 case 0: /*Uncompressed raster*/
1291 {
cristybb503372010-05-27 20:51:26 +00001292 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
cristy3ed852e2009-09-05 21:47:34 +00001293 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t)
1294 ldblk,sizeof(*BImgBuff));
1295 if (BImgBuff == (unsigned char *) NULL)
1296 goto NoMemory;
1297
cristybb503372010-05-27 20:51:26 +00001298 for(i=0; i< (ssize_t) image->rows; i++)
cristy3ed852e2009-09-05 21:47:34 +00001299 {
1300 (void) ReadBlob(image,ldblk,BImgBuff);
cristyc82a27b2011-10-21 01:07:16 +00001301 InsertRow(image,BImgBuff,i,bpp,exception);
cristy3ed852e2009-09-05 21:47:34 +00001302 }
1303
1304 if(BImgBuff)
1305 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);;
1306 break;
1307 }
1308 case 1: /*RLE for WPG2 */
1309 {
cristyc82a27b2011-10-21 01:07:16 +00001310 if( UnpackWPG2Raster(image,bpp,exception) < 0)
cristy3ed852e2009-09-05 21:47:34 +00001311 goto DecompressionFailed;
1312 break;
1313 }
1314 }
1315
1316 if(CTM[0][0]<0 && !image_info->ping)
1317 { /*?? RotAngle=360-RotAngle;*/
1318 rotated_image = FlopImage(image, exception);
1319 rotated_image->blob = image->blob;
1320 DuplicateBlob(rotated_image,image);
1321 (void) RemoveLastImageFromList(&image);
1322 AppendImageToList(&image,rotated_image);
1323 /* Try to change CTM according to Flip - I am not sure, must be checked.
1324 Tx(0,0)=-1; Tx(1,0)=0; Tx(2,0)=0;
1325 Tx(0,1)= 0; Tx(1,1)=1; Tx(2,1)=0;
1326 Tx(0,2)=(WPG._2Rect.X_ur+WPG._2Rect.X_ll);
1327 Tx(1,2)=0; Tx(2,2)=1; */
1328 }
1329 if(CTM[1][1]<0 && !image_info->ping)
1330 { /*?? RotAngle=360-RotAngle;*/
1331 rotated_image = FlipImage(image, exception);
1332 rotated_image->blob = image->blob;
1333 DuplicateBlob(rotated_image,image);
1334 (void) RemoveLastImageFromList(&image);
1335 AppendImageToList(&image,rotated_image);
1336 /* Try to change CTM according to Flip - I am not sure, must be checked.
1337 float_matrix Tx(3,3);
1338 Tx(0,0)= 1; Tx(1,0)= 0; Tx(2,0)=0;
1339 Tx(0,1)= 0; Tx(1,1)=-1; Tx(2,1)=0;
1340 Tx(0,2)= 0; Tx(1,2)=(WPG._2Rect.Y_ur+WPG._2Rect.Y_ll);
1341 Tx(2,2)=1; */
1342 }
1343
1344
1345 /* Allocate next image structure. */
cristy9950d572011-10-01 18:22:35 +00001346 AcquireNextImage(image_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001347 image->depth=8;
1348 if (image->next == (Image *) NULL)
1349 goto Finish;
1350 image=SyncNextImageInList(image);
1351 image->columns=image->rows=1;
1352 image->colors=0;
1353 break;
1354
1355 case 0x12: /* Postscript WPG2*/
1356 i=ReadBlobLSBShort(image);
1357 if(Rec2.RecordLength > (unsigned int) i)
1358 image=ExtractPostscript(image,image_info,
1359 TellBlob(image)+i, /*skip PS header in the wpg2*/
cristybb503372010-05-27 20:51:26 +00001360 (ssize_t) (Rec2.RecordLength-i-2),exception);
cristy3ed852e2009-09-05 21:47:34 +00001361 break;
1362
1363 case 0x1B: /*bitmap rectangle*/
1364 WPG2Flags = LoadWPG2Flags(image,StartWPG.PosSizePrecision,NULL,&CTM);
cristyda16f162011-02-19 23:52:17 +00001365 (void) WPG2Flags;
cristy3ed852e2009-09-05 21:47:34 +00001366 break;
1367 }
1368 }
1369
1370 break;
1371
1372 default:
1373 {
1374 ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
1375 }
1376 }
1377
1378 Finish:
1379 (void) CloseBlob(image);
1380
1381 {
1382 Image
1383 *p;
1384
cristybb503372010-05-27 20:51:26 +00001385 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001386 scene=0;
1387
1388 /*
1389 Rewind list, removing any empty images while rewinding.
1390 */
1391 p=image;
1392 image=NULL;
1393 while (p != (Image *) NULL)
1394 {
1395 Image *tmp=p;
1396 if ((p->rows == 0) || (p->columns == 0)) {
1397 p=p->previous;
1398 DeleteImageFromList(&tmp);
1399 } else {
1400 image=p;
1401 p=p->previous;
1402 }
1403 }
1404 /*
1405 Fix scene numbers.
1406 */
1407 for (p=image; p != (Image *) NULL; p=p->next)
cristybb503372010-05-27 20:51:26 +00001408 p->scene=(size_t) scene++;
cristy3ed852e2009-09-05 21:47:34 +00001409 }
1410 if (image == (Image *) NULL)
1411 ThrowReaderException(CorruptImageError,
1412 "ImageFileDoesNotContainAnyImageData");
1413 return(image);
1414}
1415
1416/*
1417%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1418% %
1419% %
1420% %
1421% R e g i s t e r W P G I m a g e %
1422% %
1423% %
1424% %
1425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426%
1427% Method RegisterWPGImage adds attributes for the WPG image format to
1428% the list of supported formats. The attributes include the image format
1429% tag, a method to read and/or write the format, whether the format
1430% supports the saving of more than one frame to the same file or blob,
1431% whether the format supports native in-memory I/O, and a brief
1432% description of the format.
1433%
1434% The format of the RegisterWPGImage method is:
1435%
cristybb503372010-05-27 20:51:26 +00001436% size_t RegisterWPGImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001437%
1438*/
cristybb503372010-05-27 20:51:26 +00001439ModuleExport size_t RegisterWPGImage(void)
cristy3ed852e2009-09-05 21:47:34 +00001440{
1441 MagickInfo
1442 *entry;
1443
1444 entry=SetMagickInfo("WPG");
1445 entry->decoder=(DecodeImageHandler *) ReadWPGImage;
1446 entry->magick=(IsImageFormatHandler *) IsWPG;
1447 entry->description=AcquireString("Word Perfect Graphics");
1448 entry->module=ConstantString("WPG");
1449 entry->seekable_stream=MagickTrue;
1450 (void) RegisterMagickInfo(entry);
1451 return(MagickImageCoderSignature);
1452}
1453
1454/*
1455%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1456% %
1457% %
1458% %
1459% U n r e g i s t e r W P G I m a g e %
1460% %
1461% %
1462% %
1463%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1464%
1465% Method UnregisterWPGImage removes format registrations made by the
1466% WPG module from the list of supported formats.
1467%
1468% The format of the UnregisterWPGImage method is:
1469%
1470% UnregisterWPGImage(void)
1471%
1472*/
1473ModuleExport void UnregisterWPGImage(void)
1474{
1475 (void) UnregisterMagickInfo("WPG");
1476}