Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(png): round dpi resolution to nearest 0.1 #4347

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/png.imageio/png_pvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,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
Loading