about summary refs log tree commit diff
path: root/editor/specialty/pgmmorphconv.c
blob: 2ba2d62d886ea1abd2713c495b1d4304ec19042c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* pgmmorphconv.c - morphological convolutions on a graymap: dilation and 
** erosion
**
** Copyright (C) 2000 by Luuk van Dijk/Mind over Matter
**
** Based on 
** pnmconvol.c - general MxN convolution on a portable anymap
**
** Copyright (C) 1989, 1991 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation.  This software is provided "as is" without express or
** implied warranty.
*/

#include "pm_c_util.h"
#include "shhopt.h"
#include "mallocvar.h"
#include "pgm.h"



enum Operation { ERODE, DILATE, OPEN, CLOSE, GRADIENT };



struct CmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const char * inputFileName;  /* File name of input file */
    const char * templateFileName;  /* File name of template file */

    enum Operation operation;
};



static void
parseCommandLine(int argc, const char ** const argv,
                 struct CmdlineInfo * const cmdlineP) {
/*----------------------------------------------------------------------------
   Note that the file spec array we return is stored in the storage that
   was passed to us as the argv array.
-----------------------------------------------------------------------------*/
    optEntry * option_def;
        /* Instructions to OptParseOptions3 on how to parse our options.
         */
    optStruct3 opt;
    unsigned int option_def_index;
    unsigned int erode, dilate, open, close, gradient;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENT3 */
    OPTENT3(0,   "erode",        OPT_FLAG,   NULL, &erode,           0);
    OPTENT3(0,   "dilate",       OPT_FLAG,   NULL, &dilate,          0);
    OPTENT3(0,   "open",         OPT_FLAG,   NULL, &open,            0);
    OPTENT3(0,   "close",        OPT_FLAG,   NULL, &close,           0);
    OPTENT3(0,   "gradient",     OPT_FLAG,   NULL, &gradient,        0);

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = TRUE;  /* We may have parms that are negative numbers */

    pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
        /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (erode + dilate + open + close + gradient > 1)
        pm_error("You may specify at most one of -erode, -dilate, "
                 "-open, -close, or -gradient");

    if (erode)
        cmdlineP->operation = ERODE;
    else if (dilate)
        cmdlineP->operation = DILATE;
    else if (open)
        cmdlineP->operation = OPEN;
    else if (close)
        cmdlineP->operation = CLOSE;
    else if (gradient)
        cmdlineP->operation = GRADIENT;
    else
        cmdlineP->operation = DILATE;

    if (argc-1 < 1)
        pm_error("You must specify the template file name as an argument");
    else {
        cmdlineP->templateFileName = argv[1];

        if (argc-1 < 2)
            cmdlineP->inputFileName = "-";
        else {
            cmdlineP->inputFileName = argv[2];
            
            if (argc-1 > 2)
                pm_error("Too many arguments: %u.  "
                         "The only possible arguments "
                         "are the template file name and the input file name",
                         argc-1);
        }
    }
}



static void
readTemplateMatrix(const char *   const fileName,
                   bit ***        const templateP,
                   unsigned int * const rowsP,
                   unsigned int * const colsP) {
/*----------------------------------------------------------------------------
  Read in the template matrix.
-----------------------------------------------------------------------------*/
    FILE * templateFileP;
    int cols, rows;

    templateFileP = pm_openr(fileName);

    *templateP = pbm_readpbm(templateFileP, &cols, &rows);

    pm_close(templateFileP);

    if (cols % 2 != 1 || rows % 2 != 1)
        pm_error("the template matrix must have an odd number of "
                 "rows and columns" );

        /* the reason is that we want the middle pixel to be the origin */
    *rowsP = rows;
    *colsP = cols;
}



static void
setAllPixel(gray **      const image,
            unsigned int const rows,
            unsigned int const cols,
            gray         const value) {

    unsigned int col;

    for (col = 0; col < cols; ++col) {
        unsigned int row;
        for (row = 0; row < rows; ++row)
            image[row][col] = value; 
    }
}



static void
dilate(bit **         const template,
       int            const trowso2,
       int            const tcolso2, 
       gray **        const inImage,
       gray **        const outImage, 
       unsigned int   const rows,
       unsigned int   const cols,
       unsigned int * const templateCountP) {

    unsigned int templateCount;
    int tr;

    setAllPixel(outImage, rows, cols, 0);

    /* for each non-black pixel of the template add in to out */

    for (tr = -trowso2, templateCount = 0; tr <= trowso2; ++tr) {
        int tc;
        for (tc = -tcolso2; tc <= tcolso2; ++tc) {
            int r;
            if (template[trowso2+tr][tcolso2+tc] != PBM_BLACK) {
                ++templateCount;

                for (r = ((tr > 0) ? 0 : -tr);
                     r < ((tr > 0) ? (rows-tr) : rows);
                     ++r) {
                    int c;
                    for (c = ((tc > 0) ? 0 : -tc);
                         c < ((tc > 0) ? (cols-tc) : cols);
                         ++c) {
                        gray const source = inImage[r+tr][c+tc];
                        outImage[r][c] = MAX(source, outImage[r][c]);
                    }
                }
            }
        }
    }
    *templateCountP = templateCount;
}



