Example #1
0
File: data.c Project: glenco/lensed
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, &regmask, 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;
}
Example #2
0
void doreg(char *cards, char *regstr,
	   int x0, int x1, int y0, int y1, int block, int mode){
  int i, c, x, y, nreg, nmask;
  int xmin=LARGENUM, xmax=0, xdim=0;
  int ymin=LARGENUM, ymax=0, ydim=0;
  char **lines=NULL;
  Regions reg=NULL;
  RegionsMask mask=NULL;

  reg = OpenRegions(cards, regstr, NULL);
  nmask = FilterRegions(reg, x0, x1, y0, y1, block, &mask, &nreg);
  fprintf(stdout, "regions: %s\n", regstr);
  switch(mode ){
  case 0:
    for(i=0; i<nmask; i++){
      fprintf(stdout, "#%d: region=%d y=%d, x=%d,%d\n", 
	      i, mask[i].region, mask[i].y, mask[i].xstart, mask[i].xstop);
    }
    break;
  case 1:
    for(i=0; i<nmask; i++){
      // get mask limits
      if( mask[i].xstart < xmin ){
	xmin = mask[i].xstart;
      }
      if( mask[i].xstop > xmax ){
	xmax = mask[i].xstop;
      }
      if( mask[i].y < ymin ){
	ymin = mask[i].y;
      }
      if( mask[i].y > ymax ){
	ymax = mask[i].y;
      }
    }
    // show a border around the mask
    xmin = MAX(1, xmin - BORDER);
    xmax = MIN(x1-x0+1, xmax + BORDER);
    ymin = MAX(1, ymin - BORDER);
    ymax = MIN(y1-y0+1, ymax + BORDER);
    xdim = xmax - xmin + 1;
    ydim = ymax - ymin + 1;
    // allocate enough lines to display mask
    lines = calloc(ydim, sizeof(char *));
    // allocate char buffers for each line and see with "."
    for(i=0; i<ydim; i++){
      lines[i] = calloc(xdim + 1, sizeof(char));
      memset(lines[i], '.', xdim);
    }
    // fill the mask with region ids
    for(i=0; i<nmask; i++){
      y = mask[i].y - ymin;
      for(x=mask[i].xstart-xmin; x<=mask[i].xstop-xmin; x++){
	/* regions start with '1' */
	c = mask[i].region + 48;
	/* but wrap around to 0 after ~ */
	while( c > 126 ){
	  c -= 79;
	}
	lines[y][x] = c;
      }
    }
    // display mask (but reverse order)
    for(i=ydim-1; i>=0; i--){
      fprintf(stdout, "%s", lines[i]);
      fprintf(stdout, "\n");
    }
    fprintf(stdout, "LL corner: %d,%d\n", x0 + xmin - 1, y0 + ymin - 1);
    fprintf(stdout, "UR corner: %d,%d\n", x0 + xmax - 1, y0 + ymax - 1);
    break;
  }
  fflush(stdout);
  /* clean up */
  CloseRegions(reg);
  xfree(mask);
  for(i=0; i<ydim; i++){
    xfree(lines[i]);
  }
  xfree(lines);
}