Coverage Report

Created: 2026-06-30 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/imagemagick/coders/wpg.c
Line
Count
Source
1
/*
2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3
%                                                                             %
4
%                                                                             %
5
%                            W   W  PPPP    GGGG                              %
6
%                            W   W  P   P  G                                  %
7
%                            W W W  PPPP   G GGG                              %
8
%                            WW WW  P      G   G                              %
9
%                            W   W  P       GGG                               %
10
%                                                                             %
11
%                                                                             %
12
%                       Read WordPerfect Image Format                         %
13
%                                                                             %
14
%                              Software Design                                %
15
%                              Jaroslav Fojtik                                %
16
%                                 June 2000                                   %
17
%                                                                             %
18
%                                                                             %
19
%  Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization         %
20
%  dedicated to making software imaging solutions freely available.           %
21
%                                                                             %
22
%  You may not use this file except in compliance with the License.  You may  %
23
%  obtain a copy of the License at                                            %
24
%                                                                             %
25
%    https://imagemagick.org/license/                                         %
26
%                                                                             %
27
%  Unless required by applicable law or agreed to in writing, software        %
28
%  distributed under the License is distributed on an "AS IS" BASIS,          %
29
%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
30
%  See the License for the specific language governing permissions and        %
31
%  limitations under the License.                                             %
32
%                                                                             %
33
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34
%
35
%
36
*/
37

38
/*
39
  Include declarations.
40
*/
41
#include "MagickCore/studio.h"
42
#include "MagickCore/attribute.h"
43
#include "MagickCore/blob.h"
44
#include "MagickCore/blob-private.h"
45
#include "MagickCore/color-private.h"
46
#include "MagickCore/colormap.h"
47
#include "MagickCore/colormap-private.h"
48
#include "MagickCore/colorspace-private.h"
49
#include "MagickCore/constitute.h"
50
#include "MagickCore/exception.h"
51
#include "MagickCore/exception-private.h"
52
#include "MagickCore/cache.h"
53
#include "MagickCore/distort.h"
54
#include "MagickCore/image.h"
55
#include "MagickCore/image-private.h"
56
#include "MagickCore/list.h"
57
#include "MagickCore/magic.h"
58
#include "MagickCore/magick.h"
59
#include "MagickCore/memory_.h"
60
#include "MagickCore/resource_.h"
61
#include "MagickCore/pixel-accessor.h"
62
#include "MagickCore/quantum-private.h"
63
#include "MagickCore/static.h"
64
#include "MagickCore/string_.h"
65
#include "MagickCore/module.h"
66
#include "MagickCore/transform.h"
67
#include "MagickCore/utility.h"
68
#include "MagickCore/utility-private.h"
69

70
/*
71
  Forward declarations.
72
*/
73
static MagickBooleanType
74
  WriteWPGImage(const ImageInfo *,Image *,ExceptionInfo *);
75

76
typedef struct
77
{
78
  unsigned char Red;
79
  unsigned char Blue;
80
  unsigned char Green;
81
} RGB_Record;
82
83
/* Default palette for WPG level 1 */
84
static const RGB_Record WPG1_Palette[256]=
85
{
86
  {  0,  0,  0},    {  0,  0,168},
87
  {  0,168,  0},    {  0,168,168},
88
  {168,  0,  0},    {168,  0,168},
89
  {168, 84,  0},    {168,168,168},
90
  { 84, 84, 84},    { 84, 84,252},
91
  { 84,252, 84},    { 84,252,252},
92
  {252, 84, 84},    {252, 84,252},
93
  {252,252, 84},    {252,252,252},  /*16*/
94
  {  0,  0,  0},    { 20, 20, 20},
95
  { 32, 32, 32},    { 44, 44, 44},
96
  { 56, 56, 56},    { 68, 68, 68},
97
  { 80, 80, 80},    { 96, 96, 96},
98
  {112,112,112},    {128,128,128},
99
  {144,144,144},    {160,160,160},
100
  {180,180,180},    {200,200,200},
101
  {224,224,224},    {252,252,252},  /*32*/
102
  {  0,  0,252},    { 64,  0,252},
103
  {124,  0,252},    {188,  0,252},
104
  {252,  0,252},    {252,  0,188},
105
  {252,  0,124},    {252,  0, 64},
106
  {252,  0,  0},    {252, 64,  0},
107
  {252,124,  0},    {252,188,  0},
108
  {252,252,  0},    {188,252,  0},
109
  {124,252,  0},    { 64,252,  0},  /*48*/
110
  {  0,252,  0},    {  0,252, 64},
111
  {  0,252,124},    {  0,252,188},
112
  {  0,252,252},    {  0,188,252},
113
  {  0,124,252},    {  0, 64,252},
114
  {124,124,252},    {156,124,252},
115
  {188,124,252},    {220,124,252},
116
  {252,124,252},    {252,124,220},
117
  {252,124,188},    {252,124,156},  /*64*/
118
  {252,124,124},    {252,156,124},
119
  {252,188,124},    {252,220,124},
120
  {252,252,124},    {220,252,124},
121
  {188,252,124},    {156,252,124},
122
  {124,252,124},    {124,252,156},
123
  {124,252,188},    {124,252,220},
124
  {124,252,252},    {124,220,252},
125
  {124,188,252},    {124,156,252},  /*80*/
126
  {180,180,252},    {196,180,252},
127
  {216,180,252},    {232,180,252},
128
  {252,180,252},    {252,180,232},
129
  {252,180,216},    {252,180,196},
130
  {252,180,180},    {252,196,180},
131
  {252,216,180},    {252,232,180},
132
  {252,252,180},    {232,252,180},
133
  {216,252,180},    {196,252,180},  /*96*/
134
  {180,220,180},    {180,252,196},
135
  {180,252,216},    {180,252,232},
136
  {180,252,252},    {180,232,252},
137
  {180,216,252},    {180,196,252},
138
  {0,0,112},    {28,0,112},
139
  {56,0,112},    {84,0,112},
140
  {112,0,112},    {112,0,84},
141
  {112,0,56},    {112,0,28},  /*112*/
142
  {112,0,0},    {112,28,0},
143
  {112,56,0},    {112,84,0},
144
  {112,112,0},    {84,112,0},
145
  {56,112,0},    {28,112,0},
146
  {0,112,0},    {0,112,28},
147
  {0,112,56},    {0,112,84},
148
  {0,112,112},    {0,84,112},
149
  {0,56,112},    {0,28,112},   /*128*/
150
  {56,56,112},    {68,56,112},
151
  {84,56,112},    {96,56,112},
152
  {112,56,112},    {112,56,96},
153
  {112,56,84},    {112,56,68},
154
  {112,56,56},    {112,68,56},
155
  {112,84,56},    {112,96,56},
156
  {112,112,56},    {96,112,56},
157
  {84,112,56},    {68,112,56},  /*144*/
158
  {56,112,56},    {56,112,69},
159
  {56,112,84},    {56,112,96},
160
  {56,112,112},    {56,96,112},
161
  {56,84,112},    {56,68,112},
162
  {80,80,112},    {88,80,112},
163
  {96,80,112},    {104,80,112},
164
  {112,80,112},    {112,80,104},
165
  {112,80,96},    {112,80,88},  /*160*/
166
  {112,80,80},    {112,88,80},
167
  {112,96,80},    {112,104,80},
168
  {112,112,80},    {104,112,80},
169
  {96,112,80},    {88,112,80},
170
  {80,112,80},    {80,112,88},
171
  {80,112,96},    {80,112,104},
172
  {80,112,112},    {80,114,112},
173
  {80,96,112},    {80,88,112},  /*176*/
174
  {0,0,64},    {16,0,64},
175
  {32,0,64},    {48,0,64},
176
  {64,0,64},    {64,0,48},
177
  {64,0,32},    {64,0,16},
178
  {64,0,0},    {64,16,0},
179
  {64,32,0},    {64,48,0},
180
  {64,64,0},    {48,64,0},
181
  {32,64,0},    {16,64,0},  /*192*/
182
  {0,64,0},    {0,64,16},
183
  {0,64,32},    {0,64,48},
184
  {0,64,64},    {0,48,64},
185
  {0,32,64},    {0,16,64},
186
  {32,32,64},    {40,32,64},
187
  {48,32,64},    {56,32,64},
188
  {64,32,64},    {64,32,56},
189
  {64,32,48},    {64,32,40},  /*208*/
190
  {64,32,32},    {64,40,32},
191
  {64,48,32},    {64,56,32},
192
  {64,64,32},    {56,64,32},
193
  {48,64,32},    {40,64,32},
194
  {32,64,32},    {32,64,40},
195
  {32,64,48},    {32,64,56},
196
  {32,64,64},    {32,56,64},
197
  {32,48,64},    {32,40,64},  /*224*/
198
  {44,44,64},    {48,44,64},
199
  {52,44,64},    {60,44,64},
200
  {64,44,64},    {64,44,60},
201
  {64,44,52},    {64,44,48},
202
  {64,44,44},    {64,48,44},
203
  {64,52,44},    {64,60,44},
204
  {64,64,44},    {60,64,44},
205
  {52,64,44},    {48,64,44},  /*240*/
206
  {44,64,44},    {44,64,48},
207
  {44,64,52},    {44,64,60},
208
  {44,64,64},    {44,60,64},
209
  {44,55,64},    {44,48,64},
210
  {0,0,0},    {0,0,0},
211
  {0,0,0},    {0,0,0},
212
  {0,0,0},    {0,0,0},
213
  {0,0,0},    {0,0,0}    /*256*/
214
};
215

216
/*
217
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218
%                                                                             %
219
%                                                                             %
220
%                                                                             %
221
%   I s W P G                                                                 %
222
%                                                                             %
223
%                                                                             %
224
%                                                                             %
225
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226
%
227
%  IsWPG() returns True if the image format type, identified by the magick
228
%  string, is WPG.
229
%
230
%  The format of the IsWPG method is:
231
%
232
%      unsigned int IsWPG(const unsigned char *magick,const size_t length)
233
%
234
%  A description of each parameter follows:
235
%
236
%    o status:  Method IsWPG returns True if the image format type is WPG.
237
%
238
%    o magick: compare image format pattern against these bytes.
239
%
240
%    o length: Specifies the length of the magick string.
241
%
242
*/
243
static unsigned int IsWPG(const unsigned char *magick,const size_t length)
244
0
{
245
0
  if (length < 4)
246
0
    return(MagickFalse);
247
0
  if (memcmp(magick,"\377WPC",4) == 0)
248
0
    return(MagickTrue);
249
0
  return(MagickFalse);
250
0
}
251

