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