File: hipass.c

package info (click to toggle)
hylafax 1%3A4.1.1-3.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 6,400 kB
  • ctags: 7,270
  • sloc: sh: 15,895; ansic: 12,661; makefile: 1,439; cpp: 850
file content (183 lines) | stat: -rw-r--r-- 4,975 bytes parent folder | download | duplicates (4)
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
/*	$Id: hipass.c,v 1.1.1.1 1998/10/12 20:47:50 root Exp $ */
/*
 * Copyright (c) 1990-1996 Sam Leffler
 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
 * HylaFAX is a trademark of Silicon Graphics
 *
 * Permission to use, copy, modify, distribute, and sell this software and 
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Sam Leffler and Silicon Graphics.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 * OF THIS SOFTWARE.
 */
/*
 *      hipass -
 *              Hi pass filter rows using a 3x3 kernel.
 *
 *                              Paul Haeberli - 1989
 *
 */
#include "stdio.h"
#include "hipass.h"

highpass *newhp(getfunc,xsize,ysize,mag)
int (*getfunc)();
int xsize, ysize;
float mag;
{
    highpass *hp; 
    int i;

    hp = (highpass *)malloc(sizeof(highpass));
    hp->getfunc = getfunc;
    hp->xsize = xsize;
    hp->ysize = ysize;
    hp->y = 0;
    hp->extrapval = 1024*mag;
    for(i=0; i<3; i++)
        hp->blurrows[i] = (short *)malloc(xsize*sizeof(short));
    for(i=0; i<2; i++)
        hp->pastrows[i] = (short *)malloc(xsize*sizeof(short));
    hp->acc = (short *)malloc(xsize*sizeof(short));
    if(xsize<3 || ysize<3 || hp->extrapval == 0) 
        hp->active = 0;
    else {
        hp->active = 1;
    }
    return hp;
}

freehp(hp)
highpass *hp; 
{
    int i;

    for(i=0; i<3; i++)
        free(hp->blurrows[i]);
    for(i=0; i<2; i++)
        free(hp->pastrows[i]);
    free(hp->acc);
    free(hp);
}

/*
 *      extrap -
 *              Extrapolate from the blurred image beyond the original
 *
 */
static extrap(pix,acc,div,n,extrapval)
unsigned short *pix;
short *acc;
int div, n;
int extrapval;
{
    int delta, val;
    int mul, ext;

    mul = div;
    div = div*256;
    ext = extrapval;
    while(n--) {
        delta = (*pix * mul)-*acc++;
        val = *pix+(ext*delta)/div;
        if(val<0)
            val = 0;
        if(val>255)
            val = 255;
        *pix++ = val;
    }
}

/*
 *      xblur -
 *              Blur a row in the x direction
 *
 */
static xblur(sptr,cptr,n)
unsigned short *sptr, *cptr;
int n;
{
    short acc;

    if (n<3) {
        fprintf(stderr,"xblur: n may not be less than 3\n");
        exit(1);
    }
    acc = cptr[0] + cptr[1];
    *sptr++ = (3*acc+1)>>1;
    acc += cptr[2];
    n-=3;
    while(n--) {
        *sptr++ = acc;
        acc -= cptr[0];
        acc += cptr[3];
        cptr++;
    }
    *sptr++ = acc;
    acc -= cptr[0];
    *sptr++ = (3*acc+1)>>1;
}

hpgetrow(hp,buf,y)
highpass *hp;
short *buf;
int y;
{
    short *temp;
    int iy;

    if(y == 0)
        hp->y = 0;
    if(hp->y != y) {
        fprintf(stderr,"hpgetrow: y error\n");
        exit(1);
    }
    if(hp->active) {
        if (y == 0) {
            memset(hp->acc,0,hp->xsize*sizeof(short));
            for(iy=0; iy<2; iy++) {
                temp = hp->pastrows[0];
                hp->pastrows[0] = hp->pastrows[1];
                hp->pastrows[1] = temp;
                hp->getfunc(hp->pastrows[1],iy);
                xblur(hp->blurrows[iy],hp->pastrows[1],hp->xsize);
                addsrow(hp->acc,hp->blurrows[iy],hp->xsize);
            }
            copyrow(hp->pastrows[0],buf,hp->xsize);
            extrap(buf,hp->acc,6,hp->xsize,hp->extrapval);
        } else if (y == hp->ysize-1) {
            copyrow(hp->pastrows[1],buf,hp->xsize);
            extrap(buf,hp->acc,6,hp->xsize,hp->extrapval);
        } else {
            temp = hp->pastrows[0];
            hp->pastrows[0] = hp->pastrows[1];
            hp->pastrows[1] = temp;
            hp->getfunc(hp->pastrows[1],y+1);
            xblur(hp->blurrows[2],hp->pastrows[1],hp->xsize);
            addsrow(hp->acc,hp->blurrows[2],hp->xsize);
            copyrow(hp->pastrows[0],buf,hp->xsize);
            extrap(buf,hp->acc,9,hp->xsize,hp->extrapval);
            subsrow(hp->acc,hp->blurrows[0],hp->xsize);
            temp = hp->blurrows[0];
            hp->blurrows[0] = hp->blurrows[1];
            hp->blurrows[1] = hp->blurrows[2];
            hp->blurrows[2] = temp;
        }
    } else {
        hp->getfunc(buf,y);
    }
    hp->y++;
}