00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifdef HAVE_CONFIG_H
00031 #include <config.h>
00032 #endif
00033
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <unistd.h>
00037
00038 #include <cpl.h>
00039 #include "vircam_utils.h"
00040 #include "vircam_fits.h"
00041 #include "vircam_mask.h"
00042 #include "vircam_dfs.h"
00043
00044 static unsigned char *vircam_mask_getbpm(vir_fits *bpmimage);
00045 static unsigned char *vircam_mask_conf2bpm(vir_fits *cpmimage);
00046
00060
00084
00085
00086 extern vir_mask *vircam_mask_define(cpl_frameset *framelist, int *labels,
00087 int nlab) {
00088 cpl_frame *master_mask;
00089 int masktype;
00090 vir_mask *m;
00091 char *fctid = "vircam_define_mask";
00092
00093
00094
00095 if ((master_mask = vircam_frameset_subgroup_1(framelist,labels,nlab,
00096 VIRCAM_CAL_BPM)) == NULL) {
00097 if ((master_mask = vircam_frameset_subgroup_1(framelist,labels,
00098 nlab,VIRCAM_CAL_CONF)) == NULL) {
00099 cpl_msg_info(fctid,
00100 "No master pixel mask found -- all pixels considered good");
00101 masktype = MASK_NONE;
00102 } else {
00103 masktype = MASK_CPM;
00104 }
00105 } else {
00106 masktype = MASK_BPM;
00107 }
00108
00109
00110
00111
00112 if (master_mask != NULL) {
00113 if (access(cpl_frame_get_filename(master_mask),R_OK) != 0) {
00114 cpl_msg_warning(fctid,"File %s is not read accessible",
00115 cpl_frame_get_filename(master_mask));
00116 masktype = MASK_NONE;
00117 freeframe(master_mask);
00118 }
00119 }
00120
00121
00122
00123 m = cpl_malloc(sizeof(*m));
00124
00125
00126
00127 m->master_mask = master_mask;
00128 m->mask_image = NULL;
00129 m->masktype = masktype;
00130 m->nx = 0;
00131 m->ny = 0;
00132 m->mdata = NULL;
00133
00134
00135
00136 return(m);
00137 }
00138
00139
00162
00163
00164 extern int vircam_mask_load(vir_mask *m, int nexten, int nx, int ny) {
00165
00166
00167
00168 if (m == NULL)
00169 return(VIR_FATAL);
00170
00171
00172
00173 if (nx <= 0 && ny <= 0 && m->masktype == MASK_NONE)
00174 return(VIR_FATAL);
00175
00176
00177
00178 if (m->mask_image != NULL) {
00179 vircam_fits_delete(m->mask_image);
00180 freespace(m->mdata);
00181 }
00182
00183
00184
00185 if (m->masktype != MASK_NONE) {
00186 m->mask_image = vircam_fits_load(m->master_mask,CPL_TYPE_INT,nexten);
00187 if (m->mask_image == NULL)
00188 return(VIR_FATAL);
00189 m->nx = cpl_image_get_size_x(vircam_fits_get_image(m->mask_image));
00190 m->ny = cpl_image_get_size_y(vircam_fits_get_image(m->mask_image));
00191 } else {
00192 m->nx = nx;
00193 m->ny = ny;
00194 }
00195
00196
00197
00198 return(VIR_OK);
00199 }
00200
00201
00220
00221
00222 extern void vircam_mask_delete(vir_mask *m) {
00223
00224 if (m == NULL)
00225 return;
00226 vircam_mask_clear(m);
00227 freeframe(m->master_mask);
00228 freespace(m);
00229 }
00230
00231
00250
00251
00252 extern void vircam_mask_clear(vir_mask *m) {
00253 if (m == NULL)
00254 return;
00255 freespace(m->mdata);
00256 freefits(m->mask_image);
00257 m->nx = 0;
00258 m->ny = 0;
00259 }
00260
00261
00286
00287
00288 extern void vircam_mask_force(vir_mask *m, int nx, int ny) {
00289 if (m == NULL)
00290 return;
00291 freespace(m->mdata);
00292 freefits(m->mask_image);
00293 freeframe(m->master_mask);
00294 m->masktype = MASK_NONE;
00295 m->nx = nx;
00296 m->ny = ny;
00297 }
00298
00299
00316
00317
00318 extern vir_fits *vircam_mask_get_fits(vir_mask *m) {
00319 return(m->mask_image);
00320 }
00321
00322
00339
00340
00341 extern const char *vircam_mask_get_filename(vir_mask *m) {
00342 if (m->master_mask != NULL) {
00343 return(cpl_frame_get_filename(m->master_mask));
00344 } else {
00345 return(NULL);
00346 }
00347 }
00348
00349
00350
00367
00368
00369 extern int vircam_mask_get_size_x(vir_mask *m) {
00370 return(m->nx);
00371 }
00372
00373
00390
00391
00392 extern int vircam_mask_get_size_y(vir_mask *m) {
00393 return(m->ny);
00394 }
00395
00396
00413
00414
00415 extern int vircam_mask_get_type(vir_mask *m) {
00416 return(m->masktype);
00417 }
00418
00419
00436
00437
00438 extern unsigned char *vircam_mask_get_data(vir_mask *m) {
00439 unsigned char *bpm;
00440 long npix;
00441
00442
00443
00444 if (m->mdata != NULL)
00445 return(m->mdata);
00446
00447
00448
00449 switch (m->masktype) {
00450 case MASK_NONE:
00451 npix = (m->nx)*(m->ny);
00452 bpm = cpl_calloc(npix,sizeof(*bpm));
00453 break;
00454 case MASK_BPM:
00455 bpm = vircam_mask_getbpm(vircam_mask_get_fits(m));
00456 break;
00457 case MASK_CPM:
00458 bpm = vircam_mask_conf2bpm(vircam_mask_get_fits(m));
00459 break;
00460 default:
00461 npix = (m->nx)*(m->ny);
00462 bpm = cpl_calloc(npix,sizeof(*bpm));
00463 break;
00464 }
00465 m->mdata = bpm;
00466 return(bpm);
00467 }
00468
00469
00490
00491
00492 static unsigned char *vircam_mask_getbpm(vir_fits *bpmimage) {
00493 long npts,i;
00494 int *bpmdata;
00495 cpl_image *b;
00496 unsigned char *bpm;
00497
00498
00499
00500 b = vircam_fits_get_image(bpmimage);
00501 npts = cpl_image_get_size_x(b)*cpl_image_get_size_y(b);
00502 bpmdata = cpl_image_get_data(b);
00503
00504
00505
00506 bpm = cpl_malloc(npts*sizeof(*bpm));
00507 for (i = 0; i < npts; i++)
00508 bpm[i] = (unsigned char)bpmdata[i];
00509
00510
00511
00512 return(bpm);
00513 }
00514
00515
00535
00536
00537 static unsigned char *vircam_mask_conf2bpm(vir_fits *cpmimage) {
00538 long npts,i;
00539 int *cpmdata;
00540 cpl_image *c;
00541 unsigned char *bpm;
00542
00543
00544
00545 c = vircam_fits_get_image(cpmimage);
00546 npts = cpl_image_get_size_x(c)*cpl_image_get_size_y(c);
00547 cpmdata = cpl_image_get_data(c);
00548
00549
00550
00551
00552 bpm = cpl_malloc(npts*sizeof(*bpm));
00553 for (i = 0; i < npts; i++)
00554 bpm[i] = (cpmdata[i] == 0);
00555
00556
00557
00558 return(bpm);
00559 }
00560
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600