2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 % Read WordPerfect Image Format %
19 % Copyright 1999-2017 ImageMagick Studio LLC, a non-profit organization %
20 % dedicated to making software imaging solutions freely available. %
22 % You may not use this file except in compliance with the License. You may %
23 % obtain a copy of the License at %
25 % https://www.imagemagick.org/script/license.php %
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. %
33 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
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*/
166 {32,0,64}, {48,0,64},
167 {64,0,64}, {64,0,48},
168 {64,0,32}, {64,0,16},
170 {64,32,0}, {64,48,0},
171 {64,64,0}, {48,64,0},
172 {32,64,0}, {16,64,0}, /*192*/
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},
204 {0,0,0}, {0,0,0} /*256*/
208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 % IsWPG() returns True if the image format type, identified by the magick
221 % The format of the IsWPG method is:
223 % unsigned int IsWPG(const unsigned char *magick,const size_t length)
225 % A description of each parameter follows:
227 % o status: Method IsWPG returns True if the image format type is WPG.
229 % o magick: compare image format pattern against these bytes.
231 % o length: Specifies the length of the magick string.
234 static unsigned int IsWPG(const unsigned char *magick,const size_t length)
238 if (memcmp(magick,"\377WPC",4) == 0)
244 static void Rd_WP_DWORD(Image *image,size_t *d)
249 b=ReadBlobByte(image);
253 b=ReadBlobByte(image);
255 b=ReadBlobByte(image);
259 *d=(*d & 0x7FFF) << 16;
260 b=ReadBlobByte(image);
262 b=ReadBlobByte(image);
267 static MagickBooleanType InsertRow(Image *image,unsigned char *p,ssize_t y,
268 int bpp,ExceptionInfo *exception)
282 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
283 if (q == (Quantum *) NULL)
287 case 1: /* Convert bitmap scanline. */
289 for (x=0; x < ((ssize_t) image->columns-7); x+=8)
291 for (bit=0; bit < 8; bit++)
293 index=((*p) & (0x80 >> bit) ? 0x01 : 0x00);
294 SetPixelIndex(image,index,q);
295 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
296 q+=GetPixelChannels(image);
300 if ((image->columns % 8) != 0)
302 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
304 index=((*p) & (0x80 >> bit) ? 0x01 : 0x00);
305 SetPixelIndex(image,index,q);
306 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
307 q+=GetPixelChannels(image);
313 case 2: /* Convert PseudoColor scanline. */
315 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
317 index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
318 SetPixelIndex(image,index,q);
319 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
320 q+=GetPixelChannels(image);
321 index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
322 SetPixelIndex(image,index,q);
323 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
324 q+=GetPixelChannels(image);
325 index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,exception);
326 SetPixelIndex(image,index,q);
327 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
328 q+=GetPixelChannels(image);
329 index=ConstrainColormapIndex(image,(*p) & 0x3,exception);
330 SetPixelIndex(image,index,q);
331 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
332 q+=GetPixelChannels(image);
335 if ((image->columns % 4) != 0)
337 index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
338 SetPixelIndex(image,index,q);
339 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
340 q+=GetPixelChannels(image);
341 if ((image->columns % 4) > 1)
343 index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
344 SetPixelIndex(image,index,q);
345 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
346 q+=GetPixelChannels(image);
347 if ((image->columns % 4) > 2)
349 index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,
351 SetPixelIndex(image,index,q);
352 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
353 q+=GetPixelChannels(image);
361 case 4: /* Convert PseudoColor scanline. */
363 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
365 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
366 SetPixelIndex(image,index,q);
367 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
368 q+=GetPixelChannels(image);
369 index=ConstrainColormapIndex(image,(*p) & 0x0f,exception);
370 SetPixelIndex(image,index,q);
371 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
373 q+=GetPixelChannels(image);
375 if ((image->columns % 2) != 0)
377 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
378 SetPixelIndex(image,index,q);
379 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
381 q+=GetPixelChannels(image);
385 case 8: /* Convert PseudoColor scanline. */
387 for (x=0; x < (ssize_t) image->columns; x++)
389 index=ConstrainColormapIndex(image,*p,exception);
390 SetPixelIndex(image,index,q);
391 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
393 q+=GetPixelChannels(image);
398 case 24: /* Convert DirectColor scanline. */
399 for (x=0; x < (ssize_t) image->columns; x++)
401 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
402 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
403 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
404 q+=GetPixelChannels(image);
408 if (!SyncAuthenticPixels(image,exception))
414 /* Helper for WPG1 raster reader. */
415 #define InsertByte(b) \
419 if((ssize_t) x>=ldblk) \
421 if (InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception) != MagickFalse) \
426 /* WPG1 raster reader. */
427 static int UnpackWPGRaster(Image *image,int bpp,ExceptionInfo *exception)
445 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
446 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
447 8*sizeof(*BImgBuff));
448 if(BImgBuff==NULL) return(-2);
450 while(y<(ssize_t) image->rows)
455 c=ReadBlobByte(image);
458 bbuf=(unsigned char) c;
459 RunCount=bbuf & 0x7F;
462 if(RunCount) /* repeat next byte runcount * */
464 bbuf=ReadBlobByte(image);
465 for(i=0;i<(int) RunCount;i++) InsertByte(bbuf);
467 else { /* read next byte as RunCount; repeat 0xFF runcount* */
468 c=ReadBlobByte(image);
471 RunCount=(unsigned char) c;
472 for(i=0;i<(int) RunCount;i++) InsertByte(0xFF);
476 if(RunCount) /* next runcount byte are readed directly */
478 for(i=0;i < (int) RunCount;i++)
480 bbuf=ReadBlobByte(image);
484 else { /* repeat previous line runcount* */
485 c=ReadBlobByte(image);
488 RunCount=(unsigned char) c;
489 if(x) { /* attempt to duplicate row from x position: */
490 /* I do not know what to do here */
491 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
494 for(i=0;i < (int) RunCount;i++)
497 y++; /* Here I need to duplicate previous row RUNCOUNT* */
499 if(y>(ssize_t) image->rows)
501 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
504 if (InsertRow(image,BImgBuff,y-1,bpp,exception) == MagickFalse)
506 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
512 if (EOFBlob(image) != MagickFalse)
515 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
516 return(y <(ssize_t) image->rows ? -5 : 0);
520 /* Helper for WPG2 reader. */
521 #define InsertByte6(b) \
523 DisableMSCWarning(4310) \
525 BImgBuff[x] = (unsigned char)~b;\
530 if((ssize_t) x >= ldblk) \
532 if (InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception) != MagickFalse) \
537 /* WPG2 raster reader. */
538 static int UnpackWPG2Raster(Image *image,int bpp,ExceptionInfo *exception)
558 SampleBuffer[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
562 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
563 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
568 while( y< image->rows)
570 bbuf=ReadBlobByte(image);
575 SampleSize=ReadBlobByte(image); /* DSZ */
578 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
583 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
588 (void) FormatLocaleFile(stderr,
589 "\nUnsupported WPG token XOR, please report!");
593 RunCount=ReadBlobByte(image); /* BLK */
596 for(i=0; i < SampleSize*(RunCount+1); i++)
602 RunCount=ReadBlobByte(image); /* EXT */
605 for(i=0; i<= RunCount;i++)
606 for(bbuf=0; bbuf < SampleSize; bbuf++)
607 InsertByte6(SampleBuffer[bbuf]);
610 RunCount=ReadBlobByte(image); /* RST */
615 (void) FormatLocaleFile(stderr,
616 "\nUnsupported WPG2 unaligned token RST x=%.20g, please report!\n"
618 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
622 /* duplicate the previous row RunCount x */
623 for(i=0;i<=RunCount;i++)
625 if (InsertRow(image,BImgBuff,(ssize_t) (image->rows > y ? y : image->rows-1),bpp,exception) != MagickFalse)
631 RunCount=ReadBlobByte(image); /* WHT */
634 for(i=0; i < SampleSize*(RunCount+1); i++)
640 RunCount=bbuf & 0x7F;
642 if(bbuf & 0x80) /* REP */
644 for(i=0; i < SampleSize; i++)
645 SampleBuffer[i]=ReadBlobByte(image);
646 for(i=0;i<=RunCount;i++)
647 for(bbuf=0;bbuf<SampleSize;bbuf++)
648 InsertByte6(SampleBuffer[bbuf]);
651 for(i=0; i< SampleSize*(RunCount+1);i++)
653 bbuf=ReadBlobByte(image);
658 if (EOFBlob(image) != MagickFalse)
661 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
666 typedef float tCTM[3][3];
668 static unsigned LoadWPG2Flags(Image *image,char Precision,float *Angle,tCTM *CTM)
670 const unsigned char TPR=1,TRN=2,SKW=4,SCL=8,ROT=0x10,OID=0x20,LCK=0x80;
675 (void) memset(*CTM,0,sizeof(*CTM)); /*CTM.erase();CTM.resize(3,3);*/
680 Flags=ReadBlobLSBShort(image);
681 if(Flags & LCK) (void) ReadBlobLSBLong(image); /*Edit lock*/
685 {(void) ReadBlobLSBShort(image);} /*ObjectID*/
687 {(void) ReadBlobLSBLong(image);} /*ObjectID (Double precision)*/
691 x=ReadBlobLSBLong(image); /*Rot Angle*/
692 if(Angle) *Angle=x/65536.0;
694 if(Flags & (ROT|SCL))
696 x=ReadBlobLSBLong(image); /*Sx*cos()*/
697 (*CTM)[0][0] = (float)x/0x10000;
698 x=ReadBlobLSBLong(image); /*Sy*cos()*/
699 (*CTM)[1][1] = (float)x/0x10000;
701 if(Flags & (ROT|SKW))
703 x=ReadBlobLSBLong(image); /*Kx*sin()*/
704 (*CTM)[1][0] = (float)x/0x10000;
705 x=ReadBlobLSBLong(image); /*Ky*sin()*/
706 (*CTM)[0][1] = (float)x/0x10000;
710 x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image); /*Tx*/
711 if(x>=0) (*CTM)[0][2] = (float)x+(float)DenX/0x10000;
712 else (*CTM)[0][2] = (float)x-(float)DenX/0x10000;
713 x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image); /*Ty*/
714 (*CTM)[1][2]=(float)x + ((x>=0)?1:-1)*(float)DenX/0x10000;
715 if(x>=0) (*CTM)[1][2] = (float)x+(float)DenX/0x10000;
716 else (*CTM)[1][2] = (float)x-(float)DenX/0x10000;
720 x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image); /*Px*/
721 (*CTM)[2][0] = x + (float)DenX/0x10000;;
722 x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image); /*Py*/
723 (*CTM)[2][1] = x + (float)DenX/0x10000;
729 static Image *ExtractPostscript(Image *image,const ImageInfo *image_info,
730 MagickOffsetType PS_Offset,ssize_t PS_Size,ExceptionInfo *exception)
733 postscript_file[MagickPathExtent];
751 magick[2*MagickPathExtent];
754 if ((clone_info=CloneImageInfo(image_info)) == NULL)
756 clone_info->blob=(void *) NULL;
757 clone_info->length=0;
759 /* Obtain temporary file */
760 (void) AcquireUniqueFilename(postscript_file);
761 ps_file=fopen_utf8(postscript_file,"wb");
762 if (ps_file == (FILE *) NULL)
765 /* Copy postscript to temporary file */
766 (void) SeekBlob(image,PS_Offset,SEEK_SET);
767 (void) ReadBlob(image, 2*MagickPathExtent, magick);
769 (void) SeekBlob(image,PS_Offset,SEEK_SET);
770 while (PS_Size-- > 0)
772 c=ReadBlobByte(image);
775 (void) fputc(c,ps_file);
777 (void) fclose(ps_file);
779 /* Detect file format - Check magic.mgk configuration file. */
780 magic_info=GetMagicInfo(magick,2*MagickPathExtent,exception);
781 if(magic_info == (const MagicInfo *) NULL) goto FINISH_UNL;
782 /* printf("Detected:%s \n",magic_info->name); */
783 if(exception->severity != UndefinedException) goto FINISH_UNL;
784 if(magic_info->name == (char *) NULL) goto FINISH_UNL;
786 (void) strncpy(clone_info->magick,magic_info->name,MagickPathExtent-1);
788 /* Read nested image */
789 /*FormatString(clone_info->filename,"%s:%s",magic_info->name,postscript_file);*/
790 FormatLocaleString(clone_info->filename,MagickPathExtent,"%s",postscript_file);
791 image2=ReadImage(clone_info,exception);
797 Replace current image with new image while copying base image
800 (void) CopyMagickString(image2->filename,image->filename,MagickPathExtent);
801 (void) CopyMagickString(image2->magick_filename,image->magick_filename,MagickPathExtent);
802 (void) CopyMagickString(image2->magick,image->magick,MagickPathExtent);
803 image2->depth=image->depth;
805 image2->blob=ReferenceBlob(image->blob);
807 if ((image->rows == 0) || (image->columns == 0))
808 DeleteImageFromList(&image);
810 AppendImageToList(&image,image2);
813 (void) RelinquishUniqueFileResource(postscript_file);
815 DestroyImageInfo(clone_info);
820 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
824 % R e a d W P G I m a g e %
828 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
830 % Method ReadWPGImage reads an WPG X image file and returns it. It
831 % allocates the memory necessary for the new Image structure and returns a
832 % pointer to the new image.
834 % The format of the ReadWPGImage method is:
836 % Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
838 % A description of each parameter follows:
840 % o image: Method ReadWPGImage returns a pointer to the image after
841 % reading. A null image is returned if there is a memory shortage or if
842 % the image cannot be read.
844 % o image_info: Specifies a pointer to a ImageInfo structure.
846 % o exception: return any errors or warnings in this structure.
849 static Image *ReadWPGImage(const ImageInfo *image_info,
850 ExceptionInfo *exception)
855 MagickOffsetType DataOffset;
856 unsigned int ProductType;
857 unsigned int FileType;
858 unsigned char MajorVersion;
859 unsigned char MinorVersion;
860 unsigned int EncryptKey;
861 unsigned int Reserved;
866 unsigned char RecType;
873 unsigned char RecType;
880 unsigned HorizontalUnits;
881 unsigned VerticalUnits;
882 unsigned char PosSizePrecision;
890 unsigned int HorzRes;
891 unsigned int VertRes;
899 unsigned char Compression;
904 unsigned int RotAngle;
905 unsigned int LowLeftX;
906 unsigned int LowLeftY;
907 unsigned int UpRightX;
908 unsigned int UpRightY;
912 unsigned int HorzRes;
913 unsigned int VertRes;
918 unsigned int StartIndex;
919 unsigned int NumOfEntries;
925 unsigned int PS_unknown2;
926 unsigned int PS_unknown3;
973 tCTM CTM; /*current transform matrix*/
978 assert(image_info != (const ImageInfo *) NULL);
979 assert(image_info->signature == MagickCoreSignature);
980 assert(exception != (ExceptionInfo *) NULL);
981 assert(exception->signature == MagickCoreSignature);
983 image=AcquireImage(image_info,exception);
985 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
986 if (status == MagickFalse)
988 image=DestroyImageList(image);
989 return((Image *) NULL);
994 Header.FileId=ReadBlobLSBLong(image);
995 Header.DataOffset=(MagickOffsetType) ReadBlobLSBLong(image);
996 Header.ProductType=ReadBlobLSBShort(image);
997 Header.FileType=ReadBlobLSBShort(image);
998 Header.MajorVersion=ReadBlobByte(image);
999 Header.MinorVersion=ReadBlobByte(image);
1000 Header.EncryptKey=ReadBlobLSBShort(image);
1001 Header.Reserved=ReadBlobLSBShort(image);
1003 if (Header.FileId!=0x435057FF || (Header.ProductType>>8)!=0x16)
1004 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1005 if (Header.EncryptKey!=0)
1006 ThrowReaderException(CoderError,"EncryptedWPGImageFileNotSupported");
1012 BitmapHeader2.RotAngle=0;
1013 Rec2.RecordLength=0;
1015 switch(Header.FileType)
1017 case 1: /* WPG level 1 */
1018 while(!EOFBlob(image)) /* object parser loop */
1020 (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
1024 Rec.RecType=(i=ReadBlobByte(image));
1027 Rd_WP_DWORD(image,&Rec.RecordLength);
1028 if (Rec.RecordLength > GetBlobSize(image))
1029 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1033 Header.DataOffset=TellBlob(image)+Rec.RecordLength;
1037 case 0x0B: /* bitmap type 1 */
1038 BitmapHeader1.Width=ReadBlobLSBShort(image);
1039 BitmapHeader1.Height=ReadBlobLSBShort(image);
1040 if ((BitmapHeader1.Width == 0) || (BitmapHeader1.Height == 0))
1041 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1042 BitmapHeader1.Depth=ReadBlobLSBShort(image);
1043 BitmapHeader1.HorzRes=ReadBlobLSBShort(image);
1044 BitmapHeader1.VertRes=ReadBlobLSBShort(image);
1046 if(BitmapHeader1.HorzRes && BitmapHeader1.VertRes)
1048 image->units=PixelsPerCentimeterResolution;
1049 image->resolution.x=BitmapHeader1.HorzRes/470.0;
1050 image->resolution.y=BitmapHeader1.VertRes/470.0;
1052 image->columns=BitmapHeader1.Width;
1053 image->rows=BitmapHeader1.Height;
1054 bpp=BitmapHeader1.Depth;
1058 case 0x0E: /*Color palette */
1059 WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1060 WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1061 if ((WPG_Palette.NumOfEntries-WPG_Palette.StartIndex) >
1062 (Rec2.RecordLength-2-2)/3)
1063 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1064 if (WPG_Palette.StartIndex > WPG_Palette.NumOfEntries)
1065 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1066 image->colors=WPG_Palette.NumOfEntries;
1067 if (!AcquireImageColormap(image,image->colors,exception))
1069 for (i=WPG_Palette.StartIndex;
1070 i < (int)WPG_Palette.NumOfEntries; i++)
1072 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1073 ReadBlobByte(image));
1074 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1075 ReadBlobByte(image));
1076 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1077 ReadBlobByte(image));
1081 case 0x11: /* Start PS l1 */
1082 if(Rec.RecordLength > 8)
1083 image=ExtractPostscript(image,image_info,
1084 TellBlob(image)+8, /* skip PS header in the wpg */
1085 (ssize_t) Rec.RecordLength-8,exception);
1088 case 0x14: /* bitmap type 2 */
1089 BitmapHeader2.RotAngle=ReadBlobLSBShort(image);
1090 BitmapHeader2.LowLeftX=ReadBlobLSBShort(image);
1091 BitmapHeader2.LowLeftY=ReadBlobLSBShort(image);
1092 BitmapHeader2.UpRightX=ReadBlobLSBShort(image);
1093 BitmapHeader2.UpRightY=ReadBlobLSBShort(image);
1094 BitmapHeader2.Width=ReadBlobLSBShort(image);
1095 BitmapHeader2.Height=ReadBlobLSBShort(image);
1096 if ((BitmapHeader2.Width == 0) || (BitmapHeader2.Height == 0))
1097 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1098 BitmapHeader2.Depth=ReadBlobLSBShort(image);
1099 BitmapHeader2.HorzRes=ReadBlobLSBShort(image);
1100 BitmapHeader2.VertRes=ReadBlobLSBShort(image);
1102 image->units=PixelsPerCentimeterResolution;
1103 image->page.width=(unsigned int)
1104 ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightX)/470.0);
1105 image->page.height=(unsigned int)
1106 ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightY)/470.0);
1107 image->page.x=(int) (BitmapHeader2.LowLeftX/470.0);
1108 image->page.y=(int) (BitmapHeader2.LowLeftX/470.0);
1109 if(BitmapHeader2.HorzRes && BitmapHeader2.VertRes)
1111 image->resolution.x=BitmapHeader2.HorzRes/470.0;
1112 image->resolution.y=BitmapHeader2.VertRes/470.0;
1114 image->columns=BitmapHeader2.Width;
1115 image->rows=BitmapHeader2.Height;
1116 bpp=BitmapHeader2.Depth;
1119 status=SetImageExtent(image,image->columns,image->rows,exception);
1120 if (status == MagickFalse)
1122 if ((image->storage_class != PseudoClass) && (bpp != 24))
1124 image->colors=one << bpp;
1125 if (!AcquireImageColormap(image,image->colors,exception))
1128 ThrowReaderException(ResourceLimitError,
1129 "MemoryAllocationFailed");
1131 /* printf("Load default colormap \n"); */
1132 for (i=0; (i < (int) image->colors) && (i < 256); i++)
1134 image->colormap[i].red=ScaleCharToQuantum(WPG1_Palette[i].Red);
1135 image->colormap[i].green=ScaleCharToQuantum(WPG1_Palette[i].Green);
1136 image->colormap[i].blue=ScaleCharToQuantum(WPG1_Palette[i].Blue);
1142 if ( (image->colors < (one << bpp)) && (bpp != 24) )
1143 image->colormap=(PixelInfo *) ResizeQuantumMemory(
1144 image->colormap,(size_t) (one << bpp),
1145 sizeof(*image->colormap));
1150 if(image->colormap[0].red==0 &&
1151 image->colormap[0].green==0 &&
1152 image->colormap[0].blue==0 &&
1153 image->colormap[1].red==0 &&
1154 image->colormap[1].green==0 &&
1155 image->colormap[1].blue==0)
1156 { /* fix crippled monochrome palette */
1157 image->colormap[1].red =
1158 image->colormap[1].green =
1159 image->colormap[1].blue = QuantumRange;
1163 if(UnpackWPGRaster(image,bpp,exception) < 0)
1164 /* The raster cannot be unpacked */
1166 DecompressionFailed:
1167 ThrowReaderException(CoderError,"UnableToDecompressImage");
1170 if(Rec.RecType==0x14 && BitmapHeader2.RotAngle!=0 && !image_info->ping)
1173 if(BitmapHeader2.RotAngle & 0x8000)
1178 flop_image = FlopImage(image, exception);
1179 if (flop_image != (Image *) NULL) {
1180 DuplicateBlob(flop_image,image);
1181 ReplaceImageInList(&image,flop_image);
1185 if(BitmapHeader2.RotAngle & 0x2000)
1190 flip_image = FlipImage(image, exception);
1191 if (flip_image != (Image *) NULL) {
1192 DuplicateBlob(flip_image,image);
1193 ReplaceImageInList(&image,flip_image);
1196 /* rotate command */
1197 if(BitmapHeader2.RotAngle & 0x0FFF)
1202 rotate_image=RotateImage(image,(BitmapHeader2.RotAngle &
1203 0x0FFF), exception);
1204 if (rotate_image != (Image *) NULL) {
1205 DuplicateBlob(rotate_image,image);
1206 ReplaceImageInList(&image,rotate_image);
1211 /* Allocate next image structure. */
1212 AcquireNextImage(image_info,image,exception);
1214 if (image->next == (Image *) NULL)
1216 image=SyncNextImageInList(image);
1217 image->columns=image->rows=1;
1221 case 0x1B: /* Postscript l2 */
1222 if(Rec.RecordLength>0x3C)
1223 image=ExtractPostscript(image,image_info,
1224 TellBlob(image)+0x3C, /* skip PS l2 header in the wpg */
1225 (ssize_t) Rec.RecordLength-0x3C,exception);
1231 case 2: /* WPG level 2 */
1232 (void) memset(CTM,0,sizeof(CTM));
1233 StartWPG.PosSizePrecision = 0;
1234 while(!EOFBlob(image)) /* object parser loop */
1236 (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
1240 Rec2.Class=(i=ReadBlobByte(image));
1243 Rec2.RecType=(i=ReadBlobByte(image));
1246 Rd_WP_DWORD(image,&Rec2.Extension);
1247 Rd_WP_DWORD(image,&Rec2.RecordLength);
1251 Header.DataOffset=TellBlob(image)+Rec2.RecordLength;
1253 switch(Rec2.RecType)
1256 StartWPG.HorizontalUnits=ReadBlobLSBShort(image);
1257 StartWPG.VerticalUnits=ReadBlobLSBShort(image);
1258 StartWPG.PosSizePrecision=ReadBlobByte(image);
1260 case 0x0C: /* Color palette */
1261 WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1262 WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1263 if ((WPG_Palette.NumOfEntries-WPG_Palette.StartIndex) >
1264 (Rec2.RecordLength-2-2) / 3)
1265 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1266 image->colors=WPG_Palette.NumOfEntries;
1267 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
1268 ThrowReaderException(ResourceLimitError,
1269 "MemoryAllocationFailed");
1270 for (i=WPG_Palette.StartIndex;
1271 i < (int)WPG_Palette.NumOfEntries; i++)
1273 image->colormap[i].red=ScaleCharToQuantum((char)
1274 ReadBlobByte(image));
1275 image->colormap[i].green=ScaleCharToQuantum((char)
1276 ReadBlobByte(image));
1277 image->colormap[i].blue=ScaleCharToQuantum((char)
1278 ReadBlobByte(image));
1279 (void) ReadBlobByte(image); /*Opacity??*/
1283 Bitmap2Header1.Width=ReadBlobLSBShort(image);
1284 Bitmap2Header1.Height=ReadBlobLSBShort(image);
1285 if ((Bitmap2Header1.Width == 0) || (Bitmap2Header1.Height == 0))
1286 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1287 Bitmap2Header1.Depth=ReadBlobByte(image);
1288 Bitmap2Header1.Compression=ReadBlobByte(image);
1290 if(Bitmap2Header1.Compression > 1)
1291 continue; /*Unknown compression method */
1292 switch(Bitmap2Header1.Depth)
1310 continue; /*Ignore raster with unknown depth*/
1312 image->columns=Bitmap2Header1.Width;
1313 image->rows=Bitmap2Header1.Height;
1314 status=SetImageExtent(image,image->columns,image->rows,exception);
1315 if (status == MagickFalse)
1317 if ((image->colors == 0) && (bpp != 24))
1319 image->colors=one << bpp;
1320 if (!AcquireImageColormap(image,image->colors,exception))
1326 if( image->colors<(one << bpp) && bpp!=24 )
1327 image->colormap=(PixelInfo *) ResizeQuantumMemory(
1328 image->colormap,(size_t) (one << bpp),
1329 sizeof(*image->colormap));
1333 switch(Bitmap2Header1.Compression)
1335 case 0: /*Uncompressed raster*/
1337 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
1338 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t)
1339 ldblk+1,sizeof(*BImgBuff));
1340 if (BImgBuff == (unsigned char *) NULL)
1343 for (i=0; i< (ssize_t) image->rows; i++)
1345 (void) ReadBlob(image,ldblk,BImgBuff);
1346 if (InsertRow(image,BImgBuff,i,bpp,exception) == MagickFalse)
1349 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
1350 goto DecompressionFailed;
1355 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
1358 case 1: /*RLE for WPG2 */
1360 if( UnpackWPG2Raster(image,bpp,exception) < 0)
1361 goto DecompressionFailed;
1366 if(CTM[0][0]<0 && !image_info->ping)
1367 { /*?? RotAngle=360-RotAngle;*/
1371 flop_image = FlopImage(image, exception);
1372 if (flop_image != (Image *) NULL) {
1373 DuplicateBlob(flop_image,image);
1374 ReplaceImageInList(&image,flop_image);
1376 /* Try to change CTM according to Flip - I am not sure, must be checked.
1377 Tx(0,0)=-1; Tx(1,0)=0; Tx(2,0)=0;
1378 Tx(0,1)= 0; Tx(1,1)=1; Tx(2,1)=0;
1379 Tx(0,2)=(WPG._2Rect.X_ur+WPG._2Rect.X_ll);
1380 Tx(1,2)=0; Tx(2,2)=1; */
1382 if(CTM[1][1]<0 && !image_info->ping)
1383 { /*?? RotAngle=360-RotAngle;*/
1387 flip_image = FlipImage(image, exception);
1388 if (flip_image != (Image *) NULL) {
1389 DuplicateBlob(flip_image,image);
1390 ReplaceImageInList(&image,flip_image);
1392 /* Try to change CTM according to Flip - I am not sure, must be checked.
1393 float_matrix Tx(3,3);
1394 Tx(0,0)= 1; Tx(1,0)= 0; Tx(2,0)=0;
1395 Tx(0,1)= 0; Tx(1,1)=-1; Tx(2,1)=0;
1396 Tx(0,2)= 0; Tx(1,2)=(WPG._2Rect.Y_ur+WPG._2Rect.Y_ll);
1401 /* Allocate next image structure. */
1402 AcquireNextImage(image_info,image,exception);
1404 if (image->next == (Image *) NULL)
1406 image=SyncNextImageInList(image);
1407 image->columns=image->rows=1;
1411 case 0x12: /* Postscript WPG2*/
1412 i=ReadBlobLSBShort(image);
1413 if(Rec2.RecordLength > (unsigned int) i)
1414 image=ExtractPostscript(image,image_info,
1415 TellBlob(image)+i, /*skip PS header in the wpg2*/
1416 (ssize_t) (Rec2.RecordLength-i-2),exception);
1419 case 0x1B: /*bitmap rectangle*/
1420 WPG2Flags = LoadWPG2Flags(image,StartWPG.PosSizePrecision,NULL,&CTM);
1430 ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
1435 (void) CloseBlob(image);
1445 Rewind list, removing any empty images while rewinding.
1449 while (p != (Image *) NULL)
1452 if ((p->rows == 0) || (p->columns == 0)) {
1454 DeleteImageFromList(&tmp);
1463 for (p=image; p != (Image *) NULL; p=p->next)
1464 p->scene=(size_t) scene++;
1466 if (image == (Image *) NULL)
1467 ThrowReaderException(CorruptImageError,
1468 "ImageFileDoesNotContainAnyImageData");
1473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477 % R e g i s t e r W P G I m a g e %
1481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1483 % Method RegisterWPGImage adds attributes for the WPG image format to
1484 % the list of supported formats. The attributes include the image format
1485 % tag, a method to read and/or write the format, whether the format
1486 % supports the saving of more than one frame to the same file or blob,
1487 % whether the format supports native in-memory I/O, and a brief
1488 % description of the format.
1490 % The format of the RegisterWPGImage method is:
1492 % size_t RegisterWPGImage(void)
1495 ModuleExport size_t RegisterWPGImage(void)
1500 entry=AcquireMagickInfo("WPG","WPG","Word Perfect Graphics");
1501 entry->decoder=(DecodeImageHandler *) ReadWPGImage;
1502 entry->magick=(IsImageFormatHandler *) IsWPG;
1503 entry->flags|=CoderDecoderSeekableStreamFlag;
1504 (void) RegisterMagickInfo(entry);
1505 return(MagickImageCoderSignature);
1509 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1513 % U n r e g i s t e r W P G I m a g e %
1517 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1519 % Method UnregisterWPGImage removes format registrations made by the
1520 % WPG module from the list of supported formats.
1522 % The format of the UnregisterWPGImage method is:
1524 % UnregisterWPGImage(void)
1527 ModuleExport void UnregisterWPGImage(void)
1529 (void) UnregisterMagickInfo("WPG");