In a pattern of single alphabet in an image I've written a small function which will start from any pixel and traverse all the contiguous pixels. In short in a matrix it will turn on all the contiguous pixels to true and rest will be zero.
This function has done it correctly. However with little change in input pattern it is behaving abnormally. I'm finding that this line onwards: //process left-up diagonally
is not getting called.
What could be the reason?
Also valgrind shows no memory corruption. The input jpg file size is 170x30 pixels maximum
System Ubuntu-16
Makefile:
CFLAGS= -O2 -c -I$(INC_DIR) -fpermissive -std=c++11
CC=g++-5
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
readpattern: readpattern.o
g++ -IPNG/include -o readpattern readpattern.o libcorona.a -lpng -ljpeg
Code
void do_start_extract_char(char **output, int x, int y, int width, int height) {
//if pixel location croses boundary then return
if (x < 0 || y < 0 || x > width - 1 || y > height - 1) {
return;
}
//if the same pixel has already been visited then just return
if (output[y][x]) {
return;
}
if (isUsefulPixel(x, y)) {
//store it
output[y][x] = 1;
} else {
return;
}
//process left
do_start_extract_char(output, x - 1, y, width, height);
//process down
do_start_extract_char(output, x, y + 1, width, height);
//process right
do_start_extract_char(output, x + 1, y, width, height);
//process up
do_start_extract_char(output, x, y - 1, width, height);
//process left-down diagonally
// /
// /
do_start_extract_char(output, x - 1, y + 1, width, height);
//process left-up diagonally
// \
// \
do_start_extract_char(output, x - 1, y - 1, width, height);
//process right-down diagonally
// \
// \
do_start_extract_char(output, x + 1, y + 1, width, height);
//process right-up diagonally
// /
// /
do_start_extract_char(output, x + 1, y - 1, width, height);
return;
}
Starting from most pixels, going left, down, right and up recursively is enough to cover every single pixel in the entire image.
Left-down pixels would only be the way a pixel is reached when a pixel cannot be reached via left, down, right and up.
Note that naive recursion is a bad plan here. If your image has a few billion pixels, that means the first call may ends up with a few billion recursive calls. And that can blow your stack.
Instead, maintain your own stack of pixels to visit, and recurse by queuing up more tasks in there.