2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12 % Read WordPerfect Image Format %
19 % Copyright 1999-2019 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://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 if (index < image->colors)
296 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
297 q+=GetPixelChannels(image);
301 if ((image->columns % 8) != 0)
303 for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
305 index=((*p) & (0x80 >> bit) ? 0x01 : 0x00);
306 SetPixelIndex(image,index,q);
307 if (index < image->colors)
308 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
309 q+=GetPixelChannels(image);
315 case 2: /* Convert PseudoColor scanline. */
317 for (x=0; x < ((ssize_t) image->columns-3); x+=4)
319 index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
320 SetPixelIndex(image,index,q);
321 if (index < image->colors)
322 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
323 q+=GetPixelChannels(image);
324 index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
325 SetPixelIndex(image,index,q);
326 if (index < image->colors)
327 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
328 q+=GetPixelChannels(image);
329 index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,exception);
330 SetPixelIndex(image,index,q);
331 if (index < image->colors)
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 if (index < image->colors)
337 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
338 q+=GetPixelChannels(image);
341 if ((image->columns % 4) != 0)
343 index=ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
344 SetPixelIndex(image,index,q);
345 if (index < image->colors)
346 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
347 q+=GetPixelChannels(image);
348 if ((image->columns % 4) > 1)
350 index=ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
351 SetPixelIndex(image,index,q);
352 if (index < image->colors)
353 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
354 q+=GetPixelChannels(image);
355 if ((image->columns % 4) > 2)
357 index=ConstrainColormapIndex(image,(*p >> 2) & 0x3,
359 SetPixelIndex(image,index,q);
360 if (index < image->colors)
361 SetPixelViaPixelInfo(image,image->colormap+(ssize_t)
363 q+=GetPixelChannels(image);
371 case 4: /* Convert PseudoColor scanline. */
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 if (index < image->colors)
378 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
379 q+=GetPixelChannels(image);
380 index=ConstrainColormapIndex(image,(*p) & 0x0f,exception);
381 SetPixelIndex(image,index,q);
382 if (index < image->colors)
383 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
385 q+=GetPixelChannels(image);
387 if ((image->columns % 2) != 0)
389 index=ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
390 SetPixelIndex(image,index,q);
391 if (index < image->colors)
392 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
394 q+=GetPixelChannels(image);
398 case 8: /* Convert PseudoColor scanline. */
400 for (x=0; x < (ssize_t) image->columns; x++)
402 index=ConstrainColormapIndex(image,*p,exception);
403 SetPixelIndex(image,index,q);
404 if (index < image->colors)
405 SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
407 q+=GetPixelChannels(image);
412 case 24: /* Convert DirectColor scanline. */
413 for (x=0; x < (ssize_t) image->columns; x++)
415 SetPixelRed(image,ScaleCharToQuantum(*p++),q);
416 SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
417 SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
418 q+=GetPixelChannels(image);
422 if (!SyncAuthenticPixels(image,exception))
428 /* Helper for WPG1 raster reader. */
429 #define InsertByte(b) \
433 if((ssize_t) x>=ldblk) \
435 if (InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception) != MagickFalse) \
440 /* WPG1 raster reader. */
441 static int UnpackWPGRaster(Image *image,int bpp,ExceptionInfo *exception)
459 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
460 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
461 8*sizeof(*BImgBuff));
462 if(BImgBuff==NULL) return(-2);
463 (void) memset(BImgBuff,0,(size_t) ldblk*8*sizeof(*BImgBuff));
465 while(y<(ssize_t) image->rows)
470 c=ReadBlobByte(image);
473 bbuf=(unsigned char) c;
474 RunCount=bbuf & 0x7F;
477 if(RunCount) /* repeat next byte runcount * */
479 bbuf=ReadBlobByte(image);
480 for(i=0;i<(int) RunCount;i++) InsertByte(bbuf);
482 else { /* read next byte as RunCount; repeat 0xFF runcount* */
483 c=ReadBlobByte(image);
486 RunCount=(unsigned char) c;
487 for(i=0;i<(int) RunCount;i++) InsertByte(0xFF);
491 if(RunCount) /* next runcount byte are readed directly */
493 for(i=0;i < (int) RunCount;i++)
495 c=ReadBlobByte(image);
501 else { /* repeat previous line runcount* */
502 c=ReadBlobByte(image);
505 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
508 RunCount=(unsigned char) c;
509 if(x) { /* attempt to duplicate row from x position: */
510 /* I do not know what to do here */
511 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
514 for(i=0;i < (int) RunCount;i++)
517 y++; /* Here I need to duplicate previous row RUNCOUNT* */
519 if(y>(ssize_t) image->rows)
521 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
524 if (InsertRow(image,BImgBuff,y-1,bpp,exception) == MagickFalse)
526 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
532 if (EOFBlob(image) != MagickFalse)
535 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
536 return(y <(ssize_t) image->rows ? -5 : 0);
540 /* Helper for WPG2 reader. */
541 #define InsertByte6(b) \
543 DisableMSCWarning(4310) \
545 BImgBuff[x] = (unsigned char)~b;\
550 if((ssize_t) x >= ldblk) \
552 if (InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception) != MagickFalse) \
557 /* WPG2 raster reader. */
558 static int UnpackWPG2Raster(Image *image,int bpp,ExceptionInfo *exception)
578 SampleBuffer[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
582 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
583 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
587 (void) memset(BImgBuff,0,ldblk*sizeof(*BImgBuff));
589 while( y< image->rows)
591 bbuf=ReadBlobByte(image);
596 SampleSize=ReadBlobByte(image); /* DSZ */
599 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
604 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
610 (void) FormatLocaleFile(stderr,
611 "\nUnsupported WPG token XOR, please report!");
615 RunCount=ReadBlobByte(image); /* BLK */
618 for(i=0; i < SampleSize*(RunCount+1); i++)
624 RunCount=ReadBlobByte(image); /* EXT */
627 for(i=0; i<= RunCount;i++)
628 for(bbuf=0; bbuf < SampleSize; bbuf++)
629 InsertByte6(SampleBuffer[bbuf]);
632 RunCount=ReadBlobByte(image); /* RST */
637 (void) FormatLocaleFile(stderr,
638 "\nUnsupported WPG2 unaligned token RST x=%.20g, please report!\n"
640 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
644 /* duplicate the previous row RunCount x */
645 for(i=0;i<=RunCount;i++)
647 if (InsertRow(image,BImgBuff,(ssize_t) (image->rows > y ? y : image->rows-1),bpp,exception) != MagickFalse)
653 RunCount=ReadBlobByte(image); /* WHT */
656 for(i=0; i < SampleSize*(RunCount+1); i++)
662 RunCount=bbuf & 0x7F;
664 if(bbuf & 0x80) /* REP */
666 for(i=0; i < SampleSize; i++)
667 SampleBuffer[i]=ReadBlobByte(image);
668 for(i=0;i<=RunCount;i++)
669 for(bbuf=0;bbuf<SampleSize;bbuf++)
670 InsertByte6(SampleBuffer[bbuf]);
673 for(i=0; i< SampleSize*(RunCount+1);i++)
675 bbuf=ReadBlobByte(image);
680 if (EOFBlob(image) != MagickFalse)
683 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
688 typedef float tCTM[3][3];
690 static unsigned LoadWPG2Flags(Image *image,char Precision,float *Angle,tCTM *CTM)
692 const unsigned char TPR=1,TRN=2,SKW=4,SCL=8,ROT=0x10,OID=0x20,LCK=0x80;
697 (void) memset(*CTM,0,sizeof(*CTM)); /*CTM.erase();CTM.resize(3,3);*/
702 Flags=ReadBlobLSBShort(image);
703 if(Flags & LCK) (void) ReadBlobLSBLong(image); /*Edit lock*/
707 {(void) ReadBlobLSBShort(image);} /*ObjectID*/
709 {(void) ReadBlobLSBLong(image);} /*ObjectID (Double precision)*/
713 x=ReadBlobLSBLong(image); /*Rot Angle*/
714 if(Angle) *Angle=x/65536.0;
716 if(Flags & (ROT|SCL))
718 x=ReadBlobLSBLong(image); /*Sx*cos()*/
719 (*CTM)[0][0] = (float)x/0x10000;
720 x=ReadBlobLSBLong(image); /*Sy*cos()*/
721 (*CTM)[1][1] = (float)x/0x10000;
723 if(Flags & (ROT|SKW))
725 x=ReadBlobLSBLong(image); /*Kx*sin()*/
726 (*CTM)[1][0] = (float)x/0x10000;
727 x=ReadBlobLSBLong(image); /*Ky*sin()*/
728 (*CTM)[0][1] = (float)x/0x10000;
732 x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image); /*Tx*/
733 if(x>=0) (*CTM)[0][2] = (float)x+(float)DenX/0x10000;
734 else (*CTM)[0][2] = (float)x-(float)DenX/0x10000;
735 x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image); /*Ty*/
736 (*CTM)[1][2]=(float)x + ((x>=0)?1:-1)*(float)DenX/0x10000;
737 if(x>=0) (*CTM)[1][2] = (float)x+(float)DenX/0x10000;
738 else (*CTM)[1][2] = (float)x-(float)DenX/0x10000;
742 x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image); /*Px*/
743 (*CTM)[2][0] = x + (float)DenX/0x10000;;
744 x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image); /*Py*/
745 (*CTM)[2][1] = x + (float)DenX/0x10000;
751 static Image *ExtractPostscript(Image *image,const ImageInfo *image_info,
752 MagickOffsetType PS_Offset,ssize_t PS_Size,ExceptionInfo *exception)
755 postscript_file[MagickPathExtent];
776 magick[2*MagickPathExtent];
781 if ((clone_info=CloneImageInfo(image_info)) == NULL)
783 clone_info->blob=(void *) NULL;
784 clone_info->length=0;
787 /* Obtain temporary file */
788 (void) AcquireUniqueFilename(postscript_file);
789 ps_file=fopen_utf8(postscript_file,"wb");
790 if (ps_file == (FILE *) NULL)
793 /* Copy postscript to temporary file */
794 if (SeekBlob(image,PS_Offset,SEEK_SET) != PS_Offset)
796 (void) fclose(ps_file);
797 ThrowException(exception,CorruptImageError,"ImproperImageHeader",
801 count=ReadBlob(image, 2*MagickPathExtent, magick);
804 (void) fclose(ps_file);
805 ThrowException(exception,CorruptImageError,"ImproperImageHeader",
810 if (SeekBlob(image,PS_Offset,SEEK_SET) != PS_Offset)
812 (void) fclose(ps_file);
813 ThrowException(exception,CorruptImageError,"ImproperImageHeader",
817 while (PS_Size-- > 0)
819 c=ReadBlobByte(image);
822 (void) fclose(ps_file);
823 ThrowException(exception,CorruptImageError,"ImproperImageHeader",
827 (void) fputc(c,ps_file);
829 (void) fclose(ps_file);
831 /* Detect file format - Check magic.mgk configuration file. */
832 magic_info=GetMagicInfo(magick,count,exception);
833 if(magic_info == (const MagicInfo *) NULL) goto FINISH_UNL;
834 /* printf("Detected:%s \n",magic_info->name); */
835 if(exception->severity != UndefinedException) goto FINISH_UNL;
836 (void) strncpy(clone_info->magick,GetMagicName(magic_info),
838 if (LocaleCompare(clone_info->magick,"PFB") != 0)
840 ThrowException(exception,CorruptImageError,"ImproperImageHeader",
845 /* Read nested image */
846 /*FormatString(clone_info->filename,"%s:%s",magic_info->name,postscript_file);*/
847 FormatLocaleString(clone_info->filename,MagickPathExtent,"%.1024s:%.1024s",
848 clone_info->magick,postscript_file);
849 image2=ReadImage(clone_info,exception);
853 if(exception->severity>=ErrorException)
856 DestroyImageList(image2);
865 Replace current image with new image while copying base image attributes.
870 (void) CopyMagickString(p->filename,image->filename,MagickPathExtent);
871 (void) CopyMagickString(p->magick_filename,image->magick_filename,
873 (void) CopyMagickString(p->magick,image->magick,MagickPathExtent);
874 if ((p->rows == 0) || (p->columns == 0))
876 DeleteImageFromList(&p);
877 if (p == (Image *) NULL)
879 image2=(Image *) NULL;
886 p->blob=ReferenceBlob(image->blob);
889 } while (p != (Image *) NULL);
892 if ((image->rows == 0 || image->columns == 0) &&
893 (image->previous != NULL || image->next != NULL))
895 DeleteImageFromList(&image);
898 AppendImageToList(&image,image2);
899 while (image->next != NULL)
904 (void) RelinquishUniqueFileResource(postscript_file);
906 DestroyImageInfo(clone_info);
907 if (status == MagickFalse)
908 return(DestroyImageList(image));
913 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
917 % R e a d W P G I m a g e %
921 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
923 % Method ReadWPGImage reads an WPG X image file and returns it. It
924 % allocates the memory necessary for the new Image structure and returns a
925 % pointer to the new image.
927 % The format of the ReadWPGImage method is:
929 % Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
931 % A description of each parameter follows:
933 % o image: Method ReadWPGImage returns a pointer to the image after
934 % reading. A null image is returned if there is a memory shortage or if
935 % the image cannot be read.
937 % o image_info: Specifies a pointer to a ImageInfo structure.
939 % o exception: return any errors or warnings in this structure.
942 static Image *ReadWPGImage(const ImageInfo *image_info,
943 ExceptionInfo *exception)
948 MagickOffsetType DataOffset;
949 unsigned int ProductType;
950 unsigned int FileType;
951 unsigned char MajorVersion;
952 unsigned char MinorVersion;
953 unsigned int EncryptKey;
954 unsigned int Reserved;
959 unsigned char RecType;
966 unsigned char RecType;
973 unsigned HorizontalUnits;
974 unsigned VerticalUnits;
975 unsigned char PosSizePrecision;
983 unsigned int HorzRes;
984 unsigned int VertRes;
992 unsigned char Compression;
997 unsigned int RotAngle;
998 unsigned int LowLeftX;
999 unsigned int LowLeftY;
1000 unsigned int UpRightX;
1001 unsigned int UpRightY;
1003 unsigned int Height;
1005 unsigned int HorzRes;
1006 unsigned int VertRes;
1011 unsigned int StartIndex;
1012 unsigned int NumOfEntries;
1018 unsigned int PS_unknown2;
1019 unsigned int PS_unknown3;
1066 tCTM CTM; /*current transform matrix*/
1071 assert(image_info != (const ImageInfo *) NULL);
1072 assert(image_info->signature == MagickCoreSignature);
1073 assert(exception != (ExceptionInfo *) NULL);
1074 assert(exception->signature == MagickCoreSignature);
1076 image=AcquireImage(image_info,exception);
1078 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1079 if (status == MagickFalse)
1081 image=DestroyImageList(image);
1082 return((Image *) NULL);
1087 Header.FileId=ReadBlobLSBLong(image);
1088 Header.DataOffset=(MagickOffsetType) ReadBlobLSBLong(image);
1089 Header.ProductType=ReadBlobLSBShort(image);
1090 Header.FileType=ReadBlobLSBShort(image);
1091 Header.MajorVersion=ReadBlobByte(image);
1092 Header.MinorVersion=ReadBlobByte(image);
1093 Header.EncryptKey=ReadBlobLSBShort(image);
1094 Header.Reserved=ReadBlobLSBShort(image);
1096 if (Header.FileId!=0x435057FF || (Header.ProductType>>8)!=0x16)
1097 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1098 if (Header.EncryptKey!=0)
1099 ThrowReaderException(CoderError,"EncryptedWPGImageFileNotSupported");
1104 image->storage_class=DirectClass;
1105 (void) ResetImagePixels(image,exception);
1107 BitmapHeader2.RotAngle=0;
1108 Rec2.RecordLength=0;
1110 switch(Header.FileType)
1112 case 1: /* WPG level 1 */
1113 while(!EOFBlob(image)) /* object parser loop */
1115 if (SeekBlob(image,Header.DataOffset,SEEK_SET) != Header.DataOffset)
1120 Rec.RecType=(i=ReadBlobByte(image));
1123 Rd_WP_DWORD(image,&Rec.RecordLength);
1124 if (Rec.RecordLength > GetBlobSize(image))
1125 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1129 Header.DataOffset=TellBlob(image)+Rec.RecordLength;
1133 case 0x0B: /* bitmap type 1 */
1134 BitmapHeader1.Width=ReadBlobLSBShort(image);
1135 BitmapHeader1.Height=ReadBlobLSBShort(image);
1136 if ((BitmapHeader1.Width == 0) || (BitmapHeader1.Height == 0))
1137 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1138 BitmapHeader1.Depth=ReadBlobLSBShort(image);
1139 BitmapHeader1.HorzRes=ReadBlobLSBShort(image);
1140 BitmapHeader1.VertRes=ReadBlobLSBShort(image);
1142 if(BitmapHeader1.HorzRes && BitmapHeader1.VertRes)
1144 image->units=PixelsPerCentimeterResolution;
1145 image->resolution.x=BitmapHeader1.HorzRes/470.0;
1146 image->resolution.y=BitmapHeader1.VertRes/470.0;
1148 image->columns=BitmapHeader1.Width;
1149 image->rows=BitmapHeader1.Height;
1150 bpp=BitmapHeader1.Depth;
1154 case 0x0E: /*Color palette */
1155 WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1156 WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1157 if ((WPG_Palette.NumOfEntries-WPG_Palette.StartIndex) >
1158 (Rec2.RecordLength-2-2)/3)
1159 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1160 if (WPG_Palette.StartIndex > WPG_Palette.NumOfEntries)
1161 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1162 image->colors=WPG_Palette.NumOfEntries;
1163 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
1165 for (i=WPG_Palette.StartIndex;
1166 i < (int)WPG_Palette.NumOfEntries; i++)
1168 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1169 ReadBlobByte(image));
1170 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1171 ReadBlobByte(image));
1172 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1173 ReadBlobByte(image));
1177 case 0x11: /* Start PS l1 */
1178 if (Rec.RecordLength > 8)
1180 image=ExtractPostscript(image,image_info,
1181 TellBlob(image)+8, /* skip PS header in the wpg */
1182 (ssize_t) Rec.RecordLength-8,exception);
1184 ThrowReaderException(CorruptImageError,
1185 "ImproperImageHeader");
1189 case 0x14: /* bitmap type 2 */
1190 BitmapHeader2.RotAngle=ReadBlobLSBShort(image);
1191 BitmapHeader2.LowLeftX=ReadBlobLSBShort(image);
1192 BitmapHeader2.LowLeftY=ReadBlobLSBShort(image);
1193 BitmapHeader2.UpRightX=ReadBlobLSBShort(image);
1194 BitmapHeader2.UpRightY=ReadBlobLSBShort(image);
1195 BitmapHeader2.Width=ReadBlobLSBShort(image);
1196 BitmapHeader2.Height=ReadBlobLSBShort(image);
1197 if ((BitmapHeader2.Width == 0) || (BitmapHeader2.Height == 0))
1198 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1199 BitmapHeader2.Depth=ReadBlobLSBShort(image);
1200 BitmapHeader2.HorzRes=ReadBlobLSBShort(image);
1201 BitmapHeader2.VertRes=ReadBlobLSBShort(image);
1203 image->units=PixelsPerCentimeterResolution;
1204 image->page.width=(unsigned int)
1205 ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightX)/470.0);
1206 image->page.height=(unsigned int)
1207 ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightY)/470.0);
1208 image->page.x=(int) (BitmapHeader2.LowLeftX/470.0);
1209 image->page.y=(int) (BitmapHeader2.LowLeftX/470.0);
1210 if(BitmapHeader2.HorzRes && BitmapHeader2.VertRes)
1212 image->resolution.x=BitmapHeader2.HorzRes/470.0;
1213 image->resolution.y=BitmapHeader2.VertRes/470.0;
1215 image->columns=BitmapHeader2.Width;
1216 image->rows=BitmapHeader2.Height;
1217 bpp=BitmapHeader2.Depth;
1220 status=SetImageExtent(image,image->columns,image->rows,exception);
1221 if (status == MagickFalse)
1223 (void) ResetImagePixels(image,exception);
1224 if ((image->storage_class != PseudoClass) && (bpp < 24))
1226 image->colors=one << bpp;
1227 if (image->colors > GetBlobSize(image))
1228 ThrowReaderException(CorruptImageError,
1229 "InsufficientImageDataInFile");
1230 if (!AcquireImageColormap(image,image->colors,exception))
1233 ThrowReaderException(ResourceLimitError,
1234 "MemoryAllocationFailed");
1236 /* printf("Load default colormap \n"); */
1237 for (i=0; (i < (int) image->colors) && (i < 256); i++)
1239 image->colormap[i].red=ScaleCharToQuantum(WPG1_Palette[i].Red);
1240 image->colormap[i].green=ScaleCharToQuantum(WPG1_Palette[i].Green);
1241 image->colormap[i].blue=ScaleCharToQuantum(WPG1_Palette[i].Blue);
1242 image->colormap[i].alpha=OpaqueAlpha;
1248 if ( (image->colors < (one << bpp)) && (bpp != 24) )
1256 colormap=image->colormap;
1257 colors=image->colors;
1258 image->colormap=(PixelInfo *) NULL;
1259 if (AcquireImageColormap(image,one << bpp,exception) == MagickFalse)
1261 colormap=(PixelInfo *)
1262 RelinquishMagickMemory(colormap);
1265 (void) memcpy(image->colormap,colormap,MagickMin(
1266 image->colors,colors)*sizeof(*image->colormap));
1267 colormap=(PixelInfo *)
1268 RelinquishMagickMemory(colormap);
1272 if ((bpp == 1) && (image->colors > 1))
1274 if(image->colormap[0].red==0 &&
1275 image->colormap[0].green==0 &&
1276 image->colormap[0].blue==0 &&
1277 image->colormap[1].red==0 &&
1278 image->colormap[1].green==0 &&
1279 image->colormap[1].blue==0)
1280 { /* fix crippled monochrome palette */
1281 image->colormap[1].red =
1282 image->colormap[1].green =
1283 image->colormap[1].blue = QuantumRange;
1284 image->colormap[1].alpha=OpaqueAlpha;
1287 if(!image_info->ping)
1288 if(UnpackWPGRaster(image,bpp,exception) < 0)
1289 /* The raster cannot be unpacked */
1291 DecompressionFailed:
1292 ThrowReaderException(CoderError,"UnableToDecompressImage");
1295 if(Rec.RecType==0x14 && BitmapHeader2.RotAngle!=0 && !image_info->ping)
1298 if(BitmapHeader2.RotAngle & 0x8000)
1303 flop_image = FlopImage(image, exception);
1304 if (flop_image != (Image *) NULL) {
1305 DuplicateBlob(flop_image,image);
1306 ReplaceImageInList(&image,flop_image);
1310 if(BitmapHeader2.RotAngle & 0x2000)
1315 flip_image = FlipImage(image, exception);
1316 if (flip_image != (Image *) NULL) {
1317 DuplicateBlob(flip_image,image);
1318 ReplaceImageInList(&image,flip_image);
1321 /* rotate command */
1322 if(BitmapHeader2.RotAngle & 0x0FFF)
1327 rotate_image=RotateImage(image,(BitmapHeader2.RotAngle &
1328 0x0FFF), exception);
1329 if (rotate_image != (Image *) NULL) {
1330 DuplicateBlob(rotate_image,image);
1331 ReplaceImageInList(&image,rotate_image);
1336 /* Allocate next image structure. */
1337 if ((image_info->ping != MagickFalse) &&
1338 (image_info->number_scenes != 0))
1339 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1341 AcquireNextImage(image_info,image,exception);
1343 if (image->next == (Image *) NULL)
1345 image=SyncNextImageInList(image);
1346 image->columns=image->rows=0;
1350 case 0x1B: /* Postscript l2 */
1351 if (Rec.RecordLength>0x3C)
1353 image=ExtractPostscript(image,image_info,
1354 TellBlob(image)+0x3C, /* skip PS l2 header in the wpg */
1355 (ssize_t) Rec.RecordLength-0x3C,exception);
1357 ThrowReaderException(CorruptImageError,
1358 "ImproperImageHeader");
1365 case 2: /* WPG level 2 */
1366 (void) memset(CTM,0,sizeof(CTM));
1367 StartWPG.PosSizePrecision = 0;
1368 while(!EOFBlob(image)) /* object parser loop */
1370 if (SeekBlob(image,Header.DataOffset,SEEK_SET) != Header.DataOffset)
1375 Rec2.Class=(i=ReadBlobByte(image));
1378 Rec2.RecType=(i=ReadBlobByte(image));
1381 Rd_WP_DWORD(image,&Rec2.Extension);
1382 Rd_WP_DWORD(image,&Rec2.RecordLength);
1386 Header.DataOffset=TellBlob(image)+Rec2.RecordLength;
1388 switch(Rec2.RecType)
1391 StartWPG.HorizontalUnits=ReadBlobLSBShort(image);
1392 StartWPG.VerticalUnits=ReadBlobLSBShort(image);
1393 StartWPG.PosSizePrecision=ReadBlobByte(image);
1395 case 0x0C: /* Color palette */
1396 WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1397 WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1398 if ((WPG_Palette.NumOfEntries-WPG_Palette.StartIndex) >
1399 (Rec2.RecordLength-2-2) / 3)
1400 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1401 if (WPG_Palette.StartIndex >= WPG_Palette.NumOfEntries)
1402 ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1403 image->colors=WPG_Palette.NumOfEntries;
1404 if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
1405 ThrowReaderException(ResourceLimitError,
1406 "MemoryAllocationFailed");
1407 for (i=WPG_Palette.StartIndex;
1408 i < (int)WPG_Palette.NumOfEntries; i++)
1410 image->colormap[i].red=ScaleCharToQuantum((char)
1411 ReadBlobByte(image));
1412 image->colormap[i].green=ScaleCharToQuantum((char)
1413 ReadBlobByte(image));
1414 image->colormap[i].blue=ScaleCharToQuantum((char)
1415 ReadBlobByte(image));
1416 image->colormap[i].alpha=OpaqueAlpha;
1417 (void) ReadBlobByte(image); /*Opacity??*/
1421 Bitmap2Header1.Width=ReadBlobLSBShort(image);
1422 Bitmap2Header1.Height=ReadBlobLSBShort(image);
1423 if ((Bitmap2Header1.Width == 0) || (Bitmap2Header1.Height == 0))
1424 ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1425 Bitmap2Header1.Depth=ReadBlobByte(image);
1426 Bitmap2Header1.Compression=ReadBlobByte(image);
1428 if(Bitmap2Header1.Compression > 1)
1429 continue; /*Unknown compression method */
1430 switch(Bitmap2Header1.Depth)
1448 continue; /*Ignore raster with unknown depth*/
1450 image->columns=Bitmap2Header1.Width;
1451 image->rows=Bitmap2Header1.Height;
1452 if (image_info->ping != MagickFalse)
1454 status=SetImageExtent(image,image->columns,image->rows,exception);
1455 if (status != MagickFalse)
1456 status=ResetImagePixels(image,exception);
1457 if (status == MagickFalse)
1459 if ((image->colors == 0) && (bpp != 24))
1461 image->colors=one << bpp;
1462 if (!AcquireImageColormap(image,image->colors,exception))
1468 if( image->colors<(one << bpp) && bpp!=24 )
1469 image->colormap=(PixelInfo *) ResizeQuantumMemory(
1470 image->colormap,(size_t) (one << bpp),
1471 sizeof(*image->colormap));
1475 switch(Bitmap2Header1.Compression)
1477 case 0: /*Uncompressed raster*/
1479 ldblk=(ssize_t) ((bpp*image->columns+7)/8);
1480 BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t)
1481 ldblk+1,sizeof(*BImgBuff));
1482 if (BImgBuff == (unsigned char *) NULL)
1484 for (i=0; i< (ssize_t) image->rows; i++)
1489 count=ReadBlob(image,(size_t) ldblk,BImgBuff);
1492 if (InsertRow(image,BImgBuff,i,bpp,exception) == MagickFalse)
1495 BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
1496 if (i < (ssize_t) image->rows)
1497 goto DecompressionFailed;
1500 case 1: /*RLE for WPG2 */
1502 if( UnpackWPG2Raster(image,bpp,exception) < 0)
1503 goto DecompressionFailed;
1508 if(CTM[0][0]<0 && !image_info->ping)
1509 { /*?? RotAngle=360-RotAngle;*/
1513 flop_image = FlopImage(image, exception);
1514 if (flop_image != (Image *) NULL) {
1515 DuplicateBlob(flop_image,image);
1516 ReplaceImageInList(&image,flop_image);
1518 /* Try to change CTM according to Flip - I am not sure, must be checked.
1519 Tx(0,0)=-1; Tx(1,0)=0; Tx(2,0)=0;
1520 Tx(0,1)= 0; Tx(1,1)=1; Tx(2,1)=0;
1521 Tx(0,2)=(WPG._2Rect.X_ur+WPG._2Rect.X_ll);
1522 Tx(1,2)=0; Tx(2,2)=1; */
1524 if(CTM[1][1]<0 && !image_info->ping)
1525 { /*?? RotAngle=360-RotAngle;*/
1529 flip_image = FlipImage(image, exception);
1530 if (flip_image != (Image *) NULL) {
1531 DuplicateBlob(flip_image,image);
1532 ReplaceImageInList(&image,flip_image);
1534 /* Try to change CTM according to Flip - I am not sure, must be checked.
1535 float_matrix Tx(3,3);
1536 Tx(0,0)= 1; Tx(1,0)= 0; Tx(2,0)=0;
1537 Tx(0,1)= 0; Tx(1,1)=-1; Tx(2,1)=0;
1538 Tx(0,2)= 0; Tx(1,2)=(WPG._2Rect.Y_ur+WPG._2Rect.Y_ll);
1543 /* Allocate next image structure. */
1544 if ((image_info->ping != MagickFalse) &&
1545 (image_info->number_scenes != 0))
1546 if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1548 AcquireNextImage(image_info,image,exception);
1550 if (image->next == (Image *) NULL)
1552 image=SyncNextImageInList(image);
1553 image->columns=image->rows=0;
1557 case 0x12: /* Postscript WPG2*/
1558 i=ReadBlobLSBShort(image);
1559 if (Rec2.RecordLength > (unsigned int) i)
1561 image=ExtractPostscript(image,image_info,
1562 TellBlob(image)+i, /*skip PS header in the wpg2*/
1563 (ssize_t) (Rec2.RecordLength-i-2),exception);
1565 ThrowReaderException(CorruptImageError,
1566 "ImproperImageHeader");
1570 case 0x1B: /*bitmap rectangle*/
1571 WPG2Flags = LoadWPG2Flags(image,StartWPG.PosSizePrecision,NULL,&CTM);
1581 ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
1586 (void) CloseBlob(image);
1596 Rewind list, removing any empty images while rewinding.
1600 while (p != (Image *) NULL)
1603 if ((p->rows == 0) || (p->columns == 0)) {
1605 DeleteImageFromList(&tmp);
1614 for (p=image; p != (Image *) NULL; p=p->next)
1615 p->scene=(size_t) scene++;
1617 if (image == (Image *) NULL)
1618 ThrowReaderException(CorruptImageError,
1619 "ImageFileDoesNotContainAnyImageData");
1624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1628 % R e g i s t e r W P G I m a g e %
1632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1634 % Method RegisterWPGImage adds attributes for the WPG image format to
1635 % the list of supported formats. The attributes include the image format
1636 % tag, a method to read and/or write the format, whether the format
1637 % supports the saving of more than one frame to the same file or blob,
1638 % whether the format supports native in-memory I/O, and a brief
1639 % description of the format.
1641 % The format of the RegisterWPGImage method is:
1643 % size_t RegisterWPGImage(void)
1646 ModuleExport size_t RegisterWPGImage(void)
1651 entry=AcquireMagickInfo("WPG","WPG","Word Perfect Graphics");
1652 entry->decoder=(DecodeImageHandler *) ReadWPGImage;
1653 entry->magick=(IsImageFormatHandler *) IsWPG;
1654 entry->flags|=CoderDecoderSeekableStreamFlag;
1655 (void) RegisterMagickInfo(entry);
1656 return(MagickImageCoderSignature);
1660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1664 % U n r e g i s t e r W P G I m a g e %
1668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1670 % Method UnregisterWPGImage removes format registrations made by the
1671 % WPG module from the list of supported formats.
1673 % The format of the UnregisterWPGImage method is:
1675 % UnregisterWPGImage(void)
1678 ModuleExport void UnregisterWPGImage(void)
1680 (void) UnregisterMagickInfo("WPG");