Skip to content

Commit 8c0e84c

Browse files
committed
Made a less confusing makeOpMem function
1 parent ec3f6f0 commit 8c0e84c

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

Makefile

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ check:
66
cd tests && make check
77

88
run:
9-
echo "TODO"
9+
convert imgs/img.png img.rgb
10+
./src/sobel img.rgb img_out.gray -i img_out_h.gray img_out_v.gray
11+
convert -size 512x512 -depth 8 img_out.gray img_out.png
12+
convert -size 512x512 -depth 8 img_out_h.gray img_out_h.png
13+
convert -size 512x512 -depth 8 img_out_v.gray img_out_v.png
14+
1015

1116
clean:
1217
cd src && make clean

src/sobel.c

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "sobel.h"
22

3+
#include <stdio.h>
34
#include <stdlib.h>
5+
#include <string.h>
46
#include <math.h>
57

68
#include "macros.h"
@@ -36,25 +38,23 @@ int rgbToGray(byte *rgb, byte **gray, int buffer_size) {
3638

3739
void makeOpMem(byte *buffer, int buffer_size, int cindex, byte *op_mem) {
3840
int width = sqrt(buffer_size);
39-
int line = cindex-width;
40-
int offset = -1;
4141

42-
for(int i=0; i<SOBEL_OP_SIZE; i++) {
43-
int index = line + offset;
42+
int bottom = cindex-width < 0;
43+
int top = cindex+width >= buffer_size;
44+
int left = cindex % width == 0;
45+
int right = (cindex+1) % width == 0;
4446

45-
if(index >= 0 && index < buffer_size) {
46-
op_mem[i] = buffer[index];
47-
} else {
48-
op_mem[i] = 0;
49-
}
47+
op_mem[0] = !bottom && !left ? buffer[cindex-width-1] : 0;
48+
op_mem[1] = !bottom ? buffer[cindex-width] : 0;
49+
op_mem[2] = !bottom && !right ? buffer[cindex-width+1] : 0;
5050

51-
offset++;
51+
op_mem[3] = !left ? buffer[cindex-1] : 0;
52+
op_mem[4] = buffer[cindex];
53+
op_mem[5] = !right ? buffer[cindex+1] : 0;
5254

53-
if((i+1) % 3 == 0) {
54-
line += width;
55-
offset = -1;
56-
}
57-
}
55+
op_mem[6] = !top && !left ? buffer[cindex+width-1] : 0;
56+
op_mem[7] = !top ? buffer[cindex+width] : 0;
57+
op_mem[8] = !top && !right ? buffer[cindex+width+1] : 0;
5858
}
5959

6060
/*
@@ -81,6 +81,7 @@ void itConv(byte *buffer, int buffer_size, int *op, byte **res) {
8181

8282
// Temporary memory for each pixel operation
8383
byte op_mem[SOBEL_OP_SIZE];
84+
memset(op_mem, 0, SOBEL_OP_SIZE);
8485

8586
// Make convolution for every pixel
8687
for(int i=0; i<buffer_size; i++) {

0 commit comments

Comments
 (0)