changeset 1288:6c8b19d1d196

More work on libgfx.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 18 Aug 2017 17:32:19 +0300
parents 32051ad352c8
children 4b025a96ad46
files src/libgfx.c src/libgfx.h tools/gfxconv.c
diffstat 3 files changed, 58 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/libgfx.c	Fri Aug 18 15:27:36 2017 +0300
+++ b/src/libgfx.c	Fri Aug 18 17:32:19 2017 +0300
@@ -137,6 +137,12 @@
 }
 
 
+int dmImageConvertTo(DMImage **dst, const DMImage *src, const DMImageSpec *spec)
+{
+    return DMERR_OK;
+}
+
+
 int dmWriteImageData(DMImage *img, void *cbdata, int (*writeRowCB)(void *, Uint8 *, size_t), const DMImageSpec *spec)
 {
     int x, y, yscale, xscale, res = 0, rowSize, rowWidth;
@@ -254,7 +260,7 @@
 }
 
 
-int dmWriteRAWImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec)
+int dmWriteRAWImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec)
 {
     int xc, yc, plane, res;
     DMBitStreamContext bs;
@@ -299,7 +305,7 @@
 }
 
 
-int dmWriteRAWImage(const char *filename, DMImage *img, DMImageSpec *spec)
+int dmWriteRAWImage(const char *filename, DMImage *img, const DMImageSpec *spec)
 {
     FILE *fp;
     int res;
@@ -327,8 +333,10 @@
 }
 
 
-int dmWritePPMImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec)
+int dmWritePPMImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec)
 {
+    DMImageSpec tmpSpec;
+
     // Write PPM header
     fprintf(fp,
         "P6\n%d %d\n255\n",
@@ -336,12 +344,13 @@
         img->height * spec->scaleY);
 
     // Write image data
-    spec->format = DM_IFMT_RGB;
-    return dmWriteImageData(img, (void *) fp, dmWritePPMRow, spec);
+    memcpy(&tmpSpec, spec, sizeof(DMImageSpec));
+    tmpSpec.format = DM_IFMT_RGB;
+    return dmWriteImageData(img, (void *) fp, dmWritePPMRow, &tmpSpec);
 }
 
 
-int dmWritePPMImage(const char *filename, DMImage *img, DMImageSpec *spec)
+int dmWritePPMImage(const char *filename, DMImage *img, const DMImageSpec *spec)
 {
     FILE *fp;
     int res;
@@ -376,7 +385,7 @@
 }
 
 
-int dmWritePNGImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec)
+int dmWritePNGImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec)
 {
     png_structp png_ptr = NULL;
     png_infop info_ptr = NULL;
@@ -483,7 +492,7 @@
 }
 
 
-int dmWritePNGImage(const char *filename, DMImage *img, DMImageSpec *spec)
+int dmWritePNGImage(const char *filename, DMImage *img, const DMImageSpec *spec)
 {
     int res;
     FILE *fp;
@@ -699,8 +708,12 @@
 typedef struct
 {
     Uint8 manufacturer,     // always 0x0a
-            version,        // Z-Soft PCX Paintbrush version:
-                            // 0 = v2.5, 2 = v2.8 with palette, 3 = v2.8 without palette, 5 = v3.0 or better
+            version,        // Z-Soft PC Paintbrush version:
+                            // 0 = v2.5
+                            // 2 = v2.8 with palette,
+                            // 3 = v2.8 without palette
+                            // 4 = PC Paintbrush for Windows
+                            // 5 = v3.0 or better
             encoding,       // usually 0x01 = RLE, 0x00 = uncompressed
             bitsPerPlane;   // bits per pixel per plane
 
@@ -809,7 +822,7 @@
 }
 
 
-int dmWritePCXImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec)
+int dmWritePCXImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec)
 {
     DMPCXData pcx;
     DMPCXHeader hdr;
@@ -823,7 +836,7 @@
 
     // Create PCX header
     dmMemset(&hdr, 0, sizeof(hdr));
-    if (spec->paletted)
+    if (spec->paletted && img->pal != NULL)
     {
         for (int i = 0; i < (img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors); i++)
         {
@@ -938,7 +951,7 @@
 }
 
 
-int dmWritePCXImage(const char *filename, DMImage *img, DMImageSpec *spec)
+int dmWritePCXImage(const char *filename, DMImage *img, const DMImageSpec *spec)
 {
     FILE *fp;
     int res;
@@ -1826,7 +1839,7 @@
 static int fmtProbePCX(const Uint8 *buf, const size_t len)
 {
     if (len > 128 + 32 &&
-        buf[0] == 10 &&
+
         (buf[1] == 5 || buf[1] == 2 || buf[1] == 3) &&
         buf[2] == 1 &&
         (buf[3] == 8 || buf[3] == 4 || buf[3] == 3 || buf[3] == 1) &&
--- a/src/libgfx.h	Fri Aug 18 15:27:36 2017 +0300
+++ b/src/libgfx.h	Fri Aug 18 17:32:19 2017 +0300
@@ -66,7 +66,9 @@
 
 typedef struct
 {
-    int scaleX, scaleY, nplanes, format;
+    int format;
+    int scaleX, scaleY;
+    int nplanes, bpp;
     BOOL interleave, paletted;
 } DMImageSpec;
 
@@ -78,8 +80,8 @@
     int  (*probe)(const Uint8 *buf, const size_t len);
     int  (*read)(const char *filename, DMImage **pimg);
     int  (*readFILE)(FILE *fp, DMImage **pimg);
-    int  (*write)(const char *filename, DMImage *pimg, DMImageSpec *spec);
-    int  (*writeFILE)(FILE *fp, DMImage *pimg, DMImageSpec *spec);
+    int  (*write)(const char *filename, DMImage *pimg, const DMImageSpec *spec);
+    int  (*writeFILE)(FILE *fp, DMImage *pimg, const DMImageSpec *spec);
 } DMImageFormat;
 
 
@@ -106,19 +108,19 @@
 int dmWriteImageData(DMImage *img, void *cbdata, int (*writeRowCB)(void *, Uint8 *, size_t), const DMImageSpec *spec);
 
 int dmWriteIFFMasterRAWPalette(FILE *fp, DMImage *img, int ncolors, const char *indent, const char *type);
-int dmWriteRAWImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec);
-int dmWriteRAWImage(const char *filename, DMImage *img, DMImageSpec *spec);
+int dmWriteRAWImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec);
+int dmWriteRAWImage(const char *filename, DMImage *img, const DMImageSpec *spec);
 
-int dmWritePPMImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec);
-int dmWritePPMImage(const char *filename, DMImage *img, DMImageSpec *spec);
+int dmWritePPMImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec);
+int dmWritePPMImage(const char *filename, DMImage *img, const DMImageSpec *spec);
 
 #ifdef DM_USE_LIBPNG
