More C++-isms

This commit is contained in:
Flössie
2020-03-09 10:33:56 +01:00
parent 5691b5498c
commit 493d7306a4
2 changed files with 75 additions and 82 deletions

View File

@@ -8,6 +8,8 @@
#include <cstring> #include <cstring>
#include <cerrno> #include <cerrno>
#include <cassert> #include <cassert>
#include <memory>
#include <vector>
#include <glibmm/fileutils.h> #include <glibmm/fileutils.h>
#include <glibmm/miscutils.h> #include <glibmm/miscutils.h>
@@ -33,9 +35,9 @@ CameraConst::CameraConst() : pdafOffset(0)
} }
bool CameraConst::parseApertureScaling(CameraConst *cc, void *ji_) bool CameraConst::parseApertureScaling(CameraConst *cc, const void *ji_)
{ {
cJSON *ji = (cJSON *)ji_; const cJSON *ji = static_cast<const cJSON *>(ji_);
if (ji->type != cJSON_Array) { if (ji->type != cJSON_Array) {
fprintf(stderr, "\"ranges\":\"aperture_scaling\" must be an array\n"); fprintf(stderr, "\"ranges\":\"aperture_scaling\" must be an array\n");
@@ -43,12 +45,14 @@ bool CameraConst::parseApertureScaling(CameraConst *cc, void *ji_)
} }
for (ji = ji->child; ji; ji = ji->next) { for (ji = ji->child; ji; ji = ji->next) {
cJSON *js = cJSON_GetObjectItem(ji, "aperture"); const cJSON *js = cJSON_GetObjectItem(ji, "aperture");
if (!js) { if (!js) {
fprintf(stderr, "missing \"ranges\":\"aperture_scaling\":\"aperture\" object item.\n"); fprintf(stderr, "missing \"ranges\":\"aperture_scaling\":\"aperture\" object item.\n");
return false; return false;
} else if (js->type != cJSON_Number) { }
if (js->type != cJSON_Number) {
fprintf(stderr, "\"ranges\":\"aperture_scaling\":\"aperture\" must be a number.\n"); fprintf(stderr, "\"ranges\":\"aperture_scaling\":\"aperture\" must be a number.\n");
return false; return false;
} }
@@ -59,28 +63,32 @@ bool CameraConst::parseApertureScaling(CameraConst *cc, void *ji_)
if (!js) { if (!js) {
fprintf(stderr, "missing \"ranges\":\"aperture_scaling\":\"scale_factor\" object item.\n"); fprintf(stderr, "missing \"ranges\":\"aperture_scaling\":\"scale_factor\" object item.\n");
return false; return false;
} else if (js->type != cJSON_Number) { }
if (js->type != cJSON_Number) {
fprintf(stderr, "\"ranges\":\"aperture_scaling\":\"scale_factor\" must be a number.\n"); fprintf(stderr, "\"ranges\":\"aperture_scaling\":\"scale_factor\" must be a number.\n");
return false; return false;
} }
const float scale_factor = js->valuedouble; const float scale_factor = js->valuedouble;
cc->mApertureScaling.insert(std::pair<float, float>(aperture, scale_factor)); cc->mApertureScaling.emplace(aperture, scale_factor);
} }
return true; return true;
} }
bool CameraConst::parseLevels(CameraConst *cc, int bw, void *ji_) bool CameraConst::parseLevels(CameraConst *cc, int bw, const void *ji_)
{ {
cJSON *ji = (cJSON *)ji_; const cJSON *ji = static_cast<const cJSON *>(ji_);
if (ji->type == cJSON_Number) { if (ji->type == cJSON_Number) {
camera_const_levels lvl; camera_const_levels lvl;
lvl.levels[0] = lvl.levels[1] = lvl.levels[2] = lvl.levels[3] = ji->valueint; lvl.levels[0] = lvl.levels[1] = lvl.levels[2] = lvl.levels[3] = ji->valueint;
cc->mLevels[bw].insert(std::pair<int, camera_const_levels>(0, lvl)); cc->mLevels[bw].emplace(0, lvl);
return true; return true;
} else if (ji->type != cJSON_Array) { }
if (ji->type != cJSON_Array) {
fprintf(stderr, "\"ranges\":\"%s\" must be a number or an array\n", bw ? "white" : "black"); fprintf(stderr, "\"ranges\":\"%s\" must be a number or an array\n", bw ? "white" : "black");
return false; return false;
} }
@@ -88,7 +96,7 @@ bool CameraConst::parseLevels(CameraConst *cc, int bw, void *ji_)
if (ji->child->type == cJSON_Number) { if (ji->child->type == cJSON_Number) {
camera_const_levels lvl; camera_const_levels lvl;
int i; int i;
cJSON *js; const cJSON *js;
for (js = ji->child, i = 0; js && i < 4; js = js->next, i++) { for (js = ji->child, i = 0; js && i < 4; js = js->next, i++) {
lvl.levels[i] = js->valueint; lvl.levels[i] = js->valueint;
@@ -103,34 +111,31 @@ bool CameraConst::parseLevels(CameraConst *cc, int bw, void *ji_)
return false; return false;
} }
cc->mLevels[bw].insert(std::pair<int, camera_const_levels>(0, lvl)); cc->mLevels[bw].emplace(0, lvl);
return true; return true;
} }
for (ji = ji->child; ji; ji = ji->next) { for (ji = ji->child; ji; ji = ji->next) {
int iso[1000] = { 0 }; const cJSON *js = cJSON_GetObjectItem(ji, "iso");
int iso_count = 0;
cJSON *js = cJSON_GetObjectItem(ji, "iso");
if (!js) { if (!js) {
fprintf(stderr, "missing \"ranges\":\"%s\":\"iso\" object item.\n", bw ? "white" : "black"); fprintf(stderr, "missing \"ranges\":\"%s\":\"iso\" object item.\n", bw ? "white" : "black");
return false; return false;
} else if (js->type == cJSON_Number) { }
iso[0] = js->valueint;
iso_count = 1;
} else if (js->type == cJSON_Array) {
int i;
for (js = js->child, i = 0; js && i < 1000; js = js->next, i++) { std::vector<int> isos;
if (js->type == cJSON_Number) {
isos.push_back(js->valueint);
} else if (js->type == cJSON_Array) {
for (js = js->child; js; js = js->next) {
if (js->type != cJSON_Number) { if (js->type != cJSON_Number) {
fprintf(stderr, "\"ranges\":\"%s\":\"iso\" must be a number or an array of numbers.\n", bw ? "white" : "black"); fprintf(stderr, "\"ranges\":\"%s\":\"iso\" must be a number or an array of numbers.\n", bw ? "white" : "black");
return false; return false;
} }
iso[i] = js->valueint; isos.push_back(js->valueint);
} }
iso_count = i;
} else { } else {
fprintf(stderr, "\"ranges\":\"%s\":\"iso\" must be an array or a number.\n", bw ? "white" : "black"); fprintf(stderr, "\"ranges\":\"%s\":\"iso\" must be an array or a number.\n", bw ? "white" : "black");
return false; return false;
@@ -172,28 +177,27 @@ bool CameraConst::parseLevels(CameraConst *cc, int bw, void *ji_)
return false; return false;
} }
for (int i = 0; i < iso_count; i++) { for (auto iso : isos) {
cc->mLevels[bw].insert(std::pair<int, camera_const_levels>(iso[i], lvl)); cc->mLevels[bw].emplace(iso, lvl);
} }
} }
return true; return true;
} }
CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model) CameraConst* CameraConst::parseEntry(const void *cJSON_, const char *make_model)
{ {
cJSON *js, *ji, *jranges; const cJSON *js = static_cast<const cJSON*>(cJSON_);
js = (cJSON *)cJSON_;
CameraConst *cc = new CameraConst; std::unique_ptr<CameraConst> cc(new CameraConst);
cc->make_model = make_model; cc->make_model = make_model;
ji = cJSON_GetObjectItem(js, "dcraw_matrix"); const cJSON *ji = cJSON_GetObjectItem(js, "dcraw_matrix");
if (ji) { if (ji) {
if (ji->type != cJSON_Array) { if (ji->type != cJSON_Array) {
fprintf(stderr, "\"dcraw_matrix\" must be an array\n"); fprintf(stderr, "\"dcraw_matrix\" must be an array\n");
goto parse_error; return nullptr;
} }
int i; int i;
@@ -201,10 +205,10 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
for (i = 0, ji = ji->child; i < 12 && ji; i++, ji = ji->next) { for (i = 0, ji = ji->child; i < 12 && ji; i++, ji = ji->next) {
if (ji->type != cJSON_Number) { if (ji->type != cJSON_Number) {
fprintf(stderr, "\"dcraw_matrix\" array must contain numbers\n"); fprintf(stderr, "\"dcraw_matrix\" array must contain numbers\n");
goto parse_error; return nullptr;
} }
cc->dcraw_matrix[i] = (short)ji->valueint; cc->dcraw_matrix[i] = ji->valueint;
} }
} }
@@ -213,7 +217,7 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
if (ji) { if (ji) {
if (ji->type != cJSON_Array) { if (ji->type != cJSON_Array) {
fprintf(stderr, "\"raw_crop\" must be an array\n"); fprintf(stderr, "\"raw_crop\" must be an array\n");
goto parse_error; return nullptr;
} }
int i; int i;
@@ -221,7 +225,7 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
for (i = 0, ji = ji->child; i < 4 && ji; i++, ji = ji->next) { for (i = 0, ji = ji->child; i < 4 && ji; i++, ji = ji->next) {
if (ji->type != cJSON_Number) { if (ji->type != cJSON_Number) {
fprintf(stderr, "\"raw_crop\" array must contain numbers\n"); fprintf(stderr, "\"raw_crop\" array must contain numbers\n");
goto parse_error; return nullptr;
} }
cc->raw_crop[i] = ji->valueint; cc->raw_crop[i] = ji->valueint;
@@ -229,7 +233,7 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
if (i != 4 || ji) { if (i != 4 || ji) {
fprintf(stderr, "\"raw_crop\" must contain 4 numbers\n"); fprintf(stderr, "\"raw_crop\" must contain 4 numbers\n");
goto parse_error; return nullptr;
} }
} }
@@ -238,7 +242,7 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
if (ji) { if (ji) {
if (ji->type != cJSON_Array) { if (ji->type != cJSON_Array) {
fprintf(stderr, "\"masked_areas\" must be an array\n"); fprintf(stderr, "\"masked_areas\" must be an array\n");
goto parse_error; return nullptr;
} }
int i; int i;
@@ -246,7 +250,7 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
for (i = 0, ji = ji->child; i < 2 * 4 && ji; i++, ji = ji->next) { for (i = 0, ji = ji->child; i < 2 * 4 && ji; i++, ji = ji->next) {
if (ji->type != cJSON_Number) { if (ji->type != cJSON_Number) {
fprintf(stderr, "\"masked_areas\" array must contain numbers\n"); fprintf(stderr, "\"masked_areas\" array must contain numbers\n");
goto parse_error; return nullptr;
} }
cc->raw_mask[i / 4][i % 4] = ji->valueint; cc->raw_mask[i / 4][i % 4] = ji->valueint;
@@ -254,27 +258,23 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
if (i % 4 != 0) { if (i % 4 != 0) {
fprintf(stderr, "\"masked_areas\" array length must be divisible by 4\n"); fprintf(stderr, "\"masked_areas\" array length must be divisible by 4\n");
goto parse_error; return nullptr;
} }
} }
jranges = cJSON_GetObjectItem(js, "ranges"); const cJSON *jranges = cJSON_GetObjectItem(js, "ranges");
if (jranges) { if (jranges) {
ji = cJSON_GetObjectItem(jranges, "black"); ji = cJSON_GetObjectItem(jranges, "black");
if (ji) { if (ji && !parseLevels(cc.get(), 0, ji)) {
if (!parseLevels(cc, 0, ji)) { return nullptr;
goto parse_error;
}
} }
ji = cJSON_GetObjectItem(jranges, "white"); ji = cJSON_GetObjectItem(jranges, "white");
if (ji) { if (ji && !parseLevels(cc.get(), 1, ji)) {
if (!parseLevels(cc, 1, ji)) { return nullptr;
goto parse_error;
}
} }
ji = cJSON_GetObjectItem(jranges, "white_max"); ji = cJSON_GetObjectItem(jranges, "white_max");
@@ -282,18 +282,16 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
if (ji) { if (ji) {
if (ji->type != cJSON_Number) { if (ji->type != cJSON_Number) {
fprintf(stderr, "\"ranges\":\"white_max\" must be a number\n"); fprintf(stderr, "\"ranges\":\"white_max\" must be a number\n");
goto parse_error; return nullptr;
} }
cc->white_max = (int)ji->valueint; cc->white_max = ji->valueint;
} }
ji = cJSON_GetObjectItem(jranges, "aperture_scaling"); ji = cJSON_GetObjectItem(jranges, "aperture_scaling");
if (ji) { if (ji && !parseApertureScaling(cc.get(), ji)) {
if (!parseApertureScaling(cc, ji)) { return nullptr;
goto parse_error;
}
} }
} }
@@ -303,9 +301,9 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
if (!cc->get_Levels(lvl, bw, 0, 0)) { if (!cc->get_Levels(lvl, bw, 0, 0)) {
const auto it = cc->mLevels[bw].cbegin(); const auto it = cc->mLevels[bw].cbegin();
if (it != cc->mLevels[bw].end()) { if (it != cc->mLevels[bw].cend()) {
// insert levels with lowest iso as the default (iso 0) // insert levels with lowest iso as the default (iso 0)
cc->mLevels[bw].insert(std::pair<int, camera_const_levels>(0, it->second)); cc->mLevels[bw].emplace(0, it->second);
} }
} }
} }
@@ -315,13 +313,13 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
if (ji) { if (ji) {
if (ji->type != cJSON_Array) { if (ji->type != cJSON_Array) {
fprintf(stderr, "\"pdaf_pattern\" must be an array\n"); fprintf(stderr, "\"pdaf_pattern\" must be an array\n");
goto parse_error; return nullptr;
} }
for (ji = ji->child; ji; ji = ji->next) { for (ji = ji->child; ji; ji = ji->next) {
if (ji->type != cJSON_Number) { if (ji->type != cJSON_Number) {
fprintf(stderr, "\"pdaf_pattern\" array must contain numbers\n"); fprintf(stderr, "\"pdaf_pattern\" array must contain numbers\n");
goto parse_error; return nullptr;
} }
cc->pdafPattern.push_back(ji->valueint); cc->pdafPattern.push_back(ji->valueint);
@@ -333,7 +331,7 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
if (ji) { if (ji) {
if (ji->type != cJSON_Number) { if (ji->type != cJSON_Number) {
fprintf(stderr, "\"pdaf_offset\" must contain a number\n"); fprintf(stderr, "\"pdaf_offset\" must contain a number\n");
goto parse_error; return nullptr;
} }
cc->pdafOffset = ji->valueint; cc->pdafOffset = ji->valueint;
@@ -344,17 +342,13 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model)
if (ji) { if (ji) {
if (ji->type != cJSON_False && ji->type != cJSON_True) { if (ji->type != cJSON_False && ji->type != cJSON_True) {
fprintf(stderr, "\"global_green_equilibration\" must be a boolean\n"); fprintf(stderr, "\"global_green_equilibration\" must be a boolean\n");
goto parse_error; return nullptr;
} }
cc->globalGreenEquilibration = (ji->type == cJSON_True); cc->globalGreenEquilibration = (ji->type == cJSON_True);
} }
return cc; return cc.release();
parse_error:
delete cc;
return nullptr;
} }
bool CameraConst::has_dcrawMatrix() const bool CameraConst::has_dcrawMatrix() const
@@ -392,6 +386,7 @@ void CameraConst::update_pdafPattern(const std::vector<int> &other)
if (other.empty()) { if (other.empty()) {
return; return;
} }
pdafPattern = other; pdafPattern = other;
} }
@@ -400,6 +395,7 @@ void CameraConst::update_pdafOffset(int other)
if (other == 0) { if (other == 0) {
return; return;
} }
pdafOffset = other; pdafOffset = other;
} }
@@ -445,18 +441,15 @@ void CameraConst::update_Levels(const CameraConst *other)
return; return;
} }
if (other->mLevels[0].size()) { if (!other->mLevels[0].empty()) {
mLevels[0].clear();
mLevels[0] = other->mLevels[0]; mLevels[0] = other->mLevels[0];
} }
if (other->mLevels[1].size()) { if (!other->mLevels[1].empty()) {
mLevels[1].clear();
mLevels[1] = other->mLevels[1]; mLevels[1] = other->mLevels[1];
} }
if (other->mApertureScaling.size()) { if (!other->mApertureScaling.empty()) {
mApertureScaling.clear();
mApertureScaling = other->mApertureScaling; mApertureScaling = other->mApertureScaling;
} }
@@ -502,7 +495,7 @@ bool CameraConst::get_Levels(camera_const_levels & lvl, int bw, int iso, float f
lvl = it->second; lvl = it->second;
if (bw == 1 && fnumber > 0 && mApertureScaling.size() > 0) { if (bw == 1 && fnumber > 0 && !mApertureScaling.empty()) {
std::map<float, float>::const_iterator scaleIt = mApertureScaling.find(fnumber); std::map<float, float>::const_iterator scaleIt = mApertureScaling.find(fnumber);
if (scaleIt == mApertureScaling.end()) { if (scaleIt == mApertureScaling.end()) {
@@ -655,7 +648,7 @@ bool CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& file
cJSON_Minify(buf); cJSON_Minify(buf);
// parse // parse
cJSON *jsroot = cJSON_Parse(buf); cJSON* const jsroot = cJSON_Parse(buf);
if (!jsroot) { if (!jsroot) {
char str[128]; char str[128];
@@ -674,7 +667,7 @@ bool CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& file
free(buf); free(buf);
cJSON *js = cJSON_GetObjectItem(jsroot, "camera_constants"); const cJSON *js = cJSON_GetObjectItem(jsroot, "camera_constants");
if (!js) { if (!js) {
fprintf(stderr, "missing \"camera_constants\" object item\n"); fprintf(stderr, "missing \"camera_constants\" object item\n");
@@ -682,7 +675,7 @@ bool CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& file
} }
for (js = js->child; js; js = js->next) { for (js = js->child; js; js = js->next) {
cJSON *ji = cJSON_GetObjectItem(js, "make_model"); const cJSON *ji = cJSON_GetObjectItem(js, "make_model");
if (!ji) { if (!ji) {
fprintf(stderr, "missing \"make_model\" object item\n"); fprintf(stderr, "missing \"make_model\" object item\n");
@@ -702,7 +695,7 @@ bool CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& file
goto parse_error; goto parse_error;
} }
CameraConst *cc = CameraConst::parseEntry((void *)js, ji->valuestring); CameraConst* const cc = CameraConst::parseEntry((const void *)js, ji->valuestring);
if (!cc) { if (!cc) {
goto parse_error; goto parse_error;
@@ -719,7 +712,7 @@ bool CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& file
} }
} else { } else {
// The CameraConst already exist for this camera make/model -> we merge the values // The CameraConst already exist for this camera make/model -> we merge the values
CameraConst *existingcc = entry.first->second; CameraConst* const existingcc = entry.first->second;
// updating the dcraw matrix // updating the dcraw matrix
existingcc->update_dcrawMatrix(cc->get_dcrawMatrix()); existingcc->update_dcrawMatrix(cc->get_dcrawMatrix());
@@ -771,7 +764,7 @@ void CameraConstantsStore::init(const Glib::ustring& baseDir, const Glib::ustrin
{ {
parse_camera_constants_file(Glib::build_filename(baseDir, "camconst.json")); parse_camera_constants_file(Glib::build_filename(baseDir, "camconst.json"));
Glib::ustring userFile(Glib::build_filename(userSettingsDir, "camconst.json")); const Glib::ustring userFile(Glib::build_filename(userSettingsDir, "camconst.json"));
if (Glib::file_test(userFile, Glib::FILE_TEST_EXISTS)) { if (Glib::file_test(userFile, Glib::FILE_TEST_EXISTS)) {
parse_camera_constants_file(userFile); parse_camera_constants_file(userFile);

View File

@@ -36,12 +36,12 @@ private:
int globalGreenEquilibration; int globalGreenEquilibration;
CameraConst(); CameraConst();
static bool parseLevels(CameraConst *cc, int bw, void *ji); static bool parseLevels(CameraConst *cc, int bw, const void *ji);
static bool parseApertureScaling(CameraConst *cc, void *ji); static bool parseApertureScaling(CameraConst *cc, const void *ji);
bool get_Levels(camera_const_levels & lvl, int bw, int iso, float fnumber) const; bool get_Levels(camera_const_levels & lvl, int bw, int iso, float fnumber) const;
public: public:
static CameraConst *parseEntry(void *cJSON, const char *make_model); static CameraConst *parseEntry(const void *cJSON, const char *make_model);
bool has_dcrawMatrix(void) const; bool has_dcrawMatrix(void) const;
void update_dcrawMatrix(const short *other); void update_dcrawMatrix(const short *other);
const short *get_dcrawMatrix(void) const; const short *get_dcrawMatrix(void) const;