histo.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. **********************************************************************
  3. *
  4. * MODULE: r.support.stats
  5. *
  6. * AUTHOR(S): Brad Douglas <rez touchofmadness com>
  7. *
  8. * PURPOSE: Update raster statistics
  9. *
  10. * COPYRIGHT: (C) 2006 by the GRASS Development Team
  11. *
  12. * This program is free software under the GNU General
  13. * Purpose License (>=v2). Read the file COPYING that
  14. * comes with GRASS for details.
  15. *
  16. ***********************************************************************/
  17. #include <stdlib.h>
  18. #include <grass/gis.h>
  19. #include <grass/raster.h>
  20. /*
  21. * do_histogram() - Creates histogram for CELL
  22. *
  23. * RETURN: 0 on success / 1 on failure
  24. */
  25. int do_histogram(const char *name)
  26. {
  27. CELL *cell;
  28. struct Cell_head cellhd;
  29. struct Cell_stats statf;
  30. int nrows, ncols;
  31. int row;
  32. int fd;
  33. Rast_get_cellhd(name, "", &cellhd);
  34. Rast_set_window(&cellhd);
  35. fd = Rast_open_old(name, "");
  36. nrows = Rast_window_rows();
  37. ncols = Rast_window_cols();
  38. cell = Rast_allocate_c_buf();
  39. Rast_init_cell_stats(&statf);
  40. /* Update statistics for each row */
  41. for (row = 0; row < nrows; row++) {
  42. G_percent(row, nrows, 2);
  43. Rast_get_c_row_nomask(fd, cell, row);
  44. Rast_update_cell_stats(cell, ncols, &statf);
  45. }
  46. /* Write histogram if it made it through the loop */
  47. if (row == nrows)
  48. Rast_write_histogram_cs(name, &statf);
  49. Rast_free_cell_stats(&statf);
  50. Rast_close(fd);
  51. G_free(cell);
  52. if (row == nrows)
  53. return 0;
  54. return 1;
  55. }