histo.c 935 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. #include "local_proto.h"
  6. /*
  7. * do_histogram() - Creates histogram for CELL
  8. *
  9. * RETURN: EXIT_SUCCESS / EXIT_FAILURE
  10. */
  11. int do_histogram(const char *name)
  12. {
  13. CELL *cell;
  14. struct Cell_head cellhd;
  15. struct Cell_stats statf;
  16. int nrows, ncols;
  17. int row;
  18. int fd;
  19. Rast_get_cellhd(name, "", &cellhd);
  20. Rast_set_window(&cellhd);
  21. fd = Rast_open_old(name, "");
  22. nrows = Rast_window_rows();
  23. ncols = Rast_window_cols();
  24. cell = Rast_allocate_c_buf();
  25. Rast_init_cell_stats(&statf);
  26. for (row = 0; row < nrows; row++) {
  27. Rast_get_c_row_nomask(fd, cell, row);
  28. Rast_update_cell_stats(cell, ncols, &statf);
  29. }
  30. if (row == nrows)
  31. Rast_write_histogram_cs(name, &statf);
  32. Rast_free_cell_stats(&statf);
  33. Rast_close(fd);
  34. G_free(cell);
  35. if (row < nrows)
  36. return -1;
  37. return 0;
  38. }