You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
quicktex/quicktex/Texture.h

220 lines
7.4 KiB
C++

/* Python-rgbcx Texture Compression Library
Copyright (C) 2021 Andrew Cassidy <drewcassidy@me.com>
Partially derived from rgbcx.h written by Richard Geldreich <richgel99@gmail.com>
and licenced under the public domain
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <array>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <tuple>
#include "Block.h"
#include "Color.h"
namespace quicktex {
class Texture {
public:
virtual ~Texture() = default;
virtual int Width() const { return _width; }
virtual int Height() const { return _height; }
virtual std::tuple<int, int> Dimensions() const { return std::tuple<int, int>(_width, _height); }
/**
* The texture's total size
* @return The size of the texture in bytes.
*/
virtual size_t Size() const noexcept = 0;
protected:
Texture(int width, int height) : _width(width), _height(height) {
if (width <= 0) throw std::invalid_argument("Texture width must be greater than 0");
if (height <= 0) throw std::invalid_argument("Texture height must be greater than 0");
}
virtual uint8_t *DataMutable() = 0;
int _width;
int _height;
};
class RawTexture : public Texture {
using Base = Texture;
public:
/**
* Create a new RawTexture
* @param width width of the texture in pixels
* @param height height of the texture in pixels
*/
RawTexture(int width, int height);
/**
* Move constructor
* @param other object to move
*/
RawTexture(RawTexture &&other);
/**
* Copy constructor
* @param other object to copy
*/
RawTexture(const RawTexture &other);
/**
* assignment operator
* @param other object to copy (passed by value)
* @return this
*/
RawTexture &operator=(RawTexture other) noexcept;
virtual ~RawTexture() override { delete[] _pixels; }
friend void swap(RawTexture &first, RawTexture &second) noexcept;
virtual Color GetPixel(int x, int y) const;
virtual void SetPixel(int x, int y, Color val);
virtual size_t Size() const noexcept override { return static_cast<unsigned long>(Width() * Height()) * sizeof(Color); }
template <int N, int M> ColorBlock<N, M> GetBlock(int block_x, int block_y) const {
if (block_x < 0 || (block_x + 1) * N > _width) throw std::invalid_argument("x value out of range.");
if (block_y < 0 || (block_y + 1) * M > _height) throw std::invalid_argument("y value out of range.");
// coordinates in the image of the top-left pixel of the selected block
ColorBlock<N, M> block;
int pixel_x = block_x * N;
int pixel_y = block_y * M;
if (pixel_x + N < _width && pixel_y + M < _height) {
// fast memcpy if the block is entirely inside the bounds of the texture
for (int y = 0; y < M; y++) {
// copy each row into the ColorBlock
block.SetRow(y, &_pixels[pixel_x + (_width * (pixel_y + y))]);
}
} else {
// slower pixel-wise copy if the block goes over the edges
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) { block.Set(x, y, GetPixel((pixel_x + x) % _width, (pixel_y + y) % _height)); }
}
}
return block;
};
template <int N, int M> void SetBlock(int block_x, int block_y, const ColorBlock<N, M> &block) {
if (block_x < 0) throw std::invalid_argument("x value out of range.");
if (block_y < 0) throw std::invalid_argument("y value out of range.");
// coordinates in the image of the top-left pixel of the selected block
int pixel_x = block_x * N;
int pixel_y = block_y * M;
if (pixel_x + N < _width && pixel_y + M < _height) {
// fast row-wise memcpy if the block is entirely inside the bounds of the texture
for (int y = 0; y < M; y++) {
// copy each row out of the ColorBlock
block.GetRow(y, &_pixels[pixel_x + (_width * (pixel_y + y))]);
}
} else {
// slower pixel-wise copy if the block goes over the edges
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) { SetPixel((pixel_x + x) % _width, (pixel_y + y) % _height, block.Get(x, y)); }
}
}
};
protected:
virtual uint8_t *DataMutable() override { return reinterpret_cast<uint8_t *>(_pixels); }
Color *_pixels;
};
template <typename B> class BlockTexture : public Texture {
public:
using BlockType = B;
using Base = Texture;
/**
* Create a new BlockTexture
* @param width width of the texture in pixels. must be divisible by B::Width
* @param height height of the texture in pixels. must be divisible by B::Height
*/
BlockTexture(int width, int height) : Base(width, height) { _blocks = new B[(size_t)(BlocksX() * BlocksY())]; }
/**
* Move constructor
* @param other object to move
*/
BlockTexture(BlockTexture<B> &&other) : Base(other._width, other._height) {
_blocks = other._blocks;
other._blocks = nullptr;
}
/**
* Copy constructor
* @param other object to copy
*/
BlockTexture(const BlockTexture<B> &other) : BlockTexture(other.width, other.height) { std::memcpy(_blocks, other._blocks, Size()); }
/**
* assignment operator
* @param other object to copy (passed by value)
* @return this
*/
BlockTexture &operator=(BlockTexture<B> other) {
swap(*this, other);
return *this;
}
friend void swap(BlockTexture<B> &first, BlockTexture<B> &second) noexcept {
using std::swap; // enable ADL
swap(first._blocks, second._blocks);
swap(first._width, second._width);
swap(first._height, second._height);
}
~BlockTexture() { delete[] _blocks; }
constexpr int BlocksX() const { return _width / B::Width; }
constexpr int BlocksY() const { return _height / B::Height; }
virtual B GetBlock(int x, int y) const {
if (x < 0 || x >= BlocksX()) throw std::invalid_argument("x value out of range.");
if (y < 0 || y >= BlocksY()) throw std::invalid_argument("y value out of range.");
return _blocks[x + (y * _width)];
}
virtual void SetBlock(int x, int y, const B &val) {
if (x < 0 || x >= BlocksX()) throw std::invalid_argument("x value out of range.");
if (y < 0 || y >= BlocksY()) throw std::invalid_argument("y value out of range.");
_blocks[x + (y * _width)] = val;
}
virtual size_t Size() const noexcept override { return (size_t)(BlocksX() * BlocksY()) * sizeof(B); }
protected:
virtual uint8_t *DataMutable() override { return reinterpret_cast<uint8_t *>(_blocks); }
B *_blocks;
};
} // namespace quicktex