252
253
static int Rd_WP_DWORD(Image *image,size_t *d)
254
20.0k
{
255
20.0k
  int
256
20.0k
    b;
257
258
20.0k
  b=ReadBlobByte(image);
259
20.0k
  *d=b;
260
20.0k
  if (b < 0xff)
261
19.2k
    return(1);
262
871
  b=ReadBlobByte(image);
263
871
  *d=(size_t) b;
264
871
  b=ReadBlobByte(image);
265
871
  *d+=(size_t) b*256l;
266
871
  if (*d < 0x8000)
267
777
    return(3);
268
94
  *d=(*d & 0x7fff) << 16;
269
94
  b=ReadBlobByte(image);
270
94
  *d+=(size_t) b;
271
94
  b=ReadBlobByte(image);
272
94
  *d+=(size_t) b*256l;
273
94
  return(5);
274
871
}
275
276
static MagickBooleanType InsertRow(Image *image,unsigned char *p,ssize_t y,
277
  int bpp,ExceptionInfo *exception)
278
0
{
279
0
  int
280
0
    bit;
281
282
0
  Quantum
283
0
    index,
284
0
    *q;
285
286
0
  ssize_t
287
0
    x;
288
289
0
  q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
290
0
  if (q == (Quantum *) NULL)
291
0
    return(MagickFalse);
292
0
  switch (bpp)
293
0
    {
294
0
    case 1:  /* Convert bitmap scanline. */
295
0
      {
296
0
        for (x=0; x < ((ssize_t) image->columns-7); x+=8)
297
0
        {
298
0
          for (bit=0; bit < 8; bit++)
299
0
          {
300
0
            index=(Quantum) (((*p) & (0x80 >> bit) ? 0x01 : 0x00));
301
0
            SetPixelIndex(image,index,q);
302
0
            if (index < image->colors)
303
0
              SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
304
0
            q+=(ptrdiff_t) GetPixelChannels(image);
305
0
          }
306
0
          p++;
307
0
        }
308
0
        if ((image->columns % 8) != 0)
309
0
          {
310
0
            for (bit=0; bit < (ssize_t) (image->columns % 8); bit++)
311
0
            {
312
0
              index=(Quantum) (((*p) & (0x80 >> bit) ? 0x01 : 0x00));
313
0
              SetPixelIndex(image,index,q);
314
0
              if (index < image->colors)
315
0
                SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
316
0
              q+=(ptrdiff_t) GetPixelChannels(image);
317
0
            }
318
0
            p++;
319
0
          }
320
0
        break;
321
0
      }
322
0
    case 2:  /* Convert PseudoColor scanline. */
323
0
      {
324
0
        for (x=0; x < ((ssize_t) image->columns-3); x+=4)
325
0
        {
326
0
            index=(Quantum) ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
327
0
            SetPixelIndex(image,index,q);
328
0
            if (index < image->colors)
329
0
              SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
330
0
            q+=(ptrdiff_t) GetPixelChannels(image);
331
0
            index=(Quantum) ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
332
0
            SetPixelIndex(image,index,q);
333
0
            if (index < image->colors)
334
0
              SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
335
0
            q+=(ptrdiff_t) GetPixelChannels(image);
336
0
            index=(Quantum) ConstrainColormapIndex(image,(*p >> 2) & 0x3,exception);
337
0
            SetPixelIndex(image,index,q);
338
0
            if (index < image->colors)
339
0
              SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
340
0
            q+=(ptrdiff_t) GetPixelChannels(image);
341
0
            index=(Quantum) ConstrainColormapIndex(image,(*p) & 0x3,exception);
342
0
            SetPixelIndex(image,index,q);
343
0
            if (index < image->colors)
344
0
              SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
345
0
            q+=(ptrdiff_t) GetPixelChannels(image);
346
0
            p++;
347
0
        }
348
0
       if ((image->columns % 4) != 0)
349
0
          {
350
0
            index=(Quantum) ConstrainColormapIndex(image,(*p >> 6) & 0x3,exception);
351
0
            SetPixelIndex(image,index,q);
352
0
            if (index < image->colors)
353
0
              SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
354
0
            q+=(ptrdiff_t) GetPixelChannels(image);
355
0
            if ((image->columns % 4) > 1)
356
0
              {
357
0
                index=(Quantum) ConstrainColormapIndex(image,(*p >> 4) & 0x3,exception);
358
0
                SetPixelIndex(image,index,q);
359
0
                if (index < image->colors)
360
0
                  SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
361
0
                q+=(ptrdiff_t) GetPixelChannels(image);
362
0
                if ((image->columns % 4) > 2)
363
0
                  {
364
0
                    index=(Quantum) ConstrainColormapIndex(image,(*p >> 2) & 0x3,
365
0
                      exception);
366
0
                    SetPixelIndex(image,index,q);
367
0
                    if (index < image->colors)
368
0
                      SetPixelViaPixelInfo(image,image->colormap+(ssize_t)
369
0
                        index,q);
370
0
                    q+=(ptrdiff_t) GetPixelChannels(image);
371
0
                  }
372
0
              }
373
0
            p++;
374
0
          }
375
0
        break;
376
0
      }
377
378
0
    case 4:  /* Convert PseudoColor scanline. */
379
0
      {
380
0
        for (x=0; x < ((ssize_t) image->columns-1); x+=2)
381
0
          {
382
0
            index=(Quantum) ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
383
0
            SetPixelIndex(image,index,q);
384
0
            if (index < image->colors)
385
0
              SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
386
0
            q+=(ptrdiff_t) GetPixelChannels(image);
387
0
            index=(Quantum) ConstrainColormapIndex(image,(*p) & 0x0f,exception);
388
0
            SetPixelIndex(image,index,q);
389
0
            if (index < image->colors)
390
0
              SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
391
0
            p++;
392
0
            q+=(ptrdiff_t) GetPixelChannels(image);
393
0
          }
394
0
        if ((image->columns % 2) != 0)
395
0
          {
396
0
            index=(Quantum) ConstrainColormapIndex(image,(*p >> 4) & 0x0f,exception);
397
0
            SetPixelIndex(image,index,q);
398
0
            if (index < image->colors)
399
0
              SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
400
0
            p++;
401
0
            q+=(ptrdiff_t) GetPixelChannels(image);
402
0
          }
403
0
        break;
404
0
      }
405
0
    case 8: /* Convert PseudoColor scanline. */
406
0
      {
407
0
        for (x=0; x < (ssize_t) image->columns; x++)
408
0
          {
409
0
            index=(Quantum) ConstrainColormapIndex(image,*p,exception);
410
0
            SetPixelIndex(image,index,q);
411
0
            if (index < image->colors)
412
0
              SetPixelViaPixelInfo(image,image->colormap+(ssize_t) index,q);
413
0
            p++;
414
0
            q+=(ptrdiff_t) GetPixelChannels(image);
415
0
          }
416
0
      }
417
0
      break;
418
419
0
    case 24:     /*  Convert DirectColor scanline.  */
420
0
      for (x=0; x < (ssize_t) image->columns; x++)
421
0
        {
422
0
          SetPixelRed(image,ScaleCharToQuantum(*p++),q);
423
0
          SetPixelGreen(image,ScaleCharToQuantum(*p++),q);
424
0
          SetPixelBlue(image,ScaleCharToQuantum(*p++),q);
425
0
          q+=(ptrdiff_t) GetPixelChannels(image);
426
0
        }
427
0
      break;
428
0
    default:
429
0
      return(MagickFalse);
430
0
    }
431
0
  if (!SyncAuthenticPixels(image,exception))
432
0
    return(MagickFalse);
433
0
  return(MagickTrue);
434
0
}
435
436
437
/* Helper for WPG1 raster reader. */
438
0
#define InsertByte(b) \
439
0
{ \
440
0
  BImgBuff[x]=b; \
441
0
  x++; \
442
0
  if((ssize_t) x>=ldblk) \
443
0
  { \
444
0
    if (InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception) != MagickFalse) \
445
0
      y++; \
446
0
    x=0; \
447
0
  } \
448
0
}
449
/* WPG1 raster reader. */
450
static int UnpackWPGRaster(Image *image,int bpp,ExceptionInfo *exception)
451
0
{
452
0
  int
453
0
    x,
454
0
    y,
455
0
    i;
456
457
0
  unsigned char
458
0
    bbuf,
459
0
    *BImgBuff,
460
0
    RunCount;
461
462
0
  ssize_t
463
0
    ldblk;
464
465
0
  x=0;
466
0
  y=0;
467
468
0
  ldblk=(ssize_t) ((bpp*(ssize_t) image->columns+7)/8);
469
0
  BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
470
0
    8*sizeof(*BImgBuff));
471
0
  if(BImgBuff==NULL) return(-2);
472
0
  (void) memset(BImgBuff,0,(size_t) ldblk*8*sizeof(*BImgBuff));
473
0
  while (y < (ssize_t) image->rows)
474
0
  {
475
0
      int
476
0
        c;
477
478
0
      c=ReadBlobByte(image);
479
0
      if (c == EOF)
480
0
        break;
481
0
      bbuf=(unsigned char) c;
482
0
      RunCount=bbuf & 0x7F;
483
0
      if(bbuf & 0x80)
484
0
        {
485
0
          if(RunCount)  /* repeat next byte runcount * */
486
0
            {
487
0
              bbuf=(unsigned char) ReadBlobByte(image);
488
0
              for(i=0;i<(int) RunCount;i++) InsertByte(bbuf);
489
0
            }
490
0
          else {  /* read next byte as RunCount; repeat 0xFF runcount* */
491
0
            c=ReadBlobByte(image);
492
0
            if (c < 0)
493
0
              break;
494
0
            RunCount=(unsigned char) c;
495
0
            for(i=0;i<(int) RunCount;i++) InsertByte(0xFF);
496
0
          }
497
0
        }
498
0
      else {
499
0
        if(RunCount)   /* next runcount byte are read directly */
500
0
          {
501
0
            for(i=0;i < (int) RunCount;i++)
502
0
              {
503
0
                c=ReadBlobByte(image);
504
0
                if (c < 0)
505
0
                  break;
506
0
                InsertByte((unsigned char) c);
507
0
              }
508
0
          }
509
0
        else {  /* repeat previous line runcount* */
510
0
          c=ReadBlobByte(image);
511
0
          if (c == EOF)
512
0
            {
513
0
              BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
514
0
              return(-7);
515
0
            }
516
0
          RunCount=(unsigned char) c;
517
0
          if(x!=0) {    /* attempt to duplicate row from x position: */
518
0
            if (InsertRow(image,BImgBuff,y,bpp,exception) == MagickFalse)
519
0
              {
520
0
                x=0;    
521
0
                y++;
522
0
              }
523
0
            BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
524
0
            return(-3);
525
0
          }
526
0
          for (i=0; i < (int) RunCount; i++)
527
0
          {
528
0
            if (y >= (ssize_t) image->rows)
529
0
              {
530
0
                BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
531
0
                return(-4);
532
0
              }
533
0
            if (InsertRow(image,BImgBuff,y,bpp,exception) == MagickFalse)
534
0
              {
535
0
                BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
536
0
                return(-6);
537
0
              }
538
0
            y++;
539
0
          }
540
0
        }
541
0
      }
542
0
      if (EOFBlob(image) != MagickFalse)
543
0
        break;
544
0
    }
