changeset 1886:1af79412f249

Remove the stdio FILE support from libgfx API, now only DMResource is supported.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 25 Jun 2018 13:52:20 +0300
parents e69de47d2419
children 297aa8f0ca7f
files tools/gfxconv.c tools/libgfx.c tools/libgfx.h
diffstat 3 files changed, 109 insertions(+), 237 deletions(-) [+]
line wrap: on
line diff
--- a/tools/gfxconv.c	Mon Jun 25 13:24:50 2018 +0300
+++ b/tools/gfxconv.c	Mon Jun 25 13:52:20 2018 +0300
@@ -1186,64 +1186,24 @@
 }
 
 
-#define DMCOL(x) (((x) >> 4) & 0xf)
-
-int dmWriteIFFMasterRAW(const char *filename, const char *prefix, const DMImage *img, const DMImageConvSpec *spec, const int fmtid)
+int dmWriteIFFMasterRAWHeaderFile(
+    const char *hdrFilename, const char *dataFilename,
+    const char *prefix, const DMImage *img,
+    const DMImageConvSpec *spec, const int fmtid)
 {
-    // Open data file for writing
-    FILE *fp = NULL;
-    int res = DMERR_OK;
+    DMResource *fp;
+    int res;
 
-    if ((fp = fopen(filename, "w")) == NULL)
+    if ((res = dmf_open_stdio(hdrFilename, "wb", &fp)) != DMERR_OK)
     {
-        res = dmError(DMERR_FOPEN,
-            "Could not create file '%s'.\n", filename);
-        goto err;
+        return dmError(res,
+            "RAW: Could not open file '%s' for writing.\n",
+            hdrFilename);
     }
 
-
-    fprintf(fp,
-        "%s_width: dw.w %d\n"
-        "%s_height: dw.w %d\n"
-        "%s_nplanes: dw.w %d\n",
-        prefix, img->width * spec->scaleX,
-        prefix, img->height * spec->scaleY,
-        prefix, spec->nplanes);
-
-    if (fmtid == DM_IMGFMT_ARAW)
-    {
-        fprintf(fp,
-            "%s_ncolors: dw.w %d\n"
-            "%s_palette:\n",
-            prefix, img->ncolors,
-            prefix);
+    res = dmWriteIFFMasterRAWHeader(fp, dataFilename, prefix, img, spec, fmtid);
 
-        for (int i = 0; i < (1 << spec->nplanes); i++)
-        {
-            Uint32 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,
-                "\tdc.w $%04X\n",
-                color);
-        }
-
-        fprintf(fp,
-            "%s: incbin \"%s\"\n",
-            prefix, filename);
-    }
-
-err:
-    if (fp != NULL)
-        fclose(fp);
-
+    dmf_close(fp);
     return res;
 }
 
@@ -1294,8 +1254,8 @@
         case DM_IMGFMT_RAW:
         case DM_IMGFMT_ARAW:
             {
-                char *prefix = NULL, *dataFilename = NULL;
-                if ((dataFilename = dm_strdup_fext(filename, "%s.inc")) == NULL ||
+                char *prefix = NULL, *hdrFilename = NULL;
+                if ((hdrFilename = dm_strdup_fext(filename, "%s.inc")) == NULL ||
                     (prefix = dm_strdup_fext(filename, "img_%s")) == NULL)
                 {
                     res = dmError(DMERR_MALLOC,
@@ -1314,12 +1274,13 @@
                         spec->planar ? "planar/interleaved" : "non-interleaved");
                     dmMsg(2, "%s datafile '%s', ID prefix '%s'.\n",
                         fmt->fmtid == DM_IMGFMT_ARAW ? "ARAW" : "RAW",
-                        dataFilename, prefix);
+                        hdrFilename, prefix);
                 }
-                res = dmWriteIFFMasterRAW(dataFilename, prefix, image, spec, fmt->fmtid);
+                res = dmWriteIFFMasterRAWHeaderFile(
+                    hdrFilename, filename, prefix, image, spec, fmt->fmtid);
 
                 dmFree(prefix);
-                dmFree(dataFilename);
+                dmFree(hdrFilename);
             }
             break;
 
