Skip to content

Commit 0c93039

Browse files
committed
Address review feedback: version-gated NODISCARD_ERROR, fix behavioral changes
- platform.h: Replace simple no-op macro with version-gated OIIO_NODISCARD_ERROR_ENABLE that defaults to disabled for OIIO < 3.3 and enabled for >= 3.3, overridable via -D flag - py_imagebuf.cpp: Restore returning empty py::bytes() instead of py::none() on open failure to preserve backward compatibility - jpegoutput.cpp: Restructure open/close flow to guarantee close() is always called even when open() fails, matching previous behavior - imageio.h: Apply clang-format to NODISCARD_ERROR-annotated functions Signed-off-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
1 parent 5b166ef commit 0c93039

File tree

4 files changed

+46
-46
lines changed

4 files changed

+46
-46
lines changed

src/include/OpenImageIO/imageio.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,17 +1415,17 @@ class OIIO_API ImageInput {
14151415
/// y, and z).
14161416
/// @returns `true` upon success, or `false` upon failure.
14171417
///
1418-
OIIO_NODISCARD_ERROR virtual bool read_image(int subimage, int miplevel,
1419-
int chbegin, int chend,
1420-
TypeDesc format,
1421-
const image_span<std::byte>& data);
1418+
OIIO_NODISCARD_ERROR virtual bool
1419+
read_image(int subimage, int miplevel, int chbegin, int chend,
1420+
TypeDesc format, const image_span<std::byte>& data);
14221421

14231422
/// A version of `read_image()` taking an `image_span<T>`, where the type
14241423
/// of the underlying data is `T`. This is a convenience wrapper around
14251424
/// the `read_image()` that takes an `image_span<std::byte>`.
14261425
template<typename T>
1427-
OIIO_NODISCARD_ERROR bool read_image(int subimage, int miplevel, int chbegin,
1428-
int chend, const image_span<T>& data)
1426+
OIIO_NODISCARD_ERROR bool read_image(int subimage, int miplevel,
1427+
int chbegin, int chend,
1428+
const image_span<T>& data)
14291429
{
14301430
static_assert(!std::is_const_v<T>,
14311431
"read_image() does not accept image_span<const T>");
@@ -1438,8 +1438,8 @@ class OIIO_API ImageInput {
14381438
/// contiguous strides in all dimensions. This is a convenience wrapper
14391439
/// around the `read_image()` that takes an `image_span<T>`.
14401440
template<typename T>
1441-
OIIO_NODISCARD_ERROR bool read_image(int subimage, int miplevel, int chbegin,
1442-
int chend, span<T> data)
1441+
OIIO_NODISCARD_ERROR bool read_image(int subimage, int miplevel,
1442+
int chbegin, int chend, span<T> data)
14431443
{
14441444
static_assert(!std::is_const_v<T>,
14451445
"read_image() does not accept span<const T>");
@@ -1486,19 +1486,18 @@ class OIIO_API ImageInput {
14861486
/// Added in OIIO 3.1, this is the "safe" preferred alternative to
14871487
/// the version of read_scanlines that takes raw pointers.
14881488
///
1489-
OIIO_NODISCARD_ERROR virtual bool read_scanlines(int subimage, int miplevel,
1490-
int ybegin, int yend,
1491-
int chbegin, int chend,
1492-
TypeDesc format,
1493-
const image_span<std::byte>& data);
1489+
OIIO_NODISCARD_ERROR virtual bool
1490+
read_scanlines(int subimage, int miplevel, int ybegin, int yend,
1491+
int chbegin, int chend, TypeDesc format,
1492+
const image_span<std::byte>& data);
14941493

14951494
/// A version of `read_scanlines()` taking an `image_span<T>`, where the
14961495
/// type of the underlying data is `T`. This is a convenience wrapper
14971496
/// around the `read_scanlines()` that takes an `image_span<std::byte>`.
14981497
template<typename T>
1499-
OIIO_NODISCARD_ERROR bool read_scanlines(int subimage, int miplevel, int ybegin,
1500-
int yend, int chbegin, int chend,
1501-
const image_span<T>& data)
1498+
OIIO_NODISCARD_ERROR bool
1499+
read_scanlines(int subimage, int miplevel, int ybegin, int yend,
1500+
int chbegin, int chend, const image_span<T>& data)
15021501
{
15031502
static_assert(!std::is_const_v<T>,
15041503
"read_scanlines() does not accept span<const T>");
@@ -1512,9 +1511,9 @@ class OIIO_API ImageInput {
15121511
/// contiguous strides in all dimensions. This is a convenience wrapper
15131512
/// around the `read_scanlines()` that takes an `image_span<T>`.
15141513
template<typename T>
1515-
OIIO_NODISCARD_ERROR bool read_scanlines(int subimage, int miplevel, int ybegin,
1516-
int yend, int chbegin, int chend,
1517-
span<T> data)
1514+
OIIO_NODISCARD_ERROR bool read_scanlines(int subimage, int miplevel,
1515+
int ybegin, int yend, int chbegin,
1516+
int chend, span<T> data)
15181517
{
15191518
static_assert(!std::is_const_v<T>,
15201519
"read_scanlines() does not accept span<const T>");
@@ -4704,6 +4703,6 @@ OIIO_NAMESPACE_END
47044703

47054704
#if FMT_VERSION >= 100000
47064705
FMT_BEGIN_NAMESPACE
4707-
template<> struct formatter<OIIO::ROI> : ostream_formatter {};
4706+
template<> struct formatter<OIIO::ROI> : ostream_formatter { };
47084707
FMT_END_NAMESPACE
47094708
#endif

src/include/OpenImageIO/platform.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,11 +462,25 @@
462462
#endif
463463

464464
// OIIO_NODISCARD_ERROR is for functions returning error status (bool) where
465-
// ignoring the return is a bad practice but not always catastrophic. This is
466-
// initially a no-op to allow a gentle transition; it can later be defined as
467-
// [[nodiscard]] once downstream callers have been updated.
468-
#ifndef OIIO_NODISCARD_ERROR
469-
# define OIIO_NODISCARD_ERROR /* nothing for now */
465+
// ignoring the return is a bad practice but not always catastrophic.
466+
//
467+
// OIIO_NODISCARD_ERROR_ENABLE, if nonzero, enables OIIO_NODISCARD_ERROR to
468+
// take effect. The default is to disable it for OIIO < 3.3, and enable it for
469+
// OIIO >= 3.3. But `-DOIIO_NODISCARD_ERROR_ENABLE=...` can be used to
470+
// override the default, for example to flag discarded errors in older
471+
// versions of OIIO, or to disable the warnings in future versions of OIIO.
472+
#ifndef OIIO_NODISCARD_ERROR_ENABLE
473+
# if OIIO_VERSION_LESS(3, 3, 0)
474+
# define OIIO_NODISCARD_ERROR_ENABLE 0 /* disable for now */
475+
# else
476+
# define OIIO_NODISCARD_ERROR_ENABLE 1 /* enable for OIIO >= 3.3 */
477+
# endif
478+
#endif
479+
480+
#if OIIO_NODISCARD_ERROR_ENABLE
481+
# define OIIO_NODISCARD_ERROR OIIO_NODISCARD
482+
#else
483+
# define OIIO_NODISCARD_ERROR /* nothing */
470484
#endif
471485

472486

src/jpeg.imageio/jpegoutput.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ class JpgOutput final : public ImageOutput {
3333
~JpgOutput() override { close(); }
3434
const char* format_name(void) const override { return "jpeg"; }
3535
int supports(string_view feature) const override
36-
{
37-
return (feature == "exif" || feature == "iptc" || feature == "ioproxy");
38-
}
36+
{ return (feature == "exif" || feature == "iptc" || feature == "ioproxy"); }
3937
bool open(const std::string& name, const ImageSpec& spec,
4038
OpenMode mode = Create) override;
4139
bool write_scanline(int y, int z, TypeDesc format, const void* data,
@@ -104,9 +102,7 @@ OIIO_PLUGIN_EXPORTS_BEGIN
104102

105103
OIIO_EXPORT ImageOutput*
106104
jpeg_output_imageio_create()
107-
{
108-
return new JpgOutput;
109-
}
105+
{ return new JpgOutput; }
110106

111107
OIIO_EXPORT const char* jpeg_output_extensions[]
112108
= { "jpg", "jpe", "jpeg", "jif", "jfif", "jfi", nullptr };
@@ -582,17 +578,14 @@ JpgOutput::copy_image(ImageInput* in)
582578
close();
583579
m_copy_coeffs = (jvirt_barray_ptr*)jpg_in->coeffs();
584580
m_copy_decompressor = &jpg_in->m_cinfo;
585-
if (!open(out_name, orig_out_spec))
586-
return false;
587-
581+
bool ok = open(out_name, orig_out_spec);
588582
// Strangeness -- the write_coefficients somehow sets things up
589583
// so that certain writes only happen in close(), which MUST
590584
// happen while the input file is still open. So we go ahead
591585
// and close() now, so that the caller of copy_image() doesn't
592586
// close the input file first and then wonder why they crashed.
593587
close();
594-
595-
return true;
588+
return ok;
596589
}
597590

598591
return ImageOutput::copy_image(in);

src/python/py_imagebuf.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ ImageBuf_setpixel(ImageBuf& buf, int x, int y, int z, py::object p)
154154

155155
void
156156
ImageBuf_setpixel2(ImageBuf& buf, int x, int y, py::object p)
157-
{
158-
ImageBuf_setpixel(buf, x, y, 0, p);
159-
}
157+
{ ImageBuf_setpixel(buf, x, y, 0, p); }
160158

161159

162160
void
@@ -194,16 +192,12 @@ ImageBuf_get_pixels(const ImageBuf& buf, TypeDesc format, ROI roi = ROI::All())
194192
void
195193
ImageBuf_set_deep_value(ImageBuf& buf, int x, int y, int z, int c, int s,
196194
float value)
197-
{
198-
buf.set_deep_value(x, y, z, c, s, value);
199-
}
195+
{ buf.set_deep_value(x, y, z, c, s, value); }
200196

201197
void
202198
ImageBuf_set_deep_value_uint(ImageBuf& buf, int x, int y, int z, int c, int s,
203199
uint32_t value)
204-
{
205-
buf.set_deep_value(x, y, z, c, s, value);
206-
}
200+
{ buf.set_deep_value(x, y, z, c, s, value); }
207201

208202

209203

@@ -271,7 +265,7 @@ ImageBuf_repr_png(const ImageBuf& self)
271265
std::unique_ptr<ImageOutput> out = ImageOutput::create("temp.png",
272266
&file_vec);
273267
if (!out || !out->open("temp.png", altered_spec))
274-
return py::none();
268+
return py::bytes();
275269
self.write(out.get());
276270
out->close();
277271

0 commit comments

Comments
 (0)