38#include "eris_nix_master_bpm.h"
39#include "eris_nix_utils.h"
61 cpl_image_delete((cpl_image *) target->bpm);
62 cpl_free((
char *) target->filename);
63 cpl_propertylist_delete((cpl_propertylist *) target->plist);
81master_bpm * en_master_bpm_load_from_frameset(
const cpl_frameset * frameset,
85 master_bpm * result = NULL;
86 cpl_image * bpm = NULL;
87 cpl_image * full_bpm = NULL;
88 cpl_propertylist * plist = NULL;
90 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
91 cpl_ensure(frameset, CPL_ERROR_NULL_INPUT, NULL);
92 cpl_ensure(tag, CPL_ERROR_NULL_INPUT, NULL);
96 const cpl_frame * target_frame = cpl_frameset_find_const(frameset, tag);
97 enu_check(target_frame != NULL, CPL_ERROR_DATA_NOT_FOUND,
98 "SoF has no file tagged %s", tag);
102 const char * filename = cpl_frame_get_filename(target_frame);
103 bpm = cpl_image_load(filename, CPL_TYPE_INT, 0, 0);
104 plist = cpl_propertylist_load(filename, 0);
113 cpl_size nx_chip = 0;
114 cpl_size ny_chip = 0;
115 cpl_boolean windowed = 0;
125 enu_check_error_code(
"Failed to read bpm window information");
127 if (nx != nx_chip || ny != ny_chip) {
128 cpl_msg_info(cpl_func,
"windowed bpm, extending to cover full chip");
129 full_bpm = cpl_image_new(nx_chip,
132 cpl_image_copy(full_bpm,
137 full_bpm = bpm; bpm = NULL;
140 result = en_master_bpm_create_from_image(filename, full_bpm, plist);
144 cpl_image_delete(bpm);
145 cpl_image_delete(full_bpm);
146 cpl_propertylist_delete(plist);
148 if (cpl_error_get_code() == CPL_ERROR_NONE) {
149 cpl_frame * dup_bpmframe = cpl_frame_duplicate(target_frame);
150 cpl_frameset_insert(used, dup_bpmframe);
160 cpl_msg_warning(cpl_func,
"no file tagged %s in SoF", tag);
182master_bpm * en_master_bpm_create(
const char * filename,
183 const cpl_mask * mask,
184 const bpm_code flag_code,
185 const cpl_propertylist * plist) {
187 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
188 cpl_ensure(mask && flag_code, CPL_ERROR_NULL_INPUT, NULL);
190 master_bpm * result = cpl_malloc(
sizeof(master_bpm));
192 cpl_size nx = cpl_mask_get_size_x(mask);
193 cpl_size ny = cpl_mask_get_size_y(mask);
194 result->bpm = cpl_image_new(nx, ny, CPL_TYPE_INT);
198 int * bpm_data = cpl_image_get_data_int((cpl_image *) result->bpm);
199 const cpl_binary * mask_data = cpl_mask_get_data_const(mask);
201 int bit_mask = (1 << (flag_code-1));
202 for (cpl_size i=0; i < nx * ny; i++) {
204 bpm_data[i] |= bit_mask;
206 bpm_data[i] &= (~bit_mask);
211 result->filename = cpl_strdup(filename);
213 result->filename = NULL;
216 result->plist = cpl_propertylist_duplicate(plist);
218 result->plist = NULL;
240master_bpm * en_master_bpm_create_from_image(
const char * filename,
241 const cpl_image * image,
242 const cpl_propertylist * plist) {
244 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
245 cpl_ensure(image, CPL_ERROR_NULL_INPUT, NULL);
247 master_bpm * result = cpl_malloc(
sizeof(master_bpm));
249 result->bpm = cpl_image_duplicate(image);
252 result->filename = cpl_strdup(filename);
254 result->filename = NULL;
257 result->plist = cpl_propertylist_duplicate(plist);
259 result->plist = NULL;
282cpl_mask * en_master_bpm_get_mask(
const master_bpm * master,
283 const int flag_mask) {
285 if (cpl_error_get_code() != CPL_ERROR_NONE)
return NULL;
286 cpl_ensure(master, CPL_ERROR_NULL_INPUT, NULL);
288 cpl_size nx = cpl_image_get_size_x(master->bpm);
289 cpl_size ny = cpl_image_get_size_y(master->bpm);
291 cpl_mask * result = cpl_mask_new(nx, ny);
292 cpl_binary * result_data = cpl_mask_get_data(result);
293 const int * bpm_data = cpl_image_get_data_int_const(master->bpm);
295 for (cpl_size j=0; j<ny; j++) {
296 for (cpl_size i=0; i<nx; i++) {
297 result_data[nx*j+i] = (cpl_binary) ((bpm_data[nx*j+i] & flag_mask) > 0);
301 if (cpl_error_get_code() != CPL_ERROR_NONE) {
302 cpl_mask_delete(result);
323cpl_error_code en_master_bpm_set(master_bpm * master,
324 const cpl_mask * mask,
325 bpm_code flag_code) {
327 if (cpl_error_get_code() != CPL_ERROR_NONE)
return cpl_error_get_code();
328 cpl_ensure_code(master && mask, CPL_ERROR_NULL_INPUT);
332 cpl_size nx = cpl_mask_get_size_x(mask);
333 cpl_size ny = cpl_mask_get_size_y(mask);
334 cpl_size nx_image = cpl_image_get_size_x(master->bpm);
335 cpl_size ny_image = cpl_image_get_size_y(master->bpm);
336 cpl_ensure_code((nx == nx_image) && (ny == ny_image),
337 CPL_ERROR_INCOMPATIBLE_INPUT);
341 int * bpm_data = cpl_image_get_data_int((cpl_image *) master->bpm);
342 const cpl_binary * mask_data = cpl_mask_get_data_const(mask);
344 int bit_mask = (1 << (flag_code-1));
345 for (cpl_size i=0; i < nx * ny; i++) {
347 bpm_data[i] |= bit_mask;
349 bpm_data[i] &= (~bit_mask);
353 return cpl_error_get_code();
void en_master_bpm_delete(master_bpm *target)
Delete a 'master_bpm' struct.
cpl_error_code enu_get_window_info(cpl_size *nx, cpl_size *ny, int *rot, cpl_size *strx, cpl_size *stry, cpl_size *nx_chip, cpl_size *ny_chip, cpl_boolean *windowed, const cpl_propertylist *plist)
Get the detector 'window' information.