-
Notifications
You must be signed in to change notification settings - Fork 0
/
xvpic2.c
3608 lines (3174 loc) · 88.7 KB
/
xvpic2.c
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
/*
* $Id: xvpic2.c,v 2.9.1.14 1995/04/24 15:34:15 ikeyan Exp $
* xvpic2.c - load and save routines for `PIC2' format pictures.
*
*
* Outline
* =======
* xvpic2.c supports the PIC2 format image file. It is used some
* Japanese personal computer users.
*
* The PIC2 format is designed by A.Yanagisawa. It is an excellent
* format except for its encode/decode speed. ;-)
*
* The features of the PIC2 format:
* - Powerful header information (included author, filename, title,
* saver, product number, created date and comment).
* - Reversible compression, and very high compression ratio (in many
* cases, a higher compression ratio than the JPEG compression;
* because of its compression method, PIC2 is especially good at
* pictures like cell animation).
* - Can handle full-color (24 bits) image.
* - Can include multi image blocks into one PIC2 file.
* - Have four different block format (P2SS, P2SF, P2BM and
* P2BI). P2SS format uses arithmetic compression for storing
* data. P2SF uses normal run-length compression. P2BM and P2BI is
* raw image format. Select any one according to the situation.
*
* Explanation of the PIC2 compression:
* - In the first place, try to record pixel color, uses color caches
* which keep some recent colors, and formed according to color's
* frequency. PIC2 has some color cache spaces that are switched by
* upper pixel value of current pixel. If cache is hit, record
* that.
* - Unfortunately, in the case of color cache didn't hit, record the
* difference from the value estimated with the value of upper and
* left pixel of current pixel (similar to PNG's AVG predictor).
* - And extract image's color chain if exist, and record that (it
* results in image's outline).
* - In all cases, arithmetic compression is used in the final stage
* before writing the file, which in theory produces the ideal
* compression ratio (P2SS).
*
* Features
* ========
* - Support 3,6,9,12,15,18,21,24bit PIC2 format (Load/Save).
* - Support all image block formats of PIC2 (Load/Save).
* - Support multi block PIC2 file (Load/Save).
*
*
* Bugs
* ====
* - Unsupport 8bit PIC2 image file.
*
* If you find other bugs (surely exist :-)), send me bug-report.
*
*
* Author
* ======
* IKEMOTO Masahiro <[email protected]>
*/
#define PIC2_IGNORE_UNUSED_FUNCTIONS
#define NEEDSDIR
#include "xv.h"
#include <setjmp.h>
#ifdef HAVE_PIC2
typedef unsigned long pixel;
#define pic2_cextoshort(addr) ( \
(((short) (((byte *) addr)[0])) << 8) | \
( (short) (((byte *) addr)[1])) \
)
#define pic2_cextolong(addr) ( \
(((long) (((byte *) addr)[0])) << 24) | \
(((long) (((byte *) addr)[1])) << 16) | \
(((long) (((byte *) addr)[2])) << 8) | \
( (long) (((byte *) addr)[3])) \
)
#define pic2_shorttocex(addr, n) { \
((byte *) addr)[0] = (((unsigned short) (n) >> 8) & 0xff); \
((byte *) addr)[1] = ( (unsigned short) (n) & 0xff); \
}
#define pic2_longtocex(addr, n) { \
((byte *) addr)[0] = (((unsigned long) (n) >> 24) & 0xff); \
((byte *) addr)[1] = (((unsigned long) (n) >> 16) & 0xff); \
((byte *) addr)[2] = (((unsigned long) (n) >> 8) & 0xff); \
((byte *) addr)[3] = ( (unsigned long) (n) & 0xff); \
}
#define pic2_shift_bits(b, n) (((n) > 0) ? ((b) << (n)) : ((b) >> -(n)))
#define PIC2_READ_MODE 0
#define PIC2_WRITE_MODE 1
#define PIC2_ARITH_CACHE 32
#define PIC2_ARITH_CONTEXT 128
#define PIC2_FAST_CACHE 64
#define PIC2_HEADER_SIZE 124
#define PIC2_BLOCK_HEADER_SIZE 26
struct pic2_header {
char magic[4];
char name[18];
char subtitle[8];
char crlf0[2];
char title[30];
char crlf1[2];
char saver[30];
char crlf2[2];
char eof[1];
char reserve0[1];
short flag;
short no;
long time;
long size;
short depth;
short x_aspect;
short y_aspect;
short x_max;
short y_max;
long reserve1;
};
struct pic2_block {
char id[4];
long size;
short flag;
short x_wid;
short y_wid;
short x_offset;
short y_offset;
long opaque;
long reserve;
};
struct pic2_info {
jmp_buf jmp;
FILE *fp;
struct {
int rest;
byte cur;
int bits;
char zero;
}bs;
long fsize;
struct pic2_header *header;
struct pic2_block *block;
int n_pal;
int pal_bits;
byte pal[256][3];
char *comment;
char mode;
long next_pos;
long block_pos;
short x_max;
short y_max;
int ynow;
byte *buf;
pixel *vram_prev;
pixel *vram_now;
pixel *vram_next;
short *flag_now;
short *flag_next;
short *flag2_now;
short *flag2_next;
short *flag2_next2;
pixel (*cache)[PIC2_ARITH_CACHE];
unsigned short *cache_pos;
unsigned short *mulu_tab;
long aa;
long cc;
long dd;
char cache_hit_c;
int (*next_line) PARM((struct pic2_info *, pixel **));
char writing_grey;
char pagebname[64];
int pnum;
};
static void pic2_open_file PARM((struct pic2_info*,char*));
static void pic2_read_header PARM((struct pic2_info*));
static void pic2_read_block_header1 PARM((struct pic2_info*));
static void pic2_read_block_header2 PARM((struct pic2_info*));
static short pic2_arith_decode_bit PARM((struct pic2_info*,int));
static short pic2_arith_decode_nn PARM((struct pic2_info*,int));
static void pic2_arith_expand_chain PARM((struct pic2_info*,int,int,pixel));
static short pic2_arith_get_number PARM((struct pic2_info*,int,int));
static pixel pic2_arith_read_color PARM((struct pic2_info*,int));
static int pic2_arith_expand_line PARM((struct pic2_info*,pixel**));
static int pic2_arith_loader_init PARM((struct pic2_info*));
static int pic2_fast_read_length PARM((struct pic2_info*));
static void pic2_fast_expand_chain PARM((struct pic2_info*,int,pixel));
static pixel pic2_fast_read_color PARM((struct pic2_info*,pixel));
static int pic2_fast_expand_line PARM((struct pic2_info*,pixel**));
static int pic2_fast_loader_init PARM((struct pic2_info*));
static int pic2_beta_expand_line PARM((struct pic2_info*,pixel**));
static int pic2_beta_loader_init PARM((struct pic2_info*));
static void pic2_make_xvpic PARM((struct pic2_info*,byte**,
byte*,byte*,byte*));
static void pic2_make_pagefile PARM((struct pic2_info*,char*,int));
static void pic2_setup_pic2_info PARM((struct pic2_info*,
char*,char*,char*,char*,
int,int,int,int,int,int,char *));
static void pic2_append PARM((struct pic2_info*));
static void pic2_write_header1 PARM((struct pic2_info*));
static void pic2_write_header2 PARM((struct pic2_info*));
static void pic2_write_block_header PARM((struct pic2_info*));
static void pic2_arith_write_zero_bit PARM((struct pic2_info*));
static void pic2_arith_flush_bit_buf PARM((struct pic2_info*));
static void pic2_arith_carry_bit PARM((struct pic2_info*));
static void pic2_arith_encode_bit PARM((struct pic2_info*,int,int));
static void pic2_arith_encode_nbyte PARM((struct pic2_info*,int,int,int));
static void pic2_arith_encode_nn PARM((struct pic2_info*,int,int));
static void pic2_arith_press_chain PARM((struct pic2_info*,int));
static void pic2_arith_put_number PARM((struct pic2_info*,int,int,int));
static void pic2_arith_write_color PARM((struct pic2_info*,int));
static void pic2_arith_press_line2 PARM((struct pic2_info*));
static int pic2_arith_press_line PARM((struct pic2_info*,pixel**));
static int pic2_arith_saver_init PARM((struct pic2_info*,pixel**));
static void pic2_fast_write_length PARM((struct pic2_info*,int));
static void pic2_fast_press_chain PARM((struct pic2_info*,int));
static void pic2_fast_press_chain2 PARM((struct pic2_info*,int));
static void pic2_fast_flush_chain PARM((struct pic2_info*));
static void pic2_fast_write_color PARM((struct pic2_info*,int));
static void pic2_fast_press_line2 PARM((struct pic2_info*));
static int pic2_fast_press_line PARM((struct pic2_info*,pixel**));
static int pic2_fast_saver_init PARM((struct pic2_info*,pixel**));
static int pic2_beta_press_line PARM((struct pic2_info*,pixel**));
static int pic2_beta_saver_init PARM((struct pic2_info*,pixel**));
static void pic2_write_data PARM((struct pic2_info*,byte*,
int,int,int,int,int,
byte*,byte*,byte*,int,int));
static int pic2_next_line PARM((struct pic2_info*,pixel**));
static int pic2_next_block PARM((struct pic2_info*));
static int pic2_find_block PARM((struct pic2_info*));
static int pic2_load_block PARM((struct pic2_info*));
static int pic2_save_block PARM((struct pic2_info*,pixel**,
int,int,int,int,char*,pixel));
#ifndef PIC2_IGNORE_UNUSED_FUNCTIONS
static void pic2_read_palette PARM((struct pic2_info*,
byte*,byte*,byte*));
static void pic2_write_palette PARM((struct pic2_info*,int,int,
byte*,byte*,byte*));
#endif /* !PIC2_IGNORE_UNUSED_FUNCTIONS */
static byte pic2_convert_color_bits PARM((int,int,int));
static byte pic2_pad_color_bits PARM((int,int,int));
static byte pic2_reduce_color_bits PARM((int,int,int));
static pixel pic2_exchange_rg PARM((pixel,int));
static void pic2_handle_para PARM((struct pic2_info*,int));
static int pic2_alloc_buffer PARM((struct pic2_info*));
static void pic2_free_buffer PARM((struct pic2_info*));
static long pic2_seek_file PARM((struct pic2_info*,long,int));
static long pic2_tell_file PARM((struct pic2_info*));
static int pic2_read_file PARM((struct pic2_info*,void*,size_t));
static long pic2_read_long PARM((struct pic2_info*));
static short pic2_read_short PARM((struct pic2_info*));
static char pic2_read_char PARM((struct pic2_info*));
static int pic2_write_file PARM((struct pic2_info*,void*,size_t));
static int pic2_write_long PARM((struct pic2_info*,long));
static int pic2_write_short PARM((struct pic2_info*,int));
static int pic2_write_char PARM((struct pic2_info*,int));
static unsigned long pic2_read_bits PARM((struct pic2_info*,int));
static void pic2_write_bits PARM((struct pic2_info*,
unsigned long,int));
static void pic2_flush_bits PARM((struct pic2_info*));
static void pic2_memory_error PARM((char*,char*));
static void pic2_error PARM((struct pic2_info*,int));
static void pic2_file_error PARM((struct pic2_info*,int));
static void pic2_init_info PARM((struct pic2_info*));
static void pic2_cleanup_pic2_info PARM((struct pic2_info*,int));
static void pic2_cleanup_pinfo PARM((PICINFO*));
static void pic2_show_pic2_info PARM((struct pic2_info*));
static char *pic2_strncpy PARM((char*,char*,size_t));
static void *pic2_malloc PARM((size_t,char*));
static void *pic2_new PARM((size_t,char*));
static int WritePIC2 PARM((FILE*,byte*,int,int,int,
byte*,byte*,byte*,int,int,char*,
int,int,int,int,int,char*));
static char *pic2_id = "P2DT";
/* Error Messages */
static char *pic2_msgs[] = {
NULL,
#define PIC2_OPEN 1
"can't open file.",
#define PIC2_CORRUPT 2
"file corrupted.",
#define PIC2_FORMAT 3
"not PIC2 format.",
#define PIC2_DEPTH 4
"bit depths not divisible by 3 are unsupported.",
#define PIC2_TMPFILE 5
"unable to create temporary filename???",
#define PIC2_PAGE 6
"couldn't load the page.",
#define PIC2_APPEND 7
"cannot append.",
#define PIC2_WRITE 8
"write failed.",
};
struct _form_tab {
char *id;
int (*loader_init) PARM((struct pic2_info *));
int (*saver_init) PARM((struct pic2_info *, pixel **));
} form_tab[] = {
{ "P2SS", pic2_arith_loader_init, pic2_arith_saver_init},
{ "P2SF", pic2_fast_loader_init, pic2_fast_saver_init},
{ "P2BM", pic2_beta_loader_init, pic2_beta_saver_init},
{ "P2BI", pic2_beta_loader_init, pic2_beta_saver_init},
};
#define n_form_tab (sizeof(form_tab) / sizeof(struct _form_tab))
#define P2SS 0
#define P2SF 1
#define P2BM 2
#define P2BI 3
/* The main routine to load a PIC2 file. */
int LoadPIC2(fname, pinfo, quick)
char *fname;
PICINFO *pinfo;
int quick;
{
int e, i, block;
struct pic2_info pic2;
if (DEBUG)
fputs("LoadPIC2:\n", stderr);
pic2_init_info(&pic2);
if ((e = setjmp(pic2.jmp)) != 0){
/* When an error occurs, comes here. */
pic2_free_buffer(&pic2);
pic2_cleanup_pic2_info(&pic2, 0);
pic2_cleanup_pinfo(pinfo);
if (pic2split)
KillPageFiles(pic2.pagebname, pic2.pnum);
SetCursors(-1);
if (DEBUG)
fputs("\n", stderr);
return (0);
}
pic2_open_file(&pic2, fname);
pic2_read_header(&pic2);
if ((i = pic2_find_block(&pic2)) == 0)
pic2_file_error(&pic2, PIC2_CORRUPT);
block = 1;
while(i == 2) {
SetISTR(ISTR_WARNING, "unknown or invalid block #%d.", block);
i = pic2_next_block(&pic2);
block++;
}
if (pic2split && !quick) {
char firstpage[512];
struct stat st;
#ifndef USE_MKSTEMP
int tmpfd;
#endif
#ifndef VMS
sprintf(pic2.pagebname, "%s/xvpic2XXXXXX", tmpdir);
#else
sprintf(pic2.pagebname, "Sys$Scratch:xvpic2XXXXXX");
#endif
#ifdef USE_MKSTEMP
close(mkstemp(pic2.pagebname));
#else
mktemp(pic2.pagebname);
tmpfd = open(pic2.pagebname, O_WRONLY|O_CREAT|O_EXCL, S_IRWUSR);
if (tmpfd < 0) FatalError("LoadPIC2(): can't create temporary file");
close(tmpfd);
#endif
if (pic2.pagebname[0] == '\0')
pic2_error(&pic2, PIC2_TMPFILE);
strcat(pic2.pagebname, ".");
sprintf(firstpage, "%s%d", pic2.pagebname, 1);
if (stat(firstpage, &st)) {
for (pic2.pnum = 1; i >= 1; pic2.pnum++) {
pic2_load_block(&pic2);
pic2_make_pagefile(&pic2, pic2.pagebname, pic2.pnum);
while(block++, (i = pic2_next_block(&pic2)) == 2)
SetISTR(ISTR_WARNING,
"unknown or invalid block #%d.", block);
}
pinfo->numpages = --pic2.pnum;
if (!LoadPIC2(firstpage, pinfo, 1))
pic2_error(&pic2, PIC2_PAGE);
if (pic2.pnum == 1)
unlink(firstpage);
else
strcpy(pinfo->pagebname, pic2.pagebname);
} else
if (!LoadPIC2(fname, pinfo, 1))
pic2_error(&pic2, PIC2_PAGE);
} else {
char buf[128], format[64];
int j;
pinfo->w = pic2.x_max;
pinfo->h = pic2.y_max;
pinfo->normw = pinfo->w;
pinfo->normh = pinfo->h;
pinfo->type = PIC24;
for (j = 0; j < n_form_tab; j++) {
if (xvbcmp(pic2.block->id, form_tab[j].id, (size_t) 4) == 0)
break;
}
pinfo->frmType = F_PIC2;
pinfo->colType = F_FULLCOLOR;
pinfo->comment = pic2.comment;
if (pic2split) {
pic2_make_xvpic(&pic2, &pinfo->pic, pinfo->r, pinfo->g, pinfo->b);
strcpy(format, form_tab[j].id);
} else {
for (pic2.pnum = 1; i >= 1; pic2.pnum++) {
SetISTR(ISTR_INFO, "composing block #%d", block);
pic2_make_xvpic(&pic2, &pinfo->pic,
pinfo->r, pinfo->g, pinfo->b);
while(block++, (i = pic2_next_block(&pic2)) == 2)
SetISTR(ISTR_WARNING,
"unknown or invalid block #%d.", block);
}
if (--block > 1)
if (block != --pic2.pnum)
sprintf(format, "MultiBlock[%d/%d]", block, pic2.pnum);
else
sprintf(format, "MultiBlock[%d]", block);
else
strcpy(format, form_tab[j].id);
}
sprintf(buf, "PIC2(%s). %d colors (%ld bytes)", format,
(int) 1 << pic2.header->depth, pic2.fsize);
strcat(pinfo->fullInfo, buf);
sprintf(pinfo->shrtInfo, "%dx%d(aspect %4.2f) PIC2(%s).",
pinfo->w, pinfo->h,
(float) pic2.header->x_aspect / (float) pic2.header->y_aspect,
format);
if (!nopicadjust)
normaspect = (float) pic2.header->x_aspect
/ (float) pic2.header->y_aspect;
}
pic2_cleanup_pic2_info(&pic2, 0);
SetCursors(-1);
if (DEBUG)
fputs("\n", stderr);
return (1);
}
/*
* This function opens the file, and set its size.
*/
static void pic2_open_file(pi, fname)
struct pic2_info *pi;
char *fname;
{
if ((pi->fp = fopen(fname, "rb")) == NULL)
pic2_file_error(pi, PIC2_OPEN);
fseek(pi->fp, (size_t) 0, SEEK_END);
pi->fsize = ftell(pi->fp);
fseek(pi->fp, (size_t) 0, SEEK_SET);
}
/*
* These functions read the PIC2 header informations.
* pic2_read_header:
* reads the PIC2 header.
* pic2_read_block_header1:
* reads the id number of block header and the size of block.
* pic2_read_block_header2:
* reads the rest of block header.
*/
static void pic2_read_header(pi)
struct pic2_info *pi;
{
long s_comment;
pi->mode = PIC2_READ_MODE;
/* read header image */
pic2_read_file(pi, pi->header->magic, 4);
pic2_read_file(pi, pi->header->name, 18);
pic2_read_file(pi, pi->header->subtitle, 8);
pic2_read_file(pi, pi->header->crlf0, 2);
pic2_read_file(pi, pi->header->title, 30);
pic2_read_file(pi, pi->header->crlf1, 2);
pic2_read_file(pi, pi->header->saver, 30);
pic2_read_file(pi, pi->header->crlf2, 2);
pic2_read_file(pi, pi->header->eof, 1);
pic2_read_file(pi, pi->header->reserve0, 1);
pi->header->flag = pic2_read_short(pi);
pi->header->no = pic2_read_short(pi);
pi->header->time = pic2_read_long(pi);
pi->header->size = pic2_read_long(pi);
pi->header->depth = pic2_read_short(pi);
pi->header->x_aspect = pic2_read_short(pi);
pi->header->y_aspect = pic2_read_short(pi);
pi->header->x_max = pic2_read_short(pi);
pi->header->y_max = pic2_read_short(pi);
pi->header->reserve1 = pic2_read_long(pi);
/* check magic number */
if (strncmp(pi->header->magic, pic2_id, (size_t) 4) != 0)
pic2_error(pi, PIC2_FORMAT);
/* read palette data, if exists */
if (pi->header->flag & 1) {
pi->pal_bits = pic2_read_char(pi);
pi->n_pal = pic2_read_short(pi);
pic2_read_file(pi, pi->pal, (size_t) (pi->n_pal * 3));
}
/* read comments */
s_comment = pi->header->size - pic2_tell_file(pi);
pi->comment = pic2_new(s_comment + 1, "pic2_read_header");
pic2_read_file(pi, pi->comment, (size_t) s_comment);
pi->comment[s_comment] = '\0';
pi->x_max = pi->header->x_max;
pi->y_max = pi->header->y_max;
/* set initial block point */
pi->next_pos = pic2_tell_file(pi);
}
static void pic2_read_block_header1(pi)
struct pic2_info *pi;
{
pic2_read_file(pi, pi->block->id, 4);
pi->block->size = pic2_read_long(pi);
}
static void pic2_read_block_header2(pi)
struct pic2_info *pi;
{
pi->block->flag = pic2_read_short(pi);
pi->block->x_wid = pic2_read_short(pi);
pi->block->y_wid = pic2_read_short(pi);
pi->block->x_offset = pic2_read_short(pi);
pi->block->y_offset = pic2_read_short(pi);
pi->block->opaque = pic2_read_long(pi);
pi->block->reserve = pic2_read_long(pi);
}
/*
* These functions are arithmetic pic2 format extractor.
*/
static short pic2_arith_decode_bit(pi, c)
struct pic2_info *pi;
int c;
{
unsigned short pp;
pp = pi->mulu_tab[(pi->aa & 0x7f00) / 2 + c];
if (pi->dd >= (int) pp) {
pi->dd -= pp;
pi->aa -= pp;
while ((short) pi->aa >= 0) {
pi->dd *= 2;
if (pic2_read_bits(pi, 1))
pi->dd++;
pi->aa *= 2;
}
return (1);
} else {
pi->aa = pp;
while ((short) pi->aa >= 0) {
pi->dd *= 2;
if (pic2_read_bits(pi, 1))
pi->dd++;
pi->aa *= 2;
}
return (0);
}
}
static short pic2_arith_decode_nn(pi, c)
struct pic2_info *pi;
int c;
{
int n;
if (pic2_arith_decode_bit(pi, c)) {
/* n < 1 */
n = 0;
} else if (pic2_arith_decode_bit(pi, c + 1)) {
/* n < 1 + 2 */
n = 1;
if (pic2_arith_decode_bit(pi, c + 8))
n += 1;
} else if (pic2_arith_decode_bit(pi, c + 2)) {
/* n < 1 + 2 + 4 */
n = 1 + 2;
if (pic2_arith_decode_bit(pi, c + 8))
n += 1;
if (pic2_arith_decode_bit(pi, c + 9))
n += 2;
} else if (pic2_arith_decode_bit(pi, c + 3)) {
/* n < 1 + 2 + 4 + 8 */
n = 1 + 2 + 4;
if (pic2_arith_decode_bit(pi, c + 8))
n += 1;
if (pic2_arith_decode_bit(pi, c + 9))
n += 2;
if (pic2_arith_decode_bit(pi, c + 10))
n += 4;
} else if (pic2_arith_decode_bit(pi, c + 4)) {
/* n < 1 + 2 + 4 + 8 + 16 */
n = 1 + 2 + 4 + 8;
if (pic2_arith_decode_bit(pi, c + 8))
n += 1;
if (pic2_arith_decode_bit(pi, c + 9))
n += 2;
if (pic2_arith_decode_bit(pi, c + 10))
n += 4;
if (pic2_arith_decode_bit(pi, c + 11))
n += 8;
} else if (pic2_arith_decode_bit(pi, c + 5)) {
/* n < 1 + 2 + 4 + 8 + 16 + 32 */
n = 1 + 2 + 4 + 8 + 16;
if (pic2_arith_decode_bit(pi, c + 8))
n += 1;
if (pic2_arith_decode_bit(pi, c + 9))
n += 2;
if (pic2_arith_decode_bit(pi, c + 10))
n += 4;
if (pic2_arith_decode_bit(pi, c + 11))
n += 8;
if (pic2_arith_decode_bit(pi, c + 12))
n += 16;
} else if (pic2_arith_decode_bit(pi, c + 6)) {
/* n < 1 + 2 + 4 + 8 + 16 + 32 + 64 */
n = 1 + 2 + 4 + 8 + 16 + 32;
if (pic2_arith_decode_bit(pi, c + 8))
n += 1;
if (pic2_arith_decode_bit(pi, c + 9))
n += 2;
if (pic2_arith_decode_bit(pi, c + 10))
n += 4;
if (pic2_arith_decode_bit(pi, c + 11))
n += 8;
if (pic2_arith_decode_bit(pi, c + 12))
n += 16;
if (pic2_arith_decode_bit(pi, c + 13))
n += 32;
} else if (pic2_arith_decode_bit(pi, c + 7)) {
/* n < 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 */
n = 1 + 2 + 4 + 8 + 16 + 32 + 64;
if (pic2_arith_decode_bit(pi, c + 8))
n += 1;
if (pic2_arith_decode_bit(pi, c + 9))
n += 2;
if (pic2_arith_decode_bit(pi, c + 10))
n += 4;
if (pic2_arith_decode_bit(pi, c + 11))
n += 8;
if (pic2_arith_decode_bit(pi, c + 12))
n += 16;
if (pic2_arith_decode_bit(pi, c + 13))
n += 32;
if (pic2_arith_decode_bit(pi, c + 14))
n += 64;
} else {
n = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128;
}
return (n);
}
static void pic2_arith_expand_chain(pi, x, y, cc)
struct pic2_info *pi;
int x, y;
pixel cc;
{
static const unsigned short c_tab[] = {
80 + 6 * 5, /* -5 */
80 + 6 * 4,
80 + 6 * 3,
80 + 6 * 2,
80 + 6 * 1,
80 + 6 * 0, /* 0 */
80 + 6 * 0, /* 1 */
};
unsigned short b;
b = c_tab[pi->flag_now[x] + 5];
if (!pic2_arith_decode_bit(pi, b++)) {
if (pic2_arith_decode_bit(pi, b++)) { /* down */
pi->vram_next[x ] = cc;
pi->flag_next[x ] = -1;
} else if (pic2_arith_decode_bit(pi, b++)) { /* left */
pi->vram_next[x - 1] = cc;
pi->flag_next[x - 1] = -2;
} else if (pic2_arith_decode_bit(pi, b++)) { /* right */
pi->vram_next[x + 1] = cc;
pi->flag_next[x + 1] = -3;
} else if (pic2_arith_decode_bit(pi, b++)) { /* left2 */
pi->vram_next[x - 2] = cc;
pi->flag_next[x - 2] = -4;
} else { /* right2 */
pi->vram_next[x + 2] = cc;
pi->flag_next[x + 2] = -5;
}
}
}
static short pic2_arith_get_number(pi, c, bef)
struct pic2_info *pi;
int c, bef;
{
unsigned short n;
byte maxcol;
maxcol = 0xff >> (8 - pi->header->depth / 3);
n = pic2_arith_decode_nn(pi, c);
if (bef > ((int) maxcol >> 1)) {
if (n > ((int) maxcol - bef) * 2)
n = maxcol - n;
else if (n & 1)
n = n / 2 + bef + 1;
else
n = bef - n / 2;
} else {
if ((int) n > (bef * 2))
n = n;
else if (n & 1)
n = n / 2 + bef + 1;
else
n = bef - n / 2;
}
return (n);
}
static pixel pic2_arith_read_color(pi, x)
struct pic2_info *pi;
int x;
{
pixel c1, c2, cc;
unsigned short i, j, k, m;
short r, g, b, r0, g0, b0;
short colbits;
pixel rmask, gmask, bmask;
byte maxcol;
colbits = pi->header->depth / 3;
rmask = (0xff >> (8 - colbits)) << (colbits * 2);
gmask = (0xff >> (8 - colbits)) << colbits;
bmask = (0xff >> (8 - colbits));
maxcol = (byte) bmask;
c1 = pi->vram_prev[x];
k = ((c1 >> ((colbits - 3) * 3)) & 0x1c0)
| ((c1 >> ((colbits - 3) * 2)) & 0x038)
| ((c1 >> (colbits - 3) ) & 0x007);
if (colbits == 5)
k = pic2_exchange_rg(k, 3);
if (pic2_arith_decode_bit(pi, pi->cache_hit_c)) { /* ouch */
pi->cache_hit_c = 16;
c2 = pi->vram_now[x - 1];
r = ((c1 & rmask) + (c2 & rmask)) >> (colbits * 2 + 1);
g = ((c1 & gmask) + (c2 & gmask)) >> (colbits + 1);
b = ((c1 & bmask) + (c2 & bmask)) >> ( 1);
g0 = pic2_arith_get_number(pi, 32, g);
r = r + g0 - g;
if (r > (short) maxcol)
r = maxcol;
else if (r < 0)
r = 0;
b = b + g0 - g;
if (b > (short) maxcol)
b = maxcol;
else if (b < 0)
b = 0;
r0 = pic2_arith_get_number(pi, 48, r);
b0 = pic2_arith_get_number(pi, 64, b);
pi->cache_pos[k] = j = (pi->cache_pos[k] - 1) & (PIC2_ARITH_CACHE - 1);
pi->cache[k][j] = cc = (r0 << (colbits * 2)) | (g0 << colbits) | b0;
} else {
pi->cache_hit_c = 15;
j = pic2_arith_decode_nn(pi, 17);
m = pi->cache_pos[k];
i = (m + j / 2) & (PIC2_ARITH_CACHE - 1);
j = (m + j) & (PIC2_ARITH_CACHE - 1);
cc = pi->cache[k][j];
pi->cache[k][j] = pi->cache[k][i];
pi->cache[k][i] = pi->cache[k][m];
pi->cache[k][m] = cc;
}
return (cc);
}
static int pic2_arith_expand_line(pi, line)
struct pic2_info *pi;
pixel **line;
{
int ymax;
int x, xw;
pixel cc;
pic2_handle_para(pi, 0);
xw = pi->block->x_wid;
ymax = pi->block->y_wid - 1;
if (pi->ynow > ymax)
return (-2); /* end */
/* set right end of previous line before left end of current line. */
if (pi->ynow == 0) {
cc = 0;
} else
cc = pi->vram_prev[xw - 1];
pi->vram_now[-1] = cc;
/* clear flag for change point */
xvbzero((char *) pi->flag_next, xw * sizeof(pi->flag_next[0]));
/* clear flag for position probability space */
xvbzero((char *) pi->flag2_next2, xw * sizeof(pi->flag2_next2[0]));
for (x = 0; x < xw; x++) {
if (pi->flag_now[x] < 0) {
cc = pi->vram_now[x];
if (pi->ynow < ymax)
pic2_arith_expand_chain(pi, x, pi->ynow, cc);
} else if (pic2_arith_decode_bit(pi, pi->flag2_now[x])) {
/* ajust probability space around of change point */
pi->flag2_now [x + 1]++;
pi->flag2_now [x + 2]++;
pi->flag2_next [x - 1]++;
pi->flag2_next [x ]++;
pi->flag2_next [x + 1]++;
pi->flag2_next2[x - 1]++;
pi->flag2_next2[x ]++;
pi->flag2_next2[x + 1]++;
pi->vram_now[x] = cc = pic2_arith_read_color(pi, x);
if (pi->ynow < ymax)
pic2_arith_expand_chain(pi, x, pi->ynow, cc);
} else
pi->vram_now[x] = cc;
}
if (line != NULL)
*line = pi->vram_now;
pi->ynow++;
pic2_handle_para(pi, 1);
return (pi->ynow - 1);
}
static int pic2_arith_loader_init(pi)
struct pic2_info *pi;
{
unsigned short p2b[256];
int i, xw;
pi->ynow = 0;
/* check the color depth */
if (pi->header->depth % 3)
pic2_error(pi, PIC2_DEPTH);
/* set function for extract next line */
pi->next_line = pic2_arith_expand_line;
/* clear cache and flags */
xw = pi->block->x_wid;
xvbzero((char *) pi->cache, 8 * 8 * 8 * sizeof(pi->cache[0]));
xvbzero((char *) pi->cache_pos, 8 * 8 * 8 * sizeof(pi->cache_pos[0]));
xvbzero((char *) pi->flag_now, xw * sizeof(pi->flag_now[0]));
xvbzero((char *) pi->flag2_now, 8 + xw * sizeof(pi->flag2_now[0]));
xvbzero((char *) pi->flag2_next, 8 + xw * sizeof(pi->flag2_next[0]));
/* go to picture data field */
pic2_seek_file(pi, pi->block_pos + PIC2_BLOCK_HEADER_SIZE, SEEK_SET);
/* clear bit field marker */
pi->bs.rest = 0;
pi->bs.cur = 0;
/* read probability table */
for (i = 0; i < PIC2_ARITH_CONTEXT; i++)
p2b[i] = pic2_read_short(pi);
/* make multiplication table */
for (i = 0; i < 16384; i++) {
pi->mulu_tab[i] = (long) (i / 128 + 128) * (int) p2b[i & 127] / 256;
if (pi->mulu_tab[i] == 0) pi->mulu_tab[i] = 1;
}
/* initialize some valuables */
pi->aa = 0xffff;
pi->dd = 0;
for (i = 0; i < 16; i++) {
pi->dd *= 2;
if (pic2_read_bits(pi, 1))
pi->dd |= 1;
}
pi->cache_hit_c = 16;
return (0);
}
/*
* These functions are fast pic2 compression extractor.
*/
static int pic2_fast_read_length(pi)
struct pic2_info *pi;
{
int a;
a = 0;
while (pic2_read_bits(pi, 1)) {
a++;
}
if (a == 0)
return (0);
return (pic2_read_bits(pi, a) + (1 << a) - 1);
}
static void pic2_fast_expand_chain(pi, x, cc)
struct pic2_info *pi;
int x;
pixel cc;
{
if (pic2_read_bits(pi, 1) != 0) {
if (pic2_read_bits(pi, 1) != 0) { /* down */
pi->vram_next[x] = cc;
pi->flag_next[x] = -1;
} else if (pic2_read_bits(pi, 1) != 0) {
if (pic2_read_bits(pi, 1) == 0) { /* left2down */
pi->vram_next[x - 2] = cc;
pi->flag_next[x - 2] = -1;
} else { /* left1down */
pi->vram_next[x - 1] = cc;
pi->flag_next[x - 1] = -1;
}
} else {
if (pic2_read_bits(pi, 1) == 0) { /* right2down */
pi->vram_next[x + 2] = cc;
pi->flag_next[x + 2] = -1;
} else { /* left1down */
pi->vram_next[x + 1] = cc;
pi->flag_next[x + 1] = -1;
}
}
}
}
static pixel pic2_fast_read_color(pi, bc)
struct pic2_info *pi;
pixel bc;
{
pixel cc;
unsigned short j, k, m;
short depth, colbits;
pixel (*cache)[PIC2_FAST_CACHE];
depth = pi->header->depth;
colbits = depth / 3;
cache = (pixel (*)[PIC2_FAST_CACHE]) pi->cache;
bc = pic2_exchange_rg(bc, colbits);
k = pic2_shift_bits(bc, 8 - depth);
if (pic2_read_bits(pi, 1) == 0) {
pi->cache_pos[k] = m = (pi->cache_pos[k] - 1) & (PIC2_FAST_CACHE - 1);
cc = pic2_read_bits(pi, depth);
cc = pic2_exchange_rg(cc, colbits);
cache[k][m] = cc;
} else {
j = pic2_read_bits(pi, 6); /* 6= log2(PIC2_FAST_CACHE) */
m = pi->cache_pos[k];
cc = cache[k][(m + j) & (PIC2_FAST_CACHE - 1)];
}
return (cc);