From 0e2e9293593148718914e90c8a53fa1363496dda Mon Sep 17 00:00:00 2001 From: Erik Auerswald Date: Thu, 6 Aug 2009 04:22:58 +0000 Subject: [PATCH] Correct indentation of code that is no longer inside an if () {}. --- ssocr.c | 1352 +++++++++++++++++++++++++++---------------------------- 1 file changed, 676 insertions(+), 676 deletions(-) diff --git a/ssocr.c b/ssocr.c index eff1c75..67106a2 100644 --- a/ssocr.c +++ b/ssocr.c @@ -312,717 +312,717 @@ int main(int argc, char **argv) exit(99); } - if(!(digits = calloc(number_of_digits, sizeof(digit_struct)))) { - perror("digits = calloc()"); - exit(99); - } + if(!(digits = calloc(number_of_digits, sizeof(digit_struct)))) { + perror("digits = calloc()"); + exit(99); + } - /* initialize some vars */ - for(i=0; i= argc-1) { - fprintf(stderr, "no commands given, using image %s unmodified\n", - argv[argc-1]); - } else { - fprintf(stderr, "got commands"); - for(i=optind; i= argc-1) { + fprintf(stderr, "no commands given, using image %s unmodified\n", + argv[argc-1]); + } else { + fprintf(stderr, "got commands"); for(i=optind; i0) && (i+10) && (i+10) && (i+10) && (i+1 ignore_pixels) { - /* 1 dark pixels darken the whole column */ - col = (ssocr_foreground == SSOCR_BLACK) ? DARK : LIGHT; + imlib_context_set_image(image); + imlib_free_image(); + image = new_image; + } else if(strcasecmp("closing",argv[i]) == 0) { + int n=atoi(argv[i+1]); + if((n>0) && (i+10) && (i+1=number_of_digits) { - fprintf(stderr, "found too many digits (%d)\n", d+1); + } + } + + /* assure we are working with the current image */ + imlib_context_set_image(image); + + /* write image to file if requested */ + if(output_file) { + save_image("output", image, output_fmt, output_file, flags); + } + + /* exit if only image processing shall be done */ + if(flags & PROCESS_ONLY) exit(3); + + if(flags & USE_DEBUG_IMAGE) { + /* copy processed image to debug image */ + debug_image = imlib_clone_image(); + } + + /* horizontal partition */ + state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; + d = 0; + for(i=0; i ignore_pixels) { + /* 1 dark pixels darken the whole column */ + col = (ssocr_foreground == SSOCR_BLACK) ? DARK : LIGHT; + } + } else if(col == UNKNOWN) /* light */ { + col = (ssocr_foreground == SSOCR_BLACK) ? LIGHT : DARK; + } + } + /* save digit position and draw partition line for DEBUG */ + if((state == ((ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT)) + && (col == ((ssocr_foreground == SSOCR_BLACK) ? DARK : LIGHT))) { + /* beginning of digit */ + if(d>=number_of_digits) { + fprintf(stderr, "found too many digits (%d)\n", d+1); + imlib_free_image_and_decache(); + if(flags & USE_DEBUG_IMAGE) { + save_image("debug", debug_image, output_fmt,debug_image_file,flags); + imlib_context_set_image(debug_image); imlib_free_image_and_decache(); - if(flags & USE_DEBUG_IMAGE) { - save_image("debug", debug_image, output_fmt,debug_image_file,flags); - imlib_context_set_image(debug_image); - imlib_free_image_and_decache(); - } - exit(1); } - digits[d].x1 = i; - digits[d].y1 = 0; - if(flags & USE_DEBUG_IMAGE) { - imlib_context_set_image(debug_image); - imlib_context_set_color(255,0,0,255);/* red line for start of digit */ - imlib_image_draw_line(i,0,i,h-1,0); - imlib_context_set_image(image); - } - state = (ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK; - } else if((state == - ((ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK)) - && (col == ((ssocr_foreground == SSOCR_BLACK) ? LIGHT : DARK))){ - /* end of digit */ - digits[d].x2 = i; - digits[d].y2 = h-1; - d++; - if(flags & USE_DEBUG_IMAGE) { - imlib_context_set_image(debug_image); - imlib_context_set_color(0,0,255,255); /* blue line for end of digit */ - imlib_image_draw_line(i,0,i,h-1,0); - imlib_context_set_image(image); - } - state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; + exit(1); } - } - - /* after the loop above the program should be in state FIND_DARK, - * i.e. after the last digit some light was found - * if it is still searching for light end the digit at the border of the - * image */ - if(state == (ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK) { - digits[d].x2 = w-1; + digits[d].x1 = i; + digits[d].y1 = 0; + if(flags & USE_DEBUG_IMAGE) { + imlib_context_set_image(debug_image); + imlib_context_set_color(255,0,0,255);/* red line for start of digit */ + imlib_image_draw_line(i,0,i,h-1,0); + imlib_context_set_image(image); + } + state = (ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK; + } else if((state == + ((ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK)) + && (col == ((ssocr_foreground == SSOCR_BLACK) ? LIGHT : DARK))){ + /* end of digit */ + digits[d].x2 = i; digits[d].y2 = h-1; d++; - state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; - } - if(d != number_of_digits) { - fprintf(stderr, "found only %d of %d digits\n", d, number_of_digits); - imlib_free_image_and_decache(); if(flags & USE_DEBUG_IMAGE) { - save_image("debug", debug_image, output_fmt, debug_image_file, flags); imlib_context_set_image(debug_image); - imlib_free_image_and_decache(); + imlib_context_set_color(0,0,255,255); /* blue line for end of digit */ + imlib_image_draw_line(i,0,i,h-1,0); + imlib_context_set_image(image); } - exit(1); - } - dig_w = digits[number_of_digits-1].x2 - digits[0].x1; - - if(flags & DEBUG_OUTPUT) { - fprintf(stderr, "found %d digits\n", d); - for(d=0; d (%d,%d), width: %d (%5.2f%%) " - "height/width (int): %d\n", d, - digits[d].x1, digits[d].y1, digits[d].x2, digits[d].y2, - digits[d].x2 - digits[d].x1, - ((digits[d].x2 - digits[d].x1) * 100.0) / dig_w, - (digits[d].y2-digits[d].y1)/(digits[d].x2-digits[d].x1) - ); - } - } - - /* at this point the digit 1 can be identified, because it is smaller than - * the other digits */ - for(i=0; i one_ratio) { - if(flags & DEBUG_OUTPUT) { - fprintf(stderr, "digit %d is a 1 (height/width = %d/%d = (int) %d)\n", - i, digits[i].y2 - digits[i].y1, digits[i].x2 - digits[i].x1, - (digits[i].y2 - digits[i].y1) / (digits[i].x2 - digits[i].x1)); - } - digits[i].digit = D_ONE; - } - } - - /* find upper and lower boundaries of every digit */ - for(d=0; d ignore_pixels) { - /* 1 pixels darken row */ - row = (ssocr_foreground == SSOCR_BLACK) ? DARK : LIGHT; - } - } else if(row == UNKNOWN) { - row = (ssocr_foreground == SSOCR_BLACK) ? LIGHT : DARK; - } - } - /* save position of digit and draw partition line for DEBUG */ - if((state == ((ssocr_foreground == SSOCR_BLACK)?FIND_DARK:FIND_LIGHT)) - && (row == ((ssocr_foreground == SSOCR_BLACK) ? DARK : LIGHT))) { - if(found_top) /* then we are searching for the bottom */ { - digits[d].y2 = j; - state = (ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK; - if(flags & USE_DEBUG_IMAGE) { - imlib_context_set_image(debug_image); - imlib_context_set_color(0,255,0,255); /* green line */ - imlib_image_draw_line(digits[d].x1,digits[d].y2, - digits[d].x2,digits[d].y2,0); - imlib_context_set_image(image); - } - } else /* found the top line */ { - digits[d].y1 = j; - found_top = 1; - state = (ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK; - if(flags & USE_DEBUG_IMAGE) { - imlib_context_set_image(debug_image); - imlib_context_set_color(0,255,0,255); /* green line */ - imlib_image_draw_line(digits[d].x1,digits[d].y1, - digits[d].x2,digits[d].y1,0); - imlib_context_set_image(image); - } - } - } else if((state == - ((ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK)) && - (row == ((ssocr_foreground == SSOCR_BLACK) ? LIGHT : DARK))) { - /* found_top has to be 1 because otherwise we were still looking for - * dark */ - digits[d].y2 = j; - state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; - if(flags & USE_DEBUG_IMAGE) { - imlib_context_set_image(debug_image); - imlib_context_set_color(0,255,0,255); /* green line */ - imlib_image_draw_line(digits[d].x1,digits[d].y2, - digits[d].x2,digits[d].y2,0); - imlib_context_set_image(image); - } - } - } - /* if we are still looking for light, use the bottom */ - if(state == ((ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK)){ - digits[d].y2 = h-1; - state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; - if(flags & USE_DEBUG_IMAGE) { - imlib_context_set_image(debug_image); - imlib_context_set_color(0,255,0,255); /* green line */ - imlib_image_draw_line(digits[d].x1,digits[d].y2, - digits[d].x2,digits[d].y2,0); - imlib_context_set_image(image); - } - } - } - if(flags & USE_DEBUG_IMAGE) { - /* draw rectangles around digits */ - imlib_context_set_image(debug_image); - imlib_context_set_color(128,128,128,255); /* gray line */ - for(d=0; d digits[d].y1 + d_height/3 && third == 1) { - if(found_pixels >= need_pixels) { - digits[d].digit |= HORIZ_UP; /* add upper segment */ - } - found_pixels = 0; - third++; - } else if(j > digits[d].y1 + 2*d_height/3 && third == 2) { - /* pixels in second third count towards middle segment */ - if(found_pixels >= need_pixels) { - digits[d].digit |= HORIZ_MID; /* add middle segment */ - } - found_pixels = 0; - third++; - } - } - /* found_pixels contains pixels of last third */ - if(found_pixels >= need_pixels) { - digits[d].digit |= HORIZ_DOWN; /* add lower segment */ - } - found_pixels = 0; - /* check upper vertical segments */ - half=1; /* in which half we are */ - quarter = digits[d].y1 + (digits[d].y2 - digits[d].y1) / 4; - for(i=digits[d].x1; i<=digits[d].x2; i++) { - imlib_image_query_pixel(i, quarter, &color); - lum = get_lum(&color, lt); - if(is_pixel_set(lum, thresh)) /* dark i.e. pixel is set */ { - if(flags & USE_DEBUG_IMAGE) { - if(half == 1) { - imlib_context_set_color(255,0,0,255); - } else if(half == 2) { - imlib_context_set_color(0,255,0,255); - } - imlib_context_set_image(debug_image); - imlib_image_draw_pixel(i,quarter,0); - imlib_context_set_image(image); - } - found_pixels++; - } - if(i >= middle && half == 1) { - if(found_pixels >= need_pixels) { - digits[d].digit |= VERT_LEFT_UP; - } - found_pixels = 0; - half++; - } - } - if(found_pixels >= need_pixels) { - digits[d].digit |= VERT_RIGHT_UP; - } - found_pixels = 0; - half = 1; - /* check lower vertical segments */ - half=1; /* in which half we are */ - three_quarters = digits[d].y1 + 3 * (digits[d].y2 - digits[d].y1) / 4; - for(i=digits[d].x1; i<=digits[d].x2; i++) { - imlib_image_query_pixel(i, three_quarters, &color); - lum = get_lum(&color, lt); - if(is_pixel_set(lum, thresh)) /* dark i.e. pixel is set */ { - if(flags & USE_DEBUG_IMAGE) { - if(half == 1) { - imlib_context_set_color(255,0,0,255); - } else if(half == 2) { - imlib_context_set_color(0,255,0,255); - } - imlib_context_set_image(debug_image); - imlib_image_draw_pixel(i,three_quarters,0); - imlib_context_set_image(image); - } - found_pixels++; - } - if(i >= middle && half == 1) { - if(found_pixels >= need_pixels) { - digits[d].digit |= VERT_LEFT_DOWN; - } - found_pixels = 0; - half++; - } - } - if(found_pixels >= need_pixels) { - digits[d].digit |= VERT_RIGHT_DOWN; - } - found_pixels = 0; - } - } - - /* print digits */ - for(i=0; i (%d,%d), width: %d (%5.2f%%) " + "height/width (int): %d\n", d, + digits[d].x1, digits[d].y1, digits[d].x2, digits[d].y2, + digits[d].x2 - digits[d].x1, + ((digits[d].x2 - digits[d].x1) * 100.0) / dig_w, + (digits[d].y2-digits[d].y1)/(digits[d].x2-digits[d].x1) + ); + } + } + + /* at this point the digit 1 can be identified, because it is smaller than + * the other digits */ + for(i=0; i one_ratio) { + if(flags & DEBUG_OUTPUT) { + fprintf(stderr, "digit %d is a 1 (height/width = %d/%d = (int) %d)\n", + i, digits[i].y2 - digits[i].y1, digits[i].x2 - digits[i].x1, + (digits[i].y2 - digits[i].y1) / (digits[i].x2 - digits[i].x1)); + } + digits[i].digit = D_ONE; + } + } + + /* find upper and lower boundaries of every digit */ + for(d=0; d ignore_pixels) { + /* 1 pixels darken row */ + row = (ssocr_foreground == SSOCR_BLACK) ? DARK : LIGHT; + } + } else if(row == UNKNOWN) { + row = (ssocr_foreground == SSOCR_BLACK) ? LIGHT : DARK; + } + } + /* save position of digit and draw partition line for DEBUG */ + if((state == ((ssocr_foreground == SSOCR_BLACK)?FIND_DARK:FIND_LIGHT)) + && (row == ((ssocr_foreground == SSOCR_BLACK) ? DARK : LIGHT))) { + if(found_top) /* then we are searching for the bottom */ { + digits[d].y2 = j; + state = (ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK; + if(flags & USE_DEBUG_IMAGE) { + imlib_context_set_image(debug_image); + imlib_context_set_color(0,255,0,255); /* green line */ + imlib_image_draw_line(digits[d].x1,digits[d].y2, + digits[d].x2,digits[d].y2,0); + imlib_context_set_image(image); + } + } else /* found the top line */ { + digits[d].y1 = j; + found_top = 1; + state = (ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK; + if(flags & USE_DEBUG_IMAGE) { + imlib_context_set_image(debug_image); + imlib_context_set_color(0,255,0,255); /* green line */ + imlib_image_draw_line(digits[d].x1,digits[d].y1, + digits[d].x2,digits[d].y1,0); + imlib_context_set_image(image); + } + } + } else if((state == + ((ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK)) && + (row == ((ssocr_foreground == SSOCR_BLACK) ? LIGHT : DARK))) { + /* found_top has to be 1 because otherwise we were still looking for + * dark */ + digits[d].y2 = j; + state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; + if(flags & USE_DEBUG_IMAGE) { + imlib_context_set_image(debug_image); + imlib_context_set_color(0,255,0,255); /* green line */ + imlib_image_draw_line(digits[d].x1,digits[d].y2, + digits[d].x2,digits[d].y2,0); + imlib_context_set_image(image); + } + } + } + /* if we are still looking for light, use the bottom */ + if(state == ((ssocr_foreground == SSOCR_BLACK) ? FIND_LIGHT : FIND_DARK)){ + digits[d].y2 = h-1; + state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; + if(flags & USE_DEBUG_IMAGE) { + imlib_context_set_image(debug_image); + imlib_context_set_color(0,255,0,255); /* green line */ + imlib_image_draw_line(digits[d].x1,digits[d].y2, + digits[d].x2,digits[d].y2,0); + imlib_context_set_image(image); + } + } + } + if(flags & USE_DEBUG_IMAGE) { + /* draw rectangles around digits */ + imlib_context_set_image(debug_image); + imlib_context_set_color(128,128,128,255); /* gray line */ + for(d=0; d digits[d].y1 + d_height/3 && third == 1) { + if(found_pixels >= need_pixels) { + digits[d].digit |= HORIZ_UP; /* add upper segment */ + } + found_pixels = 0; + third++; + } else if(j > digits[d].y1 + 2*d_height/3 && third == 2) { + /* pixels in second third count towards middle segment */ + if(found_pixels >= need_pixels) { + digits[d].digit |= HORIZ_MID; /* add middle segment */ + } + found_pixels = 0; + third++; + } + } + /* found_pixels contains pixels of last third */ + if(found_pixels >= need_pixels) { + digits[d].digit |= HORIZ_DOWN; /* add lower segment */ + } + found_pixels = 0; + /* check upper vertical segments */ + half=1; /* in which half we are */ + quarter = digits[d].y1 + (digits[d].y2 - digits[d].y1) / 4; + for(i=digits[d].x1; i<=digits[d].x2; i++) { + imlib_image_query_pixel(i, quarter, &color); + lum = get_lum(&color, lt); + if(is_pixel_set(lum, thresh)) /* dark i.e. pixel is set */ { + if(flags & USE_DEBUG_IMAGE) { + if(half == 1) { + imlib_context_set_color(255,0,0,255); + } else if(half == 2) { + imlib_context_set_color(0,255,0,255); + } + imlib_context_set_image(debug_image); + imlib_image_draw_pixel(i,quarter,0); + imlib_context_set_image(image); + } + found_pixels++; + } + if(i >= middle && half == 1) { + if(found_pixels >= need_pixels) { + digits[d].digit |= VERT_LEFT_UP; + } + found_pixels = 0; + half++; + } + } + if(found_pixels >= need_pixels) { + digits[d].digit |= VERT_RIGHT_UP; + } + found_pixels = 0; + half = 1; + /* check lower vertical segments */ + half=1; /* in which half we are */ + three_quarters = digits[d].y1 + 3 * (digits[d].y2 - digits[d].y1) / 4; + for(i=digits[d].x1; i<=digits[d].x2; i++) { + imlib_image_query_pixel(i, three_quarters, &color); + lum = get_lum(&color, lt); + if(is_pixel_set(lum, thresh)) /* dark i.e. pixel is set */ { + if(flags & USE_DEBUG_IMAGE) { + if(half == 1) { + imlib_context_set_color(255,0,0,255); + } else if(half == 2) { + imlib_context_set_color(0,255,0,255); + } + imlib_context_set_image(debug_image); + imlib_image_draw_pixel(i,three_quarters,0); + imlib_context_set_image(image); + } + found_pixels++; + } + if(i >= middle && half == 1) { + if(found_pixels >= need_pixels) { + digits[d].digit |= VERT_LEFT_DOWN; + } + found_pixels = 0; + half++; + } + } + if(found_pixels >= need_pixels) { + digits[d].digit |= VERT_RIGHT_DOWN; + } + found_pixels = 0; + } + } + + /* print digits */ + for(i=0; i