diff --git a/rtengine/camconst.cc b/rtengine/camconst.cc index 1e303ba73..aab2a252c 100644 --- a/rtengine/camconst.cc +++ b/rtengine/camconst.cc @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include @@ -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(ji_); if (ji->type != cJSON_Array) { 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) { - cJSON *js = cJSON_GetObjectItem(ji, "aperture"); + const cJSON *js = cJSON_GetObjectItem(ji, "aperture"); if (!js) { fprintf(stderr, "missing \"ranges\":\"aperture_scaling\":\"aperture\" object item.\n"); return false; - } else if (js->type != cJSON_Number) { + } + + if (js->type != cJSON_Number) { fprintf(stderr, "\"ranges\":\"aperture_scaling\":\"aperture\" must be a number.\n"); return false; } @@ -59,28 +63,32 @@ bool CameraConst::parseApertureScaling(CameraConst *cc, void *ji_) if (!js) { fprintf(stderr, "missing \"ranges\":\"aperture_scaling\":\"scale_factor\" object item.\n"); 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"); return false; } const float scale_factor = js->valuedouble; - cc->mApertureScaling.insert(std::pair(aperture, scale_factor)); + cc->mApertureScaling.emplace(aperture, scale_factor); } 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(ji_); if (ji->type == cJSON_Number) { camera_const_levels lvl; lvl.levels[0] = lvl.levels[1] = lvl.levels[2] = lvl.levels[3] = ji->valueint; - cc->mLevels[bw].insert(std::pair(0, lvl)); + cc->mLevels[bw].emplace(0, lvl); 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"); return false; } @@ -88,7 +96,7 @@ bool CameraConst::parseLevels(CameraConst *cc, int bw, void *ji_) if (ji->child->type == cJSON_Number) { camera_const_levels lvl; int i; - cJSON *js; + const cJSON *js; for (js = ji->child, i = 0; js && i < 4; js = js->next, i++) { lvl.levels[i] = js->valueint; @@ -103,34 +111,31 @@ bool CameraConst::parseLevels(CameraConst *cc, int bw, void *ji_) return false; } - cc->mLevels[bw].insert(std::pair(0, lvl)); + cc->mLevels[bw].emplace(0, lvl); return true; } for (ji = ji->child; ji; ji = ji->next) { - int iso[1000] = { 0 }; - int iso_count = 0; - cJSON *js = cJSON_GetObjectItem(ji, "iso"); + const cJSON *js = cJSON_GetObjectItem(ji, "iso"); if (!js) { fprintf(stderr, "missing \"ranges\":\"%s\":\"iso\" object item.\n", bw ? "white" : "black"); 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 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) { fprintf(stderr, "\"ranges\":\"%s\":\"iso\" must be a number or an array of numbers.\n", bw ? "white" : "black"); return false; } - iso[i] = js->valueint; + isos.push_back(js->valueint); } - - iso_count = i; } else { fprintf(stderr, "\"ranges\":\"%s\":\"iso\" must be an array or a number.\n", bw ? "white" : "black"); return false; @@ -172,28 +177,27 @@ bool CameraConst::parseLevels(CameraConst *cc, int bw, void *ji_) return false; } - for (int i = 0; i < iso_count; i++) { - cc->mLevels[bw].insert(std::pair(iso[i], lvl)); + for (auto iso : isos) { + cc->mLevels[bw].emplace(iso, lvl); } } 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; - js = (cJSON *)cJSON_; + const cJSON *js = static_cast(cJSON_); - CameraConst *cc = new CameraConst; + std::unique_ptr cc(new CameraConst); cc->make_model = make_model; - ji = cJSON_GetObjectItem(js, "dcraw_matrix"); + const cJSON *ji = cJSON_GetObjectItem(js, "dcraw_matrix"); if (ji) { if (ji->type != cJSON_Array) { fprintf(stderr, "\"dcraw_matrix\" must be an array\n"); - goto parse_error; + return nullptr; } 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) { if (ji->type != cJSON_Number) { 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->type != cJSON_Array) { fprintf(stderr, "\"raw_crop\" must be an array\n"); - goto parse_error; + return nullptr; } 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) { if (ji->type != cJSON_Number) { fprintf(stderr, "\"raw_crop\" array must contain numbers\n"); - goto parse_error; + return nullptr; } cc->raw_crop[i] = ji->valueint; @@ -229,7 +233,7 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model) if (i != 4 || ji) { 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->type != cJSON_Array) { fprintf(stderr, "\"masked_areas\" must be an array\n"); - goto parse_error; + return nullptr; } 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) { if (ji->type != cJSON_Number) { fprintf(stderr, "\"masked_areas\" array must contain numbers\n"); - goto parse_error; + return nullptr; } 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) { 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) { ji = cJSON_GetObjectItem(jranges, "black"); - if (ji) { - if (!parseLevels(cc, 0, ji)) { - goto parse_error; - } + if (ji && !parseLevels(cc.get(), 0, ji)) { + return nullptr; } ji = cJSON_GetObjectItem(jranges, "white"); - if (ji) { - if (!parseLevels(cc, 1, ji)) { - goto parse_error; - } + if (ji && !parseLevels(cc.get(), 1, ji)) { + return nullptr; } ji = cJSON_GetObjectItem(jranges, "white_max"); @@ -282,18 +282,16 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model) if (ji) { if (ji->type != cJSON_Number) { 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"); - if (ji) { - if (!parseApertureScaling(cc, ji)) { - goto parse_error; - } + if (ji && !parseApertureScaling(cc.get(), ji)) { + return nullptr; } } @@ -303,9 +301,9 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model) if (!cc->get_Levels(lvl, bw, 0, 0)) { 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) - cc->mLevels[bw].insert(std::pair(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->type != cJSON_Array) { fprintf(stderr, "\"pdaf_pattern\" must be an array\n"); - goto parse_error; + return nullptr; } for (ji = ji->child; ji; ji = ji->next) { if (ji->type != cJSON_Number) { fprintf(stderr, "\"pdaf_pattern\" array must contain numbers\n"); - goto parse_error; + return nullptr; } cc->pdafPattern.push_back(ji->valueint); @@ -333,7 +331,7 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model) if (ji) { if (ji->type != cJSON_Number) { fprintf(stderr, "\"pdaf_offset\" must contain a number\n"); - goto parse_error; + return nullptr; } cc->pdafOffset = ji->valueint; @@ -344,17 +342,13 @@ CameraConst* CameraConst::parseEntry(void *cJSON_, const char *make_model) if (ji) { if (ji->type != cJSON_False && ji->type != cJSON_True) { fprintf(stderr, "\"global_green_equilibration\" must be a boolean\n"); - goto parse_error; + return nullptr; } cc->globalGreenEquilibration = (ji->type == cJSON_True); } - return cc; - -parse_error: - delete cc; - return nullptr; + return cc.release(); } bool CameraConst::has_dcrawMatrix() const @@ -392,6 +386,7 @@ void CameraConst::update_pdafPattern(const std::vector &other) if (other.empty()) { return; } + pdafPattern = other; } @@ -400,6 +395,7 @@ void CameraConst::update_pdafOffset(int other) if (other == 0) { return; } + pdafOffset = other; } @@ -445,18 +441,15 @@ void CameraConst::update_Levels(const CameraConst *other) return; } - if (other->mLevels[0].size()) { - mLevels[0].clear(); + if (!other->mLevels[0].empty()) { mLevels[0] = other->mLevels[0]; } - if (other->mLevels[1].size()) { - mLevels[1].clear(); + if (!other->mLevels[1].empty()) { mLevels[1] = other->mLevels[1]; } - if (other->mApertureScaling.size()) { - mApertureScaling.clear(); + if (!other->mApertureScaling.empty()) { mApertureScaling = other->mApertureScaling; } @@ -502,7 +495,7 @@ bool CameraConst::get_Levels(camera_const_levels & lvl, int bw, int iso, float f lvl = it->second; - if (bw == 1 && fnumber > 0 && mApertureScaling.size() > 0) { + if (bw == 1 && fnumber > 0 && !mApertureScaling.empty()) { std::map::const_iterator scaleIt = mApertureScaling.find(fnumber); if (scaleIt == mApertureScaling.end()) { @@ -655,7 +648,7 @@ bool CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& file cJSON_Minify(buf); // parse - cJSON *jsroot = cJSON_Parse(buf); + cJSON* const jsroot = cJSON_Parse(buf); if (!jsroot) { char str[128]; @@ -674,7 +667,7 @@ bool CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& file free(buf); - cJSON *js = cJSON_GetObjectItem(jsroot, "camera_constants"); + const cJSON *js = cJSON_GetObjectItem(jsroot, "camera_constants"); if (!js) { 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) { - cJSON *ji = cJSON_GetObjectItem(js, "make_model"); + const cJSON *ji = cJSON_GetObjectItem(js, "make_model"); if (!ji) { 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; } - CameraConst *cc = CameraConst::parseEntry((void *)js, ji->valuestring); + CameraConst* const cc = CameraConst::parseEntry((const void *)js, ji->valuestring); if (!cc) { goto parse_error; @@ -719,7 +712,7 @@ bool CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& file } } else { // 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 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")); - 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)) { parse_camera_constants_file(userFile); diff --git a/rtengine/camconst.h b/rtengine/camconst.h index b959481ba..aa0702439 100644 --- a/rtengine/camconst.h +++ b/rtengine/camconst.h @@ -36,12 +36,12 @@ private: int globalGreenEquilibration; CameraConst(); - static bool parseLevels(CameraConst *cc, int bw, void *ji); - static bool parseApertureScaling(CameraConst *cc, void *ji); + static bool parseLevels(CameraConst *cc, int bw, const void *ji); + static bool parseApertureScaling(CameraConst *cc, const void *ji); bool get_Levels(camera_const_levels & lvl, int bw, int iso, float fnumber) const; 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; void update_dcrawMatrix(const short *other); const short *get_dcrawMatrix(void) const;