Skip to content

Commit fb9d818

Browse files
authored
fix(png): round dpi resolution to nearest 0.1 (#4347)
Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent e031dc3 commit fb9d818

3 files changed

Lines changed: 24 additions & 17 deletions

File tree

src/png.imageio/png_pvt.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,15 +303,22 @@ read_info(png_structp& sp, png_infop& ip, int& bit_depth, int& color_type,
303303
int unit;
304304
png_uint_32 resx, resy;
305305
if (png_get_pHYs(sp, ip, &resx, &resy, &unit)) {
306-
float scale = 1;
307306
if (unit == PNG_RESOLUTION_METER) {
308307
// Convert to inches, to match most other formats
309-
scale = 2.54 / 100.0;
308+
float scale = 2.54f / 100.0f;
309+
float rx = resx * scale;
310+
float ry = resy * scale;
311+
// Round to nearest 0.1
312+
rx = std::round(10.0f * rx) / 10.0f;
313+
ry = std::round(10.0f * ry) / 10.0f;
310314
spec.attribute("ResolutionUnit", "inch");
311-
} else
315+
spec.attribute("XResolution", rx);
316+
spec.attribute("YResolution", ry);
317+
} else {
312318
spec.attribute("ResolutionUnit", "none");
313-
spec.attribute("XResolution", (float)resx * scale);
314-
spec.attribute("YResolution", (float)resy * scale);
319+
spec.attribute("XResolution", (float)resx);
320+
spec.attribute("YResolution", (float)resy);
321+
}
315322
}
316323

317324
float aspect = (float)png_get_pixel_aspect_ratio(sp, ip);

testsuite/png/ref/out-libpng15.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Reading ../oiio-images/oiio-logo-no-alpha.png
55
Comment: "Created with GIMP"
66
DateTime: "2009:03:26 17:19:47"
77
ResolutionUnit: "inch"
8-
XResolution: 72.009
9-
YResolution: 72.009
8+
XResolution: 72
9+
YResolution: 72
1010
oiio:ColorSpace: "sRGB"
1111
Comparing "../oiio-images/oiio-logo-no-alpha.png" and "oiio-logo-no-alpha.png"
1212
PASS
@@ -17,8 +17,8 @@ Reading ../oiio-images/oiio-logo-with-alpha.png
1717
Comment: "Created with GIMP"
1818
DateTime: "2009:03:26 18:44:26"
1919
ResolutionUnit: "inch"
20-
XResolution: 72.009
21-
YResolution: 72.009
20+
XResolution: 72
21+
YResolution: 72
2222
oiio:ColorSpace: "sRGB"
2323
Comparing "../oiio-images/oiio-logo-with-alpha.png" and "oiio-logo-with-alpha.png"
2424
PASS
@@ -31,8 +31,8 @@ exif.png : 64 x 64, 3 channel, uint8 png
3131
channel list: R, G, B, A
3232
ResolutionUnit: "inch"
3333
Software: "OpenImageIO 2.4.1.1dev : oiiotool -no-autopremult SLEEP_MM.png -cut 1x1+227+1211 -o kaka.png"
34-
XResolution: 299.999
35-
YResolution: 299.999
34+
XResolution: 300
35+
YResolution: 300
3636
Exif:ImageHistory: "oiiotool -no-autopremult SLEEP_MM.png -cut 1x1+227+1211 -o kaka.png"
3737
oiio:ColorSpace: "Gamma2.2"
3838
oiio:Gamma: 2.2

testsuite/png/ref/out.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Reading ../oiio-images/oiio-logo-no-alpha.png
55
Comment: "Created with GIMP"
66
DateTime: "2009:03:26 17:19:47"
77
ResolutionUnit: "inch"
8-
XResolution: 72.009
9-
YResolution: 72.009
8+
XResolution: 72
9+
YResolution: 72
1010
oiio:ColorSpace: "sRGB"
1111
Comparing "../oiio-images/oiio-logo-no-alpha.png" and "oiio-logo-no-alpha.png"
1212
PASS
@@ -17,8 +17,8 @@ Reading ../oiio-images/oiio-logo-with-alpha.png
1717
Comment: "Created with GIMP"
1818
DateTime: "2009:03:26 18:44:26"
1919
ResolutionUnit: "inch"
20-
XResolution: 72.009
21-
YResolution: 72.009
20+
XResolution: 72
21+
YResolution: 72
2222
oiio:ColorSpace: "sRGB"
2323
Comparing "../oiio-images/oiio-logo-with-alpha.png" and "oiio-logo-with-alpha.png"
2424
PASS
@@ -35,8 +35,8 @@ exif.png : 64 x 64, 3 channel, uint8 png
3535
channel list: R, G, B, A
3636
ResolutionUnit: "inch"
3737
Software: "OpenImageIO 2.4.1.1dev : oiiotool -no-autopremult SLEEP_MM.png -cut 1x1+227+1211 -o kaka.png"
38-
XResolution: 299.999
39-
YResolution: 299.999
38+
XResolution: 300
39+
YResolution: 300
4040
Exif:ImageHistory: "oiiotool -no-autopremult SLEEP_MM.png -cut 1x1+227+1211 -o kaka.png"
4141
oiio:ColorSpace: "Gamma2.2"
4242
oiio:Gamma: 2.2

0 commit comments

Comments
 (0)