Browse Source

Don't blow up in ImageView::pixels() if the image has empty data.

pull/364/head
Vladimír Vondruš 7 years ago
parent
commit
0ba050f15e
  1. 1
      src/Magnum/ImageView.cpp
  2. 9
      src/Magnum/ImageView.h
  3. 19
      src/Magnum/Test/ImageViewTest.cpp

1
src/Magnum/ImageView.cpp

@ -62,6 +62,7 @@ template<UnsignedInt dimensions, class T> void ImageView<dimensions, T>::setData
}
template<UnsignedInt dimensions, class T> auto ImageView<dimensions, T>::pixels() const -> Containers::StridedArrayView<dimensions + 1, Type> {
if(!_data && !_data.size()) return {};
return Implementation::imagePixelView<dimensions, Type>(*this);
}

9
src/Magnum/ImageView.h

@ -430,7 +430,9 @@ template<UnsignedInt dimensions, class T> 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<dimensions + 1, Type> pixels() const;
@ -440,9 +442,12 @@ template<UnsignedInt dimensions, class T> 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<class U> Containers::StridedArrayView<dimensions, typename std::conditional<std::is_const<Type>::value, typename std::add_const<U>::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<dimensions, typename std::conditional<std::is_const<Type>::value, typename std::add_const<U>::type, U>::type>(pixels());

19
src/Magnum/Test/ImageViewTest.cpp

@ -65,6 +65,7 @@ struct ImageViewTest: TestSuite::Tester {
template<class T> void pixels1D();
template<class T> void pixels2D();
template<class T> void pixels3D();
void pixelsNullptr();
};
template<class> struct MutabilityTraits;
@ -116,7 +117,8 @@ ImageViewTest::ImageViewTest() {
&ImageViewTest::pixels2D<const char>,
&ImageViewTest::pixels2D<char>,
&ImageViewTest::pixels3D<const char>,
&ImageViewTest::pixels3D<char>});
&ImageViewTest::pixels3D<char>,
&ImageViewTest::pixelsNullptr});
}
namespace GL {
@ -667,6 +669,21 @@ template<class T> 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<const char>::Size{});
CORRADE_COMPARE(image.pixels<Color3ub>().data(), nullptr);
CORRADE_COMPARE(image.pixels<Color3ub>().size(),
Containers::StridedArrayView3D<Color3ub>::Size{});
}
}}}
CORRADE_TEST_MAIN(Magnum::Test::ImageViewTest)

Loading…
Cancel
Save