-
Notifications
You must be signed in to change notification settings - Fork 15
/
filemax.cpp
5804 lines (4817 loc) · 152 KB
/
filemax.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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
License: GPL-2
An electronic filing cabinet: scan, print, stack, arrange
Copyright (C) 2009 Simon Glass, [email protected]
.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
X-Comment: On Debian GNU/Linux systems, the complete text of the GNU General
Public License can be found in the /usr/share/common-licenses/GPL file.
*/
/* program to decode paperport .max files */
/* Simon Glass, 19nov01 */
/* picked up again xmas 2004 */
/* copyright 2005 Bluewater Systems Ltd, www.bluewatersys.com */
#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include <QDebug>
#include <QFileInfo>
#include <QHash>
#include "errno.h"
#include "jpeglib.h"
#include "tiffio.h"
#define G3CODES
#include "myt4.h"
#include "mytif_fax3.h"
#include <unistd.h>
#include "sys/stat.h"
#include "desk.h"
#include "filemax.h"
#include "utils.h"
static FILE *debugf = 0;
static int debug_level = 0;
#define warning(x) do {if (debug_level >= 0) dprintf x; } while (0)
#define debug1(x) do {if (debug_level >= 1) dprintf x; } while (0)
#define debug2(x) do {if (debug_level >= 2) dprintf x; } while (0)
#define debug3(x) do {if (debug_level >= 3) dprintf x; } while (0)
//#define FAX3_DEBUG
// this must be a multiple of 8 at the moment due to assumptions in scale_2bpp
// previews are scaled 1:24
#define PREVIEW_SCALE 24
// JPEG quality hardwired for now
#define JPEG_QUALITY 75
/** current version of max file - note that is 'our' version; legacy max files
don't have this value */
#define MAX_VERSION 1
/*
picked up again 8/12/04
Notes removed
*/
/*
max format:
0 w signature 0x46476956
...
5c w page_count
...
6c w chunk_count
...
88 w chunk0_start
..
a4 w bermuda_pos
a8 w tunguska_pos
ac w annot_pos
b0 w ?
b4 w ?
b8 w trail_pos
bermuda chunk format:
0 w ?
20 h page_count
22 h ?
24 w ?
28 h ?
2a h ?
2c w ?
30 h ?
32 h ?
34 h ?
36 c page_info records, 1 per page
page_info record format:
0 h chunkid
2 h ?
4 w roswell_pos
8 w ?
roswell chunk format:
0 h signature 0x5a56
2 h size 0x1a0
..
42 w image_pos
..
88 w noti1_pos
8c w title pos
..
ce w noti2_pos
d2 w text_pos
chunk format:
0 h 'VZ' 0x5a56
2 w size
6 h chunkid
8 h used
a h textflag (0=name, 3=not)
c h type (0=image, 1=not image)
e h ? 0
10 h ? 0
12 h ? 0
14 h ? 0
16 h ? 0
18 h ? 0
1a h flags (CHUNKF_...)
1c h flags (titletype) 0x80 = page title, 0x00 = stack title
1e h ? 0
20 h ? 0x88
22 h ? 0x88
24 h ? 0
26 h ? 1
28 h ? 0
annot format:
22 w author position
26 w author length (inc \0)
2a w title position
2e w title length
32 w notes position?
36 w notes length
3a w other position
3e w other length
for images:
2a h xsize
2c h ysize
2e h dpi_x
30 h dpi_y
32 h ? 0
34 h bpp
36 h image_channels
38 h preview_xsize
3a h preview_ysize
3c h ? 0x21
3e h ? 2
40 h ? 0x53d
42 h part count
44 - part0 starts here
image tileinfo part:
0 h code
4 h tile_size.x in bytes?
6 h tile_size.y
8 h tile_extent.x
a h tile_extent.y
c h tile_count
e a tile info records, 10 bytes each
tileinfo record:
0 h ? 2
2 w size of tile data + 4
6 h ? 0x8000
8 h ? 0
image tiledata part:
0 h tile number (0..tile_count-1)
2 h datatype (normally 0x43)
4 ? the data (always a multiple of 4 bytes?)
? repeats for each tile
**************************************************************************
older format:
0 w signature 0x41476956
...
24 w page_count
...
32 w chunk_count
...
..
58 w bermuda_pos
- w tunguska_pos
- w annot_pos
- w ?
- w ?
7c w trail_pos
bermuda chunk format:
0 w ?
20 h ? - not page_count
22 h ?
24 w ?
28 h ?
2a h ?
2c h
2e h xsize
30 h ysize
32 h dpi
34 ? page_info records, 1 per page
for images:
26 h xsize
28 h ysize
2a h dpi_x
2c h dpi_y
2e h bpp
30 h image_channels
3e h preview_xsize
40 h preview_ysize
42 h part0 starts here (preview?)
page_info record format:
0 h chunkid
2 w image_pos
*/
enum
{
POS_chunk_header_size = 0x20, // size of chunk header
POS_chunk_step = 0x20, // chunk size is a multiple of this
#define ALIGN_CHUNK(x) (((x) + POS_chunk_step) & ~(POS_chunk_step - 1))
POSn_version = 0x20, // file version number
POS_chunk0_start = 0x88,
POS_page_count = 0x5c,
POS_chunk_count = 0x6c,
POS_bermuda = 0xa4,
POS_tunguska = 0xa8,
POS_annot = 0xac,
POS_trail = 0xb8,
/** this is the envelope information, including sender and recipient */
POS_envelope = 0xbc,
// version A positions
POSva_page_count = 0x24,
POSva_chunk_count = 0x32,
POSva_chunk0_start = 0x58,
POSva_bermuda = 0x58,
POSva_trail = 0x7c,
// POS_start_pagenum = 0xfe,
/* within chunks */
POS_image_size_x = 0x2a,
POS_image_size_y = 0x2c,
POS_image_dpi_x = 0x2e,
POS_image_dpi_y = 0x30,
POS_image_bits = 0x34,
POS_image_channels = 0x36,
POS_image_preview_x = 0x38,
POS_image_preview_y = 0x3a,
POSva_image_size_x = 0x26,
POSva_image_size_y = 0x28,
POSva_image_dpi_x = 0x2a,
POSva_image_dpi_y = 0x2c,
POSva_image_bits = 0x2e,
POSva_image_channels = 0x30,
POSva_image_preview_x = 0x3e,
POSva_image_preview_y = 0x40,
// within envelope chunk
POS_env_start = 0x20, // start of envelope records within chunk
POS_env_count = 0x24,
// zeroes
POS_env_string0 = 0x30,
// envelope is simply a n integer count then a list of \0 terminated strings
// within annot chunk
POS_annot_start = 0x22, // start of annot records within chunk
POS_annot_pos = 0,
POS_annot_size = 4,
POS_annot_offset = 8,
POS_text_start = 0x20, // start of text string within chunk
POS_part_count = 0x42,
POS_part0 = 0x44, // start of (pos, size) info for each part
// within bermuda block
POS_bermuda_pagecount = 0x20,
POS_bermuda_pageinfo = 0x36,
POSva_bermuda_pageinfo = 0x34,
// within roswell block
POS_roswell_image = 0x42,
POS_roswell_noti1 = 0x88,
POS_roswell_title = 0x8c,
POS_roswell_noti2 = 0xce,
POS_roswell_text = 0xd2,
POSn_roswell_timestamp = 0x20, // page timestamp
/* chunk types */
CT_image = 0, /* image chunk */
CT_text, /* OCRed text chunk */
CT_notimage, // not an image?
CT_unknown, // completely unknown
CT_title, // page title
CT_roswell, // roswell (page info)
CT_bermuda, // bermuda chunk
CT_annot, // annotation chunk
CT_env, // envelope chunk
/* part types */
PT_colourmap = 0,
PT_preview,
PT_notes,
PT_tileinfo,
PT_tiledata,
/* flags to determine which parts of a page are decoded:
bit 0 = decode preview, bit 1 = decode full picture */
DECODE_none,
DECODE_preview,
DECODE_full,
DECODE_all,
TITLE_max_size = 128,
// allocate space for new chunks in lots of this value
CHUNK_alloc_step = 32,
PAGE_alloc_step = 8
};
typedef unsigned short ushort;
extern "C" {
#include "ztab.h"
#include "btab.h"
};
#include "epeglite.h"
#include "err.h"
#include "mem.h"
enum
{
CHUNKF_image = 0x1000, // chunk contains an image
CHUNKF_text = 0x1001, // chunk contains text
CHUNKF_roswell = 0x4000, // chunk contains roswell data
CHUNKF_bermuda = 0x8000, // chunk contains bermuda data
CHUNKF_annot = 0x8002, // chunk contains annot data
CHUNKF_env = 0x8004 // chunk contains envelope data
};
#define GET_DATA(pos) max_check_scache (pos)
typedef struct decode_info
{
int version_a; // true if an old version A file
int used; // number of bits used in 'word'
unsigned word; // current data word being processed
int line_bytes; // number of bytes in a line
int stride; // distance in bytes between pixels on consecutive lines
int y; // current ypos
int width; // image width in pixels
byte *outptr; // output data pointer
byte *inptr; // current input pointer
cpoint image_size; // image size
short *table_data; // table data
short *table; // current table line
short *table_prev; // old table line
int table_size; // table size (in ushorts)
ushort *ztab; // a table
byte *atab; // another table
unsigned *tab7; // 7 bit lookup tables
unsigned *tab_first, *tab_second; // 13 bit huffman lookup tables
unsigned *btab; // b table
debug_info *debug; // debug info
} decode_info;
// info used when encoding tiles
typedef struct encode_info
{
byte *buff; // output data buffer
int size; // current size of data buffer
int tile_line_bytes; //!< byte width of this tile
} encode_info;
static debug_info *no_debug (void)
{
static debug_info sdebug, *debug = &sdebug;
debug->level = 0;
debug->max_steps = INT_MAX;
debug->start_tile = 0;
debug->num_tiles = INT_MAX;
debug->logf = stdout;
debug->compf = NULL;
return debug;
}
Filemax::Filemax (const QString &dir, const QString &filename, Desk *desk)
: File (dir, filename, desk, Type_max)
{
_version = -1;
_chunk0_start = 0;
_size = 0;
_signature = -1;
_debug = no_debug ();
_fin = NULL;
max_clear_cache (_cache);
max_clear_cache (_scache);
_bermuda = _tunguska = _annot = _trail = _b0 = _b4 = 0;
_envelope = 0;
_chunkid_next = 0;
_hdr_updated = false;
_version_a = false;
_all_chunks_loaded = false;
}
Filemax::~Filemax ()
{
max_free ();
}
/** ensure that the page has a title loaded, if there is one */
err_info *Filemax::ensure_titlestr (int, page_info &page)
{
chunk_info *chunk;
bool has_chars = false;
char title [TITLE_max_size];
int ch, j;
bool temp = false;
err_info *e = NULL;
if (page.title_loaded)
return NULL;
page.title_loaded = page.title_saved = true;
*title = '\0';
if (page.title)
{
// find the chunk, supporting temporary loading
CALL (chunk_find (page.title, chunk, &temp));
if (page.title && chunk && chunk->type == CT_title)
{
for (j = 0; j < TITLE_max_size - 1; j++)
{
CALLB (getbytee (page.title + 0x20 + j, &ch));
title [j] = ch;
if (!title [j] || title [j] < ' ')
break;
if (title [j] > ' ')
has_chars = true;
}
if (!has_chars) // ignore a title with only spaces
j = 0;
title [j] = '\0';
}
}
// if temporarily loaded, free it now
if (temp)
{
chunk_free (*chunk);
delete chunk;
}
page.titlestr = title;
return e;
}
err_info *Filemax::find_page (int pagenum, page_info* &page)
{
if (pagenum < 0 || pagenum >= _pages.size ())
return err_make (ERRFN, ERR_page_number_out_of_range2, pagenum, _pages.size () - 1);
page = &_pages [pagenum];
return NULL;
}
static void dprintf (const char *fmt, ...)
{
va_list ptr;
char str [250];
va_start (ptr, fmt);
vsprintf (str, fmt, ptr);
va_end (ptr);
if (debugf)
fprintf (debugf, "%s", str);
}
static const char *chunk_namestr (int type)
{
static char str [8];
switch (type)
{
case CT_image : return "Image";
case CT_text : return "Text ";
case CT_notimage : return "NotI ";
case CT_title : return "Title";
case CT_roswell : return "Roswell";
case CT_bermuda : return "Bermuda";
case CT_annot : return "Annot";
case CT_env : return "Envelope";
default :
sprintf (str, "t%-4x:", type);
return str;
break;
}
}
static const char *parttype_str [] =
{
"colourmap",
"preview",
"notes",
"tileinfo",
"tiledata"
};
static int make_err (const char *func, int err)
{
printf ("err: %s %d\n", func, err);
return err;
}
#define ERR(err) make_err (__PRETTY_FUNCTION__, err)
void Filemax::debug_page (page_info *page)
{
printf ("id %05x roswell %05x image %05x title %05x\n",
page->chunkid, page->roswell, page->image, page->title);
}
void Filemax::debug_pages (void)
{
int i;
for (i = 0; i < _pages.size (); i++)
{
page_info &page = _pages [i];
printf ("Page %d: ", i + 1);
debug_page (&page);
}
}
err_info *Filemax::max_checkerr (void)
{
err_info *err;
err = _err;
_err = NULL;
return err;
}
err_info *Filemax::getworde (int pos, int *wordp)
{
int word;
byte *p;
if (pos < 0 || pos > _size - 4)
return err_make (ERRFN, ERR_file_position_out_of_range3, pos, 0,
_size);
p = GET_DATA (pos);
if (p)
word = *p | (p [1] << 8) | (p [2] << 16) | ((unsigned)p [3] << 24);
else
{
printf (" - getworde %x\n", pos);
fseek (_fin, pos, SEEK_SET);
word = fgetc (_fin);
if (word == EOF)
printf ("eof error\n");
word |= fgetc (_fin) << 8;
word |= fgetc (_fin) << 16;
word |= (unsigned)fgetc (_fin) << 24;
}
*wordp = word;
return NULL;
}
int Filemax::getword (int pos)
{
int value;
if (!_err)
_err = getworde (pos, &value);
return _err ? -1 : value;
}
err_info *Filemax::gethwe (int pos, int *valuep)
{
int word;
byte *p;
if (pos < 0 || pos > _size - 2)
return err_make (ERRFN, ERR_file_position_out_of_range3, pos, 0,
_size);
p = GET_DATA (pos);
if (p)
word = *p | (p [1] << 8);
else
{
fseek (_fin, pos, SEEK_SET);
word = fgetc (_fin);
word |= fgetc (_fin) << 8;
}
*valuep = word;
return NULL;
}
int Filemax::gethw (int pos)
{
int value;
if (!_err)
_err = gethwe (pos, &value);
return _err ? -1 : value;
}
err_info *Filemax::getbytee (int pos, int *bytep)
{
byte *data;
if (pos < 0 || pos > _size - 1)
return err_make (ERRFN, ERR_file_position_out_of_range3, pos, 0,
_size);
data = GET_DATA (pos);
if (data)
*bytep = *data;
else
{
fseek (_fin, pos, SEEK_SET);
*bytep = fgetc (_fin);
}
return NULL;
}
err_info *Filemax::merr_make (const char *func_name, int errnum, ...)
{
va_list ptr;
err_info *e;
char str [260];
va_start (ptr, errnum);
e = err_vmake (func_name, errnum, ptr);
va_end (ptr);
sprintf (str, "%s: %s", _filename.toLatin1 ().constData(), e->errstr);
strcpy (e->errstr, str);
return e;
}
err_info *Filemax::max_cache_data (cache_info &cache, int pos,
int size, int min, byte **datap)
{
int n;
cache.buff.resize (size);
// read the data
fseek (_fin, pos, SEEK_SET);
n = fread (cache.buff.data (), 1, size, _fin);
if (n != size)
{
if (n >= min)
size = n;
else
return err_make (ERRFN, ERR_file_position_out_of_range3, pos, 0,
_size);
}
if (datap)
*datap = (byte *)cache.buff.data ();
cache.pos = pos;
return NULL;
}
byte *Filemax::max_check_scache (int pos)
{
byte *ptr;
err_info *err = NULL;
#define CACHE_4K_SIZE 4096
// if the cache has the wrong data, load it
if (pos < _scache.pos || (pos + 4 > _scache.pos + _scache.buff.size ()))
err = max_cache_data (_scache, pos, CACHE_4K_SIZE, 4, NULL);
// return the data
if (err)
{
_err = err;
return NULL;
}
ptr = (byte *)_scache.buff.data () + (pos - _scache.pos);
return ptr;
}
err_info *Filemax::max_read_data (int pos, byte *buf, int size)
{
int count;
// read the data
fseek (_fin, pos, SEEK_SET);
count = fread (buf, 1, size, _fin);
return count == size ? NULL : merr_make (ERRFN, ERR_failed_to_read_bytes1, size);
}
err_info *Filemax::max_write_data (int pos, byte *data, int size)
{
int count;
// write the data
fseek (_fin, pos, SEEK_SET);
count = fwrite (data, 1, size, _fin);
max_clear_cache (_scache, pos, size);
max_clear_cache (_cache, pos, size);
return count == size ? NULL : merr_make (ERRFN, ERR_failed_to_write_bytes1, size);
}
void Filemax::max_clear_cache (cache_info &cache, int pos, int size)
{
if (size == -1
|| (pos >= cache.pos && pos < cache.pos + cache.buff.size ())
|| (pos + size >= cache.pos && pos + size < cache.pos + cache.buff.size ()))
{
cache.buff.clear ();
cache.pos = 0;
}
}
err_info *Filemax::read_part (part_info &part, int pos)
{
CALL (getworde (pos, &part.start));
CALL (getworde (pos + 4, &part.size));
part.raw_size = part.size;
return NULL;
}
unsigned Filemax::getbits (decode_info &decode, int count)
{
unsigned data, mask;
assert (decode.used + count < 0x20);
mask = 0xffffffff >> decode.used;
data = decode.word & mask;
data = data >> (0x20 - decode.used - count);
debug3 (("word=%x, used=%d, mask=%08x, count=%d, data=%x, len=%d\n", decode.word, decode.used,
mask, count, data, count));
decode.used += count;
return data;
}
int Filemax::update_table (decode_info &decode, byte *inptr, int width,
short *table)
{
short *orig_table = table;
int x, count;
int colour;
int byte;
colour = 0;
x = 0;
// debug3 (("update_table:\n"));
for (count = (width + 7) / 8; count != 0; count--)
{
byte = (*inptr++ ^ colour) & 0xff;
// debug3 (("byte=%x: ", byte));
while (byte)
{
*table++ = x + decode.ztab [byte * 2];
colour = ~colour;
byte = decode.atab [byte * 4];
// debug3 (("%x ", byte));
assert (table - orig_table <= width);
}
// debug3 (("\n"));
x += 8;
}
table [0] = width;
table [1] = width;
table [2] = width;
return table - orig_table + 3;
}
void Filemax::do_uncomp (decode_info &decode, int bits_used)
{
// move over byte used
decode.inptr += (bits_used + 7) / 8;
// copy these bytes
// debug3 (("memcpy %d bytes\n", decode.line_bytes));
memcpy (decode.outptr, decode.inptr, decode.line_bytes);
// no bits now
decode.used = 0;
// update the table
update_table (decode, decode.inptr, decode.width,
decode.table + 1);
// advance input pointer
decode.inptr += decode.line_bytes;
}
err_info *Filemax::decomp (decode_info &decode, byte *inptr, int bits_used, int width,
short *table_prev, short *table, int *bits_usedp)
{
int colour; // 0 == white, 1 == black
int x;
unsigned bits, *tab;
int bits_left;
byte *orig_inptr = inptr;
int offset;
int entry, code, count;
int pass;
int len;
table_prev++;
colour = 0;
x = -1;
*table++ = -1;
bits = (*inptr << 8) | inptr [1];
inptr += 2;
bits_left = 0x10 - bits_used;
debug3 (("decomp: starting with word=0x%x, bits_left=0x%x, width=0x%x\n", bits, bits_left, width));
while (x < width)
{
while (*table_prev <= x)
table_prev += 2;
debug3 (("1: %d, bits_left=0x%x, ", x, bits_left));
// get more bits if required
if (bits_left <= 0x10)
{
bits = (bits << 8) | *inptr++;
bits = (bits << 8) | *inptr++;
bits_left += 0x10;
}
// decode next 7 bits and lookup
entry = decode.tab7 [(bits >> (bits_left - 7)) & 0x7f];
len = entry & 0xffff;
debug3 (("word=%08x, 7bits=%x, data=%x, entry=%x, now %x",
bits, (bits >> (bits_left - 7)) & 0x7f,
(bits >> (bits_left - len)) & ((1 << len) - 1), entry, bits_left));
bits_left -= len;
if (!len)
return err_make (ERRFN, ERR_decompression_invalid_data);
code = (entry >> 16) - 3;
switch (code)
{
case 4 :
debug3 (("->h\n"));
for (pass = 2; pass != 0; pass--)
{
do
{
// get 16 more bits if needed
if (bits_left < 0x10)
{
bits = (bits << 8) | *inptr++;
bits = (bits << 8) | *inptr++;
bits_left += 0x10;
}
// decode next 13 bits and lookup in either white or black table
tab = colour == 0 ? decode.tab_first : decode.tab_second;
entry = tab [(bits >> (bits_left - 13)) & 0x1fff];
len = entry & 0xffff;
debug3 ((" 2%c: %d, word=%08x, 13bits=%x, data=%x, entry=0x%x, left=0x%x, ->%d\n",
colour ? 'b' : 'w',
x, bits, (bits >> (bits_left - 13)) & 0x1fff,
(bits >> (bits_left - len)) & ((1 << len) - 1), entry, bits_left,
x + (entry >> 16)));
bits_left -= len;
count = entry >> 16;
x += count;
} while (count > 0x3f);
*table++ = x;
colour = !colour;
}
break;
case 5 :
x = table_prev [1];
debug3 (("->p %d\n", x));
break;
default :
x = *table_prev + code;
*table++ = x;
debug3 (("->v %d\n", x));
if (x < width)
{
table_prev--;
colour = !colour;
}
break;
}
}
table [-1] = table [0] = table [1] = width;
offset = inptr - orig_inptr;
debug3 (("inptr advanced 0x%x, bits_used=0x%x, bits_left=0x%x", offset, bits_used, bits_left));
offset = offset * 8 - bits_used;