about summary refs log tree commit diff
path: root/editor/pnmalias.c
blob: 51a713128fcc9827b8ca10eb0f38a601235a4eb2 (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
/* pnmmalias.c - antialias a portable anymap.
**
** 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 "pnm.h"

int
main(int argc, char * argv[] ) {
    FILE* ifp;
    xel* xelrow[3];
    xel* newxelrow;
    pixel bgcolorppm, fgcolorppm;
    register xel* xpP;
    register xel* xP;
    register xel* xnP;
    register xel* nxP;
    xel bgcolor, fgcolor;
    int argn, rows, cols, format, newformat, bgonly, fgonly;
    int bgalias, fgalias;
    int row;
    double fmask[9], weight;
    xelval maxval;
    xelval newmaxval;
    const char* const usage = "[-bgcolor <color>] [-fgcolor <color>] [-bonly] [-fonly] [-balias] [-falias] [-weight <w>] [pnmfile]";

    pnm_init( &argc, argv );

    bgonly = fgonly = 0;
    bgalias = fgalias = 0;
    weight = 1./3.;
    argn = 1;
    PPM_ASSIGN( bgcolorppm, 0, 0, 0);
    PPM_ASSIGN( fgcolorppm, 0, 0, 0);

    while ( argn < argc && argv[argn][0] == '-' )
        {
        if ( pm_keymatch( argv[argn], "-fgcolor", 3 ) )
        {
        if ( ++argn >= argc )
        pm_usage( usage );
        else
        fgcolorppm = ppm_parsecolor( argv[argn], PPM_MAXMAXVAL );
        }
        else if ( pm_keymatch( argv[argn], "-bgcolor", 3 ) )
        {
        if ( ++argn >= argc )
        pm_usage( usage );
        else
        bgcolorppm = ppm_parsecolor( argv[argn], PPM_MAXMAXVAL );
        }
        else if ( pm_keymatch( argv[argn], "-weight", 2 ) )
        {
        if ( ++argn >= argc )
        pm_usage( usage );
        else if ( sscanf( argv[argn], "%lf", &weight ) != 1 )
            pm_usage( usage );
        else if ( weight >= 1. || weight <= 0. )
        {
        pm_message( "weight factor w must be 0.0 < w < 1.0" );
        pm_usage( usage );
        }
        }
    else if ( pm_keymatch( argv[argn], "-bonly", 3 ) )
        bgonly = 1;
    else if ( pm_keymatch( argv[argn], "-fonly", 3 ) )
        fgonly = 1;
    else if ( pm_keymatch( argv[argn], "-balias", 3 ) )
        bgalias = 1;
    else if ( pm_keymatch( argv[argn], "-falias", 3 ) )
        fgalias = 1;
    else if ( pm_keymatch( argv[argn], "-bfalias", 3 ) )
        bgalias = fgalias = 0;
    else if ( pm_keymatch( argv[argn], "-fbalias", 3 ) )
        bgalias = fgalias = 0;
        else
            pm_usage( usage );
        ++argn;
        }

    if ( argn != argc )
    {
    ifp = pm_openr( argv[argn] );
    ++argn;
    }
    else
    ifp = stdin;

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

    /* normalize mask elements */
    fmask[4] = weight;
    fmask[0] = fmask[1] = fmask[2] = fmask[3] = ( 1.0 - weight ) / 8.0;
    fmask[5] = fmask[6] = fmask[7] = fmask[8] = ( 1.0 - weight ) / 8.0;

    pnm_readpnminit( ifp, &cols, &rows, &maxval, &format );

    xelrow[0] = pnm_allocrow( cols );
    xelrow[1] = pnm_allocrow( cols );
    xelrow[2] = pnm_allocrow( cols );
    newxelrow = pnm_allocrow( cols );

    /* Promote PBM files to PGM. */
    if ( PNM_FORMAT_TYPE(format) == PBM_TYPE ) {
        newformat = PGM_TYPE;
        newmaxval = PGM_MAXMAXVAL;
        pm_message( "promoting from PBM to PGM" );
    } else {
        newformat = format;
        newmaxval = maxval;
    }

    /* Figure out foreground pixel value if none was given */
    if (PPM_GETR(fgcolorppm) == 0 && PPM_GETG(fgcolorppm) == 0 &&
        PPM_GETB(fgcolorppm) == 0 ) {
        if ( PNM_FORMAT_TYPE(newformat) == PGM_TYPE )
            PNM_ASSIGN1( fgcolor, newmaxval );
        else
            PPM_ASSIGN( fgcolor, newmaxval, newmaxval, newmaxval );
    } else {
        if ( PNM_FORMAT_TYPE(newformat) == PGM_TYPE )
            PNM_ASSIGN1( fgcolor, PPM_GETR( fgcolorppm ) );
        else
            fgcolor = fgcolorppm;
    }

    if (PPM_GETR(bgcolorppm) != 0 || PPM_GETG(bgcolorppm) != 0 ||
        PPM_GETB(bgcolorppm) != 0 ) {
        if ( PNM_FORMAT_TYPE(newformat) == PGM_TYPE )
            PNM_ASSIGN1( bgcolor, PPM_GETR( bgcolorppm) );
        else
            bgcolor = bgcolorppm;
    } else {
        if ( PNM_FORMAT_TYPE(newformat) == PGM_TYPE )
            PNM_ASSIGN1( bgcolor, 0 );
        else
            PPM_ASSIGN( bgcolor, 0, 0, 0 );
    }


    pnm_readpnmrow( ifp, xelrow[0], cols, newmaxval, format );
    pnm_readpnmrow( ifp, xelrow[1], cols, newmaxval, format );
    pnm_writepnminit( stdout, cols, rows, newmaxval, newformat, 0 );
    pnm_writepnmrow( stdout, xelrow[0], cols, newmaxval, newformat, 0 );

    for ( row = 1; row < rows - 1; ++row ) {
        int col;
        int value, valuer, valueg, valueb;

        pnm_readpnmrow( ifp, xelrow[(row+1)%3], cols, newmaxval, format );
        newxelrow[0] = xelrow[row%3][0];

        for ( col = 1, xpP = (xelrow[(row-1)%3] + 1), xP = (xelrow[row%3] + 1),
                  xnP = (xelrow[(row+1)%3] + 1), nxP = (newxelrow+1);
              col < cols - 1; ++col, ++xpP, ++xP, ++xnP, ++nxP ) {

            int fgflag, bgflag;

            /* Reset flags if anti-aliasing is to be done on foreground
             * or background pixels only */
            if ( ( bgonly && PNM_EQUAL( *xP, fgcolor ) ) ||
                 ( fgonly && PNM_EQUAL( *xP, bgcolor ) ) )
                bgflag = fgflag = 0;
            else {
                /* Do anti-aliasing here: see if pixel is at the border of a
                 * background or foreground stepwise side */
                bgflag =
                    (PNM_EQUAL(*xpP,bgcolor) && PNM_EQUAL(*(xP+1),bgcolor)) ||
                    (PNM_EQUAL(*(xP+1),bgcolor) && PNM_EQUAL(*xnP,bgcolor)) ||
                    (PNM_EQUAL(*xnP,bgcolor) && PNM_EQUAL(*(xP-1),bgcolor)) ||
                    (PNM_EQUAL(*(xP-1),bgcolor) && PNM_EQUAL(*xpP,bgcolor));
                fgflag =
                    (PNM_EQUAL(*xpP,fgcolor) && PNM_EQUAL(*(xP+1),fgcolor)) ||
                    (PNM_EQUAL(*(xP+1),fgcolor) && PNM_EQUAL(*xnP,fgcolor)) ||
                    (PNM_EQUAL(*xnP,fgcolor) && PNM_EQUAL(*(xP-1),fgcolor)) ||
                    (PNM_EQUAL(*(xP-1),fgcolor) && PNM_EQUAL(*xpP,fgcolor));
            }
            if ( ( bgflag && bgalias ) || ( fgflag && fgalias ) ||
                 ( bgflag && fgflag ) )
                switch( PNM_FORMAT_TYPE( newformat ) ) {
                case PGM_TYPE:
                    value = PNM_GET1(*(xpP-1)) * fmask[0] +
                        PNM_GET1(*(xpP  )) * fmask[1] +
                        PNM_GET1(*(xpP+1)) * fmask[2] +
                        PNM_GET1(*(xP -1)) * fmask[3] +
                        PNM_GET1(*(xP   )) * fmask[4] +
                        PNM_GET1(*(xP +1)) * fmask[5] +
                        PNM_GET1(*(xnP-1)) * fmask[6] +
                        PNM_GET1(*(xnP  )) * fmask[7] +
                        PNM_GET1(*(xnP+1)) * fmask[8] +
                        0.5;
                    PNM_ASSIGN1( *nxP, value );
                    break;
                default:
                    valuer= PPM_GETR(*(xpP-1)) * fmask[0] +
                        PPM_GETR(*(xpP  )) * fmask[1] +
                        PPM_GETR(*(xpP+1)) * fmask[2] +
                        PPM_GETR(*(xP -1)) * fmask[3] +
                        PPM_GETR(*(xP   )) * fmask[4] +
                        PPM_GETR(*(xP +1)) * fmask[5] +
                        PPM_GETR(*(xnP-1)) * fmask[6] +
                        PPM_GETR(*(xnP  )) * fmask[7] +
                        PPM_GETR(*(xnP+1)) * fmask[8] +
                        0.5;
                    valueg= PPM_GETG(*(xpP-1)) * fmask[0] +
                        PPM_GETG(*(xpP  )) * fmask[1] +
                        PPM_GETG(*(xpP+1)) * fmask[2] +
                        PPM_GETG(*(xP -1)) * fmask[3] +
                        PPM_GETG(*(xP   )) * fmask[4] +
                        PPM_GETG(*(xP +1)) * fmask[5] +
                        PPM_GETG(*(xnP-1)) * fmask[6] +
                        PPM_GETG(*(xnP  )) * fmask[7] +
                        PPM_GETG(*(xnP+1)) * fmask[8] +
                        0.5;
                    valueb= PPM_GETB(*(xpP-1)) * fmask[0] +
                        PPM_GETB(*(xpP  )) * fmask[1] +
                        PPM_GETB(*(xpP+1)) * fmask[2] +
                        PPM_GETB(*(xP -1)) * fmask[3] +
                        PPM_GETB(*(xP   )) * fmask[4] +
                        PPM_GETB(*(xP +1)) * fmask[5] +
                        PPM_GETB(*(xnP-1)) * fmask[6] +
                        PPM_GETB(*(xnP  )) * fmask[7] +
                        PPM_GETB(*(xnP+1)) * fmask[8] +
                        0.5;
                    PPM_ASSIGN( *nxP, valuer, valueg, valueb );
                    break;
                }
            else
                *nxP = *xP;
        }

        newxelrow[cols-1] = xelrow[row%3][cols-1];
        pnm_writepnmrow( stdout, newxelrow, cols, newmaxval, newformat, 0 );
    }

    pnm_writepnmrow( stdout, xelrow[row%3], cols, newmaxval, newformat, 0 );

    pm_close( ifp );
    exit ( 0 );
}