Mercurial > hg > dmlib
comparison tools/lib64gfx.c @ 912:70cbbd5b7aea
Moved lib64gfx files under tools, as it's not generic DMLIB module really.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 24 Feb 2015 00:47:39 +0200 |
parents | src/lib64gfx.c@e591ce9b3526 |
children | ba6b210c9bf4 |
comparison
equal
deleted
inserted
replaced
911:602b4aa34293 | 912:70cbbd5b7aea |
---|---|
1 /* | |
2 * Functions for reading and converting various restricted | |
3 * C64/etc and/or indexed/paletted graphics formats. | |
4 * Programmed and designed by Matti 'ccr' Hamalainen | |
5 * (C) Copyright 2012 Tecnic Software productions (TNSP) | |
6 * | |
7 * Please read file 'COPYING' for information on license and distribution. | |
8 */ | |
9 #include "lib64gfx.h" | |
10 | |
11 #define BUF_SIZE_INITIAL (16*1024) | |
12 #define BUF_SIZE_GROW (4*1024) | |
13 | |
14 | |
15 char * dmC64GetImageTypeString(char *buf, const size_t len, const int type) | |
16 { | |
17 snprintf(buf, len, | |
18 "%s%s%s", | |
19 (type & D64_FMT_FLI) ? "FLI " : "", | |
20 (type & D64_FMT_MC) ? "MCol" : "HiRes", | |
21 (type & D64_FMT_ILACE) ? " Ilace" : "" | |
22 ); | |
23 | |
24 return buf; | |
25 } | |
26 | |
27 | |
28 // Based on Pepto's palette, stolen from VICE | |
29 DMColor dmC64Palette[C64_NCOLORS] = | |
30 { | |
31 { 0x00, 0x00, 0x00, 0xff }, | |
32 { 0xFF, 0xFF, 0xFF, 0xff }, | |
33 { 0x68, 0x37, 0x2B, 0xff }, | |
34 { 0x70, 0xA4, 0xB2, 0xff }, | |
35 { 0x6F, 0x3D, 0x86, 0xff }, | |
36 { 0x58, 0x8D, 0x43, 0xff }, | |
37 { 0x35, 0x28, 0x79, 0xff }, | |
38 { 0xB8, 0xC7, 0x6F, 0xff }, | |
39 { 0x6F, 0x4F, 0x25, 0xff }, | |
40 { 0x43, 0x39, 0x00, 0xff }, | |
41 { 0x9A, 0x67, 0x59, 0xff }, | |
42 { 0x44, 0x44, 0x44, 0xff }, | |
43 { 0x6C, 0x6C, 0x6C, 0xff }, | |
44 { 0x9A, 0xD2, 0x84, 0xff }, | |
45 { 0x6C, 0x5E, 0xB5, 0xff }, | |
46 { 0x95, 0x95, 0x95, 0xff }, | |
47 }; | |
48 | |
49 | |
50 const size_t dmC64DefaultSizes[DT_LAST] = | |
51 { | |
52 C64_SCR_COLOR_SIZE, | |
53 C64_SCR_BITMAP_SIZE, | |
54 C64_SCR_SCREEN_SIZE, | |
55 1, | |
56 C64_SCR_EXTRADATA, | |
57 }; | |
58 | |
59 | |
60 #define DM_GET_ADDR_LO(addr) ((addr) & 0xff) | |
61 #define DM_GET_ADDR_HI(addr) (((addr) >> 8) & 0xff) | |
62 | |
63 | |
64 static BOOL dmCompareAddr16(const Uint8 *buf, const size_t offs, const Uint16 addr) | |
65 { | |
66 return buf[offs ] == DM_GET_ADDR_LO(addr) && | |
67 buf[offs + 1] == DM_GET_ADDR_HI(addr); | |
68 } | |
69 | |
70 | |
71 int dmC64ConvertCSDataToImage(DMImage *img, | |
72 int xoffs, int yoffs, const Uint8 *buf, | |
73 int width, int height, BOOL multicolor, int *colors) | |
74 { | |
75 int yc, widthpx = width * 8; | |
76 Uint8 *dp; | |
77 | |
78 if (img == NULL) | |
79 return DMERR_NULLPTR; | |
80 | |
81 if (xoffs < 0 || yoffs < 0 || | |
82 xoffs > img->width - widthpx || | |
83 yoffs > img->height - height) | |
84 return DMERR_INVALID_ARGS; | |
85 | |
86 dp = img->data + (yoffs * img->pitch) + xoffs; | |
87 | |
88 if (multicolor) | |
89 { | |
90 for (yc = 0; yc < height; yc++) | |
91 { | |
92 const int offs = yc * width; | |
93 int xc; | |
94 Uint8 *d = dp; | |
95 | |
96 for (xc = 0; xc < widthpx / 2; xc++) | |
97 { | |
98 const int b = buf[offs + (xc / 4)]; | |
99 const int v = 6 - ((xc * 2) & 6); | |
100 const Uint8 c = colors[(b >> v) & 3]; | |
101 | |
102 *d++ = c; | |
103 *d++ = c; | |
104 } | |
105 | |
106 dp += img->pitch; | |
107 } | |
108 } | |
109 else | |
110 { | |
111 for (yc = 0; yc < height; yc++) | |
112 { | |
113 const int offs = yc * width; | |
114 int xc; | |
115 Uint8 *d = dp; | |
116 | |
117 for (xc = 0; xc < widthpx; xc++) | |
118 { | |
119 const int b = buf[offs + (xc / 8)]; | |
120 const int v = 7 - (xc & 7); | |
121 const Uint8 c = colors[(b >> v) & 1]; | |
122 | |
123 *d++ = c; | |
124 } | |
125 | |
126 dp += img->pitch; | |
127 } | |
128 } | |
129 | |
130 return DMERR_OK; | |
131 } | |
132 | |
133 | |
134 static int fmtProbeDrazPaint20Packed(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) | |
135 { | |
136 const char *ident = (const char *) buf + 2; | |
137 | |
138 if (len > 22 && | |
139 dmCompareAddr16(buf, 0, fmt->addr) && | |
140 strncmp(ident, "DRAZPAINT ", 10) == 0 && | |
141 ident[11] == '.' && ( | |
142 (ident[10] == '1' && ident[12] == '4') || | |
143 (ident[10] == '2' && ident[12] == '0') | |
144 )) | |
145 return DM_PROBE_SCORE_MAX; | |
146 | |
147 return DM_PROBE_SCORE_FALSE; | |
148 } | |
149 | |
150 | |
151 static int dmDecodeGenericRLE(Uint8 **mem, Uint8 **pdstEnd, const Uint8 *src, const Uint8 *srcEnd, const Uint8 rleMarker) | |
152 { | |
153 Uint8 *dst, *dstEnd; | |
154 | |
155 if ((*mem = dmMalloc(C64_RAM_SIZE)) == NULL) | |
156 return DMERR_MALLOC; | |
157 | |
158 dst = *mem; | |
159 dstEnd = *mem + C64_RAM_SIZE; | |
160 | |
161 while (src <= srcEnd && dst <= dstEnd) | |
162 { | |
163 int c = *src++; | |
164 if (c == rleMarker && src + 2 <= srcEnd) | |
165 { | |
166 int cnt = *src++; | |
167 c = *src++; | |
168 while (cnt-- && dst <= dstEnd) | |
169 *dst++ = c; | |
170 } | |
171 else | |
172 *dst++ = c; | |
173 } | |
174 | |
175 *pdstEnd = dst; | |
176 | |
177 return DMERR_OK; | |
178 } | |
179 | |
180 | |
181 static int fmtDecodeDrazPaintPacked(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) | |
182 { | |
183 int res; | |
184 Uint8 *mem = NULL, *dstEnd; | |
185 | |
186 if ((res = dmDecodeGenericRLE(&mem, &dstEnd, buf + 0x0e, buf + len, *(buf + 0x0d))) != DMERR_OK) | |
187 goto out; | |
188 | |
189 res = dmC64DecodeGenericBMP(img, mem, dstEnd - mem + 1, fmt); | |
190 | |
191 out: | |
192 dmFree(mem); | |
193 return res; | |
194 } | |
195 | |
196 | |
197 static int fmtProbeDrazLace10Packed(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) | |
198 { | |
199 const char *ident = (const char *) buf + 2; | |
200 if (len > 22 && | |
201 dmCompareAddr16(buf, 0, fmt->addr) && | |
202 strncmp(ident, "DRAZLACE! 1.0", 13) == 0) | |
203 return DM_PROBE_SCORE_MAX; | |
204 | |
205 return DM_PROBE_SCORE_FALSE; | |
206 } | |
207 | |
208 | |
209 static BOOL fmtDrazLaceSetLaceType(DMC64Image *img, const struct _DMC64EncDecOp *op, const Uint8 *buf, const size_t len) | |
210 { | |
211 (void) len; | |
212 | |
213 img->laceType = buf[op->offs] ? D64_ILACE_RES : D64_ILACE_COLOR; | |
214 img->laceBank1 = img->laceBank2 = 0; | |
215 return TRUE; | |
216 } | |
217 | |
218 | |
219 #define AMICA_DM_PROBE_SIZE 2048 | |
220 static int fmtProbeAmicaPaintPacked(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) | |
221 { | |
222 size_t i, n; | |
223 if (len < AMICA_DM_PROBE_SIZE || !dmCompareAddr16(buf, 0, fmt->addr)) | |
224 return DM_PROBE_SCORE_FALSE; | |
225 | |
226 // Interpaint Hi-Res gives a false positive | |
227 if (len == 9002) | |
228 return DM_PROBE_SCORE_FALSE; | |
229 | |
230 for (n = 0, i = 2; i < len; i++) | |
231 if (buf[i] == 0xC2) n++; | |
232 | |
233 if (n > 50) | |
234 return DM_PROBE_SCORE_GOOD; | |
235 if (n > 25) | |
236 return DM_PROBE_SCORE_AVG; | |
237 if (n > 10) | |
238 return DM_PROBE_SCORE_MAYBE; | |
239 return DM_PROBE_SCORE_FALSE; | |
240 } | |
241 | |
242 | |
243 static int fmtDecodeAmicaPaintPacked(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) | |
244 { | |
245 int res; | |
246 Uint8 *mem = NULL, *dstEnd; | |
247 | |
248 if ((res = dmDecodeGenericRLE(&mem, &dstEnd, buf, buf + len, 0xC2)) != DMERR_OK) | |
249 goto out; | |
250 | |
251 res = dmC64DecodeGenericBMP(img, mem, dstEnd - mem + 1, fmt); | |
252 | |
253 out: | |
254 dmFree(mem); | |
255 return res; | |
256 } | |
257 | |
258 | |
259 static BOOL fmtTruePaintSetLaceType(DMC64Image *img, const struct _DMC64EncDecOp *op, const Uint8 *buf, const size_t len) | |
260 { | |
261 (void) op; | |
262 (void) buf; | |
263 (void) len; | |
264 img->laceType = D64_ILACE_RES; | |
265 img->laceBank1 = 0; | |
266 img->laceBank2 = 1; | |
267 return TRUE; | |
268 } | |
269 | |
270 | |
271 static BOOL fmtSetFLIType(DMC64Image *img, const struct _DMC64EncDecOp *op, const Uint8 *buf, const size_t len) | |
272 { | |
273 (void) buf; | |
274 (void) len; | |
275 img->fliType = op->bank; | |
276 return TRUE; | |
277 } | |
278 | |
279 | |
280 const DMC64ImageFormat dmC64ImageFormats[] = | |
281 { | |
282 { | |
283 D64_FMT_MC, "d2p", "DrazPaint 2.0 (packed)", 0x5800, -1, | |
284 fmtProbeDrazPaint20Packed, fmtDecodeDrazPaintPacked, | |
285 NULL, NULL, NULL, | |
286 4, | |
287 { | |
288 { DT_COLOR_RAM, 0x0000, 0, 0, NULL, NULL }, | |
289 { DT_BITMAP, 0x0800, 0, 0, NULL, NULL }, | |
290 { DT_SCREEN_RAM, 0x0400, 0, 0, NULL, NULL }, | |
291 { DT_BGCOLOR, 0x2740, 0, 0, NULL, NULL }, | |
292 } | |
293 }, | |
294 | |
295 { | |
296 D64_FMT_MC | D64_FMT_ILACE, "dlp", "DrazLace 1.0 (packed)", 0x5800, -1, | |
297 fmtProbeDrazLace10Packed, fmtDecodeDrazPaintPacked, | |
298 NULL, NULL, NULL, | |
299 6, | |
300 { | |
301 { DT_COLOR_RAM, 0x0000, 0, 0, NULL, NULL }, | |
302 { DT_BITMAP, 0x0800, 0, 0, NULL, NULL }, | |
303 { DT_SCREEN_RAM, 0x0400, 0, 0, NULL, NULL }, | |
304 { DT_BGCOLOR, 0x2740, 0, 0, NULL, NULL }, | |
305 { DT_BITMAP, 0x2800, 1, 0, NULL, NULL }, | |
306 { DT_DEC_FUNCTION, 0x2742, 0, 1, fmtDrazLaceSetLaceType, NULL }, | |
307 } | |
308 }, | |
309 | |
310 { | |
311 D64_FMT_MC, "drp", "DrazPaint (unpacked)", 0x5800, 10051, | |
312 NULL, NULL, | |
313 NULL, NULL, NULL, | |
314 4, | |
315 { | |
316 { DT_COLOR_RAM, 0x0000, 0, 0, NULL, NULL }, | |
317 { DT_BITMAP, 0x0800, 0, 0, NULL, NULL }, | |
318 { DT_SCREEN_RAM, 0x0400, 0, 0, NULL, NULL }, | |
319 { DT_BGCOLOR, 0x2740, 0, 0, NULL, NULL }, | |
320 } | |
321 }, | |
322 | |
323 { | |
324 D64_FMT_MC | D64_FMT_ILACE, "drl", "DrazLace 1.0 (unpacked)", 0x5800, 18242, | |
325 NULL, NULL, | |
326 NULL, NULL, NULL, | |
327 6, | |
328 { | |
329 { DT_COLOR_RAM, 0x0000, 0, 0, NULL, NULL }, | |
330 { DT_BITMAP, 0x0800, 0, 0, NULL, NULL }, | |
331 { DT_SCREEN_RAM, 0x0400, 0, 0, NULL, NULL }, | |
332 { DT_BGCOLOR, 0x2740, 0, 0, NULL, NULL }, | |
333 { DT_BITMAP, 0x2800, 1, 0, NULL, NULL }, | |
334 { DT_DEC_FUNCTION, 0x2742, 0, 1, fmtDrazLaceSetLaceType, NULL }, | |
335 } | |
336 }, | |
337 | |
338 { | |
339 D64_FMT_MC | D64_FMT_ILACE, "mci", "Truepaint (unpacked)", 0x9c00, 19434, | |
340 NULL, NULL, | |
341 NULL, NULL, NULL, | |
342 6, | |
343 { | |
344 { DT_SCREEN_RAM, 0x0000, 0, 0, NULL, NULL }, | |
345 { DT_BGCOLOR, 0x03e8, 0, 0, NULL, NULL }, | |
346 { DT_BITMAP, 0x0400, 0, 0, NULL, NULL }, | |
347 { DT_BITMAP, 0x2400, 1, 0, NULL, NULL }, | |
348 { DT_SCREEN_RAM, 0x4400, 1, 0, NULL, NULL }, | |
349 { DT_COLOR_RAM, 0x4800, 0, 0, NULL, NULL }, | |
350 { DT_DEC_FUNCTION, 0x0000, 0, 0, fmtTruePaintSetLaceType, NULL }, | |
351 } | |
352 }, | |
353 | |
354 { | |
355 D64_FMT_MC, "kla", "Koala Paint (unpacked)", 0x6000, 10003, | |
356 NULL, NULL, | |
357 NULL, NULL, NULL, | |
358 4, | |
359 { | |
360 { DT_BITMAP, 0x0000, 0, 0, NULL, NULL }, | |
361 { DT_SCREEN_RAM, 0x1f40, 0, 0, NULL, NULL }, | |
362 { DT_COLOR_RAM, 0x2328, 0, 0, NULL, NULL }, | |
363 { DT_BGCOLOR, 0x2710, 0, 0, NULL, NULL }, | |
364 } | |
365 }, | |
366 | |
367 { | |
368 D64_FMT_MC, "ocp", "Advanced Art Studio (unpacked)", 0x2000, 10018, | |
369 NULL, NULL, | |
370 NULL, NULL, NULL, | |
371 4, | |
372 { | |
373 { DT_BITMAP, 0x0000, 0, 0, NULL, NULL }, | |
374 { DT_SCREEN_RAM, 0x1f40, 0, 0, NULL, NULL }, | |
375 { DT_COLOR_RAM, 0x2338, 0, 0, NULL, NULL }, | |
376 { DT_BGCOLOR, 0x2329, 0, 0, NULL, NULL }, | |
377 } | |
378 }, | |
379 | |
380 { | |
381 D64_FMT_MC, "ami", "Amica Paint (packed)", 0x4000, -1, | |
382 fmtProbeAmicaPaintPacked, fmtDecodeAmicaPaintPacked, | |
383 NULL, NULL, NULL, | |
384 4, | |
385 { | |
386 { DT_COLOR_RAM, 0x2328, 0, 0, NULL, NULL }, | |
387 { DT_BITMAP, 0x0000, 0, 0, NULL, NULL }, | |
388 { DT_SCREEN_RAM, 0x1f40, 0, 0, NULL, NULL }, | |
389 { DT_BGCOLOR, 0x2710, 0, 0, NULL, NULL }, | |
390 } | |
391 }, | |
392 | |
393 { | |
394 D64_FMT_MC, "rpm", "Run Paint (unpacked)", 0x6000, 10006, | |
395 NULL, NULL, | |
396 NULL, NULL, NULL, | |
397 4, | |
398 { | |
399 { DT_COLOR_RAM, 0x2328, 0, 0, NULL, NULL }, | |
400 { DT_BITMAP, 0x0000, 0, 0, NULL, NULL }, | |
401 { DT_SCREEN_RAM, 0x1f40, 0, 0, NULL, NULL }, | |
402 { DT_BGCOLOR, 0x2710, 0, 0, NULL, NULL }, | |
403 } | |
404 }, | |
405 | |
406 { | |
407 D64_FMT_HIRES, "art", "Art Studio (unpacked)", 0x2000, 9009, | |
408 NULL, NULL, | |
409 NULL, NULL, NULL, | |
410 2, | |
411 { | |
412 { DT_BITMAP, 0x0000, 0, 0, NULL, NULL }, | |
413 { DT_SCREEN_RAM, 0x1f40, 0, 0, NULL, NULL }, | |
414 } | |
415 }, | |
416 | |
417 { | |
418 D64_FMT_HIRES, "iph", "Interpaint (unpacked)", 0x4000, 9002, | |
419 NULL, NULL, | |
420 NULL, NULL, NULL, | |
421 2, | |
422 { | |
423 { DT_BITMAP, 0x0000, 0, 0, NULL, NULL }, | |
424 { DT_SCREEN_RAM, 0x1f40, 0, 0, NULL, NULL }, | |
425 } | |
426 }, | |
427 | |
428 { | |
429 D64_FMT_HIRES, "dd", "Doodle (unpacked)", 0x1c00, 9218, | |
430 NULL, NULL, | |
431 NULL, NULL, NULL, | |
432 2, | |
433 { | |
434 { DT_SCREEN_RAM, 0x0000, 0, 0, NULL, NULL }, | |
435 { DT_BITMAP, 0x0400, 0, 0, NULL, NULL }, | |
436 } | |
437 }, | |
438 | |
439 { | |
440 D64_FMT_MC | D64_FMT_FLI, "bml", "Blackmail FLI (unpacked)", 0x3b00, 17474, | |
441 NULL, NULL, | |
442 NULL, NULL, NULL, | |
443 11, | |
444 { | |
445 { DT_COLOR_RAM, 0x0100, 0, 0, NULL, NULL }, | |
446 | |
447 { DT_SCREEN_RAM, 0x0500, 0, 0, NULL, NULL }, | |
448 { DT_SCREEN_RAM, 0x0900, 1, 0, NULL, NULL }, | |
449 { DT_SCREEN_RAM, 0x0d00, 2, 0, NULL, NULL }, | |
450 { DT_SCREEN_RAM, 0x1100, 3, 0, NULL, NULL }, | |
451 | |
452 { DT_SCREEN_RAM, 0x1500, 4, 0, NULL, NULL }, | |
453 { DT_SCREEN_RAM, 0x1900, 5, 0, NULL, NULL }, | |
454 { DT_SCREEN_RAM, 0x1d00, 6, 0, NULL, NULL }, | |
455 { DT_SCREEN_RAM, 0x2100, 7, 0, NULL, NULL }, | |
456 | |
457 { DT_BITMAP, 0x2500, 0, 0, NULL, NULL }, | |
458 { DT_DEC_FUNCTION, 0x0000, D64_FLI_8BANK, 0, fmtSetFLIType, NULL }, | |
459 } | |
460 }, | |
461 | |
462 { | |
463 D64_FMT_MC | D64_FMT_FLI, "fli", "FLI Designer (unpacked)", 0x3c00, 17409, | |
464 NULL, NULL, | |
465 NULL, NULL, NULL, | |
466 11, | |
467 { | |
468 { DT_COLOR_RAM, 0x0000, 0, 0, NULL, NULL }, | |
469 { DT_SCREEN_RAM, 0x0400, 0, 0, NULL, NULL }, | |
470 { DT_SCREEN_RAM, 0x0800, 1, 0, NULL, NULL }, | |
471 { DT_SCREEN_RAM, 0x0c00, 2, 0, NULL, NULL }, | |
472 { DT_SCREEN_RAM, 0x1000, 3, 0, NULL, NULL }, | |
473 { DT_SCREEN_RAM, 0x1400, 4, 0, NULL, NULL }, | |
474 { DT_SCREEN_RAM, 0x1800, 5, 0, NULL, NULL }, | |
475 { DT_SCREEN_RAM, 0x1c00, 6, 0, NULL, NULL }, | |
476 { DT_SCREEN_RAM, 0x2000, 7, 0, NULL, NULL }, | |
477 { DT_BITMAP, 0x2400, 0, 0, NULL, NULL }, | |
478 { DT_DEC_FUNCTION, 0x0000, D64_FLI_8BANK, 0, fmtSetFLIType, NULL }, | |
479 } | |
480 }, | |
481 | |
482 { | |
483 D64_FMT_MC, "xx1", "Unknown $2000 format (unpacked)", 0x2000, 10242, | |
484 NULL, NULL, | |
485 NULL, NULL, NULL, | |
486 4, | |
487 { | |
488 { DT_BITMAP, 0x0000, 0, 0, NULL, NULL }, | |
489 { DT_SCREEN_RAM, 0x2000, 0, 0, NULL, NULL }, | |
490 { DT_COLOR_RAM, 0x2400, 0, 0, NULL, NULL }, | |
491 { DT_BGCOLOR_SET, 0x00 , 0, 0, NULL, NULL }, | |
492 } | |
493 }, | |
494 | |
495 }; | |
496 | |
497 const int ndmC64ImageFormats = sizeof(dmC64ImageFormats) / sizeof(dmC64ImageFormats[0]); | |
498 | |
499 | |
500 // Perform probing of the given data buffer, trying to determine | |
501 // if it contains a supported "C64" image format. Returns the | |
502 // "probe score", see libgfx.h for list of values. If a match | |
503 // is found, pointer to format description is set to *pfmt. | |
504 int dmC64ProbeBMP(const Uint8 *buf, const size_t len, const DMC64ImageFormat **pfmt) | |
505 { | |
506 int i, scoreMax = DM_PROBE_SCORE_FALSE, scoreIndex = -1; | |
507 | |
508 for (i = 0; i < ndmC64ImageFormats; i++) | |
509 { | |
510 const DMC64ImageFormat *fmt = &dmC64ImageFormats[i]; | |
511 int score = DM_PROBE_SCORE_FALSE; | |
512 if (fmt->probe == NULL && fmt->size > 0 && fmt->addr > 0) | |
513 { | |
514 // Generic probe just checks matching size and load address | |
515 if (len == fmt->size && dmCompareAddr16(buf, 0, fmt->addr)) | |
516 score = DM_PROBE_SCORE_GOOD; | |
517 } | |
518 else | |
519 score = fmt->probe(buf, len, fmt); | |
520 | |
521 if (score > scoreMax) | |
522 { | |
523 scoreMax = score; | |
524 scoreIndex = i; | |
525 } | |
526 } | |
527 | |
528 if (scoreIndex >= 0) | |
529 { | |
530 *pfmt = &dmC64ImageFormats[scoreIndex]; | |
531 return scoreMax; | |
532 } | |
533 else | |
534 return DM_PROBE_SCORE_FALSE; | |
535 } | |
536 | |
537 | |
538 static int dmC64SanityCheckEncDecOp(const int i, const DMC64EncDecOp *op) | |
539 { | |
540 if (op->bank < 0 || op->bank >= C64_SCR_MAX_BANK) | |
541 { | |
542 dmError("Invalid bank %d definition in generic encode/decode operator %d @ #%d.\n", | |
543 op->bank, op->type, i); | |
544 return DMERR_INTERNAL; | |
545 } | |
546 | |
547 if (op->type < 0 || op->type >= DT_LAST) | |
548 { | |
549 dmError("Invalid encode/decode operator type %d @ #%d.\n", | |
550 op->type, i); | |
551 return DMERR_INTERNAL; | |
552 } | |
553 | |
554 return DMERR_OK; | |
555 } | |
556 | |
557 | |
558 int dmC64DecodeGenericBMP(DMC64Image *img, const Uint8 *buf, | |
559 const size_t len, const DMC64ImageFormat *fmt) | |
560 { | |
561 int i; | |
562 | |
563 if (buf == NULL || img == NULL || fmt == NULL) | |
564 return DMERR_NULLPTR; | |
565 | |
566 // Clear the image structure | |
567 memset(img, 0, sizeof(*img)); | |
568 img->type = fmt->type; | |
569 | |
570 // Perform decoding | |
571 for (i = 0; i < fmt->nencdecOps; i++) | |
572 { | |
573 const DMC64EncDecOp *op = &fmt->encdecOps[i]; | |
574 const Uint8 *src; | |
575 size_t size; | |
576 int res; | |
577 | |
578 // Check operation validity | |
579 if ((res = dmC64SanityCheckEncDecOp(i, op)) != DMERR_OK) | |
580 return res; | |
581 | |
582 // Check size | |
583 size = (op->size == 0) ? dmC64DefaultSizes[op->type] : op->size; | |
584 | |
585 // Do we need to reallocate some more space? | |
586 if (op->offs + size > len) | |
587 { | |
588 dmError("Decode out of bounds, op #%d type=%d, offs=%d ($%04x), " | |
589 "bank=%d, size=%d ($%04x) @ %d ($%04x)\n", | |
590 i, op->type, op->offs, op->offs, op->bank, size, size, len, len); | |
591 return DMERR_INVALID_DATA; | |
592 } | |
593 | |
594 src = buf + op->offs; | |
595 | |
596 // Perform operation | |
597 switch (op->type) | |
598 { | |
599 case DT_COLOR_RAM: memcpy(img->color[op->bank], src, size); break; | |
600 case DT_BITMAP: memcpy(img->bitmap[op->bank], src, size); break; | |
601 case DT_SCREEN_RAM: memcpy(img->screen[op->bank], src, size); break; | |
602 case DT_BGCOLOR: img->bgcolor = *src; break; | |
603 case DT_BGCOLOR_SET: img->bgcolor = op->offs; break; | |
604 case DT_EXTRADATA: memcpy(img->extradata, src, size); break; | |
605 case DT_DEC_FUNCTION: | |
606 if (op->decfunction == NULL) | |
607 { | |
608 dmError("Decode op is a function, but function ptr is NULL: " | |
609 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", | |
610 i, op->offs, op->offs, op->bank, size, size, len, len); | |
611 return DMERR_INTERNAL; | |
612 } | |
613 if (!op->decfunction(img, op, buf, len)) | |
614 { | |
615 dmError("Decode op custom function failed: op #%d, " | |
616 "offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", | |
617 i, op->offs, op->offs, op->bank, size, size, len, len); | |
618 return DMERR_INTERNAL; | |
619 } | |
620 break; | |
621 } | |
622 } | |
623 | |
624 return DMERR_OK; | |
625 } | |
626 | |
627 | |
628 int dmC64EncodeGenericBMP(Uint8 **pbuf, size_t *plen, const DMC64Image *img, const DMC64ImageFormat *fmt) | |
629 { | |
630 int i, res = DMERR_OK; | |
631 Uint8 *buf; | |
632 size_t allocated; | |
633 | |
634 if (pbuf == NULL || plen == NULL || img == NULL || fmt == NULL) | |
635 return DMERR_NULLPTR; | |
636 | |
637 // Allocate the output buffer | |
638 *plen = 0; | |
639 if (fmt->size > 0) | |
640 *plen = allocated = fmt->size; | |
641 else | |
642 allocated = 8 * 1024; | |
643 | |
644 if ((buf = dmMalloc(allocated)) == NULL) | |
645 { | |
646 dmError("Could not allocate %d bytes of memory for C64 image encoding buffer.\n", | |
647 allocated); | |
648 res = DMERR_MALLOC; | |
649 goto error; | |
650 } | |
651 | |
652 // Perform encoding | |
653 for (i = 0; i < fmt->nencdecOps; i++) | |
654 { | |
655 const DMC64EncDecOp *op = &fmt->encdecOps[i]; | |
656 Uint8 *dst = 2 + buf + op->offs; | |
657 size_t size; | |
658 | |
659 // Check operation validity | |
660 if ((res = dmC64SanityCheckEncDecOp(i, op)) != DMERR_OK) | |
661 goto error; | |
662 | |
663 // Check size | |
664 size = (op->size == 0) ? dmC64DefaultSizes[op->type] : op->size; | |
665 | |
666 // Do we need to reallocate some more space? | |
667 if (2 + op->offs + size > allocated) | |
668 { | |
669 size_t diff = allocated - (op->offs + size + 2), | |
670 grow = (diff / (BUF_SIZE_GROW - 1)) * BUF_SIZE_GROW; | |
671 allocated += grow; | |
672 | |
673 if ((buf = dmRealloc(buf, allocated)) == NULL) | |
674 { | |
675 dmError("Could not re-allocate %d bytes of memory for C64 image encoding buffer.\n", | |
676 allocated); | |
677 res = DMERR_MALLOC; | |
678 goto error; | |
679 } | |
680 } | |
681 | |
682 if (fmt->size == 0 && op->offs + size + 2 > *plen) | |
683 *plen = op->offs + size + 2; | |
684 | |
685 // Perform operation | |
686 switch (op->type) | |
687 { | |
688 case DT_COLOR_RAM: memcpy(dst, img->color[op->bank], size); break; | |
689 case DT_BITMAP: memcpy(dst, img->bitmap[op->bank], size); break; | |
690 case DT_SCREEN_RAM: memcpy(dst, img->screen[op->bank], size); break; | |
691 case DT_BGCOLOR: *dst = img->bgcolor; break; | |
692 case DT_EXTRADATA: memcpy(dst, img->extradata, size); break; | |
693 case DT_ENC_FUNCTION: | |
694 if (op->encfunction == NULL) | |
695 { | |
696 dmError("Encode op is a function, but function ptr is NULL: " | |
697 "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", | |
698 i, op->offs, op->offs, op->bank, size, size, *plen, *plen); | |
699 return DMERR_INTERNAL; | |
700 } | |
701 /* | |
702 if (!op->encfunction(op, buf, len)) | |
703 { | |
704 dmError("Encode op custom function failed: op #%d, " | |
705 "offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", | |
706 i, op->offs, op->offs, op->bank, size, size, len, len); | |
707 return DMERR_INTERNAL; | |
708 } | |
709 */ | |
710 break; | |
711 } | |
712 } | |
713 | |
714 buf[0] = DM_GET_ADDR_LO(fmt->addr); | |
715 buf[1] = DM_GET_ADDR_HI(fmt->addr); | |
716 | |
717 *pbuf = buf; | |
718 return DMERR_OK; | |
719 | |
720 error: | |
721 dmFree(buf); | |
722 *pbuf = NULL; | |
723 *plen = 0; | |
724 return res; | |
725 } | |
726 | |
727 | |
728 static inline Uint8 dmC64GetMCColor(const DMC64Image *img, const int bits, const int cbank, const int vbank, const int scroffs) | |
729 { | |
730 switch (bits) | |
731 { | |
732 case 0: return img->bgcolor; break; | |
733 case 1: return img->screen[vbank][scroffs] >> 4; break; | |
734 case 2: return img->screen[vbank][scroffs] & 15; break; | |
735 default: return img->color[cbank][scroffs] & 15; break; | |
736 } | |
737 } | |
738 | |
739 | |
740 // Convert a generic "C64" format bitmap in DMC64Image struct to | |
741 // a indexed/paletted bitmap image. | |
742 int dmC64ConvertGenericBMP2Image(DMImage *dst, const DMC64Image *src, const BOOL doubleMC) | |
743 { | |
744 Uint8 *dp = dst->data; | |
745 int yc; | |
746 | |
747 // Sanity check arguments | |
748 if (dst == NULL || src == NULL) | |
749 return DMERR_NULLPTR; | |
750 | |
751 if (dst->width < 8) | |
752 return DMERR_INVALID_ARGS; | |
753 | |
754 // Perform generic conversion | |
755 for (yc = 0; yc < dst->height; yc++) | |
756 { | |
757 Uint8 *d = dp; | |
758 const int y = yc / 8, yb = yc & 7; | |
759 const int scroffsy = y * C64_SCR_CH_WIDTH; | |
760 const int bmoffsy = y * C64_SCR_WIDTH; | |
761 int xc; | |
762 | |
763 if ((src->type & D64_FMT_MC) == D64_FMT_HIRES) | |
764 { | |
765 // Hi-res bitmap | |
766 for (xc = 0; xc < dst->width; xc++) | |
767 { | |
768 const int x = xc / 8; | |
769 const int scroffs = scroffsy + x; | |
770 const int bmoffs = bmoffsy + (x * 8) + yb; | |
771 const int v = 7 - (xc & 7); | |
772 | |
773 if ((src->bitmap[0][bmoffs] >> v) & 1) | |
774 *d++ = src->screen[0][scroffs] >> 4; | |
775 else | |
776 *d++ = src->screen[0][scroffs] & 15; | |
777 } | |
778 } | |
779 else | |
780 { | |
781 // Multicolor variants | |
782 const int wdivisor = doubleMC ? 2 : 1; | |
783 | |
784 for (xc = 0; xc < dst->width / wdivisor; xc++) | |
785 { | |
786 const int x = xc / 4; | |
787 const int scroffs = scroffsy + x; | |
788 const int bmoffs = bmoffsy + (x * 8) + yb; | |
789 const int v = 6 - ((xc * 2) & 6); | |
790 Uint8 c; | |
791 | |
792 if (src->type & D64_FMT_FLI) | |
793 { | |
794 int vbank = 0; | |
795 switch (src->fliType) | |
796 { | |
797 case D64_FLI_2BANK: | |
798 vbank = yb / 4; | |
799 break; | |
800 case D64_FLI_4BANK: | |
801 vbank = yb / 2; | |
802 break; | |
803 case D64_FLI_8BANK: | |
804 vbank = yb; | |
805 break; | |
806 } | |
807 c = dmC64GetMCColor(src, (src->bitmap[0][bmoffs] >> v) & 3, 0, vbank, scroffs); | |
808 *d++ = c; | |
809 if (doubleMC) | |
810 *d++ = c; | |
811 } | |
812 else | |
813 if (src->type & D64_FMT_ILACE) | |
814 { | |
815 *d++ = dmC64GetMCColor(src, (src->bitmap[0][bmoffs] >> v) & 3, 0, src->laceBank1, scroffs); | |
816 if (doubleMC) | |
817 *d++ = dmC64GetMCColor(src, (src->bitmap[1][bmoffs] >> v) & 3, 0, src->laceBank2, scroffs); | |
818 } | |
819 else | |
820 { | |
821 c = dmC64GetMCColor(src, (src->bitmap[0][bmoffs] >> v) & 3, 0, 0, scroffs); | |
822 *d++ = c; | |
823 if (doubleMC) | |
824 *d++ = c; | |
825 } | |
826 } | |
827 } | |
828 dp += dst->pitch; | |
829 } | |
830 | |
831 return DMERR_OK; | |
832 } | |
833 | |
834 | |
835 int dmC64ConvertBMP2Image(DMImage **pdst, const DMC64Image *src, const DMC64ImageFormat *fmt, const BOOL doubleMC) | |
836 { | |
837 int width, res; | |
838 DMImage *dst; | |
839 | |
840 if (pdst == NULL || src == NULL) | |
841 return DMERR_NULLPTR; | |
842 | |
843 // Calculate output image width | |
844 if ((src->type & D64_FMT_MC) && !doubleMC) | |
845 width = C64_SCR_WIDTH / 2; | |
846 else | |
847 width = C64_SCR_WIDTH; | |
848 | |
849 // Allocate image structure | |
850 if ((*pdst = dst = dmImageAlloc(width, C64_SCR_HEIGHT)) == NULL) | |
851 return DMERR_MALLOC; | |
852 | |
853 // Set palette | |
854 dst->pal = (DMColor *) &dmC64Palette; | |
855 dst->ncolors = C64_NCOLORS; | |
856 dst->constpal = TRUE; | |
857 | |
858 // Convert | |
859 if (fmt->convertFrom != NULL) | |
860 res = fmt->convertFrom(dst, src, doubleMC); | |
861 else | |
862 res = dmC64ConvertGenericBMP2Image(dst, src, doubleMC); | |
863 | |
864 return res; | |
865 } | |
866 | |
867 | |
868 int dmC64DecodeBMP(DMC64Image *img, const Uint8 *buf, const size_t len, | |
869 const size_t probeOffs, const size_t loadOffs, | |
870 const DMC64ImageFormat **fmt, const DMC64ImageFormat *forced) | |
871 { | |
872 // Check for forced format | |
873 if (forced != NULL) | |
874 *fmt = forced; | |
875 else | |
876 { | |
877 // Nope, perform a generic probe | |
878 if (probeOffs >= len) | |
879 return -200; | |
880 | |
881 if (dmC64ProbeBMP(buf + probeOffs, len - probeOffs, fmt) == DM_PROBE_SCORE_FALSE) | |
882 return -201; | |
883 } | |
884 | |
885 if (loadOffs >= len) | |
886 return -203; | |
887 | |
888 // Decode the bitmap to memory layout | |
889 if ((*fmt)->decode != NULL) | |
890 return (*fmt)->decode(img, buf + loadOffs, len - loadOffs, *fmt); | |
891 else | |
892 return dmC64DecodeGenericBMP(img, buf + loadOffs, len - loadOffs, *fmt); | |
893 } |