execute.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <unistd.h>
  2. #include <grass/rowio.h>
  3. #include <grass/raster.h>
  4. #include "glob.h"
  5. #include "filter.h"
  6. int execute_filter(ROWIO * r, int out, FILTER * filter, DCELL * cell)
  7. {
  8. int i;
  9. int count;
  10. int size;
  11. int row, rcount;
  12. int col, ccount;
  13. int startx, starty;
  14. int dx, dy;
  15. int mid;
  16. DCELL **bufs, **box, *cp;
  17. size = filter->size;
  18. mid = size / 2;
  19. bufs = (DCELL **) G_malloc(size * sizeof(DCELL *));
  20. box = (DCELL **) G_malloc(size * sizeof(DCELL *));
  21. switch (filter->start) {
  22. case UR:
  23. startx = ncols - size;
  24. starty = 0;
  25. dx = -1;
  26. dy = 1;
  27. break;
  28. case LL:
  29. startx = 0;
  30. starty = nrows - size;
  31. dx = 1;
  32. dy = -1;
  33. break;
  34. case LR:
  35. startx = ncols - size;
  36. starty = nrows - size;
  37. dx = -1;
  38. dy = -1;
  39. break;
  40. case UL:
  41. default:
  42. startx = 0;
  43. starty = 0;
  44. dx = 1;
  45. dy = 1;
  46. break;
  47. }
  48. direction = dy;
  49. G_debug(3, "direction %d, dx=%d, dy=%d", direction, dx, dy);
  50. rcount = nrows - (size - 1);
  51. ccount = ncols - (size - 1);
  52. /* rewind output */
  53. lseek(out, 0L, 0);
  54. /* copy border rows to output */
  55. row = starty;
  56. for (i = 0; i < mid; i++) {
  57. cp = (DCELL *) Rowio_get(r, row);
  58. write(out, cp, buflen);
  59. row += dy;
  60. }
  61. /* for each row */
  62. for (count = 0; count < rcount; count++) {
  63. G_percent(count, rcount, 2);
  64. row = starty;
  65. starty += dy;
  66. /* get "size" rows */
  67. for (i = 0; i < size; i++) {
  68. bufs[i] = (DCELL *) Rowio_get(r, row);
  69. box[i] = bufs[i] + startx;
  70. row += dy;
  71. }
  72. if (filter->type == SEQUENTIAL)
  73. cell = bufs[mid];
  74. /* copy border */
  75. cp = cell;
  76. for (i = 0; i < mid; i++)
  77. *cp++ = bufs[mid][i];
  78. /* filter row */
  79. col = ccount;
  80. while (col--) {
  81. if (null_only) {
  82. if (Rast_is_d_null_value(&box[mid][mid]))
  83. *cp++ = apply_filter(filter, box);
  84. else
  85. *cp++ = box[mid][mid];
  86. }
  87. else {
  88. *cp++ = apply_filter(filter, box);
  89. }
  90. for (i = 0; i < size; i++)
  91. box[i] += dx;
  92. }
  93. /* copy border */
  94. for (i = ncols - mid; i < ncols; i++)
  95. *cp++ = bufs[mid][i];
  96. /* write row */
  97. write(out, cell, buflen);
  98. }
  99. G_percent(count, rcount, 2);
  100. /* copy border rows to output */
  101. row = starty + mid * dy;
  102. for (i = 0; i < mid; i++) {
  103. cp = (DCELL *) Rowio_get(r, row);
  104. write(out, cp, buflen);
  105. row += dy;
  106. }
  107. return 0;
  108. }