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 void InsertRow(Image *image,unsigned char *p,ssize_t y,int bpp,
268 ExceptionInfo *exception)
284 case 1: /* Convert bitmap scanline. */
286 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
287 if (q == (Quantum *) NULL)
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);
311 if (!SyncAuthenticPixels(image,exception))
315 case 2: /* Convert PseudoColor scanline. */
317 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
318 if (q == (Quantum *) NULL)
320 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
322 index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
323 SetPixelIndex(image,index,q);
324 SetPixelViaPixelInfo(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 SetPixelViaPixelInfo(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 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
333 q+=GetPixelChannels(image);
334 index=ConstrainColormapIndex(image,(*p) & 0x3,exception);
335 SetPixelIndex(image,index,q);
336 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
337 q+=GetPixelChannels(image);
340 if ((image->columns % 4) != 0)
342 index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
343 SetPixelIndex(image,index,q);
344 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
345 q+=GetPixelChannels(image);
346 if ((image->columns % 4) > 1)
348 index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
349 SetPixelIndex(image,index,q);
350 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
351 q+=GetPixelChannels(image);
352 if ((image->columns % 4) > 2)
354 index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,
356 SetPixelIndex(image,index,q);
357 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
358 q+=GetPixelChannels(image);
363 if (SyncAuthenticPixels(image,exception) == MagickFalse)
368 case 4: /* Convert PseudoColor scanline. */
370 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
371 if (q == (Quantum *) NULL)
373 for (x=0; x < ((ssize_t) image->columns-1); x+=2)
375 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
376 SetPixelIndex(image,index,q);
377 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
378 q+=GetPixelChannels(image);
379 index=ConstrainColormapIndex(image,(*p) & 0x0f,exception);
380 SetPixelIndex(image,index,q);
381 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
383 q+=GetPixelChannels(image);
385 if ((image->columns % 2) != 0)
387 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
388 SetPixelIndex(image,index,q);
389 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
391 q+=GetPixelChannels(image);
393 if (SyncAuthenticPixels(image,exception) == MagickFalse)
397 case 8: /* Convert PseudoColor scanline. */
399 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
400 if (q == (Quantum *) NULL) break;
402 for (x=0; x < (ssize_t) image->columns; x++)
404 index=ConstrainColormapIndex(image,*p,exception);
405 SetPixelIndex(image,index,q);
406 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
408 q+=GetPixelChannels(image);
410 if (SyncAuthenticPixels(image,exception) == MagickFalse)
415 case 24: /* Convert DirectColor scanline. */
416 q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
417 if (q == (Quantum *) NULL)
419 for (x=0; x < (ssize_t) image->columns; x++)
421 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
422 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
423 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
424 q+=GetPixelChannels(image);
426 if (!SyncAuthenticPixels(image,exception))
433 /* Helper for WPG1 raster reader. */
434 #define InsertByte(b) \
438 if((ssize_t) x>=ldblk) \
440 InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception); \
445 /* WPG1 raster reader. */
446 static int UnpackWPGRaster(Image *image,int bpp,ExceptionInfo *exception)
464 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
465 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
466 8*sizeof(*BImgBuff));
467 if(BImgBuff==NULL) return(-2);
469 while(y<(ssize_t) image->rows)
474 c=ReadBlobByte(image);
477 bbuf=(unsigned char) c;
478 RunCount=bbuf & 0x7F;
481 if(RunCount) /* repeat next byte runcount * */
483 bbuf=ReadBlobByte(image);
484 for(i=0;i<(int) RunCount;i++) InsertByte(bbuf);
486 else { /* read next byte as RunCount; repeat 0xFF runcount* */
487 c=ReadBlobByte(image);
490 RunCount=(unsigned char) c;
491 for(i=0;i<(int) RunCount;i++) InsertByte(0xFF);
495 if(RunCount) /* next runcount byte are readed directly */
497 for(i=0;i < (int) RunCount;i++)
499 bbuf=ReadBlobByte(image);
503 else { /* repeat previous line runcount* */
504 c=ReadBlobByte(image);
507 RunCount=(unsigned char) c;
508 if(x) { /* attempt to duplicate row from x position: */
509 /* I do not know what to do here */
510 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
513 for(i=0;i < (int) RunCount;i++)
516 y++; /* Here I need to duplicate previous row RUNCOUNT* */
518 if(y>(ssize_t) image->rows)
520 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
523 InsertRow(image,BImgBuff,y-1,bpp,exception);
527 if (EOFBlob(image) != MagickFalse)
530 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
531 return(y <(ssize_t) image->rows ? -5 : 0);
535 /* Helper for WPG2 reader. */
536 #define InsertByte6(b) \
538 DisableMSCWarning(4310) \
540 BImgBuff[x] = (unsigned char)~b;\
545 if((ssize_t) x >= ldblk) \
547 InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception); \
552 /* WPG2 raster reader. */
553 static int UnpackWPG2Raster(Image *image,int bpp,ExceptionInfo *exception)
573 SampleBuffer[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
577 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
578 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
583 while( y< image->rows)
585 bbuf=ReadBlobByte(image);
590 SampleSize=ReadBlobByte(image); /* DSZ */
593 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
598 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
603 (void) FormatLocaleFile(stderr,
604 "\nUnsupported WPG token XOR, please report!");
608 RunCount=ReadBlobByte(image); /* BLK */
611 for(i=0; i < SampleSize*(RunCount+1); i++)
617 RunCount=ReadBlobByte(image); /* EXT */
620 for(i=0; i<= RunCount;i++)
621 for(bbuf=0; bbuf < SampleSize; bbuf++)
622 InsertByte6(SampleBuffer[bbuf]);
625 RunCount=ReadBlobByte(image); /* RST */
630 (void) FormatLocaleFile(stderr,
631 "\nUnsupported WPG2 unaligned token RST x=%.20g, please report!\n"
633 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
637 /* duplicate the previous row RunCount x */
638 for(i=0;i<=RunCount;i++)
640 InsertRow(image,BImgBuff,(ssize_t) (image->rows >= y ? y : image->rows-1),
647 RunCount=ReadBlobByte(image); /* WHT */
650 for(i=0; i < SampleSize*(RunCount+1); i++)
656 RunCount=bbuf & 0x7F;
658 if(bbuf & 0x80) /* REP */
660 for(i=0; i < SampleSize; i++)
661 SampleBuffer[i]=ReadBlobByte(image);
662 for(i=0;i<=RunCount;i++)
663 for(bbuf=0;bbuf<SampleSize;bbuf++)
664 InsertByte6(SampleBuffer[bbuf]);
667 for(i=0; i< SampleSize*(RunCount+1);i++)
669 bbuf=ReadBlobByte(image);
674 if (EOFBlob(image) != MagickFalse)
677 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
682 typedef float tCTM[3][3];
684 static unsigned LoadWPG2Flags(Image *image,char Precision,float *Angle,tCTM *CTM)
686 const unsigned char TPR=1,TRN=2,SKW=4,SCL=8,ROT=0x10,OID=0x20,LCK=0x80;
691 (void) memset(*CTM,0,sizeof(*CTM)); /*CTM.erase();CTM.resize(3,3);*/
696 Flags=ReadBlobLSBShort(image);
697 if(Flags & LCK) (void) ReadBlobLSBLong(image); /*Edit lock*/
701 {(void) ReadBlobLSBShort(image);} /*ObjectID*/
703 {(void) ReadBlobLSBLong(image);} /*ObjectID (Double precision)*/
707 x=ReadBlobLSBLong(image); /*Rot Angle*/
708 if(Angle) *Angle=x/65536.0;
710 if(Flags & (ROT|SCL))
712 x=ReadBlobLSBLong(image); /*Sx*cos()*/
713 (*CTM)[0][0] = (float)x/0x10000;
714 x=ReadBlobLSBLong(image); /*Sy*cos()*/
715 (*CTM)[1][1] = (float)x/0x10000;
717 if(Flags & (ROT|SKW))
719 x=ReadBlobLSBLong(image); /*Kx*sin()*/
720 (*CTM)[1][0] = (float)x/0x10000;
721 x=ReadBlobLSBLong(image); /*Ky*sin()*/
722 (*CTM)[0][1] = (float)x/0x10000;
726 x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image); /*Tx*/
727 if(x>=0) (*CTM)[0][2] = (float)x+(float)DenX/0x10000;
728 else (*CTM)[0][2] = (float)x-(float)DenX/0x10000;
729 x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image); /*Ty*/
730 (*CTM)[1][2]=(float)x + ((x>=0)?1:-1)*(float)DenX/0x10000;
731 if(x>=0) (*CTM)[1][2] = (float)x+(float)DenX/0x10000;
732 else (*CTM)[1][2] = (float)x-(float)DenX/0x10000;
736 x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image); /*Px*/
737 (*CTM)[2][0] = x + (float)DenX/0x10000;;
738 x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image); /*Py*/
739 (*CTM)[2][1] = x + (float)DenX/0x10000;
745 static Image *ExtractPostscript(Image *image,const ImageInfo *image_info,
746 MagickOffsetType PS_Offset,ssize_t PS_Size,ExceptionInfo *exception)
749 postscript_file[MagickPathExtent];
764 magick[2*MagickPathExtent];
767 if ((clone_info=CloneImageInfo(image_info)) == NULL)
769 clone_info->blob=(void *) NULL;
770 clone_info->length=0;
772 /* Obtain temporary file */
773 (void) AcquireUniqueFilename(postscript_file);
774 ps_file=fopen_utf8(postscript_file,"wb");
775 if (ps_file == (FILE *) NULL)
778 /* Copy postscript to temporary file */
779 (void) SeekBlob(image,PS_Offset,SEEK_SET);
780 (void) ReadBlob(image, 2*MagickPathExtent, magick);
782 (void) SeekBlob(image,PS_Offset,SEEK_SET);
785 (void) fputc(ReadBlobByte(image),ps_file);
787 (void) fclose(ps_file);
789 /* Detect file format - Check magic.mgk configuration file. */
790 magic_info=GetMagicInfo(magick,2*MagickPathExtent,exception);
791 if(magic_info == (const MagicInfo *) NULL) goto FINISH_UNL;
792 /* printf("Detected:%s \n",magic_info->name); */
793 if(exception->severity != UndefinedException) goto FINISH_UNL;
794 if(magic_info->name == (char *) NULL) goto FINISH_UNL;
796 (void) strncpy(clone_info->magick,magic_info->name,MagickPathExtent-1);
798 /* Read nested image */
799 /*FormatString(clone_info->filename,"%s:%s",magic_info->name,postscript_file);*/
800 FormatLocaleString(clone_info->filename,MagickPathExtent,"%s",postscript_file);
801 image2=ReadImage(clone_info,exception);
807 Replace current image with new image while copying base image
810 (void) CopyMagickString(image2->filename,image->filename,MagickPathExtent);
811 (void) CopyMagickString(image2->magick_filename,image->magick_filename,MagickPathExtent);
812 (void) CopyMagickString(image2->magick,image->magick,MagickPathExtent);
813 image2->depth=image->depth;
815 image2->blob=ReferenceBlob(image->blob);
817 if ((image->rows == 0) || (image->columns == 0))
818 DeleteImageFromList(&image);
820 AppendImageToList(&image,image2);
823 (void) RelinquishUniqueFileResource(postscript_file);
825 DestroyImageInfo(clone_info);
830 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
834 % R e a d W P G I m a g e %
838 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
840 % Method ReadWPGImage reads an WPG X image file and returns it. It
841 % allocates the memory necessary for the new Image structure and returns a
842 % pointer to the new image.
844 % The format of the ReadWPGImage method is:
846 % Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
848 % A description of each parameter follows:
850 % o image: Method ReadWPGImage returns a pointer to the image after
851 % reading. A null image is returned if there is a memory shortage or if
852 % the image cannot be read.
854 % o image_info: Specifies a pointer to a ImageInfo structure.
856 % o exception: return any errors or warnings in this structure.
859 static Image *ReadWPGImage(const ImageInfo *image_info,
860 ExceptionInfo *exception)
865 MagickOffsetType DataOffset;
866 unsigned int ProductType;
867 unsigned int FileType;
868 unsigned char MajorVersion;
869 unsigned char MinorVersion;
870 unsigned int EncryptKey;
871 unsigned int Reserved;
876 unsigned char RecType;
883 unsigned char RecType;
890 unsigned HorizontalUnits;
891 unsigned VerticalUnits;
892 unsigned char PosSizePrecision;
900 unsigned int HorzRes;
901 unsigned int VertRes;
909 unsigned char Compression;
914 unsigned int RotAngle;
915 unsigned int LowLeftX;
916 unsigned int LowLeftY;
917 unsigned int UpRightX;
918 unsigned int UpRightY;
922 unsigned int HorzRes;
923 unsigned int VertRes;
928 unsigned int StartIndex;
929 unsigned int NumOfEntries;
935 unsigned int PS_unknown2;
936 unsigned int PS_unknown3;
983 tCTM CTM; /*current transform matrix*/
988 assert(image_info != (const ImageInfo *) NULL);
989 assert(image_info->signature == MagickCoreSignature);
990 assert(exception != (ExceptionInfo *) NULL);
991 assert(exception->signature == MagickCoreSignature);
993 image=AcquireImage(image_info,exception);
995 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
996 if (status == MagickFalse)
998 image=DestroyImageList(image);
999 return((Image *) NULL);
1004 Header.FileId=ReadBlobLSBLong(image);
1005 Header.DataOffset=(MagickOffsetType) ReadBlobLSBLong(image);
1006 Header.ProductType=ReadBlobLSBShort(image);
1007 Header.FileType=ReadBlobLSBShort(image);
1008 Header.MajorVersion=ReadBlobByte(image);
1009 Header.MinorVersion=ReadBlobByte(image);
1010 Header.EncryptKey=ReadBlobLSBShort(image);
1011 Header.Reserved=ReadBlobLSBShort(image);
1013 if (Header.FileId!=0x435057FF || (Header.ProductType>>8)!=0x16)
1014 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1015 if (Header.EncryptKey!=0)
1016 ThrowReaderException(CoderError,"EncryptedWPGImageFileNotSupported");
1022 BitmapHeader2.RotAngle=0;
1023 Rec2.RecordLength=0;
1025 switch(Header.FileType)
1027 case 1: /* WPG level 1 */
1028 while(!EOFBlob(image)) /* object parser loop */
1030 (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
1034 Rec.RecType=(i=ReadBlobByte(image));
1037 Rd_WP_DWORD(image,&Rec.RecordLength);
1041 Header.DataOffset=TellBlob(image)+Rec.RecordLength;
1045 case 0x0B: /* bitmap type 1 */
1046 BitmapHeader1.Width=ReadBlobLSBShort(image);
1047 BitmapHeader1.Height=ReadBlobLSBShort(image);
1048 if ((BitmapHeader1.Width == 0) || (BitmapHeader1.Height == 0))
1049 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1050 BitmapHeader1.Depth=ReadBlobLSBShort(image);
1051 BitmapHeader1.HorzRes=ReadBlobLSBShort(image);
1052 BitmapHeader1.VertRes=ReadBlobLSBShort(image);
1054 if(BitmapHeader1.HorzRes && BitmapHeader1.VertRes)
1056 image->units=PixelsPerCentimeterResolution;
1057 image->resolution.x=BitmapHeader1.HorzRes/470.0;
1058 image->resolution.y=BitmapHeader1.VertRes/470.0;
1060 image->columns=BitmapHeader1.Width;
1061 image->rows=BitmapHeader1.Height;
1062 bpp=BitmapHeader1.Depth;
1066 case 0x0E: /*Color palette */
1067 WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1068 WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1069 if ((WPG_Palette.NumOfEntries-WPG_Palette.StartIndex) >
1070 (Rec2.RecordLength-2-2) / 3)
1071 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1072 image->colors=WPG_Palette.NumOfEntries;
1073 if (!AcquireImageColormap(image,image->colors,exception))
1075 for (i=WPG_Palette.StartIndex;
1076 i < (int)WPG_Palette.NumOfEntries; i++)
1078 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1079 ReadBlobByte(image));
1080 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1081 ReadBlobByte(image));
1082 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1083 ReadBlobByte(image));
1087 case 0x11: /* Start PS l1 */
1088 if(Rec.RecordLength > 8)
1089 image=ExtractPostscript(image,image_info,
1090 TellBlob(image)+8, /* skip PS header in the wpg */
1091 (ssize_t) Rec.RecordLength-8,exception);
1094 case 0x14: /* bitmap type 2 */
1095 BitmapHeader2.RotAngle=ReadBlobLSBShort(image);
1096 BitmapHeader2.LowLeftX=ReadBlobLSBShort(image);
1097 BitmapHeader2.LowLeftY=ReadBlobLSBShort(image);
1098 BitmapHeader2.UpRightX=ReadBlobLSBShort(image);
1099 BitmapHeader2.UpRightY=ReadBlobLSBShort(image);
1100 BitmapHeader2.Width=ReadBlobLSBShort(image);
1101 BitmapHeader2.Height=ReadBlobLSBShort(image);
1102 if ((BitmapHeader2.Width == 0) || (BitmapHeader2.Height == 0))
1103 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1104 BitmapHeader2.Depth=ReadBlobLSBShort(image);
1105 BitmapHeader2.HorzRes=ReadBlobLSBShort(image);
1106 BitmapHeader2.VertRes=ReadBlobLSBShort(image);
1108 image->units=PixelsPerCentimeterResolution;
1109 image->page.width=(unsigned int)
1110 ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightX)/470.0);
1111 image->page.height=(unsigned int)
1112 ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightY)/470.0);
1113 image->page.x=(int) (BitmapHeader2.LowLeftX/470.0);
1114 image->page.y=(int) (BitmapHeader2.LowLeftX/470.0);
1115 if(BitmapHeader2.HorzRes && BitmapHeader2.VertRes)
1117 image->resolution.x=BitmapHeader2.HorzRes/470.0;
1118 image->resolution.y=BitmapHeader2.VertRes/470.0;
1120 image->columns=BitmapHeader2.Width;
1121 image->rows=BitmapHeader2.Height;
1122 bpp=BitmapHeader2.Depth;
1125 status=SetImageExtent(image,image->columns,image->rows,exception);
1126 if (status == MagickFalse)
1128 if ((image->colors == 0) && (bpp != 24))
1130 image->colors=one << bpp;
1131 if (!AcquireImageColormap(image,image->colors,exception))
1134 ThrowReaderException(ResourceLimitError,
1135 "MemoryAllocationFailed");
1137 /* printf("Load default colormap \n"); */
1138 for (i=0; (i < (int) image->colors) && (i < 256); i++)
1140 image->colormap[i].red=ScaleCharToQuantum(WPG1_Palette[i].Red);
1141 image->colormap[i].green=ScaleCharToQuantum(WPG1_Palette[i].Green);
1142 image->colormap[i].blue=ScaleCharToQuantum(WPG1_Palette[i].Blue);
1148 if ( (image->colors < (one << bpp)) && (bpp != 24) )
1149 image->colormap=(PixelInfo *) ResizeQuantumMemory(
1150 image->colormap,(size_t) (one << bpp),
1151 sizeof(*image->colormap));
1156 if(image->colormap[0].red==0 &&
1157 image->colormap[0].green==0 &&
1158 image->colormap[0].blue==0 &&
1159 image->colormap[1].red==0 &&
1160 image->colormap[1].green==0 &&
1161 image->colormap[1].blue==0)
1162 { /* fix crippled monochrome palette */
1163 image->colormap[1].red =
1164 image->colormap[1].green =
1165 image->colormap[1].blue = QuantumRange;
1169 if(UnpackWPGRaster(image,bpp,exception) < 0)
1170 /* The raster cannot be unpacked */
1172 DecompressionFailed:
1173 ThrowReaderException(CoderError,"UnableToDecompressImage");
1176 if(Rec.RecType==0x14 && BitmapHeader2.RotAngle!=0 && !image_info->ping)
1179 if(BitmapHeader2.RotAngle & 0x8000)
1184 flop_image = FlopImage(image, exception);
1185 if (flop_image != (Image *) NULL) {
1186 DuplicateBlob(flop_image,image);
1187 ReplaceImageInList(&image,flop_image);
1191 if(BitmapHeader2.RotAngle & 0x2000)
1196 flip_image = FlipImage(image, exception);
1197 if (flip_image != (Image *) NULL) {
1198 DuplicateBlob(flip_image,image);
1199 ReplaceImageInList(&image,flip_image);
1202 /* rotate command */
1203 if(BitmapHeader2.RotAngle & 0x0FFF)
1208 rotate_image=RotateImage(image,(BitmapHeader2.RotAngle &
1209 0x0FFF), exception);
1210 if (rotate_image != (Image *) NULL) {
1211 DuplicateBlob(rotate_image,image);
1212 ReplaceImageInList(&image,rotate_image);
1217 /* Allocate next image structure. */
1218 AcquireNextImage(image_info,image,exception);
1220 if (image->next == (Image *) NULL)
1222 image=SyncNextImageInList(image);
1223 image->columns=image->rows=1;
1227 case 0x1B: /* Postscript l2 */
1228 if(Rec.RecordLength>0x3C)
1229 image=ExtractPostscript(image,image_info,
1230 TellBlob(image)+0x3C, /* skip PS l2 header in the wpg */
1231 (ssize_t) Rec.RecordLength-0x3C,exception);
1237 case 2: /* WPG level 2 */
1238 (void) memset(CTM,0,sizeof(CTM));
1239 StartWPG.PosSizePrecision = 0;
1240 while(!EOFBlob(image)) /* object parser loop */
1242 (void) SeekBlob(image,Header.DataOffset,SEEK_SET);
1246 Rec2.Class=(i=ReadBlobByte(image));
1249 Rec2.RecType=(i=ReadBlobByte(image));
1252 Rd_WP_DWORD(image,&Rec2.Extension);
1253 Rd_WP_DWORD(image,&Rec2.RecordLength);
1257 Header.DataOffset=TellBlob(image)+Rec2.RecordLength;
1259 switch(Rec2.RecType)
1262 StartWPG.HorizontalUnits=ReadBlobLSBShort(image);
1263 StartWPG.VerticalUnits=ReadBlobLSBShort(image);
1264 StartWPG.PosSizePrecision=ReadBlobByte(image);
1266 case 0x0C: /* Color palette */
1267 WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1268 WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1269 if ((WPG_Palette.NumOfEntries-WPG_Palette.StartIndex) >
1270 (Rec2.RecordLength-2-2) / 3)
1271 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1272 image->colors=WPG_Palette.NumOfEntries;
1273 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
1274 ThrowReaderException(ResourceLimitError,
1275 "MemoryAllocationFailed");
1276 for (i=WPG_Palette.StartIndex;
1277 i < (int)WPG_Palette.NumOfEntries; i++)
1279 image->colormap[i].red=ScaleCharToQuantum((char)
1280 ReadBlobByte(image));
1281 image->colormap[i].green=ScaleCharToQuantum((char)
1282 ReadBlobByte(image));
1283 image->colormap[i].blue=ScaleCharToQuantum((char)
1284 ReadBlobByte(image));
1285 (void) ReadBlobByte(image); /*Opacity??*/
1289 Bitmap2Header1.Width=ReadBlobLSBShort(image);
1290 Bitmap2Header1.Height=ReadBlobLSBShort(image);
1291 if ((Bitmap2Header1.Width == 0) || (Bitmap2Header1.Height == 0))
1292 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1293 Bitmap2Header1.Depth=ReadBlobByte(image);
1294 Bitmap2Header1.Compression=ReadBlobByte(image);
1296 if(Bitmap2Header1.Compression > 1)
1297 continue; /*Unknown compression method */
1298 switch(Bitmap2Header1.Depth)
1316 continue; /*Ignore raster with unknown depth*/
1318 image->columns=Bitmap2Header1.Width;
1319 image->rows=Bitmap2Header1.Height;
1320 status=SetImageExtent(image,image->columns,image->rows,exception);
1321 if (status == MagickFalse)
1323 if ((image->colors == 0) && (bpp != 24))
1325 image->colors=one << bpp;
1326 if (!AcquireImageColormap(image,image->colors,exception))
1332 if( image->colors<(one << bpp) && bpp!=24 )
1333 image->colormap=(PixelInfo *) ResizeQuantumMemory(
1334 image->colormap,(size_t) (one << bpp),
1335 sizeof(*image->colormap));
1339 switch(Bitmap2Header1.Compression)
1341 case 0: /*Uncompressed raster*/
1343 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
1344 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t)
1345 ldblk+1,sizeof(*BImgBuff));
1346 if (BImgBuff == (unsigned char *) NULL)
1349 for(i=0; i< (ssize_t) image->rows; i++)
1351 (void) ReadBlob(image,ldblk,BImgBuff);
1352 InsertRow(image,BImgBuff,i,bpp,exception);
1356 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
1359 case 1: /*RLE for WPG2 */
1361 if( UnpackWPG2Raster(image,bpp,exception) < 0)
1362 goto DecompressionFailed;
1367 if(CTM[0][0]<0 && !image_info->ping)
1368 { /*?? RotAngle=360-RotAngle;*/
1372 flop_image = FlopImage(image, exception);
1373 if (flop_image != (Image *) NULL) {
1374 DuplicateBlob(flop_image,image);
1375 ReplaceImageInList(&image,flop_image);
1377 /* Try to change CTM according to Flip - I am not sure, must be checked.
1378 Tx(0,0)=-1; Tx(1,0)=0; Tx(2,0)=0;
1379 Tx(0,1)= 0; Tx(1,1)=1; Tx(2,1)=0;
1380 Tx(0,2)=(WPG._2Rect.X_ur+WPG._2Rect.X_ll);
1381 Tx(1,2)=0; Tx(2,2)=1; */
1383 if(CTM[1][1]<0 && !image_info->ping)
1384 { /*?? RotAngle=360-RotAngle;*/
1388 flip_image = FlipImage(image, exception);
1389 if (flip_image != (Image *) NULL) {
1390 DuplicateBlob(flip_image,image);
1391 ReplaceImageInList(&image,flip_image);
1393 /* Try to change CTM according to Flip - I am not sure, must be checked.
1394 float_matrix Tx(3,3);
1395 Tx(0,0)= 1; Tx(1,0)= 0; Tx(2,0)=0;
1396 Tx(0,1)= 0; Tx(1,1)=-1; Tx(2,1)=0;
1397 Tx(0,2)= 0; Tx(1,2)=(WPG._2Rect.Y_ur+WPG._2Rect.Y_ll);
1402 /* Allocate next image structure. */
1403 AcquireNextImage(image_info,image,exception);
1405 if (image->next == (Image *) NULL)
1407 image=SyncNextImageInList(image);
1408 image->columns=image->rows=1;
1412 case 0x12: /* Postscript WPG2*/
1413 i=ReadBlobLSBShort(image);
1414 if(Rec2.RecordLength > (unsigned int) i)
1415 image=ExtractPostscript(image,image_info,
1416 TellBlob(image)+i, /*skip PS header in the wpg2*/
1417 (ssize_t) (Rec2.RecordLength-i-2),exception);
1420 case 0x1B: /*bitmap rectangle*/
1421 WPG2Flags = LoadWPG2Flags(image,StartWPG.PosSizePrecision,NULL,&CTM);
1431 ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
1436 (void) CloseBlob(image);
1446 Rewind list, removing any empty images while rewinding.
1450 while (p != (Image *) NULL)
1453 if ((p->rows == 0) || (p->columns == 0)) {
1455 DeleteImageFromList(&tmp);
1464 for (p=image; p != (Image *) NULL; p=p->next)
1465 p->scene=(size_t) scene++;
1467 if (image == (Image *) NULL)
1468 ThrowReaderException(CorruptImageError,
1469 "ImageFileDoesNotContainAnyImageData");
1474 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1478 % R e g i s t e r W P G I m a g e %
1482 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1484 % Method RegisterWPGImage adds attributes for the WPG image format to
1485 % the list of supported formats. The attributes include the image format
1486 % tag, a method to read and/or write the format, whether the format
1487 % supports the saving of more than one frame to the same file or blob,
1488 % whether the format supports native in-memory I/O, and a brief
1489 % description of the format.
1491 % The format of the RegisterWPGImage method is:
1493 % size_t RegisterWPGImage(void)
1496 ModuleExport size_t RegisterWPGImage(void)
1501 entry=AcquireMagickInfo("WPG","WPG","Word Perfect Graphics");
1502 entry->decoder=(DecodeImageHandler *) ReadWPGImage;
1503 entry->magick=(IsImageFormatHandler *) IsWPG;
1504 entry->flags|=CoderDecoderSeekableStreamFlag;
1505 (void) RegisterMagickInfo(entry);
1506 return(MagickImageCoderSignature);
1510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1514 % U n r e g i s t e r W P G I m a g e %
1518 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1520 % Method UnregisterWPGImage removes format registrations made by the
1521 % WPG module from the list of supported formats.
1523 % The format of the UnregisterWPGImage method is:
1525 % UnregisterWPGImage(void)
1528 ModuleExport void UnregisterWPGImage(void)
1530 (void) UnregisterMagickInfo("WPG");