-
Notifications
You must be signed in to change notification settings - Fork 11
/
woba.cpp
715 lines (635 loc) · 17.7 KB
/
woba.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
WOBA Decoder for C++
(c) 2005 Rebecca Bettencourt / Kreative Korporation
This decodes the compressed bitmap format that HyperCard uses to store card images.
The format is called WOBA, which stands for Wrath Of Bill Atkinson, because it was
written by Bill Atkinson and we had a heck of a time figuring it out.
This code is under the MIT license.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define DEBUGOUTPUT 0
#if DEBUGOUTPUT
#include <iostream>
#endif
#include "picture.h"
#include "woba.h"
#include <arpa/inet.h>
#include "CBuf.h"
#include "byteutils.h"
using namespace std;
inline int __min(int x, int y)
{
return (x > y) ? y : x;
}
void woba_decode(picture & p, char * woba)
{
#if DEBUGOUTPUT
std::cout << "===== NEXT BMAP =====" << endl;
#endif
int totalRectTop = 0,
totalRectLeft = 0,
totalRectBottom = 0,
totalRectRight = 0; /* total rectangle for whole picture */
int maskBoundRectTop = 0,
maskBoundRectLeft = 0,
maskBoundRectBottom = 0,
maskBoundRectRight = 0; /* mask bounding rect */
int pictureBoundRectTop = 0,
pictureBoundRectLeft = 0,
pictureBoundRectBottom = 0,
pictureBoundRectRight = 0; /* picture bounding rect */
int maskDataLength = 0,
pictureDataLength = 0; /* mask and picture data length */
int bx = 0, bx8 = 0, x = 0, y = 0;
int rowwidth = 0, rowwidth8 = 0, height = 0;
int dx = 0;
int dy = 0;
int repeat = 1;
CBuf patternbuffer(8);
CBuf buffer1;
CBuf buffer2;
int i = 0, j = 0;
int opcode = 0;
int operand = 0;
CBuf operandata(256);
int numberOfZeroBytes = 0, numberOfDataBytes = 0;
int k = 0;
/*
-16 0 - block size & type (already stripped)
-8 8 - block ID & filler (already stripped)
8 16 - something
12 24 - total rect
20 32 - mask rect
28 40 - picture rect
36 48 - nothing
48 56 - length
52 64 - start of mask (or bitmap if mask length == 0)
*/
#define MASK_START 52
#define INT16_AT(woba,pos) ntohs(*(u_int16_t*)(woba+pos))
#define INT32_AT(woba,pos) ntohl(*(u_int32_t*)(woba+pos))
totalRectTop = INT16_AT(woba,12);
totalRectLeft = INT16_AT(woba,14);
totalRectBottom = INT16_AT(woba,16);
totalRectRight = INT16_AT(woba,18);
maskBoundRectTop = INT16_AT(woba,20);
maskBoundRectLeft = INT16_AT(woba,22);
maskBoundRectBottom = INT16_AT(woba,24);
maskBoundRectRight = INT16_AT(woba,26);
pictureBoundRectTop = INT16_AT(woba,28);
pictureBoundRectLeft = INT16_AT(woba,30);
pictureBoundRectBottom = INT16_AT(woba,32);
pictureBoundRectRight = INT16_AT(woba,34);
maskDataLength = INT32_AT(woba,44);
pictureDataLength = INT32_AT(woba,48);
#if DEBUGOUTPUT
std::cout << "Total Rect: " << totalRectLeft << "," << totalRectTop << "," << totalRectRight << "," << totalRectBottom << endl;
std::cout << "Bitmap Rect: " << pictureBoundRectLeft << "," << pictureBoundRectTop << "," << pictureBoundRectRight << "," << pictureBoundRectBottom << endl;
std::cout << "Mask Rect: " << maskBoundRectLeft << "," << maskBoundRectTop << "," << maskBoundRectRight << "," << maskBoundRectBottom << endl;
std::cout << "Bitmap Size: " << pictureDataLength << endl;
std::cout << "Mask Size: " << maskDataLength << endl;
#endif
p.reinit( totalRectRight -totalRectLeft, totalRectBottom -totalRectTop, 1, false);
p.__directcopybmptomask(); /* clear the mask to zero */
i= MASK_START;
/* decode mask */
if( maskDataLength )
{
bx8 = maskBoundRectLeft & (~ 0x1F); // Get high 11 bits (mask out low 5 bits).
bx = bx8 / 8; // Bitshift by 3?
x = 0;
y = maskBoundRectTop;
rowwidth8 = ( (maskBoundRectRight & 0x1F)?((maskBoundRectRight | 0x1F)+1):maskBoundRectRight ) - (maskBoundRectLeft & (~ 0x1F));
rowwidth = rowwidth8 / 8;
height = maskBoundRectBottom - maskBoundRectTop;
dx = dy = 0;
repeat = 1;
// Build a 50% grey checkerboard pattern:
patternbuffer[0] = patternbuffer[2] = patternbuffer[4] = patternbuffer[6] = 170; // Even rows: 170 == 10101010 binary.
patternbuffer[1] = patternbuffer[3] = patternbuffer[5] = patternbuffer[7] = 85; // Odd rows: 85 == 01010101 binary.
// Make both buffers large enough to hold a full row of pixels:
buffer1.resize(rowwidth);
buffer2.resize(rowwidth);
j = 0;
#if DEBUGOUTPUT
std::cout << "DECODE MASK:" << endl;
std::cout << "BX8: " << bx8 << endl << "BX: " << bx << endl << "X: " << x << endl << "Y: " << y << endl;
std::cout << "RW8: " << rowwidth8 << endl << "RW: " << rowwidth << endl << "H: " << height << endl;
#endif
while( j < maskDataLength )
{
opcode = (unsigned char)woba[i];
#if DEBUGOUTPUT
std::cout << "Opcode: " << __hex(opcode) << endl;
std::cout << "Repeat: " << repeat << endl;
std::cout << "i: " << i << endl << "j: " << j << endl;
std::cout << "x: " << x << endl << "y: " << y << endl;
std::cout << "dx: " << dx << endl << "dy: " << dy << endl;
#endif
i++; j++;
if( (opcode & 0x80) == 0 )
{
/* zeros followed by data */
numberOfDataBytes = opcode >> 4; // nd = number of data bytes?
numberOfZeroBytes = opcode & 15; // nz = number of zeroes?
#if DEBUGOUTPUT
std::cout << "nd: " << numberOfDataBytes << endl << "nz: " << numberOfZeroBytes << endl;
#endif
if( numberOfDataBytes )
{
operandata.memcpy( 0, woba, i, numberOfDataBytes );
i += numberOfDataBytes; j += numberOfDataBytes;
}
while( repeat )
{
for( k = numberOfZeroBytes; k > 0; k-- )
{
buffer1[x] = 0;
x++;
}
buffer1.memcpy( x, operandata, 0, numberOfDataBytes );
x += numberOfDataBytes;
repeat--;
}
repeat = 1;
}
else if( (opcode & 0xE0) == 0xC0 )
{
/* opcode & 1F * 8 bytes of data */
numberOfDataBytes = (opcode & 0x1F) * 8;
#if DEBUGOUTPUT
std::cout << "nd: " << numberOfDataBytes << endl;
#endif
if (numberOfDataBytes)
{
operandata.memcpy( 0, woba, i, numberOfDataBytes );
i += numberOfDataBytes; j += numberOfDataBytes;
}
while( repeat )
{
buffer1.memcpy(x, operandata, 0, numberOfDataBytes);
x += numberOfDataBytes;
repeat--;
}
repeat = 1;
}
else if( (opcode & 0xE0) == 0xE0 )
{
/* opcode & 1F * 16 bytes of zero */
numberOfZeroBytes = (opcode & 0x1F)*16;
#if DEBUGOUTPUT
std::cout << "nz: " << numberOfZeroBytes << endl;
#endif
while( repeat )
{
for (k=numberOfZeroBytes; k>0; k--)
{
if( x < buffer1.size() )
buffer1[x] = 0;
x++;
}
repeat--;
}
repeat=1;
}
if( (opcode & 0xE0) == 0xA0 ) // Repeat the next opcode a certain number of times.
{
/* repeat opcode */
repeat = (opcode & 0x1F);
}
else
{
switch (opcode)
{
case 0x80: /* uncompressed data */
x = 0;
while( repeat )
{
p.maskmemcopyin(woba+i, bx8, y, rowwidth);
y++;
repeat--;
}
repeat=1;
i += rowwidth; j += rowwidth;
break;
case 0x81: /* white row */
x = 0;
while( repeat )
{
p.maskmemfill(0, bx8, y, rowwidth);
y++;
repeat--;
}
repeat=1;
break;
case 0x82: /* black row */
x = 0;
while( repeat )
{
p.maskmemfill(0xFF, bx8, y, rowwidth);
y++;
repeat--;
}
repeat=1;
break;
case 0x83: /* pattern */
operand = (unsigned char)woba[i];
#if DEBUGOUTPUT
std::cout << "patt: " << __hex(operand) << endl;
#endif
i++; j++;
x = 0;
while( repeat )
{
patternbuffer[y & 7] = operand;
p.maskmemfill(operand, bx8, y, rowwidth);
y++;
repeat--;
}
repeat=1;
break;
case 0x84: /* last pattern */
x = 0;
while( repeat )
{
operand = patternbuffer[y & 7];
#if DEBUGOUTPUT
std::cout << "patt: " << __hex(operand) << endl;
#endif
p.maskmemfill(operand, bx8, y, rowwidth);
y++;
repeat--;
}
repeat=1;
break;
case 0x85: /* previous row */
x = 0;
while( repeat )
{
p.maskcopyrow(y, y-1);
y++;
repeat--;
}
repeat=1;
break;
case 0x86: /* two rows back */
x = 0;
while( repeat )
{
p.maskcopyrow(y, y-2);
y++;
repeat--;
}
repeat=1;
break;
case 0x87: /* three rows back */
x = 0;
while( repeat )
{
p.maskcopyrow(y, y-3);
y++;
repeat--;
}
repeat = 1;
break;
case 0x88:
dx = 16; dy = 0;
break;
case 0x89:
dx = 0; dy = 0;
break;
case 0x8A:
dx = 0; dy = 1;
break;
case 0x8B:
dx = 0; dy = 2;
break;
case 0x8C:
dx = 1; dy = 0;
break;
case 0x8D:
dx = 1; dy = 1;
break;
case 0x8E:
dx = 2; dy = 2;
break;
case 0x8F:
dx = 8; dy = 0;
break;
default: /* it's not a repeat or a whole row */
if( x >= rowwidth )
{
x = 0;
if (dx)
{
buffer2.memcpy( 0, buffer1, 0, rowwidth );
for( k = rowwidth8 / dx; k > 0; k-- )
{
buffer2.shiftnstr(0, rowwidth, dx);
buffer1.xornstr(0, buffer2, 0, rowwidth);
}
}
if (dy)
{
p.maskmemcopyout(buffer2, bx8, y-dy, rowwidth);
buffer1.xornstr(0, buffer2, 0, rowwidth);
}
p.maskmemcopyin(buffer1.buf(), bx8, y, rowwidth);
y++;
}
break;
}
}
}
buffer1.resize(0);
buffer2.resize(0);
}
else if( maskBoundRectTop | maskBoundRectLeft | maskBoundRectBottom | maskBoundRectRight )
{
/* mask is a simple rectangle */
bx = maskBoundRectLeft / 8;
x = 0;
rowwidth = (maskBoundRectRight -maskBoundRectLeft) / 8;
if( rowwidth > 0 )
{
buffer1.resize(rowwidth);
for( k = bx; x < rowwidth; k++, x++ )
{
buffer1[x] = 0xFF; // was k as index.
}
for( k = maskBoundRectTop; k < maskBoundRectBottom; k++ )
{
p.maskmemcopyin( buffer1.buf(), 0, k, rowwidth );
}
buffer1.resize(0);
}
}
/* decode bitmap */
if( pictureDataLength )
{
bx8 = pictureBoundRectLeft & (~ 0x1F);
bx = bx8/8;
x = 0;
y = pictureBoundRectTop;
rowwidth8 = ( (pictureBoundRectRight & 0x1F)?((pictureBoundRectRight | 0x1F)+1):pictureBoundRectRight ) - (pictureBoundRectLeft & (~ 0x1F));
rowwidth = rowwidth8/8;
height = pictureBoundRectBottom - pictureBoundRectTop;
dx = dy = 0;
repeat = 1;
// Build a 50% grey pattern, even rows are 10101010 and odd rows are 01010101:
patternbuffer[0] = patternbuffer[2] = patternbuffer[4] = patternbuffer[6] = 170;
patternbuffer[1] = patternbuffer[3] = patternbuffer[5] = patternbuffer[7] = 85;
buffer1.resize(rowwidth);
buffer2.resize(rowwidth);
j = 0;
#if DEBUGOUTPUT
std::cout << "DECODE BITMAP:" << endl;
std::cout << "BX8: " << bx8 << endl << "BX: " << bx << endl << "X: " << x << endl << "Y: " << y << endl;
std::cout << "RW8: " << rowwidth8 << endl << "RW: " << rowwidth << endl << "H: " << height << endl;
#endif
while( j < pictureDataLength )
{
opcode = (unsigned char)woba[i];
#if DEBUGOUTPUT
std::cout << "Opcode: " << __hex(opcode) << endl;
std::cout << "Repeat: " << repeat << endl;
std::cout << "i: " << i << endl << "j: " << j << endl;
std::cout << "x: " << x << endl << "y: " << y << endl;
std::cout << "dx: " << dx << endl << "dy: " << dy << endl;
#endif
i++; j++;
if( (opcode & 0x80) == 0 )
{
/* zeros followed by data */
numberOfDataBytes = opcode >> 4;
numberOfZeroBytes = opcode & 15;
#if DEBUGOUTPUT
std::cout << "nd: " << numberOfDataBytes << endl << "nz: " << numberOfZeroBytes << endl;
#endif
if( numberOfDataBytes )
{
operandata.memcpy( 0, woba,i, numberOfDataBytes );
i += numberOfDataBytes; j += numberOfDataBytes;
}
while( repeat )
{
for( k = numberOfZeroBytes; k > 0; k-- )
{
buffer1[x] = 0;
x++;
}
buffer1.memcpy( x, operandata, 0, numberOfDataBytes );
x += numberOfDataBytes;
repeat--;
}
repeat = 1;
}
else if( (opcode & 0xE0) == 0xC0 )
{
/* opcode & 1F * 8 bytes of data */
numberOfDataBytes = (opcode & 0x1F) * 8;
#if DEBUGOUTPUT
std::cout << "nd: " << numberOfDataBytes << endl;
#endif
if( numberOfDataBytes )
{
operandata.memcpy( 0, woba, i, numberOfDataBytes );
i += numberOfDataBytes; j += numberOfDataBytes;
}
while( repeat )
{
buffer1.memcpy( x, operandata, 0, __min(buffer1.size() -x, numberOfDataBytes) );
x += numberOfDataBytes;
repeat--;
}
repeat = 1;
}
else if( (opcode & 0xE0) == 0xE0 )
{
/* opcode & 1F * 16 bytes of zero */
numberOfZeroBytes = (opcode & 0x1F) * 16;
#if DEBUGOUTPUT
std::cout << "nz: " << numberOfZeroBytes << endl;
#endif
while( repeat )
{
for( k = numberOfZeroBytes; k > 0; k-- )
{
buffer1[ __min(x,buffer1.size()-1) ] = 0;
x++;
}
repeat--;
}
repeat = 1;
}
if( (opcode & 0xE0) == 0xA0 )
{
/* repeat opcode */
repeat = (opcode & 0x1F);
}
else
{
switch (opcode)
{
case 0x80: /* uncompressed data */
x = 0;
while( repeat )
{
p.memcopyin( woba+i, bx8, y, rowwidth );
y++;
repeat--;
}
repeat = 1;
i += rowwidth; j += rowwidth;
break;
case 0x81: /* white row */
x = 0;
while( repeat )
{
p.memfill( 0, bx8, y, rowwidth );
y++;
repeat--;
}
repeat = 1;
break;
case 0x82: /* black row */
x = 0;
while( repeat )
{
p.memfill( 0xFF, bx8, y, rowwidth );
y++;
repeat--;
}
repeat = 1;
break;
case 0x83: /* pattern */
operand = (unsigned char)woba[i];
#if DEBUGOUTPUT
std::cout << "patt: " << __hex(operand) << endl;
#endif
i++; j++;
x = 0;
while( repeat )
{
patternbuffer[y & 7] = operand;
p.memfill( operand, bx8, y, rowwidth );
y++;
repeat--;
}
repeat = 1;
break;
case 0x84: /* last pattern */
x = 0;
while( repeat )
{
operand = patternbuffer[y & 7];
#if DEBUGOUTPUT
std::cout << "patt: " << __hex(operand) << endl;
#endif
p.memfill( operand, bx8, y, rowwidth );
y++;
repeat--;
}
repeat = 1;
break;
case 0x85: /* previous row */
x = 0;
while( repeat )
{
p.copyrow( y, y -1 );
y++;
repeat--;
}
repeat = 1;
break;
case 0x86: /* two rows back */
x = 0;
while( repeat )
{
p.copyrow( y, y -2 );
y++;
repeat--;
}
repeat=1;
break;
case 0x87: /* three rows back */
x = 0;
while( repeat )
{
p.copyrow( y, y -3 );
y++;
repeat--;
}
repeat = 1;
break;
case 0x88:
dx = 16; dy = 0;
break;
case 0x89:
dx = 0; dy = 0;
break;
case 0x8A:
dx = 0; dy = 1;
break;
case 0x8B:
dx = 0; dy = 2;
break;
case 0x8C:
dx = 1; dy = 0;
break;
case 0x8D:
dx = 1; dy = 1;
break;
case 0x8E:
dx = 2; dy = 2;
break;
case 0x8F:
dx = 8; dy = 0;
break;
default: /* it's not a repeat or a whole row */
if( x >= rowwidth )
{
x = 0;
if( dx )
{
buffer2.memcpy( 0, buffer1, 0, rowwidth );
for( k = rowwidth8 / dx; k > 0; k-- )
{
buffer2.shiftnstr( 0, rowwidth, dx );
buffer1.xornstr( 0, buffer2, 0, rowwidth );
}
}
if( dy )
{
p.memcopyout( buffer2, bx8, y-dy, rowwidth );
buffer1.xornstr( 0, buffer2, 0, rowwidth );
}
p.memcopyin( buffer1.buf(), bx8, y, rowwidth );
y++;
}
break;
}
}
}
buffer1.resize(0);
buffer2.resize(0);
}
if( ! (maskDataLength | maskBoundRectTop | maskBoundRectLeft | maskBoundRectBottom | maskBoundRectRight) )
{
/* mask needs to be copied from picture */
p.__directcopybmptomask();
}
}