1 #ifndef STACKING_H_
2 #define STACKING_H_
3 
4 #include "core/processing.h"
5 
6 //#define STACK_DEBUG
7 
8 /* the stacking method */
9 struct stacking_args;
10 typedef int (*stack_method)(struct stacking_args *args);
11 
12 typedef struct normalization_coeff norm_coeff;
13 
14 enum {
15 	ST_ALLOC_ERROR = -10,
16 	ST_SEQUENCE_ERROR = -2,
17 	ST_GENERIC_ERROR = -1,
18 	ST_OK = 0
19 };
20 
21 typedef enum {
22 	STACK_SUM,
23 	STACK_MEAN,
24 	STACK_MEDIAN,
25 	STACK_MAX,
26 	STACK_MIN,
27 } stackMethod;
28 
29 /* TYPE OF SIGMA CLIPPING */
30 typedef enum {
31 	NO_REJEC,
32 	PERCENTILE,
33 	SIGMA,
34 	MAD,
35 	SIGMEDIAN,
36 	WINSORIZED,
37 	LINEARFIT,
38 	GESDT
39 } rejection;
40 
41 /* TYPE OF NORMALIZATION */
42 typedef enum {
43 	NO_NORM,
44 	ADDITIVE,
45 	MULTIPLICATIVE,
46 	ADDITIVE_SCALING,
47 	MULTIPLICATIVE_SCALING,
48 } normalization;
49 
50 /* identical to the combo box items */
51 typedef enum {
52 	ALL_IMAGES,
53 	SELECTED_IMAGES,
54 	BEST_PSF_IMAGES,
55 	BEST_WPSF_IMAGES,
56 	BEST_ROUND_IMAGES,
57 	BEST_QUALITY_IMAGES
58 } stackType;
59 
60 struct normalization_coeff {
61 	double *offset;
62 	double *mul;
63 	double *scale;
64 	double *poffset[3];
65 	double *pmul[3];
66 	double *pscale[3];
67 };
68 
69 
70 struct stacking_args {
71 	stack_method method;
72 	sequence *seq;
73 	int ref_image;	// takes precedences over seq->reference_image which may not be applicable
74 	seq_image_filter filtering_criterion;
75 	double filtering_parameter;
76 	int nb_images_to_stack; // calculated from the above, for display purposes
77 	int *image_indices;	// conversion between selected image indices and sequence image indices
78 	char *description;	// description of the filtering
79 	const char *output_filename;	// used in the idle function only
80 	gboolean output_overwrite;	// used in the idle function only
81 	struct timeval t_start;
82 	int retval;
83 	float sig[2];		/* low and high sigma rejection or GESTD parameters */
84 	float *critical_value; /* index of critical_values for GESTD */
85 	rejection type_of_rejection;	/* type of rejection */
86 	normalization normalize;	/* type of normalization */
87 	norm_coeff coeff;		/* normalization data */
88 	gboolean force_norm;		/* TRUE = force normalization */
89 	gboolean output_norm;		/* normalize final image to the [0, 1] range */
90 	gboolean use_32bit_output;	/* output to 32 bit float */
91 	int reglayer;		/* layer used for registration data */
92 
93 	gboolean apply_weight;			/* enable weights */
94 	double *weights; 				/* computed weights for each (layer,image)*/
95 
96 	float (*sd_calculator)(const WORD *, const int); // internal, for ushort
97 	float (*mad_calculator)(const WORD *, const size_t, const double, gboolean) ; // internal, for ushort
98 };
99 
100 /* configuration from the command line */
101 struct stacking_configuration {
102 	struct timeval t_start;
103 	gchar *seqfile;
104 	gchar *result_file;
105 	stack_method method;
106 	rejection type_of_rejection;	/* type of rejection */
107 	double sig[2];
108 	gboolean force_no_norm;
109 	gboolean output_norm;
110 	normalization norm;
111 	int number_of_loaded_sequences;
112 	float f_fwhm, f_fwhm_p, f_wfwhm, f_wfwhm_p, f_round, f_round_p, f_quality, f_quality_p; // on if >0
113 	gboolean filter_included;
114 	gboolean apply_weight;
115 };
116 
117 
118 /*
119  * ESD test statistic data.
120  */
121 struct outliers {
122 	float x;
123 	int i;
124 	int out;
125 };
126 
127 
128 void initialize_stacking_default();
129 void initialize_stacking_methods();
130 gboolean evaluate_stacking_should_output_32bits(stack_method method,
131 		sequence *seq, int nb_img_to_stack, gchar **err);
132 
133 int stack_median(struct stacking_args *args);
134 int stack_mean_with_rejection(struct stacking_args *args);
135 int stack_addmax(struct stacking_args *args);
136 int stack_addmin(struct stacking_args *args);
137 
138 void main_stack(struct stacking_args *args);
139 void clean_end_stacking(struct stacking_args *args);
140 
141 void get_sequence_filtering_from_gui(seq_image_filter *filtering_criterion,
142 		double *filtering_parameter);
143 void update_stack_interface(gboolean dont_change_stack_type);
144 
145 
146 	/* normalization functions, normalize.c */
147 
148 int do_normalization(struct stacking_args *args);
149 
150 
151 	/* median and mean functions */
152 
153 int check_G_values(float Gs, float Gc);
154 void confirm_outliers(struct outliers *out, int N, double median, int *rejected, guint64 rej[2]);
155 
156 struct _image_block {
157 	long channel, start_row, end_row, height; // long matches naxes type
158 };
159 
160 int stack_compute_parallel_blocks(struct _image_block **blocksptr, long max_number_of_rows,
161 		long naxes[3], int nb_threads, long *largest_block_height, int *nb_blocks);
162 
163 /* pool of memory blocks for parallel processing */
164 struct _data_block {
165 	void **pix;	// buffer for a block on all images
166 	void *tmp;	// the actual single buffer for pix
167 	void *stack;	// the reordered stack for one pixel in all images
168 	int *rejected;  // 0 if pixel ok, 1 or -1 if rejected
169 	void *w_stack;	// stack for the winsorized rejection
170 	void *o_stack;  // original unordered stack
171 	float *xf, *yf, m_x, m_dx2;// data for the linear fit rejection
172 	int layer;	// to identify layer for normalization
173 };
174 
175 int stack_open_all_files(struct stacking_args *args, int *bitpix, int *naxis, long *naxes, GList **date_time, fits *fit);
176 int find_refimage_in_indices(int *indices, int nb, int ref);
177 
178 	/* up-scaling functions */
179 
180 int upscale_sequence(struct stacking_args *args);
181 void remove_tmp_drizzle_files(struct stacking_args *args);
182 
183 
184 	/* rejection_float.c */
185 
186 int apply_rejection_float(struct _data_block *data, int nb_frames, struct stacking_args *args, guint64 crej[2]);
187 
188 #endif
189