#pragma once #include #include #include "color.h" #include "image.h" namespace std { using string_view = experimental::string_view; } template class PiRaw { public: PiRaw() = delete; PiRaw(const PiRaw&) = delete; PiRaw(PiRaw&&) = delete; static std::unique_ptr> FromJpeg(const std::string_view& jpeg); static std::unique_ptr> FromRaw(const std::string_view& raw); private: static constexpr int32_t kJpegHeaderBytes = 32768; static constexpr const char* kJpegHeaderMagic = "BRCM"; static constexpr int32_t kPixelsPerChunk = 4; static constexpr int32_t kBitsPerByte = 8; static constexpr int32_t GetRawBytes(); static constexpr int32_t GetRowBytes(); static constexpr int32_t GetNumRows(); static constexpr int32_t GetChunkBytes(); static constexpr int32_t Align(int32_t val); typedef Array Chunk; static constexpr Chunk GetChunk(const std::string_view& raw, const int32_t x_chunk, const int32_t y); static constexpr RgbColor CombineRaw(int32_t y0x0, int32_t y0x1, int32_t y1x0, int32_t y1x1); }; typedef PiRaw<3280, 2464, 10, 16, 2> PiRaw2; template typename std::unique_ptr> PiRaw::FromJpeg(const std::string_view& jpeg) { size_t container_len = GetRawBytes() + kJpegHeaderBytes; assert(jpeg.substr(jpeg.size() - container_len, 4) == kJpegHeaderMagic); return FromRaw(jpeg.substr(jpeg.size() - GetRawBytes(), GetRawBytes())); } template typename std::unique_ptr> PiRaw::FromRaw(const std::string_view& raw) { static_assert(X % 2 == 0); static_assert(Y % 2 == 0); static_assert(kPixelsPerChunk == 4); assert(raw.size() == GetRawBytes()); auto image = std::make_unique>(); for (int32_t y = 0, out_y = 0; y < Y; y += 2, ++out_y) { for (int32_t x_chunk = 0, out_x = 0; x_chunk < X / kPixelsPerChunk; ++x_chunk, out_x += kPixelsPerChunk / 2) { auto chunk1 = GetChunk(raw, x_chunk, y + 0); auto chunk2 = GetChunk(raw, x_chunk, y + 1); image->at(out_y).at(out_x + 0) = CombineRaw(chunk1.at(0), chunk1.at(1), chunk2.at(0), chunk2.at(1)); image->at(out_y).at(out_x + 1) = CombineRaw(chunk1.at(2), chunk1.at(3), chunk2.at(2), chunk2.at(3)); } } return image; } template constexpr int32_t PiRaw::GetRawBytes() { return GetRowBytes() * GetNumRows(); } template constexpr int32_t PiRaw::GetRowBytes() { return Align(Align(X + P) * D / kBitsPerByte); } template constexpr int32_t PiRaw::GetNumRows() { return Align(Y + P); } template constexpr int32_t PiRaw::GetChunkBytes() { return D * kPixelsPerChunk / kBitsPerByte; } template constexpr int32_t PiRaw::Align(int32_t val) { return (~(A - 1)) & ((val) + (A - 1)); } template constexpr typename PiRaw::Chunk PiRaw::GetChunk(const std::string_view& raw, const int32_t x_chunk, const int32_t y) { // Function is bit depth & layout specific static_assert(D == 10); size_t start = static_cast(y * GetRowBytes() + x_chunk * GetChunkBytes()); uint32_t high0 = static_cast(raw.at(start + 0)); uint32_t high1 = static_cast(raw.at(start + 1)); uint32_t high2 = static_cast(raw.at(start + 2)); uint32_t high3 = static_cast(raw.at(start + 3)); uint32_t packed_low = static_cast(raw.at(start + 4)); Chunk ret; ret.at(0) = static_cast(((high0 << 2) | ((packed_low >> 6) & 0b11)) << 6); ret.at(1) = static_cast(((high1 << 2) | ((packed_low >> 4) & 0b11)) << 6); ret.at(2) = static_cast(((high2 << 2) | ((packed_low >> 2) & 0b11)) << 6); ret.at(3) = static_cast(((high3 << 2) | ((packed_low >> 0) & 0b11)) << 6); return ret; } template constexpr RgbColor PiRaw::CombineRaw(int32_t y0x0, int32_t y0x1, int32_t y1x0, int32_t y1x1) { RgbColor ret; ret.at(0) = static_cast(y1x1); ret.at(1) = static_cast((y0x1 + y1x0) / 2); ret.at(2) = static_cast(y0x0); return ret; }