remove size checks from image class

This commit is contained in:
2018-11-01 18:10:01 +07:00
parent 4b9de8c927
commit ebfb9152cc
7 changed files with 29 additions and 96 deletions

View File

@@ -38,7 +38,10 @@ namespace e2d
rgb_pvrtc4,
rgba_pvrtc2,
rgba_pvrtc4
rgba_pvrtc4,
rgba_pvrtc2_v2,
rgba_pvrtc4_v2
};
class bad_image_access final : public exception {
@@ -48,13 +51,6 @@ namespace e2d
}
};
class bad_image_data_format final : public exception {
public:
const char* what() const noexcept final {
return "bad image data format";
}
};
class image final {
public:
image() = default;
@@ -65,14 +61,12 @@ namespace e2d
image(const image& other);
image& operator=(const image& other);
image(const v2u& size, image_data_format format);
image(const v2u& size, image_data_format format, buffer&& data);
image(const v2u& size, image_data_format format, const buffer& data);
image& assign(image&& other) noexcept;
image& assign(const image& other);
image& assign(const v2u& size, image_data_format format);
image& assign(const v2u& size, image_data_format format, buffer&& data);
image& assign(const v2u& size, image_data_format format, const buffer& data);

View File

@@ -11,29 +11,29 @@ namespace
using namespace e2d;
struct data_format_description {
u32 minimal_size;
u32 bits_per_pixel;
image_data_format format;
bool compressed;
bool must_be_square;
bool must_be_power_of_two;
};
const data_format_description data_format_descriptions[] = {
{1, 8, image_data_format::g8, false, false, false},
{1, 16, image_data_format::ga8, false, false, false},
{1, 24, image_data_format::rgb8, false, false, false},
{1, 32, image_data_format::rgba8, false, false, false},
{ 8, image_data_format::g8, false},
{16, image_data_format::ga8, false},
{24, image_data_format::rgb8, false},
{32, image_data_format::rgba8, false},
{4, 4, image_data_format::rgb_dxt1, true, false, true},
{4, 4, image_data_format::rgba_dxt1, true, false, true},
{4, 8, image_data_format::rgba_dxt3, true, false, true},
{4, 8, image_data_format::rgba_dxt5, true, false, true},
{ 4, image_data_format::rgb_dxt1, true},
{ 4, image_data_format::rgba_dxt1, true},
{ 8, image_data_format::rgba_dxt3, true},
{ 8, image_data_format::rgba_dxt5, true},
{8, 2, image_data_format::rgb_pvrtc2, true, true, true},
{8, 4, image_data_format::rgb_pvrtc4, true, true, true},
{8, 2, image_data_format::rgba_pvrtc2, true, true, true},
{8, 4, image_data_format::rgba_pvrtc4, true, true, true}
{ 2, image_data_format::rgb_pvrtc2, true},
{ 4, image_data_format::rgb_pvrtc4, true},
{ 2, image_data_format::rgba_pvrtc2, true},
{ 4, image_data_format::rgba_pvrtc4, true},
{ 2, image_data_format::rgba_pvrtc2_v2, true},
{ 4, image_data_format::rgba_pvrtc4_v2, true}
};
const data_format_description& get_data_format_description(image_data_format format) noexcept {
@@ -43,36 +43,6 @@ namespace
E2D_ASSERT(fdesc.format == format);
return fdesc;
}
v2u adjust_image_size(const v2u& size, image_data_format format) noexcept {
const data_format_description& format_desc = get_data_format_description(format);
v2u nsize = math::maximized(size, v2u(format_desc.minimal_size));
if ( format_desc.must_be_square ) {
nsize = v2u(math::maximum(nsize));
}
if ( format_desc.must_be_power_of_two ) {
nsize = v2u(math::next_power_of_2(nsize.x),
math::next_power_of_2(nsize.y));
}
return nsize;
}
std::size_t buffer_size_for_image(const v2u& size, image_data_format format) noexcept {
const v2u nsize = adjust_image_size(size, format);
const std::size_t bpp = get_data_format_description(format).bits_per_pixel;
const std::size_t bits = nsize.x * nsize.y * bpp;
E2D_ASSERT(bits % 8 == 0);
return bits / 8;
}
bool check_image_format(const v2u& size, image_data_format format) noexcept {
return size == adjust_image_size(size, format);
}
bool check_image_format(const v2u& size, image_data_format format, const buffer& data) noexcept {
return check_image_format(size, format)
&& data.size() == buffer_size_for_image(size, format);
}
}
namespace e2d
@@ -93,10 +63,6 @@ namespace e2d
return assign(other);
}
image::image(const v2u& size, image_data_format format) {
assign(size, format);
}
image::image(const v2u& size, image_data_format format, buffer&& data) {
assign(size, format, std::move(data));
}
@@ -122,20 +88,7 @@ namespace e2d
return *this;
}
image& image::assign(const v2u& size, image_data_format format) {
if ( !check_image_format(size, format) ) {
throw bad_image_data_format();
}
data_.resize(buffer_size_for_image(size, format));
size_ = size;
format_ = format;
return *this;
}
image& image::assign(const v2u& size, image_data_format format, buffer&& data) {
if ( !check_image_format(size, format, data) ) {
throw bad_image_data_format();
}
data_.assign(std::move(data));
size_ = size;
format_ = format;
@@ -143,9 +96,6 @@ namespace e2d
}
image& image::assign(const v2u& size, image_data_format format, const buffer& data) {
if ( !check_image_format(size, format, data) ) {
throw bad_image_data_format();
}
data_.assign(data);
size_ = size;
format_ = format;

View File

@@ -9,7 +9,7 @@
namespace e2d { namespace images { namespace impl
{
bool try_load_image_dds(image& dst, const buffer& src) noexcept {
//TODO: implme
//TODO(BlackMat): implme
E2D_UNUSED(dst, src);
return false;
}

View File

@@ -9,7 +9,7 @@
namespace e2d { namespace images { namespace impl
{
bool try_load_image_pvr(image& dst, const buffer& src) noexcept {
//TODO: implme
//TODO(BlackMat): implme
E2D_UNUSED(dst, src);
return false;
}

View File

@@ -32,7 +32,7 @@ namespace e2d { namespace images { namespace impl
bool try_save_image_dds(const image& src, buffer& dst) noexcept {
E2D_UNUSED(src, dst);
if ( is_save_image_dds_supported(src.format()) ) {
//TODO: implme
//TODO(BlackMat): implme
E2D_ASSERT_MSG(false, "implme");
}
return false;

View File

@@ -16,10 +16,16 @@ namespace
case image_data_format::ga8:
case image_data_format::rgb8:
case image_data_format::rgba8:
case image_data_format::rgb_dxt1:
case image_data_format::rgba_dxt1:
case image_data_format::rgba_dxt3:
case image_data_format::rgba_dxt5:
case image_data_format::rgb_pvrtc2:
case image_data_format::rgb_pvrtc4:
case image_data_format::rgba_pvrtc2:
case image_data_format::rgba_pvrtc4:
case image_data_format::rgba_pvrtc2_v2:
case image_data_format::rgba_pvrtc4_v2:
return true;
default:
return false;
@@ -32,7 +38,7 @@ namespace e2d { namespace images { namespace impl
bool try_save_image_pvr(const image& src, buffer& dst) noexcept {
E2D_UNUSED(src, dst);
if ( is_save_image_pvr_supported(src.format()) ) {
//TODO: implme
//TODO(BlackMat): implme
E2D_ASSERT_MSG(false, "implme");
}
return false;

View File

@@ -15,23 +15,6 @@ TEST_CASE("images") {
REQUIRE(i.data().empty());
REQUIRE(i.empty());
}
{
REQUIRE_THROWS_AS(image(v2u::zero(), image_data_format::g8), bad_image_data_format);
REQUIRE_THROWS_AS(image(v2u::zero(), image_data_format::ga8), bad_image_data_format);
REQUIRE_THROWS_AS(image(v2u::zero(), image_data_format::rgb8), bad_image_data_format);
REQUIRE_THROWS_AS(image(v2u::zero(), image_data_format::rgba8), bad_image_data_format);
REQUIRE(image(v2u::unit(), image_data_format::g8).data().size() == 1);
REQUIRE(image(v2u::unit(), image_data_format::ga8).data().size() == 2);
REQUIRE(image(v2u::unit(), image_data_format::rgb8).data().size() == 3);
REQUIRE(image(v2u::unit(), image_data_format::rgba8).data().size() == 4);
REQUIRE_THROWS_AS(image(v2u::unit() * 1u, image_data_format::rgba_dxt1), bad_image_data_format);
REQUIRE_THROWS_AS(image(v2u::unit() * 2u, image_data_format::rgba_dxt1), bad_image_data_format);
REQUIRE_THROWS_AS(image(v2u::unit() * 3u, image_data_format::rgba_dxt1), bad_image_data_format);
REQUIRE(image(v2u::unit() * 4u, image_data_format::rgba_dxt1).data().size() == 8);
REQUIRE(image(v2u::unit() * 4u, image_data_format::rgba_dxt5).data().size() == 16);
}
{
const u8 img[] = {1,2,3,4,5,6,7,8};
image i0(v2u(2,2), image_data_format::g8, {img,4});