@@ -1330,6 +1291,8 @@
     // If no error has occured thus far, write the image
     if (res == DMERR_OK)
     {
+        DMResource *fp;
+
         if (info)
         {
             char *str;
@@ -1343,7 +1306,16 @@
             dmMsg(2, "Using %s output.\n", str);
         }
 
-        res = fmt->write(filename, image, spec);
+        if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK)
+        {
+            dmErrorMsg("Could not open file '%s' for writing.\n",
+                filename);
+            goto err;
+        }
+
+        res = fmt->write(fp, image, spec);
+
+        dmf_close(fp);
     }
 
 err:
@@ -1951,8 +1923,8 @@
                 }
 
                 // Read input
-                if (ifmt->readFILE != NULL)
-                    res = ifmt->readFILE(fp, &inImage);
+                if (ifmt->read != NULL)
+                    res = ifmt->read(fp, &inImage);
                 else
                     dmErrorMsg("Unsupported input image format for image conversion.\n");
 
--- a/tools/libgfx.c	Mon Jun 25 13:24:50 2018 +0300
+++ b/tools/libgfx.c	Mon Jun 25 13:52:20 2018 +0300
@@ -299,6 +299,54 @@
 }
 
 
+#define DMCOL(x) (((x) >> 4) & 0xf)
+
+int dmWriteIFFMasterRAWHeader(
+    DMResource *fp, const char *filename, const char *prefix,
+    const DMImage *img, const DMImageConvSpec *spec, const int fmtid)
+{
+    dmfprintf(fp,
+        "%s_width: dw.w %d\n"
+        "%s_height: dw.w %d\n"
+        "%s_nplanes: dw.w %d\n",
+        prefix, img->width * spec->scaleX,
+        prefix, img->height * spec->scaleY,
+        prefix, spec->nplanes);
+
+    if (fmtid == DM_IMGFMT_ARAW)
+    {
+        dmfprintf(fp,
+            "%s_ncolors: dw.w %d\n"
+            "%s_palette:\n",
+            prefix, img->ncolors,
+            prefix);
+
+        for (int i = 0; i < (1 << spec->nplanes); i++)
+        {
+            Uint32 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;
+
+            dmfprintf(fp,
+                "\tdc.w $%04X\n",
+                color);
+        }
+
+        dmfprintf(fp,
+            "%s: incbin \"%s\"\n",
+            prefix, filename);
+    }
+
+    return dmferror(fp);
+}
+
+
 static BOOL dmWriteRAWRow(DMBitStreamContext *bs, const DMImage *img, const DMImageConvSpec *spec, const int yc, const int plane)
 {
     const Uint8 *sp = img->data + (yc * img->pitch);
@@ -312,7 +360,7 @@
 }
 
 
-int dmWriteRAWImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
+int dmWriteRAWImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
 {
     int res;
     DMBitStreamContext bs;
@@ -348,25 +396,6 @@
 }
 
 
-int dmWriteRAWImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec)
-{
-    DMResource *fp;
-    int res;
-
-    if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK)
-    {
-        return dmError(res,
-            "RAW: Could not open file '%s' for writing.\n",
-            filename);
-    }
-
-    res = dmWriteRAWImageFILE(fp, img, spec);
-
-    dmf_close(fp);
-    return res;
-}
-
-
 static int dmWritePPMRow(void *cbdata, const Uint8 *row, const size_t len)
 {
     if (dmf_write_str((DMResource *) cbdata, row, len))
@@ -376,7 +405,7 @@
 }
 
 
-int dmWritePPMImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
+int dmWritePPMImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
 {
     DMImageConvSpec tmpSpec;
 
@@ -399,26 +428,6 @@
 }
 
 
-int dmWritePPMImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec)
-{
-    DMResource *fp;
-    int res;
-
-    // Create output file
-    if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK)
-    {
-        return dmError(res,
-            "PPM: could not open file '%s' for writing.\n",
-            filename);
-    }
-
-    res = dmWritePPMImageFILE(fp, img, spec);
-
-    dmf_close(fp);
-    return res;
-}
-
-
 #ifdef DM_USE_LIBPNG
 static int fmtProbePNG(const Uint8 *buf, const size_t len)
 {
@@ -466,7 +475,7 @@
 }
 
 
-int dmWritePNGImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
+int dmWritePNGImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec)
 {
     png_structp png_ptr = NULL;
     png_infop info_ptr = NULL;
@@ -574,25 +583,6 @@
 }
 
 
