GNU Astronomy Utilities


Next: , Previous: , Up: Library demo programs   [Contents][Index]


10.4.2 Library demo - inspecting neighbors

The following simple program shows how you can inspect the neighbors of a pixel using the GAL_DIMENSION_NEIGHBOR_OP function-like macro that was introduced in Dimensions (dimension.h). For easy linking/compilation of this program along with a first run see BuildProgram. Before running, also change the file name and HDU (first and second arguments to gal_fits_img_read_to_type) to specify an existing FITS file and/or extension/HDU.

#include <stdio.h>
#include <gnuastro/fits.h>
#include <gnuastro/dimension.h>

int
main(void)
{
  double sum;
  float *array;
  size_t i, num, *dinc;
  gal_data_t *input=gal_fits_img_read_to_type("input.fits", "1",
                                              GAL_TYPE_FLOAT32, -1);

  /* To avoid the `void *' pointer and have `dinc'. */
  array=input->array;
  dinc=gal_dimension_increment(input->ndim, input->dsize);

  /* Go over all the pixels. */
  for(i=0;i<input->size;++i)
    {
      num=0;
      sum=0.0f;
      GAL_DIMENSION_NEIGHBOR_OP( i, input->ndim, input->dsize,
                                 input->ndim, dinc,
                                 {++num; sum+=array[nind];} );
      printf("%zu: num: %zu, sum: %f\n", i, num, sum);
    }

  /* Clean up and return. */
  gal_data_free(input);
  return EXIT_SUCCESS;
}