comparison tools/lib64gfx.c @ 2265:48b48251610a

Refactor how the image "mode/type" is handled. It is still not perfect for our purposes, but better now.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 17 Jun 2019 02:03:35 +0300
parents 5db6e0b63b35
children cd266022e4a8
comparison
equal deleted inserted replaced
2264:e25fa516b53a 2265:48b48251610a
239 blk->size = 0; 239 blk->size = 0;
240 } 240 }
241 } 241 }
242 242
243 243
244 static void dmC64SetupImageData(DMC64Image *img, const DMC64ImageFormat *fmt)
245 {
246 img->fmt = fmt->format;
247 img->nblocks = dmC64ImageGetNumBlocks(fmt);
248
249 memset(img->extraInfo, 0, sizeof(img->extraInfo));
250 img->extraInfo[D64_EI_MODE] = fmt->format->mode;
251 }
252
253
244 DMC64Image *dmC64ImageAlloc(const DMC64ImageFormat *fmt) 254 DMC64Image *dmC64ImageAlloc(const DMC64ImageFormat *fmt)
245 { 255 {
246 DMC64Image *img = dmMalloc0(sizeof(DMC64Image)); 256 DMC64Image *img = dmMalloc0(sizeof(DMC64Image));
247 257
248 if (img == NULL) 258 if (img == NULL)
249 return NULL; 259 return NULL;
250 260
251 // Initialize image information 261 // Initialize image information
252 img->fmt = fmt->format; 262 dmC64SetupImageData(img, fmt);
253 img->nblocks = dmC64ImageGetNumBlocks(fmt);
254 263
255 // Allocate banks 264 // Allocate banks
256 if ((img->color = dmCalloc(img->nblocks, sizeof(DMC64MemBlock))) == NULL || 265 if ((img->color = dmCalloc(img->nblocks, sizeof(DMC64MemBlock))) == NULL ||
257 (img->bitmap = dmCalloc(img->nblocks, sizeof(DMC64MemBlock))) == NULL || 266 (img->bitmap = dmCalloc(img->nblocks, sizeof(DMC64MemBlock))) == NULL ||
258 (img->screen = dmCalloc(img->nblocks, sizeof(DMC64MemBlock))) == NULL || 267 (img->screen = dmCalloc(img->nblocks, sizeof(DMC64MemBlock))) == NULL ||
884 "Bitmap RAM", 893 "Bitmap RAM",
885 "Screen RAM", 894 "Screen RAM",
886 "Extra data", 895 "Extra data",
887 "Character data", 896 "Character data",
888 897
889 "d020", 898 "d020 / border",
890 "d021/bgcol", 899 "d021 / background",
891 "d022", 900 "d022",
892 "d023", 901 "d023",
893 "d024", 902 "d024",
894 }; 903 };
895 if (subject >= 0 && subject < DS_LAST) 904 if (subject >= 0 && subject < DS_LAST)
942 int res = DMERR_OK; 951 int res = DMERR_OK;
943 952
944 if (buf == NULL || buf->data == NULL || img == NULL || fmt == NULL) 953 if (buf == NULL || buf->data == NULL || img == NULL || fmt == NULL)
945 return DMERR_NULLPTR; 954 return DMERR_NULLPTR;
946 955
947 // Clear the image structure, set basics 956 dmC64SetupImageData(img, fmt);
948 img->nblocks = dmC64ImageGetNumBlocks(fmt);
949 957
950 // Perform decoding 958 // Perform decoding
951 for (int i = 0; i < D64_MAX_ENCDEC_OPS; i++) 959 for (int i = 0; i < D64_MAX_ENCDEC_OPS; i++)
952 { 960 {
953 const DMC64EncDecOp *op = fmtGetEncDecOp(fmt, i); 961 const DMC64EncDecOp *op = fmtGetEncDecOp(fmt, i);
1123 break; 1131 break;
1124 } 1132 }
1125 } 1133 }
1126 1134
1127 // Sanity check certain things .. 1135 // Sanity check certain things ..
1128 if ((fmt->format->type & D64_FMT_ILACE) && 1136 if ((img->extraInfo[D64_EI_MODE] & D64_FMT_ILACE) &&
1129 img->extraInfo[D64_EI_ILACE_TYPE] == D64_ILACE_NONE) { 1137 img->extraInfo[D64_EI_ILACE_TYPE] == D64_ILACE_NONE)
1138 {
1130 return dmError(DMERR_INTERNAL, 1139 return dmError(DMERR_INTERNAL,
1131 "Format '%s' (%s) has interlace flag set, but interlace type is not set.\n", 1140 "Format '%s' (%s) has interlace flag set, but interlace type is not set.\n",
1132 fmt->name, fmt->fext); 1141 fmt->name, fmt->fext);
1133 } 1142 }
1134 1143
1352 1361
1353 if (!img->extraInfo[D64_EI_CHAR_CUSTOM] && 1362 if (!img->extraInfo[D64_EI_CHAR_CUSTOM] &&
1354 img->extraInfo[D64_EI_CHAR_CASE]) 1363 img->extraInfo[D64_EI_CHAR_CASE])
1355 chr += 256; // lower case, so add 256 to char ROM offset 1364 chr += 256; // lower case, so add 256 to char ROM offset
1356 1365
1357 switch (img->extraInfo[D64_EI_CHAR_MODE]) 1366 switch (img->extraInfo[D64_EI_MODE] & D64_FMT_MODE_MASK)
1358 { 1367 {
1359 case D64_FMT_HIRES: 1368 case D64_FMT_HIRES:
1360 return dmC64GetGenericCharSCPixel( 1369 return dmC64GetGenericCharSCPixel(
1361 col, img, 1370 col, img,
1362 scroffs, rasterX, 1371 scroffs, rasterX,
1377 0, ((chr & 0x3f) * D64_CHR_SIZE) + (rasterY & 7), chr, 1386 0, ((chr & 0x3f) * D64_CHR_SIZE) + (rasterY & 7), chr,
1378 0, img->bgcolor, img->d022, img->d023, img->d024); 1387 0, img->bgcolor, img->d022, img->d023, img->d024);
1379 1388
1380 default: 1389 default:
1381 return dmError(DMERR_INVALID_DATA, 1390 return dmError(DMERR_INVALID_DATA,
1382 "Invalid character map image type/fmt=0x%x.\n", 1391 "Invalid character map image mode=0x%x.\n",
1383 img->fmt->type); 1392 img->extraInfo[D64_EI_MODE]);
1384 } 1393 }
1385 } 1394 }
1386 1395
1387 1396
1388 // Convert a generic "C64" format bitmap in DMC64Image struct to 1397 // Convert a generic "C64" format bitmap in DMC64Image struct to
1406 1415
1407 // Check pixel getter function 1416 // Check pixel getter function
1408 if (src->fmt->getPixel != NULL) 1417 if (src->fmt->getPixel != NULL)
1409 getPixel = src->fmt->getPixel; 1418 getPixel = src->fmt->getPixel;
1410 else 1419 else
1411 if (src->fmt->type & D64_FMT_CHAR) 1420 if (src->extraInfo[D64_EI_MODE] & D64_FMT_CHAR)
1412 getPixel = fmtGetGenericCharPixel; 1421 getPixel = fmtGetGenericCharPixel;
1413 else 1422 else
1414 switch (src->fmt->type & D64_FMT_MODE_MASK) 1423 switch (src->extraInfo[D64_EI_MODE] & D64_FMT_MODE_MASK)
1415 { 1424 {
1416 case D64_FMT_MC : getPixel = fmtGetGenericMCPixel; break; 1425 case D64_FMT_MC : getPixel = fmtGetGenericMCPixel; break;
1417 case D64_FMT_HIRES : getPixel = fmtGetGenericSCPixel; break; 1426 case D64_FMT_HIRES : getPixel = fmtGetGenericSCPixel; break;
1418 default: 1427 default:
1419 return dmError(DMERR_INVALID_DATA, 1428 return dmError(DMERR_INVALID_DATA,
1420 "Invalid bitmap image type/fmt=0x%x.\n", 1429 "Invalid bitmap image type/fmt=0x%x.\n",
1421 src->fmt->type); 1430 src->extraInfo[D64_EI_MODE]);
1422 } 1431 }
1423 1432
1424 // Perform conversion 1433 // Perform conversion
1425 for (int yc = 0; yc < dst->height; yc++) 1434 for (int yc = 0; yc < dst->height; yc++)
1426 { 1435 {
1450 if ((*pdst = dst = dmImageAlloc( 1459 if ((*pdst = dst = dmImageAlloc(
1451 src->fmt->width, src->fmt->height, DM_PIXFMT_PALETTE, -1)) == NULL) 1460 src->fmt->width, src->fmt->height, DM_PIXFMT_PALETTE, -1)) == NULL)
1452 return DMERR_MALLOC; 1461 return DMERR_MALLOC;
1453 1462
1454 // Set palette information 1463 // Set palette information
1455 mixed = (src->fmt->type & D64_FMT_ILACE) && 1464 mixed = (src->extraInfo[D64_EI_MODE] & D64_FMT_ILACE) &&
1456 src->extraInfo[D64_EI_ILACE_TYPE] == D64_ILACE_COLOR; 1465 src->extraInfo[D64_EI_ILACE_TYPE] == D64_ILACE_COLOR;
1457 1466
1458 if ((res = dmC64SetImagePalette(dst, spec, mixed)) != DMERR_OK) 1467 if ((res = dmC64SetImagePalette(dst, spec, mixed)) != DMERR_OK)
1459 return res; 1468 return res;
1460 1469
1660 { 1669 {
1661 const DMC64ImageFormat 1670 const DMC64ImageFormat
1662 *fmta = *(DMC64ImageFormat **) va, 1671 *fmta = *(DMC64ImageFormat **) va,
1663 *fmtb = *(DMC64ImageFormat **) vb; 1672 *fmtb = *(DMC64ImageFormat **) vb;
1664 1673
1665 int res = fmta->format->type - fmtb->format->type; 1674 int res = fmta->format->mode - fmtb->format->mode;
1666 if (res == 0) 1675 if (res == 0)
1667 return strcmp(fmta->name, fmtb->name); 1676 return strcmp(fmta->name, fmtb->name);
1668 else 1677 else
1669 return res; 1678 return res;
1670 } 1679 }