#pragma once #include "CompressedTexture.h" #include "../../core/Serializer.h" #include "../../inc/tbb/parallel_for.h" #include "../../core/BitHelper.h" template class BasicTexture : public CompressedTexture { private: std::vector mData; public: BasicTexture() : mData(std::vector()) {} ~BasicTexture() {} // Uncompress and retrieve the texture (possibly from a certain index) T operator[](size_t i) const override { return mData[i]; } unsigned64 size() const override { return mData.size(); } std::vector GetTexture(size_t fromIndex = 0) override { std::vector res(size() - fromIndex); std::copy(mData.begin() + fromIndex, mData.end(), res.begin()); return res; } // Compresses the texture, replacing everything after fromIndex by whatever is in texture void SetTexture(const std::vector& texture, size_t fromIndex = 0) override { mData.resize(fromIndex + texture.size()); std::copy(texture.begin(), texture.end(), mData.begin() + fromIndex); } // Replace all materials by other colors. This could be useful as some methods don't need to fully decompress and compress the whole texture to do this. void ReplaceMaterials(const std::unordered_map& replacers) override { bool replacersEqual = true; for (auto replacer : replacers) if (replacer.first != replacer.second) { replacersEqual = false; break; } if (!replacersEqual) { tbb::parallel_for(size_t(0), mData.size(), [&](size_t i) { auto replacer = replacers.find(mData[i]); if (replacer != replacers.end()) mData[i] = replacer->second; }); } } void Recompress() override { return; } void ReadFromFile(std::istream& file) override { Serializer, unsigned64>::Deserialize(mData, file); } virtual void WriteToFile(std::ostream& file) const override { Serializer, unsigned64>::Serialize(mData, file); } std::vector GetTexturePool() const override { size_t poolSize = GetTexturePoolSize(); std::vector pool = std::vector(poolSize); tbb::parallel_for(size_t(0), mData.size(), [&](size_t i) { // Convert all values to 32 bit integers and split them into bytes unsigned32 value = (unsigned32)mData[i]; BitHelper::SplitInBytesAndMove(value, pool, i * 4); }); return pool; } size_t GetTexturePoolSize() const override { return mData.size() * 4; } virtual std::map GetAdditionalProperties() const override { return std::map(); } };