545
0
  BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
546
0
  return(y <(ssize_t) image->rows ? -5 : 0);
547
0
}
548
549
550
/* Helper for WPG2 reader. */
551
0
#define InsertByte6(b) \
552
0
{ \
553
0
DisableMSCWarning(4310) \
554
0
  if(XorMe)\
555
0
    BImgBuff[x] = (unsigned char)~b;\
556
0
  else\
557
0
    BImgBuff[x] = b;\
558
0
RestoreMSCWarning \
559
0
  x++; \
560
0
  if((ssize_t) x >= ldblk) \
561
0
  { \
562
0
    if (InsertRow(image,BImgBuff,(ssize_t) y,bpp,exception) != MagickFalse) \
563
0
      y++; \
564
0
    x=0; \
565
0
   } \
566
0
}
567
/* WPG2 raster reader. */
568
static int UnpackWPG2Raster(Image *image,int bpp,ExceptionInfo *exception)
569
0
{
570
0
  int
571
0
    RunCount,
572
0
    XorMe = 0;
573
574
0
  ssize_t
575
0
    i,
576
0
    ldblk,
577
0
    x,
578
0
    y;
579
580
0
  unsigned int
581
0
    SampleSize=1;
582
583
0
  unsigned char
584
0
    bbuf,
585
0
    *BImgBuff,
586
0
    SampleBuffer[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
587
588
0
  x=0;
589
0
  y=0;
590
0
  ldblk=(ssize_t) ((bpp*(ssize_t) image->columns+7)/8);
591
0
  BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t) ldblk,
592
0
    8*sizeof(*BImgBuff));
593
0
  if(BImgBuff==NULL)
594
0
    return(-2);
595
0
  (void) memset(BImgBuff,0,((size_t) ldblk*8*sizeof(*BImgBuff)));
596
597
0
  while( y< (ssize_t) image->rows)
598
0
  {
599
0
      bbuf=(unsigned char) ReadBlobByte(image);
600
601
0
      switch(bbuf)
602
0
        {
603
0
        case 0x7D:
604
0
          SampleSize=(unsigned int) ReadBlobByte(image);  /* DSZ */
605
0
          if(SampleSize>8)
606
0
            {
607
0
              BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
608
0
              return(-2);
609
0
            }
610
0
          if(SampleSize<1)
611
0
            {
612
0
              BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
613
0
              return(-2);
614
0
            }
615
0
          break;
616
0
        case 0x7E:
617
0
          if (y == 0)
618
0
            (void) FormatLocaleFile(stderr,
619
0
              "\nUnsupported WPG token XOR, please report!");
620
0
          XorMe=!XorMe;
621
0
          break;
622
0
        case 0x7F:
623
0
          RunCount=ReadBlobByte(image);   /* BLK */
624
0
          if (RunCount < 0)
625
0
            break;
626
0
          for(i=0; i < ((ssize_t) SampleSize*(RunCount+1)); i++)
627
0
            {
628
0
              InsertByte6(0);
629
0
            }
630
0
          break;
631
0
        case 0xFD:
632
0
          RunCount=ReadBlobByte(image);   /* EXT */
633
0
          if (RunCount < 0)
634
0
            break;
635
0
          for(i=0; i<= RunCount;i++)
636
0
            for(bbuf=0; bbuf < SampleSize; bbuf++)
637
0
              InsertByte6(SampleBuffer[bbuf]);
638
0
          break;
639
0
        case 0xFE:
640
0
          RunCount=ReadBlobByte(image);  /* RST */
641
0
          if (RunCount < 0)
642
0
            break;
643
0
          if(x!=0)
644
0
            {
645
0
              (void) FormatLocaleFile(stderr,
646
0
                "\nUnsupported WPG2 unaligned token RST x=%.20g, please report!\n"
647
0
                ,(double) x);
648
0
              BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
649
0
              return(-3);
650
0
            }
651
0
          {
652
            /* duplicate the previous row RunCount x */
653
0
            for(i=0;i<=RunCount;i++)
654
0
              {
655
0
                if (InsertRow(image,BImgBuff,((ssize_t) image->rows > y ? y : (ssize_t) image->rows-1),bpp,exception) == MagickFalse)
656
0
                  {
657
0
                    BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
658
0
                    return(-3);
659
0
                  }
660
0
                y++;
661
0
              }
662
0
          }
663
0
          break;
664
0
        case 0xFF:
665
0
          RunCount=ReadBlobByte(image);   /* WHT */
666
0
          if (RunCount < 0)
667
0
            break;
668
0
          for(i=0; i < ((ssize_t) SampleSize*(RunCount+1)); i++)
669
0
            {
670
0
              InsertByte6(0xFF);
671
0
            }
672
0
          break;
673
0
        default:
674
0
          RunCount=bbuf & 0x7F;
675
676
0
          if(bbuf & 0x80)     /* REP */
677
0
            {
678
0
              for(i=0; i < (ssize_t) SampleSize; i++)
679
0
                SampleBuffer[i]=(unsigned char) ReadBlobByte(image);
680
0
              for(i=0;i<=RunCount;i++)
681
0
                for(bbuf=0;bbuf<SampleSize;bbuf++)
682
0
                  InsertByte6(SampleBuffer[bbuf]);
683
0
            }
684
0
          else {      /* NRP */
685
0
            for(i=0; i < (ssize_t) ((int) SampleSize*((int) RunCount+1)); i++)
686
0
              {
687
0
                bbuf=(unsigned char) ReadBlobByte(image);
688
0
                InsertByte6(bbuf);
689
0
              }
690
0
          }
691
0
        }
692
0
      if (EOFBlob(image) != MagickFalse)
693
0
        break;
694
0
    }
695
0
  BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
696
0
  return(y < (ssize_t) image->rows ? -5 : 0);
697
0
}
698
699
700
typedef float tCTM[3][3];
701
702
static unsigned LoadWPG2Flags(Image *image,char Precision,float *Angle,tCTM *CTM)
703
1.24k
{
704
1.24k
const unsigned char TPR=1,TRN=2,SKW=4,SCL=8,ROT=0x10,OID=0x20,LCK=0x80;
705
1.24k
int x;
706
1.24k
unsigned DenX;
707
1.24k
unsigned Flags;
708
709
1.24k
 (void) memset(*CTM,0,sizeof(*CTM));     /*CTM.erase();CTM.resize(3,3);*/
710
1.24k
 (*CTM)[0][0]=1;
711
1.24k
 (*CTM)[1][1]=1;
712
1.24k
 (*CTM)[2][2]=1;
713
714
1.24k
 Flags=ReadBlobLSBShort(image);
715
1.24k
 if(Flags & LCK) (void) ReadBlobLSBLong(image);  /*Edit lock*/
716
1.24k
 if(Flags & OID)
717
320
  {
718
    /* Read object ID. */
719
320
    if (Precision == 0)
720
269
      {
721
269
        x=ReadBlobLSBShort(image);
722
269
        if (x >= 0x8000)
723
86
          {
724
86
            Precision=1;
725
86
            (void) ReadBlobLSBShort(image);
726
86
          }
727
269
      }
728
51
    else
729
51
      (void) ReadBlobLSBLong(image);
730
320
  }
731
1.24k
 if(Flags & ROT)
732
896
  {
733
896
  x=ReadBlobLSBLong(image);  /*Rot Angle*/
734
896
  if(Angle) *Angle=(float)x/65536.0f;
735
896
  }
736
1.24k
 if(Flags & (ROT|SCL))
737
945
  {
738
945
  x=ReadBlobLSBLong(image);  /*Sx*cos()*/
739
945
  (*CTM)[0][0] = (float)x/0x10000;
740
945
  x=ReadBlobLSBLong(image);  /*Sy*cos()*/
741
945
  (*CTM)[1][1] = (float)x/0x10000;
742
945
  }
743
1.24k
 if(Flags & (ROT|SKW))
744
1.02k
  {
745
1.02k
  x=ReadBlobLSBLong(image);       /*Kx*sin()*/
746
1.02k
  (*CTM)[1][0] = (float)x/0x10000;
747
1.02k
  x=ReadBlobLSBLong(image);       /*Ky*sin()*/
748
1.02k
  (*CTM)[0][1] = (float)x/0x10000;
749
1.02k
  }
750
1.24k
 if(Flags & TRN)
751
834
  {
752
834
  x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image);  /*Tx*/
753
834
        if(x>=0) (*CTM)[0][2] = (float)x+(float)DenX/0x10000;
754
67
            else (*CTM)[0][2] = (float)x-(float)DenX/0x10000;
755
834
  x=ReadBlobLSBLong(image); DenX=ReadBlobLSBShort(image);  /*Ty*/
756
834
  (*CTM)[1][2]=(float)x + ((x>=0)?1:-1)*(float)DenX/0x10000;
757
834
        if(x>=0) (*CTM)[1][2] = (float)x+(float)DenX/0x10000;
758
101
            else (*CTM)[1][2] = (float)x-(float)DenX/0x10000;
759
834
  }
760
1.24k
 if(Flags & TPR)
761
878
  {
762
878
  x=ReadBlobLSBShort(image); DenX=ReadBlobLSBShort(image);  /*Px*/
763
878
  (*CTM)[2][0] = x + (float)DenX/0x10000;;
764
878
  x=ReadBlobLSBShort(image);  DenX=ReadBlobLSBShort(image); /*Py*/
765
878
  (*CTM)[2][1] = x + (float)DenX/0x10000;
766
878
  }
767
1.24k
 return(Flags);
768
1.24k
}
769
770
771
static Image *ExtractPostscript(Image *image,const ImageInfo *image_info,
772
  MagickOffsetType PS_Offset,ssize_t PS_Size,ExceptionInfo *exception)