-int dmWritePNGImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec);
-int dmWritePNGImage(const char *filename, DMImage *img, DMImageSpec *spec);
+int dmWritePNGImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec);
+int dmWritePNGImage(const char *filename, DMImage *img, const DMImageSpec *spec);
 #endif
 
-int dmWritePCXImageFILE(FILE *fp, DMImage *img, DMImageSpec *spec);
-int dmWritePCXImage(const char *filename, DMImage *img, DMImageSpec *spec);
+int dmWritePCXImageFILE(FILE *fp, DMImage *img, const DMImageSpec *spec);
+int dmWritePCXImage(const char *filename, DMImage *img, const DMImageSpec *spec);
 int dmReadPCXImageFILE(FILE *fp, DMImage **pimg);
 int dmReadPCXImage(const char *filename, DMImage **pimg);
 
--- a/tools/gfxconv.c	Fri Aug 18 15:27:36 2017 +0300
+++ b/tools/gfxconv.c	Fri Aug 18 17:32:19 2017 +0300
@@ -145,6 +145,7 @@
     .scaleX = 1,
     .scaleY = 1,
     .nplanes = 4,
+    .bpp = 8,
     .interleave = FALSE,
     .paletted = FALSE,
     .format = 0,
@@ -167,7 +168,8 @@
     {  9, 'S', "scale",         "Scale output image by <n> or <x>:<y> integer factor(s). "
                                 "-S <n> scales both height and width by <n>.", OPT_ARGREQ },
     { 12, 'P', "paletted",      "Use indexed/paletted output IF possible.", OPT_NONE },
-    { 13, 'B', "bplanes",       "Bits per pixel OR # of bitplanes (certain output formats)", OPT_ARGREQ },
+    { 13, 'N', "nplanes",       "# of bitplanes (certain output formats)", OPT_ARGREQ },
+    { 18, 'B', "bpp",           "Bits per pixel (certain output formats)", OPT_ARGREQ },
     { 14, 'I', "interleave",    "Interleave output image scanlines (default: output whole planes)", OPT_NONE },
     { 16, 'R', "remap",         "Remap output image colors (-R <(#RRGGBB|index):index>[,<..>][+remove] | -R @map.txt[+remove])", OPT_ARGREQ },
 };
@@ -666,7 +668,19 @@
                 int tmp = atoi(optArg);
                 if (tmp < 1 || tmp > 8)
                 {
-                    dmErrorMsg("Invalid bitplanes/bpp value '%s'.\n", optArg);
+                    dmErrorMsg("Invalid number of bitplanes value '%s'.\n", optArg);
+                    return FALSE;
+                }
+                optSpec.nplanes = tmp;
+            }
+            break;
+
+        case 18:
+            {
+                int tmp = atoi(optArg);
+                if (tmp < 1 || tmp > 32)
+                {
+                    dmErrorMsg("Invalid number of bits per pixel value '%s'.\n", optArg);
                     return FALSE;
                 }
                 optSpec.nplanes = tmp;
@@ -1445,7 +1459,7 @@
                 goto error;
             }
 
-            outImage = dmImageAlloc(outWidthPX, outHeight);
+            outImage = dmImageAlloc(outWidthPX, outHeight, DM_IFMT_PALETTE, -1);
             dmMsg(1, "Outputting sequence of %d images @ %d x %d -> %d x %d.\n",
                 optItemCount,
                 outImage->width, outImage->height,
@@ -1465,7 +1479,7 @@
             if (optItemCount % optPlanedWidth)
                 outIHeight++;
 
-            outImage = dmImageAlloc(outWidthPX * outIWidth, outIHeight * outHeight);
+            outImage = dmImageAlloc(outWidthPX * outIWidth, outIHeight * outHeight, DM_IFMT_PALETTE, -1);
         }
 
         outImage->constpal = TRUE;