From fb9d818b1791be8d694f2e2a0d352b6c5c4462a6 Mon Sep 17 00:00:00 2001 From: Larry Gritz Date: Wed, 7 Aug 2024 11:53:52 -0700 Subject: [PATCH] fix(png): round dpi resolution to nearest 0.1 (#4347) Signed-off-by: Larry Gritz --- src/png.imageio/png_pvt.h | 17 ++++++++++++----- testsuite/png/ref/out-libpng15.txt | 12 ++++++------ testsuite/png/ref/out.txt | 12 ++++++------ 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/png.imageio/png_pvt.h b/src/png.imageio/png_pvt.h index 55ce5d62cf..9119e19f65 100644 --- a/src/png.imageio/png_pvt.h +++ b/src/png.imageio/png_pvt.h @@ -303,15 +303,22 @@ read_info(png_structp& sp, png_infop& ip, int& bit_depth, int& color_type, int unit; png_uint_32 resx, resy; if (png_get_pHYs(sp, ip, &resx, &resy, &unit)) { - float scale = 1; if (unit == PNG_RESOLUTION_METER) { // Convert to inches, to match most other formats - scale = 2.54 / 100.0; + float scale = 2.54f / 100.0f; + float rx = resx * scale; + float ry = resy * scale; + // Round to nearest 0.1 + rx = std::round(10.0f * rx) / 10.0f; + ry = std::round(10.0f * ry) / 10.0f; spec.attribute("ResolutionUnit", "inch"); - } else + spec.attribute("XResolution", rx); + spec.attribute("YResolution", ry); + } else { spec.attribute("ResolutionUnit", "none"); - spec.attribute("XResolution", (float)resx * scale); - spec.attribute("YResolution", (float)resy * scale); + spec.attribute("XResolution", (float)resx); + spec.attribute("YResolution", (float)resy); + } } float aspect = (float)png_get_pixel_aspect_ratio(sp, ip); diff --git a/testsuite/png/ref/out-libpng15.txt b/testsuite/png/ref/out-libpng15.txt index e8855cd272..f19f586729 100644 --- a/testsuite/png/ref/out-libpng15.txt +++ b/testsuite/png/ref/out-libpng15.txt @@ -5,8 +5,8 @@ Reading ../oiio-images/oiio-logo-no-alpha.png Comment: "Created with GIMP" DateTime: "2009:03:26 17:19:47" ResolutionUnit: "inch" - XResolution: 72.009 - YResolution: 72.009 + XResolution: 72 + YResolution: 72 oiio:ColorSpace: "sRGB" Comparing "../oiio-images/oiio-logo-no-alpha.png" and "oiio-logo-no-alpha.png" PASS @@ -17,8 +17,8 @@ Reading ../oiio-images/oiio-logo-with-alpha.png Comment: "Created with GIMP" DateTime: "2009:03:26 18:44:26" ResolutionUnit: "inch" - XResolution: 72.009 - YResolution: 72.009 + XResolution: 72 + YResolution: 72 oiio:ColorSpace: "sRGB" Comparing "../oiio-images/oiio-logo-with-alpha.png" and "oiio-logo-with-alpha.png" PASS @@ -31,8 +31,8 @@ exif.png : 64 x 64, 3 channel, uint8 png channel list: R, G, B, A ResolutionUnit: "inch" Software: "OpenImageIO 2.4.1.1dev : oiiotool -no-autopremult SLEEP_MM.png -cut 1x1+227+1211 -o kaka.png" - XResolution: 299.999 - YResolution: 299.999 + XResolution: 300 + YResolution: 300 Exif:ImageHistory: "oiiotool -no-autopremult SLEEP_MM.png -cut 1x1+227+1211 -o kaka.png" oiio:ColorSpace: "Gamma2.2" oiio:Gamma: 2.2 diff --git a/testsuite/png/ref/out.txt b/testsuite/png/ref/out.txt index 3a8ed2f5ab..730a5cdd0a 100644 --- a/testsuite/png/ref/out.txt +++ b/testsuite/png/ref/out.txt @@ -5,8 +5,8 @@ Reading ../oiio-images/oiio-logo-no-alpha.png Comment: "Created with GIMP" DateTime: "2009:03:26 17:19:47" ResolutionUnit: "inch" - XResolution: 72.009 - YResolution: 72.009 + XResolution: 72 + YResolution: 72 oiio:ColorSpace: "sRGB" Comparing "../oiio-images/oiio-logo-no-alpha.png" and "oiio-logo-no-alpha.png" PASS @@ -17,8 +17,8 @@ Reading ../oiio-images/oiio-logo-with-alpha.png Comment: "Created with GIMP" DateTime: "2009:03:26 18:44:26" ResolutionUnit: "inch" - XResolution: 72.009 - YResolution: 72.009 + XResolution: 72 + YResolution: 72 oiio:ColorSpace: "sRGB" Comparing "../oiio-images/oiio-logo-with-alpha.png" and "oiio-logo-with-alpha.png" PASS @@ -35,8 +35,8 @@ exif.png : 64 x 64, 3 channel, uint8 png channel list: R, G, B, A ResolutionUnit: "inch" Software: "OpenImageIO 2.4.1.1dev : oiiotool -no-autopremult SLEEP_MM.png -cut 1x1+227+1211 -o kaka.png" - XResolution: 299.999 - YResolution: 299.999 + XResolution: 300 + YResolution: 300 Exif:ImageHistory: "oiiotool -no-autopremult SLEEP_MM.png -cut 1x1+227+1211 -o kaka.png" oiio:ColorSpace: "Gamma2.2" oiio:Gamma: 2.2