about summary refs log tree commit diff
path: root/editor/pamoil.c
blob: 6cb8d3aca2d7e20d7631dc576c9542f98098729b (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
/* pgmoil.c - read a portable pixmap and turn into an oil painting
**
** Copyright (C) 1990 by Wilson Bent (whb@hoh-2.att.com)
** Shamelessly butchered into a color version by Chris Sheppard
** 2001
**
** 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "pam.h"
#include "mallocvar.h"

static void 
convertRow(struct pam const inpam, tuple ** const tuples,
           tuple * const tuplerow, int const row, int const smearFactor,
           int * const hist) {

    int sample;
    for (sample = 0; sample < inpam.depth; sample++) {
        int col;
        for (col = 0; col < inpam.width; ++col)  {
            int i;
            int drow;
            int modalval;
                /* The sample value that occurs most often in the neighborhood
                   of the pixel being examined
                */

            /* Compute hist[] - frequencies, in the neighborhood, of each 
               sample value
            */
            for (i = 0; i <= inpam.maxval; ++i) hist[i] = 0;

            for (drow = row - smearFactor; drow <= row + smearFactor; ++drow) {
                if (drow >= 0 && drow < inpam.height) {
                    int dcol;
                    for (dcol = col - smearFactor; 
                         dcol <= col + smearFactor; 
                         ++dcol) {
                        if ( dcol >= 0 && dcol < inpam.width )
                            ++hist[tuples[drow][dcol][sample]];
                    }
                }
            }
            {
                /* Compute modalval */
                int sampleval;
                int maxfreq;

                maxfreq = 0;
                modalval = 0;

                for (sampleval = 0; sampleval <= inpam.maxval; ++sampleval) {
                    if (hist[sampleval] > maxfreq) {
                        maxfreq = hist[sampleval];
                        modalval = sampleval;
                    }
                }
            }
            tuplerow[col][sample] = modalval;
        }
    }
}



int
main(int argc, char *argv[] ) {
    struct pam inpam, outpam;
    FILE* ifp;
    tuple ** tuples;
    tuple * tuplerow;
    int * hist;
        /* A buffer for the convertRow subroutine to use */
    int argn;
    int row;
    int smearFactor;
    const char* const usage = "[-n <n>] [ppmfile]";

    ppm_init( &argc, argv );

    argn = 1;
    smearFactor = 3;       /* DEFAULT VALUE */

    /* Check for options. */
    if ( argn < argc && argv[argn][0] == '-' ) {
        if ( argv[argn][1] == 'n' ) {
            ++argn;
            if ( argn == argc || sscanf(argv[argn], "%d", &smearFactor) != 1 )
                pm_usage( usage );
        } else
            pm_usage( usage );
        ++argn;
    }
    if ( argn < argc ) {
        ifp = pm_openr( argv[argn] );
        ++argn;
    } else
        ifp = stdin;

    if ( argn != argc )
        pm_usage( usage );

    tuples = pnm_readpam(ifp, &inpam, PAM_STRUCT_SIZE(tuple_type));
    pm_close(ifp);

    MALLOCARRAY(hist, inpam.maxval + 1);
    if (hist == NULL)
        pm_error("Unable to allocate memory for histogram.");

    outpam = inpam; outpam.file = stdout;

    pnm_writepaminit(&outpam);

    tuplerow = pnm_allocpamrow(&inpam);

    for (row = 0; row < inpam.height; ++row) {
        convertRow(inpam, tuples, tuplerow, row, smearFactor, hist);
        pnm_writepamrow(&outpam, tuplerow);
    }

    pnm_freepamrow(tuplerow);
    free(hist);
    pnm_freepamarray(tuples, &inpam);

    pm_close(stdout);
    exit(0);
}