c_begin.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <stdlib.h>
  2. #include <grass/cluster.h>
  3. /****************************************************************
  4. * I_cluster_begin (C,nbands)
  5. *
  6. * initialize the cluster routines for nbands
  7. *
  8. * returns
  9. * 0 ok
  10. * -1 out of memory
  11. * 1 illegal number of bands
  12. *
  13. ***************************************************************/
  14. int I_cluster_begin(struct Cluster *C, int nbands)
  15. {
  16. int band;
  17. if (C->points != NULL) {
  18. for (band = 0; band < C->nbands; band++)
  19. if (C->points[band] != NULL)
  20. free(C->points[band]);
  21. free(C->points);
  22. }
  23. if (C->band_sum != NULL)
  24. free(C->band_sum);
  25. if (C->band_sum2 != NULL)
  26. free(C->band_sum2);
  27. C->points = NULL;
  28. C->band_sum = NULL;
  29. C->band_sum2 = NULL;
  30. I_free_signatures(&C->S);
  31. /* record the number of bands */
  32. C->nbands = nbands;
  33. if (nbands <= 0)
  34. return 1;
  35. /* prepare the signatures for nbands */
  36. I_init_signatures(&C->S, nbands);
  37. sprintf(C->S.title, "produced by i.cluster");
  38. /* allocate the data (points) arrays */
  39. C->points = (DCELL **) malloc(C->nbands * sizeof(DCELL *));
  40. if (C->points == NULL)
  41. return -1;
  42. for (band = 0; band < C->nbands; band++)
  43. C->points[band] = NULL;
  44. C->np = 128;
  45. for (band = 0; band < C->nbands; band++) {
  46. C->points[band] = (DCELL *) malloc(C->np * sizeof(DCELL));
  47. if (C->points[band] == NULL)
  48. return -1;
  49. }
  50. /* initialize the count to zero */
  51. C->npoints = 0;
  52. /* allocate the band sums and means */
  53. C->band_sum = (double *)malloc(C->nbands * sizeof(double));
  54. if (C->band_sum == NULL)
  55. return -1;
  56. C->band_sum2 = (double *)malloc(C->nbands * sizeof(double));
  57. if (C->band_sum2 == NULL)
  58. return -1;
  59. for (band = 0; band < C->nbands; band++) {
  60. C->band_sum[band] = 0;
  61. C->band_sum2[band] = 0;
  62. }
  63. return 0;
  64. }