773
12
{
774
12
  char
775
12
    postscript_file[MagickPathExtent];
776
777
12
  const MagicInfo
778
12
    *magic_info;
779
780
12
  FILE
781
12
    *ps_file;
782
783
12
  int
784
12
    c;
785
786
12
  ImageInfo
787
12
    *clone_info;
788
789
12
  Image
790
12
    *image2;
791
792
12
  MagickBooleanType
793
12
    status;
794
795
12
  unsigned char
796
12
    magick[2*MagickPathExtent];
797
798
12
  ssize_t
799
12
    count;
800
801
12
  if ((clone_info=CloneImageInfo(image_info)) == NULL)
802
0
    return(image);
803
12
  clone_info->blob=(void *) NULL;
804
12
  clone_info->length=0;
805
12
  status=MagickFalse;
806
807
  /* Obtain temporary file */
808
12
  (void) AcquireUniqueFilename(postscript_file);
809
12
  ps_file=fopen_utf8(postscript_file,"wb");
810
12
  if (ps_file == (FILE *) NULL)
811
0
    goto FINISH;
812
813
  /* Copy postscript to temporary file */
814
12
  if (SeekBlob(image,PS_Offset,SEEK_SET) != PS_Offset)
815
0
    {
816
0
      (void) fclose(ps_file);
817
0
      ThrowException(exception,CorruptImageError,"ImproperImageHeader",
818
0
        image->filename);
819
0
      goto FINISH_UNL;
820
0
    }
821
12
  count=ReadBlob(image, 2*MagickPathExtent, magick);
822
12
  if (count < 1)
823
2
    {
824
2
      (void) fclose(ps_file);
825
2
      ThrowException(exception,CorruptImageError,"ImproperImageHeader",
826
2
        image->filename);
827
2
      goto FINISH_UNL;
828
2
    }
829
830
10
  if (SeekBlob(image,PS_Offset,SEEK_SET) != PS_Offset)
831
0
    {
832
0
      (void) fclose(ps_file);
833
0
      ThrowException(exception,CorruptImageError,"ImproperImageHeader",
834
0
        image->filename);
835
0
      goto FINISH_UNL;
836
0
    }
837
2.44k
  while (PS_Size-- > 0)
838
2.43k
  {
839
2.43k
    c=ReadBlobByte(image);
840
2.43k
    if (c == EOF)
841
4
      {
842
4
        (void) fclose(ps_file);
843
4
        ThrowException(exception,CorruptImageError,"ImproperImageHeader",
844
4
          image->filename);
845
4
        goto FINISH_UNL;
846
4
      }
847
2.43k
    (void) fputc(c,ps_file);
848
2.43k
  }
849
6
  (void) fclose(ps_file);
850
851
    /* Detect file format - Check magic.mgk configuration file. */
852
6
  magic_info=GetMagicInfo(magick,(size_t) count,exception);
853
6
  if(magic_info == (const MagicInfo *) NULL) goto FINISH_UNL;
854
3
  if(exception->severity != UndefinedException) goto FINISH_UNL;
855
2
  (void) CopyMagickString(clone_info->magick,GetMagicName(magic_info),
856
2
    MagickPathExtent);
857
2
  if ((LocaleCompare(clone_info->magick,"PFB") != 0) &&
858
2
      (LocaleCompare(clone_info->magick,"8BIMTEXT") != 0))
859
2
    {
860
2
      ThrowException(exception,CorruptImageError,
861
2
        "DataStorageTypeIsNotSupported",image->filename);
862
2
      goto FINISH_UNL;
863
2
    }
864
865
  /* Read nested image */
866
0
  FormatLocaleString(clone_info->filename,MagickPathExtent,"ps:%.1024s",
867
0
    postscript_file);
868
0
  image2=ReadImage(clone_info,exception);
869
0
  if (!image2)
870
0
    goto FINISH_UNL;
871
0
  if (exception->severity >= ErrorException)
872
0
    {
873
0
      CloseBlob(image2);
874
0
      DestroyImageList(image2);
875
0
      goto FINISH_UNL;
876
0
    }
877
878
0
  {
879
0
    Image
880
0
      *p;
881
882
    /*
883
      Replace current image with new image while copying base image attributes.
884
    */
885
0
    p=image2;
886
0
    do
887
0
    {
888
0
      (void) CopyMagickString(p->filename,image->filename,MagickPathExtent);
889
0
      (void) CopyMagickString(p->magick_filename,image->magick_filename,
890
0
        MagickPathExtent);
891
0
      (void) CopyMagickString(p->magick,image->magick,MagickPathExtent);
892
0
      if ((p->rows == 0) || (p->columns == 0))
893
0
        {
894
0
          DeleteImageFromList(&p);
895
0
          if (p == (Image *) NULL)
896
0
            {
897
0
              image2=(Image *) NULL;
898
0
              goto FINISH_UNL;
899
0
            }
900
0
        }
901
0
      else
902
0
        {
903
0
          DestroyBlob(p);
904
0
          p->blob=ReferenceBlob(image->blob);
905
0
          p=p->next;
906
0
        }
907
0
    } while (p != (Image *) NULL);
908
0
  }
909
910
0
  if ((image->rows == 0 || image->columns == 0) &&
911
0
      (image->previous != NULL || image->next != NULL))
912
0
  {
913
0
    DeleteImageFromList(&image);
914
0
  }
915
916
0
  AppendImageToList(&image,image2);
917
0
  while (image->next != NULL)
918
0
    image=image->next;
919
0
  status=MagickTrue;
920
921
12
 FINISH_UNL:
922
12
  (void) RelinquishUniqueFileResource(postscript_file);
923
12
 FINISH:
924
12
  DestroyImageInfo(clone_info);
925
12
  if (status == MagickFalse)
926
12
    return(DestroyImageList(image));
927
0
  return(image);
928
12
}
929

