From 1838d8c62fe6b562753deedf87d44cab2f6aaf02 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Sat, 12 Aug 2017 12:01:21 -0700 Subject: [PATCH] Lut base class, ScoreImage -> ScoreLut --- Makefile | 2 +- colorchecker.h | 6 +++--- lut.cc | 4 ++++ lut.h | 51 +++++++++++++++++++++++++++++--------------------- piphoto.cc | 5 +++-- 5 files changed, 41 insertions(+), 27 deletions(-) create mode 100644 lut.cc diff --git a/Makefile b/Makefile index 88111f5..1554d9c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ all: piphoto -objects = piphoto.o util.o +objects = piphoto.o lut.o util.o piphoto: $(objects) Makefile clang-3.9 -O3 -g -Weverything -Werror --std=c++1z --stdlib=libc++ -o piphoto $(objects) -lc++ -lunwind -lpng diff --git a/colorchecker.h b/colorchecker.h index 13c2f91..41d7821 100644 --- a/colorchecker.h +++ b/colorchecker.h @@ -69,7 +69,7 @@ Array, kColorCheckerSrgb.size()> FindClosest(const Image& imag } template -int32_t ScoreImage(const Image& image) { +int32_t ScoreLut(const Image& image, const Lut& lut) { static_assert(C == 3); Array diff; @@ -79,7 +79,7 @@ int32_t ScoreImage(const Image& image) { const auto& row = image.at(y); for (int32_t x = 0; x < X; ++x) { - const auto& pixel = row.at(x); + const auto pixel = lut.MapColor(row.at(x)); for (int32_t cc = 0; cc < kColorCheckerSrgb.ssize(); ++cc) { auto pixel_diff = pixel.AbsDiff(kColorCheckerSrgb.at(cc)); @@ -138,7 +138,7 @@ int32_t OptimizeLut(const Image& image, Lut3d MapColor(const Color<3>& in) const = 0; + + template + std::unique_ptr> MapImage(const Image& in) const; +}; + +template +std::unique_ptr> Lut::MapImage(const Image& in) const { + auto out = std::make_unique>(); + + for (int32_t y = 0; y < Y; ++y) { + for (int32_t x = 0; x < X; ++x) { + Coord<2> coord = {{{{x, y}}}}; + out->SetPixel(coord, MapColor(in.GetPixel(coord))); + } + } + + return out; +} + -// Hardcoded to Color<3>, so color dimensions == LUT dimensions template -class Lut3d : public Array, X>, Y>, Z> { +class Lut3d : public Array, X>, Y>, Z>, public Lut { public: static Lut3d Identity(); - Color<3> MapColor(const Color<3>& in) const; - - template - std::unique_ptr> MapImage(const Image& in) const; + Color<3> MapColor(const Color<3>& in) const override; private: // Return value is (root_indices, remainders) @@ -82,21 +106,6 @@ Color<3> Lut3d::MapColor(const Color<3>& in) const { return inter0.Interpolate(inter1, rem.at(2), BlockSize(Z)).Crop(); } -template -template -std::unique_ptr> Lut3d::MapImage(const Image& in) const { - auto out = std::make_unique>(); - - for (int32_t y = 0; y < IMG_Y; ++y) { - for (int32_t x = 0; x < IMG_X; ++x) { - Coord<2> coord = {{{{x, y}}}}; - out->SetPixel(coord, MapColor(in.GetPixel(coord))); - } - } - - return out; -} - template constexpr std::pair, Coord<3>> Lut3d::FindRoot(const Color<3>& in) { auto root_x = FindChannelRoot(in.at(0), X); diff --git a/piphoto.cc b/piphoto.cc index 9586b8b..c50fa03 100644 --- a/piphoto.cc +++ b/piphoto.cc @@ -8,13 +8,14 @@ int main() { auto image = PiRaw2::FromJpeg(ReadFile("test.jpg")); WriteFile("start.png", HighlightClosest(*image)->ToPng()); - std::cout << "Initial error: " << ScoreImage(*image) << std::endl; auto lut = MinimalLut3d::Identity(); + std::cout << "Initial error: " << ScoreLut(*image, lut) << std::endl; + int32_t diff = 1; while (diff) { diff = OptimizeLut<4>(*image, &lut); - std::cout << "diff=" << diff << " error=" << ScoreImage(*lut.MapImage(*image)) << std::endl; + std::cout << "diff=" << diff << " error=" << ScoreLut(*image, lut) << std::endl; WriteFile("inter.png", HighlightClosest(*lut.MapImage(*image))->ToPng()); }