Skip to content

Commit caa5174

Browse files
authored
fix(ImageSpec): ImageSpec::get_string_attribute didn't correctly translate to string (#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 0e43845 commit caa5174

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
@@ -721,7 +721,7 @@ class OIIO_API ImageSpec {
721721
/// Retrieve the named metadata attribute and return its value as an
722722
/// `int`. Any integer type will convert to `int` by truncation or
723723
/// expansion, string data will parsed into an `int` if its contents
724-
/// consist of of the text representation of one integer. Floating point
724+
/// consist of the text representation of one integer. Floating point
725725
/// data will not succeed in converting to an `int`. If no such metadata
726726
/// exists, or are of a type that cannot be converted, the `defaultval`
727727
/// will be returned.
@@ -730,7 +730,7 @@ class OIIO_API ImageSpec {
730730
/// Retrieve the named metadata attribute and return its value as a
731731
/// `float`. Any integer or floating point type will convert to `float`
732732
/// in the obvious way (like a C cast), and so will string metadata if
733-
/// its contents consist of of the text representation of one floating
733+
/// its contents consist of the text representation of one floating
734734
/// point value. If no such metadata exists, or are of a type that cannot
735735
/// be converted, the `defaultval` will be returned.
736736
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
@@ -646,7 +646,7 @@ string_view
646646
ImageSpec::get_string_attribute(string_view name, string_view defaultval) const
647647
{
648648
ParamValue tmpparam;
649-
const ParamValue* p = find_attribute(name, tmpparam, TypeDesc::STRING);
649+
const ParamValue* p = find_attribute(name, tmpparam);
650650
return p ? p->get_ustring() : defaultval;
651651
}
652652

src/libOpenImageIO/imagespec_test.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ test_get_attribute()
259259
spec.attribute("pi", float(M_PI));
260260
spec.attribute("bar", "barbarbar?");
261261
spec["baz"] = (unsigned int)14;
262+
spec.attribute("foostring", "42");
263+
spec.attribute("pistring", Strutil::to_string(float(M_PI)));
262264

263265
OIIO_CHECK_EQUAL(spec.get_int_attribute("width"), 640);
264266
OIIO_CHECK_EQUAL(spec.get_int_attribute("height"), 480);
@@ -282,8 +284,13 @@ test_get_attribute()
282284
OIIO_CHECK_EQUAL(spec.get_int_attribute("foo"), 42);
283285
OIIO_CHECK_EQUAL(spec.get_int_attribute("pi", 4), 4); // should fail int
284286
OIIO_CHECK_EQUAL(spec.get_float_attribute("pi"), float(M_PI));
287+
OIIO_CHECK_EQUAL(spec.get_float_attribute("foo"), 42.0f);
288+
OIIO_CHECK_EQUAL(spec.get_float_attribute("pistring"),
289+
spec.get_float_attribute("pi"));
285290
OIIO_CHECK_EQUAL(spec.get_int_attribute("bar"), 0);
286-
OIIO_CHECK_EQUAL(spec.get_int_attribute("bar"), 0);
291+
OIIO_CHECK_EQUAL(spec.get_int_attribute("foostring"), 42);
292+
OIIO_CHECK_EQUAL(spec.get_string_attribute("foostring"), "42");
293+
OIIO_CHECK_EQUAL(spec.get_string_attribute("foo"), "42");
287294
OIIO_CHECK_EQUAL(spec.get_string_attribute("bar"), "barbarbar?");
288295
OIIO_CHECK_ASSERT(spec.find_attribute("foo") != NULL);
289296
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
@@ -284,8 +284,11 @@ test_from_string()
284284
void
285285
populate_pvl(ParamValueList& pl)
286286
{
287-
pl["foo"] = 42;
288-
pl["pi"] = float(M_PI);
287+
pl["foo"] = 42;
288+
pl["foostring"] = "42";
289+
pl["pi"] = float(M_PI);
290+
pl["pistring"] = Strutil::to_string(float(M_PI));
291+
289292
pl["bar"] = "barbarbar?";
290293
pl["bar2"] = std::string("barbarbar?");
291294
pl["bar3"] = ustring("barbarbar?");
@@ -307,12 +310,17 @@ test_paramlist()
307310
print("ParamValueList pl footprint is: {}\n", pvt::footprint(pl));
308311

309312
OIIO_CHECK_EQUAL(pl.get_int("foo"), 42);
313+
OIIO_CHECK_EQUAL(pl.get_int("foostring"), 42);
314+
OIIO_CHECK_EQUAL(pl.get_int("foostring", -12, false, false), -12);
310315
OIIO_CHECK_EQUAL(pl.get_int("pi", 4), 4); // should fail int
311316
OIIO_CHECK_EQUAL(pl.get_float("pi"), float(M_PI));
317+
OIIO_CHECK_EQUAL(pl.get_float("pistring"), float(M_PI));
318+
OIIO_CHECK_EQUAL(pl.get_float("pistring", 3.0f, false, false), 3.0f);
312319
OIIO_CHECK_EQUAL(pl.get_int("bar"), 0);
313320
OIIO_CHECK_EQUAL(pl.get_int("bar"), 0);
314321
OIIO_CHECK_EQUAL(pl.get_string("bar"), "barbarbar?");
315322
OIIO_CHECK_EQUAL(pl.get_string("foo"), "42");
323+
OIIO_CHECK_EQUAL(pl.get_string("foo", "fail", false, false), "fail");
316324
OIIO_CHECK_ASSERT(pl.find("foo") != pl.cend());
317325
OIIO_CHECK_ASSERT(pl.find("Foo") == pl.cend());
318326
OIIO_CHECK_ASSERT(pl.find("Foo", TypeDesc::UNKNOWN, false) != pl.cend());

0 commit comments

Comments
 (0)