930
/*
931
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
932
%                                                                             %
933
%                                                                             %
934
%                                                                             %
935
%   R e a d W P G I m a g e                                                   %
936
%                                                                             %
937
%                                                                             %
938
%                                                                             %
939
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
940
%
941
%  Method ReadWPGImage reads an WPG X image file and returns it.  It
942
%  allocates the memory necessary for the new Image structure and returns a
943
%  pointer to the new image.
944
%
945
%  The format of the ReadWPGImage method is:
946
%
947
%    Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
948
%
949
%  A description of each parameter follows:
950
%
951
%    o image:  Method ReadWPGImage returns a pointer to the image after
952
%      reading. A null image is returned if there is a memory shortage or if
953
%      the image cannot be read.
954
%
955
%    o image_info: Specifies a pointer to a ImageInfo structure.
956
%
957
%    o exception: return any errors or warnings in this structure.
958
%
959
*/
960
static Image *ReadWPGImage(const ImageInfo *image_info,ExceptionInfo *exception)
961
219
{
962
219
  typedef struct
963
219
  {
964
219
    size_t FileId;
965
219
    MagickOffsetType DataOffset;
966
219
    unsigned char ProductType;
967
219
    unsigned char FileType;
968
219
    unsigned char MajorVersion;
969
219
    unsigned char MinorVersion;
970
219
    unsigned int EncryptKey;
971
219
    unsigned int Reserved;
972
219
  } WPGHeader;
973
974
219
  typedef struct
975
219
  {
976
219
    unsigned char RecType;
977
219
    size_t RecordLength;
978
219
  } WPGRecord;
979
980
219
  typedef struct
981
219
  {
982
219
    unsigned char Class;
983
219
    unsigned char RecType;
984
219
    size_t Extension;
985
219
    size_t RecordLength;
986
219
  } WPG2Record;
987
988
219
  typedef struct
989
219
  {
990
219
    unsigned  HorizontalUnits;
991
219
    unsigned  VerticalUnits;
992
219
    unsigned char PosSizePrecision;
993
219
  } WPG2Start;
994
995
219
  typedef struct
996
219
  {
997
219
    unsigned int Width;
998
219
    unsigned int Height;
999
219
    unsigned int Depth;
1000
219
    unsigned int HorzRes;
1001
219
    unsigned int VertRes;
1002
219
  } WPGBitmapType1;
1003
1004
219
  typedef struct
1005
219
  {
1006
219
    unsigned int Width;
1007
219
    unsigned int Height;
1008
219
    unsigned char Depth;
1009
219
    unsigned char Compression;
1010
219
  } WPG2BitmapType1;
1011
1012
219
  typedef struct
1013
219
  {
1014
219
    unsigned int RotAngle;
1015
219
    unsigned int LowLeftX;
1016
219
    unsigned int LowLeftY;
1017
219
    unsigned int UpRightX;
1018
219
    unsigned int UpRightY;
1019
219
    unsigned int Width;
1020
219
    unsigned int Height;
1021
219
    unsigned int Depth;
1022
219
    unsigned int HorzRes;
1023
219
    unsigned int VertRes;
1024
219
  } WPGBitmapType2;
1025
1026
219
  typedef struct
1027
219
  {
1028
219
    unsigned int StartIndex;
1029
219
    unsigned int NumOfEntries;
1030
219
  } WPGColorMapRec;
1031
1032
  /*
1033
  typedef struct {
1034
    size_t PS_unknown1;
1035
    unsigned int PS_unknown2;
1036
    unsigned int PS_unknown3;
1037
  } WPGPSl1Record;
1038
  */
1039
1040
219
  Image
1041
219
    *image;
1042
1043
219
  unsigned int
1044
219
    status;
1045
1046
219
  WPGHeader
1047
219
    Header;
1048
1049
219
  WPGRecord
1050
219
    Rec;
1051
1052
219
  WPG2Record
1053
219
    Rec2;
1054
1055
219
  WPG2Start StartWPG;
1056
1057
219
  WPGBitmapType1
1058
219
    BitmapHeader1;
1059
1060
219
  WPG2BitmapType1
1061
219
    Bitmap2Header1;
1062
1063
219
  WPGBitmapType2
1064
219
    BitmapHeader2;
1065
1066
219
  WPGColorMapRec
1067
219
    WPG_Palette;
1068
1069
219
  int
1070
219
    i,
1071
219
    bpp,
1072
219
    WPG2Flags;
1073
1074
219
  ssize_t
1075
219
    ldblk;
1076
1077
219
  size_t
1078
219
    one;
1079
1080
219
  unsigned char
1081
219
    *BImgBuff;
1082
1083
219
  tCTM CTM;         /*current transform matrix*/
1084
1085
  /*
1086
    Open image file.
1087
  */
1088
219
  assert(image_info != (const ImageInfo *) NULL);
1089
219
  assert(image_info->signature == MagickCoreSignature);
1090
219
  assert(exception != (ExceptionInfo *) NULL);
1091
219
  assert(exception->signature == MagickCoreSignature);
1092
219
  one=1;
1093
219
  image=AcquireImage(image_info,exception);
1094
219
  image->columns=0;
1095
219
  image->rows=0;
1096
219
  image->depth=8;
1097
219
  status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1098
219
  if (status == MagickFalse)
1099
0
    {
1100
0
      image=DestroyImageList(image);
1101
0
      return((Image *) NULL);
1102
0
    }
1103
  /*
1104
    Read WPG image.
1105
  */
1106
219
  Header.FileId=ReadBlobLSBLong(image);
1107
219
  Header.DataOffset=(MagickOffsetType) ReadBlobLSBLong(image);
1108
219
  Header.ProductType=(unsigned char) ReadBlobByte(image);
1109
219
  Header.FileType=(unsigned char) ReadBlobByte(image);
1110
219
  Header.MajorVersion=(unsigned char) ReadBlobByte(image);
1111
219
  Header.MinorVersion=(unsigned char) ReadBlobByte(image);
1112
219
  Header.EncryptKey=ReadBlobLSBShort(image);
1113
219
  Header.Reserved=ReadBlobLSBShort(image);
1114
1115
219
  if ((Header.FileId != 0x435057FF) || (Header.FileType != 0x16))
1116
215
    ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1117
215
  if (Header.EncryptKey != 0)
1118
214
    ThrowReaderException(CoderError,"EncryptedWPGImageFileNotSupported");
1119
1120
214
  image->colors=0;
1121
214
  image->storage_class=DirectClass;
1122
214
  bpp=0;
1123
214
  BitmapHeader2.RotAngle=0;
1124
214
  Rec2.RecordLength=0;
1125
214
  switch(Header.MajorVersion)
1126
214
  {
1127
102
    case 1:     /* WPG level 1 */
1128
10.5k
      while(!EOFBlob(image)) /* object parser loop */
1129
10.5k
        {
1130
10.5k
          if (SeekBlob(image,Header.DataOffset,SEEK_SET) != Header.DataOffset)
1131
0
            break;
1132
10.5k
          if (EOFBlob(image))
1133
0
            break;
1134
10.5k
          Rec.RecType=(unsigned char) (i=ReadBlobByte(image));
1135
10.5k
          if (i==EOF)
1136
9
            break;
1137
10.5k
          i=Rd_WP_DWORD(image,&Rec.RecordLength);
1138
10.5k
          if ((Rec.RecordLength+4) >= GetBlobSize(image))
1139
10.5k
            ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1140
10.5k
          if (EOFBlob(image))
1141
14
            break;
1142
10.5k
          Header.DataOffset=(MagickOffsetType) TellBlob(image)+(ssize_t)
1143
10.5k
            Rec.RecordLength;
1144
10.5k
          if (Header.DataOffset > (MagickOffsetType) GetBlobSize(image))
1145
32
            ThrowReaderException(CorruptImageError, 
1146
10.5k
              "InsufficientImageDataInFile");
1147
10.4k
          switch(Rec.RecType)
1148
10.4k
            {
1149
1.64k
            case 0x0B: /* bitmap type 1 */
1150
1.64k
              BitmapHeader1.Width=ReadBlobLSBShort(image);
1151
1.64k
              BitmapHeader1.Height=ReadBlobLSBShort(image);
1152
1.64k
              if ((BitmapHeader1.Width == 0) || (BitmapHeader1.Height == 0))
1153
1.64k
                ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1154
1.64k
              BitmapHeader1.Depth=ReadBlobLSBShort(image);
1155
1.64k
              BitmapHeader1.HorzRes=ReadBlobLSBShort(image);
1156
1.64k
              BitmapHeader1.VertRes=ReadBlobLSBShort(image);
1157
1158
1.64k
              if(BitmapHeader1.HorzRes && BitmapHeader1.VertRes)
1159
1.16k
                {
1160
1.16k
                  image->units=PixelsPerCentimeterResolution;
1161
1.16k
                  image->resolution.x=BitmapHeader1.HorzRes/470.0;
1162
1.16k
                  image->resolution.y=BitmapHeader1.VertRes/470.0;
1163
1.16k
                }
1164
1.64k
              image->columns=BitmapHeader1.Width;
1165
1.64k
              image->rows=BitmapHeader1.Height;
1166
1.64k
              bpp=(int) BitmapHeader1.Depth;
1167
1.64k
              if ((bpp == 1) &&
1168
232
                  (AcquireImageColormap(image,2,exception) == MagickFalse))
1169
0
                goto NoMemory;
1170
1.64k
              goto UnpackRaster;
1171
1172
1.64k
            case 0x0E:  /*Color palette */
1173
187
              WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1174
187
              WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1175
187
              if ((WPG_Palette.NumOfEntries-WPG_Palette.StartIndex) > (Rec.RecordLength-2-2)/3)
1176
182
                ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1177
182
              if (WPG_Palette.StartIndex > WPG_Palette.NumOfEntries)
1178
181
                ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1179
181
              image->colors=WPG_Palette.NumOfEntries;
1180
181
              if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
1181
0
                goto NoMemory;
1182
181
              for (i=(int) WPG_Palette.StartIndex;
1183
21.6k
                   i < (int) WPG_Palette.NumOfEntries; i++)
1184
21.4k
                {
1185
21.4k
                  image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1186
21.4k
                    ReadBlobByte(image));
1187
21.4k
                  image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1188
21.4k
                    ReadBlobByte(image));
1189
21.4k
                  image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1190
21.4k
                    ReadBlobByte(image));
1191
21.4k
                }
1192
181
              break;
1193
1194
344
            case 0x11:  /* Start PS l1 */
1195
344
              if (Rec.RecordLength > 8)
1196
3
                {
1197
3
                  image=ExtractPostscript(image,image_info,
1198
3
                    TellBlob(image)+8,   /* skip PS header in the wpg */
1199
3
                    (ssize_t) Rec.RecordLength-8,exception);
1200
3
                  if (image == NULL)
1201
3
                    ThrowReaderException(CorruptImageError,
1202
3
                      "ImproperImageHeader");
1203
0
                }
1204
341
              break;
1205
1206
341
            case 0x14:  /* bitmap type 2 */
1207
123
              BitmapHeader2.RotAngle=ReadBlobLSBShort(image);
1208
123
              BitmapHeader2.LowLeftX=ReadBlobLSBShort(image);
1209
123
              BitmapHeader2.LowLeftY=ReadBlobLSBShort(image);
1210
123
              BitmapHeader2.UpRightX=ReadBlobLSBShort(image);
1211
123
              BitmapHeader2.UpRightY=ReadBlobLSBShort(image);
1212
123
              BitmapHeader2.Width=ReadBlobLSBShort(image);
1213
123
              BitmapHeader2.Height=ReadBlobLSBShort(image);
1214
123
              if ((BitmapHeader2.Width == 0) || (BitmapHeader2.Height == 0))
1215
120
                ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1216
120
              BitmapHeader2.Depth=ReadBlobLSBShort(image);
1217
120
              BitmapHeader2.HorzRes=ReadBlobLSBShort(image);
1218
120
              BitmapHeader2.VertRes=ReadBlobLSBShort(image);
1219
1220
120
              image->units=PixelsPerCentimeterResolution;
1221
120
              image->page.width=(unsigned int)
1222
120
                ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightX)/470.0);
1223
120
              image->page.height=(unsigned int)
1224
120
                ((BitmapHeader2.LowLeftX-BitmapHeader2.UpRightY)/470.0);
1225
120
              image->page.x=(int) (BitmapHeader2.LowLeftX/470.0);
1226
120
              image->page.y=(int) (BitmapHeader2.LowLeftX/470.0);
1227
120
              if(BitmapHeader2.HorzRes && BitmapHeader2.VertRes)
1228
29
                {
1229
29
                  image->resolution.x=BitmapHeader2.HorzRes/470.0;
1230
29
                  image->resolution.y=BitmapHeader2.VertRes/470.0;
1231
29
                }
1232
120
              image->columns=BitmapHeader2.Width;
1233
120
              image->rows=BitmapHeader2.Height;
1234
120
              bpp=(int) BitmapHeader2.Depth;
1235
1236
1.76k
            UnpackRaster:
1237
1.76k
              status=SetImageExtent(image,image->columns,image->rows,exception);
1238
1.76k
              if (status == MagickFalse)
1239
1.42k
                break;
1240
336
              (void) ResetImagePixels(image,exception);
1241
336
              if ((image->storage_class != PseudoClass) && (bpp < 24))
1242
120
                {
1243
120
                  image->colors=one << bpp;
1244
120
                  if (image->colors > GetBlobSize(image))
1245
1
                    ThrowReaderException(CorruptImageError,
1246
120
                      "InsufficientImageDataInFile");
1247
119
                  if (!AcquireImageColormap(image,image->colors,exception))
1248
0
                    {
1249
1
                    NoMemory:
1250
1
                      ThrowReaderException(ResourceLimitError,
1251
0
                        "MemoryAllocationFailed");
1252
0
                    }
1253
462
                  for (i=0; (i < (int) image->colors) && (i < 256); i++)
1254
343
                    {
1255
343
                      image->colormap[i].red=ScaleCharToQuantum(WPG1_Palette[i].Red);
1256
343
                      image->colormap[i].green=ScaleCharToQuantum(WPG1_Palette[i].Green);
1257
343
                      image->colormap[i].blue=ScaleCharToQuantum(WPG1_Palette[i].Blue);
1258
343
                      image->colormap[i].alpha=OpaqueAlpha;
1259
343
                    }
1260
119
                }
1261
216
              else
1262
216
                {
1263
216
                  if (bpp < 24)
1264
84
                  if ( (image->colors < (one << bpp)) && (bpp != 24) )
1265
5
                    {
1266
5
                      PixelInfo
1267
5
                        *colormap;
1268
1269
5
                      size_t
1270
5
                        colors;
1271
1272
5
                      colormap=image->colormap;
1273
5
                      colors=image->colors;
1274
5
                      image->colormap=(PixelInfo *) NULL;
1275
5
                      if (AcquireImageColormap(image,one << bpp,exception) == MagickFalse)
1276
1
                        {
1277
1
                          colormap=(PixelInfo *)
1278
1
                            RelinquishMagickMemory(colormap);
1279
1
                          goto NoMemory;
1280
1
                        }
1281
4
                      (void) memcpy(image->colormap,colormap,MagickMin(
1282
4
                        image->colors,colors)*sizeof(*image->colormap));
1283
4
                      colormap=(PixelInfo *)
1284
4
                        RelinquishMagickMemory(colormap);
1285
4
                    }
1286
216
                }