static void
erode(bit **         const template,
      int            const trowso2,
      int            const tcolso2, 
      gray **        const inImage,
      gray **        const outImage, 
      unsigned int   const rows,
      unsigned int   const cols,
      unsigned int * const templateCountP) {

    unsigned int templateCount;
    int tr;

    setAllPixel(outImage, rows, cols, PGM_MAXMAXVAL);

    /* For each non-black pixel of the template add in to out */

    for (tr = -trowso2, templateCount = 0; tr <= trowso2; ++tr) {
        int tc;
        for (tc = -tcolso2; tc <= tcolso2; ++tc) {
            if (template[trowso2+tr][tcolso2+tc] != PBM_BLACK) {
                int r;
                ++templateCount;

                for (r = ((tr > 0) ? 0 : -tr);
                     r < ((tr > 0) ? (rows-tr) : rows);
                     ++r){
                    int c;

                    for (c = ((tc > 0) ? 0 : -tc);
                         c < ((tc > 0) ? (cols-tc) : cols);
                         ++c) {
                        
                        gray const source = inImage[r+tr][c+tc];
                        outImage[r][c] = MIN(source, outImage[r][c]);
      
                    }
                }
            }
        }
    }
    *templateCountP = templateCount;
}



static void
openMorph(bit **         const template,
          int            const trowso2,
          int            const tcolso2, 
          gray **        const inputImage,
          gray **        const outputImage, 
          unsigned int   const rows,
          unsigned int   const cols,
          unsigned int * const templateCountP) {

    gray ** erodedImage;
    unsigned int erodedTemplateCount;

    erodedImage = pgm_allocarray(cols, rows);
    
    erode(template, trowso2, tcolso2, 
          inputImage, erodedImage, rows, cols, &erodedTemplateCount);

    dilate(template, trowso2, tcolso2, 
           erodedImage, outputImage, rows, cols, templateCountP);

    pgm_freearray(erodedImage, rows);
}



static void
closeMorph(bit **         const template,
           int            const trowso2,
           int            const tcolso2, 
           gray **        const inputImage,
           gray **        const outputImage, 
           unsigned int   const rows,
           unsigned int   const cols,
           unsigned int * const templateCountP) {

    gray ** dilatedImage;
    unsigned int dilatedTemplateCount;

    dilatedImage = pgm_allocarray(cols, rows);

    dilate(template, trowso2, tcolso2, 
           inputImage, dilatedImage, rows, cols, &dilatedTemplateCount);

    erode(template, trowso2, tcolso2, 
          dilatedImage, outputImage, rows, cols, templateCountP);

    pgm_freearray(dilatedImage, rows);
}



static void
subtract(gray **      const subtrahendImage,
         gray **      const subtractorImage,
         gray **      const outImage, 
         unsigned int const rows,
         unsigned int const cols ) {

    /* (I call the minuend the subtrahend and the subtrahend the subtractor,
       to be consistent with other arithmetic terminology).
    */

    unsigned int c;

    for (c = 0; c < cols; ++c) {
        unsigned int r;
        for (r = 0; r < rows; ++r)
            outImage[r][c] = subtrahendImage[r][c] - subtractorImage[r][c];
    }
}



static void
gradient(bit **         const template,
         int            const trowso2,
         int            const tcolso2, 
         gray **        const inputImage,
         gray **        const outputImage, 
         unsigned int   const rows,
         unsigned int   const cols,
         unsigned int * const templateCountP) {

    gray ** dilatedImage;
    gray ** erodedImage;
    unsigned int dilatedTemplateCount;
    
    dilatedImage = pgm_allocarray(cols, rows);
    erodedImage = pgm_allocarray(cols, rows);

    dilate(template, trowso2, tcolso2, 
           inputImage, dilatedImage, rows, cols, &dilatedTemplateCount);

    erode(template, trowso2, tcolso2, 
          inputImage, erodedImage, rows, cols, templateCountP);

    subtract(dilatedImage, erodedImage, outputImage, rows, cols);

    pgm_freearray(erodedImage, rows );
    pgm_freearray(dilatedImage, rows );
}



int
main(int argc, const char ** argv) {

    struct CmdlineInfo cmdline;
    FILE * ifP;
    bit ** template;
    unsigned int templateCols, templateRows;
    int cols, rows;
    gray maxval;
    gray ** inputImage;
    gray ** outputImage;
    unsigned int templateCount;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    ifP = pm_openr(cmdline.inputFileName);

    readTemplateMatrix(cmdline.templateFileName,
                       &template, &templateRows, &templateCols);

    /* Template coords run from -templateCols/2 .. 0 .. + templateCols/2 */
  
    inputImage = pgm_readpgm(ifP, &cols, &rows, &maxval);

    if (cols < templateCols || rows < templateRows)
        pm_error("the image is smaller than the convolution matrix" );
  
    outputImage = pgm_allocarray(cols, rows);
  
    switch (cmdline.operation) {
    case DILATE:
        dilate(template, templateRows/2, templateCols/2,
               inputImage, outputImage, rows, cols,
               &templateCount);
        break;
    case ERODE:
        erode(template, templateRows/2, templateCols/2,
              inputImage, outputImage, rows, cols,
              &templateCount);
        break;
    case OPEN:
        openMorph(template, templateRows/2, templateCols/2,
              inputImage, outputImage, rows, cols,
              &templateCount);
        break;
    case CLOSE:
        closeMorph(template, templateRows/2, templateCols/2,
                   inputImage, outputImage, rows, cols,
                   &templateCount);
        break;
    case GRADIENT:
        gradient(template, templateRows/2, templateCols/2,
                 inputImage, outputImage, rows, cols,
                 &templateCount);
        break;
    }

    if (templateCount == 0)
        pm_error( "The template was empty!" );

    pgm_writepgm(stdout, outputImage, cols, rows, maxval, 0);

    pgm_freearray(outputImage, rows);
    pgm_freearray(inputImage, rows);
    pm_close(ifP);

    return 0;
}