Saving state before merge up

This commit is contained in:
samcake 2016-11-04 22:56:18 -07:00
parent f077b9a6c0
commit 41109553eb
3 changed files with 118 additions and 54 deletions

View file

@ -32,7 +32,7 @@ template <> void uncompress(const CB_BC1& src, PB_RGB32& dst) {
auto r = pixel::mix(
c0,
c1,
(pixel::Byte)bc1.table);
(Byte)bc1.table);
}
}

View file

@ -2,6 +2,8 @@
Let s try compressing images
*/
#include <glm/glm.hpp>
#include "Forward.h"
@ -11,6 +13,20 @@ namespace image {
using Byte = uint8_t;
using Byte2 = uint16_t;
using Byte4 = uint32_t;
using Byte8 = uint64_t;
// Storage type selector based on size (sizeof of a type)
template<int size, typename T1 = Byte, typename T2 = Byte2, typename T4 = Byte4, typename T8 = Byte8>
struct storage { typedef T1 type; };
template<typename T1, typename T2, typename T4, typename T8>
struct storage<2, T1, T2, T4, T8> { typedef T2 type; };
template<typename T1, typename T2, typename T4, typename T8>
struct storage<4, T1, T2, T4, T8> { typedef T4 type; };
template<typename T1, typename T2, typename T4, typename T8>
struct storage<8, T1, T2, T4, T8> { typedef T8 type; };
static const Byte BLACK8 { 0 };
static const Byte WHITE8 { 255 };
@ -25,7 +41,6 @@ namespace image {
namespace pixel {
struct RGB32 {
Byte r { BLACK8 };
Byte g { BLACK8 };
@ -40,7 +55,6 @@ namespace image {
Byte a { WHITE8 };
};
struct RGB16_565 {
Byte2 b : 5;
Byte2 g : 6;
@ -49,58 +63,66 @@ namespace image {
RGB16_565() : b(BLACK8), g(BLACK8), r(BLACK8) {}
RGB16_565(Byte pR, Byte pG, Byte pB) : b(pB), g(pG), r(pR) {}
};
struct R8 {
Byte r { BLACK8 };
};
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);
};
template <typename F, typename S> class Pixel {
public:
using Format = F;
using Storage = S;
union {
Storage raw;
Format val{ Format() }; // Format last to be initialized by Format's default constructor
template <typename F, typename S = typename storage<sizeof(F)>::type > class Pixel {
public:
using Format = F;
using Storage = S;
union {
Storage raw;
Format val{ Format() }; // Format last to be initialized by Format's default constructor
};
Pixel() {};
Pixel(Format v) : val(v) {}
Pixel(Storage s) : raw(s) {}
};
Pixel() {};
Pixel(Format v) : val(v) {}
Pixel(Storage s) : raw(s) {}
template <typename P> class PixelBlock {
public:
using Format = typename P::Format;
using Storage = typename P::Storage;
static const uint32_t WIDTH { 4 };
static const uint32_t HEIGHT { WIDTH };
static const uint32_t SIZE { WIDTH * HEIGHT };
uint32_t getSize() const { return SIZE * sizeof(P); }
P pixels[SIZE];
PixelBlock() {}
PixelBlock(const P* srcPixels) {
setPixels(srcPixels);
}
void setPixels(const P* srcPixels) {
memcpy(pixels, srcPixels, getSize());
}
const Storage* getStorage() const { return static_cast<const Storage*> (&pixels->raw); }
};
};
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:
using Format = typename P::Format;
using Storage = typename P::Storage;
static const uint32_t WIDTH { 4 };
static const uint32_t HEIGHT { WIDTH };
static const uint32_t SIZE { WIDTH * HEIGHT };
uint32_t getSize() const { return SIZE * sizeof(P); }
P pixels[SIZE];
PixelBlock() {}
PixelBlock(const P* srcPixels) {
setPixels(srcPixels);
}
void setPixels(const P* srcPixels) {
memcpy(pixels, srcPixels, getSize());
}
const Storage* getStorage() const { return static_cast<const Storage*> (&pixels->raw); }
};
using PixRGB565 = pixel::Pixel<pixel::RGB16_565>;
using PixRGB32 = pixel::Pixel<pixel::RGB32>;
using PixRGBA32 = pixel::Pixel<pixel::RGBA32>;
using PixR8 = pixel::Pixel<pixel::R8>;
class BC {
public:
@ -109,15 +131,13 @@ namespace image {
struct BC1 {
PixRGB565 color0;
PixRGB565 color1;
pixel::Byte4 table;
Byte
Byte4 table;
};
struct BC4 {
PixRGB565 color0;
PixRGB565 color1;
pixel::Byte4 table;
Byte4 table;
};
};
@ -125,19 +145,20 @@ namespace image {
public:
static const uint32_t SIZE { sizeof(T) };
union {
pixel::Byte bytes[SIZE];
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<PixRGB32>;
using PB_RGBA32 = PixelBlock<PixRGBA32>;
using PB_RGB32 = pixel::PixelBlock<PixRGB32>;
using PB_RGBA32 = pixel::PixelBlock<PixRGBA32>;
using CB_BC1 = CompressedBlock<BC::BC1>;
using CB_BC4 = CompressedBlock<BC::BC4>;
@ -147,4 +168,44 @@ namespace image {
template <> void uncompress(const CB_BC1& src, PB_RGB32& dst);
template <> void uncompress(const CB_BC4& src, PB_RGBA32& dst);
template <typename P, typename B>
class PixelBlockArray {
public:
using Pixel = P;
using Block = pixel::PixelBlock<Pixel>;
using Blocks = std::vector<Block>;
};
class Grid {
public:
using Coord = uint16_t;
using Coord2 = glm::u16vec2;
using Size = uint32_t;
Coord2 _block { 1, 1 };
Coord2 _surface { 1, 1 };
Coord width() const { return _surface.x; }
Coord height() const { return _surface.y; }
Size size() const { return width() * height(); }
Coord blockWidth() const { return _block.x; }
Coord blockHeight() const { return _block.y; }
Size blockSize() const { return blockWidth() * blockHeight(); }
Coord pixelWidth() const { return _surface.x * _block.x; }
Coordisd pixelHeight() const { return _surface.y * _block.y; }
Size pixelSize() const { return pixelWidth() * pixelHeight(); }
Coord2 pixelToBlock(const Coord2& coord) const {
auto blockX = coord.x / blockWidth();
auto blockY = coord.y / blockHeight();
return Coord2(blockX, blockY);
}
};
}

View file

@ -252,6 +252,8 @@ gpu::Texture* TextureUsage::process2DTextureColorFromImage(const QImage& srcImag
image::PixRGBA32 pix1;
image::PixR8 pix2;
image::PixRGB565 pix3;
image::PB_RGB32 pb0;
@ -260,6 +262,7 @@ gpu::Texture* TextureUsage::process2DTextureColorFromImage(const QImage& srcImag
auto pix0_s = sizeof(pix0);
auto pix1_s = sizeof(pix1);
auto pix2_s = sizeof(pix2);
auto pix3_s = sizeof(pix3);
auto pb0_s = sizeof(pb0);