40 #define LANCZOS_RESOLUTION 256
93 #define OFFSET(x) offsetof(LensfunContext, x)
94 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
108 {
"focal_length",
"focal length of video (zoom; constant for the duration of the use of this filter)",
OFFSET(focal_length),
AV_OPT_TYPE_FLOAT, {.dbl=18}, 0.0, DBL_MAX,
FLAGS },
109 {
"aperture",
"aperture (constant for the duration of the use of this filter)",
OFFSET(aperture),
AV_OPT_TYPE_FLOAT, {.dbl=3.5}, 0.0, DBL_MAX,
FLAGS },
110 {
"focus_distance",
"focus distance (constant for the duration of the use of this filter)",
OFFSET(focus_distance),
AV_OPT_TYPE_FLOAT, {.dbl=1000.0f}, 0.0, DBL_MAX,
FLAGS },
112 {
"target_geometry",
"target geometry of the lens correction (only when geometry correction is enabled)",
OFFSET(target_geometry),
AV_OPT_TYPE_INT, {.i64=LF_RECTILINEAR}, 0, INT_MAX,
FLAGS, .unit =
"lens_geometry" },
113 {
"rectilinear",
"rectilinear lens (default)", 0,
AV_OPT_TYPE_CONST, {.i64=LF_RECTILINEAR}, 0, 0,
FLAGS, .unit =
"lens_geometry" },
114 {
"fisheye",
"fisheye lens", 0,
AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE}, 0, 0,
FLAGS, .unit =
"lens_geometry" },
115 {
"panoramic",
"panoramic (cylindrical)", 0,
AV_OPT_TYPE_CONST, {.i64=LF_PANORAMIC}, 0, 0,
FLAGS, .unit =
"lens_geometry" },
116 {
"equirectangular",
"equirectangular", 0,
AV_OPT_TYPE_CONST, {.i64=LF_EQUIRECTANGULAR}, 0, 0,
FLAGS, .unit =
"lens_geometry" },
117 {
"fisheye_orthographic",
"orthographic fisheye", 0,
AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_ORTHOGRAPHIC}, 0, 0,
FLAGS, .unit =
"lens_geometry" },
118 {
"fisheye_stereographic",
"stereographic fisheye", 0,
AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_STEREOGRAPHIC}, 0, 0,
FLAGS, .unit =
"lens_geometry" },
119 {
"fisheye_equisolid",
"equisolid fisheye", 0,
AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_EQUISOLID}, 0, 0,
FLAGS, .unit =
"lens_geometry" },
120 {
"fisheye_thoby",
"fisheye as measured by thoby", 0,
AV_OPT_TYPE_CONST, {.i64=LF_FISHEYE_THOBY}, 0, 0,
FLAGS, .unit =
"lens_geometry" },
121 {
"reverse",
"Does reverse correction (regular image to lens distorted)",
OFFSET(reverse),
AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1,
FLAGS },
135 const lfCamera **cameras;
136 const lfLens **lenses;
139 if ((lensfun->
db_path ? lf_db_load_path(db, lensfun->
db_path) : lf_db_load(db)) != LF_NO_ERROR) {
147 const lfCamera *
const *cameras = lf_db_get_cameras(db);
151 for (
int i = 0; cameras && cameras[
i];
i++)
156 const lfLens *
const *lenses = lf_db_get_lenses(db);
160 for (
int i = 0; lenses && lenses[
i];
i++)
166 lensfun->
lens = lf_lens_create();
167 lensfun->
camera = lf_camera_create();
169 cameras = lf_db_find_cameras(db, lensfun->
make, lensfun->
model);
170 if (cameras && *cameras) {
171 lf_camera_copy(lensfun->
camera, *cameras);
182 if (lenses && *lenses) {
183 lf_lens_copy(lensfun->
lens, *lenses);
201 }
else if (x > -2.0
f && x < 2.0
f) {
219 lensfun->
camera->CropFactor,
225 lf_modifier_enable_distortion_correction(lensfun->
modifier);
227 lf_modifier_enable_scaling(lensfun->
modifier, lensfun->
scale);
230 lf_modifier_enable_tca_correction(lensfun->
modifier);
244 lf_modifier_apply_subpixel_geometry_distortion(lensfun->
modifier,
250 lf_modifier_apply_subpixel_distortion(lensfun->
modifier,
260 lf_modifier_apply_geometry_distortion(lensfun->
modifier,
291 lf_modifier_apply_color_modification(
thread_data->modifier,
314 int x, y,
i, j, rgb_index;
315 float interpolated, new_x, new_y,
d, norm;
316 int new_x_int, new_y_int;
319 for (rgb_index = 0; rgb_index < 3; ++rgb_index) {
324 new_x_int =
thread_data->distortion_coords[x * 2 * 3 + y *
thread_data->width * 2 * 3 + rgb_index * 2] + 0.5f;
325 new_y_int =
thread_data->distortion_coords[x * 2 * 3 + y *
thread_data->width * 2 * 3 + rgb_index * 2 + 1] + 0.5f;
326 if (new_x_int < 0 || new_x_int >=
thread_data->width || new_y_int < 0 || new_y_int >=
thread_data->height) {
338 if (new_x_int < 0 || new_x_int + 1 >=
thread_data->width || new_y_int < 0 || new_y_int + 1 >=
thread_data->height) {
342 thread_data->data_in[ new_x_int * 3 + rgb_index + new_y_int *
thread_data->linesize_in] * (new_x_int + 1 - new_x) * (new_y_int + 1 - new_y)
343 +
thread_data->data_in[(new_x_int + 1) * 3 + rgb_index + new_y_int *
thread_data->linesize_in] * (new_x - new_x_int) * (new_y_int + 1 - new_y)
344 +
thread_data->data_in[ new_x_int * 3 + rgb_index + (new_y_int + 1) *
thread_data->linesize_in] * (new_x_int + 1 - new_x) * (new_y - new_y_int)
345 +
thread_data->data_in[(new_x_int + 1) * 3 + rgb_index + (new_y_int + 1) *
thread_data->linesize_in] * (new_x - new_x_int) * (new_y - new_y_int);
355 for (j = 0; j < 4; ++j)
356 for (
i = 0;
i < 4; ++
i) {
357 if (new_x_int +
i - 2 < 0 || new_x_int +
i - 2 >=
thread_data->width || new_y_int + j - 2 < 0 || new_y_int + j - 2 >=
thread_data->height)
359 d =
square(new_x - (new_x_int +
i - 2)) *
square(new_y - (new_y_int + j - 2));
364 interpolated +=
thread_data->data_in[(new_x_int +
i - 2) * 3 + rgb_index + (new_y_int + j - 2) *
thread_data->linesize_in] *
d;
369 interpolated /= norm;
370 thread_data->data_out[x * 3 + rgb_index + y *
thread_data->linesize_out] = interpolated < 0.0f ? 0.0f : interpolated > 255.0f ? 255.0f : interpolated;
380 if (new_x_int < 0 || new_x_int >=
thread_data->width || new_y_int < 0 || new_y_int >=
thread_data->height) {
392 if (new_x_int < 0 || new_x_int + 1 >=
thread_data->width || new_y_int < 0 || new_y_int + 1 >=
thread_data->height) {
396 thread_data->data_in[ new_x_int * 3 + rgb_index + new_y_int *
thread_data->linesize_in] * (new_x_int + 1 - new_x) * (new_y_int + 1 - new_y)
397 +
thread_data->data_in[(new_x_int + 1) * 3 + rgb_index + new_y_int *
thread_data->linesize_in] * (new_x - new_x_int) * (new_y_int + 1 - new_y)
398 +
thread_data->data_in[ new_x_int * 3 + rgb_index + (new_y_int + 1) *
thread_data->linesize_in] * (new_x_int + 1 - new_x) * (new_y - new_y_int)
399 +
thread_data->data_in[(new_x_int + 1) * 3 + rgb_index + (new_y_int + 1) *
thread_data->linesize_in] * (new_x - new_x_int) * (new_y - new_y_int);
409 for (j = 0; j < 4; ++j)
410 for (
i = 0;
i < 4; ++
i) {
411 if (new_x_int +
i - 2 < 0 || new_x_int +
i - 2 >=
thread_data->width || new_y_int + j - 2 < 0 || new_y_int + j - 2 >=
thread_data->height)
413 d =
square(new_x - (new_x_int +
i - 2)) *
square(new_y - (new_y_int + j - 2));
418 interpolated +=
thread_data->data_in[(new_x_int +
i - 2) * 3 + rgb_index + (new_y_int + j - 2) *
thread_data->linesize_in] *
d;
423 interpolated /= norm;
424 thread_data->data_out[x * 3 + rgb_index + y *
thread_data->linesize_out] = interpolated < 0.0f ? 0.0f : interpolated > 255.0f ? 255.0f : interpolated;
457 .data_in = in->
data[0],
464 &vignetting_thread_data,
NULL,
480 .data_in = in->
data[0],
481 .data_out =
out->data[0],
483 .linesize_out =
out->linesize[0],
485 .mode = lensfun->
mode,
490 &distortion_correction_thread_data,
NULL,
505 lf_camera_destroy(lensfun->
camera);
507 lf_lens_destroy(lensfun->
lens);
509 lf_modifier_destroy(lensfun->
modifier);
525 .description =
NULL_IF_CONFIG_SMALL(
"Apply correction to an image based on info derived from the lensfun database."),
532 .priv_class = &lensfun_class,