From 2fbb92c3856b4961d39929aba4f6bf27e4a2ced7 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sun, 6 Aug 2017 19:12:44 +0000 Subject: [PATCH] File splits --- Makefile | 2 +- color.h | 18 ++++++++++++++ colorchecker.h | 28 +++++++++++++++++++++ colors.h | 4 +++ piphoto.cc | 67 ++++++++------------------------------------------ 5 files changed, 61 insertions(+), 58 deletions(-) create mode 100644 color.h create mode 100644 colorchecker.h create mode 100644 colors.h diff --git a/Makefile b/Makefile index e81136a..0c8d0bb 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ all: piphoto -piphoto: piphoto.cc Makefile +piphoto: *.cc *.h Makefile clang-3.9 -O3 -g -Weverything -Werror -Wno-c++98-compat -Wno-c++98-c++11-compat-pedantic --std=c++1z --stdlib=libc++ -o piphoto piphoto.cc -lc++ -lunwind -lpng run: piphoto diff --git a/color.h b/color.h new file mode 100644 index 0000000..78190bf --- /dev/null +++ b/color.h @@ -0,0 +1,18 @@ +#pragma once + +struct Color { + // 32-bit for compiler convenience, but values are 16-bit + uint32_t r; + uint32_t g; + uint32_t b; + + constexpr uint32_t Difference(const Color& other) const; +}; + +constexpr uint32_t Color::Difference(const Color& other) const { + return ( + ((r > other.r) ? (r - other.r) : (other.r - r)) + + ((g > other.g) ? (g - other.g) : (other.g - g)) + + ((b > other.b) ? (b - other.b) : (other.b - b)) + ); +} diff --git a/colorchecker.h b/colorchecker.h new file mode 100644 index 0000000..fc544fe --- /dev/null +++ b/colorchecker.h @@ -0,0 +1,28 @@ +#pragma once + +constexpr std::array kColorCheckerSrgb = {{ + {0x7300, 0x5200, 0x4400}, + {0xc200, 0x9600, 0x8200}, + {0x6200, 0x7a00, 0x9d00}, + {0x5700, 0x6c00, 0x4300}, + {0x8500, 0x8000, 0xb100}, + {0x6700, 0xbd00, 0xaa00}, + {0xd600, 0x7e00, 0x2c00}, + {0x5000, 0x5b00, 0xa600}, + {0xc100, 0x5a00, 0x6300}, + {0x5e00, 0x3c00, 0x6c00}, + {0x9d00, 0xbc00, 0x4000}, + {0xe000, 0xa300, 0x2e00}, + {0x3800, 0x3d00, 0x9600}, + {0x4600, 0x9400, 0x4900}, + {0xaf00, 0x3600, 0x3c00}, + {0xe700, 0xc700, 0x1f00}, + {0xbb00, 0x5600, 0x9500}, + {0x0800, 0x8500, 0xa100}, + {0xf300, 0xf300, 0xf200}, + {0xc800, 0xc800, 0xc800}, + {0xa000, 0xa000, 0xa000}, + {0x7a00, 0x7a00, 0x7900}, + {0x5500, 0x5500, 0x5500}, + {0x3400, 0x3400, 0x3400}, +}}; diff --git a/colors.h b/colors.h new file mode 100644 index 0000000..04edd16 --- /dev/null +++ b/colors.h @@ -0,0 +1,4 @@ +#pragma once + +constexpr Color kBlack = Color{0x0000, 0x0000, 0x0000}; +constexpr Color kWhite = Color{0xffff, 0xffff, 0xffff}; diff --git a/piphoto.cc b/piphoto.cc index d6895dd..c2de5c6 100644 --- a/piphoto.cc +++ b/piphoto.cc @@ -11,6 +11,10 @@ #include #include +#include "color.h" +#include "colorchecker.h" +#include "colors.h" + namespace std { using string_view = experimental::string_view; } @@ -18,57 +22,6 @@ using string_view = experimental::string_view; std::string ReadFile(const std::string& filename); void WriteFile(const std::string& filename, const std::string& contents); - -struct Color { - // 32-bit for compiler convenience, but values are 16-bit - uint32_t r; - uint32_t g; - uint32_t b; - - uint32_t Difference(const Color& other) const; -}; - -uint32_t Color::Difference(const Color& other) const { - return ( - ((r > other.r) ? (r - other.r) : (other.r - r)) + - ((g > other.g) ? (g - other.g) : (other.g - g)) + - ((b > other.b) ? (b - other.b) : (other.b - b)) - ); -} - - -constexpr uint32_t kNumColorChecker = 24; -constexpr std::array kColorCheckerSrgb = {{ - {0x7300, 0x5200, 0x4400}, - {0xc200, 0x9600, 0x8200}, - {0x6200, 0x7a00, 0x9d00}, - {0x5700, 0x6c00, 0x4300}, - {0x8500, 0x8000, 0xb100}, - {0x6700, 0xbd00, 0xaa00}, - {0xd600, 0x7e00, 0x2c00}, - {0x5000, 0x5b00, 0xa600}, - {0xc100, 0x5a00, 0x6300}, - {0x5e00, 0x3c00, 0x6c00}, - {0x9d00, 0xbc00, 0x4000}, - {0xe000, 0xa300, 0x2e00}, - {0x3800, 0x3d00, 0x9600}, - {0x4600, 0x9400, 0x4900}, - {0xaf00, 0x3600, 0x3c00}, - {0xe700, 0xc700, 0x1f00}, - {0xbb00, 0x5600, 0x9500}, - {0x0800, 0x8500, 0xa100}, - {0xf300, 0xf300, 0xf200}, - {0xc800, 0xc800, 0xc800}, - {0xa000, 0xa000, 0xa000}, - {0x7a00, 0x7a00, 0x7900}, - {0x5500, 0x5500, 0x5500}, - {0x3400, 0x3400, 0x3400}, -}}; - -constexpr Color kBlack = Color{0x0000, 0x0000, 0x0000}; -constexpr Color kWhite = Color{0xffff, 0xffff, 0xffff}; - - struct Coord { uint32_t x; uint32_t y; @@ -83,7 +36,7 @@ std::ostream& operator<<(std::ostream& os, const Coord& coord) { template class Image : public std::array, Y> { public: - std::array ColorCheckerClosest() const; + std::array ColorCheckerClosest() const; Color GetPixel(const Coord& coord) const; @@ -95,9 +48,9 @@ class Image : public std::array, Y> { }; template -std::array Image::ColorCheckerClosest() const { - std::array closest; - std::array diff; +std::array Image::ColorCheckerClosest() const { + std::array closest; + std::array diff; diff.fill(UINT32_MAX); for (uint32_t y = 0; y < Y; ++y) { @@ -106,7 +59,7 @@ std::array Image::ColorCheckerClosest() const { for (uint32_t x = 0; x < X; ++x) { const auto& pixel = row.at(x); - for (uint32_t cc = 0; cc < kNumColorChecker; ++cc) { + for (uint32_t cc = 0; cc < kColorCheckerSrgb.size(); ++cc) { auto pixel_diff = pixel.Difference(kColorCheckerSrgb.at(cc)); if (pixel_diff < diff.at(cc)) { diff.at(cc) = pixel_diff; @@ -356,7 +309,7 @@ int main() { auto raw = PiRaw2::FromJpeg(ReadFile("test.jpg")); auto* image = raw.GetImage(); auto closest = image->ColorCheckerClosest(); - for (uint32_t cc = 0; cc < kNumColorChecker; ++cc) { + for (uint32_t cc = 0; cc < kColorCheckerSrgb.size(); ++cc) { const auto& coord = closest.at(cc); const auto& color = kColorCheckerSrgb.at(cc); std::cout << cc << ": " << coord << " difference=" << color.Difference(image->GetPixel(coord)) << std::endl;