RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
XTransPhase.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2023 Roman Lebedev
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#pragma once
22
23#include "adt/Array2DRef.h"
24#include "adt/Optional.h"
25#include "adt/Point.h"
27#include <array>
28#include <cassert>
29#include <cmath>
30#include <cstdlib>
31
32namespace rawspeed {
33
35
37 iPoint2D off = tgt - src;
38 return {std::abs(off.x), std::abs(off.y)};
39}
40
41// NOTE: phase shift is direction-independent (phase order does not matter.)
42template <typename T>
43inline std::array<T, 6 * 6> applyPhaseShift(std::array<T, 6 * 6> srcData,
44 XTransPhase srcPhase,
45 XTransPhase tgtPhase) {
46 const iPoint2D coordOffset = getTranslationalOffset(srcPhase, tgtPhase);
47 assert(coordOffset >= iPoint2D(0, 0) && "Offset is non-negative.");
48 const Array2DRef<const T> src(srcData.data(), 6, 6);
49
50 std::array<T, 6 * 6> tgtData;
51 const Array2DRef<T> tgt(tgtData.data(), 6, 6);
52 for (int row = 0; row < tgt.height(); ++row) {
53 for (int col = 0; col < tgt.width(); ++col) {
54 tgt(row, col) = src((coordOffset.y + row) % 6, (coordOffset.x + col) % 6);
55 }
56 }
57
58 return tgtData;
59}
60
61inline std::array<CFAColor, 6 * 6> getAsCFAColors(XTransPhase p) {
62 const XTransPhase basePhase(0, 0);
63 // From Fujifilm X-Pro1.
64 using enum CFAColor;
65 const std::array<CFAColor, 6 * 6> basePat = {
70 return applyPhaseShift(basePat, basePhase, /*tgtPhase=*/p);
71}
72
74 if (CFA.getSize() != iPoint2D(6, 6))
75 return {};
76
77 std::array<CFAColor, 6 * 6> patData;
78 const Array2DRef<CFAColor> pat(patData.data(), 6, 6);
79 for (int row = 0; row < pat.height(); ++row) {
80 for (int col = 0; col < pat.width(); ++col) {
81 pat(row, col) = CFA.getColorAt(col, row);
82 }
83 }
84
85 iPoint2D off;
86 for (off.y = 0; off.y < pat.height(); ++off.y) {
87 for (off.x = 0; off.x < pat.width(); ++off.x) {
88 if (getAsCFAColors(off) == patData)
89 return off;
90 }
91 }
92
93 return {};
94}
95
96} // namespace rawspeed
assert(dim.area() >=area)
int RAWSPEED_READONLY height() const
int RAWSPEED_READONLY width() const
CFAColor getColorAt(int x, int y) const
value_type x
Definition Point.h:102
value_type y
Definition Point.h:103
std::array< CFAColor, 4 > getAsCFAColors(BayerPhase p)
Definition BayerPhase.h:97
std::array< T, 4 > applyPhaseShift(std::array< T, 4 > srcData, BayerPhase srcPhase, BayerPhase tgtPhase)
Definition BayerPhase.h:79
iPoint2D XTransPhase
Definition XTransPhase.h:34
iPoint2D getTranslationalOffset(BayerPhase src, BayerPhase tgt)
Definition BayerPhase.h:67
Optional< XTransPhase > getAsXTransPhase(const ColorFilterArray &CFA)
Definition XTransPhase.h:73