1
1
#include "sobel.h"
2
2
3
+ #include <stdio.h>
3
4
#include <stdlib.h>
5
+ #include <string.h>
4
6
#include <math.h>
5
7
6
8
#include "macros.h"
@@ -36,25 +38,23 @@ int rgbToGray(byte *rgb, byte **gray, int buffer_size) {
36
38
37
39
void makeOpMem (byte * buffer , int buffer_size , int cindex , byte * op_mem ) {
38
40
int width = sqrt (buffer_size );
39
- int line = cindex - width ;
40
- int offset = -1 ;
41
41
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 ;
44
46
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 ;
50
50
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 ;
52
54
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 ;
58
58
}
59
59
60
60
/*
@@ -81,6 +81,7 @@ void itConv(byte *buffer, int buffer_size, int *op, byte **res) {
81
81
82
82
// Temporary memory for each pixel operation
83
83
byte op_mem [SOBEL_OP_SIZE ];
84
+ memset (op_mem , 0 , SOBEL_OP_SIZE );
84
85
85
86
// Make convolution for every pixel
86
87
for (int i = 0 ; i < buffer_size ; i ++ ) {
0 commit comments