Merge pull request #5684 from Beep6581/camconst_review (with @heckflosse's permission)

camconst.cc/h : code review
This commit is contained in:
Floessie 2020-03-09 13:13:55 +01:00 committed by GitHub
commit 5e4dc51c80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 186 additions and 344 deletions

View File

@ -2,20 +2,25 @@
* This file is part of RawTherapee.
*/
#include "camconst.h"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cerrno>
#include <cassert>
#include <memory>
#include <vector>
#include <glibmm/fileutils.h>
#include <glibmm/miscutils.h>
#include <glibmm/ustring.h>
#include "settings.h"
#include "rt_math.h"
#include <cstdio>
#include <cstring>
// cJSON is a very minimal JSON parser lib in C, not for threaded stuff etc, so if we're going to use JSON more than just
// here we should probably replace cJSON with something beefier.
#include "cJSON.h"
#include <errno.h>
#include <assert.h>
#include <inttypes.h>
namespace rtengine
{
@ -30,66 +35,70 @@ 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) {
fprintf(stderr, "\"ranges\":\"aperture_scaling\" must be an array\n");
return false;
}
for (ji = ji->child; ji != nullptr; ji = ji->next) {
cJSON *js = cJSON_GetObjectItem(ji, "aperture");
for (ji = ji->child; ji; ji = ji->next) {
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;
}
float aperture = (float)js->valuedouble;
const float aperture = js->valuedouble;
js = cJSON_GetObjectItem(ji, "scale_factor");
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;
}
float scale_factor = (float)js->valuedouble;
cc->mApertureScaling.insert(std::pair<float, float>(aperture, scale_factor));
const float scale_factor = js->valuedouble;
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<const cJSON *>(ji_);
if (ji->type == cJSON_Number) {
struct camera_const_levels lvl;
camera_const_levels lvl;
lvl.levels[0] = lvl.levels[1] = lvl.levels[2] = lvl.levels[3] = ji->valueint;
cc->mLevels[bw].insert(std::pair<int, struct camera_const_levels>(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;
}
if (ji->child->type == cJSON_Number) {
struct camera_const_levels lvl;
camera_const_levels lvl;
int i;
cJSON *js;
const cJSON *js;
for (js = ji->child, i = 0; js != nullptr && i < 4; js = js->next, i++) {
for (js = ji->child, i = 0; js && i < 4; js = js->next, i++) {
lvl.levels[i] = js->valueint;
}
@ -97,39 +106,36 @@ CameraConst::parseLevels(CameraConst *cc, int bw, void *ji_)
lvl.levels[3] = lvl.levels[1]; // G2 = G1
} else if (i == 1) {
lvl.levels[3] = lvl.levels[2] = lvl.levels[1] = lvl.levels[0];
} else if (i != 4 || js != nullptr) {
} else if (i != 4 || js) {
fprintf(stderr, "\"ranges\":\"%s\" array must have 1, 3 or 4 numbers.\n", bw ? "white" : "black");
return false;
}
cc->mLevels[bw].insert(std::pair<int, struct camera_const_levels>(0, lvl));
cc->mLevels[bw].emplace(0, lvl);
return true;
}
for (ji = ji->child; ji != nullptr; ji = ji->next) {
int iso[1000] = { 0 };
int iso_count = 0;
cJSON *js = cJSON_GetObjectItem(ji, "iso");
for (ji = ji->child; ji; ji = ji->next) {
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 != nullptr && 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) {
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;
@ -142,14 +148,14 @@ CameraConst::parseLevels(CameraConst *cc, int bw, void *ji_)
return false;
}
struct camera_const_levels lvl;
camera_const_levels lvl;
if (js->type == cJSON_Number) {
lvl.levels[0] = lvl.levels[1] = lvl.levels[2] = lvl.levels[3] = js->valueint;
} else if (js->type == cJSON_Array) {
int i;
for (js = js->child, i = 0; js != nullptr && i < 4; js = js->next, i++) {
for (js = js->child, i = 0; js && i < 4; js = js->next, i++) {
if (js->type != cJSON_Number) {
fprintf(stderr, "\"ranges\":\"%s\":\"levels\" must be a number or an array of numbers.\n", bw ? "white" : "black");
return false;
@ -162,7 +168,7 @@ CameraConst::parseLevels(CameraConst *cc, int bw, void *ji_)
lvl.levels[3] = lvl.levels[1]; // G2 = G1
} else if (i == 1) {
lvl.levels[3] = lvl.levels[2] = lvl.levels[1] = lvl.levels[0];
} else if (i != 4 || js != nullptr) {
} else if (i != 4 || js) {
fprintf(stderr, "\"ranges\":\"%s\":\"levels\" array must have 1, 3 or 4 numbers.\n", bw ? "white" : "black");
return false;
}
@ -171,40 +177,38 @@ CameraConst::parseLevels(CameraConst *cc, int bw, void *ji_)
return false;
}
for (int i = 0; i < iso_count; i++) {
cc->mLevels[bw].insert(std::pair<int, struct camera_const_levels>(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<const cJSON*>(cJSON_);
CameraConst *cc = new CameraConst;
std::unique_ptr<CameraConst> 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;
for (i = 0, ji = ji->child; i < 12 && ji != nullptr; i++, ji = ji->next) {
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,23 +217,23 @@ 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;
for (i = 0, ji = ji->child; i < 4 && ji != nullptr; i++, ji = ji->next) {
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;
}
if (i != 4 || ji != nullptr) {
if (i != 4 || ji) {
fprintf(stderr, "\"raw_crop\" must contain 4 numbers\n");
goto parse_error;
return nullptr;
}
}
@ -238,15 +242,15 @@ 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;
for (i = 0, ji = ji->child; i < 8 * 4 && ji != nullptr; i++, ji = ji->next) {
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::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,32 +282,28 @@ 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;
}
}
for (int bw = 0; bw < 2; bw++) {
struct camera_const_levels lvl;
camera_const_levels lvl;
if (!cc->get_Levels(lvl, bw, 0, 0)) {
std::map<int, struct camera_const_levels>::iterator it;
it = cc->mLevels[bw].begin();
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)
struct camera_const_levels lvl = it->second;
cc->mLevels[bw].insert(std::pair<int, struct camera_const_levels>(0, lvl));
cc->mLevels[bw].emplace(0, it->second);
}
}
}
@ -317,13 +313,13 @@ 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 != nullptr; ji = ji->next) {
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);
@ -335,7 +331,7 @@ 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;
@ -346,27 +342,21 @@ 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()
bool CameraConst::has_dcrawMatrix() const
{
return dcraw_matrix[0] != 0;
}
void
CameraConst::update_dcrawMatrix(const short *other)
void CameraConst::update_dcrawMatrix(const short *other)
{
if (!other) {
return;
@ -377,8 +367,7 @@ CameraConst::update_dcrawMatrix(const short *other)
}
}
const short *
CameraConst::get_dcrawMatrix()
const short* CameraConst::get_dcrawMatrix() const
{
if (!has_dcrawMatrix()) {
return nullptr;
@ -387,44 +376,35 @@ CameraConst::get_dcrawMatrix()
return dcraw_matrix;
}
bool
CameraConst::has_pdafPattern()
{
return pdafPattern.size() > 0;
}
std::vector<int>
CameraConst::get_pdafPattern()
const std::vector<int>& CameraConst::get_pdafPattern() const
{
return pdafPattern;
}
void
CameraConst::update_pdafPattern(const std::vector<int> &other)
void CameraConst::update_pdafPattern(const std::vector<int> &other)
{
if (other.empty()) {
return;
}
pdafPattern = other;
}
void
CameraConst::update_pdafOffset(int other)
void CameraConst::update_pdafOffset(int other)
{
if (other == 0) {
return;
}
pdafOffset = other;
}
bool
CameraConst::has_rawCrop()
bool CameraConst::has_rawCrop() const
{
return raw_crop[0] != 0 || raw_crop[1] != 0 || raw_crop[2] != 0 || raw_crop[3] != 0;
}
void
CameraConst::get_rawCrop(int& left_margin, int& top_margin, int& width, int& height)
void CameraConst::get_rawCrop(int& left_margin, int& top_margin, int& width, int& height) const
{
left_margin = raw_crop[0];
top_margin = raw_crop[1];
@ -432,22 +412,20 @@ CameraConst::get_rawCrop(int& left_margin, int& top_margin, int& width, int& hei
height = raw_crop[3];
}
bool
CameraConst::has_rawMask(int idx)
bool CameraConst::has_rawMask(int idx) const
{
if (idx < 0 || idx > 7) {
if (idx < 0 || idx > 1) {
return false;
}
return (raw_mask[idx][0] | raw_mask[idx][1] | raw_mask[idx][2] | raw_mask[idx][3]) != 0;
}
void
CameraConst::get_rawMask(int idx, int& top, int& left, int& bottom, int& right)
void CameraConst::get_rawMask(int idx, int& top, int& left, int& bottom, int& right) const
{
top = left = bottom = right = 0;
if (idx < 0 || idx > 7) {
if (idx < 0 || idx > 1) {
return;
}
@ -457,38 +435,30 @@ CameraConst::get_rawMask(int idx, int& top, int& left, int& bottom, int& right)
right = raw_mask[idx][3];
}
void
CameraConst::update_Levels(const CameraConst *other)
void CameraConst::update_Levels(const CameraConst *other)
{
if (!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;
}
if (other->white_max) {
white_max = other->white_max;
}
// for (std::map<int, struct camera_const_levels>::iterator i=other->mLevels[0].begin(); i!=other->mLevels[0].end(); i++) {
// }
}
void
CameraConst::update_Crop(CameraConst *other)
void CameraConst::update_Crop(CameraConst *other)
{
if (!other) {
return;
@ -499,18 +469,16 @@ CameraConst::update_Crop(CameraConst *other)
}
}
bool
CameraConst::get_Levels(struct camera_const_levels & lvl, int bw, int iso, float fnumber)
bool CameraConst::get_Levels(camera_const_levels & lvl, int bw, int iso, float fnumber) const
{
std::map<int, struct camera_const_levels>::iterator it;
it = mLevels[bw].find(iso);
std::map<int, camera_const_levels>::const_iterator it = mLevels[bw].find(iso);
if (it == mLevels[bw].end()) {
std::map<int, struct camera_const_levels>::iterator best_it = mLevels[bw].begin();
auto best_it = mLevels[bw].cbegin();
if (iso > 0) {
for (it = mLevels[bw].begin(); it != mLevels[bw].end(); ++it) {
if (abs(it->first - iso) <= abs(best_it->first - iso)) {
if (std::abs(it->first - iso) <= std::abs(best_it->first - iso)) {
best_it = it;
} else {
break;
@ -527,35 +495,34 @@ CameraConst::get_Levels(struct camera_const_levels & lvl, int bw, int iso, float
lvl = it->second;
if (bw == 1 && fnumber > 0 && mApertureScaling.size() > 0) {
std::map<float, float>::iterator it;
it = mApertureScaling.find(fnumber);
if (bw == 1 && fnumber > 0 && !mApertureScaling.empty()) {
std::map<float, float>::const_iterator scaleIt = mApertureScaling.find(fnumber);
if (it == mApertureScaling.end()) {
if (scaleIt == mApertureScaling.end()) {
// fnumber may be an exact aperture, eg 1.414, or a rounded eg 1.4. In our map we
// should have rounded numbers so we translate and retry the lookup
// table with traditional 1/3 stop f-number rounding used by most cameras, we only
// have in the range 0.7 - 10.0, but aperture scaling rarely happen past f/4.0
const float fn_tab[8][3] = {
{ 0.7, 0.8, 0.9 },
{ 1.0, 1.1, 1.2 },
{ 1.4, 1.6, 1.8 },
{ 2.0, 2.2, 2.5 },
{ 2.8, 3.2, 3.5 },
{ 4.0, 4.5, 5.0 },
{ 5.6, 6.3, 7.1 },
{ 8.0, 9.0, 10.0 }
constexpr float fn_tab[8][3] = {
{ 0.7f, 0.8f, 0.9f },
{ 1.f, 1.1f, 1.2f },
{ 1.4f, 1.6f, 1.8f },
{ 2.f, 2.2f, 2.5f },
{ 2.8f, 3.2f, 3.5f },
{ 4.f, 4.5f, 5.f },
{ 5.6f, 6.3f, 7.1f },
{ 8.f, 9.f, 10.f }
};
for (int avh = 0; avh < 8; avh++) {
for (int k = 0; k < 3; k++) {
float av = (avh - 1) + (float)k / 3;
float aperture = sqrtf(powf(2, av));
const float av = (avh - 1) + k / 3.f;
const float aperture = std::sqrt(std::pow(2.f, av));
if (fnumber > aperture * 0.97f && fnumber < aperture / 0.97f) {
fnumber = fn_tab[avh][k];
it = mApertureScaling.find(fnumber);
scaleIt = mApertureScaling.find(fnumber);
avh = 7;
break;
}
@ -563,20 +530,18 @@ CameraConst::get_Levels(struct camera_const_levels & lvl, int bw, int iso, float
}
}
float scaling = 1.0;
float scaling = 1.f;
if (it == mApertureScaling.end()) {
std::map<float, float>::reverse_iterator it;
for (it = mApertureScaling.rbegin(); it != mApertureScaling.rend(); ++it) {
if (it->first > fnumber) {
scaling = it->second;
if (scaleIt == mApertureScaling.end()) {
for (auto entry = mApertureScaling.crbegin(); entry != mApertureScaling.crend(); ++entry) {
if (entry->first > fnumber) {
scaling = entry->second;
} else {
break;
}
}
} else {
scaling = it->second;
scaling = scaleIt->second;
}
if (scaling > 1.f) {
@ -593,24 +558,22 @@ CameraConst::get_Levels(struct camera_const_levels & lvl, int bw, int iso, float
return true;
}
int
CameraConst::get_BlackLevel(const int idx, const int iso_speed)
int CameraConst::get_BlackLevel(const int idx, const int iso_speed) const
{
assert(idx >= 0 && idx <= 3);
struct camera_const_levels lvl;
camera_const_levels lvl;
if (!get_Levels(lvl, 0, iso_speed, 0.0)) {
if (!get_Levels(lvl, 0, iso_speed, 0.f)) {
return -1;
}
return lvl.levels[idx];
}
int
CameraConst::get_WhiteLevel(const int idx, const int iso_speed, const float fnumber)
int CameraConst::get_WhiteLevel(const int idx, const int iso_speed, const float fnumber) const
{
assert(idx >= 0 && idx <= 3);
struct camera_const_levels lvl;
camera_const_levels lvl;
if (!get_Levels(lvl, 1, iso_speed, fnumber)) {
return -1;
@ -619,48 +582,44 @@ CameraConst::get_WhiteLevel(const int idx, const int iso_speed, const float fnum
return lvl.levels[idx];
}
bool
CameraConst::has_globalGreenEquilibration()
bool CameraConst::has_globalGreenEquilibration() const
{
return globalGreenEquilibration >= 0;
}
bool
CameraConst::get_globalGreenEquilibration()
bool CameraConst::get_globalGreenEquilibration() const
{
return globalGreenEquilibration > 0;
}
void
CameraConst::update_globalGreenEquilibration(bool other)
void CameraConst::update_globalGreenEquilibration(bool other)
{
globalGreenEquilibration = (other ? 1 : 0);
}
bool
CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& filename_)
bool CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& filename_)
{
// read the file into a single long string
const char *filename = filename_.c_str();
FILE *stream = fopen(filename, "rt");
if (stream == nullptr) {
if (!stream) {
fprintf(stderr, "Could not open camera constants file \"%s\": %s\n", filename, strerror(errno));
return false;
}
size_t bufsize = 16384;
size_t increment = 2 * bufsize;
size_t bufsize = 262144;
size_t increment = bufsize;
size_t datasize = 0, ret;
char *buf = (char *)malloc(bufsize);
while ((ret = fread(&buf[datasize], 1, bufsize - datasize, stream)) != 0) {
while ((ret = fread(&buf[datasize], 1, bufsize - datasize - 1, stream)) != 0) {
datasize += ret;
if (datasize == bufsize) { // we need more memory
if (datasize == bufsize - 1) { // we need more memory
bufsize += increment;
void *temp = realloc(buf, bufsize); // try to realloc buffer with new size
if(!temp) { // realloc failed
if (!temp) { // realloc failed
temp = malloc(bufsize); // alloc now buffer
if (temp) { // alloc worked
memcpy(temp, buf, bufsize - increment); // copy old buffer content to new buffer
@ -683,17 +642,13 @@ CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& filename_
fclose(stream);
if(datasize == bufsize) {
buf = (char *)realloc(buf, datasize + 1);
}
buf[datasize] = '\0';
// remove comments
cJSON_Minify(buf);
// parse
cJSON *jsroot = cJSON_Parse(buf);
cJSON* const jsroot = cJSON_Parse(buf);
if (!jsroot) {
char str[128];
@ -711,20 +666,16 @@ CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& filename_
}
free(buf);
/*{
char *js_str = cJSON_Print(jsroot);
printf("%s\n", js_str);
free(js_str);
}*/
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");
goto parse_error;
}
for (js = js->child; js != nullptr; js = js->next) {
cJSON *ji = cJSON_GetObjectItem(js, "make_model");
for (js = js->child; js; js = js->next) {
const cJSON *ji = cJSON_GetObjectItem(js, "make_model");
if (!ji) {
fprintf(stderr, "missing \"make_model\" object item\n");
@ -738,30 +689,30 @@ CameraConstantsStore::parse_camera_constants_file(const Glib::ustring& filename_
is_array = true;
}
while (ji != nullptr) {
while (ji) {
if (ji->type != cJSON_String) {
fprintf(stderr, "\"make_model\" must be a string or an array of strings\n");
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;
}
Glib::ustring make_model(ji->valuestring);
make_model = make_model.uppercase();
std::string make_model(ji->valuestring);
std::transform(make_model.begin(), make_model.end(), make_model.begin(), ::toupper);
const auto ret = mCameraConstants.emplace(make_model, cc);
const auto entry = mCameraConstants.emplace(make_model, cc);
if(ret.second) { // entry inserted into map
if (entry.second) { // entry inserted into map
if (settings->verbose) {
printf("Add camera constants for \"%s\"\n", make_model.c_str());
}
} else {
// The CameraConst already exist for this camera make/model -> we merge the values
CameraConst *existingcc = ret.first->second;
CameraConst* const existingcc = entry.first->second;
// updating the dcraw matrix
existingcc->update_dcrawMatrix(cc->get_dcrawMatrix());
@ -813,29 +764,26 @@ 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);
}
}
CameraConstantsStore *
CameraConstantsStore::getInstance()
CameraConstantsStore* CameraConstantsStore::getInstance()
{
static CameraConstantsStore instance_;
return &instance_;
}
CameraConst *
CameraConstantsStore::get(const char make[], const char model[])
const CameraConst* CameraConstantsStore::get(const char make[], const char model[]) const
{
Glib::ustring key(make);
std::string key(make);
key += " ";
key += model;
key = key.uppercase();
std::map<std::string, CameraConst *>::iterator it;
it = mCameraConstants.find(key);
std::transform(key.begin(), key.end(), key.begin(), ::toupper);
const auto it = mCameraConstants.find(key);
if (it == mCameraConstants.end()) {
return nullptr;

View File

@ -27,35 +27,34 @@ private:
std::string make_model;
short dcraw_matrix[12];
int raw_crop[4];
int raw_mask[8][4];
int raw_mask[2][4];
int white_max;
std::map<int, struct camera_const_levels> mLevels[2];
std::map<int, camera_const_levels> mLevels[2];
std::map<float, float> mApertureScaling;
std::vector<int> pdafPattern;
int pdafOffset;
int globalGreenEquilibration;
CameraConst();
static bool parseLevels(CameraConst *cc, int bw, void *ji);
static bool parseApertureScaling(CameraConst *cc, void *ji);
bool get_Levels(struct camera_const_levels & lvl, int bw, int iso, float fnumber);
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);
bool has_dcrawMatrix(void);
bool has_pdafPattern(void);
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);
std::vector<int> get_pdafPattern();
int get_pdafOffset() {return pdafOffset;}
bool has_rawCrop(void);
void get_rawCrop(int& left_margin, int& top_margin, int& width, int& height);
bool has_rawMask(int idx);
void get_rawMask(int idx, int& top, int& left, int& bottom, int& right);
int get_BlackLevel(int idx, int iso_speed);
int get_WhiteLevel(int idx, int iso_speed, float fnumber);
bool has_globalGreenEquilibration();
bool get_globalGreenEquilibration();
const short *get_dcrawMatrix(void) const;
const std::vector<int>& get_pdafPattern() const;
int get_pdafOffset() const {return pdafOffset;};
bool has_rawCrop(void) const;
void get_rawCrop(int& left_margin, int& top_margin, int& width, int& height) const;
bool has_rawMask(int idx) const;
void get_rawMask(int idx, int& top, int& left, int& bottom, int& right) const;
int get_BlackLevel(int idx, int iso_speed) const;
int get_WhiteLevel(int idx, int iso_speed, float fnumber) const;
bool has_globalGreenEquilibration() const;
bool get_globalGreenEquilibration() const;
void update_Levels(const CameraConst *other);
void update_Crop(CameraConst *other);
void update_pdafPattern(const std::vector<int> &other);
@ -75,7 +74,7 @@ public:
~CameraConstantsStore();
void init(const Glib::ustring& baseDir, const Glib::ustring& userSettingsDir);
static CameraConstantsStore *getInstance(void);
CameraConst *get(const char make[], const char model[]);
const CameraConst *get(const char make[], const char model[]) const;
};
}

View File

@ -177,7 +177,7 @@ PDAFLinesFilter::PDAFLinesFilter(RawImage *ri):
gthresh_ = new PDAFGreenEqulibrateThreshold(W_, H_);
CameraConstantsStore* ccs = CameraConstantsStore::getInstance();
CameraConst *cc = ccs->get(ri_->get_maker().c_str(), ri_->get_model().c_str());
const CameraConst *cc = ccs->get(ri_->get_maker().c_str(), ri_->get_model().c_str());
if (cc) {
pattern_ = cc->get_pdafPattern();

View File

@ -524,7 +524,7 @@ int RawImage::loadRaw (bool loadData, unsigned int imageNum, bool closeFile, Pro
}
CameraConstantsStore* ccs = CameraConstantsStore::getInstance();
CameraConst *cc = ccs->get(make, model);
const CameraConst *cc = ccs->get(make, model);
if (raw_image) {
if (cc && cc->has_rawCrop()) {
@ -850,42 +850,18 @@ DCraw::dcraw_coeff_overrides(const char make[], const char model[], const int is
short trans[12]; // set first value to 0 for no change
} table[] = {
{
"Canon EOS 5D Mark III", -1, 0x3a98, /* RT */
{ 6722, -635, -963, -4287, 12460, 2028, -908, 2162, 5668 }
},
{
"Canon EOS 5D", -1, 0xe6c, /* RT */
{ 6319, -793, -614, -5809, 13342, 2738, -1132, 1559, 7971 }
},
{
"Canon EOS 6D", -1, 0x3c82,
{ 7034, -804, -1014, -4420, 12564, 2058, -851, 1994, 5758 }
},
{
"Canon EOS 7D", -1, 0x3510, /* RT - Colin Walker */
{ 5962, -171, -732, -4189, 12307, 2099, -911, 1981, 6304 }
},
{
"Canon EOS 20D", -1, 0xfff, /* RT */
{ 7590, -1646, -673, -4697, 12411, 2568, -627, 1118, 7295 }
},
{
"Canon EOS 40D", -1, 0x3f60, /* RT */
{ 6070, -531, -883, -5763, 13647, 2315, -1533, 2582, 6801 }
},
{
"Canon EOS 60D", -1, 0x2ff7, /* RT - Colin Walker */
{ 5678, -179, -718, -4389, 12381, 2243, -869, 1819, 6380 }
},
{
"Canon EOS 450D", -1, 0x390d, /* RT */
{ 6246, -1272, -523, -5075, 12357, 3075, -1035, 1825, 7333 }
},
{
"Canon EOS 550D", -1, 0x3dd7, /* RT - Lebedev*/
{ 6519, -772, -703, -4994, 12737, 2519, -1387, 2492, 6175 }
},
{
"Canon EOS-1D Mark III", 0, 0x3bb0, /* RT */
{ 7406, -1592, -646, -4856, 12457, 2698, -432, 726, 7921 }
@ -894,18 +870,10 @@ DCraw::dcraw_coeff_overrides(const char make[], const char model[], const int is
"Canon PowerShot G10", -1, -1, /* RT */
{ 12535, -5030, -796, -2711, 10134, 3006, -413, 1605, 5264 }
},
{
"Canon PowerShot G12", -1, -1, /* RT */
{ 12222, -4097, -1380, -2876, 11016, 2130, -888, 1630, 4434 }
},
{
"Fujifilm X100", -1, -1, /* RT - Colin Walker */
{ 10841, -3288, -807, -4652, 12552, 2344, -642, 1355, 7206 }
},
{
"Nikon D200", -1, 0xfbc, /* RT */
{ 8498, -2633, -295, -5423, 12869, 2860, -777, 1077, 8124 }
@ -918,32 +886,14 @@ DCraw::dcraw_coeff_overrides(const char make[], const char model[], const int is
"Nikon D3100", -1, -1, /* RT */
{ 7729, -2212, -481, -5709, 13148, 2858, -1295, 1908, 8936 }
},
{
"Nikon D3S", -1, -1, /* RT */
{ 8792, -2663, -344, -5221, 12764, 2752, -1491, 2165, 8121 }
},
{
"Nikon D5200", -1, -1, // color matrix copied from D5200 DNG D65 matrix
{ 8322, -3112, -1047, -6367, 14342, 2179, -988, 1638, 6394 }
},
{
"Nikon D7000", -1, -1, /* RT - Tanveer(tsk1979) */
{ 7530, -1942, -255, -4318, 11390, 3362, -926, 1694, 7649 }
},
{
"Nikon D7100", -1, -1, // color matrix and WP copied from D7100 DNG D65 matrix
{ 8322, -3112, -1047, -6367, 14342, 2179, -988, 1638, 6394 }
},
{
"Nikon D700", -1, -1, /* RT */
{ 8364, -2503, -352, -6307, 14026, 2492, -1134, 1512, 8156 }
},
{
"Nikon COOLPIX A", -1, 0x3e00, // color matrix and WP copied from "COOLPIX A" DNG D65 matrix
{ 8198, -2239, -724, -4871, 12389, 2798, -1043, 205, 7181 }
},
{
"Olympus E-30", -1, 0xfbc, /* RT - Colin Walker */
{ 8510, -2355, -693, -4819, 12520, 2578, -1029, 2067, 7752 }
@ -984,69 +934,14 @@ DCraw::dcraw_coeff_overrides(const char make[], const char model[], const int is
"Olympus XZ-1", -1, -1, /* RT - Colin Walker */
{ 8665, -2247, -762, -2424, 10372, 2382, -1011, 2286, 5189 }
},
/* since Dcraw_v9.21 Panasonic BlackLevel is read from exif (tags 0x001c BlackLevelRed, 0x001d BlackLevelGreen, 0x001e BlackLevelBlue
and we define here the needed offset of around 15. The total BL is BL + BLoffset (cblack + black) */
{
"Panasonic DMC-FZ150", 15, 0xfd2, /* RT */
{ 10435, -3208, -72, -2293, 10506, 2067, -486, 1725, 4682 }
},
{
"Panasonic DMC-G10", 15, 0xf50, /* RT - Colin Walker - variable WL 3920 - 4080 */
{ 8310, -1811, -960, -4941, 12990, 2151, -1378, 2468, 6860 }
},
{
"Panasonic DMC-G1", 15, 0xf50, /* RT - Colin Walker - variable WL 3920 - 4080 */
{ 7477, -1615, -651, -5016, 12769, 2506, -1380, 2475, 7240 }
},
{
"Panasonic DMC-G2", 15, 0xf50, /* RT - Colin Walker - variable WL 3920 - 4080 */
{ 8310, -1811, -960, -4941, 12990, 2151, -1378, 2468, 6860 }
},
{
"Panasonic DMC-G3", 15, 0xfdc, /* RT - Colin Walker - WL 4060 */
{ 6051, -1406, -671, -4015, 11505, 2868, -1654, 2667, 6219 }
},
{
"Panasonic DMC-G5", 15, 0xfdc, /* RT - WL 4060 */
{ 7122, -2092, -419, -4643, 11769, 3283, -1363, 2413, 5944 }
},
{
"Panasonic DMC-GF1", 15, 0xf50, /* RT - Colin Walker - Variable WL 3920 - 4080 */
{ 7863, -2080, -668, -4623, 12331, 2578, -1020, 2066, 7266 }
},
{
"Panasonic DMC-GF2", 15, 0xfd2, /* RT - Colin Walker - WL 4050 */
{ 7694, -1791, -745, -4917, 12818, 2332, -1221, 2322, 7197 }
},
{
"Panasonic DMC-GF3", 15, 0xfd2, /* RT - Colin Walker - WL 4050 */
{ 8074, -1846, -861, -5026, 12999, 2239, -1320, 2375, 7422 }
},
{
"Panasonic DMC-GH1", 15, 0xf5a, /* RT - Colin Walker - variable WL 3930 - 4080 */
{ 6360, -1557, -375, -4201, 11504, 3086, -1378, 2518, 5843 }
},
{
"Panasonic DMC-GH2", 15, 0xf5a, /* RT - Colin Walker - variable WL 3930 - 4080 */
// { 6855,-1765,-456,-4223,11600,2996,-1450,2602,5761 } }, disabled due to problems with underwater WB
{ 7780, -2410, -806, -3913, 11724, 2484, -1018, 2390, 5298 }
}, // dcraw original
{
"Pentax K200D", -1, -1, /* RT */
{ 10962, -4428, -542, -5486, 13023, 2748, -569, 842, 8390 }
},
{
"Leica Camera AG M9 Digital Camera", -1, -1, /* RT */
{ 7181, -1706, -55, -3557, 11409, 2450, -621, 2072, 7533 }
},
{
"SONY NEX-3", 128 << dcraw_arw2_scaling_bugfix_shift, -1, /* RT - Colin Walker */
{ 5145, -741, -123, -4915, 12310, 2945, -794, 1489, 6906 }
@ -1104,7 +999,7 @@ DCraw::dcraw_coeff_overrides(const char make[], const char model[], const int is
{
// test if we have any information in the camera constants store, if so we take that.
rtengine::CameraConstantsStore* ccs = rtengine::CameraConstantsStore::getInstance();
rtengine::CameraConst *cc = ccs->get(make, model);
const rtengine::CameraConst *cc = ccs->get(make, model);
if (cc) {
if (RT_blacklevel_from_constant == ThreeValBool::T) {

View File

@ -1453,7 +1453,7 @@ void RawImageSource::preprocess (const RAWParams &raw, const LensProfParams &le
[&]() -> bool
{
CameraConstantsStore *ccs = CameraConstantsStore::getInstance();
CameraConst *cc = ccs->get(ri->get_maker().c_str(), ri->get_model().c_str());
const CameraConst *cc = ccs->get(ri->get_maker().c_str(), ri->get_model().c_str());
return cc && cc->get_globalGreenEquilibration();
};