~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to raster/r.mfilter.fp/getfilt.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

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