2021-02-13 08:16:04 +00:00
|
|
|
/* Python-rgbcx Texture Compression Library
|
|
|
|
Copyright (C) 2021 Andrew Cassidy <drewcassidy@me.com>
|
|
|
|
Partially derived from rgbcx.h written by Richard Geldreich 2020 <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 <cmath>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <span>
|
|
|
|
#include <vector>
|
|
|
|
|
2021-02-15 11:27:52 +00:00
|
|
|
#include "BlockView.h"
|
2021-02-13 10:32:26 +00:00
|
|
|
#include "ndebug.h"
|
2021-02-15 11:27:52 +00:00
|
|
|
#include "util.h"
|
2021-02-13 08:16:04 +00:00
|
|
|
|
|
|
|
namespace rgbcx {
|
|
|
|
|
|
|
|
template <class B, size_t M, size_t N> class BlockDecoder {
|
|
|
|
public:
|
2021-02-15 11:27:52 +00:00
|
|
|
using DecodedBlock = ColorBlockView<M, N>;
|
2021-02-13 08:16:04 +00:00
|
|
|
using EncodedBlock = B;
|
|
|
|
|
|
|
|
BlockDecoder() noexcept = default;
|
|
|
|
virtual ~BlockDecoder() noexcept = default;
|
2021-02-15 11:27:52 +00:00
|
|
|
|
2021-02-13 10:32:26 +00:00
|
|
|
virtual void DecodeBlock(DecodedBlock dest, EncodedBlock *const block) const noexcept(ndebug) = 0;
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-16 17:00:12 +00:00
|
|
|
void DecodeImage(uint8_t *encoded, Color *decoded, unsigned image_width, unsigned image_height) {
|
|
|
|
assert(image_width % N == 0);
|
|
|
|
assert(image_width % M == 0);
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-16 17:00:12 +00:00
|
|
|
unsigned block_width = image_width / N;
|
|
|
|
unsigned block_height = image_height / M;
|
|
|
|
|
|
|
|
auto blocks = reinterpret_cast<B *>(encoded);
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-16 07:02:24 +00:00
|
|
|
// from experimentation, multithreading this using OpenMP actually makes decoding slower
|
|
|
|
// due to thread creation/teardown taking longer than the decoding process itself.
|
|
|
|
// As a result, this is left as a serial operation despite being embarassingly parallelizable
|
|
|
|
for (unsigned y = 0; y < block_height; y++) {
|
2021-02-13 08:16:04 +00:00
|
|
|
for (unsigned x = 0; x < block_width; x++) {
|
2021-02-16 07:02:24 +00:00
|
|
|
unsigned pixel_x = x * N;
|
|
|
|
unsigned pixel_y = y * M;
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-16 07:02:24 +00:00
|
|
|
assert(pixel_x >= 0);
|
|
|
|
assert(pixel_y >= 0);
|
|
|
|
assert(pixel_y + M <= image_height);
|
|
|
|
assert(pixel_x + N <= image_width);
|
2021-02-13 08:16:04 +00:00
|
|
|
|
2021-02-16 07:02:24 +00:00
|
|
|
unsigned top_left = pixel_x + (pixel_y * image_width);
|
2021-02-16 17:00:12 +00:00
|
|
|
auto dest = DecodedBlock(&decoded[top_left], image_width);
|
2021-02-15 11:27:52 +00:00
|
|
|
|
2021-02-16 07:02:24 +00:00
|
|
|
DecodeBlock(dest, &blocks[x + block_width * y]);
|
2021-02-13 08:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace rgbcx
|