-int dmWritePNGImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec)
-{
-    int res;
-    DMResource *fp;
-
-    if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK)
-    {
-        return dmError(res,
-            "PNG: could not open file '%s' for writing.\n",
-            filename);
-    }
-
-    res = dmWritePNGImageFILE(fp, img, spec);
-
-    dmf_close(fp);
-    return res;
-}
-
-
 void dmPNGReadData(png_structp png_ptr, png_bytep data, png_size_t length)
 {
     DMResource *res = (DMResource *) png_get_io_ptr(png_ptr);
@@ -601,7 +591,8 @@
     dmf_read_str(res, data, length);
 }
 
-int dmReadPNGImageFILE(DMResource *fp, DMImage **pimg)
+
+int dmReadPNGImage(DMResource *fp, DMImage **pimg)
 {
     png_structp png_ptr = NULL;
     png_infop info_ptr = NULL;
@@ -775,25 +766,6 @@
 
     return res;
 }
-
-
-int dmReadPNGImage(const char *filename, DMImage **img)
-{
-    int res;
-    DMResource *fp;
-
-    if ((res = dmf_open_stdio(filename, "rb", &fp)) != DMERR_OK)
-    {
-        return dmError(res,
-            "PNG: Could not open file '%s' for reading.\n",
-            filename);
-    }
-
-    res = dmReadPNGImageFILE(fp, img);
-
-    dmf_close(fp);
-    return res;
-}
 #endif
 
 
@@ -944,7 +916,7 @@
 }
 
 
-int dmWritePCXImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *pspec)
+int dmWritePCXImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *pspec)
 {
     DMPCXData pcx;
     DMPCXHeader hdr;
@@ -1113,25 +1085,6 @@
 }
 
 
-int dmWritePCXImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec)
-{
-    DMResource *fp;
-    int res;
-
-    if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK)
-    {
-        return dmError(res,
-            "PCX: Could not open file '%s' for writing.\n",
-            filename);
-    }
-
-    res = dmWritePCXImageFILE(fp, img, spec);
-
-    dmf_close(fp);
-    return res;
-}
-
-
 static BOOL dmPCXDecodeRLERow(DMResource *fp, Uint8 *buf, const size_t bufLen)
 {
     size_t offs = 0;
@@ -1188,7 +1141,7 @@
 }
 
 
-int dmReadPCXImageFILE(DMResource *fp, DMImage **pimg)
+int dmReadPCXImage(DMResource *fp, DMImage **pimg)
 {
     DMImage *img;
     DMPCXData pcx;
@@ -1419,25 +1372,6 @@
 }
 
 
-int dmReadPCXImage(const char *filename, DMImage **pimg)
-{
-    DMResource *fp;
-    int res;
-
-    if ((res = dmf_open_stdio(filename, "rb", &fp)) != DMERR_OK)
-    {
-        return dmError(res,
-            "PCX: Could not open file '%s' for reading.\n",
-            filename);
-    }
-
-    res = dmReadPCXImageFILE(fp, pimg);
-
-    dmf_close(fp);
-    return res;
-}
-
-
 //
 // IFF ILBM / PBM format
 //
@@ -1744,7 +1678,7 @@
 }
 
 
-int dmReadILBMImageFILE(DMResource *fp, DMImage **pimg)
+int dmReadILBMImage(DMResource *fp, DMImage **pimg)
 {
     DMIFFChunk chunk;
     DMIFF iff;
@@ -1997,25 +1931,6 @@
 }
 
 
-int dmReadILBMImage(const char *filename, DMImage **pimg)
-{
-    DMResource *fp;
-    int res;
-
-    if ((res = dmf_open_stdio(filename, "rb", &fp)) != DMERR_OK)
-    {
-        return dmError(res,
-            "ILBM: Could not open file '%s' for reading.\n",
-            filename);
-    }
-
-    res = dmReadILBMImageFILE(fp, pimg);
-
-    dmf_close(fp);
-    return res;
-}
-
-
 //
 // List of formats
 //
