changeset 1628:a549d33d543a

Add image aspect ratio information.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 16 May 2018 15:42:24 +0300
parents d0e626e039bf
children f31b77416a83
files tools/libgfx.c tools/libgfx.h
diffstat 2 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/tools/libgfx.c	Wed May 16 15:15:39 2018 +0300
+++ b/tools/libgfx.c	Wed May 16 15:42:24 2018 +0300
@@ -118,8 +118,9 @@
     img->format  = format;
     img->bpp     = (bpp <= 0) ? dmImageGetBytesPerPixel(format) * 8 : bpp;
     img->pitch   = width * img->bpp;
-    img->size = img->pitch * img->height;
+    img->size    = img->pitch * img->height;
     img->ctransp = -1;
+    img->aspect  = -1;
 
     if ((img->data = dmMalloc(img->size)) == NULL)
     {
@@ -599,8 +600,8 @@
     png_colorp palette = NULL;
     png_bytep *row_pointers = NULL;
     png_bytep trans = NULL;
-    png_uint_32 width, height;
-    int i, bit_depth, color_type, ncolors, ntrans;
+    png_uint_32 width, height, res_x, res_y;
+    int i, bit_depth, color_type, ncolors, ntrans, unit_type;
     int res = DMERR_OK;
     DMImage *img;
 
@@ -687,6 +688,13 @@
         goto error;
     }
 
+    // Set image aspect ratio
+    png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y, &unit_type);
+
+    if (res_x > 0 && res_y > 0 &&
+        res_y / res_x != (unsigned) (img->height / img->width))
+        img->aspect = (float) res_y / (float) res_x;
+
     // ...
     row_pointers = png_malloc(png_ptr, height * sizeof(png_bytep));
     for (i = 0; i < img->height; i++)
@@ -1254,6 +1262,11 @@
         goto error;
     }
 
+    // Set image aspect ratio
+    if (hdr.hScreenSize > 0 && hdr.vScreenSize > 0 &&
+        hdr.vScreenSize / hdr.hScreenSize != img->height / img->width)
+        img->aspect = (float) hdr.vScreenSize / (float) hdr.hScreenSize;
+
     // Sanity check bytes per line value
     if (hdr.bpl < (img->width * hdr.bitsPerPlane) / 8)
     {
@@ -1860,6 +1873,9 @@
                     )) == NULL)
                     return DMERR_MALLOC;
 
+                // Set image aspect ratio
+                if (iff.bmhd.xasp > 0 && iff.bmhd.yasp > 0)
+                    (*pimg)->aspect = (float) iff.bmhd.yasp / (float) iff.bmhd.xasp;
 
                 // Decode the body
                 if (iff.planar)
--- a/tools/libgfx.h	Wed May 16 15:15:39 2018 +0300
+++ b/tools/libgfx.h	Wed May 16 15:42:24 2018 +0300
@@ -73,6 +73,8 @@
     int pitch;      // bytes per scanline
     int bpp;        // bits per pixel
 
+    float aspect;   // aspect ratio (vert / horiz), <= 0 if not set
+
     int ncolors;    // number of colors in palette, if any
     int ctransp;    // transparency color index
     BOOL constpal;  // is the palette a const?