Skip to content

Commit 8cca7d6

Browse files
committed
implement colour transform cache
Signed-off-by: Anton Dukhovnikov <[email protected]>
1 parent 7e7f39e commit 8cca7d6

File tree

8 files changed

+638
-138
lines changed

8 files changed

+638
-138
lines changed

include/rawtoaces/cache_base.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright Contributors to the rawtoaces project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
// https://github.com/AcademySoftwareFoundation/rawtoaces
4+
5+
#include <iostream>
6+
#include <filesystem>
7+
8+
#include <string>
9+
#include <list>
10+
#include <variant>
11+
12+
#include <rawtoaces/metadata.h>
13+
14+
namespace rta
15+
{
16+
namespace cache
17+
{
18+
19+
template <class CacheEntryData> class CacheEntryDescriptor
20+
{
21+
public:
22+
virtual size_t map_index() const = 0;
23+
virtual bool fetch( CacheEntryData &data, int verbosity = 0 ) const = 0;
24+
virtual bool operator==( const CacheEntryDescriptor &other ) const = 0;
25+
26+
// Each subclass must also implement the folowing 2 methods.
27+
// Not sure how to declare them here.
28+
29+
// friend std::ostream& operator<< (std::ostream& stream, const DescriptorBase& descriptor);
30+
// std::tuple<Types && ...> construct_entry() const
31+
};
32+
33+
template <class Descriptor, class CacheEntryData, size_t size> class CacheBase
34+
{
35+
public:
36+
const CacheEntryData &fetch( const Descriptor &descriptor )
37+
{
38+
size_t map_index = descriptor.map_index();
39+
40+
std::list<std::pair<Descriptor, CacheEntryData>> &map =
41+
_maps[map_index];
42+
43+
if ( verbosity > 0 )
44+
{
45+
std::cerr << name << ": searching for a " << descriptor;
46+
}
47+
48+
for ( auto iter = map.begin(); iter != map.end(); ++iter )
49+
{
50+
if ( iter->first == descriptor )
51+
{
52+
if ( iter != map.begin() )
53+
{
54+
map.splice( map.begin(), map, iter, std::next( iter ) );
55+
}
56+
57+
if ( verbosity > 0 )
58+
{
59+
std::cerr << name << ": found in cache!" << std::endl;
60+
}
61+
return map.front().second;
62+
}
63+
}
64+
65+
if ( map.size() == capacity )
66+
{
67+
map.pop_back();
68+
}
69+
70+
if ( verbosity > 0 )
71+
{
72+
std::cerr << name << ": not found. Calculating a new entry."
73+
<< std::endl;
74+
}
75+
76+
map.emplace_front(
77+
std::piecewise_construct,
78+
std::forward_as_tuple( descriptor ),
79+
descriptor.construct_entry() );
80+
81+
CacheEntryData &data = map.front().second;
82+
descriptor.fetch( data, verbosity );
83+
return data;
84+
};
85+
86+
int capacity = 10;
87+
int verbosity = 0;
88+
std::string name = "Cache";
89+
90+
private:
91+
std::list<std::pair<Descriptor, CacheEntryData>> _maps[size];
92+
};
93+
94+
} // namespace cache
95+
} // namespace rta

include/rawtoaces/metadata.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,35 @@ struct Metadata
2222
std::vector<double> neutralRGB;
2323

2424
double baselineExposure;
25+
26+
friend bool operator==( const Metadata &m1, const Metadata &m2 )
27+
{
28+
if ( m1.calibrationIlluminant1 != m2.calibrationIlluminant1 )
29+
return false;
30+
if ( m1.calibrationIlluminant2 != m2.calibrationIlluminant2 )
31+
return false;
32+
if ( m1.baselineExposure != m2.baselineExposure )
33+
return false;
34+
if ( m1.analogBalance != m2.analogBalance )
35+
return false;
36+
if ( m1.neutralRGB != m2.neutralRGB )
37+
return false;
38+
if ( m1.cameraCalibration1 != m2.cameraCalibration1 )
39+
return false;
40+
if ( m1.cameraCalibration2 != m2.cameraCalibration2 )
41+
return false;
42+
if ( m1.xyz2rgbMatrix1 != m2.xyz2rgbMatrix1 )
43+
return false;
44+
if ( m1.xyz2rgbMatrix2 != m2.xyz2rgbMatrix2 )
45+
return false;
46+
47+
return true;
48+
}
49+
50+
friend bool operator!=( const Metadata &m1, const Metadata &m2 )
51+
{
52+
return !( m1 == m2 );
53+
}
2554
};
2655

2756
} // namespace rta

src/rawtoaces2/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ target_link_libraries ( rawtoaces2
1818
rawtoaces_util2
1919
)
2020

21+
22+
set_target_properties(rawtoaces2 PROPERTIES XCODE_SCHEME_ARGUMENTS "${CMAKE_XCODE_SCHEME_ARGUMENTS}" )
23+
2124
install( TARGETS rawtoaces2 DESTINATION bin )

src/rawtoaces_idt/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ install(FILES
4343
${PROJECT_SOURCE_DIR}/include/rawtoaces/define.h
4444
${PROJECT_SOURCE_DIR}/include/rawtoaces/mathOps.h
4545
${PROJECT_SOURCE_DIR}/include/rawtoaces/rta.h
46-
47-
DESTINATION include/rawtoaces
46+
${PROJECT_SOURCE_DIR}/include/rawtoaces/metadata.h
47+
DESTINATION include/rawtoaces
4848
)
4949

5050
install( TARGETS ${RAWTOACESIDTLIB} DESTINATION lib )

src/rawtoaces_util2/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ cmake_minimum_required(VERSION 3.10)
22
include_directories( "${CMAKE_CURRENT_SOURCE_DIR}" )
33

44
add_library ( rawtoaces_util2 ${DO_SHARED}
5+
transform_cache.cpp
6+
transform_cache.h
57
rawtoaces_util.cpp
68
${PROJECT_SOURCE_DIR}/include/rawtoaces/rawtoaces_util.h
9+
${PROJECT_SOURCE_DIR}/include/rawtoaces/cache_base.h
710
)
811

912
set_property(TARGET rawtoaces_util2 PROPERTY CXX_STANDARD 17)
@@ -31,7 +34,8 @@ set_target_properties( rawtoaces_util2 PROPERTIES
3134

3235
install(
3336
FILES
34-
${PROJECT_SOURCE_DIR}/include/rawtoaces/rawtoaces_util.h
37+
${PROJECT_SOURCE_DIR}/include/rawtoaces/rawtoaces_util.h
38+
${PROJECT_SOURCE_DIR}/include/rawtoaces/cache_base.h
3539
DESTINATION include/rawtoaces
3640
)
3741

0 commit comments

Comments
 (0)