@@ -2025,45 +1940,34 @@
     {
         "png", "Portable Network Graphics",
         DM_IMGFMT_PNG, DM_FMT_RDWR,
-        fmtProbePNG,
-        dmReadPNGImage, dmReadPNGImageFILE,
-        dmWritePNGImage, dmWritePNGImageFILE,
+        fmtProbePNG, dmReadPNGImage, dmWritePNGImage,
     },
 #endif
     {
         "ppm", "Portable PixMap",
         DM_IMGFMT_PPM, DM_FMT_WR,
-        NULL,
-        NULL, NULL,
-        dmWritePPMImage, dmWritePPMImageFILE,
+        NULL, NULL, dmWritePPMImage,
     },
     {
         "pcx", "Z-Soft Paintbrush",
         DM_IMGFMT_PCX, DM_FMT_RDWR,
-        fmtProbePCX,
-        dmReadPCXImage, dmReadPCXImageFILE,
-        dmWritePCXImage, dmWritePCXImageFILE,
+        fmtProbePCX, dmReadPCXImage, dmWritePCXImage,
     },
     {
         "ilbm", "IFF ILBM / PBM",
-        DM_IMGFMT_ILBM, DM_FMT_RD,
-        fmtProbeILBM,
-        dmReadILBMImage, dmReadILBMImageFILE,
-        NULL, NULL,
+        DM_IMGFMT_ILBM, DM_FMT_RDWR,
+        fmtProbeILBM, dmReadILBMImage,
+        NULL,
     },
     {
         "raw", "Plain bitplaned (planar or non-planar) RAW",
         DM_IMGFMT_RAW, DM_FMT_WR,
-        NULL,
-        NULL, NULL,
-        dmWriteRAWImage, dmWriteRAWImageFILE,
+        NULL, NULL, dmWriteRAWImage,
     },
     {
         "araw", "IFFMaster Amiga RAW",
         DM_IMGFMT_ARAW, DM_FMT_WR,
-        NULL,
-        NULL, NULL,
-        dmWriteRAWImage, dmWriteRAWImageFILE,
+        NULL, NULL, dmWriteRAWImage,
     }
 };
 
--- a/tools/libgfx.h	Mon Jun 25 13:24:50 2018 +0300
+++ b/tools/libgfx.h	Mon Jun 25 13:52:20 2018 +0300
@@ -102,10 +102,8 @@
     int  fmtid;  // DM_IMGFMT_*
     int  flags;  // DM_FMT_* flags
     int  (*probe)(const Uint8 *buf, const size_t len);
-    int  (*read)(const char *filename, DMImage **pimg);
-    int  (*readFILE)(DMResource *fp, DMImage **pimg);
-    int  (*write)(const char *filename, const DMImage *pimg, const DMImageConvSpec *spec);
-    int  (*writeFILE)(DMResource *fp, const DMImage *pimg, const DMImageConvSpec *spec);
+    int  (*read)(DMResource *fp, DMImage **pimg);
+    int  (*write)(DMResource *fp, const DMImage *pimg, const DMImageConvSpec *spec);
 } DMImageFormat;
 
 
@@ -124,26 +122,24 @@
 BOOL dmImagePaletteAlloc(DMImage *img, int ncolors, int ctransp);
 
 
-int dmWriteImageData(const DMImage *img, void *cbdata, int (*writeRowCB)(void *, const Uint8 *, const size_t), const DMImageConvSpec *spec);
+int dmWriteImageData(const DMImage *img, void *cbdata,
+    int (*writeRowCB)(void *, const Uint8 *, const size_t), const DMImageConvSpec *spec);
+
 
-int dmWriteRAWImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec);
-int dmWriteRAWImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec);
+int dmWriteIFFMasterRAWHeader(DMResource *fp, const char *filename, const char *prefix, const DMImage *img, const DMImageConvSpec *spec, const int fmtid);
+int dmWriteRAWImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec);
 
-int dmWritePPMImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec);
-int dmWritePPMImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec);
+int dmWritePPMImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec);
 
 #ifdef DM_USE_LIBPNG
-int dmWritePNGImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec);
-int dmWritePNGImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec);
+int dmWritePNGImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec);
+int dmReadPNGImage(DMResource *fp, DMImage **pimg);
 #endif
 
-int dmWritePCXImageFILE(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec);
-int dmWritePCXImage(const char *filename, const DMImage *img, const DMImageConvSpec *spec);
-int dmReadPCXImageFILE(DMResource *fp, DMImage **pimg);
-int dmReadPCXImage(const char *filename, DMImage **pimg);
+int dmWritePCXImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec);
+int dmReadPCXImage(DMResource *fp, DMImage **pimg);
 
-int dmReadILBMImageFILE(DMResource *fp, DMImage **pimg);
-int dmReadILBMImage(const char *filename, DMImage **pimg);
+int dmReadILBMImage(DMResource *fp, DMImage **pimg);
 
 
 typedef struct _DMBitStreamContext