1287
1288
334
              if (bpp == 1)
1289
78
                {
1290
78
                  image->colormap[0].red=image->colormap[0].green=
1291
78
                    image->colormap[0].blue=0;
1292
78
                  image->colormap[0].alpha=OpaqueAlpha;
1293
78
                  image->colormap[1].red=image->colormap[1].green=
1294
78
                    image->colormap[1].blue=QuantumRange;
1295
78
                  image->colormap[1].alpha=OpaqueAlpha;
1296
78
                }
1297
334
              if(!image_info->ping)
1298
0
                if(UnpackWPGRaster(image,bpp,exception) < 0)
1299
                  /* The raster cannot be unpacked */
1300
0
                  {
1301
0
                  DecompressionFailed:
1302
0
                    ThrowReaderException(CoderError,"UnableToDecompressImage");
1303
0
                  }
1304
1305
334
              if(Rec.RecType==0x14 && BitmapHeader2.RotAngle!=0 && !image_info->ping)
1306
0
                {
1307
                  /* flop command */
1308
0
                  if(BitmapHeader2.RotAngle & 0x8000)
1309
0
                    {
1310
0
                      Image
1311
0
                        *flop_image;
1312
1313
0
                      flop_image = FlopImage(image, exception);
1314
0
                      if (flop_image != (Image *) NULL) {
1315
0
                        DuplicateBlob(flop_image,image);
1316
0
                        ReplaceImageInList(&image,flop_image);
1317
0
                      }
1318
0
                    }
1319
                  /* flip command */
1320
0
                  if(BitmapHeader2.RotAngle & 0x2000)
1321
0
                    {
1322
0
                      Image
1323
0
                        *flip_image;
1324
1325
0
                      flip_image = FlipImage(image, exception);
1326
0
                      if (flip_image != (Image *) NULL) {
1327
0
                        DuplicateBlob(flip_image,image);
1328
0
                        ReplaceImageInList(&image,flip_image);
1329
0
                      }
1330
0
                    }
1331
                  /* rotate command */
1332
0
                  if(BitmapHeader2.RotAngle & 0x0FFF)
1333
0
                    {
1334
0
                      Image
1335
0
                        *rotate_image;
1336
1337
0
                      rotate_image=RotateImage(image,(BitmapHeader2.RotAngle &
1338
0
                        0x0FFF), exception);
1339
0
                      if (rotate_image != (Image *) NULL) {
1340
0
                        DuplicateBlob(rotate_image,image);
1341
0
                        ReplaceImageInList(&image,rotate_image);
1342
0
                      }
1343
0
                    }
1344
0
                }
1345
1346
              /* Allocate next image structure. */
1347
334
              if ((image_info->ping != MagickFalse) &&
1348
334
                  (image_info->number_scenes != 0))
1349
0
                if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1350
0
                  goto Finish;
1351
334
              AcquireNextImage(image_info,image,exception);
1352
334
              image->depth=8;
1353
334
              if (image->next == (Image *) NULL)
1354
0
                goto Finish;
1355
334
              image=SyncNextImageInList(image);
1356
334
              image->columns=image->rows=0;
1357
334
              image->colors=0;
1358
334
              break;
1359
1360
47
            case 0x1B:  /* Postscript l2 */
1361
47
              if (Rec.RecordLength>0x3C)
1362
1
                {
1363
1
                  image=ExtractPostscript(image,image_info,
1364
1
                    TellBlob(image)+0x3C,   /* skip PS l2 header in the wpg */
1365
1
                    (ssize_t) Rec.RecordLength-0x3C,exception);
1366
1
                  if (image == NULL)
1367
1
                    ThrowReaderException(CorruptImageError,
1368
1
                      "ImproperImageHeader");
1369
0
                }
1370
46
              break;
1371
10.4k
            }
1372
10.4k
        }
1373
29
      break;
1374
1375
111
    case 2:  /* WPG level 2 */
1376
111
      (void) memset(CTM,0,sizeof(CTM));
1377
111
      StartWPG.PosSizePrecision = 0;
1378
4.85k
      while(!EOFBlob(image)) /* object parser loop */
1379
4.82k
        {
1380
4.82k
          if (SeekBlob(image,Header.DataOffset,SEEK_SET) != Header.DataOffset)
1381
0
            break;
1382
4.82k
          if (EOFBlob(image))
1383
0
            break;
1384
1385
4.82k
          Rec2.Class=(unsigned char) (i=ReadBlobByte(image));
1386
4.82k
          if(i==EOF)
1387
50
            break;
1388
4.77k
          Rec2.RecType=(unsigned char) (i=ReadBlobByte(image));
1389
4.77k
          if(i==EOF)
1390
5
            break;
1391
4.76k
          Rd_WP_DWORD(image,&Rec2.Extension);
1392
4.76k
          Rd_WP_DWORD(image,&Rec2.RecordLength);
1393
4.76k
          if(EOFBlob(image))
1394
6
            break;
1395
1396
4.76k
          Header.DataOffset=(MagickOffsetType) (TellBlob(image)+(ssize_t)
1397
4.76k
            Rec2.RecordLength);
1398
1399
4.76k
          switch(Rec2.RecType)
1400
4.76k
            {
1401
162
      case 1:
1402
162
              StartWPG.HorizontalUnits=ReadBlobLSBShort(image);
1403
162
              StartWPG.VerticalUnits=ReadBlobLSBShort(image);
1404
162
              StartWPG.PosSizePrecision=(unsigned char) ReadBlobByte(image);
1405
162
              break;
1406
19
            case 0x0C:    /* Color palette */
1407
19
              WPG_Palette.StartIndex=ReadBlobLSBShort(image);
1408
19
              WPG_Palette.NumOfEntries=ReadBlobLSBShort(image);
1409
19
              if ((WPG_Palette.NumOfEntries-WPG_Palette.StartIndex) >
1410
19
                  (Rec2.RecordLength-2-2) / 3)
1411
18
                ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1412
18
              if (WPG_Palette.StartIndex >= WPG_Palette.NumOfEntries)
1413
17
                ThrowReaderException(CorruptImageError,"InvalidColormapIndex");
1414
17
              image->colors=WPG_Palette.NumOfEntries;
1415
17
              if (AcquireImageColormap(image,image->colors,exception) == MagickFalse)
1416
0
                ThrowReaderException(ResourceLimitError,
1417
17
                  "MemoryAllocationFailed");
1418
17
              for (i=(int) WPG_Palette.StartIndex;
1419
49.3k
                   i < (int) WPG_Palette.NumOfEntries; i++)
1420
49.3k
                {
1421
49.3k
                  image->colormap[i].red=ScaleCharToQuantum((unsigned char)
1422
49.3k
                    ReadBlobByte(image));
1423
49.3k
                  image->colormap[i].green=ScaleCharToQuantum((unsigned char)
1424
49.3k
                    ReadBlobByte(image));
1425
49.3k
                  image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
1426
49.3k
                    ReadBlobByte(image));
1427
49.3k
                  image->colormap[i].alpha=OpaqueAlpha;
1428
49.3k
                  (void) ReadBlobByte(image);   /*Opacity??*/
1429
49.3k
                }
1430
17
              break;
1431
399
            case 0x0E:
1432
399
              Bitmap2Header1.Width=ReadBlobLSBShort(image);
1433
399
              Bitmap2Header1.Height=ReadBlobLSBShort(image);
1434
399
              if ((Bitmap2Header1.Width == 0) || (Bitmap2Header1.Height == 0))
1435
397
                ThrowReaderException(CorruptImageError,"ImproperImageHeader");
1436
397
              Bitmap2Header1.Depth=(unsigned char) ReadBlobByte(image);
1437
397
              Bitmap2Header1.Compression=(unsigned char) ReadBlobByte(image);
1438
1439
397
              if(Bitmap2Header1.Compression > 1)
1440
347
                continue; /*Unknown compression method */
1441
50
              switch(Bitmap2Header1.Depth)
1442
50
                {
1443
1
                case 1:
1444
1
                  bpp=1;
1445
1
                  break;
1446
4
                case 2:
1447
4
                  bpp=2;
1448
4
                  break;
1449
1
                case 3:
1450
1
                  bpp=4;
1451
1
                  break;
1452
1
                case 4:
1453
1
                  bpp=8;
1454
1
                  break;
1455
1
                case 8:
1456
1
                  bpp=24;
1457
1
                  break;
1458
42
                default:
1459
42
                  continue;  /*Ignore raster with unknown depth*/
1460
50
                }
1461
8
              image->columns=Bitmap2Header1.Width;
1462
8
              image->rows=Bitmap2Header1.Height;
1463
8
              if (image_info->ping != MagickFalse)
1464
8
                return(image);
1465
0
              status=SetImageExtent(image,image->columns,image->rows,exception);
1466
0
              if (status != MagickFalse)
1467
0
                status=ResetImagePixels(image,exception);
1468
0
              if (status == MagickFalse)
1469
0
                break;
1470
0
              if ((image->colors == 0) && (bpp != 24))
1471
0
                {
1472
0
                  image->colors=one << bpp;
1473
0
                  if (!AcquireImageColormap(image,image->colors,exception))
1474
0
                    goto NoMemory;
1475
0
                }
1476
0
              else
1477
0
                {
1478
0
                  if(bpp < 24)
1479
0
                    if( image->colors<(one << bpp) && bpp!=24 )
1480
0
                      image->colormap=(PixelInfo *) ResizeQuantumMemory(
1481
0
                       image->colormap,(size_t) (one << bpp),
1482
0
                       sizeof(*image->colormap));
1483
0
                }
1484
1485
1486
0
              switch(Bitmap2Header1.Compression)
1487
0
                {
1488
0
                case 0:    /*Uncompressed raster*/
1489
0
                  {
1490
0
                    ldblk=(ssize_t) ((bpp*(ssize_t) image->columns+7)/8);
1491
0
                    BImgBuff=(unsigned char *) AcquireQuantumMemory((size_t)
1492
0
                      ldblk+1,sizeof(*BImgBuff));
1493
0
                    if (BImgBuff == (unsigned char *) NULL)
1494
0
                      goto NoMemory;
1495
0
                    for (i=0; i< (ssize_t) image->rows; i++)
1496
0
                    {
1497
0
                      ssize_t
1498
0
                        count;
1499
1500
0
                      count=ReadBlob(image,(size_t) ldblk,BImgBuff);
1501
0
                      if (count != ldblk)
1502
0
                        break;
1503
0
                      if (InsertRow(image,BImgBuff,i,bpp,exception) == MagickFalse)
1504
0
                        break;
1505
0
                    }
1506
0
                    BImgBuff=(unsigned char *) RelinquishMagickMemory(BImgBuff);
1507
0
                    if (i < (ssize_t) image->rows)
1508
0
                      goto DecompressionFailed;
1509
0
                    break;
1510
0
                  }
1511
0
                case 1:    /*RLE for WPG2 */
1512
0
                  {
1513
0
                    if( UnpackWPG2Raster(image,bpp,exception) < 0)
1514
0
                      goto DecompressionFailed;
1515
0
                    break;
1516
0
                  }
1517
0
                }
1518
1519
0
              if(CTM[0][0]<0 && !image_info->ping)
1520
0
                {    /*?? RotAngle=360-RotAngle;*/
1521
0
                  Image
1522
0
                    *flop_image;
1523
1524
0
                  flop_image = FlopImage(image, exception);
1525
0
                  if (flop_image != (Image *) NULL) {
1526
0
                    DuplicateBlob(flop_image,image);
1527
0
                    ReplaceImageInList(&image,flop_image);
1528
0
                  }
1529
                  /* Try to change CTM according to Flip - I am not sure, must be checked.
1530
                     Tx(0,0)=-1;      Tx(1,0)=0;   Tx(2,0)=0;
1531
                     Tx(0,1)= 0;      Tx(1,1)=1;   Tx(2,1)=0;
1532
                     Tx(0,2)=(WPG._2Rect.X_ur+WPG._2Rect.X_ll);
1533
                     Tx(1,2)=0;   Tx(2,2)=1; */
1534
0
                }
1535
0
              if(CTM[1][1]<0 && !image_info->ping)
1536
0
                {    /*?? RotAngle=360-RotAngle;*/
1537
0
                  Image
1538
0
                    *flip_image;
1539
1540
0
                   flip_image = FlipImage(image, exception);
1541
0
                   if (flip_image != (Image *) NULL) {
1542
0
                     DuplicateBlob(flip_image,image);
1543
0
                     ReplaceImageInList(&image,flip_image);
1544
0
                    }
1545
                  /* Try to change CTM according to Flip - I am not sure, must be checked.
1546
                     float_matrix Tx(3,3);
1547
                     Tx(0,0)= 1;   Tx(1,0)= 0;   Tx(2,0)=0;
1548
                     Tx(0,1)= 0;   Tx(1,1)=-1;   Tx(2,1)=0;
1549
                     Tx(0,2)= 0;   Tx(1,2)=(WPG._2Rect.Y_ur+WPG._2Rect.Y_ll);
1550
                     Tx(2,2)=1; */
1551
0
              }
1552
1553
1554
              /* Allocate next image structure. */
1555
0
              if ((image_info->ping != MagickFalse) &&
1556
0
                  (image_info->number_scenes != 0))
1557
0
                if (image->scene >= (image_info->scene+image_info->number_scenes-1))
1558
0
                  goto Finish;
1559
0
              AcquireNextImage(image_info,image,exception);
1560
0
              image->depth=8;
1561
0
              if (image->next == (Image *) NULL)
1562
0
                goto Finish;
1563
0
              image=SyncNextImageInList(image);
1564
0
              image->columns=image->rows=0;
1565
0
              image->colors=0;
1566
0
              break;
1567
1568
167
            case 0x12:  /* Postscript WPG2*/
1569
167
        i=ReadBlobLSBShort(image);
1570
167
              if (Rec2.RecordLength > (unsigned int) i)
1571
8
                {
1572
8
                  image=ExtractPostscript(image,image_info,
1573
8
                    TellBlob(image)+i,    /*skip PS header in the wpg2*/
1574
8
                    (ssize_t) Rec2.RecordLength-i-2,exception);
1575
8
                  if (image == NULL)
1576
8
                    ThrowReaderException(CorruptImageError,
1577
8
                      "ImproperImageHeader");
1578
0
                }
1579
159
              break;
1580
1581
1.24k
      case 0x1B:          /*bitmap rectangle*/
1582
1.24k
              WPG2Flags = (int) LoadWPG2Flags(image,(char)
1583
1.24k
                StartWPG.PosSizePrecision,NULL,&CTM);
1584
1.24k
              (void) WPG2Flags;
1585
1.24k
              break;
1586
4.76k
            }
