Mercurial > hg > dmlib
comparison tools/lib64gfx.c @ 2586:9807ae37ad69
Require stdbool.h, we require C11 now.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 08 Dec 2022 15:59:22 +0200 |
parents | c6ee41fd98dd |
children | c3c1d3c75f53 |
comparison
equal
deleted
inserted
replaced
2585:ef6c826c5b7a | 2586:9807ae37ad69 |
---|---|
10 | 10 |
11 #define BUF_SIZE_INITIAL (16*1024) | 11 #define BUF_SIZE_INITIAL (16*1024) |
12 #define BUF_SIZE_GROW (4*1024) | 12 #define BUF_SIZE_GROW (4*1024) |
13 | 13 |
14 | 14 |
15 int dmC64PaletteFromC64Colors(DMPalette **ppal, const DMColor *colors, const BOOL mixed) | 15 int dmC64PaletteFromC64Colors(DMPalette **ppal, const DMColor *colors, const bool mixed) |
16 { | 16 { |
17 int res; | 17 int res; |
18 | 18 |
19 if (ppal == NULL || colors == NULL) | 19 if (ppal == NULL || colors == NULL) |
20 return DMERR_NULLPTR; | 20 return DMERR_NULLPTR; |
50 | 50 |
51 return DMERR_OK; | 51 return DMERR_OK; |
52 } | 52 } |
53 | 53 |
54 | 54 |
55 int dmC64PaletteFromC64Palette(DMPalette **ppal, const DMC64Palette *cpal, const BOOL mixed) | 55 int dmC64PaletteFromC64Palette(DMPalette **ppal, const DMC64Palette *cpal, const bool mixed) |
56 { | 56 { |
57 if (ppal == NULL || cpal == NULL) | 57 if (ppal == NULL || cpal == NULL) |
58 return DMERR_NULLPTR; | 58 return DMERR_NULLPTR; |
59 | 59 |
60 return dmC64PaletteFromC64Colors(ppal, cpal->colors, mixed); | 60 return dmC64PaletteFromC64Colors(ppal, cpal->colors, mixed); |
61 } | 61 } |
62 | 62 |
63 | 63 |
64 int dmC64SetImagePalette(DMImage *img, const DMC64ImageConvSpec *spec, const BOOL mixed) | 64 int dmC64SetImagePalette(DMImage *img, const DMC64ImageConvSpec *spec, const bool mixed) |
65 { | 65 { |
66 if (img == NULL || spec == NULL) | 66 if (img == NULL || spec == NULL) |
67 return DMERR_NULLPTR; | 67 return DMERR_NULLPTR; |
68 | 68 |
69 // Free previous palette | 69 // Free previous palette |
70 if (!img->constpal) | 70 if (!img->constpal) |
71 dmPaletteFree(img->pal); | 71 dmPaletteFree(img->pal); |
72 | 72 |
73 img->constpal = FALSE; | 73 img->constpal = false; |
74 | 74 |
75 // If specific palette is wanted, use it | 75 // If specific palette is wanted, use it |
76 if (spec->pal != NULL) | 76 if (spec->pal != NULL) |
77 { | 77 { |
78 if (spec->pal->ncolors > D64_NCOLORS) | 78 if (spec->pal->ncolors > D64_NCOLORS) |
85 // Else, use the c64 palette specified | 85 // Else, use the c64 palette specified |
86 return dmC64PaletteFromC64Palette(&img->pal, spec->cpal, mixed); | 86 return dmC64PaletteFromC64Palette(&img->pal, spec->cpal, mixed); |
87 } | 87 } |
88 | 88 |
89 | 89 |
90 BOOL dmCompareAddr16(const DMGrowBuf *buf, const size_t offs, const Uint16 addr) | 90 bool dmCompareAddr16(const DMGrowBuf *buf, const size_t offs, const Uint16 addr) |
91 { | 91 { |
92 return | 92 return |
93 offs + 1 < buf->len && | 93 offs + 1 < buf->len && |
94 buf->data[offs ] == (addr & 0xff) && | 94 buf->data[offs ] == (addr & 0xff) && |
95 buf->data[offs + 1] == ((addr >> 8) & 0xff); | 95 buf->data[offs + 1] == ((addr >> 8) & 0xff); |
223 } | 223 } |
224 | 224 |
225 | 225 |
226 int dmC64ConvertCSDataToImage(DMImage *img, | 226 int dmC64ConvertCSDataToImage(DMImage *img, |
227 const int xoffs, const int yoffs, const Uint8 *buf, | 227 const int xoffs, const int yoffs, const Uint8 *buf, |
228 const int width, const int height, const BOOL multicolor, | 228 const int width, const int height, const bool multicolor, |
229 const int *colors) | 229 const int *colors) |
230 { | 230 { |
231 int yc, widthpx = width * 8; | 231 int yc, widthpx = width * 8; |
232 Uint8 *dpp; | 232 Uint8 *dpp; |
233 | 233 |
347 void dmSetupRLEBuffers(DMGrowBuf *dst, DMGrowBuf *src, const DMCompParams *cfg) | 347 void dmSetupRLEBuffers(DMGrowBuf *dst, DMGrowBuf *src, const DMCompParams *cfg) |
348 { | 348 { |
349 if (src != NULL && (cfg->flags & DM_RLE_BACKWARDS_INPUT)) | 349 if (src != NULL && (cfg->flags & DM_RLE_BACKWARDS_INPUT)) |
350 { | 350 { |
351 src->offs = src->len; | 351 src->offs = src->len; |
352 src->backwards = TRUE; | 352 src->backwards = true; |
353 } | 353 } |
354 | 354 |
355 if (dst != NULL && (cfg->flags & DM_RLE_BACKWARDS_OUTPUT)) | 355 if (dst != NULL && (cfg->flags & DM_RLE_BACKWARDS_OUTPUT)) |
356 { | 356 { |
357 dst->backwards = TRUE; | 357 dst->backwards = true; |
358 dst->offs = dst->size; | 358 dst->offs = dst->size; |
359 } | 359 } |
360 | 360 |
361 #ifdef RLE_DEBUG | 361 #ifdef RLE_DEBUG |
362 fprintf(stderr, "dmSetupRLEBuffers:\n"); | 362 fprintf(stderr, "dmSetupRLEBuffers:\n"); |
564 } | 564 } |
565 | 565 |
566 | 566 |
567 int dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, unsigned int count, const DMCompParams *cfg) | 567 int dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, unsigned int count, const DMCompParams *cfg) |
568 { | 568 { |
569 BOOL copyOnly = FALSE; | 569 bool copyOnly = false; |
570 int res; | 570 int res; |
571 | 571 |
572 switch (cfg->type) | 572 switch (cfg->type) |
573 { | 573 { |
574 case DM_COMP_RLE_MARKER: | 574 case DM_COMP_RLE_MARKER: |
620 goto out; | 620 goto out; |
621 break; | 621 break; |
622 } | 622 } |
623 } | 623 } |
624 else | 624 else |
625 copyOnly = TRUE; | 625 copyOnly = true; |
626 break; | 626 break; |
627 | 627 |
628 case DM_COMP_RLE_MASK: | 628 case DM_COMP_RLE_MASK: |
629 if (count >= cfg->rleMinCountB || (data & cfg->rleMarkerMask) == cfg->rleMarkerBits) | 629 if (count >= cfg->rleMinCountB || (data & cfg->rleMarkerMask) == cfg->rleMarkerBits) |
630 { | 630 { |
633 if (!dmGrowBufPutU8(dst, cfg->rleMarkerBits | count) || | 633 if (!dmGrowBufPutU8(dst, cfg->rleMarkerBits | count) || |
634 !dmGrowBufPutU8(dst, data)) | 634 !dmGrowBufPutU8(dst, data)) |
635 goto out; | 635 goto out; |
636 } | 636 } |
637 else | 637 else |
638 copyOnly = TRUE; | 638 copyOnly = true; |
639 break; | 639 break; |
640 } | 640 } |
641 | 641 |
642 if (copyOnly && (res = dmGenericRLEOutputRun(dst, cfg, data, count)) != DMERR_OK) | 642 if (copyOnly && (res = dmGenericRLEOutputRun(dst, cfg, data, count)) != DMERR_OK) |
643 return res; | 643 return res; |
1073 | 1073 |
1074 return DMERR_OK; | 1074 return DMERR_OK; |
1075 } | 1075 } |
1076 | 1076 |
1077 | 1077 |
1078 int dmC64EncodeGenericBMP(const BOOL allocate, DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt) | 1078 int dmC64EncodeGenericBMP(const bool allocate, DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt) |
1079 { | 1079 { |
1080 DMC64EncDecCtx ctx; | 1080 DMC64EncDecCtx ctx; |
1081 int res = DMERR_OK; | 1081 int res = DMERR_OK; |
1082 | 1082 |
1083 if (img == NULL || fmt == NULL) | 1083 if (img == NULL || fmt == NULL) |
1479 | 1479 |
1480 | 1480 |
1481 int dmC64ConvertBMP2Image(DMImage **pdst, const DMC64Image *src, const DMC64ImageConvSpec *pspec) | 1481 int dmC64ConvertBMP2Image(DMImage **pdst, const DMC64Image *src, const DMC64ImageConvSpec *pspec) |
1482 { | 1482 { |
1483 DMC64ImageConvSpec spec; | 1483 DMC64ImageConvSpec spec; |
1484 BOOL mixed; | 1484 bool mixed; |
1485 int res; | 1485 int res; |
1486 | 1486 |
1487 if (pdst == NULL || src == NULL || pspec == NULL) | 1487 if (pdst == NULL || src == NULL || pspec == NULL) |
1488 return DMERR_NULLPTR; | 1488 return DMERR_NULLPTR; |
1489 | 1489 |
1531 | 1531 |
1532 if ((size_t) probeOffs >= buf->len) | 1532 if ((size_t) probeOffs >= buf->len) |
1533 return DMERR_OUT_OF_DATA; | 1533 return DMERR_OUT_OF_DATA; |
1534 | 1534 |
1535 dmGrowBufConstCopyOffs(&tmp, buf, probeOffs); | 1535 dmGrowBufConstCopyOffs(&tmp, buf, probeOffs); |
1536 if (dmC64ProbeBMP(&tmp, fmt) == DM_PROBE_SCORE_FALSE) | 1536 if (dmC64ProbeBMP(&tmp, fmt) == DM_PROBE_SCORE_false) |
1537 return DMERR_NOT_SUPPORTED; | 1537 return DMERR_NOT_SUPPORTED; |
1538 } | 1538 } |
1539 | 1539 |
1540 if (*fmt == NULL) | 1540 if (*fmt == NULL) |
1541 return DMERR_NOT_SUPPORTED; | 1541 return DMERR_NOT_SUPPORTED; |
1641 | 1641 |
1642 // Attempt to encode the image to a buffer | 1642 // Attempt to encode the image to a buffer |
1643 if (fmt->encode != NULL) | 1643 if (fmt->encode != NULL) |
1644 res = fmt->encode(buf, img, fmt); | 1644 res = fmt->encode(buf, img, fmt); |
1645 else | 1645 else |
1646 res = dmC64EncodeGenericBMP(FALSE, buf, img, fmt); | 1646 res = dmC64EncodeGenericBMP(false, buf, img, fmt); |
1647 | 1647 |
1648 if (res != DMERR_OK) | 1648 if (res != DMERR_OK) |
1649 goto out; | 1649 goto out; |
1650 | 1650 |
1651 // Finally, if the format has a set size and our buffer is smaller | 1651 // Finally, if the format has a set size and our buffer is smaller |
1671 // if it contains a supported "C64" image format. Returns the | 1671 // if it contains a supported "C64" image format. Returns the |
1672 // "probe score", see libgfx.h for list of values. If a match | 1672 // "probe score", see libgfx.h for list of values. If a match |
1673 // is found, pointer to format description is set to *pfmt. | 1673 // is found, pointer to format description is set to *pfmt. |
1674 int dmC64ProbeBMP(const DMGrowBuf *buf, const DMC64ImageFormat **pfmt) | 1674 int dmC64ProbeBMP(const DMGrowBuf *buf, const DMC64ImageFormat **pfmt) |
1675 { | 1675 { |
1676 int scoreMax = DM_PROBE_SCORE_FALSE, scoreIndex = -1; | 1676 int scoreMax = DM_PROBE_SCORE_false, scoreIndex = -1; |
1677 | 1677 |
1678 for (int i = 0; i < ndmC64ImageFormats; i++) | 1678 for (int i = 0; i < ndmC64ImageFormats; i++) |
1679 { | 1679 { |
1680 const DMC64ImageFormat *fmt = &dmC64ImageFormats[i]; | 1680 const DMC64ImageFormat *fmt = &dmC64ImageFormats[i]; |
1681 int score = DM_PROBE_SCORE_FALSE; | 1681 int score = DM_PROBE_SCORE_false; |
1682 if (fmt->probe == NULL && fmt->size > 0 && fmt->addr > 0) | 1682 if (fmt->probe == NULL && fmt->size > 0 && fmt->addr > 0) |
1683 { | 1683 { |
1684 // Generic probe just checks matching size and load address | 1684 // Generic probe just checks matching size and load address |
1685 if (buf->len == fmt->size && dmCompareAddr16(buf, 0, fmt->addr)) | 1685 if (buf->len == fmt->size && dmCompareAddr16(buf, 0, fmt->addr)) |
1686 score = DM_PROBE_SCORE_GOOD; | 1686 score = DM_PROBE_SCORE_GOOD; |
1700 { | 1700 { |
1701 *pfmt = &dmC64ImageFormats[scoreIndex]; | 1701 *pfmt = &dmC64ImageFormats[scoreIndex]; |
1702 return scoreMax; | 1702 return scoreMax; |
1703 } | 1703 } |
1704 else | 1704 else |
1705 return DM_PROBE_SCORE_FALSE; | 1705 return DM_PROBE_SCORE_false; |
1706 } | 1706 } |
1707 | 1707 |
1708 | 1708 |
1709 BOOL dmLib64GFXInitialized = FALSE; | 1709 bool dmLib64GFXInitialized = false; |
1710 DMC64ImageFormat **dmC64ImageFormatsSorted = NULL; | 1710 DMC64ImageFormat **dmC64ImageFormatsSorted = NULL; |
1711 | 1711 |
1712 | 1712 |
1713 int dmC64ImageFormatCompare(const void *va, const void *vb) | 1713 int dmC64ImageFormatCompare(const void *va, const void *vb) |
1714 { | 1714 { |
1728 { | 1728 { |
1729 // Safety check | 1729 // Safety check |
1730 if (dmLib64GFXInitialized) | 1730 if (dmLib64GFXInitialized) |
1731 dmLib64GFXClose(); | 1731 dmLib64GFXClose(); |
1732 | 1732 |
1733 dmLib64GFXInitialized = TRUE; | 1733 dmLib64GFXInitialized = true; |
1734 | 1734 |
1735 if ((dmC64ImageFormatsSorted = dmCalloc(ndmC64ImageFormats, sizeof(dmC64ImageFormatsSorted[0]))) == NULL) | 1735 if ((dmC64ImageFormatsSorted = dmCalloc(ndmC64ImageFormats, sizeof(dmC64ImageFormatsSorted[0]))) == NULL) |
1736 return DMERR_MALLOC; | 1736 return DMERR_MALLOC; |
1737 | 1737 |
1738 for (int i = 0; i < ndmC64ImageFormats; i++) | 1738 for (int i = 0; i < ndmC64ImageFormats; i++) |
1752 | 1752 |
1753 | 1753 |
1754 void dmLib64GFXClose(void) | 1754 void dmLib64GFXClose(void) |
1755 { | 1755 { |
1756 dmFreeR(&dmC64ImageFormatsSorted); | 1756 dmFreeR(&dmC64ImageFormatsSorted); |
1757 dmLib64GFXInitialized = FALSE; | 1757 dmLib64GFXInitialized = false; |
1758 } | 1758 } |