Skip to content

Commit 35795cb

Browse files
committed
fix(ImageSpec): ImageSpec::get_string_attribute didn't correctly translate to string (AcademySoftwareFoundation#5161)
This method is advertised as being able to convert any type of the attribute into a string, but actually, it would only *search* for attributes that were entered as strings, so there was no conversion happening. Beef up testing of paramlist and imagespec to cover the holes that let this mistake happen. Fixed a couple typos in imageio.h comments as well. Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent 4ca49d1 commit 35795cb

4 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/include/OpenImageIO/imageio.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ class OIIO_API ImageSpec {
616616
/// Retrieve the named metadata attribute and return its value as an
617617
/// `int`. Any integer type will convert to `int` by truncation or
618618
/// expansion, string data will parsed into an `int` if its contents
619-
/// consist of of the text representation of one integer. Floating point
619+
/// consist of the text representation of one integer. Floating point
620620
/// data will not succeed in converting to an `int`. If no such metadata
621621
/// exists, or are of a type that cannot be converted, the `defaultval`
622622
/// will be returned.
@@ -625,7 +625,7 @@ class OIIO_API ImageSpec {
625625
/// Retrieve the named metadata attribute and return its value as a
626626
/// `float`. Any integer or floating point type will convert to `float`
627627
/// in the obvious way (like a C cast), and so will string metadata if
628-
/// its contents consist of of the text representation of one floating
628+
/// its contents consist of the text representation of one floating
629629
/// point value. If no such metadata exists, or are of a type that cannot
630630
/// be converted, the `defaultval` will be returned.
631631
float get_float_attribute (string_view name, float defaultval=0) const;

src/libOpenImageIO/formatspec.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ string_view
579579
ImageSpec::get_string_attribute(string_view name, string_view defaultval) const
580580
{
581581
ParamValue tmpparam;
582-
const ParamValue* p = find_attribute(name, tmpparam, TypeDesc::STRING);
582+
const ParamValue* p = find_attribute(name, tmpparam);
583583
return p ? p->get_ustring() : defaultval;
584584
}
585585

src/libOpenImageIO/imagespec_test.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ test_get_attribute()
236236
spec.attribute("pi", float(M_PI));
237237
spec.attribute("bar", "barbarbar?");
238238
spec["baz"] = (unsigned int)14;
239+
spec.attribute("foostring", "42");
240+
spec.attribute("pistring", Strutil::to_string(float(M_PI)));
239241

240242
OIIO_CHECK_EQUAL(spec.get_int_attribute("width"), 640);
241243
OIIO_CHECK_EQUAL(spec.get_int_attribute("height"), 480);
@@ -259,8 +261,13 @@ test_get_attribute()
259261
OIIO_CHECK_EQUAL(spec.get_int_attribute("foo"), 42);
260262
OIIO_CHECK_EQUAL(spec.get_int_attribute("pi", 4), 4); // should fail int
261263
OIIO_CHECK_EQUAL(spec.get_float_attribute("pi"), float(M_PI));
264+
OIIO_CHECK_EQUAL(spec.get_float_attribute("foo"), 42.0f);
265+
OIIO_CHECK_EQUAL(spec.get_float_attribute("pistring"),
266+
spec.get_float_attribute("pi"));
262267
OIIO_CHECK_EQUAL(spec.get_int_attribute("bar"), 0);
263-
OIIO_CHECK_EQUAL(spec.get_int_attribute("bar"), 0);
268+
OIIO_CHECK_EQUAL(spec.get_int_attribute("foostring"), 42);
269+
OIIO_CHECK_EQUAL(spec.get_string_attribute("foostring"), "42");
270+
OIIO_CHECK_EQUAL(spec.get_string_attribute("foo"), "42");
264271
OIIO_CHECK_EQUAL(spec.get_string_attribute("bar"), "barbarbar?");
265272
OIIO_CHECK_ASSERT(spec.find_attribute("foo") != NULL);
266273
OIIO_CHECK_ASSERT(spec.find_attribute("Foo") != NULL);

src/libutil/paramlist_test.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,11 @@ test_from_string()
276276
void
277277
populate_pvl(ParamValueList& pl)
278278
{
279-
pl["foo"] = 42;
280-
pl["pi"] = float(M_PI);
279+
pl["foo"] = 42;
280+
pl["foostring"] = "42";
281+
pl["pi"] = float(M_PI);
282+
pl["pistring"] = Strutil::to_string(float(M_PI));
283+
281284
pl["bar"] = "barbarbar?";
282285
pl["bar2"] = std::string("barbarbar?");
283286
pl["bar3"] = ustring("barbarbar?");
@@ -299,12 +302,17 @@ test_paramlist()
299302
print("ParamValueList pl footprint is: {}\n", pvt::footprint(pl));
300303

301304
OIIO_CHECK_EQUAL(pl.get_int("foo"), 42);
305+
OIIO_CHECK_EQUAL(pl.get_int("foostring"), 42);
306+
OIIO_CHECK_EQUAL(pl.get_int("foostring", -12, false, false), -12);
302307
OIIO_CHECK_EQUAL(pl.get_int("pi", 4), 4); // should fail int
303308
OIIO_CHECK_EQUAL(pl.get_float("pi"), float(M_PI));
309+
OIIO_CHECK_EQUAL(pl.get_float("pistring"), float(M_PI));
310+
OIIO_CHECK_EQUAL(pl.get_float("pistring", 3.0f, false, false), 3.0f);
304311
OIIO_CHECK_EQUAL(pl.get_int("bar"), 0);
305312
OIIO_CHECK_EQUAL(pl.get_int("bar"), 0);
306313
OIIO_CHECK_EQUAL(pl.get_string("bar"), "barbarbar?");
307314
OIIO_CHECK_EQUAL(pl.get_string("foo"), "42");
315+
OIIO_CHECK_EQUAL(pl.get_string("foo", "fail", false, false), "fail");
308316
OIIO_CHECK_ASSERT(pl.find("foo") != pl.cend());
309317
OIIO_CHECK_ASSERT(pl.find("Foo") == pl.cend());
310318
OIIO_CHECK_ASSERT(pl.find("Foo", TypeDesc::UNKNOWN, false) != pl.cend());

0 commit comments

Comments
 (0)