rewrite dds,pvr loading and saving

add new a8 texture format
remove rgb_dxt1
add image loading and saving tests
This commit is contained in:
2019-08-08 23:14:02 +07:00
parent b697a8a007
commit e909556c99
63 changed files with 1397 additions and 839 deletions

View File

@@ -48,19 +48,18 @@ namespace e2d
depth24,
depth24_stencil8,
g8,
ga8,
a8,
l8,
la8,
rgb8,
rgba8,
rgb_dxt1,
rgba_dxt1,
rgba_dxt3,
rgba_dxt5,
rgb_pvrtc2,
rgb_pvrtc4,
rgba_pvrtc2,
rgba_pvrtc4,

View File

@@ -9,7 +9,7 @@
#include "_utils.hpp"
#include "buffer.hpp"
#include "color.hpp"
#include "buffer_view.hpp"
#include "color32.hpp"
#include "streams.hpp"
@@ -24,19 +24,18 @@ namespace e2d
};
enum class image_data_format : u8 {
g8,
ga8,
a8,
l8,
la8,
rgb8,
rgba8,
rgb_dxt1,
rgba_dxt1,
rgba_dxt3,
rgba_dxt5,
rgb_pvrtc2,
rgb_pvrtc4,
rgba_pvrtc2,
rgba_pvrtc4,
@@ -74,8 +73,6 @@ namespace e2d
void clear() noexcept;
bool empty() const noexcept;
color pixel(u32 u, u32 v) const;
color pixel(const v2u& uv) const;
color32 pixel32(u32 u, u32 v) const;
color32 pixel32(const v2u& uv) const;
@@ -97,7 +94,7 @@ namespace e2d::images
{
bool try_load_image(
image& dst,
const buffer& src) noexcept;
buffer_view src) noexcept;
bool try_load_image(
image& dst,
@@ -112,4 +109,8 @@ namespace e2d::images
const image& src,
image_file_format format,
const output_stream_uptr& dst) noexcept;
bool check_save_image_support(
const image& src,
image_file_format format) noexcept;
}

View File

@@ -26,12 +26,13 @@ namespace
{"depth24", 24, false, true, false, pixel_declaration::pixel_type::depth24, false, v2u(1)},
{"depth24_stencil8", 32, false, true, true, pixel_declaration::pixel_type::depth24_stencil8, false, v2u(1)},
{"g8", 8, true, false, false, pixel_declaration::pixel_type::g8, false, v2u(1)},
{"ga8", 16, true, false, false, pixel_declaration::pixel_type::ga8, false, v2u(1)},
{"a8", 8, true, false, false, pixel_declaration::pixel_type::a8, false, v2u(1)},
{"l8", 8, true, false, false, pixel_declaration::pixel_type::l8, false, v2u(1)},
{"la8", 16, true, false, false, pixel_declaration::pixel_type::la8, false, v2u(1)},
{"rgb8", 24, true, false, false, pixel_declaration::pixel_type::rgb8, false, v2u(1)},
{"rgba8", 32, true, false, false, pixel_declaration::pixel_type::rgba8, false, v2u(1)},
{"rgb_dxt1", 4, true, false, false, pixel_declaration::pixel_type::rgb_dxt1, true, v2u(4,4)},
{"rgba_dxt1", 4, true, false, false, pixel_declaration::pixel_type::rgba_dxt1, true, v2u(4,4)},
{"rgba_dxt3", 8, true, false, false, pixel_declaration::pixel_type::rgba_dxt3, true, v2u(4,4)},
{"rgba_dxt5", 8, true, false, false, pixel_declaration::pixel_type::rgba_dxt5, true, v2u(4,4)},

View File

@@ -1152,12 +1152,12 @@ namespace e2d
case pixel_declaration::pixel_type::depth24_stencil8:
return caps.depth_texture_supported
&& caps.depth24_stencil8_supported;
case pixel_declaration::pixel_type::g8:
case pixel_declaration::pixel_type::ga8:
case pixel_declaration::pixel_type::a8:
case pixel_declaration::pixel_type::l8:
case pixel_declaration::pixel_type::la8:
case pixel_declaration::pixel_type::rgb8:
case pixel_declaration::pixel_type::rgba8:
return true;
case pixel_declaration::pixel_type::rgb_dxt1:
case pixel_declaration::pixel_type::rgba_dxt1:
case pixel_declaration::pixel_type::rgba_dxt3:
case pixel_declaration::pixel_type::rgba_dxt5:

View File

