changeset 2093:d17512dbb4ef

Some work on reading >8bpp images.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 28 Feb 2019 12:32:07 +0200
parents 614b161c0aa5
children 4276b8c0fef0
files tools/libgfx.c tools/libgfx.h
diffstat 2 files changed, 40 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/tools/libgfx.c	Thu Feb 28 12:30:21 2019 +0200
+++ b/tools/libgfx.c	Thu Feb 28 12:32:07 2019 +0200
@@ -130,8 +130,8 @@
     img->width   = width;
     img->height  = height;
     img->format  = format;
-    img->bpp     = (bpp <= 0) ? dmImageGetBytesPerPixel(format) * 8 : bpp;
-    img->pitch   = width * img->bpp;
+    img->bpp     = (bpp <= 0) ? dmImageGetBitsPerPixel(format) : bpp;
+    img->pitch   = (width * img->bpp) / 8;
     img->size    = img->pitch * img->height;
     img->ctransp = -1;
     img->aspect  = -1;
@@ -714,9 +714,10 @@
     // Write PNG header info
     switch (spec->format)
     {
-        case DM_COLFMT_PALETTE: fmt = PNG_COLOR_TYPE_PALETTE; break;
-        case DM_COLFMT_RGB    : fmt = PNG_COLOR_TYPE_RGB; break;
-        case DM_COLFMT_RGBA   : fmt = PNG_COLOR_TYPE_RGB_ALPHA; break;
+        case DM_COLFMT_PALETTE   : fmt = PNG_COLOR_TYPE_PALETTE; break;
+        case DM_COLFMT_GRAYSCALE : fmt = PNG_COLOR_TYPE_GRAY; break;
+        case DM_COLFMT_RGB       : fmt = PNG_COLOR_TYPE_RGB; break;
+        case DM_COLFMT_RGBA      : fmt = PNG_COLOR_TYPE_RGB_ALPHA; break;
         default:
             res = dmError(DMERR_NOT_SUPPORTED,
                 "PNG: Unsupported image format %d.\n",
@@ -801,7 +802,7 @@
     png_bytep *row_pointers = NULL;
     png_bytep trans = NULL;
     png_uint_32 width, height, res_x = 0, res_y = 0;
-    int i, bit_depth, color_type, ncolors, ntrans, unit_type;
+    int i, itype, bit_depth, color_type, ncolors, ntrans, unit_type;
     int res = DMERR_OK;
     DMImage *img;
 
@@ -852,9 +853,6 @@
     switch (color_type)
     {
         case PNG_COLOR_TYPE_GRAY:
-            if (bit_depth < 8)
-                png_set_expand_gray_1_2_4_to_8(png_ptr);
-
             if (bit_depth > 8)
             {
                 res = dmError(DMERR_NOT_SUPPORTED,
@@ -862,10 +860,24 @@
                     bit_depth);
                 goto error;
             }
+
+            if (bit_depth < 8)
+                png_set_expand_gray_1_2_4_to_8(png_ptr);
+
+            itype = DM_COLFMT_GRAYSCALE;
             break;
 
         case PNG_COLOR_TYPE_PALETTE:
             png_set_packing(png_ptr);
+            itype = DM_COLFMT_PALETTE;
+            break;
+
+        case PNG_COLOR_TYPE_RGB:
+            itype = DM_COLFMT_RGB;
+            break;
+
+        case PNG_COLOR_TYPE_RGBA:
+            itype = DM_COLFMT_RGBA;
             break;
 
         default:
@@ -879,7 +891,7 @@
         width, height, bit_depth, color_type);
 
     if ((*pimg = img = dmImageAlloc(width, height,
-        DM_COLFMT_PALETTE,
+        itype,
         // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp
         -1 /* bit_depth */)) == NULL)
     {
@@ -908,11 +920,12 @@
     png_free(png_ptr, row_pointers);
 
     // Create palette
-    switch (color_type)
+    if (color_type == PNG_COLOR_TYPE_PALETTE)
     {
-        case PNG_COLOR_TYPE_GRAY:
-            ncolors = 256;
-            dmMsg(2, "PNG: Generating %d color grayscale palette.\n", ncolors);
+        png_get_PLTE(png_ptr, info_ptr, &palette, &ncolors);
+        if (ncolors > 0 && palette != NULL)
+        {
+            dmMsg(2, "PNG: Palette of %d colors found.\n", ncolors);
 
             if (!dmImagePaletteAlloc(img, ncolors, -1))
             {
@@ -922,34 +935,12 @@
 
             for (i = 0; i < img->ncolors; i++)
             {
-                img->pal[i].r = img->pal[i].g = img->pal[i].b = i;
+                img->pal[i].r = palette[i].red;
+                img->pal[i].g = palette[i].green;
+                img->pal[i].b = palette[i].blue;
             }
-            break;
-
-        case PNG_COLOR_TYPE_PALETTE:
-            png_get_PLTE(png_ptr, info_ptr, &palette, &ncolors);
-            dmMsg(2, "PNG: Palette of %d colors found.\n", ncolors);
-            if (ncolors > 0 && palette != NULL)
-            {
-                if (!dmImagePaletteAlloc(img, ncolors, -1))
-                {
-                    res = DMERR_MALLOC;
-                    goto error;
-                }
-
-                for (i = 0; i < img->ncolors; i++)
-                {
-                    img->pal[i].r = palette[i].red;
-                    img->pal[i].g = palette[i].green;
-                    img->pal[i].b = palette[i].blue;
-                }
-            }
-            break;
-    }
-
-    if (color_type == PNG_COLOR_TYPE_PALETTE ||
-        color_type == PNG_COLOR_TYPE_GRAY)
-    {
+        }
+
         png_get_tRNS(png_ptr, info_ptr, &trans, &ntrans, NULL);
         if (trans != NULL && ntrans > 0)
         {
@@ -1131,7 +1122,8 @@
     spec.planar = TRUE;
 
     // XXX: 24bit PCX does not work yet ..
-    if (spec.format != DM_COLFMT_PALETTE)
+    if (spec.format != DM_COLFMT_PALETTE &&
+        spec.format != DM_COLFMT_GRAYSCALE)
     {
         return dmError(DMERR_NOT_SUPPORTED,
             "24bit PCX not supported yet.\n");
@@ -1150,7 +1142,8 @@
 
     // Create PCX header
     dmMemset(&hdr, 0, sizeof(hdr));
-    if (spec.format == DM_COLFMT_PALETTE)
+    if (spec.format == DM_COLFMT_PALETTE ||
+        spec.format == DM_COLFMT_GRAYSCALE)
     {
         const int ncolors = img->ncolors > DMPCX_PAL_COLORS ? DMPCX_PAL_COLORS : img->ncolors;
         for (int i = 0; i < ncolors; i++)
@@ -1250,7 +1243,8 @@
     res = dmWriteImageData(img, (void *) &pcx, dmWritePCXRow, &spec);
 
     // Write VGA palette
-    if (spec.format == DM_COLFMT_PALETTE)
+    if (spec.format == DM_COLFMT_PALETTE ||
+        spec.format == DM_COLFMT_GRAYSCALE)
     {
         int i;
         dmMsg(2, "PCX: Writing palette of %d active entries.\n", img->ncolors);
@@ -1426,7 +1420,7 @@
 
     // Allocate image
     if ((*pimg = img = dmImageAlloc(hdr.xmax - hdr.xmin + 1, hdr.ymax - hdr.ymin + 1,
-        isPaletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGBA,
+        isPaletted ? DM_COLFMT_PALETTE : DM_COLFMT_RGB,
         // XXX TODO? When/if we ever handle < 8bit indexed correctly, we can use the actual bpp
         // isPaletted ? (hdr.bitsPerPlane * hdr.nplanes) : -1
         -1
--- a/tools/libgfx.h	Thu Feb 28 12:30:21 2019 +0200
+++ b/tools/libgfx.h	Thu Feb 28 12:32:07 2019 +0200
@@ -121,6 +121,7 @@
 DMImage * dmImageAlloc(const int width, const int height, const int format, const int bpp);
 void      dmImageFree(DMImage *img);
 int       dmImageGetBytesPerPixel(const int format);
+int       dmImageGetBitsPerPixel(const int format);
 int       dmImageProbeGeneric(const Uint8 *buf, const size_t len, const DMImageFormat **fmt, int *index);
 
 BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, BOOL alpha);