67 #include "muclipm_make_image.h"
68 #include "muclipm_priv_error.h"
104 long raw_image_nx = cpl_image_get_size_x(raw_img);
105 long raw_image_ny = cpl_image_get_size_y(raw_img);
108 int err=0, shiftx, shifty;
114 if (raw_img == NULL) {
115 return(CPL_ERROR_INCOMPATIBLE_INPUT);
118 if (raw_image_nx < 64 || raw_image_ny < 64) {
119 return(CPL_ERROR_INCOMPATIBLE_INPUT);
135 Qd[q][0] = 0.5*shiftx*raw_image_nx + 1;
136 Qd[q][1] = Qd[q][0] -1 +0.5*raw_image_nx;
137 Qd[q][2] = 0.5*shifty*raw_image_ny + 1;
138 Qd[q][3] = Qd[q][2] -1 +0.5*raw_image_ny;
145 Ov[q][0] = Qd[q][1] -32 +4;
147 Ov[q][0] = Qd[q][0] +4;
149 Ov[q][1] = Ov[q][0] +32 - 2*4;
151 Ov[q][2] = Qd[q][2]+4;
152 Ov[q][3] = Qd[q][3]-4;
159 median = cpl_image_get_median_window(raw_img,Ov[q][0],Ov[q][2],Ov[q][1],Ov[q][3]);
161 for (j=Qd[q][2];j<=Qd[q][3];j++)
162 for (i=Qd[q][0];i<=Qd[q][1];i++)
163 cpl_image_set(raw_img,i,j,cpl_image_get(raw_img,i,j,&err) -median);
166 return(CPL_ERROR_NONE);
208 const cpl_image *offset,
209 const cpl_image *flat,
210 const cpl_image *mask,
211 const long *slice_xstart,
212 const long *slice_xend,
216 long i, j, k, l, m, n, min, imin, imax, outvec_size;
217 long raw_image_nx = cpl_image_get_size_x(raw_img);
218 long raw_image_ny = cpl_image_get_size_y(raw_img);
219 long wr_col, wr_row, noslice;
220 int err=0, xoverscan=0;
223 double *invec_x_data, *outvec_x_data;
224 double *invec_y_data, *outvec_y_data;
226 cpl_bivector *invec, *outvec;
227 cpl_vector *invec_x, *invec_y;
228 cpl_image *tmp_img, *collapsed_img;
231 int slit_position[MUCLIPM_NB_SLICES] =
232 {9, 8, 1, 10, 7, 2, 11, 6, 3, 12, 5, 4};
236 MUCLIPM_TRY_CHECK(raw_img != NULL, CPL_ERROR_NULL_INPUT,
237 "raw image",
"is mandatory");
240 if (raw_image_ny <= 0)
242 MUCLIPM_TRY_EXIT_WITH_ERROR_MSG
243 (CPL_ERROR_INCOMPATIBLE_INPUT,
"raw image",
244 "should not be empty");
247 err = cpl_image_multiply_scalar(*out_img,(
double)0.0);
250 MUCLIPM_TRY_EXIT_WITH_ERROR_MSG
251 (cpl_error_get_code(),
"output image",
"could not set values");
254 tmp_img = cpl_image_duplicate(raw_img);
257 MUCLIPM_TRY_EXIT_WITH_ERROR_MSG
258 (cpl_error_get_code(),
"temporary image",
"could not create");
264 err = cpl_image_subtract(tmp_img, offset);
265 if (err != CPL_ERROR_NONE)
267 cpl_image_delete(tmp_img);
279 err = cpl_image_divide(tmp_img, flat);
280 if (err != CPL_ERROR_NONE)
282 cpl_image_delete(tmp_img);
288 MUCLIPM_TRY_CHECK(mask != NULL, CPL_ERROR_NULL_INPUT,
289 "mask image",
"is mandatory");
291 err = cpl_image_multiply(tmp_img, mask);
292 if (err != CPL_ERROR_NONE)
294 cpl_image_delete(tmp_img);
299 collapsed_img = cpl_image_collapse_create(tmp_img,0);
301 if (collapsed_img == NULL)
303 cpl_image_delete(tmp_img);
304 MUCLIPM_TRY_EXIT_WITH_ERROR_MSG
305 (cpl_error_get_code(),
"collapsed image",
"could not create");
308 cpl_image_delete(tmp_img);
313 cpl_version = cpl_version_get_major() + 0.1*cpl_version_get_minor();
316 if (cpl_version >= 5.2) {
317 outvec = cpl_bivector_new(MUCLIPM_NB_SPEC_PER_SLICE);
319 imax = MUCLIPM_NB_SPEC_PER_SLICE;
323 outvec = cpl_bivector_new(MUCLIPM_NB_SPEC_PER_SLICE + 1);
325 imax = MUCLIPM_NB_SPEC_PER_SLICE + 1;
330 MUCLIPM_TRY_EXIT_WITH_ERROR_MSG
331 (cpl_error_get_code(),
"bivector outvec",
"could not create");
334 outvec_x_data = cpl_bivector_get_x_data(outvec);
335 outvec_y_data = cpl_bivector_get_y_data(outvec);
337 outvec_x_data[0] = 1.;
338 for (m = imin; m < imax ; m++)
340 outvec_x_data[m] = (double)(m + 1.0);
344 for (k= 1; k <= MUCLIPM_NB_SUBSLICERS; k++)
347 for (l= 1; l <= MUCLIPM_NB_SLICES; l++)
349 noslice = (k-1)*MUCLIPM_NB_SLICES + l-1;
351 n = slice_xend[noslice] - slice_xstart[noslice] +1;
353 if ((n - 2*MUCLIPM_OVERSCAN) > (MUCLIPM_NB_SPEC_PER_SLICE -2))
356 n -= 2*MUCLIPM_OVERSCAN;
364 invec_x = cpl_vector_new(n);
365 invec_y = cpl_vector_new(n);
366 if (invec_x == NULL || invec_y == NULL)
368 cpl_image_delete(collapsed_img);
369 MUCLIPM_TRY_EXIT_WITH_ERROR_MSG
370 (cpl_error_get_code(),
"vector invecx or invecy",
374 invec_x_data = cpl_vector_get_data(invec_x);
375 invec_y_data = cpl_vector_get_data(invec_y);
377 for (i=slice_xstart[noslice], j=0;
378 (i <= slice_xend[noslice]) && (i < raw_image_nx) ; i++)
381 val = (double)cpl_image_get(collapsed_img, i, 1, &err);
393 (MUCLIPM_NB_SPEC_PER_SLICE + 1) / n);
394 invec_y_data[j] = val;
399 if ((i <= raw_image_nx/2 - MUCLIPM_OVERSCAN)
400 || (i > raw_image_nx/2 + MUCLIPM_OVERSCAN))
404 (MUCLIPM_NB_SPEC_PER_SLICE + 1) / n);
405 invec_y_data[j] = val;
412 invec = cpl_bivector_wrap_vectors(invec_x, invec_y);
415 cpl_image_delete(collapsed_img);
416 cpl_bivector_delete(outvec);
417 cpl_bivector_delete(invec);
418 MUCLIPM_TRY_EXIT_WITH_ERROR_MSG
419 (cpl_error_get_code(),
"bivector invec",
423 outvec_size= cpl_bivector_get_size(outvec);
425 if (outvec_size >= n)
428 min = (n < MUCLIPM_NB_SPEC_PER_SLICE) ?
429 n : MUCLIPM_NB_SPEC_PER_SLICE;
431 for (i=imin, m= 0; i < imax && m < min; i++, m++)
432 outvec_y_data[i] = invec_y_data[m];
437 err = cpl_bivector_interpolate_linear(outvec, invec);
440 cpl_image_delete(collapsed_img);
441 cpl_bivector_delete(outvec);
442 cpl_bivector_delete(invec);
443 MUCLIPM_TRY_EXIT_WITH_ERROR_MSG
444 (err,
"bivector invec",
"could not interpolate");
449 wr_row = MUCLIPM_NB_SLICES - slit_position[l - 1] + 1;
450 wr_col = k * MUCLIPM_NB_SPEC_PER_SLICE;
451 for (i=imin; i< imax ; i++, wr_col--)
453 cpl_image_set(*out_img, wr_col, wr_row,
454 (
float)outvec_y_data[i]);
456 cpl_bivector_delete(invec);
460 cpl_bivector_delete(outvec);
461 cpl_image_delete(collapsed_img);
469 return MUCLIPM_ERROR_GET_NEW_SINCE_TRY();
cpl_error_code muclipm_make_image(const cpl_image *raw_img, const cpl_image *offset, const cpl_image *flat, const cpl_image *mask, const long *slice_xstart, const long *slice_xend, cpl_image **out_img)
Collapses spectra over the spectral direction.
cpl_error_code muclipm_bias_subtract_using_overscan(cpl_image *raw_img)
Evaluates per quadrant overscan median values and subtract them from input image. ...