diff tools/gfxconv.c @ 1609:c29adf5ce240

Convert libgfx file format routines to use DMResource instead of stdio FILE. Also do necessary changes in gfxconv due to these API changes.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 14 May 2018 12:42:24 +0300
parents d91d2a67eda5
children 36d073c45327
line wrap: on
line diff
--- a/tools/gfxconv.c	Mon May 14 12:40:57 2018 +0300
+++ b/tools/gfxconv.c	Mon May 14 12:42:24 2018 +0300
@@ -1091,6 +1091,32 @@
 }
 
 
+#define DMCOL(x) (((x) >> 4) & 0xf)
+
+void dmWriteIFFMasterRAWPalette(FILE *fp,
+    const DMImage *img, int ncolors,
+    const char *indent, const char *type)
+{
+    for (int i = 0; i < ncolors; i++)
+    {
+        int color;
+        if (i < img->ncolors)
+        {
+            color = (DMCOL(img->pal[i].r) << 8) |
+                    (DMCOL(img->pal[i].g) << 4) |
+                    (DMCOL(img->pal[i].b));
+        }
+        else
+            color = 0;
+
+        fprintf(fp, "%s%s $%04X\n",
+            indent != NULL ? indent : "\t",
+            type != NULL ? type : "dc.w",
+            color);
+    }
+}
+
+
 int dmWriteImage(const char *filename, DMImage *pimage, DMImageConvSpec *spec, int iformat, BOOL info)
 {
     int res = DMERR_OK;
@@ -1786,6 +1812,7 @@
             {
                 DMImage *inImage = NULL;
                 int res = DMERR_OK;
+                DMResource *fp;
 
                 if (optOutFilename == NULL)
                 {
@@ -1793,13 +1820,21 @@
                     goto error;
                 }
 
+                if ((res = dmf_open_memio(NULL, optInFilename, dataBuf, dataSize, &fp)) != DMERR_OK)
+                {
+                    dmErrorMsg("Could not create MemIO handle for input.\n");
+                    goto error;
+                }
+
                 // Read input
                 DMImageFormat *ifmt = &dmImageFormatList[optInSubFormat];
                 if (ifmt->readFILE != NULL)
-                    res = ifmt->readFILE(inFile, &inImage);
+                    res = ifmt->readFILE(fp, &inImage);
                 else
                     dmErrorMsg("Unsupported input image format for bitmap/image conversion.\n");
 
+                dmf_close(fp);
+
                 if (res != DMERR_OK || inImage == NULL)
                     break;
 
@@ -1831,9 +1866,6 @@
     }
 
 error:
-    if (inFile != NULL)
-        fclose(inFile);
-
     dmFree(dataBufOrig);
     dmC64ImageFree(inC64Image);
     dmC64ImageFree(outC64Image);