mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 04:44:11 +02:00
a few step further
This commit is contained in:
parent
07d1daaa9b
commit
ce68be2a59
3 changed files with 103 additions and 40 deletions
|
@ -1,28 +1,45 @@
|
|||
#include "Image.h"
|
||||
|
||||
|
||||
int image::Pixel::cpp { 0 };
|
||||
int image::BC::cpp { 0 };
|
||||
|
||||
namespace image {
|
||||
|
||||
template <> void compress(const PB_RGB32& src, CB_8& dst) {
|
||||
namespace pixel {
|
||||
template <> const RGB16_565 mix(const RGB16_565 p0, const RGB16_565 p1, const Byte alpha) {
|
||||
return RGB16_565(
|
||||
mix5_4(p0.r, p1.r, alpha),
|
||||
mix6_4(p0.g, p1.g, alpha),
|
||||
mix5_4(p0.b, p1.b, alpha));
|
||||
}
|
||||
}
|
||||
|
||||
template <> void compress(const PB_RGB32& src, CB_BC1& dst) {
|
||||
|
||||
for (auto& b : dst.bytes) {
|
||||
b = 12;
|
||||
}
|
||||
}
|
||||
|
||||
template <> void uncompress(const CB_8& src, PB_RGB32& dst) {
|
||||
for (auto& b : dst.bytes) {
|
||||
b = 12;
|
||||
template <> void uncompress(const CB_BC1& src, PB_RGB32& dst) {
|
||||
auto bc1 = src.bc;
|
||||
auto c0 = bc1.color0;
|
||||
auto c1 = bc1.color1;
|
||||
|
||||
for (auto& p : dst.pixels) {
|
||||
auto r = pixel::mix(
|
||||
c0,
|
||||
c1,
|
||||
(pixel::Byte)bc1.table);
|
||||
///p.val =
|
||||
}
|
||||
}
|
||||
|
||||
template <> void compress(const PB_RGBA32& src, CB_8& dst) {
|
||||
template <> void compress(const PB_RGBA32& src, CB_BC4& dst) {
|
||||
|
||||
}
|
||||
|
||||
template <> void uncompress(const CB_8& src, PB_RGBA32& dst) {
|
||||
template <> void uncompress(const CB_BC4& src, PB_RGBA32& dst) {
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@
|
|||
|
||||
namespace image {
|
||||
|
||||
class Pixel {
|
||||
public:
|
||||
namespace pixel {
|
||||
|
||||
using Byte = uint8_t;
|
||||
using Byte2 = uint16_t;
|
||||
using Byte4 = uint32_t;
|
||||
|
@ -16,19 +16,22 @@ namespace image {
|
|||
static const Byte BLACK8 { 0 };
|
||||
static const Byte WHITE8 { 255 };
|
||||
|
||||
|
||||
template <int N> int bitVal() { return 1 << N; }
|
||||
template <int Tn, int An> int bitProduct() { return bitVal<Tn>() * bitVal<An>(); }
|
||||
template <int Tn, int An, typename T = Byte, typename A = T> T mix(const T x, const T y, const A a) { return T(((bitVal<An>() - a) * x + a * y) / bitProduct<Tn, An>()); }
|
||||
|
||||
struct RGB32 {
|
||||
Byte r { BLACK };
|
||||
Byte g { BLACK };
|
||||
Byte b { BLACK };
|
||||
Byte x { WHITE };
|
||||
Byte r { BLACK8 };
|
||||
Byte g { BLACK8 };
|
||||
Byte b { BLACK8 };
|
||||
Byte x { WHITE8 };
|
||||
};
|
||||
|
||||
struct RGBA32 {
|
||||
Byte r { BLACK };
|
||||
Byte g { BLACK };
|
||||
Byte b { BLACK };
|
||||
Byte a { WHITE };
|
||||
Byte r { BLACK8 };
|
||||
Byte g { BLACK8 };
|
||||
Byte b { BLACK8 };
|
||||
Byte a { WHITE8 };
|
||||
};
|
||||
|
||||
|
||||
|
@ -36,17 +39,48 @@ namespace image {
|
|||
Byte2 b : 5;
|
||||
Byte2 g : 6;
|
||||
Byte2 r : 5;
|
||||
|
||||
RGB16_565() : b(BLACK8), g(BLACK8), r(BLACK8) {}
|
||||
RGB16_565(Byte pR, Byte pG, Byte pB) : b(pB), g(pG), r(pR) {}
|
||||
};
|
||||
|
||||
|
||||
Byte mix5_4(const Byte x, const Byte y, const Byte a) { return mix<5, 4>(x, y, a); }
|
||||
Byte mix6_4(const Byte x, const Byte y, const Byte a) { return mix<6, 4>(x, y, a); }
|
||||
|
||||
|
||||
template <typename P, typename S> const P mix(const P p0, const P p1, const S alpha) { return p0; }
|
||||
|
||||
template <> const RGB16_565 mix(const RGB16_565 p0, const RGB16_565 p1, const Byte alpha);
|
||||
|
||||
|
||||
static int cpp;
|
||||
};
|
||||
|
||||
template <typename F, typename S> class Pixel {
|
||||
public:
|
||||
using Format = F;
|
||||
using Storage = S;
|
||||
|
||||
union {
|
||||
Format val;
|
||||
Storage raw;
|
||||
};
|
||||
|
||||
Pixel() {};
|
||||
Pixel(Format v) : val(v) {}
|
||||
Pixel(Storage s) : raw(s) {}
|
||||
};
|
||||
|
||||
using PixRGB565 = Pixel<pixel::RGB16_565, pixel::Byte2>;
|
||||
using PixRGB32 = Pixel<pixel::RGB32, pixel::Byte4>;
|
||||
using PixRGBA32 = Pixel<pixel::RGBA32, pixel::Byte4>;
|
||||
|
||||
template <typename P> class PixelBlock {
|
||||
public:
|
||||
static const uint32_t WIDTH { 4 };
|
||||
static const uint32_t HEIGHT { WIDTH };
|
||||
static const uint32_t SIZE { WIDTH * HEIGHT };
|
||||
uint32_t getByteSize() const { return SIZE * sizeof(P); }
|
||||
uint32_t getSize() const { return SIZE * sizeof(P); }
|
||||
|
||||
P pixels[SIZE];
|
||||
|
||||
|
@ -58,38 +92,50 @@ namespace image {
|
|||
}
|
||||
|
||||
void setPixels(const P* srcPixels) {
|
||||
memcpy(pixels, srcPixels, getByteSize());
|
||||
memcpy(pixels, srcPixels, getSize());
|
||||
}
|
||||
};
|
||||
|
||||
template <int Size> class CompressedBlock {
|
||||
public:
|
||||
uint8_t bytes[Size];
|
||||
};
|
||||
|
||||
class BC {
|
||||
public:
|
||||
static int cpp;
|
||||
|
||||
struct BC1 {
|
||||
Pixel::RGB16_565 color_0;
|
||||
Pixel::RGB16_565 color_1;
|
||||
Pixel::Byte4 table;
|
||||
PixRGB565 color0;
|
||||
PixRGB565 color1;
|
||||
pixel::Byte4 table;
|
||||
};
|
||||
struct BC4 {
|
||||
PixRGB565 color0;
|
||||
PixRGB565 color1;
|
||||
pixel::Byte4 table;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
||||
template <typename T> class CompressedBlock {
|
||||
public:
|
||||
static const uint32_t SIZE { sizeof(T) };
|
||||
union {
|
||||
pixel::Byte bytes[SIZE];
|
||||
T bc;
|
||||
};
|
||||
|
||||
CompressedBlock() {}
|
||||
};
|
||||
|
||||
template <typename PB, typename CB> void compress(const PB& srcBlock, CB& dstBlock) { }
|
||||
template <typename PB, typename CB> void uncompress(const CB& srcBlock, PB& dstBlock) { }
|
||||
|
||||
|
||||
using PB_RGB32 = PixelBlock<Pixel::RGB32>;
|
||||
using PB_RGBA32 = PixelBlock<Pixel::RGBA32>;
|
||||
using PB_RGB32 = PixelBlock<PixRGB32>;
|
||||
using PB_RGBA32 = PixelBlock<PixRGBA32>;
|
||||
|
||||
using CB_8 = CompressedBlock<8>;
|
||||
using CB_16 = CompressedBlock<16>;
|
||||
using CB_BC1 = CompressedBlock<BC::BC1>;
|
||||
using CB_BC4 = CompressedBlock<BC::BC4>;
|
||||
|
||||
template <> void compress(const PB_RGB32& src, CB_8& dst);
|
||||
template <> void compress(const PB_RGBA32& src, CB_8& dst);
|
||||
template <> void compress(const PB_RGB32& src, CB_BC1& dst);
|
||||
template <> void compress(const PB_RGBA32& src, CB_BC4& dst);
|
||||
|
||||
template <> void uncompress(const CB_8& src, PB_RGB32& dst);
|
||||
template <> void uncompress(const CB_8& src, PB_RGBA32& dst);
|
||||
template <> void uncompress(const CB_BC1& src, PB_RGB32& dst);
|
||||
template <> void uncompress(const CB_BC4& src, PB_RGBA32& dst);
|
||||
}
|
|
@ -202,7 +202,7 @@ gpu::Texture* TextureUsage::process2DTextureColorFromImage(const QImage& srcImag
|
|||
theTexture->assignStoredMip(0, formatMip, image.byteCount(), image.constBits());
|
||||
|
||||
image::PB_RGB32 pb;
|
||||
image::CB_8 cb;
|
||||
image::CB_BC1 cb;
|
||||
|
||||
image::compress(pb, cb);
|
||||
|
||||
|
|
Loading…
Reference in a new issue