Skip to content

Commit 9b5349c

Browse files
committed
backend: Remove use of std::vector from the default config
Switch to using std::array types for the default config parameters read from the JSON config file. This allows the BackEnd class to be standard layout compliant. Signed-off-by: Naushir Patuck <[email protected]>
1 parent b611b1d commit 9b5349c

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/libpisp/backend/backend.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <string>
1313
#include <type_traits>
1414
#include <utility>
15-
#include <vector>
1615

1716
#include "common/shm_mutex.hpp"
1817
#include "tiling/pisp_tiling.hpp"
@@ -27,6 +26,11 @@ namespace libpisp
2726

2827
using TileArray = std::array<pisp_tile, PISP_BACK_END_NUM_TILES>;
2928

29+
// We use std::array<std::pair<.,.>> insead of std::map<.,.> to ensure this object provides a standard layout.
30+
using YcbcrMap = std::array<std::pair<std::string, pisp_be_ccm_config>, 16>;
31+
using ResampleMap = std::array<std::pair<std::string, pisp_be_resample_config>, 16>;
32+
using ResampleList = std::array<std::pair<double, std::string>, 16>;
33+
3034
class BackEnd final
3135
{
3236
public:
@@ -188,11 +192,10 @@ class BackEnd final
188192
uint32_t smart_resize_dirty_;
189193

190194
// Default config
191-
// We use std::vector<std::pair<.,.>> insead of std::map<.,.> to ensure this object provides a standard layout.
192-
std::vector<std::pair<std::string, pisp_be_ccm_config>> ycbcr_map_;
193-
std::vector<std::pair<std::string, pisp_be_ccm_config>> inverse_ycbcr_map_;
194-
std::vector<std::pair<std::string, pisp_be_resample_config>> resample_filter_map_;
195-
std::vector<std::pair<double, std::string>> resample_select_list_;
195+
YcbcrMap ycbcr_map_;
196+
YcbcrMap inverse_ycbcr_map_;
197+
ResampleMap resample_filter_map_;
198+
ResampleList resample_select_list_;
196199
pisp_be_sharpen_config default_sharpen_;
197200
pisp_be_sh_fc_combine_config default_shfc_;
198201
};

src/libpisp/backend/backend_default_config.cpp

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ void initialise_gamma(pisp_be_gamma_config &gamma, const json &root)
133133
}
134134
}
135135

136-
void read_resample(std::vector<std::pair<std::string, pisp_be_resample_config>> &resample_filter_map,
137-
std::vector<std::pair<double, std::string>> &resample_select_list, const json &root)
136+
void read_resample(libpisp::ResampleMap &resample_filter_map, libpisp::ResampleList &resample_select_list,
137+
const json &root)
138138
{
139139
auto &filters = root["resample"]["filters"];
140+
unsigned int i = 0, j = 0;
140141

141142
for (auto const &[name, filter] : filters.items())
142143
{
@@ -148,22 +149,28 @@ void read_resample(std::vector<std::pair<std::string, pisp_be_resample_config>>
148149
throw std::runtime_error("read_resample: Incorrect number of filter coefficients");
149150

150151
memcpy(r.coef, coefs.data(), sizeof(r.coef));
151-
resample_filter_map.emplace_back(name, r);
152+
resample_filter_map[i++] = { name, r };
153+
if (i == resample_filter_map.size())
154+
break;
152155
}
153156

157+
i = 0;
154158
auto &smart = root["resample"]["smart_selection"];
155159
for (auto &scale : smart["downscale"])
156-
resample_select_list.emplace_back(scale.get<double>(), std::string {});
160+
{
161+
resample_select_list[i++] = { scale.get<double>(), std::string {} };
162+
if (i == resample_select_list.size())
163+
break;
164+
}
157165

158-
unsigned int i = 0;
159166
for (auto &filter : smart["filter"])
160167
{
161-
resample_select_list[i].second = filter.get<std::string>();
162-
if (++i == resample_select_list.size())
168+
resample_select_list[j++].second = filter.get<std::string>();
169+
if (j == resample_select_list.size())
163170
break;
164171
}
165-
if (i != resample_select_list.size())
166-
throw std::runtime_error("read_resample: Incorrect number of filters");
172+
if (j != i)
173+
throw std::runtime_error("read_resample: Incorrect number of smart filters/downscale factors");
167174
}
168175

169176
// Macros for the sharpening filters, to avoid repeating the same code 5 times
@@ -222,10 +229,10 @@ void read_sharpen(pisp_be_sharpen_config &sharpen, pisp_be_sh_fc_combine_config
222229
shfc.y_factor = params["shfc_y_factor"].get<double>() * (1 << 8);
223230
}
224231

225-
void read_ycbcr(std::vector<std::pair<std::string, pisp_be_ccm_config>> &ycbcr_map,
226-
std::vector<std::pair<std::string, pisp_be_ccm_config>> &inverse_ycbcr_map, const json &root)
232+
void read_ycbcr(libpisp::YcbcrMap &ycbcr_map, libpisp::YcbcrMap &inverse_ycbcr_map, const json &root)
227233
{
228234
auto encoding = root["colour_encoding"];
235+
unsigned int i = 0;
229236

230237
for (auto const &[format, enc] : encoding.items())
231238
{
@@ -247,15 +254,17 @@ void read_ycbcr(std::vector<std::pair<std::string, pisp_be_ccm_config>> &ycbcr_m
247254
memcpy(ccm.offsets, offsets.data(), sizeof(ccm.offsets));
248255

249256
if (key == "ycbcr")
250-
ycbcr_map.emplace_back(format, ccm);
257+
ycbcr_map[i] = { format, ccm };
251258
else
252-
inverse_ycbcr_map.emplace_back(format, ccm);
259+
inverse_ycbcr_map[i] = { format, ccm };
253260
}
261+
262+
if (++i == ycbcr_map.size())
263+
break;
254264
}
255265
}
256266

257-
void get_matrix(pisp_be_ccm_config &matrix, const std::vector<std::pair<std::string, pisp_be_ccm_config>> &map,
258-
const std::string &colour_space)
267+
void get_matrix(pisp_be_ccm_config &matrix, const libpisp::YcbcrMap &map, const std::string &colour_space)
259268
{
260269
memset(matrix.coeffs, 0, sizeof(matrix.coeffs));
261270
memset(matrix.offsets, 0, sizeof(matrix.offsets));

0 commit comments

Comments
 (0)