1587
4.76k
        }
1588
1589
91
      break;
1590
1591
91
    default:
1592
1
      {
1593
1
         ThrowReaderException(CoderError,"DataEncodingSchemeIsNotSupported");
1594
0
      }
1595
214
   }
1596
1597
120
 Finish:
1598
120
  (void) CloseBlob(image);
1599
1600
120
  {
1601
120
    Image
1602
120
      *p;
1603
1604
120
    ssize_t
1605
120
      scene=0;
1606
1607
    /*
1608
      Rewind list, removing any empty images while rewinding.
1609
    */
1610
120
    p=image;
1611
120
    image=NULL;
1612
336
    while (p != (Image *) NULL)
1613
216
      {
1614
216
        Image *tmp=p;
1615
216
        if ((p->rows == 0) || (p->columns == 0)) {
1616
110
          p=p->previous;
1617
110
          DeleteImageFromList(&tmp);
1618
110
        } else {
1619
106
          image=p;
1620
106
          p=p->previous;
1621
106
        }
1622
216
      }
1623
    /*
1624
      Fix scene numbers.
1625
    */
1626
226
    for (p=image; p != (Image *) NULL; p=p->next)
1627
106
      p->scene=(size_t) scene++;
1628
120
  }
1629
120
  if (image == (Image *) NULL)
1630
103
    ThrowReaderException(CorruptImageError,
1631
120
      "ImageFileDoesNotContainAnyImageData");
1632
17
  return(image);
1633
120
}
1634

1635
/*
1636
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1637
%                                                                             %
1638
%                                                                             %
1639
%                                                                             %
1640
%   R e g i s t e r W P G I m a g e                                           %
1641
%                                                                             %
1642
%                                                                             %
1643
%                                                                             %
1644
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1645
%
1646
%  Method RegisterWPGImage adds attributes for the WPG image format to
1647
%  the list of supported formats.  The attributes include the image format
1648
%  tag, a method to read and/or write the format, whether the format
1649
%  supports the saving of more than one frame to the same file or blob,
1650
%  whether the format supports native in-memory I/O, and a brief
1651
%  description of the format.
1652
%
1653
%  The format of the RegisterWPGImage method is:
1654
%
1655
%      size_t RegisterWPGImage(void)
1656
%
1657
*/
1658
ModuleExport size_t RegisterWPGImage(void)
1659
9
{
1660
9
  MagickInfo
1661
9
    *entry;
1662
1663
9
  entry=AcquireMagickInfo("WPG","WPG","Word Perfect Graphics");
1664
9
  entry->decoder=(DecodeImageHandler *) ReadWPGImage;
1665
9
  entry->encoder=(EncodeImageHandler *) WriteWPGImage;
1666
9
  entry->magick=(IsImageFormatHandler *) IsWPG;
1667
9
  entry->flags^=CoderAdjoinFlag;
1668
9
  entry->flags|=CoderDecoderSeekableStreamFlag;
1669
9
  (void) RegisterMagickInfo(entry);
1670
9
  return(MagickImageCoderSignature);
1671
9
}
1672

1673
/*
1674
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1675
%                                                                             %
1676
%                                                                             %
1677
%                                                                             %
1678
%   U n r e g i s t e r W P G I m a g e                                       %
1679
%                                                                             %
1680
%                                                                             %
1681
%                                                                             %
1682
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1683
%
1684
%  Method UnregisterWPGImage removes format registrations made by the
1685
%  WPG module from the list of supported formats.
1686
%
1687
%  The format of the UnregisterWPGImage method is:
1688
%
1689
%      UnregisterWPGImage(void)
1690
%
1691
*/
1692
ModuleExport void UnregisterWPGImage(void)
1693
0
{
1694
0
  (void) UnregisterMagickInfo("WPG");
1695
0
}
1696

1697
/*
1698
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1699
%                                                                             %
1700
%                                                                             %
1701
%                                                                             %
1702
%   W r i t e W P G I m a g e                                                 %
1703
%                                                                             %
1704
%                                                                             %
1705
%                                                                             %
1706
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1707
%
1708
%  WriteWPGImage() writes an image in the WPG format to a file.
1709
%
1710
%  The format of the WriteWPGImage method is:
1711
%
1712
%      MagickBooleanType WriteWPGImage(const ImageInfo *image_info,
1713
%        Image *image,ExceptionInfo *exception)
1714
%
1715
%  A description of each parameter follows.
1716
%
1717
%    o image_info: the image info.
1718
%
1719
%    o image:  The image.
1720
%
1721
%    o exception: return any errors or warnings in this structure.
1722
%
1723
*/
1724
1725
typedef struct
1726
{
1727
  size_t
1728
    count;
1729
1730
  ssize_t
1731
    offset;
1732
1733
  unsigned char
1734
    pixels[256];
1735
} WPGRLEInfo;
1736
1737
static void WPGFlushRLE(WPGRLEInfo *rle_info,Image *image,unsigned char n)
1738
0
{
1739
0
  if (n > rle_info->offset)
1740
0
    n=(unsigned char) rle_info->offset;
1741
0
  if (n > 0x7F)
1742
0
    n=0x7F;
1743
0
  if (n > 0)
1744
0
    {
1745
0
      (void) WriteBlobByte(image,n);
1746
0
      (void) WriteBlob(image,n,rle_info->pixels);
1747
0
      rle_info->offset-=n;
1748
0
      if (rle_info->offset > 0)
1749
0
        (void) memmove(rle_info->pixels,rle_info->pixels+n,n);
1750
0
      else
1751
0
        rle_info->count=0;
1752
0
    }
1753
0
}
1754
1755
static void WPGAddRLEByte(WPGRLEInfo *rle_info,Image *image,
1756
  const unsigned char byte)
