MOONS Pipeline Reference Manual 0.13.2
moo_badpix.c
1/*
2 * This file is part of the MOONS Pipeline
3 * Copyright (C) 2002-2016 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 Street, Fifth Floor, Boston, MA 02110-1301 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#include "moo_badpix.h"
30/*----------------------------------------------------------------------------*/
35/*----------------------------------------------------------------------------*/
36
40/*----------------------------------------------------------------------------*/
56/*----------------------------------------------------------------------------*/
57cpl_error_code
58moo_badpix_to_mask(cpl_image *badpix, cpl_mask *mask, unsigned int level)
59{
60 cpl_ensure_code(badpix != NULL, CPL_ERROR_NULL_INPUT);
61 cpl_ensure_code(mask != NULL, CPL_ERROR_NULL_INPUT);
62
63 cpl_type type = cpl_image_get_type(badpix);
64 cpl_ensure_code(type == CPL_TYPE_INT, CPL_ERROR_ILLEGAL_INPUT);
65
66 int nx = cpl_image_get_size_x(badpix);
67 int ny = cpl_image_get_size_y(badpix);
68 int mask_nx = cpl_mask_get_size_x(mask);
69 int mask_ny = cpl_mask_get_size_y(mask);
70 cpl_ensure_code(nx == mask_nx, CPL_ERROR_ILLEGAL_INPUT);
71 cpl_ensure_code(ny == mask_ny, CPL_ERROR_ILLEGAL_INPUT);
72
73 int *data = cpl_image_get_data_int(badpix);
74 cpl_binary *mdata = cpl_mask_get_data(mask);
75
76 int i;
77 for (i = 0; i < nx * ny; i++) {
78 if ((data[i] & level) > 0) {
79 mdata[i] = 1;
80 }
81 else {
82 mdata[i] = 0;
83 }
84 }
85 return CPL_ERROR_NONE;
86}
87
88/*----------------------------------------------------------------------------*/
103/*----------------------------------------------------------------------------*/
104cpl_error_code
105moo_mask_to_badpix(cpl_image *badpix, cpl_mask *mask, unsigned int level)
106{
107 cpl_ensure_code(badpix != NULL, CPL_ERROR_NULL_INPUT);
108 cpl_ensure_code(mask != NULL, CPL_ERROR_NULL_INPUT);
109
110 cpl_type type = cpl_image_get_type(badpix);
111 cpl_ensure_code(type == CPL_TYPE_INT, CPL_ERROR_ILLEGAL_INPUT);
112
113 int nx = cpl_image_get_size_x(badpix);
114 int ny = cpl_image_get_size_y(badpix);
115 int mask_nx = cpl_mask_get_size_x(mask);
116 int mask_ny = cpl_mask_get_size_y(mask);
117 cpl_ensure_code(nx == mask_nx, CPL_ERROR_ILLEGAL_INPUT);
118 cpl_ensure_code(ny == mask_ny, CPL_ERROR_ILLEGAL_INPUT);
119
120 int *data = cpl_image_get_data_int(badpix);
121 cpl_binary *mdata = cpl_mask_get_data(mask);
122 int i;
123
124 for (i = 0; i < nx * ny; i++) {
125 if (mdata[i] > 0) {
126 data[i] |= level;
127 }
128 }
129 return CPL_ERROR_NONE;
130}
131
132/*----------------------------------------------------------------------------*/
144/*----------------------------------------------------------------------------*/
145cpl_error_code
146moo_badpix_merge(cpl_image *badpix1, cpl_image *badpix2)
147{
148 cpl_ensure_code(badpix1 != NULL, CPL_ERROR_NULL_INPUT);
149 cpl_ensure_code(badpix2 != NULL, CPL_ERROR_NULL_INPUT);
150
151 cpl_type type1 = cpl_image_get_type(badpix1);
152 cpl_ensure_code(type1 == CPL_TYPE_INT, CPL_ERROR_ILLEGAL_INPUT);
153 cpl_type type2 = cpl_image_get_type(badpix2);
154 cpl_ensure_code(type2 == CPL_TYPE_INT, CPL_ERROR_ILLEGAL_INPUT);
155
156 int nx = cpl_image_get_size_x(badpix1);
157 int ny = cpl_image_get_size_y(badpix1);
158 int nx2 = cpl_image_get_size_x(badpix2);
159 int ny2 = cpl_image_get_size_y(badpix2);
160
161 cpl_ensure_code(nx == nx2, CPL_ERROR_ILLEGAL_INPUT);
162 cpl_ensure_code(ny == ny2, CPL_ERROR_ILLEGAL_INPUT);
163 int *data1 = cpl_image_get_data_int(badpix1);
164 int *data2 = cpl_image_get_data_int(badpix2);
165 int i;
166
167 for (i = 0; i < nx * ny; i++) {
168 data1[i] |= data2[i];
169 }
170 return CPL_ERROR_NONE;
171}
cpl_error_code moo_mask_to_badpix(cpl_image *badpix, cpl_mask *mask, unsigned int level)
Add the mask of the badpix level to the badpix map.
Definition: moo_badpix.c:105
cpl_error_code moo_badpix_merge(cpl_image *badpix1, cpl_image *badpix2)
Merge to bad pixel map.
Definition: moo_badpix.c:146
cpl_error_code moo_badpix_to_mask(cpl_image *badpix, cpl_mask *mask, unsigned int level)
Apply the badpix map on the given mask.
Definition: moo_badpix.c:58