]> granicus.if.org Git - imagemagick/blob - coders/wpg.c
(no commit message)
[imagemagick] / coders / wpg.c
1 /*
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 %                                                                             %
19 %  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
20 %  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 \f
38 /*
39   Include declarations.
40 */
41 #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"
51 #include "MagickCore/image.h"
52 #include "MagickCore/image-private.h"
53 #include "MagickCore/list.h"
54 #include "MagickCore/magic.h"
55 #include "MagickCore/magick.h"
56 #include "MagickCore/memory_.h"
57 #include "MagickCore/resource_.h"
58 #include "MagickCore/pixel-accessor.h"
59 #include "MagickCore/quantum-private.h"
60 #include "MagickCore/shear.h"
61 #include "MagickCore/static.h"
62 #include "MagickCore/string_.h"
63 #include "MagickCore/module.h"
64 #include "MagickCore/transform.h"
65 #include "MagickCore/utility.h"
66 #include "MagickCore/utility-private.h"
67 \f
68 typedef 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 */
76 const 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 \f
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 */
234 static 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 \f
243
244 static void Rd_WP_DWORD(Image *image,size_t *d)
245 {
246   unsigned char
247     b;
248
249   b=ReadBlobByte(image);
250   *d=b;
251   if (b < 0xFFU)
252     return;
253   b=ReadBlobByte(image);
254   *d=(size_t) b;
255   b=ReadBlobByte(image);
256   *d+=(size_t) b*256l;
257   if (*d < 0x8000)
258     return;
259   *d=(*d & 0x7FFF) << 16;
260   b=ReadBlobByte(image);
261   *d+=(size_t) b;
262   b=ReadBlobByte(image);
263   *d+=(size_t) b*256l;
264   return;
265 }
266
267 static void InsertRow(unsigned char *p,ssize_t y,Image *image, int bpp)
268 {
269   ExceptionInfo
270     *exception;
271
272   int
273     bit;
274
275   Quantum
276     index;
277
278   register Quantum
279     *q;
280
281   ssize_t
282     x;
283
284   exception=(&image->exception);
285   switch (bpp)
286     {
287     case 1:  /* Convert bitmap scanline. */
288       {
289         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
290         if (q == (Quantum *) NULL)
291           break;
292         for (x=0; x < ((ssize_t) image->columns-7); x+=8)
293         {
294           for (bit=0; bit < 8; bit++)
295           {
296             index=((*p) & (0x80 >> bit) ? 0x01 : 0x00);
297             SetPixelIndex(image,index,q);
298             SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
299             q+=GetPixelChannels(image);
300           }
301           p++;
302         }
303         if ((image->columns % 8) != 0)
304           {
305             for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
306             {
307               index=((*p) & (0x80 >> bit) ? 0x01 : 0x00);
308               SetPixelIndex(image,index,q);
309               SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
310               q+=GetPixelChannels(image);
311             }
312             p++;
313           }
314         if (!SyncAuthenticPixels(image,exception))
315           break;
316         break;
317       }
318     case 2:  /* Convert PseudoColor scanline. */
319       {
320         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
321         if (q == (Quantum *) NULL)
322           break;
323         for (x=0; x < ((ssize_t) image->columns-1); x+=2)
324         {
325             index=ConstrainColormapIndex(image,(*p >> 6) & 0x3);
326             SetPixelIndex(image,index,q);
327             SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
328             q+=GetPixelChannels(image);
329             index=ConstrainColormapIndex(image,(*p >> 4) & 0x3);
330             SetPixelIndex(image,index,q);
331             SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
332             q+=GetPixelChannels(image);
333             index=ConstrainColormapIndex(image,(*p >> 2) & 0x3);
334             SetPixelIndex(image,index,q);
335             SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
336             q+=GetPixelChannels(image);
337             index=ConstrainColormapIndex(image,(*p) & 0x3);
338             SetPixelIndex(image,index,q);
339             SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
340             p++;
341             q+=GetPixelChannels(image);
342         }
343        if ((image->columns % 4) != 0)
344           {
345             index=ConstrainColormapIndex(image,(*p >> 6) & 0x3);
346             SetPixelIndex(image,index,q);
347             SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
348             q+=GetPixelChannels(image);
349             if ((image->columns % 4) >= 1)
350
351               {
352                 index=ConstrainColormapIndex(image,(*p >> 4) & 0x3);
353                 SetPixelIndex(image,index,q);
354                 SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
355                 q+=GetPixelChannels(image);
356                 if ((image->columns % 4) >= 2)
357
358                   {
359                     index=ConstrainColormapIndex(image,(*p >> 2) & 0x3);
360                     SetPixelIndex(image,index,q);
361                     SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
362                     q+=GetPixelChannels(image);
363                   }
364               }
365             p++;
366           }
367         if (SyncAuthenticPixels(image,exception) == MagickFalse)
368           break;
369         break;
370       }
371  
372     case 4:  /* Convert PseudoColor scanline. */
373       {
374         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
375         if (q == (Quantum *) NULL)
376           break;
377         for (x=0; x < ((ssize_t) image->columns-1); x+=2)
378           { 
379             index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f);
380             SetPixelIndex(image,index,q);
381             SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
382             q+=GetPixelChannels(image);
383             index=ConstrainColormapIndex(image,(*p) & 0x0f);
384             SetPixelIndex(image,index,q);
385             SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
386             p++;
387             q+=GetPixelChannels(image);
388           }
389         if ((image->columns % 2) != 0)
390           {
391             index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f);
392             SetPixelIndex(image,index,q);
393             SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
394             p++;
395             q+=GetPixelChannels(image);
396           }
397         if (SyncAuthenticPixels(image,exception) == MagickFalse)
398           break;
399         break;
400       }
401     case 8: /* Convert PseudoColor scanline. */
402       {
403         q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
404         if (q == (Quantum *) NULL) break;
405
406         for (x=0; x < (ssize_t) image->columns; x++)
407           {
408             index=ConstrainColormapIndex(image,*p);
409             SetPixelIndex(image,index,q);
410             SetPixelPixelInfo(image,image->colormap+(ssize_t) index,q);
411             p++;
412             q+=GetPixelChannels(image);
413           }
414         if (SyncAuthenticPixels(image,exception) == MagickFalse)
415           break;
416       }
417       break;
418      
419     case 24:     /*  Convert DirectColor scanline.  */
420       q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
421       if (q == (Quantum *) NULL)
422         break;
423       for (x=0; x < (ssize_t) image->columns; x++)
424         {
425           SetPixelRed(image,ScaleCharToQuantum(*p++),q);
426           SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
427           SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
428           q+=GetPixelChannels(image);
429         }
430       if (!SyncAuthenticPixels(image,exception))
431         break;
432       break;     
433     }
434 }
435
436
437 /* Helper for WPG1 raster reader. */
438 #define InsertByte(b) \
439 { \
440   BImgBuff[x]=b; \
441   x++; \
442   if((ssize_t) x>=ldblk) \
443   { \
444     InsertRow(BImgBuff,(ssize_t) y,image,bpp); \
445     x=0; \
446     y++; \
447     } \
448 }
449 /* WPG1 raster reader. */
450 static int UnpackWPGRaster(Image *image,int bpp)
451 {
452   int
453     x,
454     y,
455     i;
456
457   unsigned char
458     bbuf,
459     *BImgBuff,
460     RunCount;
461
462   ssize_t
463     ldblk;
464
465   x=0;
466   y=0;
467
468   ldblk=(ssize_t) ((bpp*image->columns+7)/8);
469   BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
470     sizeof(*BImgBuff));
471   if(BImgBuff==NULL) return(-2);
472
473   while(y<(ssize_t) image->rows)
474     {
475       bbuf=ReadBlobByte(image);
476
477       RunCount=bbuf & 0x7F;
478       if(bbuf & 0x80)
479         {
480           if(RunCount)  /* repeat next byte runcount * */
481             {
482               bbuf=ReadBlobByte(image);
483               for(i=0;i<(int) RunCount;i++) InsertByte(bbuf);
484             }
485           else {  /* read next byte as RunCount; repeat 0xFF runcount* */
486             RunCount=ReadBlobByte(image);
487             for(i=0;i<(int) RunCount;i++) InsertByte(0xFF);
488           }
489         }
490       else {
491         if(RunCount)   /* next runcount byte are readed directly */
492           {
493             for(i=0;i < (int) RunCount;i++)
494               {
495                 bbuf=ReadBlobByte(image);
496                 InsertByte(bbuf);
497               }
498           }
499         else {  /* repeat previous line runcount* */
500           RunCount=ReadBlobByte(image);
501           if(x) {    /* attempt to duplicate row from x position: */
502             /* I do not know what to do here */
503             BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
504             return(-3);
505           }
506           for(i=0;i < (int) RunCount;i++)
507             {
508               x=0;
509               y++;    /* Here I need to duplicate previous row RUNCOUNT* */
510               if(y<2) continue;
511               if(y>(ssize_t) image->rows)
512                 {
513                   BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
514                   return(-4);
515                 }
516               InsertRow(BImgBuff,y-1,image,bpp);
517             }
518         }
519       }
520     }
521   BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
522   return(0);
523 }
524
525
526 /* Helper for WPG2 reader. */
527 #define InsertByte6(b) \
528 { \
529   if(XorMe)\
530     BImgBuff[x] = (unsigned char)~b;\
531   else\
532     BImgBuff[x] = b;\
533   x++; \
534   if((ssize_t) x >= ldblk) \
535   { \
536     InsertRow(BImgBuff,(ssize_t) y,image,bpp); \
537     x=0; \
538     y++; \
539    } \
540 }
541 /* WPG2 raster reader. */
542 static int UnpackWPG2Raster(Image *image,int bpp)
543 {
544   size_t
545     x,
546     y;
547
548   ssize_t
549     ldblk;
550
551   int XorMe = 0;
552
553   unsigned int
554     SampleSize=1;
555
556   unsigned char
557     bbuf,
558     *BImgBuff,
559     RunCount,
560     SampleBuffer[8];
561
562   unsigned int
563     i;
564
565   x=0;
566   y=0;
567   ldblk=(ssize_t) ((bpp*image->columns+7)/8);
568   BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
569     sizeof(*BImgBuff));
570   if(BImgBuff==NULL)
571     return(-2);
572
573   while( y< image->rows)
574     {
575       bbuf=ReadBlobByte(image);
576
577       switch(bbuf)
578         {
579         case 0x7D:
580           SampleSize=ReadBlobByte(image);  /* DSZ */
581           if(SampleSize>8)
582             return(-2);
583           if(SampleSize<1)
584             return(-2);
585           break;
586         case 0x7E:
587           (void) FormatLocaleFile(stderr,
588             "\nUnsupported WPG token XOR, please report!");
589           XorMe=!XorMe;
590           break;
591         case 0x7F:
592           RunCount=ReadBlobByte(image);   /* BLK */
593           for(i=0; i < SampleSize*(RunCount+1); i++)
594             {
595               InsertByte6(0);
596             }
597           break;
598         case 0xFD:
599           RunCount=ReadBlobByte(image);   /* EXT */
600           for(i=0; i<= RunCount;i++)
601             for(bbuf=0; bbuf < SampleSize; bbuf++)
602               InsertByte6(SampleBuffer[bbuf]);          
603           break;
604         case 0xFE:
605           RunCount=ReadBlobByte(image);  /* RST */
606           if(x!=0)
607             {
608               (void) FormatLocaleFile(stderr,
609                 "\nUnsupported WPG2 unaligned token RST x=%.20g, please report!\n"
610                 ,(double) x);
611               return(-3);
612             }
613           {
614             /* duplicate the previous row RunCount x */
615             for(i=0;i<=RunCount;i++)
616               {      
617                 InsertRow(BImgBuff,(ssize_t) (image->rows >= y ? y : image->rows-1),
618                           image,bpp);
619                 y++;
620               }    
621           }
622           break;
623         case 0xFF:
624           RunCount=ReadBlobByte(image);   /* WHT */
625           for(i=0; i < SampleSize*(RunCount+1); i++)
626             {
627               InsertByte6(0xFF);
628             }
629           break;
630         default:
631           RunCount=bbuf & 0x7F;
632
633           if(bbuf & 0x80)     /* REP */
634             {  
635               for(i=0; i < SampleSize; i++)
636                 SampleBuffer[i]=ReadBlobByte(image);
637               for(i=0;i<=RunCount;i++)
638                 for(bbuf=0;bbuf<SampleSize;bbuf++)
639                   InsertByte6(SampleBuffer[bbuf]);
640             }
641           else {      /* NRP */
642             for(i=0; i< SampleSize*(RunCount+1);i++)
643               {
644                 bbuf=ReadBlobByte(image);
645                 InsertByte6(bbuf);
646               }
647           }
648         }
649     }
650   BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
651   return(0);
652 }
653
654
655 typedef float tCTM[3][3];
656
657 static unsigned LoadWPG2Flags(Image *image,char Precision,float *Angle,tCTM *CTM)
658 {
659 const unsigned char TPR=1,TRN=2,SKW=4,SCL=8,ROT=0x10,OID=0x20,LCK=0x80;
660 ssize_t x;
661 unsigned DenX;
662 unsigned Flags;
663
664  (void) memset(*CTM,0,sizeof(*CTM));     /*CTM.erase();CTM.resize(3,3);*/
665  (*CTM)[0][0]=1;
666  (*CTM)[1][1]=1;
667  (*CTM)[2][2]=1;
668
669  Flags=ReadBlobLSBShort(image);
670  if(Flags & LCK) x=ReadBlobLSBLong(image);  /*Edit lock*/
671  if(Flags & OID)
672   {
673   if(Precision==0)
674     {x=ReadBlobLSBShort(image);}  /*ObjectID*/
675   else
676     {x=ReadBlobLSBLong(image);}  /*ObjectID (Double precision)*/
677   }
678  if(Flags & ROT)
679   {
680   x=ReadBlobLSBLong(image);  /*Rot Angle*/
681   if(Angle) *Angle=x/65536.0;
682   }
683  if(Flags & (ROT|SCL))
684   {
685   x=ReadBlobLSBLong(image);  /*Sx*cos()*/
686   (*CTM)[0][0] = (float)x/0x10000;
687   x=ReadBlobLSBLong(image);  /*Sy*cos()*/
688   (*CTM)[1][1] = (float)x/0x10000;
689   }
690  if(Flags & (ROT|SKW))
691   {
692   x=ReadBlobLSBLong(image);       /*Kx*sin()*/
693   (*CTM)[1][0] = (float)x/0x10000;
694   x=ReadBlobLSBLong(image);       /*Ky*sin()*/
695   (*CTM)[0][1] = (float)x/0x10000;
696   }
697  if(Flags & TRN)
698   {
699   x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image);  /*Tx*/
700         if(x>=0) (*CTM)[0][2] = (float)x+(float)DenX/0x10000;
701             else (*CTM)[0][2] = (float)x-(float)DenX/0x10000;
702   x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image);  /*Ty*/
703   (*CTM)[1][2]=(float)x + ((x>=0)?1:-1)*(float)DenX/0x10000;
704         if(x>=0) (*CTM)[1][2] = (float)x+(float)DenX/0x10000;
705             else (*CTM)[1][2] = (float)x-(float)DenX/0x10000;
706   }
707  if(Flags & TPR)
708   {
709   x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image);  /*Px*/
710   (*CTM)[2][0] = x + (float)DenX/0x10000;;
711   x=ReadBlobLSBShort(image);  DenX=ReadBlobLSBShort(image); /*Py*/
712   (*CTM)[2][1] = x + (float)DenX/0x10000;
713   }
714  return(Flags);
715 }
716
717
718 static Image *ExtractPostscript(Image *image,const ImageInfo *image_info,
719   MagickOffsetType PS_Offset,ssize_t PS_Size,ExceptionInfo *exception)
720 {
721   char
722     postscript_file[MaxTextExtent];
723
724   const MagicInfo
725     *magic_info;    
726
727   FILE
728     *ps_file;
729
730   ImageInfo
731     *clone_info;
732     
733   Image
734     *image2;
735     
736   unsigned char
737     magick[2*MaxTextExtent];    
738     
739
740   if ((clone_info=CloneImageInfo(image_info)) == NULL)
741     return(image);
742   clone_info->blob=(void *) NULL;
743   clone_info->length=0;
744
745   /* Obtain temporary file */
746   AcquireUniqueFilename(postscript_file);
747   ps_file=fopen_utf8(postscript_file,"wb");
748   if (ps_file == (FILE *) NULL)
749     goto FINISH;
750
751   /* Copy postscript to temporary file */
752   (void) SeekBlob(image,PS_Offset,SEEK_SET);
753   (void) ReadBlob(image, 2*MaxTextExtent, magick);
754   
755   (void) SeekBlob(image,PS_Offset,SEEK_SET);
756   while(PS_Size-- > 0)
757     {
758       (void) fputc(ReadBlobByte(image),ps_file);
759     }
760   (void) fclose(ps_file);
761   
762     /* Detect file format - Check magic.mgk configuration file. */
763   magic_info=GetMagicInfo(magick,2*MaxTextExtent,exception);
764   if(magic_info == (const MagicInfo *) NULL) goto FINISH_UNL;
765   /*     printf("Detected:%s  \n",magic_info->name); */
766   if(exception->severity != UndefinedException) goto FINISH_UNL;     
767   if(magic_info->name == (char *) NULL) goto FINISH_UNL;
768     
769   (void) CopyMagickMemory(clone_info->magick,magic_info->name,MaxTextExtent);
770   
771     /* Read nested image */
772   /*FormatString(clone_info->filename,"%s:%s",magic_info->name,postscript_file);*/
773   FormatLocaleString(clone_info->filename,MaxTextExtent,"%s",postscript_file);
774   image2=ReadImage(clone_info,exception);
775
776   if (!image2)
777     goto FINISH_UNL;
778
779   /*
780     Replace current image with new image while copying base image
781     attributes.
782   */
783   (void) CopyMagickMemory(image2->filename,image->filename,MaxTextExtent);
784   (void) CopyMagickMemory(image2->magick_filename,image->magick_filename,MaxTextExtent);
785   (void) CopyMagickMemory(image2->magick,image->magick,MaxTextExtent);
786   image2->depth=image->depth;
787   DestroyBlob(image2);
788   image2->blob=ReferenceBlob(image->blob);
789
790   if ((image->rows == 0) || (image->columns == 0))
791     DeleteImageFromList(&image);
792
793   AppendImageToList(&image,image2);
794
795  FINISH_UNL:    
796   (void) RelinquishUniqueFileResource(postscript_file);
797  FINISH:
798   DestroyImageInfo(clone_info);
799   return(image);
800 }
801 \f
802 /*
803 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
804 %                                                                             %
805 %                                                                             %
806 %                                                                             %
807 %   R e a d W P G I m a g e                                                   %
808 %                                                                             %
809 %                                                                             %
810 %                                                                             %
811 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
812 %
813 %  Method ReadWPGImage reads an WPG X image file and returns it.  It
814 %  allocates the memory necessary for the new Image structure and returns a
815 %  pointer to the new image.
816 %
817 %  The format of the ReadWPGImage method is:
818 %
819 %    Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
820 %
821 %  A description of each parameter follows:
822 %
823 %    o image:  Method ReadWPGImage returns a pointer to the image after
824 %      reading. A null image is returned if there is a memory shortage or if
825 %      the image cannot be read.
826 %
827 %    o image_info: Specifies a pointer to a ImageInfo structure.
828 %
829 %    o exception: return any errors or warnings in this structure.
830 %
831 */
832 static Image *ReadWPGImage(const ImageInfo *image_info,
833   ExceptionInfo *exception)
834 {
835   typedef struct
836   {
837     size_t FileId;
838     MagickOffsetType DataOffset;
839     unsigned int ProductType;
840     unsigned int FileType;
841     unsigned char MajorVersion;
842     unsigned char MinorVersion;
843     unsigned int EncryptKey;
844     unsigned int Reserved;
845   } WPGHeader;
846
847   typedef struct
848   {
849     unsigned char RecType;
850     size_t RecordLength;
851   } WPGRecord;
852
853   typedef struct
854   {
855     unsigned char Class;
856     unsigned char RecType;
857     size_t Extension;
858     size_t RecordLength;
859   } WPG2Record;
860
861   typedef struct
862   {
863     unsigned  HorizontalUnits;
864     unsigned  VerticalUnits;
865     unsigned char PosSizePrecision;
866   } WPG2Start;
867
868   typedef struct
869   {
870     unsigned int Width;
871     unsigned int Heigth;
872     unsigned int Depth;
873     unsigned int HorzRes;
874     unsigned int VertRes;
875   } WPGBitmapType1;
876
877   typedef struct
878   {
879     unsigned int Width;
880     unsigned int Heigth;
881     unsigned char Depth;
882     unsigned char Compression;
883   } WPG2BitmapType1;
884
885   typedef struct
886   {
887     unsigned int RotAngle;
888     unsigned int LowLeftX;
889     unsigned int LowLeftY;
890     unsigned int UpRightX;
891     unsigned int UpRightY;
892     unsigned int Width;
893     unsigned int Heigth;
894     unsigned int Depth;
895     unsigned int HorzRes;
896     unsigned int VertRes;
897   } WPGBitmapType2;
898
899   typedef struct
900   {
901     unsigned int StartIndex;
902     unsigned int NumOfEntries;
903   } WPGColorMapRec;
904
905   typedef struct {
906     size_t PS_unknown1;
907     unsigned int PS_unknown2;
908     unsigned int PS_unknown3;
909   } WPGPSl1Record;  
910
911   Image
912     *image,
913     *rotated_image;
914
915   unsigned int
916     status;
917
918   WPGHeader
919     Header;
920
921   WPGRecord
922     Rec;
923
924   WPG2Record
925     Rec2;
926
927   WPG2Start StartWPG;
928
929   WPGBitmapType1
930     BitmapHeader1;
931
932   WPG2BitmapType1
933     Bitmap2Header1;
934
935   WPGBitmapType2
936     BitmapHeader2;
937
938   WPGColorMapRec
939     WPG_Palette;
940
941   int
942     i,
943     bpp,
944     WPG2Flags;
945
946   ssize_t
947     ldblk;
948
949   size_t
950     one;
951
952   unsigned char
953     *BImgBuff;
954
955   tCTM CTM;         /*current transform matrix*/
956
957   /*
958     Open image file.
959   */
960   assert(image_info != (const ImageInfo *) NULL);
961   assert(image_info->signature == MagickSignature);
962   assert(exception != (ExceptionInfo *) NULL);
963   assert(exception->signature == MagickSignature);
964   one=1;
965   image=AcquireImage(image_info,exception);
966   image->depth=8;
967   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
968   if (status == MagickFalse)
969     {
970       image=DestroyImageList(image);
971       return((Image *) NULL);
972     }
973   /*
974     Read WPG image.
975   */
976   Header.FileId=ReadBlobLSBLong(image);
977   Header.DataOffset=(MagickOffsetType) ReadBlobLSBLong(image);
978   Header.ProductType=ReadBlobLSBShort(image);
979   Header.FileType=ReadBlobLSBShort(image);
980   Header.MajorVersion=ReadBlobByte(image);
981   Header.MinorVersion=ReadBlobByte(image);
982   Header.EncryptKey=ReadBlobLSBShort(image);
983   Header.Reserved=ReadBlobLSBShort(image);
984
985   if (Header.FileId!=0x435057FF || (Header.ProductType>>8)!=0x16)
986     ThrowReaderException(CorruptImageError,"ImproperImageHeader");
987   if (Header.EncryptKey!=0)
988     ThrowReaderException(CoderError,"EncryptedWPGImageFileNotSupported");
989
990   image->columns = 1;
991   image->rows = 1;
992   image->colors = 0;
993   bpp=0;
994   BitmapHeader2.RotAngle=0;
995
996   switch(Header.FileType)
997     {
998     case 1:     /* WPG level 1 */
999       while(!EOFBlob(image)) /* object parser loop */
1000         {
1001           (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
1002           if(EOFBlob(image))
1003             break;
1004
1005           Rec.RecType=(i=ReadBlobByte(image));
1006           if(i==EOF)
1007             break;
1008           Rd_WP_DWORD(image,&Rec.RecordLength);
1009           if(EOFBlob(image))
1010             break;
1011
1012           Header.DataOffset=TellBlob(image)+Rec.RecordLength;
1013
1014           switch(Rec.RecType)
1015             {
1016             case 0x0B: /* bitmap type 1 */
1017               BitmapHeader1.Width=ReadBlobLSBShort(image);
1018               BitmapHeader1.Heigth=ReadBlobLSBShort(image);
1019               BitmapHeader1.Depth=ReadBlobLSBShort(image);
1020               BitmapHeader1.HorzRes=ReadBlobLSBShort(image);
1021               BitmapHeader1.VertRes=ReadBlobLSBShort(image);
1022
1023               if(BitmapHeader1.HorzRes && BitmapHeader1.VertRes)
1024                 {
1025                   image->units=PixelsPerCentimeterResolution;
1026                   image->x_resolution=BitmapHeader1.HorzRes/470.0;
1027                   image->y_resolution=BitmapHeader1.VertRes/470.0;
1028                 }
1029               image->columns=BitmapHeader1.Width;
1030               image->rows=BitmapHeader1.Heigth;
1031               bpp=BitmapHeader1.Depth;
1032
1033               goto UnpackRaster;
1034
1035             case 0x0E:  /*Color palette */
1036               WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1037               WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1038
1039               image->colors=WPG_Palette.NumOfEntries;
1040               if (!AcquireImageColormap(image,image->colors,exception))
1041                 goto NoMemory;
1042               for (i=WPG_Palette.StartIndex;
1043                    i < (int)WPG_Palette.NumOfEntries; i++)
1044                 {
1045                   image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1046                     ReadBlobByte(image));
1047                   image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1048                     ReadBlobByte(image));
1049                   image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1050                     ReadBlobByte(image));
1051                 }
1052               break;
1053      
1054             case 0x11:  /* Start PS l1 */
1055               if(Rec.RecordLength > 8)
1056                 image=ExtractPostscript(image,image_info,
1057                   TellBlob(image)+8,   /* skip PS header in the wpg */
1058                   (ssize_t) Rec.RecordLength-8,exception);
1059               break;     
1060
1061             case 0x14:  /* bitmap type 2 */
1062               BitmapHeader2.RotAngle=ReadBlobLSBShort(image);
1063               BitmapHeader2.LowLeftX=ReadBlobLSBShort(image);
1064               BitmapHeader2.LowLeftY=ReadBlobLSBShort(image);
1065               BitmapHeader2.UpRightX=ReadBlobLSBShort(image);
1066               BitmapHeader2.UpRightY=ReadBlobLSBShort(image);
1067               BitmapHeader2.Width=ReadBlobLSBShort(image);
1068               BitmapHeader2.Heigth=ReadBlobLSBShort(image);
1069               BitmapHeader2.Depth=ReadBlobLSBShort(image);
1070               BitmapHeader2.HorzRes=ReadBlobLSBShort(image);
1071               BitmapHeader2.VertRes=ReadBlobLSBShort(image);
1072
1073               image->units=PixelsPerCentimeterResolution;
1074               image->page.width=(unsigned int)
1075                 ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightX)/470.0);
1076               image->page.height=(unsigned int)
1077                 ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightY)/470.0);
1078               image->page.x=(int) (BitmapHeader2.LowLeftX/470.0);
1079               image->page.y=(int) (BitmapHeader2.LowLeftX/470.0);
1080               if(BitmapHeader2.HorzRes && BitmapHeader2.VertRes)
1081                 {
1082                   image->x_resolution=BitmapHeader2.HorzRes/470.0;
1083                   image->y_resolution=BitmapHeader2.VertRes/470.0;
1084                 }
1085               image->columns=BitmapHeader2.Width;
1086               image->rows=BitmapHeader2.Heigth;
1087               bpp=BitmapHeader2.Depth;
1088
1089             UnpackRaster:      
1090               if ((image->colors == 0) && (bpp != 24))
1091                 {
1092                   image->colors=one << bpp;
1093                   if (!AcquireImageColormap(image,image->colors,exception))
1094                     {
1095                     NoMemory:
1096                       ThrowReaderException(ResourceLimitError,
1097                         "MemoryAllocationFailed");
1098                     }
1099                   /* printf("Load default colormap \n"); */
1100                   for (i=0; (i < (int) image->colors) && (i < 256); i++)
1101                     {               
1102                       image->colormap[i].red=ScaleCharToQuantum(WPG1_Palette[i].Red);
1103                       image->colormap[i].green=ScaleCharToQuantum(WPG1_Palette[i].Green);
1104                       image->colormap[i].blue=ScaleCharToQuantum(WPG1_Palette[i].Blue);
1105                     }
1106                 }
1107               else
1108                 {
1109                   if (bpp < 24)
1110                     if ( (image->colors < (one << bpp)) && (bpp != 24) )
1111                       image->colormap=(PixelInfo *) ResizeQuantumMemory(
1112                         image->colormap,(size_t) (one << bpp),
1113                         sizeof(*image->colormap));
1114                 }
1115           
1116               if (bpp == 1)
1117                 {
1118                   if(image->colormap[0].red==0 &&
1119                      image->colormap[0].green==0 &&
1120                      image->colormap[0].blue==0 &&
1121                      image->colormap[1].red==0 &&
1122                      image->colormap[1].green==0 &&
1123                      image->colormap[1].blue==0)
1124                     {  /* fix crippled monochrome palette */
1125                       image->colormap[1].red =
1126                         image->colormap[1].green =
1127                         image->colormap[1].blue = QuantumRange;
1128                     }
1129                 }      
1130
1131               if(UnpackWPGRaster(image,bpp) < 0)
1132                 /* The raster cannot be unpacked */
1133                 {
1134                 DecompressionFailed:
1135                   ThrowReaderException(CoderError,"UnableToDecompressImage");
1136                     }
1137
1138               if(Rec.RecType==0x14 && BitmapHeader2.RotAngle!=0 && !image_info->ping)
1139                 {  
1140                   /* flop command */
1141                   if(BitmapHeader2.RotAngle & 0x8000)
1142                     {
1143                       rotated_image = FlopImage(image, exception);
1144                       rotated_image->blob = image->blob;
1145                       DuplicateBlob(rotated_image,image);
1146                       (void) RemoveLastImageFromList(&image);
1147                       AppendImageToList(&image,rotated_image);
1148                     }
1149                   /* flip command */
1150                   if(BitmapHeader2.RotAngle & 0x2000)
1151                     {
1152                       rotated_image = FlipImage(image, exception);
1153                       rotated_image->blob = image->blob;
1154                       DuplicateBlob(rotated_image,image);
1155                       (void) RemoveLastImageFromList(&image);
1156                       AppendImageToList(&image,rotated_image);    
1157                     }
1158     
1159       /* rotate command */
1160                   if(BitmapHeader2.RotAngle & 0x0FFF)
1161                     {
1162                       rotated_image = RotateImage(image, (BitmapHeader2.RotAngle & 0x0FFF), exception);
1163                       rotated_image->blob = image->blob;
1164                       DuplicateBlob(rotated_image,image);
1165                       (void) RemoveLastImageFromList(&image);
1166                       AppendImageToList(&image,rotated_image);    
1167                     }                
1168                 }
1169
1170               /* Allocate next image structure. */
1171               AcquireNextImage(image_info,image,exception);
1172               image->depth=8;
1173               if (image->next == (Image *) NULL)
1174                 goto Finish;
1175               image=SyncNextImageInList(image);
1176               image->columns=image->rows=0;
1177               image->colors=0;
1178               break;
1179
1180             case 0x1B:  /* Postscript l2 */
1181               if(Rec.RecordLength>0x3C)
1182                 image=ExtractPostscript(image,image_info,
1183                   TellBlob(image)+0x3C,   /* skip PS l2 header in the wpg */
1184                   (ssize_t) Rec.RecordLength-0x3C,exception);
1185               break;
1186             }
1187         }
1188       break;
1189
1190     case 2:  /* WPG level 2 */
1191       (void) memset(CTM,0,sizeof(CTM));
1192       StartWPG.PosSizePrecision = 0;
1193       while(!EOFBlob(image)) /* object parser loop */
1194         {
1195           (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
1196           if(EOFBlob(image))
1197             break;
1198
1199           Rec2.Class=(i=ReadBlobByte(image));
1200           if(i==EOF)
1201             break;
1202           Rec2.RecType=(i=ReadBlobByte(image));
1203           if(i==EOF)
1204             break;
1205           Rd_WP_DWORD(image,&Rec2.Extension);
1206           Rd_WP_DWORD(image,&Rec2.RecordLength);
1207           if(EOFBlob(image))
1208             break;
1209
1210           Header.DataOffset=TellBlob(image)+Rec2.RecordLength;
1211
1212           switch(Rec2.RecType)
1213             {
1214       case 1:
1215               StartWPG.HorizontalUnits=ReadBlobLSBShort(image);
1216               StartWPG.VerticalUnits=ReadBlobLSBShort(image);
1217               StartWPG.PosSizePrecision=ReadBlobByte(image);
1218               break;
1219             case 0x0C:    /* Color palette */
1220               WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1221               WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1222
1223               image->colors=WPG_Palette.NumOfEntries;
1224               if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
1225                 ThrowReaderException(ResourceLimitError,
1226                   "MemoryAllocationFailed");
1227               for (i=WPG_Palette.StartIndex;
1228                    i < (int)WPG_Palette.NumOfEntries; i++)
1229                 {
1230                   image->colormap[i].red=ScaleCharToQuantum((char)
1231                     ReadBlobByte(image));
1232                   image->colormap[i].green=ScaleCharToQuantum((char)
1233                     ReadBlobByte(image));
1234                   image->colormap[i].blue=ScaleCharToQuantum((char)
1235                     ReadBlobByte(image));
1236                   (void) ReadBlobByte(image);   /*Opacity??*/
1237                 }
1238               break;
1239             case 0x0E:
1240               Bitmap2Header1.Width=ReadBlobLSBShort(image);
1241               Bitmap2Header1.Heigth=ReadBlobLSBShort(image);
1242               Bitmap2Header1.Depth=ReadBlobByte(image);
1243               Bitmap2Header1.Compression=ReadBlobByte(image);
1244
1245               if(Bitmap2Header1.Compression > 1)
1246                 continue; /*Unknown compression method */
1247               switch(Bitmap2Header1.Depth)
1248                 {
1249                 case 1:
1250                   bpp=1;
1251                   break;
1252                 case 2:
1253                   bpp=2;
1254                   break;
1255                 case 3:
1256                   bpp=4;
1257                   break;
1258                 case 4:
1259                   bpp=8;
1260                   break;
1261                 case 8:
1262                   bpp=24;
1263                   break;
1264                 default:
1265                   continue;  /*Ignore raster with unknown depth*/
1266                 }
1267               image->columns=Bitmap2Header1.Width;
1268               image->rows=Bitmap2Header1.Heigth;  
1269
1270               if ((image->colors == 0) && (bpp != 24))
1271                 {
1272                   size_t
1273                     one;
1274
1275                   one=1;
1276                   image->colors=one << bpp;
1277                   if (!AcquireImageColormap(image,image->colors,exception))
1278                     goto NoMemory;
1279                 }
1280               else
1281                 {
1282                   if(bpp < 24)
1283                     if( image->colors<(one << bpp) && bpp!=24 )
1284                       image->colormap=(PixelInfo *) ResizeQuantumMemory(
1285                        image->colormap,(size_t) (one << bpp),
1286                        sizeof(*image->colormap));
1287                 }
1288
1289
1290               switch(Bitmap2Header1.Compression)
1291                 {
1292                 case 0:    /*Uncompressed raster*/
1293                   {
1294                     ldblk=(ssize_t) ((bpp*image->columns+7)/8);
1295                     BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t)
1296                       ldblk,sizeof(*BImgBuff));
1297                     if (BImgBuff == (unsigned char *) NULL)
1298                       goto NoMemory;
1299
1300                     for(i=0; i< (ssize_t) image->rows; i++)
1301                       {
1302                         (void) ReadBlob(image,ldblk,BImgBuff);
1303                         InsertRow(BImgBuff,i,image,bpp);
1304                       }
1305
1306                     if(BImgBuff)
1307                       BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);;
1308                     break;
1309                   }
1310                 case 1:    /*RLE for WPG2 */
1311                   {
1312                     if( UnpackWPG2Raster(image,bpp) < 0)
1313                       goto DecompressionFailed;
1314                     break;
1315                   }   
1316                 }
1317
1318               if(CTM[0][0]<0 && !image_info->ping)
1319     {    /*?? RotAngle=360-RotAngle;*/
1320       rotated_image = FlopImage(image, exception);
1321       rotated_image->blob = image->blob;
1322       DuplicateBlob(rotated_image,image);
1323       (void) RemoveLastImageFromList(&image);
1324       AppendImageToList(&image,rotated_image);
1325                   /* Try to change CTM according to Flip - I am not sure, must be checked.      
1326                      Tx(0,0)=-1;      Tx(1,0)=0;   Tx(2,0)=0;
1327                      Tx(0,1)= 0;      Tx(1,1)=1;   Tx(2,1)=0;
1328                      Tx(0,2)=(WPG._2Rect.X_ur+WPG._2Rect.X_ll);
1329                      Tx(1,2)=0;   Tx(2,2)=1; */                  
1330                 }
1331               if(CTM[1][1]<0 && !image_info->ping)
1332     {    /*?? RotAngle=360-RotAngle;*/
1333       rotated_image = FlipImage(image, exception);
1334       rotated_image->blob = image->blob;
1335       DuplicateBlob(rotated_image,image);
1336       (void) RemoveLastImageFromList(&image);
1337       AppendImageToList(&image,rotated_image);
1338                   /* Try to change CTM according to Flip - I am not sure, must be checked.
1339                      float_matrix Tx(3,3);
1340                      Tx(0,0)= 1;   Tx(1,0)= 0;   Tx(2,0)=0;
1341                      Tx(0,1)= 0;   Tx(1,1)=-1;   Tx(2,1)=0;
1342                      Tx(0,2)= 0;   Tx(1,2)=(WPG._2Rect.Y_ur+WPG._2Rect.Y_ll);
1343                      Tx(2,2)=1; */      
1344     }    
1345     
1346
1347               /* Allocate next image structure. */
1348               AcquireNextImage(image_info,image,exception);
1349               image->depth=8;
1350               if (image->next == (Image *) NULL)
1351                 goto Finish;
1352               image=SyncNextImageInList(image);
1353               image->columns=image->rows=1;
1354               image->colors=0;
1355               break;
1356
1357             case 0x12:  /* Postscript WPG2*/
1358         i=ReadBlobLSBShort(image);
1359               if(Rec2.RecordLength > (unsigned int) i)
1360                 image=ExtractPostscript(image,image_info,
1361                   TellBlob(image)+i,    /*skip PS header in the wpg2*/
1362                   (ssize_t) (Rec2.RecordLength-i-2),exception);
1363               break;
1364
1365       case 0x1B:          /*bitmap rectangle*/
1366               WPG2Flags = LoadWPG2Flags(image,StartWPG.PosSizePrecision,NULL,&CTM);
1367               (void) WPG2Flags;
1368               break;
1369             }
1370         }
1371
1372       break;
1373
1374     default:
1375       {
1376          ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
1377       }
1378    }
1379
1380  Finish:
1381   (void) CloseBlob(image);
1382
1383   {
1384     Image
1385       *p;
1386
1387     ssize_t
1388       scene=0;
1389
1390     /*
1391       Rewind list, removing any empty images while rewinding.
1392     */
1393     p=image;
1394     image=NULL;
1395     while (p != (Image *) NULL)
1396       {
1397         Image *tmp=p;
1398         if ((p->rows == 0) || (p->columns == 0)) {
1399           p=p->previous;
1400           DeleteImageFromList(&tmp);
1401         } else {
1402           image=p;
1403           p=p->previous;
1404         }
1405       }
1406     /*
1407       Fix scene numbers.
1408     */
1409     for (p=image; p != (Image *) NULL; p=p->next)
1410       p->scene=(size_t) scene++;
1411   }
1412   if (image == (Image *) NULL)
1413     ThrowReaderException(CorruptImageError,
1414       "ImageFileDoesNotContainAnyImageData");
1415   return(image);
1416 }
1417 \f
1418 /*
1419 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1420 %                                                                             %
1421 %                                                                             %
1422 %                                                                             %
1423 %   R e g i s t e r W P G I m a g e                                           %
1424 %                                                                             %
1425 %                                                                             %
1426 %                                                                             %
1427 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1428 %
1429 %  Method RegisterWPGImage adds attributes for the WPG image format to
1430 %  the list of supported formats.  The attributes include the image format
1431 %  tag, a method to read and/or write the format, whether the format
1432 %  supports the saving of more than one frame to the same file or blob,
1433 %  whether the format supports native in-memory I/O, and a brief
1434 %  description of the format.
1435 %
1436 %  The format of the RegisterWPGImage method is:
1437 %
1438 %      size_t RegisterWPGImage(void)
1439 %
1440 */
1441 ModuleExport size_t RegisterWPGImage(void)
1442 {
1443   MagickInfo
1444     *entry;
1445
1446   entry=SetMagickInfo("WPG");
1447   entry->decoder=(DecodeImageHandler *) ReadWPGImage;
1448   entry->magick=(IsImageFormatHandler *) IsWPG;
1449   entry->description=AcquireString("Word Perfect Graphics");
1450   entry->module=ConstantString("WPG");
1451   entry->seekable_stream=MagickTrue;
1452   (void) RegisterMagickInfo(entry);
1453   return(MagickImageCoderSignature);
1454 }
1455 \f
1456 /*
1457 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1458 %                                                                             %
1459 %                                                                             %
1460 %                                                                             %
1461 %   U n r e g i s t e r W P G I m a g e                                       %
1462 %                                                                             %
1463 %                                                                             %
1464 %                                                                             %
1465 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1466 %
1467 %  Method UnregisterWPGImage removes format registrations made by the
1468 %  WPG module from the list of supported formats.
1469 %
1470 %  The format of the UnregisterWPGImage method is:
1471 %
1472 %      UnregisterWPGImage(void)
1473 %
1474 */
1475 ModuleExport void UnregisterWPGImage(void)
1476 {
1477   (void) UnregisterMagickInfo("WPG");
1478 }