From 0ba050f15e6a17b3e0fd1f6ec1ef1bf9f7936dff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 5 Aug 2019 22:03:28 +0200 Subject: [PATCH] Don't blow up in ImageView::pixels() if the image has empty data. --- src/Magnum/ImageView.cpp | 1 + src/Magnum/ImageView.h | 9 +++++++-- src/Magnum/Test/ImageViewTest.cpp | 19 ++++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Magnum/ImageView.cpp b/src/Magnum/ImageView.cpp index 69d6768aa..b8bd542a3 100644 --- a/src/Magnum/ImageView.cpp +++ b/src/Magnum/ImageView.cpp @@ -62,6 +62,7 @@ template void ImageView::setData } template auto ImageView::pixels() const -> Containers::StridedArrayView { + if(!_data && !_data.size()) return {}; return Implementation::imagePixelView(*this); } diff --git a/src/Magnum/ImageView.h b/src/Magnum/ImageView.h index d3f226ce2..ba949963c 100644 --- a/src/Magnum/ImageView.h +++ b/src/Magnum/ImageView.h @@ -430,7 +430,9 @@ template class ImageView { * @brief View on pixel data * * Provides direct and easy-to-use access to image pixels. See - * @ref Image-pixel-views for more information. + * @ref Image-pixel-views for more information. If the view is empty + * (with @ref data() being @cpp nullptr @ce), returns @cpp nullptr @ce + * as well. */ Containers::StridedArrayView pixels() const; @@ -440,9 +442,12 @@ template class ImageView { * Compared to non-templated @ref pixels() in addition casts the pixel * data to a specified type. The user is responsible for choosing * correct type for given @ref format() --- checking it on the library - * side is not possible for the general case. + * side is not possible for the general case. If the view is empty + * (with @ref data() being @cpp nullptr @ce), returns @cpp nullptr @ce + * as well. */ template Containers::StridedArrayView::value, typename std::add_const::type, U>::type> pixels() const { + if(!_data && !_data.size()) return {}; /* Deliberately not adding a StridedArrayView include, it should work without since this is a templated function */ return Containers::arrayCast::value, typename std::add_const::type, U>::type>(pixels()); diff --git a/src/Magnum/Test/ImageViewTest.cpp b/src/Magnum/Test/ImageViewTest.cpp index ee38c24e9..6193ad15a 100644 --- a/src/Magnum/Test/ImageViewTest.cpp +++ b/src/Magnum/Test/ImageViewTest.cpp @@ -65,6 +65,7 @@ struct ImageViewTest: TestSuite::Tester { template void pixels1D(); template void pixels2D(); template void pixels3D(); + void pixelsNullptr(); }; template struct MutabilityTraits; @@ -116,7 +117,8 @@ ImageViewTest::ImageViewTest() { &ImageViewTest::pixels2D, &ImageViewTest::pixels2D, &ImageViewTest::pixels3D, - &ImageViewTest::pixels3D}); + &ImageViewTest::pixels3D, + &ImageViewTest::pixelsNullptr}); } namespace GL { @@ -667,6 +669,21 @@ template void ImageViewTest::pixels3D() { CORRADE_COMPARE(pixels.data(), image.data() + 140 + 2*20 + 3*3); } +void ImageViewTest::pixelsNullptr() { + ImageView3D image{PixelFormat::RGB8Unorm, {2, 4, 3}}; + + CORRADE_COMPARE(image.data(), nullptr); + CORRADE_COMPARE(image.data().size(), 0); + + CORRADE_COMPARE(image.pixels().data(), nullptr); + CORRADE_COMPARE(image.pixels().size(), + Containers::StridedArrayView4D::Size{}); + + CORRADE_COMPARE(image.pixels().data(), nullptr); + CORRADE_COMPARE(image.pixels().size(), + Containers::StridedArrayView3D::Size{}); +} + }}} CORRADE_TEST_MAIN(Magnum::Test::ImageViewTest)