int read_regions_mask(const char* maskfile, const char* imagefile, const pcsdata* pcs, size_t width, size_t height, int** mask) { // FITS header of image int status = 0; fitsfile* fptr; char* fitshdr; int nkeys; // regions specifications char* regspec; Regions reg; RegionsMask regmask; int nmask; // read image FITS header to string fits_open_file(&fptr, imagefile, READONLY, &status); fits_convert_hdr2str(fptr, 0, NULL, 0, &fitshdr, &nkeys, &status); fits_close_file(fptr, &status); if(status) fits_error(imagefile, status); // make regions specification regspec = malloc(strlen(maskfile) + 3); if(!regspec) errori(NULL); sprintf(regspec, "@%s\n", maskfile); // load regions from file reg = OpenRegions(fitshdr, regspec, NULL); if(!reg) { // could not read regions free(regspec); free(fitshdr); return 1; } // allocate zero-filled space for mask *mask = calloc(width*height, sizeof(int)); if(!*mask) error(NULL, 0); // filter regions for image section nmask = FilterRegions(reg, pcs->rx, pcs->rx + width - 1, pcs->ry, pcs->ry + height - 1, 1, ®mask, NULL); // go through regions and mask pixels for(int i = 0; i < nmask; ++i) for(int x = regmask[i].xstart; x <= regmask[i].xstop; ++x) (*mask)[(regmask[i].y - 1)*width + (x - 1)] = 1; // clean up CloseRegions(reg); free(regspec); free(fitshdr); // mask created return 0; }
void getHeaderToString(fitsfile *fptr, char **cardstr, int *ncard, int *status){ fits_convert_hdr2str(fptr, 1, NULL, 0, cardstr, ncard, status); }