Commit a1664805 authored by Cyril Richard's avatar Cyril Richard
Browse files

Running background extraction in another thread

(cherry picked from commit 880554ae)
parent ef955f5f
......@@ -809,16 +809,33 @@ void generate_background_samples(int nb_of_samples, double tolerance) {
redraw(REDRAW_OVERLAY);
}
static gboolean end_background(gpointer p) {
stop_processing_thread();
struct background_data *args = (struct background_data *)p;
invalidate_stats_from_fit(args->fit);
background_computed = TRUE;
if (!args->from_ui) {
free_background_sample_list(com.grad_samples);
com.grad_samples = NULL;
}
adjust_cutoff_from_updated_gfit();
redraw(REMAP_ALL);
set_cursor_waiting(FALSE);
free(args);
return FALSE;
}
/* uses samples from com.grad_samples */
gboolean remove_gradient_from_image(int correction, poly_order degree, double smoothing, gboolean use_dither, int interpolation_method) {
gpointer remove_gradient_from_image(gpointer p) {
struct background_data *args = (struct background_data *)p;
gchar *error;
double *background = (double*)malloc(gfit.ry * gfit.rx * sizeof(double));
if (!background && !com.script) {
PRINT_ALLOC_ERR;
set_cursor_waiting(FALSE);
return FALSE;
return NULL;
}
const size_t n = gfit.naxes[0] * gfit.naxes[1];
......@@ -826,7 +843,7 @@ gboolean remove_gradient_from_image(int correction, poly_order degree, double sm
if (!image) {
free(background);
PRINT_ALLOC_ERR;
return FALSE;
return NULL;
}
/* Make sure to update local median. Useful if undo is pressed */
......@@ -838,12 +855,10 @@ gboolean remove_gradient_from_image(int correction, poly_order degree, double sm
for (int channel = 0; channel < gfit.naxes[2]; channel++) {
/* compute background */
gboolean interpolation_worked = TRUE;
if (interpolation_method == INTER_POLY){
interpolation_worked = computeBackground_Polynom(com.grad_samples, background, channel,
gfit.rx, gfit.ry, degree, &error);
if (args->interpolation_method == INTER_POLY) {
interpolation_worked = computeBackground_Polynom(com.grad_samples, background, channel, gfit.rx, gfit.ry, args->degree, &error);
} else {
interpolation_worked = computeBackground_RBF(com.grad_samples, background, channel,
gfit.rx, gfit.ry, smoothing, &error);
interpolation_worked = computeBackground_RBF(com.grad_samples, background, channel, gfit.rx, gfit.ry, args->smoothing, &error);
}
if (!interpolation_worked) {
......@@ -852,22 +867,23 @@ gboolean remove_gradient_from_image(int correction, poly_order degree, double sm
siril_message_dialog(GTK_MESSAGE_ERROR, _("Not enough samples."),
error);
set_cursor_waiting(FALSE);
return FALSE;
return NULL;
}
/* remove background */
const char *c_name = vport_number_to_name(channel);
siril_log_message(_("Background extraction from channel %s.\n"), c_name);
convert_fits_to_img(&gfit, image, channel, use_dither);
remove_gradient(image, background, background_mean, gfit.naxes[0] * gfit.naxes[1], correction);
convert_fits_to_img(&gfit, image, channel, args->dither);
remove_gradient(image, background, background_mean, gfit.naxes[0] * gfit.naxes[1], args->correction);
convert_img_to_fits(image, &gfit, channel);
}
siril_log_message(_("Background with %s interpolation computed.\n"), (interpolation_method == INTER_POLY) ? "polynomial" : "RBF");
siril_log_message(_("Background with %s interpolation computed.\n"), (args->interpolation_method == INTER_POLY) ? "polynomial" : "RBF");
gettimeofday(&t_end, NULL);
show_time(t_start, t_end);
/* free memory */
free(image);
free(background);
return TRUE;
siril_add_idle(end_background, args);
return args;
}
/** Apply for sequence **/
......@@ -1070,13 +1086,18 @@ void on_bkg_compute_bkg_clicked(GtkButton *button, gpointer user_data) {
gboolean use_dither = is_dither_checked();
int interpolation_method = get_interpolation_method();
remove_gradient_from_image(correction, degree, smoothing, use_dither, interpolation_method);
background_computed = TRUE;
invalidate_stats_from_fit(&gfit);
adjust_cutoff_from_updated_gfit();
redraw(REMAP_ALL);
set_cursor_waiting(FALSE);
struct background_data *args = malloc(sizeof(struct background_data));
args->threads = com.max_thread;
args->from_ui = TRUE;
args->correction = correction;
args->interpolation_method = interpolation_method;
args->degree = (poly_order) (degree - 1);
args->smoothing = smoothing;
args->dither = use_dither;
args->threads = com.max_thread;
args->fit = &gfit;
start_in_new_thread(remove_gradient_from_image, args);
}
void on_background_ok_button_clicked(GtkButton *button, gpointer user_data) {
......
......@@ -23,8 +23,10 @@ struct background_data {
int interpolation_method;
poly_order degree;
double smoothing;
int threads;
gboolean dither;
fits *fit;
gboolean from_ui;
sequence *seq;
const gchar *seqEntry;
};
......@@ -36,7 +38,7 @@ void free_background_sample_list(GSList *list);
GSList* add_background_sample(GSList *list, fits *fit, point pt);
GSList* remove_background_sample(GSList *orig, fits *fit, point pt);
void generate_background_samples(int nb_of_samples, double tolerance);
gboolean remove_gradient_from_image(int correction, poly_order degree, double smoothing, gboolean use_dither, int interpolation_method);
gpointer remove_gradient_from_image(gpointer p);
void apply_background_extraction_to_sequence(struct background_data *background_args);
gboolean background_sample_is_valid(background_sample *sample);
......
......@@ -2174,7 +2174,6 @@ int process_fixbanding(int nb) {
return 0;
}
int process_subsky(int nb) {
if (get_thread_run()) {
PRINT_ANOTHER_THREAD_RUNNING;
......@@ -2200,18 +2199,19 @@ int process_subsky(int nb) {
siril_log_message(_("Polynomial degree order must be within the [1, 4] range.\n"));
return 1;
}
struct background_data *args = malloc(sizeof(struct background_data));
args->nb_of_samples = 20;
args->tolerance = 2;
args->correction = 0;
args->interpolation_method = INTER_POLY;
args->degree = (poly_order) (degree - 1);
args->smoothing = FALSE;
args->threads = com.max_thread;
args->from_ui = FALSE;
if (is_sequence) {
struct background_data *args = malloc(sizeof(struct background_data));
args->seq = seq;
args->nb_of_samples = 20;
args->tolerance = 1.0;
args->correction = 0; //subtraction
args->seqEntry = "bkg_";
args->degree = (poly_order) (degree - 1);
args->dither = TRUE;
char *prefix = NULL;
int startoptargs = 3;
if (nb > startoptargs) {
for (int i = startoptargs; i < nb; i++) {
......@@ -2223,24 +2223,26 @@ int process_subsky(int nb) {
siril_log_message(_("Missing argument to %s, aborting.\n"), current);
return 1;
}
args->seqEntry = strdup(value);
prefix = strdup(value);
}
}
}
}
args->seq = seq;
args->dither = TRUE;
args->seqEntry = prefix ? prefix : "bkg_";
apply_background_extraction_to_sequence(args);
} else {
args->seq = NULL;
args->dither = FALSE;
args->seqEntry = NULL;
args->fit = &gfit;
set_cursor_waiting(TRUE);
generate_background_samples(20, 1.0);
/* TODO: add new interpolation algorithm */
remove_gradient_from_image(0, (poly_order) (degree - 1), 0.0, TRUE, INTER_POLY);
free_background_sample_list(com.grad_samples);
com.grad_samples = NULL;
generate_background_samples(20, 2);
adjust_cutoff_from_updated_gfit();
redraw(REMAP_ALL);
set_cursor_waiting(FALSE);
start_in_new_thread(remove_gradient_from_image, args);
}
return 0;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment