Files
Megapixels/src/dcp.c
Egor Shestakov e02b54ec57 add buffer size check in find_calibration
Drop find_calibration_by_model function that was occassionally,
as I suppose, marked not static.
2025-03-12 12:16:04 +00:00

57 lines
1.8 KiB
C

#include "dcp.h"
#include <libdng.h>
#include <libmegapixels.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifndef SYSCONFDIR
#include "config.h"
#endif
bool
find_calibration(size_t size, char *conffile, const char *sensor)
{
char model[512];
int model_found = libmegapixels_get_model(sizeof(model), model);
if(!model_found)
return false;
static const char *paths[] = {
"config/%s,%s.dcp",
SYSCONFDIR "/megapixels/config/%s,%s.dcp",
DATADIR "/megapixels/config/%s,%s.dcp",
NULL
};
for (const char *fmt = paths[0]; fmt; fmt++) {
snprintf(conffile, size, fmt, model, sensor);
if (access(conffile, F_OK) != -1) {
printf("Found calibration file at %s\n", conffile);
return true;
}
}
printf("No calibration found for %s,%s\n", model, sensor);
return false;
}
struct MPCameraCalibration
parse_calibration_file(const char *path)
{
struct MPCameraCalibration result;
libdng_info temp = { 0 };
libdng_new(&temp);
libdng_load_calibration_file(&temp, path);
memcpy(result.color_matrix_1, temp.color_matrix_1, 9 * sizeof(float));
memcpy(result.color_matrix_2, temp.color_matrix_2, 9 * sizeof(float));
memcpy(result.forward_matrix_1, temp.forward_matrix_1, 9 * sizeof(float));
memcpy(result.forward_matrix_2, temp.forward_matrix_2, 9 * sizeof(float));
result.tone_curve_length = temp.tone_curve_length;
result.tone_curve = malloc(temp.tone_curve_length * sizeof(float));
memcpy(result.tone_curve,
temp.tone_curve,
temp.tone_curve_length * sizeof(float));
return result;
}