CR2RE Pipeline Reference Manual 1.6.10
cr2res_flat.c
1/*
2 * This file is part of the CR2RES Pipeline
3 * Copyright (C) 2002,2003 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27#include <math.h>
28#include <cpl.h>
29
30#include "cr2res_bpm.h"
31#include "cr2res_flat.h"
32
33/*-----------------------------------------------------------------------------
34 Functions prototypes
35 -----------------------------------------------------------------------------*/
36
37static cpl_image * cr2res_bpm_from_master_flat(
38 const hdrl_image * master_flat,
39 double low,
40 double high,
41 double bad_per_line_limit) ;
42
43/*----------------------------------------------------------------------------*/
47/*----------------------------------------------------------------------------*/
48
51/*----------------------------------------------------------------------------*/
62/*----------------------------------------------------------------------------*/
63hdrl_image * cr2res_master_flat(
64 const hdrl_image * collapsed,
65 const hdrl_image * model_master,
66 double low,
67 double high,
68 double bad_per_line_limit,
69 cpl_image ** bpm)
70{
71 hdrl_image * master_flat ;
72 cpl_image * bpm_loc ;
73
74 /* Check Entries */
75 if (collapsed == NULL) return NULL ;
76 if (model_master == NULL) return NULL ;
77
78 /* Compute the master flat */
79 if ((master_flat = hdrl_image_div_image_create(collapsed,
80 model_master)) == NULL) {
81 cpl_msg_error(__func__, "Failed to divide by the model") ;
82 return NULL ;
83 }
84
85 /* Compute BPM */
86 if ((bpm_loc = cr2res_bpm_from_master_flat(master_flat, low, high,
87 bad_per_line_limit)) == NULL) {
88 cpl_msg_error(__func__, "Failed to compute the BPM") ;
89 hdrl_image_delete(master_flat) ;
90 return NULL ;
91 }
92
93 if (bpm != NULL) *bpm = bpm_loc ;
94 else cpl_image_delete(bpm_loc) ;
95 return master_flat ;
96}
97
100/*----------------------------------------------------------------------------*/
106/*----------------------------------------------------------------------------*/
107static cpl_image * cr2res_bpm_from_master_flat(
108 const hdrl_image * master_flat,
109 double low,
110 double high,
111 double bad_per_line_limit)
112{
113 const cpl_image * ima ;
114 const double * pima ;
115 cpl_mask * mask_flat ;
116 cpl_mask * mask_inter_order ;
117 cpl_binary * pmask_flat ;
118 cpl_binary * pmask_inter_order ;
119 cpl_image * bpm ;
120 int nx, ny;
121 int i, j ;
122
123 /* Test entries */
124 if (master_flat == NULL) return NULL ;
125
126 /* Initialise */
127 ima = hdrl_image_get_image_const(master_flat) ;
128 pima = cpl_image_get_data_double_const(ima) ;
129 nx = cpl_image_get_size_x(ima) ;
130 ny = cpl_image_get_size_y(ima) ;
131 mask_flat = cpl_mask_new(nx, ny) ;
132 pmask_flat = cpl_mask_get_data(mask_flat) ;
133 mask_inter_order = cpl_mask_new(nx, ny) ;
134 pmask_inter_order = cpl_mask_get_data(mask_inter_order) ;
135
136 /* Threshold to get the BPMs */
137 for (j=0 ; j<ny ; j++) {
138 for (i=0 ; i<nx ; i++) {
139 if (isinf(pima[i+j*nx])) continue ;
140 if (isnan(pima[i+j*nx])) {
141 pmask_inter_order[i+j*nx] = CPL_BINARY_1 ;
142 } else if (pima[i+j*nx] < low || pima[i+j*nx] > high) {
143 pmask_flat[i+j*nx] = CPL_BINARY_1 ;
144 }
145 }
146 }
147
148 /*
149 Post processing : Big zones of bad pixels are not considered as
150 bad pixels. Each line containing more than
151 100*bad_per_line_limit percent bad pixels is reset to contain
152 only good pixels.
153 */
154 for (j=0 ; j<ny ; j++) {
155 int cur_bp_nb;
156 cur_bp_nb = cpl_mask_count_window(mask_flat, 1, j+1, nx, j+1) ;
157 /* Check if the line has too many bad pixels */
158 if (cur_bp_nb > bad_per_line_limit * nx) {
159 /* Reset the bad pixels on the current line */
160 for (i=0 ; i<nx ; i++) pmask_flat[i+j*nx] = CPL_BINARY_0 ;
161 }
162 }
163
164 /* Create BPM */
165 bpm = cpl_image_new(nx, ny, CPL_TYPE_INT) ;
166 cr2res_bpm_add_mask(bpm, mask_flat, CR2RES_BPM_FLAT) ;
167 cr2res_bpm_add_mask(bpm, mask_inter_order, CR2RES_BPM_OUTOFORDER) ;
168
169 cpl_mask_delete(mask_flat) ;
170 cpl_mask_delete(mask_inter_order) ;
171 return bpm ;
172}
173
int cr2res_bpm_add_mask(cpl_image *bpm_ima, cpl_mask *bpm, int bpm_code)
Add a mask to a BPM image with a dedicated code
Definition: cr2res_bpm.c:287
hdrl_image * cr2res_master_flat(const hdrl_image *collapsed, const hdrl_image *model_master, double low, double high, double bad_per_line_limit, cpl_image **bpm)
Compute the Master Flat.
Definition: cr2res_flat.c:63
hdrl_image * hdrl_image_div_image_create(const hdrl_image *self, const hdrl_image *other)
Divide two images and return the resulting image.
const cpl_image * hdrl_image_get_image_const(const hdrl_image *himg)
get data as cpl image
Definition: hdrl_image.c:118
void hdrl_image_delete(hdrl_image *himg)
delete hdrl_image
Definition: hdrl_image.c:379