// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute // SPDX-License-Identifier: GPL-3.0-only #pragma once #include #include #include #include #include #include #include #include "../compression/CompressionAlgorithmEnum.h" #include "ColorScale.h" // std::vector value-initialises whatever it resizes into. A buffer that the very next read fills in // full has no use for that, and on a compressed image chunk it is megabytes of memset per image. This // allocator default-initialises instead, which for bytes is no initialisation at all. The rebind is // not optional: without it the container rebinds to std::allocator and the zeroing comes back. template struct NoInitAllocator : std::allocator { NoInitAllocator() = default; template NoInitAllocator(const NoInitAllocator &) {} template struct rebind { using other = NoInitAllocator; }; template void construct(U *p) { ::new (static_cast(p)) U; } template void construct(U *p, Args &&...args) { ::new (static_cast(p)) U(std::forward(args)...); } }; // Bytes of an image as they are stored: sized by the reader and then overwritten by the read. using RawByteBuffer = std::vector>; enum class CompressedImageMode {Int8, Int16, Int32, Uint8, Uint16, Uint32, RGB, Float16, Float32, Float64}; CompressedImageMode CalcImageMode(size_t byte_depth, bool is_float, bool is_signed); class CompressedImage { size_t xpixel; size_t ypixel; CompressedImageMode mode; CompressionAlgorithm algorithm; const uint8_t *data; size_t size; // After compression std::string channel; public: CompressedImage(); CompressedImage(const void* data, size_t size, size_t xpixel, size_t ypixel, CompressedImageMode mode, CompressionAlgorithm algorithm = CompressionAlgorithm::NO_COMPRESSION, std::string channel = "default"); CompressedImage(const std::vector& input, size_t xpixel, size_t ypixel); CompressedImage(const std::vector& input, size_t xpixel, size_t ypixel); CompressedImage(const std::vector& input, size_t xpixel, size_t ypixel, CompressedImageMode mode = CompressedImageMode::Uint8, CompressionAlgorithm algorithm = CompressionAlgorithm::NO_COMPRESSION); CompressedImage(const std::vector& input, size_t xpixel, size_t ypixel); CompressedImage(const std::vector& input, size_t xpixel, size_t ypixel); CompressedImage(const std::vector& input, size_t xpixel, size_t ypixel); CompressedImage(const std::vector& input, size_t xpixel, size_t ypixel); CompressedImage &Channel(std::string channel); [[nodiscard]] size_t GetUncompressedSize() const; [[nodiscard]] bool IsSigned() const; void GetUncompressed(std::vector &buffer) const; [[nodiscard]] const uint8_t* GetUncompressedPtr(std::vector &buffer) const; [[nodiscard]] size_t GetByteDepth() const; [[nodiscard]] size_t GetNumChannels() const; [[nodiscard]] std::string GetChannel() const; [[nodiscard]] size_t GetWidth() const; [[nodiscard]] size_t GetHeight() const; [[nodiscard]] CompressedImageMode GetMode() const; [[nodiscard]] CompressionAlgorithm GetCompressionAlgorithm() const; [[nodiscard]] const uint8_t *GetCompressed() const; [[nodiscard]] size_t GetCompressedSize() const; };