getfilt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <string.h>
  2. #include <grass/glocale.h>
  3. #include "filter.h"
  4. #include "local_proto.h"
  5. FILTER *get_filter(char *name, int *nfilters, char *title)
  6. {
  7. FILE *fd;
  8. FILTER *filter, *f;
  9. char buf[300];
  10. char temp[100];
  11. char label[50];
  12. int row, col;
  13. int n;
  14. int have_divisor;
  15. int have_type;
  16. int have_start;
  17. int count;
  18. double div;
  19. f = filter = 0;
  20. count = *nfilters = 0;
  21. *title = 0;
  22. fd = fopen(name, "r");
  23. if (!fd) {
  24. G_fatal_error(_("Cannot open filter file '%s'"), name);
  25. }
  26. while (fgets(buf, sizeof buf, fd)) {
  27. G_strip(buf);
  28. if (*buf == '#' || *buf == 0)
  29. continue;
  30. if (sscanf(buf, "%s %[^\n]", label, temp) == 2) {
  31. uppercase(label);
  32. if (strcmp(label, "TITLE") == 0) {
  33. G_strip(temp);
  34. strcpy(title, temp);
  35. continue;
  36. }
  37. }
  38. uppercase(buf);
  39. if (sscanf(buf, "MATRIX %d", &n) == 1) {
  40. if (n < 3) {
  41. G_fatal_error(_("Illegal filter matrix size specified"));
  42. }
  43. if (n % 2 == 0) {
  44. G_fatal_error(_("Even filter matrix size specified"));
  45. }
  46. count++;
  47. filter = (FILTER *) G_realloc(filter, count * sizeof(FILTER));
  48. f = &filter[count - 1];
  49. f->size = n;
  50. f->divisor = 1;
  51. f->dmatrix = NULL;
  52. f->type = PARALLEL;
  53. f->start = UL;
  54. have_divisor = 0;
  55. have_type = 0;
  56. have_start = 0;
  57. f->matrix = (DCELL **) G_malloc(n * sizeof(DCELL *));
  58. for (row = 0; row < n; row++)
  59. f->matrix[row] = (DCELL *) G_malloc(n * sizeof(DCELL));
  60. for (row = 0; row < n; row++)
  61. for (col = 0; col < n; col++)
  62. if (fscanf(fd, "%lf", &f->matrix[row][col]) != 1) {
  63. G_fatal_error(_("Illegal filter matrix"));
  64. }
  65. continue;
  66. }
  67. if (sscanf(buf, "DIVISOR %lf", &div) == 1)
  68. if (sscanf(buf, "%s", label) == 1 &&
  69. strcmp(label, "DIVISOR") == 0) {
  70. if (!filter) {
  71. G_fatal_error(_("Filter file format error"));
  72. }
  73. if (have_divisor) {
  74. G_fatal_error(_("Duplicate filter divisor specified"));
  75. }
  76. have_divisor = 1;
  77. if (sscanf(buf, "DIVISOR %lf", &div) == 1) {
  78. f->divisor = div;
  79. if (div == 0)
  80. f->dmatrix = f->matrix;
  81. continue;
  82. }
  83. f->divisor = 0;
  84. f->dmatrix = (DCELL **) G_malloc(f->size * sizeof(DCELL *));
  85. for (row = 0; row < f->size; row++)
  86. f->dmatrix[row] =
  87. (DCELL *) G_malloc(f->size * sizeof(DCELL));
  88. for (row = 0; row < f->size; row++)
  89. for (col = 0; col < f->size; col++)
  90. if (fscanf(fd, "%lf", &f->dmatrix[row][col]) != 1) {
  91. G_fatal_error(_("Illegal divisor matrix"));
  92. }
  93. continue;
  94. }
  95. if (sscanf(buf, "TYPE %s", temp) == 1) {
  96. if (!filter) {
  97. G_fatal_error(_("Filter file format error"));
  98. }
  99. if (have_type) {
  100. G_fatal_error(_("Duplicate filter type specified"));
  101. }
  102. if (strcmp(temp, "P") == 0)
  103. f->type = PARALLEL;
  104. else if (strcmp(temp, "S") == 0)
  105. f->type = SEQUENTIAL;
  106. else {
  107. G_fatal_error(_("Illegal filter type specified"));
  108. }
  109. have_type = 1;
  110. continue;
  111. }
  112. if (sscanf(buf, "START %s", temp) == 1) {
  113. if (!filter) {
  114. G_fatal_error(_("Filter file format error"));
  115. }
  116. if (have_start) {
  117. G_fatal_error(_("Duplicate filter start specified"));
  118. }
  119. if (strcmp(temp, "UL") == 0)
  120. f->start = UL;
  121. /* disable any other starting corner until the rest of
  122. * this program handles them properly
  123. */
  124. else
  125. G_warning(_("Filter start %s ignored, using UL"), temp);
  126. /* disable these others
  127. else if (strcmp (temp, "UR") == 0)
  128. f->start = UR ;
  129. else if (strcmp (temp, "LL") == 0)
  130. f->start = LL ;
  131. else if (strcmp (temp, "LR") == 0)
  132. f->start = LR ;
  133. else
  134. {
  135. ERROR ("illegal filter type specified");
  136. }
  137. */
  138. have_start = 1;
  139. continue;
  140. }
  141. /* other lines are ignored */
  142. }
  143. if (!filter) {
  144. G_fatal_error(_("Illegal filter file format"));
  145. }
  146. *nfilters = count;
  147. return filter;
  148. }