-
Notifications
You must be signed in to change notification settings - Fork 4
/
xvpic.c
1285 lines (1167 loc) · 29 KB
/
xvpic.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
/*
* xvpic.c - load routine for `PIC' format pictures.
*
* The `PIC' format is used by many Japanese personal computer users.
*/
#include "xv.h"
#include <setjmp.h>
#ifdef HAVE_PIC
typedef unsigned short data16;
typedef unsigned int data32;
struct pic_info {
jmp_buf jmp;
FILE *fp;
struct {
int rest;
byte cur;
}bs;
long fsize;
int type, mode;
int width, height;
float aspect;
int cbits;
int cmapped;
byte *cmap;
int cached;
struct cache_t {
int newest;
struct cachenode_t {
data32 dat;
int newer, older;
} *node;
} cache;
int g_bits, r_bits, b_bits, i_bits;
int inv_gr;
int tiled256;
int numcols;
int writing_grey;
data32 *data;
};
static void pic_open_file PARM((struct pic_info*,char*));
static void pic_check_id PARM((struct pic_info*));
static void pic_read_comment PARM((struct pic_info*, char**));
static void pic_read_header PARM((struct pic_info*));
static void pic_expand_data PARM((struct pic_info*));
static int pic_expanding_read_len PARM((struct pic_info*));
static data32 pic_expanding_read_color PARM((struct pic_info*));
static void pic_expanding_read_chain
PARM((struct pic_info*, int, int, data32));
static void pic_make_xvpic
PARM((struct pic_info*, byte**, byte*, byte*, byte*));
static void pic_write_id PARM((struct pic_info*));
static void pic_write_comment PARM((struct pic_info*, char*));
static void pic_write_header PARM((struct pic_info*));
static void pic_write_palette
PARM((struct pic_info*, byte*, byte*, byte*));
static void pic_make_sparse_data PARM((struct pic_info*, byte*));
static void pic_write_data PARM((struct pic_info*));
static void pic_write_length PARM((struct pic_info*, data32));
static void pic_write_color PARM((struct pic_info*, data32));
static void pic_write_chain
PARM((struct pic_info*, int, int, data32));
static data32 pic_read_rgb PARM((struct pic_info*));
static data32 pic_read_color_code PARM((struct pic_info*));
static void pic_write_rgb PARM((struct pic_info*, data32));
static void pic_write_color_code PARM((struct pic_info*, data32));
static void pic_cache_init PARM((struct pic_info*));
static data32 pic_cache_get_value PARM((struct pic_info*, int));
static void pic_cache_add_value PARM((struct pic_info*, data32));
static int pic_cache_lookup PARM((struct pic_info*, data32));
static data32 pic_read_bits PARM((struct pic_info*, int));
static void pic_write_bits PARM((struct pic_info*, data32, int));
static byte pic_pad_bit PARM((int, data32));
static void pic_init_info PARM((struct pic_info*));
static void pic_cleanup_pic_info PARM((struct pic_info*, int));
static void pic_cleanup_pinfo PARM((PICINFO*));
static void pic_memory_error PARM((char*, char*));
static void pic_error PARM((struct pic_info*, int));
static void pic_file_error PARM((struct pic_info*, int));
static void pic_file_warning PARM((struct pic_info*, int));
static void pic_show_pic_info PARM((struct pic_info*));
static void *pic_malloc PARM((size_t, char*));
static void *pic_realloc PARM((void*, size_t, char*));
static char *pic_id = "PIC";
/* Error Messages */
static char *pic_msgs[] = {
NULL,
#define PIC_OPEN 1
"can't open file.",
#define PIC_CORRUPT 2
"file corrupted.",
#define PIC_FORMAT 3
"not PIC format.",
#define PIC_SUPPORT 4
"unsupported type.",
#define PIC_COMMENT 5
"can't read comment.",
#define PIC_TYPE 6
"bad machine type.",
#define PIC_MODE 7
"bad machine-dependent mode.",
#define PIC_NUM_COLORS 8
"bad number of colors.",
#define PIC_SIZE 9
"bad size.",
#define PIC_ASPECT 10
"bad aspect.",
#define PIC_WRITE 11
"write failed.",
};
#define H4(b) (((b) >> 4) & 0x0f)
#define L4(b) ( (b) & 0x0f)
/* The main routine to load a PIC file. */
int LoadPIC(fname, pinfo)
char *fname;
PICINFO *pinfo;
{
int e;
struct pic_info pic;
char buf[128];
if(DEBUG) fputs("LoadPIC:\n", stderr);
pic_init_info(&pic);
pinfo->comment = NULL;
if((e = setjmp(pic.jmp)) != 0){
/* When an error occurs, comes here. */
pic_cleanup_pic_info(&pic, 0);
pic_cleanup_pinfo(pinfo);
if(DEBUG) fputs("\n", stderr);
return 0;
}
pic_open_file(&pic, fname);
pic_check_id(&pic);
pic_read_comment(&pic, &pinfo->comment);
pic_read_header(&pic);
pic_expand_data(&pic);
pic_make_xvpic(&pic, &pinfo->pic, pinfo->r, pinfo->g, pinfo->b);
pinfo->w = pic.width;
if(pic.tiled256)
pinfo->h = pic.height * 2;
else
pinfo->h = pic.height;
pinfo->normw = pinfo->w;
pinfo->normh = pinfo->h;
pinfo->type = pic.cmapped ? PIC8 : PIC24;
pinfo->frmType = F_PIC;
pinfo->colType = F_FULLCOLOR;
strcpy(pinfo->fullInfo, "PIC");
switch(pic.type){
case 0x0:
strcat(pinfo->fullInfo, ", X68k");
break;
case 0x1:
strcat(pinfo->fullInfo, ", PC-88VA");
if(pic.mode & 1)
strcat(pinfo->fullInfo, ", HR");
if(pic.mode & 2)
strcat(pinfo->fullInfo, ", tiled 256");
break;
case 0x2:
strcat(pinfo->fullInfo, ", FM-TOWNS");
if(pic.mode == 0x5){
strcat(pinfo->fullInfo, ", low-resolution");
}else{
strcat(pinfo->fullInfo, ", high-resolution");
}
break;
case 0x3:
strcat(pinfo->fullInfo, ", Macintosh");
break;
case 0xf:
;
}
sprintf(buf, " (%ld bytes)", pic.fsize);
strcat(pinfo->fullInfo, buf);
sprintf(pinfo->shrtInfo, "%dx%d(aspect %4.2f) PIC.",
pinfo->w, pinfo->h, pic.aspect);
if (!nopicadjust)
normaspect = pic.aspect;
pic_cleanup_pic_info(&pic, 0);
if(DEBUG) fputs("\n", stderr);
return 1;
}
static void pic_open_file(pi, fname)
struct pic_info *pi;
char *fname;
{
if((pi->fp = fopen(fname, "rb")) == NULL)
pic_file_error(pi, PIC_OPEN);
fseek(pi->fp, (size_t) 0, SEEK_END);
pi->fsize = ftell(pi->fp);
fseek(pi->fp, (size_t) 0, SEEK_SET);
}
static void pic_check_id(pi)
struct pic_info *pi;
{
char buf[3];
if(fread(buf, (size_t) 3, (size_t) 1, pi->fp) != 1)
pic_file_error(pi, PIC_CORRUPT);
if(strncmp(buf, pic_id, (size_t) 3) != 0)
pic_error(pi, PIC_FORMAT);
}
static void pic_read_comment(pi, comm)
struct pic_info *pi;
char **comm;
{
/* The comment field is like:
* comment-string ^Z dummy \0 \0
*/
int max = -1, i = 0;
int c;
while(1){
if((c = fgetc(pi->fp)) == EOF)
pic_file_error(pi, PIC_CORRUPT);
if(c == '\032') /* 0x1a, '^Z' */
break;
if(max < i){
max += 32;
*comm = pic_realloc(*comm, (size_t) max + 1, "pic_read_comment#1");
}
(*comm)[i++] = c;
}
if(max < i){
max++;
*comm = pic_realloc(*comm, (size_t) max + 1, "pic_read_comment#2");
}
(*comm)[i] = '\0';
while((c = fgetc(pi->fp)) != '\0'){ /* skip the dummy area */
if(c == EOF)
pic_file_error(pi, PIC_CORRUPT);
}
if(fgetc(pi->fp) != '\0') /* check the reserved byte */
pic_error(pi, PIC_SUPPORT);
}
static void pic_read_header(pi)
struct pic_info *pi;
{
pi->mode = pic_read_bits(pi, 4);
pi->type = pic_read_bits(pi, 4);
pi->cbits = pic_read_bits(pi, 16);
pi->width = pic_read_bits(pi, 16);
pi->height = pic_read_bits(pi, 16);
/* machine-dependent setup. */
switch(pi->type){
case 0x0: /* X68K */
if(pi->mode != 0)
pic_error(pi, PIC_MODE);
switch(pi->cbits){
case 4:
pi->aspect = 1.0;
pi->g_bits = pi->r_bits = pi->b_bits = 5;
pi->i_bits = 1;
pi->cmapped = 1;
break;
case 8:
pi->aspect = 4.0 / 3.0;
pi->g_bits = pi->r_bits = pi->b_bits = 5;
pi->i_bits = 1;
pi->cmapped = 1;
break;
case 15:
pi->aspect = 4.0 / 3.0;
pi->g_bits = pi->r_bits = pi->b_bits = 5;
pi->cached = 1;
break;
case 16:
pi->aspect = 4.0 / 3.0;
pi->g_bits = pi->r_bits = pi->b_bits = 5;
pi->i_bits = 1;
pi->cached = 1;
break;
default:
pic_error(pi, PIC_NUM_COLORS);
}
break;
case 0x1: /* PC-88VA */
if(pi->height > 1000)
pic_error(pi, PIC_SIZE);
switch(pi->width * 1000 + pi->height){
case 640400:
case 640204:
case 640200:
case 320408:
case 320400:
case 320200:
break;
default:
pic_error(pi, PIC_SIZE);
}
pi->aspect = 400.0 / pi->height;
pi->aspect *= pi->width / 640.0;
if(pi->mode & 0x1) /* HR mode */
pi->aspect *= 2.0;
if(pi->mode & 0x2){ /* tiled 256 format */
if(pi->cbits != 16)
pic_error(pi, PIC_NUM_COLORS);
pi->tiled256 = 1;
}
switch(pi->cbits){
case 8:
pi->g_bits = pi->r_bits = 3;
pi->b_bits = 2;
break;
case 12:
pi->g_bits = pi->r_bits = pi->b_bits = 4;
pi->cached = 1;
break;
case 16:
pi->g_bits = 6;
pi->r_bits = pi->b_bits = 5;
pi->cached = 1;
break;
default:
pic_error(pi, PIC_NUM_COLORS);
}
break;
case 0x2: /* FM-TOWNS */
if(pi->cbits != 15)
pic_error(pi, PIC_NUM_COLORS);
switch(pi->mode){
case 0x5:
case 0xc:
break;
default:
pic_error(pi, PIC_MODE);
}
pi->g_bits = pi->r_bits = pi->b_bits = 5;
pi->cached = 1;
break;
case 0x3: /* MAC */
if(pi->cbits != 15)
pic_error(pi, PIC_NUM_COLORS);
pi->r_bits = pi->g_bits = pi->b_bits = 5;
pi->inv_gr = 1;
break;
case 0xf: /* misc */
{
byte ah, al;
switch(pi->mode){
case 0x0:
break;
case 0x1:
pi->aspect = 4.0 / 3.0;
break;
case 0xf:
break;
default:
pic_error(pi, PIC_MODE);
}
pic_read_bits(pi, 16); /* x */
pic_read_bits(pi, 16); /* y */
ah = pic_read_bits(pi, 8);
al = pic_read_bits(pi, 8);
if(ah > 0 && al > 0)
pi->aspect = (float) al / (int) ah;
else if(pi->mode == 0xf)
pic_error(pi, PIC_ASPECT);
switch(pi->cbits){
case 4:
case 8:
pi->g_bits = pi->r_bits = pi->b_bits = pic_read_bits(pi, 8);
pi->cmapped = 1;
break;
case 12:
pi->g_bits = pi->r_bits = pi->b_bits = 4;
pi->cached = 1;
break;
case 15:
pi->g_bits = pi->r_bits = pi->b_bits = 5;
pi->cached = 1;
break;
case 16:
pi->g_bits = pi->r_bits = pi->b_bits = 5;
pi->i_bits = 1;
pi->cached = 1;
break;
case 24:
pi->g_bits = pi->r_bits = pi->b_bits = 8;
pi->cached = 1;
break;
case 32:
pic_error(pi, PIC_SUPPORT);
break;
default:
pic_error(pi, PIC_NUM_COLORS);
}
}
break;
default:
pic_error(pi, PIC_TYPE);
}
pi->numcols = 1 << pi->cbits;
/* read palette data */
if(pi->cmapped){
int i;
pi->cmap = pic_malloc((size_t) 3 * pi->numcols, "pic_read_header#1");
for(i = 0; i < pi->numcols; i++){
data32 c = pic_read_rgb(pi);
pi->cmap[i * 3 ] = c >> 16 & 0xff;
pi->cmap[i * 3 + 1] = c >> 8 & 0xff;
pi->cmap[i * 3 + 2] = c & 0xff;
}
}
/* setup color code cache */
if(pi->cached)
pic_cache_init(pi);
pi->data = pic_malloc(sizeof(data32) * pi->width * pi->height, // GRR POSSIBLE OVERFLOW / FIXME
"pic_read_header#2");
{
int i;
for(i = 0; i < pi->width * pi->height; i++)
pi->data[i] = 0xffffffff;
}
if(DEBUG)
pic_show_pic_info(pi);
}
/* The main routine to expand a PIC file. */
static void pic_expand_data(pi)
struct pic_info *pi;
{
int cnt;
data32 c;
pi->data[0] = c = 0;
for(cnt = -1; cnt < pi->width * pi->height; ){
int len = pic_expanding_read_len(pi);
cnt += len;
if(cnt < pi->width * pi->height){
int x = cnt % pi->width;
int y = cnt / pi->width;
data32 c = pic_expanding_read_color(pi);
pic_expanding_read_chain(pi, x, y, c);
}
}
}
static int pic_expanding_read_len(pi)
struct pic_info *pi;
{
int len;
byte bits;
for(len = 2, bits = 1; pic_read_bits(pi, 1) == 1; bits++)
len <<= 1;
return len - 1 + pic_read_bits(pi, bits);
}
static data32 pic_expanding_read_color(pi)
struct pic_info *pi;
{
if(pi->cached){
byte b = pic_read_bits(pi, 1);
if(b){
return pic_cache_get_value(pi, (int) pic_read_bits(pi, 7));
}else{
data32 c = pic_read_color_code(pi);
pic_cache_add_value(pi, c);
return c;
}
}
return pic_read_color_code(pi);
}
static void pic_expanding_read_chain(pi, x, y, c)
struct pic_info *pi;
int x, y;
data32 c;
{
pi->data[y * pi->width + x] = c;
if(pic_read_bits(pi, 1) == 1){
int fin = 0;
while(!fin){
switch(pic_read_bits(pi, 2)){
case 1: /* left */
pi->data[(++y) * pi->width + (--x)] = c;
break;
case 2: /* middle */
pi->data[(++y) * pi->width + x ] = c;
break;
case 3: /* right */
pi->data[(++y) * pi->width + (++x)] = c;
break;
case 0: /* far or nothing */
if(pic_read_bits(pi, 1) == 0)
fin = 1;
else{
if(pic_read_bits(pi, 1) == 0)
pi->data[(++y) * pi->width + (x -= 2)] = c;
else
pi->data[(++y) * pi->width + (x += 2)] = c;
}
}
}
}
}
/*
* Make a picture from the expanded data.
*/
static void pic_make_xvpic(pi, xp, rp, gp, bp)
struct pic_info *pi;
byte **xp, *rp, *gp, *bp;
{
if(pi->cmapped){
if(pi->tiled256)
*xp = pic_malloc((size_t) pi->width * pi->height * 2, // GRR POSSIBLE OVERFLOW / FIXME
"pic_make_xvpic#1");
else
*xp = pic_malloc((size_t) pi->width * pi->height, // GRR POSSIBLE OVERFLOW / FIXME
"pic_make_xvpic#2");
}else
*xp = pic_malloc((size_t) pi->width * pi->height * 3, // GRR POSSIBLE OVERFLOW / FIXME
"pic_make_xvpic#3");
if(pi->cmapped){
int i;
for(i = 0; i < pi->numcols; i++){
rp[i] = pi->cmap[i * 3 ];
gp[i] = pi->cmap[i * 3 + 1];
bp[i] = pi->cmap[i * 3 + 2];
}
if(pi->tiled256){
int pic_idx = 0, dat_idx;
data16 col = 0;
for(dat_idx = 0; dat_idx < pi->width * pi->height; dat_idx++){
if(pi->data[dat_idx] != 0xffffffff)
col = pi->data[dat_idx];
(*xp)[pic_idx++] = col & 0xff;
(*xp)[pic_idx++] = col >> 8 & 0xff;
dat_idx++;
}
}else{
int pic_idx = 0, dat_idx;
byte col = 0;
for(dat_idx = 0; dat_idx < pi->width * pi->height; dat_idx++){
if(pi->data[dat_idx] != 0xffffffff)
col = pi->data[dat_idx];
(*xp)[pic_idx++] = col;
}
}
}else{
int pic_idx = 0, dat_idx;
byte r = 0, g = 0, b = 0;
for(dat_idx = 0; dat_idx < pi->width * pi->height; dat_idx++){
if(pi->data[dat_idx] != 0xffffffff){
data32 col = pi->data[dat_idx];
r = col >> 16 & 0xff;
g = col >> 8 & 0xff;
b = col & 0xff;
}
(*xp)[pic_idx++] = r;
(*xp)[pic_idx++] = g;
(*xp)[pic_idx++] = b;
}
}
}
/* The main routine to write PIC file. */
int WritePIC(fp, pic0, ptype, w, h, rmap, gmap, bmap, numcols, colorstyle,
comment)
FILE *fp;
byte *pic0;
int ptype, w, h;
byte *rmap, *gmap, *bmap;
int numcols, colorstyle;
char *comment;
{
struct pic_info pic;
int e;
if(DEBUG) fputs("WritePIC:\n", stderr);
pic_init_info(&pic);
pic.fp = fp;
pic.width = w;
pic.height = h;
pic.writing_grey = (colorstyle == F_GREYSCALE);
if(ptype != PIC24){ /* PIC8 */
pic.cmapped = 1;
pic.cached = 0;
pic.cbits = 8;
pic.g_bits =
pic.r_bits =
pic.b_bits = 8;
pic.i_bits = 0;
pic.numcols = numcols;
}else{ /* PIC24 */
pic.cmapped = 0;
pic.cached = 1;
pic.cbits = 24;
pic.g_bits =
pic.r_bits =
pic.b_bits = 8;
pic.i_bits = 0;
pic.numcols = 1 << 24;
pic_cache_init(&pic);
}
if((e = setjmp(pic.jmp)) != 0){
/* When an error occurs while writing, comes here. */
pic_cleanup_pic_info(&pic, 1);
if(DEBUG) fputs("\n", stderr);
return -1;
}
pic_write_id(&pic);
pic_write_comment(&pic, comment);
pic_write_header(&pic);
if(pic.cmapped)
pic_write_palette(&pic, rmap, gmap, bmap);
pic_make_sparse_data(&pic, pic0);
pic_write_data(&pic);
pic_write_bits(&pic, 0, 8);
pic_cleanup_pic_info(&pic, 1);
if(DEBUG) fputs("\n", stderr);
return 0;
}
static void pic_write_id(pi)
struct pic_info *pi;
{
if(fwrite("PIC", (size_t) 3, (size_t) 1, pi->fp) != 1)
pic_file_error(pi, PIC_WRITE);
}
static void pic_write_comment(pi, comm)
struct pic_info *pi;
char *comm;
{
if(comm){
while(*comm){
int c = *comm;
if(c == '\032')
c = ' ';
if(fputc(*comm, pi->fp) == EOF)
pic_file_error(pi, PIC_WRITE);
comm++;
}
}
/* write ^Z, 0, and reserved. */
if(fwrite("\032\0\0", (size_t)3, (size_t) 1, pi->fp) != 1)
pic_file_error(pi, PIC_WRITE);
}
static void pic_write_header(pi)
struct pic_info *pi;
{
if(DEBUG) pic_show_pic_info(pi);
pic_write_bits(pi, (data32) 0, 4); /* mode: 1:1 */
pic_write_bits(pi, (data32) 0xf, 4); /* type: misc */
pic_write_bits(pi, (data32) pi->cbits, 16); /* bits */
pic_write_bits(pi, (data32) pi->width, 16); /* width */
pic_write_bits(pi, (data32) pi->height, 16); /* height */
pic_write_bits(pi, (data32) 0xffff, 16); /* x: unused */
pic_write_bits(pi, (data32) 0xffff, 16); /* y: unused */
pic_write_bits(pi, (data32) 0x0101, 16); /* real aspect */
}
static void pic_write_palette(pi, r, g, b)
struct pic_info *pi;
byte *r, *g, *b;
{
int i;
data32 rgb = 0;
pic_write_bits(pi, (data32) pi->g_bits, 8);
for(i = 0; i < pi->numcols; i++){
rgb = (data32) *r++ << 16 | (data32) *g++ << 8 | (data32) *b++;
pic_write_rgb(pi, rgb);
}
for( ; i < 256; i++)
pic_write_rgb(pi, rgb);
}
static void pic_make_sparse_data(pi, dat)
struct pic_info *pi;
byte *dat;
{
int i;
data32 c;
pi->data = pic_malloc(sizeof(data32) * pi->width * pi->height, // GRR POSSIBLE OVERFLOW / FIXME
"pic_make_sparse_data");
if(pi->cmapped){
c = 0;
for(i = 0; i < pi->width * pi->height; i++){
if(c != dat[i])
c = pi->data[i] = dat[i];
else
pi->data[i] = 0xffffffff;
}
}else{
int j = 0;
c = 0;
for(i = 0; i < pi->width * pi->height; i++){
data32 r, g, b, t;
r = dat[j++];
g = dat[j++];
b = dat[j++];
t = r << 16 | g << 8 | b;
if(c != t)
c = pi->data[i] = t;
else
pi->data[i] = 0xffffffff;
}
}
}
static void pic_write_data(pi)
struct pic_info *pi;
{
int i;
int max = pi->width * pi->height;
data32 c = 0;
i = -1;
while(i < max){
int j;
for(j = i + 1; j < max; j++){
if(pi->data[j] != 0xffffffff)
break;
}
pic_write_length(pi, (data32) j - i);
i = j;
if(i < max){
pic_write_color(pi, c = pi->data[i]);
pic_write_chain(pi, i % pi->width, i / pi->width, c);
}
}
}
static void pic_write_length(pi, len)
struct pic_info *pi;
data32 len;
{
int bits = 0; /* leading 1's */
int max = 2;
while(len > max){
max = (max + 1) * 2;
bits++;
}
pic_write_bits(pi, 0xffffffff, bits);
pic_write_bits(pi, 0, 1);
pic_write_bits(pi, len - max / 2, bits + 1);
}
static void pic_write_color(pi, c)
struct pic_info *pi;
data32 c;
{
if(pi->cached){
int idx = pic_cache_lookup(pi, c);
if(idx < 0){ /* not found */
pic_write_bits(pi, 0, 1);
pic_write_color_code(pi, c);
pic_cache_add_value(pi, c);
}else{ /* found */
pic_write_bits(pi, (data32) 0xffffffff, 1);
pic_write_bits(pi, (data32) idx, 7);
}
}else
pic_write_color_code(pi, c);
}
static void pic_write_chain(pi, x, y, c)
struct pic_info *pi;
int x, y;
data32 c;
{
int ctr = (y + 1) * pi->width + x;
if(y < pi->height - 1 &&
( pi->data[ctr ] == c ||
(x > 0 && pi->data[ctr - 1] == c) ||
(x < pi->width - 1 && pi->data[ctr + 1] == c) ||
(x > 1 && pi->data[ctr - 2] == c) ||
(x < pi->width - 2 && pi->data[ctr + 2] == c))){
pic_write_bits(pi, 1, 1);
while(++y < pi->height){
if(pi->data[ctr] == c){ /* center */
pic_write_bits(pi, 2, 2);
pi->data[ctr] = 0xffffffff;
ctr += pi->width;
}else if(x > 0 && pi->data[ctr - 1] == c){ /* left */
pic_write_bits(pi, 1, 2);
pi->data[ctr - 1] = 0xffffffff;
ctr += pi->width - 1;
}else if(x < pi->width - 1 && pi->data[ctr + 1] == c){/* right */
pic_write_bits(pi, 3, 2);
pi->data[ctr + 1] = 0xffffffff;
ctr += pi->width + 1;
}else if(x > 1 && pi->data[ctr - 2] == c){ /* 2-left */
pic_write_bits(pi, 2, 4);
pi->data[ctr - 2] = 0xffffffff;
ctr += pi->width - 2;
}else if(x < pi->width - 2 && pi->data[ctr + 2] == c){/* 2-right */
pic_write_bits(pi, 3, 4);
pi->data[ctr + 2] = 0xffffffff;
ctr += pi->width + 2;
}else /* nothing */
break;
}
pic_write_bits(pi, 0, 3);
}else
pic_write_bits(pi, 0, 1);
}
/*
* These 4 functions read or write a color.
*
* pic_read_rgb:
* reads an RGB. Each bit length is [rgb]_bits, but
* it is expanded to 8bits when returned.
*
* pic_read_color_code:
* reads a color code, whose length is cbits.
* It is the index to the colormap or RGB itself.
*
* pic_write_rgb:
* writes an RGB value.
*
* pic_write_color_code:
* writes a color code.
*/
static data32 pic_read_rgb(pi)
struct pic_info *pi;
{
int rb = pi->r_bits, gb = pi->g_bits, bb = pi->b_bits;
byte r, g, b;
if(pi->inv_gr){
r = pic_read_bits(pi, rb);
g = pic_read_bits(pi, gb);
}else{
g = pic_read_bits(pi, gb);
r = pic_read_bits(pi, rb);
}
b = pic_read_bits(pi, bb);
if(pi->i_bits){
byte i;
i = pic_read_bits(pi, pi->i_bits);
r = r << pi->i_bits | i;
g = g << pi->i_bits | i;
b = b << pi->i_bits | i;
rb += pi->i_bits;
gb += pi->i_bits;
bb += pi->i_bits;
}
r = pic_pad_bit(rb, r);
g = pic_pad_bit(gb, g);
b = pic_pad_bit(bb, b);
return (data32) r << 16 | (data32) g << 8 | (data32) b;
}
static data32 pic_read_color_code(pi)
struct pic_info *pi;
{
if(pi->cmapped)
return pic_read_bits(pi, pi->cbits);
return pic_read_rgb(pi);
}
static void pic_write_rgb(pi, rgb)
struct pic_info *pi;
data32 rgb;
{
byte r = rgb >> 16;
byte g = rgb >> 8;
byte b = rgb;
if(pi->writing_grey)
r = g = b = MONO(r, g, b);
pic_write_bits(pi, g, pi->g_bits);
pic_write_bits(pi, r, pi->r_bits);
pic_write_bits(pi, b, pi->b_bits);
}
static void pic_write_color_code(pi, code)
struct pic_info *pi;
data32 code;
{
if(pi->cmapped){
pic_write_bits(pi, code, pi->cbits);
}else{
pic_write_rgb(pi, code);
}
}
/*
* These pic_cache_* functions are an implementation of the color cache.
*
* pic_cache_init:
* initializes the cache.
*
* pic_cache_get_value:
* gets a color indexed by the argument `idx'.
* It updates the `most recently used' time.
*
* pic_cache_add_value:
* adds a color to the top of the cache list.
*/
static void pic_cache_init(pi)
struct pic_info *pi;
{
int i;
pi->cache.node = pic_malloc(sizeof(struct cachenode_t) * 128,
"pic_cache_init");
for(i = 0; i < 128; i++){
pi->cache.node[i].newer = i + 1;
pi->cache.node[i].older = i - 1;
pi->cache.node[i].dat = 0;
}
pi->cache.node[ 0].older = 127;
pi->cache.node[127].newer = 0;
pi->cache.newest = 0;
}
static data32 pic_cache_get_value(pi, idx)
struct pic_info *pi;
int idx;
{
struct cachenode_t *p = pi->cache.node;
int n = pi->cache.newest;
if(n != idx){
p[p[idx].newer].older = p[idx].older;
p[p[idx].older].newer = p[idx].newer;
p[p[n].newer].older = idx;
p[idx].newer = p[n].newer;
p[n].newer = idx;
p[idx].older = n;
pi->cache.newest = idx;
}
return pi->cache.node[idx].dat;
}
static void pic_cache_add_value(pi, dat)
struct pic_info *pi;
data32 dat;
{