@@ -758,8 +758,9 @@ namespace e2d::opengl
GLenum convert_image_data_format_to_external_format(image_data_format f) noexcept {
#define DEFINE_CASE(x,y) case image_data_format::x: return y
switch ( f ) {
DEFINE_CASE(g8, GL_LUMINANCE);
DEFINE_CASE(ga8, GL_LUMINANCE_ALPHA);
DEFINE_CASE(a8, GL_ALPHA);
DEFINE_CASE(l8, GL_LUMINANCE);
DEFINE_CASE(la8, GL_LUMINANCE_ALPHA);
DEFINE_CASE(rgb8, GL_RGB);
DEFINE_CASE(rgba8, GL_RGBA);
default:
@@ -772,8 +773,9 @@ namespace e2d::opengl
GLenum convert_image_data_format_to_external_data_type(image_data_format f) noexcept {
#define DEFINE_CASE(x,y) case image_data_format::x: return y
switch ( f ) {
DEFINE_CASE(g8, GL_UNSIGNED_BYTE);
DEFINE_CASE(ga8, GL_UNSIGNED_BYTE);
DEFINE_CASE(a8, GL_UNSIGNED_BYTE);
DEFINE_CASE(l8, GL_UNSIGNED_BYTE);
DEFINE_CASE(la8, GL_UNSIGNED_BYTE);
DEFINE_CASE(rgb8, GL_UNSIGNED_BYTE);
DEFINE_CASE(rgba8, GL_UNSIGNED_BYTE);
default:
@@ -789,8 +791,9 @@ namespace e2d::opengl
DEFINE_CASE(depth16, GL_DEPTH_COMPONENT);
DEFINE_CASE(depth24, GL_DEPTH_COMPONENT);
DEFINE_CASE(depth24_stencil8, GL_DEPTH_STENCIL);
DEFINE_CASE(g8, GL_ALPHA);
DEFINE_CASE(ga8, GL_LUMINANCE_ALPHA);
DEFINE_CASE(a8, GL_ALPHA);
DEFINE_CASE(l8, GL_LUMINANCE);
DEFINE_CASE(la8, GL_LUMINANCE_ALPHA);
DEFINE_CASE(rgb8, GL_RGB);
DEFINE_CASE(rgba8, GL_RGBA);
default:
@@ -806,8 +809,9 @@ namespace e2d::opengl
DEFINE_CASE(depth16, GL_UNSIGNED_SHORT);
DEFINE_CASE(depth24, GL_UNSIGNED_INT);
DEFINE_CASE(depth24_stencil8, GL_UNSIGNED_INT_24_8);
DEFINE_CASE(g8, GL_UNSIGNED_BYTE);
DEFINE_CASE(ga8, GL_UNSIGNED_BYTE);
DEFINE_CASE(a8, GL_UNSIGNED_BYTE);
DEFINE_CASE(l8, GL_UNSIGNED_BYTE);
DEFINE_CASE(la8, GL_UNSIGNED_BYTE);
DEFINE_CASE(rgb8, GL_UNSIGNED_BYTE);
DEFINE_CASE(rgba8, GL_UNSIGNED_BYTE);
default:
@@ -824,19 +828,18 @@ namespace e2d::opengl
DEFINE_CASE(depth24, GL_DEPTH_COMPONENT24);
DEFINE_CASE(depth24_stencil8, GL_DEPTH24_STENCIL8);
DEFINE_CASE(g8, GL_ALPHA);
DEFINE_CASE(ga8, GL_LUMINANCE_ALPHA);
DEFINE_CASE(a8, GL_ALPHA);
DEFINE_CASE(l8, GL_LUMINANCE);
DEFINE_CASE(la8, GL_LUMINANCE_ALPHA);
DEFINE_CASE(rgb8, GL_RGB);
DEFINE_CASE(rgba8, GL_RGBA);
DEFINE_CASE(rgb_dxt1, GL_COMPRESSED_RGB_S3TC_DXT1_EXT);
DEFINE_CASE(rgba_dxt1, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT);
DEFINE_CASE(rgba_dxt3, GL_COMPRESSED_RGBA_S3TC_DXT3_EXT);
DEFINE_CASE(rgba_dxt5, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT);
DEFINE_CASE(rgb_pvrtc2, GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG);
DEFINE_CASE(rgb_pvrtc4, GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG);
DEFINE_CASE(rgba_pvrtc2, GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG);
DEFINE_CASE(rgba_pvrtc4, GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG);
@@ -856,19 +859,18 @@ namespace e2d::opengl
pixel_declaration convert_image_data_format_to_pixel_declaration(image_data_format f) noexcept {
#define DEFINE_CASE(x,y) case image_data_format::x: return pixel_declaration::pixel_type::y
switch ( f ) {
DEFINE_CASE(g8, g8);
DEFINE_CASE(ga8, ga8);
DEFINE_CASE(a8, a8);
DEFINE_CASE(l8, l8);
DEFINE_CASE(la8, la8);
DEFINE_CASE(rgb8, rgb8);
DEFINE_CASE(rgba8, rgba8);
DEFINE_CASE(rgb_dxt1, rgb_dxt1);
DEFINE_CASE(rgba_dxt1, rgba_dxt1);
DEFINE_CASE(rgba_dxt3, rgba_dxt3);
DEFINE_CASE(rgba_dxt5, rgba_dxt5);
DEFINE_CASE(rgb_pvrtc2, rgb_pvrtc2);
DEFINE_CASE(rgb_pvrtc4, rgb_pvrtc4);
DEFINE_CASE(rgba_pvrtc2, rgba_pvrtc2);
DEFINE_CASE(rgba_pvrtc4, rgba_pvrtc4);
@@ -1370,7 +1372,7 @@ namespace e2d::opengl
render::api_profile::opengl_compat;
caps.npot_texture_supported =
version >= gl_version::gl_210 ||
version >= gl_version::gl_210 || // gl_200
version >= gl_version::gles_300 ||
gl_has_any_extension(debug,
"GL_OES_texture_npot",
@@ -1381,38 +1383,38 @@ namespace e2d::opengl
version >= gl_version::gles_300 ||
gl_has_any_extension(debug,
"GL_OES_depth_texture",
"GL_ARB_depth_texture");
"GL_ARB_depth_texture"); // gl_140
caps.render_target_supported =
version >= gl_version::gl_300 ||
version >= gl_version::gl_300 || // gl_300
version >= gl_version::gles_200 ||
gl_has_any_extension(debug,
"GL_OES_framebuffer_object",
"GL_ARB_framebuffer_object",
"GL_EXT_framebuffer_object");
"GL_EXT_framebuffer_object",
"GL_ARB_framebuffer_object");
caps.element_index_uint =
version >= gl_version::gl_210 ||
version >= gl_version::gl_210 || // gl_100
version >= gl_version::gles_300 ||
gl_has_any_extension(debug,
"GL_OES_element_index_uint");
caps.depth16_supported =
version >= gl_version::gl_210 ||
version >= gl_version::gl_210 || // gl_140
version >= gl_version::gles_300 ||
gl_has_any_extension(debug,
"GL_OES_depth_texture",
"GL_ARB_depth_texture");
caps.depth24_supported =
version >= gl_version::gl_210 ||
version >= gl_version::gl_210 || // gl_140
version >= gl_version::gles_300 ||
gl_has_any_extension(debug,
"GL_OES_depth24",
"GL_ARB_depth_texture");
caps.depth24_stencil8_supported =
version >= gl_version::gl_300 ||
version >= gl_version::gl_300 || // gl_300
version >= gl_version::gles_300 ||
gl_has_any_extension(debug,
"GL_OES_packed_depth_stencil",

View File

@@ -17,12 +17,12 @@ namespace
};
const data_format_description data_format_descriptions[] = {
{ 8, image_data_format::g8, false},
{16, image_data_format::ga8, false},
{ 8, image_data_format::a8, false},
{ 8, image_data_format::l8, false},
{16, image_data_format::la8, false},
{24, image_data_format::rgb8, false},
{32, image_data_format::rgba8, false},
{ 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},
@@ -119,14 +119,6 @@ namespace e2d
return data_.empty();
}
color image::pixel(u32 u, u32 v) const {
return color(pixel32(u, v));
}
color image::pixel(const v2u& uv) const {
return color(pixel32(uv));
}
color32 image::pixel32(u32 u, u32 v) const {
const data_format_description& format_desc =
get_data_format_description(format_);
@@ -145,15 +137,18 @@ namespace e2d
const u8* const pixel = data_.data() + pixel_index;
switch ( format_ ) {
case image_data_format::g8:
case image_data_format::a8:
E2D_ASSERT(bytes_per_pixel == 1);
return color32(pixel[0], pixel[0], pixel[0]);
case image_data_format::ga8:
return color32(0, 0, 0, pixel[0]);
case image_data_format::l8:
E2D_ASSERT(bytes_per_pixel == 1);
return color32(pixel[0], pixel[0], pixel[0], 255);
case image_data_format::la8:
E2D_ASSERT(bytes_per_pixel == 2);
return color32(pixel[0], pixel[0], pixel[0], pixel[1]);
case image_data_format::rgb8:
E2D_ASSERT(bytes_per_pixel == 3);
return color32(pixel[0], pixel[1], pixel[2]);
return color32(pixel[0], pixel[1], pixel[2], 255);
case image_data_format::rgba8:
E2D_ASSERT(bytes_per_pixel == 4);
return color32(pixel[0], pixel[1], pixel[2], pixel[3]);
@@ -201,7 +196,7 @@ namespace e2d::images
{
bool try_load_image(
image& dst,
const buffer& src) noexcept
buffer_view src) noexcept
{
try {
return impl::load_image_dds(dst, src)
@@ -256,4 +251,25 @@ namespace e2d::images
return try_save_image(src, format, file_data)
&& streams::try_write_tail(file_data, dst);
}
bool check_save_image_support(
const image& src,
image_file_format format) noexcept
{
switch ( format ) {
case image_file_format::dds:
return impl::check_save_image_dds(src);
case image_file_format::jpg:
return impl::check_save_image_jpg(src);
case image_file_format::png:
return impl::check_save_image_png(src);
case image_file_format::pvr:
return impl::check_save_image_pvr(src);
case image_file_format::tga:
return impl::check_save_image_tga(src);
default:
E2D_ASSERT_MSG(false, "unexpected image file format");
return false;
}
}
}

View File

@@ -8,16 +8,106 @@
#include <enduro2d/utils/image.hpp>
#include <enduro2d/utils/buffer.hpp>
#include <enduro2d/utils/buffer_view.hpp>
namespace e2d::images::impl
{
bool load_image_dds(image& dst, const buffer& src);
bool load_image_pvr(image& dst, const buffer& src);
bool load_image_stb(image& dst, const buffer& src);
bool load_image_dds(image& dst, buffer_view src);
bool load_image_pvr(image& dst, buffer_view src);
bool load_image_stb(image& dst, buffer_view src);
bool save_image_dds(const image& src, buffer& dst);
bool save_image_jpg(const image& src, buffer& dst);
bool save_image_png(const image& src, buffer& dst);
bool save_image_pvr(const image& src, buffer& dst);
bool save_image_tga(const image& src, buffer& dst);
bool check_save_image_dds(const image& src) noexcept;
bool check_save_image_jpg(const image& src) noexcept;
bool check_save_image_png(const image& src) noexcept;
bool check_save_image_pvr(const image& src) noexcept;
bool check_save_image_tga(const image& src) noexcept;
}
namespace e2d::images::impl
{
inline std::size_t bgr8_to_rgb8(u8* dst, buffer_view src) noexcept {
E2D_ASSERT(src.size() % 3 == 0);
const std::size_t dst_size = src.size() / 3 * 3;
if ( !dst ) {
return dst_size;
}
const u8* data = static_cast<const u8*>(src.data());
for ( std::size_t i = 0; i < dst_size / 3; ++i ) {
const u8 b = data[0];
const u8 g = data[1];
const u8 r = data[2];
dst[0] = r;
dst[1] = g;
dst[2] = b;
dst += 3;
data += 3;
}
return dst_size;
}
inline std::size_t bgrx8_to_rgb8(u8* dst, buffer_view src) noexcept {
E2D_ASSERT(src.size() % 4 == 0);
const std::size_t dst_size = src.size() / 4 * 3;
if ( !dst ) {
return dst_size;
}
const u8* data = static_cast<const u8*>(src.data());
for ( std::size_t i = 0; i < dst_size / 3; ++i ) {
const u8 b = data[0];
const u8 g = data[1];
const u8 r = data[2];
dst[0] = r;
dst[1] = g;
dst[2] = b;
dst += 3;
data += 4;
}
return dst_size;
}
inline std::size_t bgra8_to_rgba8(u8* dst, buffer_view src) noexcept {
E2D_ASSERT(src.size() % 4 == 0);
const std::size_t dst_size = src.size() / 4 * 4;
if ( !dst ) {
return dst_size;
}
const u8* data = static_cast<const u8*>(src.data());
for ( std::size_t i = 0; i < dst_size / 4; ++i ) {
const u8 b = data[0];
const u8 g = data[1];
const u8 r = data[2];
const u8 a = data[3];
dst[0] = r;
dst[1] = g;
dst[2] = b;
dst[3] = a;
dst += 4;
data += 4;
}
return dst_size;
}
inline buffer bgr8_to_rgb8(buffer_view src) {
buffer result(bgr8_to_rgb8(nullptr, src));
bgr8_to_rgb8(result.data(), src);
return result;
}
inline buffer bgrx8_to_rgb8(buffer_view src) {
buffer result(bgrx8_to_rgb8(nullptr, src));
bgrx8_to_rgb8(result.data(), src);
return result;
}
inline buffer bgra8_to_rgba8(buffer_view src) {
buffer result(bgra8_to_rgba8(nullptr, src));
bgra8_to_rgba8(result.data(), src);
return result;
}
}

View File

@@ -0,0 +1,80 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#pragma once
#include <enduro2d/utils/_utils.hpp>
namespace e2d::images::impl::dds
{
// DDS format specification:
// https://docs.microsoft.com/en-us/windows/desktop/direct3ddds/dx-graphics-dds-pguide
// header flags
const u32 dds_hf_caps = 0x00000001;
const u32 dds_hf_height = 0x00000002;
const u32 dds_hf_width = 0x00000004;
const u32 dds_hf_pitch = 0x00000008;
const u32 dds_hf_pixel_format = 0x00001000;
const u32 dds_hf_mipmap_count = 0x00020000;
const u32 dds_hf_linear_size = 0x00080000;
const u32 dds_hf_depth = 0x00800000;
// pixel format flags
const u32 dds_pff_alphapixels = 0x1;
const u32 dds_pff_alpha = 0x2;
const u32 dds_pff_fourcc = 0x4;
const u32 dds_pff_rgb = 0x40;
const u32 dds_pff_yuv = 0x200;
const u32 dds_pff_luminance = 0x20000;
// caps flags
const u32 dds_caps_complex = 0x8;
const u32 dds_caps_mipmap = 0x400000;
const u32 dds_caps_texture = 0x1000;
// caps2 flags
const u32 dds_caps2_cubemap = 0x200;
const u32 dds_caps2_volume = 0x200000;
struct dds_pixel_format {
u32 size{0};
u32 flags{0};
u32 fourcc{0};
u32 rgbbit_count{0};
u32 rbit_mask{0};
u32 gbit_mask{0};
u32 bbit_mask{0};
u32 abit_mask{0};
};
struct dds_header {
u32 magic{0};
u32 size{0};
u32 flags{0};
u32 height{0};
u32 width{0};
u32 pitch_or_linear_size{0};
u32 depth{0};
u32 mipmap_count{0};
u32 reserved1[11] = {0};
dds_pixel_format pf;
u32 caps{0};
u32 caps2{0};
u32 reserved2[3] = {0};
};
static_assert(sizeof(dds_pixel_format) == 32, "invalid dds_pixel_format structure size");
static_assert(sizeof(dds_header) == 128, "invalid dds_header structure size");
inline constexpr u32 make_fourcc(u8 a, u8 b, u8 c, u8 d) noexcept {
return
(static_cast<u32>(a)) |
(static_cast<u32>(b) << 8) |
(static_cast<u32>(c) << 16) |
(static_cast<u32>(d) << 24);
}
}

View File

@@ -0,0 +1,137 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#pragma once
#include <enduro2d/utils/_utils.hpp>
namespace e2d::images::impl::pvr
{
// PVR format specification:
// https://cdn.imgtec.com/sdk-documentation/PVR+File+Format.Specification.pdf
enum class pvr_flags : u32 {
none = 0,
premultiplied = 0x02,
};
enum class pvr_pixel_format : u64 {
pvrtc_2bpp_rgb = 0,
pvrtc_2bpp_rgba = 1,
pvrtc_4bpp_rgb = 2,
pvrtc_4bpp_rgba = 3,
pvrtc2_2bpp = 4,
pvrtc2_4bpp = 5,
etc1 = 6,
dxt1 = 7,
dxt2 = 8,
dxt3 = 9,
dxt4 = 10,
dxt5 = 11,
bc1 = 7,
bc2 = 9,
bc3 = 11,
bc4 = 12,
bc5 = 13,
bc6 = 14,
bc7 = 15,
uyvy = 16,
yuy2 = 17,
bw_1bpp = 18,
r9g9b9e5 = 19,
rgbg8888 = 20,
grgb8888 = 21,
etc2_rgb = 22,
etc2_rgba = 23,
etc2_rgb_a1 = 24,
eac_r11 = 25,
eac_rg11 = 26,
astc_4x4 = 27,
astc_5x4 = 28,
astc_5x5 = 29,
astc_6x5 = 30,
astc_6x6 = 31,
astc_8x5 = 32,
astc_8x6 = 33,
astc_8x8 = 34,
astc_10x5 = 35,
astc_10x6 = 36,
astc_10x8 = 37,
astc_10x10 = 38,
astc_12x10 = 39,
astc_12x12 = 40,
astc_3x3x3 = 41,
astc_4x3x3 = 42,
astc_4x4x3 = 43,
astc_4x4x4 = 44,
astc_5x4x4 = 45,
astc_5x5x4 = 46,
astc_5x5x5 = 47,
astc_6x5x5 = 48,
astc_6x6x5 = 49,
astc_6x6x6 = 50,
a8 = 0x00000008'00000061ull,
l8 = 0x00000008'0000006Cull,
la8 = 0x00000808'0000616CuLL,
rgb8 = 0x00080808'00626772ull,
rgba8 = 0x08080808'61626772ull,
bgr8 = 0x08080808'00726762ull,
bgra8 = 0x08080808'61726762ull,
};
enum class pvr_color_space : u32 {
linear = 0,
srgb = 1,
};
enum class pvr_channel_type : u32 {
byte_unorm = 0,
byte_snorm = 1,
ubyte = 2,
sbyte = 3,
short_unorm = 4,
short_snorm = 5,
ushort = 6,
sshort = 7,
int_unorm = 8,
int_snorm = 9,
uint = 10,
sint = 11,
sfloat = 12,
};
struct pvr_header {
u32 version;
u32 flags;
u32 pixel_format0;
u32 pixel_format1;
u32 color_space;
u32 channel_type;
u32 height;
u32 width;
u32 depth;
u32 num_surfaces;
u32 num_faces;
u32 mipmap_count;
u32 meta_data_size;
};
struct pvr_meta_data {
u32 fourcc;
u32 key;
u32 data_size;
};
static_assert(sizeof(pvr_header) == 52, "invalid pvr_header size");
inline constexpr u32 make_fourcc(u8 a, u8 b, u8 c, u8 d) noexcept {
return
(static_cast<u32>(a)) |
(static_cast<u32>(b) << 8) |
(static_cast<u32>(c) << 16) |
(static_cast<u32>(d) << 24);
}
}

View File

@@ -1,174 +0,0 @@
/*******************************************************************************
* This file is part of the "Enduro2D"
* For conditions of distribution and use, see copyright notice in LICENSE.md
* Copyright (C) 2018-2019, by Matvey Cherevko (blackmatov@gmail.com)
******************************************************************************/
#pragma once
#include <enduro2d/utils/image.hpp>
#include <enduro2d/utils/buffer.hpp>
namespace e2d::images::impl::dds
{
// pixel format flags
const u32 ddsf_alphapixels = 0x00000001;
const u32 ddsf_fourcc = 0x00000004;
const u32 ddsf_rgb = 0x00000040;
const u32 ddsf_rgba = 0x00000041;
// dwCaps2 flags
const u32 ddsf_cubemap = 0x00000200;
const u32 ddsf_volume = 0x00200000;
// compressed texture types
const u32 fourcc_dxt1 = 0x31545844; // 'DXT1'
const u32 fourcc_dxt3 = 0x33545844; // 'DXT3'
const u32 fourcc_dxt5 = 0x35545844; // 'DXT5'
struct dds_pixel_format {
u32 dwSize;
u32 dwFlags;
u32 dwFourCC;
u32 dwRGBBitCount;
u32 dwRBitMask;
u32 dwGBitMask;
u32 dwBBitMask;
u32 dwABitMask;
};
struct dds_header {
u32 dwMagic;
u32 dwSize;
u32 dwFlags;
u32 dwHeight;
u32 dwWidth;
u32 dwPitchOrLinearSize;
u32 dwDepth;
u32 dwMipMapCount;
u32 dwReserved1[11];
dds_pixel_format ddspf;
u32 dwCaps;
u32 dwCaps2;
u32 dwCaps3;
u32 dwCaps4;
u32 dwReserved2;
};
// pixel format flags
const u32 ddsd_caps = 0x00000001;
const u32 ddsd_height = 0x00000002;
const u32 ddsd_width = 0x00000004;
const u32 ddsd_pitch = 0x00000008;
const u32 ddsd_pixelformat = 0x00001000;
const u32 ddsd_mipmapcount = 0x00020000;
const u32 ddsd_linearsize = 0x00080000;
const u32 ddsd_dept = 0x00800000;
static_assert(sizeof(dds_pixel_format) == 32, "invalid dds_pixel_format structure size");
static_assert(sizeof(dds_header) == 128, "invalid dds_header structure size");
}
namespace e2d::images::impl::pvr
{
enum class pvr_flags : u32 {
none = 0,
premultiplied = 0x02,
};
enum class pvr_pixel_format : u64 {
pvrtc_2bpp_rgb = 0,
pvrtc_2bpp_rgba = 1,
pvrtc_4bpp_rgb = 2,
pvrtc_4bpp_rgba = 3,
pvrtc_ii_2bpp = 4,
pvrtc_ii_4bpp = 5,
etc1 = 6,
dxt1 = 7,
dxt2 = 8,
dxt3 = 9,
dxt4 = 10,
dxt5 = 11,
bc1 = 7,
bc2 = 9,
bc3 = 11,
bc4 = 12,
bc5 = 13,
bc6 = 14,
bc7 = 15,
uyvy = 16,
yuy2 = 17,
bw_1bpp = 18,
r9g9b9e5 = 19,
rgbg8888 = 20,
grgb8888 = 21,
etc2_rgb = 22,
etc2_rgba = 23,
etc2_rgb_a1 = 24,
eac_r11 = 25,
eac_rg11 = 26,
astc_4x4 = 27,
astc_5x4 = 28,
astc_5x5 = 29,
astc_6x5 = 30,
astc_6x6 = 31,
astc_8x5 = 32,
astc_8x6 = 33,
astc_8x8 = 34,
astc_10x5 = 35,
astc_10x6 = 36,
astc_10x8 = 37,
astc_10x10 = 38,
astc_12x10 = 39,
astc_12x12 = 40,
rgba8 = 0x08080808'61626772ull,
r8 = 0x00000008'00000072ull,
rg8 = 0x00000808'00006772ull,
rgb8 = 0x00080808'00626772ull,
};
enum class pvr_color_space : u32 {
linear = 0,
srgb = 1,
};
enum class pvr_channel_type : u32 {
byte_unorm = 0,
byte_snorm = 1,
ubyte = 2,
sbyte = 3,
short_unorm = 4,
short_snorm = 5,
ushort = 6,
sshort = 7,
int_unorm = 8,
int_snorm = 9,
uint = 10,
sint = 11,
sfloat = 12,
};
struct pvr_header {
u32 version;
u32 flags;
u32 pixelFormat0;
u32 pixelFormat1;
u32 colorSpace;
u32 channelType;
u32 height;
u32 width;
u32 depth;
u32 numSurfaces;
u32 numFaces;
u32 mipMapCount;
u32 metaDataSize;
};
struct pvr_meta_data {
u32 four_cc{0};
u32 key{0};
u32 data_size{0};
};
static_assert(sizeof(pvr_header) == 52, "invalid PVR header size");
}

View File

@@ -5,114 +5,234 @@
******************************************************************************/
#include "image_impl.hpp"
#include "image_impl_structures.hpp"
#include "image_impl_dds.hpp"
namespace
{
// DDS format specification:
// https://docs.microsoft.com/en-us/windows/desktop/direct3ddds/dx-graphics-dds-pguide
using namespace e2d;
using namespace e2d::images::impl;
using namespace e2d::images::impl::dds;
bool is_dds(const void* data, std::size_t byte_size) noexcept {
if ( byte_size > sizeof(dds_header) ) {
const auto* hdr = reinterpret_cast<const dds_header*>(data);
return hdr->dwMagic == 0x20534444; // 'DDS '
}
return false;
}
struct image_info {
buffer data;
image_data_format format = image_data_format(-1);
u32 bytes_per_block = 0;
v2u block_size;
bool get_dds_format(
const dds_header& hdr,
image_data_format& out_format,
u32& out_bytes_per_block,
v2u& out_block_size) noexcept
{
if ( math::check_all_flags(hdr.ddspf.dwFlags, ddsf_fourcc) ) {
switch ( hdr.ddspf.dwFourCC ) {
case fourcc_dxt1:
out_format = math::check_all_flags(hdr.ddspf.dwFlags, ddsf_alphapixels)
? image_data_format::rgba_dxt1
: image_data_format::rgb_dxt1;
out_bytes_per_block = 8;
out_block_size = v2u(4,4);
break;
case fourcc_dxt3:
out_format = image_data_format::rgba_dxt3;
out_bytes_per_block = 16;
out_block_size = v2u(4,4);
break;
case fourcc_dxt5:
out_format = image_data_format::rgba_dxt5;
out_bytes_per_block = 16;
out_block_size = v2u(4,4);
break;
default:
return false;
}
} else if ( math::check_all_flags(hdr.ddspf.dwFlags, ddsf_rgba) && (hdr.ddspf.dwRGBBitCount == 32) ) {
out_format = image_data_format::rgba8;
out_bytes_per_block = 4;
out_block_size = v2u(1,1);
if ( hdr.ddspf.dwRBitMask == 0x00FF0000 ) {
return false; // BGRA format is not supported
}
} else if ( math::check_all_flags(hdr.ddspf.dwFlags, ddsf_rgb) && (hdr.ddspf.dwRGBBitCount == 24) ) {
out_format = image_data_format::rgb8;
out_bytes_per_block = 3;
out_block_size = v2u(1,1);
if (hdr.ddspf.dwRBitMask == 0x00FF0000) {
return false; // BGRA format is not supported
}
} else if ( hdr.ddspf.dwRGBBitCount == 16 ) {
return false; // 16-bit format is not supported by engine
} else if ( hdr.ddspf.dwRGBBitCount == 8 ) {
out_format = image_data_format::g8;
out_bytes_per_block = 1;
out_block_size = v2u(1,1);
} else {
return false;
image_info() = default;
image_info(buffer data, image_data_format format, u32 bytes_per_block, v2u block_size)
: data(std::move(data))
, format(format)
, bytes_per_block(bytes_per_block)
, block_size(block_size) {}
bool valid() const noexcept {
return !data.empty()
&& format != image_data_format(-1)
&& bytes_per_block > 0
&& block_size.x > 0
&& block_size.y > 0;
}
return true;
};
image_info extract_image_info(
const dds_header& hdr,
buffer_view content)
{
if ( math::check_all_flags(hdr.pf.flags, dds_pff_fourcc) ) {
switch ( hdr.pf.fourcc ) {
case make_fourcc('D', 'X', 'T', '1'):
return {
buffer(content),
image_data_format::rgba_dxt1,
8,
{4,4}};
case make_fourcc('D', 'X', 'T', '2'):
case make_fourcc('D', 'X', 'T', '3'):
return {
buffer(content),
image_data_format::rgba_dxt3,
16,
{4,4}};
case make_fourcc('D', 'X', 'T', '4'):
case make_fourcc('D', 'X', 'T', '5'):
return {
buffer(content),
image_data_format::rgba_dxt5,
16,
{4,4}};
}
}
if ( math::check_all_flags(hdr.pf.flags, dds_pff_alpha) ) {
if ( hdr.pf.rgbbit_count == 8 &&
hdr.pf.abit_mask == 0x000000FF )
{
return {
buffer(content),
image_data_format::a8,
1,
{1,1}};
}
}
if ( math::check_all_flags(hdr.pf.flags, dds_pff_luminance | dds_pff_alphapixels) ) {
if ( hdr.pf.rgbbit_count == 16 &&
hdr.pf.rbit_mask == 0x000000FF &&
hdr.pf.abit_mask == 0x0000FF00 )
{
return {
buffer(content),
image_data_format::la8,
2,
{1,1}};
}
}
if ( math::check_all_flags(hdr.pf.flags, dds_pff_luminance) ) {
if ( hdr.pf.rgbbit_count == 8 &&
hdr.pf.rbit_mask == 0x000000FF )
{
return {
buffer(content),
image_data_format::l8,
1,
{1,1}};
}
}
if ( math::check_all_flags(hdr.pf.flags, dds_pff_rgb | dds_pff_alphapixels) ) {
if ( hdr.pf.rgbbit_count == 8 ) {
if ( hdr.pf.rbit_mask == 0x00000000 &&
hdr.pf.gbit_mask == 0x00000000 &&
hdr.pf.bbit_mask == 0x00000000 &&
hdr.pf.abit_mask == 0x000000FF )
{
return {
buffer(content),
image_data_format::a8,
1,
{1,1}};
}
}
if ( hdr.pf.rgbbit_count == 32 ) {
if ( hdr.pf.rbit_mask == 0x000000FF &&
hdr.pf.gbit_mask == 0x0000FF00 &&
hdr.pf.bbit_mask == 0x00FF0000 &&
hdr.pf.abit_mask == 0xFF000000 )
{
return {
buffer(content),
image_data_format::rgba8,
4,
{1,1}};
}
if ( hdr.pf.rbit_mask == 0x00FF0000 &&
hdr.pf.gbit_mask == 0x0000FF00 &&
hdr.pf.bbit_mask == 0x000000FF &&
hdr.pf.abit_mask == 0xFF000000 )
{
return {
bgra8_to_rgba8(content),
image_data_format::rgba8,
4,
{1,1}};
}
}
}
if ( math::check_all_flags(hdr.pf.flags, dds_pff_rgb) ) {
if ( hdr.pf.rgbbit_count == 24 ) {
if ( hdr.pf.rbit_mask == 0x000000FF &&
hdr.pf.gbit_mask == 0x0000FF00 &&
hdr.pf.bbit_mask == 0x00FF0000 )
{
return {
buffer(content),
image_data_format::rgb8,
3,
{1,1}};
}
if ( hdr.pf.rbit_mask == 0x00FF0000 &&
hdr.pf.gbit_mask == 0x0000FF00 &&
hdr.pf.bbit_mask == 0x000000FF )
{
return {
bgr8_to_rgb8(content),
image_data_format::rgb8,
3,
{1,1}};
}
}
if ( hdr.pf.rgbbit_count == 32 ) {
if ( hdr.pf.rbit_mask == 0x00FF0000 &&
hdr.pf.gbit_mask == 0x0000FF00 &&
hdr.pf.bbit_mask == 0x000000FF )
{
return {
bgrx8_to_rgb8(content),
image_data_format::rgb8,
3,
{1,1}};
}
}
}
return image_info();
}
}
namespace e2d::images::impl
{
bool load_image_dds(image& dst, const buffer& src) {
if ( !is_dds(src.data(), src.size()) ) {
bool load_image_dds(image& dst, buffer_view src) {
if ( src.size() < sizeof(dds_header) ) {
return false;
}
const dds_header& hdr = *reinterpret_cast<const dds_header*>(src.data());
const u8* content = src.data() + sizeof(dds_header);
const std::size_t content_size = static_cast<std::size_t>(src.data() + src.size() - content);
if ( math::check_all_flags(hdr.dwCaps2, ddsf_cubemap) ||
math::check_all_flags(hdr.dwCaps2, ddsf_volume) ||
(hdr.dwDepth > 0) )
if ( hdr.magic != make_fourcc('D', 'D', 'S', ' ') ) {
return false;
}
if ( hdr.size != sizeof(dds_header) - sizeof(hdr.magic) ) {
return false;
}
if ( hdr.pf.size != sizeof(dds_pixel_format) ) {
return false;
}
if ( math::check_any_flags(hdr.caps, dds_caps_complex | dds_caps_mipmap) ||
math::check_any_flags(hdr.caps2, dds_caps2_cubemap | dds_caps2_volume) ||
hdr.depth > 1 ||
hdr.mipmap_count > 1 )
{
return false;
}
image_data_format format = image_data_format(-1);
u32 bytes_per_block = 0;
v2u block_size = v2u(1,1);
if ( !get_dds_format(hdr, format, bytes_per_block, block_size) ) {
image_info info = extract_image_info(hdr, {
static_cast<const u8*>(src.data()) + sizeof(dds_header),
src.size() - sizeof(dds_header)});
if ( !info.valid() ) {
return false;
}
const v2u dimension = v2u(hdr.dwWidth, hdr.dwHeight);
const std::size_t size = bytes_per_block *
((dimension.x + block_size.x - 1) / block_size.x) *
((dimension.y + block_size.y - 1) / block_size.y);
const v2u dimension = v2u(hdr.width, hdr.height);
const std::size_t expected_image_data_size = info.bytes_per_block *
((dimension.x + info.block_size.x - 1) / info.block_size.x) *
((dimension.y + info.block_size.y - 1) / info.block_size.y);
if ( content_size != size ) {
if ( info.data.size() != expected_image_data_size ) {
return false;
}
dst = image(dimension, format, buffer(content, size));
dst = image(dimension, info.format, std::move(info.data));
return true;
}
}

View File

@@ -5,141 +5,196 @@
******************************************************************************/
#include "image_impl.hpp"
#include "image_impl_structures.hpp"
#include "image_impl_pvr.hpp"
namespace
{
// PVR format specification:
// https://cdn.imgtec.com/sdk-documentation/PVR+File+Format.Specification.pdf
using namespace e2d;
using namespace e2d::images::impl;
using namespace e2d::images::impl::pvr;
bool is_pvr(const void* data, std::size_t byte_size) noexcept {
if ( byte_size > sizeof(pvr_header) ) {
const pvr_header* hdr = reinterpret_cast<const pvr_header*>(data);
return hdr->version == 0x03525650;
}
return false;
}
struct image_info {
buffer data;
image_data_format format = image_data_format(-1);
u32 bytes_per_block = 0;
v2u block_size;
bool get_pvr_format(
image_info() = default;
image_info(buffer data, image_data_format format, u32 bytes_per_block, v2u block_size)
: data(std::move(data))
, format(format)
, bytes_per_block(bytes_per_block)
, block_size(block_size) {}
bool valid() const noexcept {
return !data.empty()
&& format != image_data_format(-1)
&& bytes_per_block > 0
&& block_size.x > 0
&& block_size.y > 0;
}
};
image_info extract_image_info(
const pvr_header& hdr,
image_data_format& out_format,
u32& out_bytes_per_block,
v2u& out_block_size) noexcept
buffer_view content) noexcept
{
if ( pvr_color_space(hdr.colorSpace) != pvr_color_space::linear ) {
return false;
if ( pvr_color_space(hdr.color_space) != pvr_color_space::linear ) {
return image_info();
}
const pvr_pixel_format fmt = pvr_pixel_format(
hdr.pixelFormat0 | (u64(hdr.pixelFormat1) << 32));
switch (fmt)
{
(static_cast<u64>(hdr.pixel_format0)) |
(static_cast<u64>(hdr.pixel_format1) << 32));
switch ( fmt ) {
case pvr_pixel_format::pvrtc_2bpp_rgb:
out_format = image_data_format::rgb_pvrtc2;
out_bytes_per_block = 8;
out_block_size = v2u(8,4);
break;
return {
buffer(content),
image_data_format::rgb_pvrtc2,
8,
{8,4}};
case pvr_pixel_format::pvrtc_2bpp_rgba:
out_format = image_data_format::rgba_pvrtc2;
out_bytes_per_block = 8;
out_block_size = v2u(8,4);
break;
return {
buffer(content),
image_data_format::rgba_pvrtc2,
8,
{8,4}};
case pvr_pixel_format::pvrtc_4bpp_rgb:
out_format = image_data_format::rgb_pvrtc4;
out_bytes_per_block = 8;
out_block_size = v2u(4,4);
break;
return {
buffer(content),
image_data_format::rgb_pvrtc4,
8,
{4,4}};
case pvr_pixel_format::pvrtc_4bpp_rgba:
out_format = image_data_format::rgba_pvrtc4;
out_bytes_per_block = 8;
out_block_size = v2u(4,4);
break;
case pvr_pixel_format::pvrtc_ii_2bpp:
out_format = image_data_format::rgba_pvrtc2_v2;
out_bytes_per_block = 8;
out_block_size = v2u(8,4);
break;
case pvr_pixel_format::pvrtc_ii_4bpp:
out_format = image_data_format::rgba_pvrtc4_v2;
out_bytes_per_block = 8;
out_block_size = v2u(4,4);
break;
return {
buffer(content),
image_data_format::rgba_pvrtc4,
8,
{4,4}};
case pvr_pixel_format::pvrtc2_2bpp:
return {
buffer(content),
image_data_format::rgba_pvrtc2_v2,
8,
{8,4}};
case pvr_pixel_format::pvrtc2_4bpp:
return {
buffer(content),
image_data_format::rgba_pvrtc4_v2,
8,
{4,4}};
case pvr_pixel_format::dxt1:
out_format = image_data_format::rgb_dxt1;
out_bytes_per_block = 8;
out_block_size = v2u(4,4);
break;
return {
buffer(content),
image_data_format::rgba_dxt1,
8,
{4,4}};
case pvr_pixel_format::dxt2:
case pvr_pixel_format::dxt3:
out_format = image_data_format::rgba_dxt3;
out_bytes_per_block = 16;
out_block_size = v2u(4,4);
break;
return {
buffer(content),
image_data_format::rgba_dxt3,
16,
{4,4}};
case pvr_pixel_format::dxt4:
case pvr_pixel_format::dxt5:
out_format = image_data_format::rgba_dxt5;
out_bytes_per_block = 16;
out_block_size = v2u(4,4);
break;
case pvr_pixel_format::rgba8:
out_format = image_data_format::rgba8;
out_bytes_per_block = 4;
out_block_size = v2u(1,1);
break;
case pvr_pixel_format::r8:
out_format = image_data_format::g8;
out_bytes_per_block = 1;
out_block_size = v2u(1,1);
break;
case pvr_pixel_format::rg8:
out_format = image_data_format::ga8;
out_bytes_per_block = 2;
out_block_size = v2u(1,1);
break;
return {
buffer(content),
image_data_format::rgba_dxt5,
16,
{4,4}};
case pvr_pixel_format::a8:
return {
buffer(content),
image_data_format::a8,
1,
{1,1}};
case pvr_pixel_format::l8:
return {
buffer(content),
image_data_format::l8,
1,
{1,1}};
case pvr_pixel_format::la8:
return {
buffer(content),
image_data_format::la8,
2,
{1,1}};
case pvr_pixel_format::rgb8:
out_format = image_data_format::rgb8;
out_bytes_per_block = 3;
out_block_size = v2u(1,1);
break;
return {
buffer(content),
image_data_format::rgb8,
3,
{1,1}};
case pvr_pixel_format::rgba8:
return {
buffer(content),
image_data_format::rgba8,
4,
{1,1}};
case pvr_pixel_format::bgr8:
return {
bgr8_to_rgb8(content),
image_data_format::rgb8,
3,
{1,1}};
case pvr_pixel_format::bgra8:
return {
bgra8_to_rgba8(content),
image_data_format::rgba8,
4,
{1,1}};
default:
return false;
return image_info();
}
return true;
}
}
namespace e2d::images::impl
{
bool load_image_pvr(image& dst, const buffer& src) {
if ( !is_pvr(src.data(), src.size()) ) {
bool load_image_pvr(image& dst, buffer_view src) {
if ( src.size() < sizeof(pvr_header) ) {
return false;
}
const pvr_header& hdr = *reinterpret_cast<const pvr_header*>(src.data());
const u8* content = src.data() + sizeof(pvr_header) + hdr.metaDataSize;
const std::size_t content_size = static_cast<std::size_t>(src.data() + src.size() - content);
if ( hdr.numSurfaces != 1 || hdr.numFaces != 1 || hdr.depth > 1 ) {
return false; // cubemap and volume textures are not supported
if ( hdr.version != make_fourcc('P', 'V', 'R', 3) ) {
return false;
}
image_data_format format = image_data_format(-1);
u32 bytes_per_block = 0;
v2u block_size = v2u(1,1);
if ( !get_pvr_format(hdr, format, bytes_per_block, block_size) ) {
if ( src.size() < sizeof(pvr_header) + hdr.meta_data_size ) {
return false;
}
if ( hdr.mipmap_count > 1 ||
hdr.num_surfaces > 1 ||
hdr.num_faces > 1 ||
hdr.depth > 1 )
{
return false;
}
image_info info = extract_image_info(hdr, {
static_cast<const u8*>(src.data()) + sizeof(pvr_header) + hdr.meta_data_size,
src.size() - sizeof(pvr_header) - hdr.meta_data_size});
if ( !info.valid() ) {
return false;
}
const v2u dimension = v2u(hdr.width, hdr.height);
const std::size_t size = bytes_per_block *
((dimension.x + block_size.x - 1) / block_size.x) *
((dimension.y + block_size.y - 1) / block_size.y);
const std::size_t expected_image_data_size = info.bytes_per_block *
((dimension.x + info.block_size.x - 1) / info.block_size.x) *
((dimension.y + info.block_size.y - 1) / info.block_size.y);
if ( content_size != size ) {
if ( info.data.size() != expected_image_data_size ) {
return false;
}
dst = image(dimension, format, buffer(content, size));
dst = image(dimension, info.format, std::move(info.data));
return true;
}
}

View File

@@ -31,7 +31,7 @@ namespace
using stbi_img_uptr = std::unique_ptr<stbi_uc, decltype(&stbi_image_free)>;
stbi_img_uptr load_stb_image(const buffer& data, v2u& out_size, u32& out_channels) noexcept {
stbi_img_uptr load_stb_image(buffer_view data, v2u& out_size, u32& out_channels) noexcept {
int img_w = 0, img_h = 0, img_c = 0;
stbi_uc* img = stbi_load_from_memory(
static_cast<const stbi_uc*>(data.data()),
@@ -48,8 +48,8 @@ namespace
image_data_format image_format_from_stb_channels(u32 channels) noexcept {
switch ( channels ) {
case 1: return image_data_format::g8;
case 2: return image_data_format::ga8;
case 1: return image_data_format::l8;
case 2: return image_data_format::la8;
case 3: return image_data_format::rgb8;
case 4: return image_data_format::rgba8;
default:
@@ -61,7 +61,7 @@ namespace
namespace e2d::images::impl
{
bool load_image_stb(image& dst, const buffer& src) {
bool load_image_stb(image& dst, buffer_view src) {
v2u img_size;
u32 img_channels = 0;
const stbi_img_uptr img_ptr = load_stb_image(src, img_size, img_channels);

View File

@@ -5,20 +5,24 @@
******************************************************************************/
#include "image_impl.hpp"
#include "image_impl_structures.hpp"
#include "image_impl_dds.hpp"
namespace
{
using namespace e2d;
using namespace e2d::images::impl::dds;
}
bool is_save_image_dds_supported(image_data_format data_format) noexcept {
switch ( data_format ) {
case image_data_format::g8:
namespace e2d::images::impl
{
bool check_save_image_dds(const image& src) noexcept {
switch ( src.format() ) {
case image_data_format::a8:
case image_data_format::l8:
case image_data_format::la8:
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:
@@ -28,105 +32,109 @@ namespace
}
}
u32 get_pitch_or_linear_size(image_data_format data_format) noexcept {
switch ( data_format ) {
case image_data_format::g8:
break;
case image_data_format::rgb8:
break;
case image_data_format::rgba8:
break;
case image_data_format::rgb_dxt1:
break;
case image_data_format::rgba_dxt1:
break;
case image_data_format::rgba_dxt3:
break;
case image_data_format::rgba_dxt5:
break;
default:
E2D_ASSERT_MSG(false, "unused pixel format");
break;
}
return 0;
}
}
namespace e2d::images::impl
{
bool save_image_dds(const image& src, buffer& dst) {
E2D_UNUSED(src, dst);
if ( is_save_image_dds_supported(src.format()) ) {
dds_header hdr;
hdr.dwMagic = 0x20534444;
hdr.dwSize = 124;
hdr.dwFlags = ddsd_caps | ddsd_height | ddsd_width | ddsd_pixelformat;
hdr.dwHeight = src.size().y;
hdr.dwWidth = src.size().x;
hdr.dwPitchOrLinearSize = get_pitch_or_linear_size(src.format());
hdr.dwDepth = 0;
hdr.dwMipMapCount = 0;
hdr.ddspf.dwSize = 32;
hdr.ddspf.dwFlags = 0;
hdr.ddspf.dwFourCC = 0;
hdr.ddspf.dwRBitMask = 0;
hdr.ddspf.dwGBitMask = 0;
hdr.ddspf.dwBBitMask = 0;
hdr.ddspf.dwABitMask = 0;
switch ( src.format() ) {
case image_data_format::g8:
hdr.ddspf.dwRGBBitCount = 8;
break;
case image_data_format::rgb8:
hdr.ddspf.dwFlags |= ddsf_rgb;
hdr.ddspf.dwRGBBitCount = 24;
hdr.ddspf.dwRBitMask = 0x000000FF;
hdr.ddspf.dwGBitMask = 0x0000FF00;
hdr.ddspf.dwBBitMask = 0x00FF0000;
hdr.ddspf.dwABitMask = 0x00000000;
break;
case image_data_format::rgba8:
hdr.ddspf.dwFlags |= ddsf_rgba;
hdr.ddspf.dwRGBBitCount = 32;
hdr.ddspf.dwRBitMask = 0x000000FF;
hdr.ddspf.dwGBitMask = 0x0000FF00;
hdr.ddspf.dwBBitMask = 0x00FF0000;
hdr.ddspf.dwABitMask = 0xFF000000;
break;
case image_data_format::rgb_dxt1:
hdr.ddspf.dwFlags |= ddsf_fourcc;
hdr.ddspf.dwFourCC = fourcc_dxt1;
break;
case image_data_format::rgba_dxt1:
hdr.ddspf.dwFlags |= ddsf_fourcc | ddsf_alphapixels;
hdr.ddspf.dwFourCC = fourcc_dxt1;
break;
case image_data_format::rgba_dxt3:
hdr.ddspf.dwFlags |= ddsf_fourcc;
hdr.ddspf.dwFourCC |= fourcc_dxt3;
break;
case image_data_format::rgba_dxt5:
hdr.ddspf.dwFlags |= ddsf_fourcc;
hdr.ddspf.dwFourCC = fourcc_dxt5;
break;
default:
E2D_ASSERT_MSG(false, "unused pixel format");
break;;
}
hdr.dwCaps = 0x1000;//DDSCAPS_TEXTURE
hdr.dwCaps2 = 0;
hdr.dwCaps3 = 0;
hdr.dwCaps4 = 0;
hdr.dwReserved2 = 0;
size_t size = sizeof(hdr) + src.data().size();
u8 content[size];
std::memcpy(content, &hdr, sizeof(hdr));
std::memcpy(content + sizeof(hdr), src.data().data(), src.data().size());
dst.assign(content, size);
return true;
if ( !check_save_image_dds(src) ) {
return false;
}
return false;
dds_header hdr;
hdr.magic = make_fourcc('D', 'D', 'S', ' ');
hdr.size = sizeof(dds_header) - sizeof(hdr.magic);
hdr.flags =
dds_hf_caps |
dds_hf_width |
dds_hf_height |
dds_hf_linear_size |
dds_hf_pixel_format;
hdr.width = src.size().x;
hdr.height = src.size().y;
hdr.pitch_or_linear_size = math::numeric_cast<u32>(src.data().size());
hdr.depth = 0;
hdr.mipmap_count = 0;
hdr.caps = dds_caps_texture;
hdr.caps2 = 0;
hdr.pf.size = sizeof(dds_pixel_format);
hdr.pf.flags = 0;
hdr.pf.fourcc = 0;
hdr.pf.rgbbit_count = 0;
hdr.pf.rbit_mask = 0;
hdr.pf.gbit_mask = 0;
hdr.pf.bbit_mask = 0;
hdr.pf.abit_mask = 0;
switch ( src.format() ) {
case image_data_format::a8:
hdr.pf.flags = dds_pff_alpha | dds_pff_alphapixels;
hdr.pf.rgbbit_count = 8;
hdr.pf.rbit_mask = 0x00000000;
hdr.pf.gbit_mask = 0x00000000;
hdr.pf.bbit_mask = 0x00000000;
hdr.pf.abit_mask = 0x000000FF;
break;
case image_data_format::l8:
hdr.pf.flags = dds_pff_luminance;
hdr.pf.rgbbit_count = 8;
hdr.pf.rbit_mask = 0x000000FF;
hdr.pf.gbit_mask = 0x00000000;
hdr.pf.bbit_mask = 0x00000000;
hdr.pf.abit_mask = 0x00000000;
break;
case image_data_format::la8:
hdr.pf.flags = dds_pff_luminance | dds_pff_alphapixels;
hdr.pf.rgbbit_count = 16;
hdr.pf.rbit_mask = 0x000000FF;
hdr.pf.gbit_mask = 0x00000000;
hdr.pf.bbit_mask = 0x00000000;
hdr.pf.abit_mask = 0x0000FF00;
break;
case image_data_format::rgb8:
hdr.pf.flags = dds_pff_rgb;
hdr.pf.rgbbit_count = 24;
hdr.pf.rbit_mask = 0x000000FF;
hdr.pf.gbit_mask = 0x0000FF00;
hdr.pf.bbit_mask = 0x00FF0000;
hdr.pf.abit_mask = 0x00000000;
break;
case image_data_format::rgba8:
hdr.pf.flags = dds_pff_rgb | dds_pff_alphapixels;
hdr.pf.rgbbit_count = 32;
hdr.pf.rbit_mask = 0x000000FF;
hdr.pf.gbit_mask = 0x0000FF00;
hdr.pf.bbit_mask = 0x00FF0000;
hdr.pf.abit_mask = 0xFF000000;
break;
case image_data_format::rgba_dxt1:
hdr.pf.flags = dds_pff_fourcc;
hdr.pf.fourcc = make_fourcc('D', 'X', 'T', '1');
break;
case image_data_format::rgba_dxt3:
hdr.pf.flags = dds_pff_fourcc;
hdr.pf.fourcc = make_fourcc('D', 'X', 'T', '3');
break;
case image_data_format::rgba_dxt5:
hdr.pf.flags = dds_pff_fourcc;
hdr.pf.fourcc = make_fourcc('D', 'X', 'T', '5');
break;
default:
E2D_ASSERT_MSG(false, "unexpected image data format");
return false;
}
buffer image_data(sizeof(dds_header) + src.data().size());
std::memcpy(
image_data.data(),
&hdr,
sizeof(dds_header));
std::memcpy(
image_data.data() + sizeof(dds_header),
src.data().data(),
src.data().size());
dst.assign(std::move(image_data));
return true;
}
}

View File

@@ -5,26 +5,58 @@
******************************************************************************/
#include "image_impl.hpp"
#include "image_impl_structures.hpp"
#include "image_impl_pvr.hpp"
namespace
{
using namespace e2d;
using namespace e2d::images::impl::pvr;
bool is_save_image_pvr_supported(image_data_format data_format) noexcept {
switch ( data_format ) {
case image_data_format::g8:
case image_data_format::ga8:
pvr_pixel_format get_pvr_pixel_format(image_data_format fmt) noexcept {
switch ( fmt ) {
case image_data_format::a8: return pvr_pixel_format::a8;
case image_data_format::l8: return pvr_pixel_format::l8;
case image_data_format::la8: return pvr_pixel_format::la8;
case image_data_format::rgb8: return pvr_pixel_format::rgb8;
case image_data_format::rgba8: return pvr_pixel_format::rgba8;
case image_data_format::rgba_dxt1: return pvr_pixel_format::dxt1;
case image_data_format::rgba_dxt3: return pvr_pixel_format::dxt3;
case image_data_format::rgba_dxt5: return pvr_pixel_format::dxt5;
case image_data_format::rgb_pvrtc2: return pvr_pixel_format::pvrtc_2bpp_rgb;
case image_data_format::rgb_pvrtc4: return pvr_pixel_format::pvrtc_4bpp_rgb;
case image_data_format::rgba_pvrtc2: return pvr_pixel_format::pvrtc_2bpp_rgba;
case image_data_format::rgba_pvrtc4: return pvr_pixel_format::pvrtc_4bpp_rgba;
case image_data_format::rgba_pvrtc2_v2: return pvr_pixel_format::pvrtc2_2bpp;
case image_data_format::rgba_pvrtc4_v2: return pvr_pixel_format::pvrtc2_4bpp;
default:
E2D_ASSERT_MSG(false, "unexpected image data format");
return pvr_pixel_format(-1);
}
}
}
namespace e2d::images::impl
{
bool check_save_image_pvr(const image& src) noexcept {
switch ( src.format() ) {
case image_data_format::a8:
case image_data_format::l8:
case image_data_format::la8:
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;
@@ -32,89 +64,42 @@ namespace
return false;
}
}
u64 get_pvr_pixel_format(image_data_format data_format) noexcept {
pvr_pixel_format res;
switch ( data_format ) {
case image_data_format::g8:
res = pvr_pixel_format::r8;
break;
case image_data_format::ga8:
res = pvr_pixel_format::rg8;
break;
case image_data_format::rgb8:
res = pvr_pixel_format::rgb8;
break;
case image_data_format::rgba8:
res = pvr_pixel_format::rgba8;
break;
case image_data_format::rgb_dxt1:
res = pvr_pixel_format::dxt1;
break;
case image_data_format::rgba_dxt3:
res = pvr_pixel_format::dxt3;
break;
case image_data_format::rgba_dxt5:
res = pvr_pixel_format::dxt5;
break;
case image_data_format::rgb_pvrtc2:
res = pvr_pixel_format::pvrtc_2bpp_rgb;
break;
case image_data_format::rgb_pvrtc4:
res = pvr_pixel_format::pvrtc_4bpp_rgb;
break;
case image_data_format::rgba_pvrtc2:
res = pvr_pixel_format::pvrtc_2bpp_rgba;
break;
case image_data_format::rgba_pvrtc4:
res = pvr_pixel_format::pvrtc_4bpp_rgba;
break;
case image_data_format::rgba_pvrtc2_v2:
res = pvr_pixel_format::pvrtc_ii_2bpp;
break;
case image_data_format::rgba_pvrtc4_v2:
res = pvr_pixel_format::pvrtc_ii_4bpp;
break;
default:
E2D_ASSERT_MSG(false, "unused pixel format");
break;
}
return static_cast<u64>(res);
}
}
namespace e2d::images::impl
{
bool save_image_pvr(const image& src, buffer& dst) {
E2D_UNUSED(src, dst);
if ( is_save_image_pvr_supported(src.format()) ) {
u64 pixel_format = get_pvr_pixel_format(src.format());
pvr_header hdr;
hdr.version = 0x03525650;
hdr.flags = 0;
hdr.pixelFormat0 = pixel_format & 0x00000000'FFFFFFFFull;
hdr.pixelFormat1 = pixel_format >> 32;
hdr.colorSpace = static_cast<u32>(pvr_color_space::linear);
hdr.channelType = static_cast<u32>(pvr_channel_type::ubyte);
hdr.height = src.size().y;
hdr.width = src.size().x;
hdr.depth = 0;
hdr.numSurfaces = 1;
hdr.numFaces = 1;
hdr.mipMapCount = 0;
hdr.metaDataSize = sizeof(pvr_meta_data);
pvr_meta_data md;
size_t size = sizeof(hdr) + sizeof(md) + src.data().size();
u8 content[size];
std::memcpy(content, &hdr, sizeof(hdr));
std::memcpy(content + sizeof(hdr), &md, sizeof(md));
std::memcpy(content + sizeof(hdr) + sizeof(md), src.data().data(), src.data().size());
dst.assign(content, size);
return true;
if ( !check_save_image_pvr(src) ) {
return false;
}
return false;
pvr_header hdr;
hdr.version = make_fourcc('P', 'V', 'R', 3);
hdr.flags = static_cast<u32>(pvr_flags::none);
hdr.pixel_format0 = static_cast<u32>(
static_cast<u64>(get_pvr_pixel_format(src.format())) & 0xFFFFFFFF);
hdr.pixel_format1 = static_cast<u32>(
static_cast<u64>(get_pvr_pixel_format(src.format())) >> 32);
hdr.color_space = static_cast<u32>(pvr_color_space::linear);
hdr.channel_type = static_cast<u32>(pvr_channel_type::ubyte);
hdr.height = src.size().y;
hdr.width = src.size().x;
hdr.depth = 1;
hdr.num_surfaces = 1;
hdr.num_faces = 1;
hdr.mipmap_count = 1;
hdr.meta_data_size = 0;
buffer image_data(sizeof(pvr_header) + src.data().size());
std::memcpy(
image_data.data(),
&hdr,
sizeof(pvr_header));
std::memcpy(
image_data.data() + sizeof(pvr_header),
src.data().data(),
src.data().size());
dst.assign(std::move(image_data));
return true;
}
}

View File

@@ -42,20 +42,53 @@ namespace
}
}
bool stb_check_save_image_format(image_data_format format) noexcept {
switch ( format ) {
case image_data_format::l8:
case image_data_format::la8:
case image_data_format::rgb8:
case image_data_format::rgba8:
return true;
default:
return false;
}
}
int stb_channels_from_image_format(image_data_format format) noexcept {
switch ( format ) {
case image_data_format::g8: return 1;
case image_data_format::ga8: return 2;
case image_data_format::rgb8: return 3;
case image_data_format::l8: return 1;
case image_data_format::la8: return 2;
case image_data_format::rgb8: return 3;
case image_data_format::rgba8: return 4;
default: return 0;
default:
E2D_ASSERT_MSG(false, "unexpected image data format");
return 0;
}
}
}
namespace e2d::images::impl
{
bool check_save_image_jpg(const image& src) noexcept {
return stb_check_save_image_format(src.format());
}
bool check_save_image_png(const image& src) noexcept {
return stb_check_save_image_format(src.format());
}
bool check_save_image_tga(const image& src) noexcept {
return stb_check_save_image_format(src.format());
}
}
namespace e2d::images::impl
{
bool save_image_jpg(const image& src, buffer& dst) {
if ( !check_save_image_jpg(src) ) {
return false;
}
int img_w = math::numeric_cast<int>(src.size().x);
int img_h = math::numeric_cast<int>(src.size().y);
int img_c = stb_channels_from_image_format(src.format());
@@ -82,6 +115,10 @@ namespace e2d::images::impl
}
bool save_image_png(const image& src, buffer& dst) {
if ( !check_save_image_png(src) ) {
return false;
}
int img_w = math::numeric_cast<int>(src.size().x);
int img_h = math::numeric_cast<int>(src.size().y);
int img_c = stb_channels_from_image_format(src.format());
@@ -108,6 +145,10 @@ namespace e2d::images::impl
}
bool save_image_tga(const image& src, buffer& dst) {
if ( !check_save_image_tga(src) ) {
return false;
}
int img_w = math::numeric_cast<int>(src.size().x);
int img_h = math::numeric_cast<int>(src.size().y);
int img_c = stb_channels_from_image_format(src.format());

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:6d2cd928ace63bf0ed4f9e71007fcf435b414c8b34312f1fb5a021ac01ef9876
size 8320

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:bf310ae21482921cc00c0b962be7cc109bc28f7bab713528bd84817ebba0e68b
size 16512

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:2bee89070f7f01c6f3316e3f5faabc8db94202fc8fe6ef4eb0159add01b3304d
size 32896

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:5a04f77d73155ddaf20e137005698b7f98d859656d8f02d6d32b767c87dd5370
size 4224

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:0aaac08ab41d31e0c13a1e1b03a9919c33791af29be568912865eedee395a654
size 8320

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:fe4a3d42cb22478986d9d8342959b44fe256245be4941e7e8f3cbccc49a5ede2
size 8320

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:4c75ef2254f7315714f86097bc4b99adfff0bf54569154d028991fb65c5bba8e
size 8320

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:e69ae09fd2e5a0adcbf6301ff55d52ca562a8a93db3a96cc3fd7f61c6f197aac
size 24704

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d1c1f816df198d8d41f01455627f52fa40c55e786caff8e0fb4731aa118cb8df
size 32896

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:8e6b1c2fce3ae13125353b4334374294362c3cd44a1e2f5f523570d07d1776c0
size 32896

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:26640127a07a5b7b3e8390bb34791a6926357df07dad0944834527c288eff36e
size 8259

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1e4727acbfb5e77879b12c64480eea431c339b90017820e6debe6f71c0e3c756
size 32835

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:e0adcd804b56aa97d144636481699c49a3a8bac21713953dff72533a47a98b89
size 4163

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:b39f3c1fb9ae31b0ee8de9c1af051d90364e4c5a9887fd1631eac45ddf11c174
size 8259

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:4ebc3997990b57a35538480c384c6c7722b000c675d389b2bd4d3afd57dc5de7
size 8259

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:d9170f6956e1686f841bcbf460c589e7e0baf502a072b540b22ea19550fb5f45
size 4163

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:9b2533bb57489a6eefcf05d986eee9e97f7bf6070809748c8aece27fbc199c5a
size 4163

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:198d7a47737c3f16f5c4b9268ebc77c1ea63aa7128d335cc9e0c399b518311a0
size 4163

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:000885d3ae07adbcfc9e2ba7aee4f56e7e7d3b9f350755ec580d27ca18ed2490
size 8259

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:8cf733d37c4d52720a41786656fd57e7dc554f090824fdf48bc0788a699594e2
size 4163

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:89eb84779b83638fb0ab66e001d601ee970a9489b9f7cb1f132aca0b3ee6e8cf
size 8259

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:58063363c4966b5c7d1f1003ebcad8e8ed0b71ba0480fd763d53ff0b952ffa74
size 16451

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1322266525db79e804f569aebb9d9d68ebf8e878212b9dd92d07226d6d3ed4c5
size 2115

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:446048e908f4147dd43a55b47ed5fe8103e5d2bec24ce61b8e69e176318dfb89
size 4163

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:c51efd4f2782e37a7eb86cfe142e004868b45617b6f3d2b4646135295b422f98
size 2115

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:ef1a1165023faff98302bfa809880e5aa8243e47e337bf408b9e2eec6be31b8d
size 2115

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1b644648eaa698b47c11bc21cbb67fe3beff76fa362514c1fd48b90bde909dd0
size 4163

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:fbfe371606b6857368be8abf653a25bb7674c2b4334e9ff054c6a08fe4f81f54
size 4163

View File

@@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:6b7f41f18f51ec169a9c521cd1177fd49037b200a850aad05a923240c37d8d87
size 4163

View File

@@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:bd1c2362dc5b0b75d97ca3d5bbdf2cf647b25a5b2f8834a9005e8c9c0a70e82c
size 8259

View File

@@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7a62a7760e33b7130539c3b6a8b7c287ccdd11edb9dd273be3c1e6ec7b81b47e
size 2115

View File

@@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:6a9b1a7f97b26ff7df0a7275f786e029dbc45aab796247f8063c820140933685
size 2115

View File

@@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:69e2bab57471fbde23d432f77270678e0a28cf809eb079fbb39a61e68abe4059
size 4163

View File

@@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:f730702b1075c5a0ef929272106275debf12dbce31a59664038545b5b91d1545
size 4163

View File

@@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:7fcf6cb866364b0a8b31bea7407a40aa2dcda1af4f618604aec2249e2e0cc588
size 2115

View File

@@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:1a928929dc7532d84072e5668e5098b17289fdea1d36bff3ae48fa232294f770
size 4163

View File

@@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:e4f93405f3302067a0a1bfc05be48c20ef2e12b649d722574497f6dbd64c3b61
size 8259

View File

@@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:958b8490c189abdebde1f75e8eacbcb8e6ec06dac34baed545e383639e3288d3
size 16451

View File

@@ -1,3 +0,0 @@
version https://git-lfs.github.com/spec/v1
oid sha256:07b0e533a85a8eef51e9baf6ebca2f5a451f74e712557ef315fd1952dcaef307
size 8320

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:332aff6826f8eb97cf3420112aa6a42655c5e284b46acd2caa80d5dfe4404e46
size 5438

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:90a41d4bedcd8d388eee4a12c168b1364bd17742c817b2ad532d799731505164
size 11620

View File

@@ -0,0 +1,3 @@
version https://git-lfs.github.com/spec/v1
oid sha256:823bcbe99c6914db817de30b1a6dd0a64418703462a72c5136ad767bcef50c13
size 17842

View File

@@ -198,7 +198,7 @@ TEST_CASE("render"){
REQUIRE_NOTHROW(r.update_texture(tex, src, b2u(0, 0, 128, 128)));
}
{
texture_ptr tex = r.create_texture(v2u(128,128), pixel_declaration::pixel_type::g8);
texture_ptr tex = r.create_texture(v2u(128,128), pixel_declaration::pixel_type::l8);
REQUIRE(tex != nullptr);
buffer src;
@@ -251,7 +251,7 @@ TEST_CASE("render"){
c = rand() % 255;
}
image img(v2u(31, 44), image_data_format::ga8, src);
image img(v2u(31, 44), image_data_format::la8, src);
REQUIRE_THROWS_AS(
r.update_texture(tex, img, v2u(11,27)),
bad_render_operation);

View File

@@ -8,219 +8,356 @@
using namespace e2d;
TEST_CASE("images") {
{
image i;
REQUIRE(i.size() == v2u::zero());
REQUIRE(i.format() == image_data_format::rgba8);
REQUIRE(i.data().empty());
REQUIRE(i.empty());
SECTION("image") {
{
image i;
REQUIRE(i.size() == v2u::zero());
REQUIRE(i.format() == image_data_format::rgba8);
REQUIRE(i.data().empty());
REQUIRE(i.empty());
}
{
const u8 img[] = {1,2,3,4,5,6,7,8};
image i0(v2u(2,2), image_data_format::a8, {img,4});
image i1(v2u(2,2), image_data_format::l8, {img,4});
image i2(v2u(2,1), image_data_format::la8, {img,4});
image i3(v2u(1,2), image_data_format::la8, {img,4});
image i4(v2u(2,1), image_data_format::rgb8, {img,6});
image i5(v2u(1,2), image_data_format::rgb8, {img,6});
image i6(v2u(2,1), image_data_format::rgba8, {img,8});
image i7(v2u(1,2), image_data_format::rgba8, {img,8});
REQUIRE(i0.format() == image_data_format::a8);
REQUIRE(i1.format() == image_data_format::l8);
REQUIRE(i2.format() == image_data_format::la8);
REQUIRE(i3.format() == image_data_format::la8);
REQUIRE(i4.format() == image_data_format::rgb8);
REQUIRE(i5.format() == image_data_format::rgb8);
REQUIRE(i6.format() == image_data_format::rgba8);
REQUIRE(i7.format() == image_data_format::rgba8);
}
}
{
const u8 img[] = {1,2,3,4,5,6,7,8};
image i0(v2u(2,2), image_data_format::g8, {img,4});
image i1(v2u(2,1), image_data_format::ga8, {img,4});
image i2(v2u(1,2), image_data_format::ga8, {img,4});
image i3(v2u(2,1), image_data_format::rgb8, {img,6});
image i4(v2u(1,2), image_data_format::rgb8, {img,6});
image i5(v2u(2,1), image_data_format::rgba8, {img,8});
image i6(v2u(1,2), image_data_format::rgba8, {img,8});
REQUIRE(i0.format() == image_data_format::g8);
REQUIRE(i1.format() == image_data_format::ga8);
REQUIRE(i2.format() == image_data_format::ga8);
REQUIRE(i3.format() == image_data_format::rgb8);
REQUIRE(i4.format() == image_data_format::rgb8);
REQUIRE(i5.format() == image_data_format::rgba8);
REQUIRE(i6.format() == image_data_format::rgba8);
SECTION("stb") {
{
REQUIRE(filesystem::remove_file("image_save_test.jpg"));
REQUIRE(filesystem::remove_file("image_save_test.png"));
REQUIRE(filesystem::remove_file("image_save_test.tga"));
const u8 img_data[] = {255,0,0, 0,255,0, 0,0,255};
image img(v2u(3,1), image_data_format::rgb8, buffer(img_data, sizeof(img_data)));
REQUIRE(images::try_save_image(
img,
image_file_format::jpg,
make_write_file("image_save_test.jpg", false)));
REQUIRE(filesystem::file_exists("image_save_test.jpg"));
REQUIRE(images::try_save_image(
img,
image_file_format::png,
make_write_file("image_save_test.png", false)));
REQUIRE(filesystem::file_exists("image_save_test.png"));
REQUIRE(images::try_save_image(
img,
image_file_format::tga,
make_write_file("image_save_test.tga", false)));
REQUIRE(filesystem::file_exists("image_save_test.tga"));
}
{
image img;
REQUIRE(images::try_load_image(img, make_read_file("image_save_test.jpg")));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 10u));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 10u));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 10u));
REQUIRE(images::try_load_image(img, make_read_file("image_save_test.png")));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0u));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0u));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0u));
REQUIRE(images::try_load_image(img, make_read_file("image_save_test.tga")));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0u));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0u));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0u));
}
{
REQUIRE(filesystem::remove_file("image_save_test.jpg"));
REQUIRE(filesystem::remove_file("image_save_test.png"));
REQUIRE(filesystem::remove_file("image_save_test.tga"));
const u8 img_data[] = {255,0,0, 0,255,0, 0,0,255};
image img(v2u(3,1), image_data_format::rgb8, buffer(img_data, sizeof(img_data)));
buffer buf;
REQUIRE(images::try_save_image(
img,
image_file_format::jpg,
buf));
REQUIRE(filesystem::try_write_all(buf, "image_save_test.jpg", false));
REQUIRE(images::try_save_image(
img,
image_file_format::png,
buf));
REQUIRE(filesystem::try_write_all(buf, "image_save_test.png", false));
REQUIRE(images::try_save_image(
img,
image_file_format::tga,
buf));
REQUIRE(filesystem::try_write_all(buf, "image_save_test.tga", false));
}
{
image img;
buffer buf;
REQUIRE(filesystem::try_read_all(buf, "image_save_test.jpg"));
REQUIRE(images::try_load_image(img, buf));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 10));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 10));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 10));
REQUIRE(filesystem::try_read_all(buf, "image_save_test.png"));
REQUIRE(images::try_load_image(img, buf));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0));
REQUIRE(filesystem::try_read_all(buf, "image_save_test.tga"));
REQUIRE(images::try_load_image(img, buf));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0));
}
}
{
REQUIRE(filesystem::remove_file("image_save_test.jpg"));
REQUIRE(filesystem::remove_file("image_save_test.png"));
REQUIRE(filesystem::remove_file("image_save_test.tga"));
const u8 img_data[] = {255,0,0, 0,255,0, 0,0,255};
image img(v2u(3,1), image_data_format::rgb8, buffer(img_data, sizeof(img_data)));
REQUIRE(images::try_save_image(
img,
image_file_format::jpg,
make_write_file("image_save_test.jpg", false)));
REQUIRE(filesystem::file_exists("image_save_test.jpg"));
REQUIRE(images::try_save_image(
img,
image_file_format::png,
make_write_file("image_save_test.png", false)));
REQUIRE(filesystem::file_exists("image_save_test.png"));
REQUIRE(images::try_save_image(
img,
image_file_format::tga,
make_write_file("image_save_test.tga", false)));
REQUIRE(filesystem::file_exists("image_save_test.tga"));
REQUIRE(images::try_save_image(
img,
image_file_format::pvr,
make_write_file("image_save_test.pvr", false)));
REQUIRE(filesystem::file_exists("image_save_test.pvr"));
REQUIRE(images::try_save_image(
img,
image_file_format::dds,
make_write_file("image_save_test.dds", false)));
REQUIRE(filesystem::file_exists("image_save_test.dds"));
}
{
image img;
REQUIRE(images::try_load_image(img, make_read_file("image_save_test.jpg")));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 10u));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 10u));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 10u));
str resources;
REQUIRE(filesystem::extract_predef_path(
resources,
filesystem::predef_path::resources));
REQUIRE(images::try_load_image(img, make_read_file("image_save_test.png")));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0u));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0u));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0u));
REQUIRE(images::try_load_image(img, make_read_file("image_save_test.tga")));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0u));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0u));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0u));
REQUIRE(images::try_load_image(img, make_read_file("image_save_test.pvr")));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0u));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0u));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0u));
REQUIRE(images::try_load_image(img, make_read_file("image_save_test.dds")));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0u));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0u));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0u));
}
{
REQUIRE(filesystem::remove_file("image_save_test.jpg"));
REQUIRE(filesystem::remove_file("image_save_test.png"));
REQUIRE(filesystem::remove_file("image_save_test.tga"));
REQUIRE(filesystem::remove_file("image_save_test.pvr"));
REQUIRE(filesystem::remove_file("image_save_test.dds"));
const u8 img_data[] = {255,0,0, 0,255,0, 0,0,255};
image img(v2u(3,1), image_data_format::rgb8, buffer(img_data, sizeof(img_data)));
buffer buf;
REQUIRE(images::try_save_image(
img,
image_file_format::jpg,
buf));
REQUIRE(filesystem::try_write_all(buf, "image_save_test.jpg", false));
REQUIRE(images::try_save_image(
img,
image_file_format::png,
buf));
REQUIRE(filesystem::try_write_all(buf, "image_save_test.png", false));
REQUIRE(images::try_save_image(
img,
image_file_format::tga,
buf));
REQUIRE(filesystem::try_write_all(buf, "image_save_test.tga", false));
REQUIRE(images::try_save_image(
img,
image_file_format::pvr,
buf));
REQUIRE(filesystem::try_write_all(buf, "image_save_test.pvr", false));
REQUIRE(images::try_save_image(
img,
image_file_format::dds,
buf));
REQUIRE(filesystem::try_write_all(buf, "image_save_test.dds", false));
}
{
image img;
buffer buf;
REQUIRE(filesystem::try_read_all(buf, "image_save_test.jpg"));
REQUIRE(images::try_load_image(img, buf));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 10));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 10));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 10));
REQUIRE(filesystem::try_read_all(buf, "image_save_test.png"));
REQUIRE(images::try_load_image(img, buf));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0));
REQUIRE(filesystem::try_read_all(buf, "image_save_test.tga"));
REQUIRE(images::try_load_image(img, buf));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0));
REQUIRE(filesystem::try_read_all(buf, "image_save_test.pvr"));
REQUIRE(images::try_load_image(img, buf));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0));
REQUIRE(filesystem::try_read_all(buf, "image_save_test.dds"));
REQUIRE(images::try_load_image(img, buf));
REQUIRE(img.size() == v2u(3,1));
REQUIRE(img.format() == image_data_format::rgb8);
REQUIRE(math::approximately(img.pixel32(0,0), color32::red(), 0));
REQUIRE(math::approximately(img.pixel32(1,0), color32::green(), 0));
REQUIRE(math::approximately(img.pixel32(2,0), color32::blue(), 0));
}
{
SECTION("stb") {
struct img_info {
const char* name;
const char* path;
std::size_t size;
image_data_format format;
};
const img_info test_infos[] = {
{"bin/images/stb/ship.jpg", 64 * 128 * 3, image_data_format::rgb8},
{"bin/images/stb/ship.png", 64 * 128 * 4, image_data_format::rgba8},
{"bin/images/stb/ship.tga", 64 * 128 * 4, image_data_format::rgba8}};
for ( const auto& info : test_infos ) {
input_stream_uptr stream = make_read_file(path::combine(resources, info.path));
REQUIRE(stream);
image img;
REQUIRE(images::try_load_image(img, stream));
REQUIRE(img.format() == info.format);
REQUIRE(img.data().size() == info.size);
REQUIRE(img.size().x == 64);
REQUIRE(img.size().y == 128);
}
}
SECTION("dds") {
struct img_info {
const char* path;
std::size_t size;
bool can_load;
image_data_format format;
};
const img_info test_images[] = {
{"bin/images/ship_pvrtc_2bpp_rgba.pvr", true, image_data_format::rgba_pvrtc2},
{"bin/images/ship_pvrtc_2bpp_rgb.pvr", true, image_data_format::rgb_pvrtc2},
{"bin/images/ship_pvrtc_4bpp_rgba.pvr", true, image_data_format::rgba_pvrtc4},
{"bin/images/ship_pvrtc_4bpp_rgb.pvr", true, image_data_format::rgb_pvrtc4},
{"bin/images/ship_pvrtc_ii_2bpp.pvr", true, image_data_format::rgba_pvrtc2_v2},
{"bin/images/ship_pvrtc_ii_4bpp.pvr", true, image_data_format::rgba_pvrtc4_v2},
{"bin/images/ship_etc1.pvr", false, image_data_format(-1)},
{"bin/images/ship_etc2.pvr", false, image_data_format(-1)},
{"bin/images/ship_eac_rg11.pvr", false, image_data_format(-1)},
{"bin/images/ship_rgba8.pvr", true, image_data_format::rgba8},
{"bin/images/ship_rgba.dds", true, image_data_format::rgba_dxt5},
{"bin/images/ship_r8.pvr", true, image_data_format::g8},
{"bin/images/ship_rg8.pvr", true, image_data_format::ga8},
{"bin/images/ship_rgb8.pvr", true, image_data_format::rgb8}
const img_info test_infos[] = {
{"bin/images/dds/ship_a8.dds", 64 * 128 * 1, true, image_data_format::a8},
{"bin/images/dds/ship_l8.dds", 64 * 128 * 1, true, image_data_format::l8},
{"bin/images/dds/ship_a8l8.dds", 64 * 128 * 2, true, image_data_format::la8},
{"bin/images/dds/ship_r8g8b8.dds", 64 * 128 * 3, true, image_data_format::rgb8},
{"bin/images/dds/ship_x8r8g8b8.dds", 64 * 128 * 3, true, image_data_format::rgb8},
{"bin/images/dds/ship_rgba8.dds", 64 * 128 * 4, true, image_data_format::rgba8},
{"bin/images/dds/ship_a8r8g8b8.dds", 64 * 128 * 4, true, image_data_format::rgba8},
{"bin/images/dds/ship_dxt1.dds", 64 * 128 * 4 / 8, true, image_data_format::rgba_dxt1},
{"bin/images/dds/ship_dxt3.dds", 64 * 128 * 8 / 8, true, image_data_format::rgba_dxt3},
{"bin/images/dds/ship_dxt5.dds", 64 * 128 * 8 / 8, true, image_data_format::rgba_dxt5},
};
str resources;
REQUIRE(filesystem::extract_predef_path(
resources,
filesystem::predef_path::resources));
for ( const auto& info : test_infos ) {
CAPTURE(info.path);
for ( const auto& info : test_images ) {
input_stream_uptr stream = make_read_file(path::combine(resources, info.name));
REQUIRE(stream);
image img;
REQUIRE(images::try_load_image(img, stream) == info.can_load);
REQUIRE(info.can_load == images::try_load_image(
img,
make_read_file(path::combine(resources, info.path))));
if ( info.can_load ) {
REQUIRE(img.format() == info.format);
REQUIRE(img.data().size() == info.size);
REQUIRE(img.size().x == 64);
REQUIRE(img.size().y == 128);
REQUIRE(img.data().size() > 0);
}
}
for ( const auto& info : test_infos ) {
CAPTURE(info.path);
if ( !info.can_load ) {
continue;
}
image img;
REQUIRE(images::try_load_image(
img,
make_read_file(path::combine(resources, info.path))));
REQUIRE(filesystem::remove_file("image_save_test.dds"));
REQUIRE(images::try_save_image(
img,
image_file_format::dds, make_write_file("image_save_test.dds", false)));
image img2;
REQUIRE(images::try_load_image(
img2,
make_read_file("image_save_test.dds")));
REQUIRE(img == img2);
}
}
SECTION("dds_bgr") {
{
image img;
REQUIRE(images::try_load_image(
img,
make_read_file(path::combine(resources, "bin/images/dds/ship_r8g8b8.dds"))));
image img2;
REQUIRE(images::try_load_image(
img2,
make_read_file(path::combine(resources, "bin/images/dds/ship_r8g8b8.dds"))));
image img3;
REQUIRE(images::try_load_image(
img3,
make_read_file(path::combine(resources, "bin/images/dds/ship_x8r8g8b8.dds"))));
REQUIRE(img == img2);
REQUIRE(img2 == img3);
}
{
image img;
REQUIRE(images::try_load_image(
img,
make_read_file(path::combine(resources, "bin/images/dds/ship_rgba8.dds"))));
image img2;
REQUIRE(images::try_load_image(
img2,
make_read_file(path::combine(resources, "bin/images/dds/ship_a8r8g8b8.dds"))));
REQUIRE(img == img2);
}
}
SECTION("pvr") {
struct img_info {
const char* path;
std::size_t size;
bool can_load;
image_data_format format;
};
const img_info test_infos[] = {
{"bin/images/pvr/ship_a8.pvr", 64 * 128 * 1, true, image_data_format::a8},
{"bin/images/pvr/ship_l8.pvr", 64 * 128 * 1, true, image_data_format::l8},
{"bin/images/pvr/ship_la8.pvr", 64 * 128 * 2, true, image_data_format::la8},
{"bin/images/pvr/ship_rgb8.pvr", 64 * 128 * 3, true, image_data_format::rgb8},
{"bin/images/pvr/ship_rgba8.pvr", 64 * 128 * 4, true, image_data_format::rgba8},
{"bin/images/pvr/ship_bgra8.pvr", 64 * 128 * 4, true, image_data_format::rgba8},
{"bin/images/pvr/ship_dxt1.pvr", 64 * 128 * 4 / 8, true, image_data_format::rgba_dxt1},
{"bin/images/pvr/ship_dxt3.pvr", 64 * 128 * 8 / 8, true, image_data_format::rgba_dxt3},
{"bin/images/pvr/ship_dxt5.pvr", 64 * 128 * 8 / 8, true, image_data_format::rgba_dxt5},
{"bin/images/pvr/ship_pvrtc_2bpp_rgb.pvr", 64 * 128 * 2 / 8, true, image_data_format::rgb_pvrtc2},
{"bin/images/pvr/ship_pvrtc_2bpp_rgba.pvr", 64 * 128 * 2 / 8, true, image_data_format::rgba_pvrtc2},
{"bin/images/pvr/ship_pvrtc_4bpp_rgb.pvr", 64 * 128 * 4 / 8, true, image_data_format::rgb_pvrtc4},
{"bin/images/pvr/ship_pvrtc_4bpp_rgba.pvr", 64 * 128 * 4 / 8, true, image_data_format::rgba_pvrtc4},
{"bin/images/pvr/ship_pvrtc2_2bpp.pvr", 64 * 128 * 2 / 8, true, image_data_format::rgba_pvrtc2_v2},
{"bin/images/pvr/ship_pvrtc2_4bpp.pvr", 64 * 128 * 4 / 8, true, image_data_format::rgba_pvrtc4_v2},
{"bin/images/pvr/ship_etc1.pvr", 64 * 128 * 4 / 8, false, image_data_format(-1)},
{"bin/images/pvr/ship_etc2_rgb.pvr", 64 * 128 * 4 / 8, false, image_data_format(-1)},
{"bin/images/pvr/ship_etc2_rgba.pvr", 64 * 128 * 8 / 8, false, image_data_format(-1)},
{"bin/images/pvr/ship_etc2_rgba1.pvr", 64 * 128 * 4 / 8, false, image_data_format(-1)},
{"bin/images/pvr/ship_eac_r11.pvr", 0, false, image_data_format(-1)},
{"bin/images/pvr/ship_eac_rg11.pvr", 0, false, image_data_format(-1)},
};
for ( const auto& info : test_infos ) {
CAPTURE(info.path);
image img;
REQUIRE(info.can_load == images::try_load_image(
img,
make_read_file(path::combine(resources, info.path))));
if ( info.can_load ) {
REQUIRE(img.format() == info.format);
REQUIRE(img.data().size() == info.size);
REQUIRE(img.size().x == 64);
REQUIRE(img.size().y == 128);
}
}
for ( const auto& info : test_infos ) {
CAPTURE(info.path);
if ( !info.can_load ) {
continue;
}
image img;
REQUIRE(images::try_load_image(
img,
make_read_file(path::combine(resources, info.path))));
REQUIRE(filesystem::remove_file("image_save_test.pvr"));
REQUIRE(images::try_save_image(
img,
image_file_format::pvr, make_write_file("image_save_test.pvr", false)));
image img2;
REQUIRE(images::try_load_image(
img2,
make_read_file("image_save_test.pvr")));
REQUIRE(img == img2);
}
}
SECTION("pvr_bgr") {
{
image img;
REQUIRE(images::try_load_image(
img,
make_read_file(path::combine(resources, "bin/images/pvr/ship_rgba8.pvr"))));
image img2;
REQUIRE(images::try_load_image(
img2,
make_read_file(path::combine(resources, "bin/images/pvr/ship_bgra8.pvr"))));
REQUIRE(img == img2);
}
}
}