1757
0
{
1758
0
  rle_info->pixels[rle_info->offset++]=byte;
1759
0
  if (rle_info->offset > 1)
1760
0
    {
1761
0
      if ((rle_info->count == 0x7E) ||
1762
0
          (rle_info->pixels[rle_info->offset-2] != byte))
1763
0
        {
1764
0
          if (rle_info->count >= 1)
1765
0
            {
1766
0
              rle_info->count++;
1767
0
              WPGFlushRLE(rle_info,image,(unsigned char) (rle_info->offset-
1768
0
                (ssize_t) rle_info->count-1));
1769
0
              (void) WriteBlobByte(image,(unsigned char) (
1770
0
                rle_info->count | 0x80));
1771
0
              (void) WriteBlobByte(image,rle_info->pixels[0]);
1772
0
              rle_info->offset=1;
1773
0
              rle_info->pixels[0]=byte;
1774
0
            }
1775
0
          rle_info->count = 0;
1776
0
        }
1777
0
      else
1778
0
        rle_info->count++;
1779
0
  }
1780
0
  if ((rle_info->offset-(ssize_t) rle_info->count) > 0x7E)
1781
0
    {
1782
0
      WPGFlushRLE(rle_info,image,0x7F);
1783
0
      return;
1784
0
    }
1785
0
  if ((rle_info->offset > 0x7E) && (rle_info->count >= 1))
1786
0
     {
1787
0
       WPGFlushRLE(rle_info,image,(unsigned char) (rle_info->offset-
1788
0
         (ssize_t) rle_info->count-1));
1789
0
       return;
1790
0
     }
1791
0
}
1792
1793
static void WPGFlush(WPGRLEInfo *rle_info,Image *image)
1794
0
{
1795
0
  if (rle_info->count > 1)
1796
0
    {
1797
0
      WPGAddRLEByte(rle_info,image,rle_info->pixels[rle_info->offset-1] ^ 0xFF);
1798
0
      rle_info->offset=0;
1799
0
    }
1800
0
  else
1801
0
    {
1802
0
      WPGFlushRLE(rle_info,image,0x7F);
1803
0
      WPGFlushRLE(rle_info,image,0x7F);
1804
0
      rle_info->count=0;
1805
0
    }
1806
0
}
1807
1808
static void WPGAddRLEBlock(WPGRLEInfo *rle_info,Image *image,
1809
  const unsigned char *pixels,unsigned short extent)
1810
0
{
1811
0
  while (extent-- > 0)
1812
0
  {
1813
0
    WPGAddRLEByte(rle_info,image,*pixels);
1814
0
    pixels++;
1815
0
  }
1816
0
}
1817
1818
static void WPGInitializeRLE(WPGRLEInfo *rle_info)
1819
0
{
1820
0
  rle_info->count=0;
1821
0
  rle_info->offset=0;
1822
0
  (void) memset(rle_info->pixels,0,sizeof(rle_info->pixels));
1823
0
}
1824
1825
static MagickBooleanType WriteWPGImage(const ImageInfo *image_info,Image *image,
1826
  ExceptionInfo *exception)
1827
0
{
1828
0
  MagickBooleanType
1829
0
    status;
1830
1831
0
  MagickOffsetType
1832
0
    current_offset,
1833
0
    offset;
1834
1835
0
  QuantumInfo
1836
0
    *quantum_info;
1837
1838
0
  size_t
1839
0
    extent;
1840
1841
0
  ssize_t
1842
0
    y;
1843
1844
0
  unsigned char
1845
0
    *pixels;
1846
1847
0
  WPGRLEInfo
1848
0
    rle_info;
1849
1850
  /*
1851
    Open output image file.
1852
  */
1853
0
  assert(image_info != (const ImageInfo *) NULL);
1854
0
  assert(image_info->signature == MagickCoreSignature);
1855
0
  assert(image != (Image *) NULL);
1856
0
  assert(image->signature == MagickCoreSignature);
1857
0
  assert(exception != (ExceptionInfo *) NULL);
1858
0
  assert(exception->signature == MagickCoreSignature);
1859
0
  if (IsEventLogging() != MagickFalse)
1860
0
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1861
0
  status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
1862
0
  if (status == MagickFalse)
1863
0
    return(status);
1864
0
  if ((image->columns > 65535UL) || (image->rows > 65535UL))
1865
0
    ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
1866
0
  if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
1867
0
    (void) TransformImageColorspace(image,sRGBColorspace,exception);
1868
0
  (void) SetImageType(image,PaletteType,exception);
1869
  /*
1870
    Write WPG header.
1871
  */
1872
0
  (void) WriteBlobLSBLong(image,0x435057FF);  /* FileId */
1873
0
  (void) WriteBlobLSBLong(image,16);  /* data offset */
1874
0
  (void) WriteBlobByte(image,1);  /* product type */
1875
0
  (void) WriteBlobByte(image,0x16);  /* file type */
1876
0
  (void) WriteBlobByte(image,1);  /* major version */
1877
0
  (void) WriteBlobByte(image,0);  /* minor version */
1878
0
  (void) WriteBlobLSBShort(image,0);  /* encypt key */
1879
0
  (void) WriteBlobLSBShort(image,0);  /* reserved */
1880
  /*
1881
    Write WPG level 1 header.
1882
  */
1883
0
  (void) WriteBlobByte(image,0x0f);
1884
0
  (void) WriteBlobByte(image,0x06);
1885
0
  (void) WriteBlobByte(image,1);  /* version number */
1886
0
  (void) WriteBlobByte(image,0);  /* flags */
1887
0
  (void) WriteBlobLSBShort(image,(unsigned short) image->columns);
1888
0
  (void) WriteBlobLSBShort(image,(unsigned short) image->rows);
1889
0
  image->depth=8;
1890
0
  if (image->colors <= 16)
1891
0
    image->depth=4;
1892
0
  if (image->colors <= 2)
1893
0
    image->depth=1;
1894
0
  if (image->depth > 1)
1895
0
    {
1896
      /*
1897
        Write colormap.
1898
      */
1899
0
      ssize_t i = 0;
1900
0
      unsigned short number_entries = 0;
1901
0
      (void) WriteBlobByte(image,0x0e);
1902
0
      number_entries=3*(1U << image->depth)+4;
1903
0
      if (number_entries < 0xff)
1904
0
        (void) WriteBlobByte(image,(unsigned char) number_entries);
1905
0
      else
1906
0
        {
1907
0
          (void) WriteBlobByte(image,0xff);
1908
0
          (void) WriteBlobLSBShort(image,number_entries);
1909
0
        }
1910
0
      (void) WriteBlobLSBShort(image,0); /* start index */
1911
0
      (void) WriteBlobLSBShort(image,1U << image->depth);
1912
0
      for ( ; i < (ssize_t) ((size_t) 1U << image->depth); i++)
1913
0
        if (i >= (ssize_t) image->colors)
1914
0
          {
1915
0
            (void) WriteBlobByte(image,(unsigned char) i);
1916
0
            (void) WriteBlobByte(image,(unsigned char) i);
1917
0
            (void) WriteBlobByte(image,(unsigned char) i);
1918
0
          }
1919
0
        else
1920
0
          {
1921
0
            (void) WriteBlobByte(image,ScaleQuantumToChar(
1922
0
              (Quantum) image->colormap[i].red));
1923
0
            (void) WriteBlobByte(image,ScaleQuantumToChar(
1924
0
              (Quantum) image->colormap[i].green));
1925
0
            (void) WriteBlobByte(image,ScaleQuantumToChar(
1926
0
              (Quantum) image->colormap[i].blue));
1927
0
          }
1928
0
    }
1929
  /*
1930
    Bitmap 1 header.
1931
  */
1932
0
  (void) WriteBlobByte(image,0x0b);
1933
0
  (void) WriteBlobByte(image,0xff);
1934
0
  offset=TellBlob(image);
1935
0
  (void) WriteBlobLSBShort(image,0x8000);
1936
0
  (void) WriteBlobLSBShort(image,0);
1937
0
  (void) WriteBlobLSBShort(image,(unsigned short) image->columns);
1938
0
  (void) WriteBlobLSBShort(image,(unsigned short) image->rows);
1939
0
  (void) WriteBlobLSBShort(image,(unsigned char) image->depth);
1940
0
  (void) WriteBlobLSBShort(image,75);  /* resolution */
1941
0
  (void) WriteBlobLSBShort(image,75);
1942
  /*
1943
    Write WPG image pixels.
1944
  */
1945
0
  quantum_info=AcquireQuantumInfo(image_info,image);
1946
0
  if (quantum_info == (QuantumInfo *) NULL)
1947
0
    ThrowWriterException(ImageError,"MemoryAllocationFailed");
1948
0
  pixels=(unsigned char *) GetQuantumPixels(quantum_info);
1949
0
  extent=GetQuantumExtent(image,quantum_info,image->depth == 1 ? GrayQuantum :
1950
0
    IndexQuantum);
1951
0
  (void) memset(pixels,0,extent*sizeof(*pixels));
1952
0
  WPGInitializeRLE(&rle_info);
1953
0
  for (y=0; y < (ssize_t) image->rows; y++)
1954
0
  {
1955
0
    const Quantum
1956
0
      *p;
1957
1958
0
    size_t
1959
0
      length;
1960
1961
0
    p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1962
0
    if (p == (const Quantum *) NULL)
1963
0
      break;
1964
0
    length=ExportQuantumPixels(image,(CacheView *) NULL,quantum_info,
1965
0
      image->depth == 1 ? GrayQuantum : IndexQuantum,pixels,exception);
1966
0
    if (length == 0)
1967
0
      break;
1968
0
    WPGAddRLEBlock(&rle_info,image,pixels,(unsigned short) length);
1969
0
    WPGFlush(&rle_info,image);
1970
0
    status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
1971
0
      image->rows);
1972
0
    if (status == MagickFalse)
1973
0
      break;
1974
0
  }
1975
0
  quantum_info=DestroyQuantumInfo(quantum_info);
1976
0
  current_offset=TellBlob(image);
1977
0
  (void) WriteBlobByte(image,0x10);
1978
0
  (void) WriteBlobByte(image,0);
1979
0
  (void) SeekBlob(image,offset,SEEK_SET);
1980
0
  offset=current_offset-offset-4;
1981
0
  (void) WriteBlobLSBShort(image,(unsigned short) (0x8000 | (offset >> 16)));
1982
0
  (void) WriteBlobLSBShort(image,offset & 0xffff);
1983
0
  if (y < (ssize_t) image->rows)
1984
0
    ThrowWriterException(CorruptImageError,"UnableToWriteImageData");
1985
0
  if (CloseBlob(image) == MagickFalse)
1986
0
    status=MagickFalse;
1987
0
  return(status);
1988
0
}