diff tools/libgfx.c @ 2208:90ec1ec89c56

Revamp the palette handling in lib64gfx somewhat, add helper functions to lib64util for handling external palette file options and add support for specifying one of the "internal" palettes or external (.act) palette file to gfxconv and 64vw.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 14 Jun 2019 05:01:12 +0300
parents 1ea48084055e
children 7a0af15fbe97
line wrap: on
line diff
--- a/tools/libgfx.c	Fri Jun 14 03:28:46 2019 +0300
+++ b/tools/libgfx.c	Fri Jun 14 05:01:12 2019 +0300
@@ -233,6 +233,8 @@
     if (ncolors - pal->ncolors > 0)
         memset(&(pal->colors[pal->ncolors]), 0, sizeof(DMColor) * (ncolors - pal->ncolors));
 
+    pal->ncolors = ncolors;
+
     return DMERR_OK;
 }
 
@@ -329,13 +331,10 @@
 }
 
 
-#define ACT_MAGIC_ID 0x0010ffff
-
-
 static int fmtProbeACT(const Uint8 *buf, const size_t len)
 {
     if (len == 0x304 &&
-        DM_BE32_TO_NATIVE(*(Uint32 *) (buf + 0x300)) == ACT_MAGIC_ID)
+        buf[0x300] == 0)
         return DM_PROBE_SCORE_MAX;
 
     return DM_PROBE_SCORE_FALSE;
@@ -345,7 +344,7 @@
 int dmReadACTPalette(DMResource *fp, DMPalette **pdst)
 {
     int res;
-    Uint32 magicID;
+    Uint16 tmp1, tmp2;
 
     if ((res = dmPaletteAlloc(pdst, 256, -1)) != DMERR_OK)
         return res;
@@ -353,18 +352,24 @@
     if ((res = dmPaletteReadData(fp, *pdst, 256)) != DMERR_OK)
         goto error;
 
-    if (!dmf_read_be32(fp, &magicID))
+    if (!dmf_read_be16(fp, &tmp1) ||
+        !dmf_read_be16(fp, &tmp2))
     {
         res = DMERR_FREAD;
         goto error;
     }
 
-    if (magicID != ACT_MAGIC_ID)
+    if (tmp1 == 0 || tmp1 > 256)
     {
         res = DMERR_INVALID_DATA;
         goto error;
     }
 
+    if ((res = dmPaletteResize(pdst, tmp1)) != DMERR_OK)
+        goto error;
+
+    (*pdst)->ctransp = tmp2 < 256 ? tmp2 : -1;
+
     return res;
 
 error:
@@ -381,7 +386,8 @@
     if ((res = dmPaletteWriteData(fp, ppal, ppal->ncolors, 256)) != DMERR_OK)
         return res;
 
-    if (!dmf_write_be32(fp, ACT_MAGIC_ID))
+    if (!dmf_write_be16(fp, ppal->ncolors) ||
+        !dmf_write_be16(fp, ppal->ctransp >= 0 ? ppal->ctransp : 0xffff))
         return DMERR_FWRITE;
 
     return DMERR_OK;