/* Python-rgbcx Texture Compression Library Copyright (C) 2021 Andrew Cassidy Partially derived from rgbcx.h written by Richard Geldreich 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 . */ #pragma once #include #include #include #include #include #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 Dimensions() const { return std::tuple(_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(Width() * Height()) * sizeof(Color); } template ColorBlock 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 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 void SetBlock(int block_x, int block_y, const ColorBlock &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(_pixels); } Color *_pixels; }; template 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 &&other) : Base(other._width, other._height) { _blocks = other._blocks; other._blocks = nullptr; } /** * Copy constructor * @param other object to copy */ BlockTexture(const BlockTexture &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 other) { swap(*this, other); return *this; } friend void swap(BlockTexture &first, BlockTexture &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(_blocks); } B *_blocks; }; } // namespace quicktex