transform.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/gmath.h>
  4. #include <grass/glocale.h>
  5. #include "local_proto.h"
  6. int
  7. transform(int *datafds, int *outfds, int rows, int cols,
  8. double **eigmat, int bands, CELL *mins, CELL *maxs)
  9. {
  10. int i, j, k, l;
  11. double *sum;
  12. CELL **rowbufs;
  13. sum = G_alloc_vector(bands);
  14. rowbufs = (CELL**)G_calloc(bands, sizeof(CELL*));
  15. /* allocate row buffers for each band */
  16. for (i = 0; i < bands; i++)
  17. if ((rowbufs[i] = Rast_allocate_c_buf()) == NULL)
  18. G_fatal_error(_("Unable to allocate cell buffers."));
  19. for (i = 0; i < rows; i++) {
  20. /* get one row of data */
  21. for (j = 0; j < bands; j++)
  22. Rast_get_c_row(datafds[j], rowbufs[j], i);
  23. /* transform each cell in the row */
  24. for (l = 0; l < cols; l++) {
  25. for (j = 0; j < bands; j++) {
  26. sum[j] = 0.0;
  27. for (k = 0; k < bands; k++) {
  28. sum[j] += eigmat[j][k] * (double)rowbufs[k][l];
  29. }
  30. }
  31. for (j = 0; j < bands; j++) {
  32. rowbufs[j][l] = (CELL) (sum[j] + 0.5);
  33. if (rowbufs[j][l] > maxs[j])
  34. maxs[j] = rowbufs[j][l];
  35. if (rowbufs[j][l] < mins[j])
  36. mins[j] = rowbufs[j][l];
  37. }
  38. }
  39. /* output the row of data */
  40. for (j = 0; j < bands; j++)
  41. Rast_put_row(outfds[j], rowbufs[j], CELL_TYPE);
  42. }
  43. for (i = 0; i < bands; i++)
  44. G_free(rowbufs[i]);
  45. G_free(rowbufs);
  46. G_free_vector(sum);
  47. G_message(_("Transform completed.\n"));
  48. return 0;
  49. }