about summary refs log tree commit diff
path: root/generator/pgmkernel.c
blob: b741d596b598cf6c3f6f38635728b750ffbcdb33 (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
/* pgmkernel.c - generate a portable graymap convolution kernel
**
** Creates a Portable Graymap file containing a convolution filter
** with max value = 255 and minimum value > 127 that can be used as a 
** smoothing kernel for pnmconvol.
**
** Copyright (C) 1992 by Alberto Accomazzi, Smithsonian Astrophysical
** Observatory.
**
** 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 <math.h>
#include "pgm.h"
#include "mallocvar.h"

int
main ( argc, argv )
    int argc;
    char *argv[];
{
    register    int i, j;
    int     argn = 1, ixsize, iysize, maxval = 255;
    double  fxsize = 0.0, fysize = 0.0, w = 6.0, kxcenter, kycenter, 
        tmax = 0, *fkernel;
    const char  *usage = "[-weight f] width [height]";

    pgm_init( &argc, argv );

    while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
    {
        if ( pm_keymatch( argv[argn], "-weight", 2 )) {
            if (++argn >= argc)
                pm_usage( usage );
            else if (sscanf(argv[argn], "%lf", &w) != 1)
                pm_usage( usage );
        }
        else
            pm_usage( usage );
        argn++;
    }

    if (argn == argc)
        pm_usage( usage );
    
    if (sscanf(argv[argn], "%lf", &fxsize) != 1) 
        pm_error( "error reading input kernel x size, (%s)\n", argv[argn]);

    ++argn;
    if (argn == argc - 1) {
        if (sscanf(argv[argn], "%lf", &fysize) != 1)
            pm_error( "error reading input kernel y size, (%s)\n", argv[argn]);
    }
    else if (argn == argc)
        fysize = fxsize;
    else
        pm_usage( usage );

    if (fxsize <= 1 || fysize <= 1)
        pm_usage( usage );

    kxcenter = (fxsize - 1) / 2.0;
    kycenter = (fysize - 1) / 2.0;
    ixsize = fxsize + 0.999;
    iysize = fysize + 0.999;
    MALLOCARRAY(fkernel, ixsize * iysize);
    for (i = 0; i < iysize; i++) 
        for (j = 0; j < ixsize; j++) {
            fkernel[i*ixsize+j] = 1.0 / (1.0 + w * sqrt((double)
                                                        (i-kycenter)*(i-kycenter)+
                                                        (j-kxcenter)*(j-kxcenter)));
            if (tmax < fkernel[i*ixsize+j])
                tmax = fkernel[i*ixsize+j];
        }

    /* output PGM header + data (ASCII format only) */
    printf("P2\n%d %d\n%d\n", ixsize, iysize, maxval);
    
    for (i = 0; i < iysize; i++, printf("\n"))
        for (j = 0; j < ixsize; j++)
            printf(" %3d", (int)(maxval * (fkernel[i*ixsize+j] / 
                                           (2*tmax) + 0.5)));
    
    exit(0);
}