Skip to content

Commit 3a3a004

Browse files
committed
Updated example L7 to reflect change in PNG
1 parent db9d0b1 commit 3a3a004

1 file changed

Lines changed: 33 additions & 36 deletions

File tree

L7_case-study_image-filter/image_filter.sac

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ use String: {string};
55

66
/*
77
* ========================================================================
8-
* Helper functions
8+
* Helper functions
99
*/
1010
#ifndef RAWIMAGE
1111
use Color8: {newColor};
12-
use PNG: {drawArray};
12+
use PNG: {writePNG};
1313
#endif
1414

1515
#ifdef SIMPLECLOCK
@@ -67,7 +67,7 @@ void write( string fn, int[.,.] m)
6767
{
6868
m = transpose( m);
6969
c = { i -> newColor( m[i], m[i], m[i])};
70-
unused = drawArray( fn, c);
70+
unused = writePNG( fn, c);
7171
}
7272
#endif /*RAWIMAGE*/
7373

@@ -76,13 +76,13 @@ int[.,.] readImage( string fn)
7676
{
7777
if( fn String::== "") {
7878
res = FibreScanIntArray( stdin);
79-
}
79+
}
8080
else {
8181
err, f = fopen( fn, "r");
8282
if( SysErr::fail( err)) {
8383
printf("\nFailed to open \"%s\", expecting data on stdin.\n", fn);
8484
res = FibreScanIntArray( stdin);
85-
}
85+
}
8686
else {
8787
res = FibreScanIntArray( f);
8888
fclose( f);
@@ -110,18 +110,18 @@ inline
110110
int[*] thrs( int[*] vals, int max_val)
111111
{
112112
res = with{ ( . <= iv <= . ) : thrs( vals[iv], max_val);
113-
} : genarray( shape( vals), 0);
113+
} : genarray( shape( vals), 0);
114114

115115
return( res);
116116
}
117117

118118
/*
119-
* Convolution with given mask
119+
* Convolution with given mask
120120
*/
121121
inline
122122
int[.,.] apply( int[.,.] kernel, int[.,.] img)
123123
{
124-
/* Generate offset matrix for given kernel
124+
/* Generate offset matrix for given kernel
125125
* for example, offsets for a 3x3 kernel are as follows:
126126
*
127127
* [-1,-1] | [0, -1] | [1, -1]
@@ -132,20 +132,20 @@ int[.,.] apply( int[.,.] kernel, int[.,.] img)
132132
*/
133133
offs = with { (. <= iv <= .) : iv - (shape( kernel)[0]/2);
134134
} : genarray( shape( kernel), 0*shape( kernel));
135-
135+
136136
/* Add boundary large enough for given kernel */
137137
b_size = shape(kernel)[0]/2;
138-
framed = with{
139-
( 0*shape( img) + b_size <= iv < shape( img)+b_size) :
138+
framed = with{
139+
( 0*shape( img) + b_size <= iv < shape( img)+b_size) :
140140
img[iv - b_size];
141-
} : genarray( shape( img) + 2*b_size, 0);
141+
} : genarray( shape( img) + 2*b_size, 0);
142142

143143

144144
/* apply kernel to framed image */
145-
res = with { (0*shape(framed)+b_size <= iv < shape( framed)-b_size) :
146-
abs(
147-
sum(
148-
with { ( . <= iiv <= . ) :
145+
res = with { (0*shape(framed)+b_size <= iv < shape( framed)-b_size) :
146+
abs(
147+
sum(
148+
with { ( . <= iiv <= . ) :
149149
kernel[iiv] * framed[iv + offs[iiv]];
150150
} : genarray( shape( kernel), 0)
151151
)
@@ -158,15 +158,15 @@ int[.,.] apply( int[.,.] kernel, int[.,.] img)
158158

159159

160160

161-
int[.,.] sobelEdges( int[.,.] img)
161+
int[.,.] sobelEdges( int[.,.] img)
162162
{
163163
SY = [
164164
[1, 2, 1],
165165
[0, 0, 0],
166166
[-1, -2, -1]
167167
];
168168
SX = transpose( SY);
169-
169+
170170
resX = apply( SX, img);
171171
resY = apply( SY, img);
172172

@@ -176,19 +176,19 @@ specialize int[.,.] sobelEdges( int[4288,2848] img);
176176

177177

178178

179-
int[.,.] gaussBlur( int[.,.] img)
179+
int[.,.] gaussBlur( int[.,.] img)
180180
{
181-
GB = [
181+
GB = [
182182
[ 1, 2, 1],
183183
[ 2, 3, 2],
184184
[ 1, 2, 1]
185185
];
186186

187-
return( apply( GB, img) / sum( GB));
187+
return( apply( GB, img) / sum( GB));
188188
}
189-
specialize int[.,.] gaussBlur( int[4288,2848] img);
189+
specialize int[.,.] gaussBlur( int[4288,2848] img);
190190

191-
int[.,.] gaussBlur25( int[.,.] img)
191+
int[.,.] gaussBlur25( int[.,.] img)
192192
{
193193
GB25 = [
194194
[ 1, 4, 7, 4, 1],
@@ -197,15 +197,15 @@ int[.,.] gaussBlur25( int[.,.] img)
197197
[ 4, 20, 33, 20, 4],
198198
[ 1, 4, 7, 4, 1]
199199
];
200-
return( apply( GB25, img) / sum( GB25));
200+
return( apply( GB25, img) / sum( GB25));
201201
}
202202

203203

204204
int main()
205205
{
206206
/*
207207
* ========================================================================
208-
* Handling of Command Line Arguments
208+
* Handling of Command Line Arguments
209209
*/
210210
itcount = 1;
211211
inputfile = "";
@@ -218,11 +218,11 @@ int main()
218218
"\n <input> : name of input file in Fibre format (default: stdin)"
219219
"\n <output> : name of output file (default: result[.png,.dat])"
220220
"\n\n", argv( 0));
221-
221+
222222
for( i=1; i<argc()-1; i++) {
223223
if( argv( i) String::== "-c") {
224-
itcount = String::toi( argv( i+1));
225-
}
224+
itcount = String::toi( argv( i+1));
225+
}
226226
if( argv( i) String::== "-o") {
227227
outputfile = argv( i+1);
228228
}
@@ -238,8 +238,8 @@ int main()
238238
printf( "\n---------------------------------------------------------------"
239239
"\nIterations: %d, Input: \"%s\", Output: \"%s\""
240240
"\n---------------------------------------------------------------\n",
241-
itcount,
242-
(inputfile String::== "") ? "stdin" : inputfile,
241+
itcount,
242+
(inputfile String::== "") ? "stdin" : inputfile,
243243
outputfile);
244244
/*
245245
* END OF Handling of Command Line Arguments
@@ -250,7 +250,7 @@ int main()
250250
fprintf( stderr, "\nReading image ...\n");
251251

252252
img = readImage( inputfile);
253-
253+
254254
fprintf( stderr, "\nApplying filter %d time(s)...", itcount);
255255
img, start = timestamp( img);
256256
for( i=0; i<itcount; i++) {
@@ -264,12 +264,9 @@ int main()
264264
}
265265
img, end = timestamp( img);
266266
fprintf( stderr, " finished after %fs\n", difftime( end, start));
267-
267+
268268
fprintf( stderr, "\nWriting result image ...\n");
269-
269+
270270
write( outputfile, img);
271271
return( 0);
272272
}
273-
274-
275-

0 commit comments

Comments
 (0)