Skip to content

Commit

Permalink
fix(png): round dpi resolution to nearest 0.1 (#4347)
Browse files Browse the repository at this point in the history
Signed-off-by: Larry Gritz <[email protected]>
  • Loading branch information
lgritz committed Aug 7, 2024
1 parent e031dc3 commit fb9d818
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
17 changes: 12 additions & 5 deletions src/png.imageio/png_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions testsuite/png/ref/out-libpng15.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions testsuite/png/ref/out.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit fb9d818

Please sign in to comment.