Move ToPng() into Image, make PiRaw static-only
This commit is contained in:
44
image.h
44
image.h
@@ -1,5 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <png.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "coord.h"
|
#include "coord.h"
|
||||||
|
|
||||||
@@ -13,6 +18,8 @@ class Image : public std::array<std::array<Color, X>, Y> {
|
|||||||
void DrawYLine(const Coord& start, const Color& color, uint32_t length);
|
void DrawYLine(const Coord& start, const Color& color, uint32_t length);
|
||||||
void DrawRectangle(const Coord& start, const Color& color, uint32_t x_length, uint32_t y_length);
|
void DrawRectangle(const Coord& start, const Color& color, uint32_t x_length, uint32_t y_length);
|
||||||
void DrawSquare(const Coord& start, const Color& color, uint32_t length);
|
void DrawSquare(const Coord& start, const Color& color, uint32_t length);
|
||||||
|
|
||||||
|
std::string ToPng();
|
||||||
};
|
};
|
||||||
|
|
||||||
template <uint32_t X, uint32_t Y>
|
template <uint32_t X, uint32_t Y>
|
||||||
@@ -53,3 +60,40 @@ template <uint32_t X, uint32_t Y>
|
|||||||
void Image<X, Y>::DrawSquare(const Coord& start, const Color& color, uint32_t length) {
|
void Image<X, Y>::DrawSquare(const Coord& start, const Color& color, uint32_t length) {
|
||||||
DrawRectangle(start, color, length, length);
|
DrawRectangle(start, color, length, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void WriteCallback(png_structp png_ptr, png_bytep data, png_size_t length) {
|
||||||
|
auto dest = static_cast<std::string*>(png_get_io_ptr(png_ptr));
|
||||||
|
dest->append(reinterpret_cast<char*>(data), length);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <uint32_t X, uint32_t Y>
|
||||||
|
std::string Image<X, Y>::ToPng() {
|
||||||
|
std::string ret;
|
||||||
|
|
||||||
|
auto png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||||
|
assert(png_ptr);
|
||||||
|
auto info_ptr = png_create_info_struct(png_ptr);
|
||||||
|
assert(info_ptr);
|
||||||
|
|
||||||
|
png_set_write_fn(png_ptr, &ret, &WriteCallback, nullptr);
|
||||||
|
png_set_IHDR(png_ptr, info_ptr, X / 2, Y / 2,
|
||||||
|
16, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
|
||||||
|
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
||||||
|
|
||||||
|
png_write_info(png_ptr, info_ptr);
|
||||||
|
for (auto& row : *this) {
|
||||||
|
std::array<uint16_t, X * 3> out_row;
|
||||||
|
for (uint32_t x = 0; x < X; ++x) {
|
||||||
|
out_row[x * 3 + 0] = htons(static_cast<uint16_t>(row[x].r));
|
||||||
|
out_row[x * 3 + 1] = htons(static_cast<uint16_t>(row[x].g));
|
||||||
|
out_row[x * 3 + 2] = htons(static_cast<uint16_t>(row[x].b));
|
||||||
|
}
|
||||||
|
png_write_row(png_ptr, reinterpret_cast<unsigned char*>(out_row.data()));
|
||||||
|
}
|
||||||
|
png_write_end(png_ptr, nullptr);
|
||||||
|
|
||||||
|
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
||||||
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,8 +9,7 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
auto raw = PiRaw2::FromJpeg(ReadFile("test.jpg"));
|
auto image = PiRaw2::FromJpeg(ReadFile("test.jpg"));
|
||||||
auto* image = raw.GetImage();
|
|
||||||
auto closest = ColorCheckerClosest(*image);
|
auto closest = ColorCheckerClosest(*image);
|
||||||
for (uint32_t cc = 0; cc < kColorCheckerSrgb.size(); ++cc) {
|
for (uint32_t cc = 0; cc < kColorCheckerSrgb.size(); ++cc) {
|
||||||
const auto& coord = closest.at(cc);
|
const auto& coord = closest.at(cc);
|
||||||
@@ -22,5 +21,5 @@ int main() {
|
|||||||
image->DrawSquare({std::max(8U, coord.x) - 8, std::max(8U, coord.y) - 8}, color, 16);
|
image->DrawSquare({std::max(8U, coord.x) - 8, std::max(8U, coord.y) - 8}, color, 16);
|
||||||
image->DrawSquare({std::max(9U, coord.x) - 9, std::max(9U, coord.y) - 9}, kWhite, 18);
|
image->DrawSquare({std::max(9U, coord.x) - 9, std::max(9U, coord.y) - 9}, kWhite, 18);
|
||||||
}
|
}
|
||||||
WriteFile("test.png", raw.ToPng());
|
WriteFile("test.png", image->ToPng());
|
||||||
}
|
}
|
||||||
|
|||||||
82
piraw.h
82
piraw.h
@@ -1,8 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <png.h>
|
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <experimental/string_view>
|
#include <experimental/string_view>
|
||||||
|
|
||||||
@@ -16,16 +13,12 @@ using string_view = experimental::string_view;
|
|||||||
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
||||||
class PiRaw {
|
class PiRaw {
|
||||||
public:
|
public:
|
||||||
PiRaw(std::unique_ptr<Image<X, Y>>);
|
static std::unique_ptr<Image<X, Y>> FromJpeg(const std::string_view& jpeg);
|
||||||
static PiRaw FromJpeg(const std::string_view& jpeg);
|
static std::unique_ptr<Image<X, Y>> FromRaw(const std::string_view& raw);
|
||||||
static PiRaw FromRaw(const std::string_view& raw);
|
|
||||||
|
|
||||||
std::string ToPng();
|
|
||||||
|
|
||||||
Image<X, Y>* GetImage();
|
|
||||||
const Image<X, Y>& GetImage() const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
PiRaw() {}
|
||||||
|
|
||||||
static constexpr uint32_t kJpegHeaderBytes = 32768;
|
static constexpr uint32_t kJpegHeaderBytes = 32768;
|
||||||
static constexpr const char* kJpegHeaderMagic = "BRCM";
|
static constexpr const char* kJpegHeaderMagic = "BRCM";
|
||||||
static constexpr uint32_t kPixelsPerChunk = 4;
|
static constexpr uint32_t kPixelsPerChunk = 4;
|
||||||
@@ -40,27 +33,21 @@ class PiRaw {
|
|||||||
|
|
||||||
typedef std::array<uint32_t, kPixelsPerChunk> Chunk;
|
typedef std::array<uint32_t, kPixelsPerChunk> Chunk;
|
||||||
|
|
||||||
static Chunk GetChunk(const std::string_view& raw, const uint32_t x_chunk, const uint32_t y);
|
static constexpr Chunk GetChunk(const std::string_view& raw, const uint32_t x_chunk, const uint32_t y);
|
||||||
static Color CombineRaw(uint32_t y0x0, uint32_t y0x1, uint32_t y1x0, uint32_t y1x1);
|
static constexpr Color CombineRaw(uint32_t y0x0, uint32_t y0x1, uint32_t y1x0, uint32_t y1x1);
|
||||||
|
|
||||||
std::unique_ptr<Image<X, Y>> image_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef PiRaw<3280, 2464, 10, 16, 2> PiRaw2;
|
typedef PiRaw<3280, 2464, 10, 16, 2> PiRaw2;
|
||||||
|
|
||||||
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
||||||
PiRaw<X, Y, D, A, P>::PiRaw(std::unique_ptr<Image<X, Y>> image)
|
typename std::unique_ptr<Image<X, Y>> PiRaw<X, Y, D, A, P>::FromJpeg(const std::string_view& jpeg) {
|
||||||
: image_(std::move(image)) {}
|
|
||||||
|
|
||||||
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
|
||||||
PiRaw<X, Y, D, A, P> PiRaw<X, Y, D, A, P>::FromJpeg(const std::string_view& jpeg) {
|
|
||||||
auto container_len = GetRawBytes() + kJpegHeaderBytes;
|
auto container_len = GetRawBytes() + kJpegHeaderBytes;
|
||||||
assert(jpeg.substr(jpeg.size() - container_len, 4) == kJpegHeaderMagic);
|
assert(jpeg.substr(jpeg.size() - container_len, 4) == kJpegHeaderMagic);
|
||||||
return FromRaw(jpeg.substr(jpeg.size() - GetRawBytes(), GetRawBytes()));
|
return FromRaw(jpeg.substr(jpeg.size() - GetRawBytes(), GetRawBytes()));
|
||||||
}
|
}
|
||||||
|
|
||||||
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
||||||
PiRaw<X, Y, D, A, P> PiRaw<X, Y, D, A, P>::FromRaw(const std::string_view& raw) {
|
typename std::unique_ptr<Image<X, Y>> PiRaw<X, Y, D, A, P>::FromRaw(const std::string_view& raw) {
|
||||||
static_assert(X % 2 == 0);
|
static_assert(X % 2 == 0);
|
||||||
static_assert(Y % 2 == 0);
|
static_assert(Y % 2 == 0);
|
||||||
static_assert(kPixelsPerChunk == 4);
|
static_assert(kPixelsPerChunk == 4);
|
||||||
@@ -77,7 +64,7 @@ PiRaw<X, Y, D, A, P> PiRaw<X, Y, D, A, P>::FromRaw(const std::string_view& raw)
|
|||||||
image->at(out_y).at(out_x + 1) = CombineRaw(chunk1.at(2), chunk1.at(3), chunk2.at(2), chunk2.at(3));
|
image->at(out_y).at(out_x + 1) = CombineRaw(chunk1.at(2), chunk1.at(3), chunk2.at(2), chunk2.at(3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return PiRaw<X, Y, D, A, P>(std::move(image));
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
||||||
@@ -106,7 +93,7 @@ constexpr uint32_t PiRaw<X, Y, D, A, P>::Align(uint32_t val) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
||||||
typename PiRaw<X, Y, D, A, P>::Chunk PiRaw<X, Y, D, A, P>::GetChunk(const std::string_view& raw, const uint32_t x_chunk, const uint32_t y) {
|
constexpr typename PiRaw<X, Y, D, A, P>::Chunk PiRaw<X, Y, D, A, P>::GetChunk(const std::string_view& raw, const uint32_t x_chunk, const uint32_t y) {
|
||||||
// Function is bit depth & layout specific
|
// Function is bit depth & layout specific
|
||||||
static_assert(D == 10);
|
static_assert(D == 10);
|
||||||
|
|
||||||
@@ -126,7 +113,7 @@ typename PiRaw<X, Y, D, A, P>::Chunk PiRaw<X, Y, D, A, P>::GetChunk(const std::s
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
||||||
Color PiRaw<X, Y, D, A, P>::CombineRaw(uint32_t y0x0, uint32_t y0x1, uint32_t y1x0, uint32_t y1x1) {
|
constexpr Color PiRaw<X, Y, D, A, P>::CombineRaw(uint32_t y0x0, uint32_t y0x1, uint32_t y1x0, uint32_t y1x1) {
|
||||||
// Function is bit layout specific
|
// Function is bit layout specific
|
||||||
Color ret;
|
Color ret;
|
||||||
ret.r = y1x1;
|
ret.r = y1x1;
|
||||||
@@ -134,50 +121,3 @@ Color PiRaw<X, Y, D, A, P>::CombineRaw(uint32_t y0x0, uint32_t y0x1, uint32_t y1
|
|||||||
ret.b = y0x0;
|
ret.b = y0x0;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteCallback(png_structp png_ptr, png_bytep data, png_size_t length) {
|
|
||||||
auto dest = static_cast<std::string*>(png_get_io_ptr(png_ptr));
|
|
||||||
dest->append(reinterpret_cast<char*>(data), length);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
|
||||||
std::string PiRaw<X, Y, D, A, P>::ToPng() {
|
|
||||||
std::string ret;
|
|
||||||
|
|
||||||
auto png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
||||||
assert(png_ptr);
|
|
||||||
auto info_ptr = png_create_info_struct(png_ptr);
|
|
||||||
assert(info_ptr);
|
|
||||||
|
|
||||||
png_set_write_fn(png_ptr, &ret, &WriteCallback, nullptr);
|
|
||||||
png_set_IHDR(png_ptr, info_ptr, X / 2, Y / 2,
|
|
||||||
16, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
|
|
||||||
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
||||||
|
|
||||||
png_write_info(png_ptr, info_ptr);
|
|
||||||
for (auto& row : *image_) {
|
|
||||||
std::array<uint16_t, X * 3> out_row;
|
|
||||||
for (uint32_t x = 0; x < X; ++x) {
|
|
||||||
out_row[x * 3 + 0] = htons(static_cast<uint16_t>(row[x].r));
|
|
||||||
out_row[x * 3 + 1] = htons(static_cast<uint16_t>(row[x].g));
|
|
||||||
out_row[x * 3 + 2] = htons(static_cast<uint16_t>(row[x].b));
|
|
||||||
}
|
|
||||||
png_write_row(png_ptr, reinterpret_cast<unsigned char*>(out_row.data()));
|
|
||||||
}
|
|
||||||
png_write_end(png_ptr, nullptr);
|
|
||||||
|
|
||||||
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
|
||||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
|
||||||
Image<X, Y>* PiRaw<X, Y, D, A, P>::GetImage() {
|
|
||||||
return image_.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <uint32_t X, uint32_t Y, uint32_t D, uint32_t A, uint32_t P>
|
|
||||||
const Image<X, Y>& PiRaw<X, Y, D, A, P>::GetImage() const {
|
|
||||||
return *image_;
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user