changeset 902:c6c480e8e1c8

Add separate X and Y scaling to gfxconv and libgfx outputters.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 23 Feb 2015 20:45:31 +0200
parents f532262f90b1
children f29a6164ec80
files src/libgfx.c src/libgfx.h tools/gfxconv.c
diffstat 3 files changed, 45 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/src/libgfx.c	Mon Feb 23 20:18:34 2015 +0200
+++ b/src/libgfx.c	Mon Feb 23 20:45:31 2015 +0200
@@ -134,7 +134,7 @@
     Uint8 *row = NULL;
 
     // Allocate memory for row buffer
-    rowWidth = img->width * spec->scale;
+    rowWidth = img->width * spec->scaleX;
     rowSize = rowWidth * dmImageGetBytesPerPixel(spec->format);
 
     if ((row = dmMalloc(rowSize + 16)) == NULL)
@@ -156,7 +156,7 @@
             switch (spec->format)
             {
                 case DM_IFMT_PALETTE:
-                    for (xscale = 0; xscale < spec->scale; xscale++)
+                    for (xscale = 0; xscale < spec->scaleX; xscale++)
                         *ptr1++ = c;
                     break;
 
@@ -166,7 +166,7 @@
                     qb = img->pal[c].b;
                     qa = img->pal[c].a;
                 
-                    for (xscale = 0; xscale < spec->scale; xscale++)
+                    for (xscale = 0; xscale < spec->scaleX; xscale++)
                     {
                         *ptr1++ = qr;
                         *ptr1++ = qg;
@@ -180,7 +180,7 @@
                     qg = img->pal[c].g;
                     qb = img->pal[c].b;
                 
-                    for (xscale = 0; xscale < spec->scale; xscale++)
+                    for (xscale = 0; xscale < spec->scaleX; xscale++)
                     {
                         *ptr1++ = qr;
                         *ptr1++ = qg;
@@ -193,7 +193,7 @@
                     qg = img->pal[c].g;
                     qb = img->pal[c].b;
                 
-                    for (xscale = 0; xscale < spec->scale; xscale++)
+                    for (xscale = 0; xscale < spec->scaleX; xscale++)
                     {
                         *ptr1++ = qr;
                         *ptr2++ = qg;
@@ -203,7 +203,7 @@
             }
         }
 
-        for (yscale = 0; yscale < spec->scale; yscale++)
+        for (yscale = 0; yscale < spec->scaleY; yscale++)
         {
             if ((res = writeRowCB(cbdata, row, rowSize)) != DMERR_OK)
                 goto done;
@@ -322,8 +322,8 @@
     // Write PPM header
     fprintf(fp,
         "P6\n%d %d\n255\n",
-        img->width * spec->scale,
-        img->height * spec->scale);
+        img->width * spec->scaleX,
+        img->height * spec->scaleY);
 
     // Write image data
     spec->format = DM_IFMT_RGB;
@@ -413,8 +413,8 @@
     }
  
     png_set_IHDR(png_ptr, info_ptr,
-        img->width * spec->scale,
-        img->height * spec->scale,
+        img->width * spec->scaleX,
+        img->height * spec->scaleY,
         8,                    /* bits per component */
         fmt,
         PNG_INTERLACE_NONE,
@@ -422,8 +422,8 @@
         PNG_FILTER_TYPE_DEFAULT);
 
     dmMsg(3, "PNG: %d x %d, depth=%d, type=%d\n",
-        img->width * spec->scale,
-        img->height * spec->scale,
+        img->width * spec->scaleX,
+        img->height * spec->scaleY,
         8, fmt);
 
     // Palette
@@ -820,15 +820,15 @@
     hdr.version      = 5;
     hdr.encoding     = 1;
     hdr.bpp          = 8;
-    hdr.hres         = img->width * spec->scale;
-    hdr.vres         = img->height * spec->scale;
+    hdr.hres         = img->width * spec->scaleX;
+    hdr.vres         = img->height * spec->scaleY;
     hdr.xmin         = hdr.ymin = 0;
     hdr.xmax         = hdr.hres - 1;
     hdr.ymax         = hdr.vres - 1;
     hdr.nplanes      = dmImageGetBytesPerPixel(pcx.format);
     hdr.palinfo      = 1;
 
-    res = (img->width * spec->scale);
+    res = (img->width * spec->scaleX);
     hdr.bpl = res / 2;
     if (res % 2) hdr.bpl++;
     hdr.bpl *= 2;
--- a/src/libgfx.h	Mon Feb 23 20:18:34 2015 +0200
+++ b/src/libgfx.h	Mon Feb 23 20:45:31 2015 +0200
@@ -58,7 +58,7 @@
 
 typedef struct
 {
-    int scale, nplanes, format;
+    int scaleX, scaleY, nplanes, format;
     BOOL interleave, paletted;
 } DMImageSpec;
 
--- a/tools/gfxconv.c	Mon Feb 23 20:18:34 2015 +0200
+++ b/tools/gfxconv.c	Mon Feb 23 20:45:31 2015 +0200
@@ -142,7 +142,8 @@
 
 DMImageSpec optSpec =
 {
-    .scale = 1,
+    .scaleX = 1,
+    .scaleY = 1,
     .nplanes = 4,
     .interleave = FALSE,
     .paletted = FALSE,
@@ -620,26 +621,38 @@
             break;
 
         case 9:
+            if (sscanf(optArg, "%d:%d", &optSpec.scaleX, &optSpec.scaleY) != 2)
             {
-                int tmp = atoi(optArg);
-                if (tmp < 1 || tmp > 50)
+                if (sscanf(optArg, "%d", &optSpec.scaleX) == 1)
+                    optSpec.scaleY = optSpec.scaleX;
+                else
                 {
-                    dmError("Invalid scale value '%s'.\n", optArg);
+                    dmError("Invalid scale option value '%s', should be <n> or <w>:<h>.\n", optArg);
                     return FALSE;
                 }
-                optSpec.scale = tmp;
+            }
+            if (optSpec.scaleX < 1 || optSpec.scaleX > 50)
+            {
+                dmError("Invalid X scale value '%d'.\n", optSpec.scaleX);
+                return FALSE;
+            }
+            if (optSpec.scaleY < 1 || optSpec.scaleY > 50)
+            {
+                dmError("Invalid Y scale value '%d'.\n", optSpec.scaleY);
+                return FALSE;
             }
             break;
 
         case 11:
+            if (sscanf(optArg, "%d", &optPlanedWidth) != 1)
             {
-                int tmp = atoi(optArg);
-                if (tmp < 1 || tmp > 512)
-                {
-                    dmError("Invalid width value '%s'.\n", optArg);
-                    return FALSE;
-                }
-                optPlanedWidth = tmp;
+                dmError("Invalid planed width value argument '%s'.\n", optArg);
+                return FALSE;
+            }
+            if (optPlanedWidth < 1 || optPlanedWidth > 512)
+            {
+                dmError("Invalid planed width value '%d' [1..512].\n", optPlanedWidth);
+                return FALSE;
             }
             break;
 
@@ -1052,11 +1065,11 @@
 {
     if (info)
     {
-        dmMsg(1, "Outputting %s image %d x %d -> %d x %d [%d]\n",
+        dmMsg(1, "Outputting %s image %d x %d -> %d x %d [%d x %d]\n",
             dmImageFormatList[iformat].fext,
             image->width, image->height,
-            image->width * spec->scale, image->height * spec->scale,
-            spec->scale);
+            image->width * spec->scaleX, image->height * spec->scaleY,
+            spec->scaleX, spec->scaleY);
     }
 
     // Perform color remapping
@@ -1425,7 +1438,7 @@
             dmMsg(1, "Outputting sequence of %d images @ %d x %d -> %d x %d.\n",
                 optItemCount,
                 outImage->width, outImage->height,
-                outImage->width * optSpec.scale, outImage->height * optSpec.scale);
+                outImage->width * optSpec.scaleX, outImage->height * optSpec.scaleY);
         }
         else
         {