1 /* $Header: /home/yav/xpx/RCS/cbb.c,v 1.8 1995/11/23 16:28:39 yav Exp $
2 * xpx save load PC-Engine .cbb .ccb format
3 * written by yav (UHD98984@pcvan.or.jp)
4 */
5
6 #include <X11/Xlib.h>
7
8 #include "headers.h"
9 #include "xpx.h"
10 #include "work.h"
11 #include "extern.h"
12
13 char rcsid_cbb[] = "$Id: cbb.c,v 1.8 1995/11/23 16:28:39 yav Exp $";
14
15 static unsigned char lbittbl[8] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
16
set_ccb_sub()17 int set_ccb_sub()
18 {
19 free_color();
20 get_color(color_buf+current_pal*MAXCOLSET, MAXCOLSET);
21 image_update_col();
22 return 0;
23 }
24
read_ccb_sub(fp,func)25 int read_ccb_sub(fp, func)
26 FILE *fp;
27 void (*func)();
28 {
29 unsigned char buf[2*MAXCOLSET];
30 int i, r;
31 COL *p;
32
33 p = color_buf;
34 r = 0;
35 while (fread(buf, 2*MAXCOLSET, 1, fp) != 0) {
36 for (i = 0; i < MAXCOLSET; i++) {
37 func(p, buf+i*2);
38 p++;
39 }
40 r++;
41 }
42 if (r != MAXPALSET)
43 return 1;
44 return set_ccb_sub();
45 }
46
47 static unsigned char dfcoltbl[8] = {
48 0, 70, 120, 160, 180, 200, 230, 255
49 };
50
conv_ccb(p,buf)51 void conv_ccb(p, buf)
52 COL *p;
53 unsigned char *buf;
54 {
55 p->rgb.blue = *buf & 7;
56 p->rgb.red = (*buf>>3) & 7;
57 p->rgb.green = ((*buf>>6) & 3)|((*(buf+1)&1) << 2);
58 p->rgb.blue = dfcoltbl[p->rgb.blue];
59 p->rgb.red = dfcoltbl[p->rgb.red];
60 p->rgb.green = dfcoltbl[p->rgb.green];
61 }
62
63 /* read CE.EXE ccb */
rd_ccb(fp)64 int rd_ccb(fp)
65 FILE *fp;
66 {
67 return read_ccb_sub(fp, conv_ccb);
68 }
69
70 #define cv5to8(x) (((x)<<3)|((x)>>2))
conv_cxb(p,buf)71 void conv_cxb(p, buf)
72 COL *p;
73 unsigned char *buf;
74 {
75 register int i;
76
77 i = *buf & 31;
78 p->rgb.red = cv5to8(i);
79 i = (*buf>>5) | ((*(buf+1)&3)<<3);
80 p->rgb.green = cv5to8(i);
81 i = (*(buf+1))>>2;
82 p->rgb.blue = cv5to8(i);
83 }
84
rd_cxb(fp)85 int rd_cxb(fp)
86 FILE *fp;
87 {
88 return read_ccb_sub(fp, conv_cxb);
89 }
90
91
write_ccb_sub(FILE * fp,void (* func)())92 int write_ccb_sub(
93 #if NeedFunctionPrototypes
94 FILE *fp, void (*func)())
95 #else
96 fp, func)
97 FILE *fp;
98 void (*func)();
99 #endif
100 {
101 unsigned char buf[2*MAXCOLSET];
102 int i, set;
103 COL *p;
104
105 p = color_buf;
106 for (set = 0; set < MAXPALSET; set++) {
107 for (i = 0; i < MAXCOLSET; i++) {
108 func(p, buf+i*2);
109 p++;
110 }
111 if (!fwrite(buf, 2*MAXCOLSET, 1, fp))
112 break;
113 }
114 if (set != MAXPALSET)
115 return 1;
116 return set_ccb_sub();
117 }
118
119
cv8to3(n)120 int cv8to3(n)
121 int n;
122 {
123 int i;
124
125 for (i = 0; i < 7; i++)
126 if (n < (dfcoltbl[i] + dfcoltbl[i+1])/2)
127 break;
128 return i;
129 }
130
to_ccb(COL * p,unsigned char * buf)131 void to_ccb(
132 #if NeedFunctionPrototypes
133 COL *p, unsigned char *buf)
134 #else
135 p, buf)
136 COL *p;
137 unsigned char *buf;
138 #endif
139 {
140 int r, g, b;
141
142 r = cv8to3(p->rgb.red);
143 g = cv8to3(p->rgb.green);
144 b = cv8to3(p->rgb.blue);
145 *buf = (g << 6) | (r << 3) | b;
146 *(buf+1) = (g >> 2);
147 }
148
wr_ccb(FILE * fp)149 int wr_ccb(
150 #if NeedFunctionPrototypes
151 FILE *fp)
152 #else
153 fp)
154 FILE *fp;
155 #endif
156 {
157 return write_ccb_sub(fp, to_ccb);
158 }
159
to_cxb(COL * p,unsigned char * buf)160 void to_cxb(
161 #if NeedFunctionPrototypes
162 COL *p, unsigned char *buf)
163 #else
164 p, buf)
165 COL *p;
166 unsigned char *buf;
167 #endif
168 {
169 int r, g, b;
170
171 r = p->rgb.red >> 3;
172 g = p->rgb.green >> 3;
173 b = p->rgb.blue >> 3;
174 *buf = (g << 5)|r;
175 *(buf+1) = (b << 2)|(g >> 3);
176 }
177
wr_cxb(FILE * fp)178 int wr_cxb(
179 #if NeedFunctionPrototypes
180 FILE *fp)
181 #else
182 fp)
183 FILE *fp;
184 #endif
185 {
186 return write_ccb_sub(fp, to_cxb);
187 }
188
189
190 #define BITCHKCBB(p0,c0,b) \
191 {pp = p0+7; c = c0; while (c) {if (c & 1) *pp |= b; --pp; c >>= 1;}}
192
conv_cbbfont(p,cbb)193 void conv_cbbfont(p, cbb)
194 unsigned char *p;
195 unsigned char *cbb;
196 {
197 unsigned char c;
198 unsigned char *pp;
199 int y;
200
201 bzero(p, 8*8);
202 y = 8;
203 while (y--) {
204 BITCHKCBB(p,*cbb,1);
205 BITCHKCBB(p,*(cbb+1),2);
206 BITCHKCBB(p,*(cbb+16),4);
207 BITCHKCBB(p,*(cbb+17),8);
208 p += 8;
209 cbb += 2;
210 }
211 }
212 #undef BITCHKCBB
213
214 #define IMGCOPY8(dst,src) (memcpy(dst,src,8), src+=8, dst+=imgmaxw)
set_font8x8(p,x0,y0)215 void set_font8x8(p, x0, y0)
216 unsigned char *p;
217 int x0;
218 int y0;
219 {
220 unsigned char *dst;
221
222 dst = imgdata+y0*imgmaxw+x0;
223 IMGCOPY8(dst,p);
224 IMGCOPY8(dst,p);
225 IMGCOPY8(dst,p);
226 IMGCOPY8(dst,p);
227 IMGCOPY8(dst,p);
228 IMGCOPY8(dst,p);
229 IMGCOPY8(dst,p);
230 IMGCOPY8(dst,p);
231 }
232 #undef IMGCOPY8
233
gen_image_cbb(cbb)234 void gen_image_cbb(cbb)
235 unsigned char *cbb;
236 {
237 int x, y;
238 unsigned char fontpix[8*8];
239
240 for (y = 0; y < 16; y++) {
241 for (x = 0; x < 16; x++) {
242 conv_cbbfont(fontpix, cbb);
243 set_font8x8(fontpix, x<<3, y<<3);
244 cbb += 32;
245 }
246 }
247 }
248
249 /* read CE.EXE cbb */
rd_cbb(fp)250 int rd_cbb(fp)
251 FILE *fp;
252 {
253 int r;
254 unsigned char cbb_data[32*256];
255
256 init_image();
257 r = fread(cbb_data, sizeof(cbb_data), 1, fp);
258 if (!r)
259 return 1;
260 gen_image_cbb(cbb_data);
261 imgupdate(128, 128);
262 return 0;
263 }
264
conv_csbfont(p,csb)265 void conv_csbfont(p, csb)
266 unsigned char *p;
267 unsigned char *csb;
268 {
269 int x, y;
270
271 for (y = 0; y < 8; y++) {
272 for (x = 0; x < 8; x++) {
273 if (*csb & lbittbl[x])
274 *p = 1;
275 else
276 *p = 0;
277 if (*(csb+0x20) & lbittbl[x])
278 *p |= 2;
279 if (*(csb+0x40) & lbittbl[x])
280 *p |= 4;
281 if (*(csb+0x60) & lbittbl[x])
282 *p |= 8;
283 p++;
284 }
285 csb += 2;
286 }
287 }
288
csb_font1(csb,x,y)289 void csb_font1(csb, x, y)
290 unsigned char *csb;
291 int x;
292 int y;
293 {
294 unsigned char fontpix[8*8];
295
296 conv_csbfont(fontpix, csb+1);
297 set_font8x8(fontpix, x, y);
298 conv_csbfont(fontpix, csb);
299 set_font8x8(fontpix, x+8, y);
300 conv_csbfont(fontpix, csb+16+1);
301 set_font8x8(fontpix, x, y+8);
302 conv_csbfont(fontpix, csb+16);
303 set_font8x8(fontpix, x+8, y+8);
304 }
305
gen_image_csb(csb)306 void gen_image_csb(csb)
307 unsigned char *csb;
308 {
309 int x, y;
310
311 for (x = 0; x < 4; x++) {
312 for (y = 0; y < 8; y++) {
313 csb_font1(csb, x*32, y*16);
314 csb += 128;
315 csb_font1(csb, x*32+16, y*16);
316 csb += 128;
317 }
318 }
319 }
320
321 /* read CE.EXE csb */
rd_csb(fp)322 int rd_csb(fp)
323 FILE *fp;
324 {
325 int r;
326 unsigned char csb_data[32*256];
327
328 init_image();
329 r = fread(csb_data, sizeof(csb_data), 1, fp);
330 if (!r)
331 return 1;
332 gen_image_csb(csb_data);
333 imgupdate(128, 128);
334 return 0;
335 }
336
337
gen_cbbfont(unsigned char * dst,unsigned char * src)338 void gen_cbbfont(
339 #if NeedFunctionPrototypes
340 unsigned char *dst, unsigned char *src)
341 #else
342 dst, src)
343 unsigned char *dst;
344 unsigned char *src;
345 #endif
346 {
347 int x, y;
348 static unsigned char bittbl[8] =
349 {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
350
351 bzero(dst, 32);
352 for (y = 0; y < 8; y++) {
353 for (x = 0; x < 8; x++) {
354 if (*src & 1)
355 *dst |= bittbl[x];
356 if (*src & 2)
357 *(dst+1) |= bittbl[x];
358 if (*src & 4)
359 *(dst+16) |= bittbl[x];
360 if (*src & 8)
361 *(dst+17) |= bittbl[x];
362 src++;
363 }
364 src += imgmaxw-8;
365 dst += 2;
366 }
367 }
368
369 /* write CE.EXE cbb */
wr_cbb(FILE * fp)370 int wr_cbb(
371 #if NeedFunctionPrototypes
372 FILE *fp)
373 #else
374 fp)
375 FILE *fp;
376 #endif
377 {
378 int x, y;
379 unsigned char *p;
380 unsigned char cbbfont[32];
381
382 for (y = 0; y < 16; y++) {
383 p = imgdata + y*8*imgmaxw;
384 for (x = 0; x < 16; x++) {
385 gen_cbbfont(cbbfont, p);
386 if (!fwrite(cbbfont, sizeof(cbbfont), 1, fp))
387 return 1;
388 p += 8;
389 }
390 }
391 return 0;
392 }
393
394 /* End of file */
395