From 73d7d697c271cc4c09571cc1a38f4d2c35a9b7d3 Mon Sep 17 00:00:00 2001 From: Erik Auerswald Date: Sun, 4 Nov 2012 17:24:21 +0000 Subject: [PATCH] Added auto-detection of number of digits in seven segment display. Instead of specifying the excat number of digits in the display, use --digits -1 to have ssocr auto-detect the number of digits. When this is used, ssocr cannot check if the correct number of digits has been recognized. Bumped version number to 2.13.0 to indicate a new feature. --- defines.h | 2 +- help.c | 2 +- ssocr.1.in | 3 +++ ssocr.c | 31 +++++++++++++++++++++++++------ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/defines.h b/defines.h index ded02f3..170e915 100644 --- a/defines.h +++ b/defines.h @@ -20,7 +20,7 @@ #define SSOCR2_DEFINES_H /* version number */ -#define VERSION "2.12.0" +#define VERSION "2.13.0" /* states */ #define FIND_DARK 0 diff --git a/help.c b/help.c index 374c12a..b45c93b 100644 --- a/help.c +++ b/help.c @@ -88,7 +88,7 @@ void usage(char *name, FILE *f) fprintf(f, " -n, --number-pixels=# number of pixels needed to recognize a segment\n"); fprintf(f, " -i, --ignore-pixels=# number of pixels ignored when searching digit\n"); fprintf(f, " boundaries\n"); - fprintf(f, " -d, --number-digits=# number of digits in image\n"); + fprintf(f, " -d, --number-digits=# number of digits in image (-1 for auto)\n"); fprintf(f, " -r, --one-ratio=# height/width ratio to recognize a \'one\'\n"); fprintf(f, " -o, --output-image=FILE write processed image to FILE\n"); fprintf(f, " -O, --output-format=FMT use output format FMT (Imlib2 formats)\n"); diff --git a/ssocr.1.in b/ssocr.1.in index e8ccccc..827775e 100644 --- a/ssocr.1.in +++ b/ssocr.1.in @@ -52,6 +52,9 @@ for a description of the algorithm. .SS -d, --number-digits NUMBER Specifies the number of digits shown in the image. Default value is .IR 6 . +Use +.I -1 +to automatically detect the number of digits. .SS -r, --one-ratio RATIO Set the height/width ratio threshold to recognize a digit as a one. RATIO takes integers only. diff --git a/ssocr.c b/ssocr.c index 3947ee3..3d1a675 100644 --- a/ssocr.c +++ b/ssocr.c @@ -235,7 +235,7 @@ int main(int argc, char **argv) case 'd': if(optarg) { number_of_digits = atoi(optarg); - if(number_of_digits < 1) { + if((number_of_digits < 1) && (number_of_digits != -1)) { fprintf(stderr, "warning: ignoring --number-digits=%s\n", optarg); number_of_digits = NUMBER_OF_DIGITS; } @@ -749,9 +749,16 @@ int main(int argc, char **argv) } /* allocate memory for seven segment digits */ - if(!(digits = calloc(number_of_digits, sizeof(digit_struct)))) { - perror("digits = calloc()"); - exit(99); + if(number_of_digits > -1) { + if(!(digits = calloc(number_of_digits, sizeof(digit_struct)))) { + perror("digits = calloc()"); + exit(99); + } + } else { + if(!(digits = calloc(1, sizeof(digit_struct)))) { + perror("digits = calloc()"); + exit(99); + } } /* horizontal partition */ @@ -778,7 +785,7 @@ int main(int argc, char **argv) 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) { + if((number_of_digits > -1) && (d >= number_of_digits)) { fprintf(stderr, "found too many digits (%d)\n", d+1); imlib_free_image_and_decache(); if(flags & USE_DEBUG_IMAGE) { @@ -810,6 +817,13 @@ int main(int argc, char **argv) imlib_image_draw_line(i,0,i,h-1,0); imlib_context_set_image(image); } + /* if number of digits is not known, add memory for another digit */ + if(!(digits = realloc(digits, (d+1) * sizeof(digit_struct)))) { + perror("digits = realloc()"); + exit(99); + } + /* initialize additional memory */ + memset(&digits[d], 0, sizeof(digit_struct)); state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; } } @@ -824,7 +838,7 @@ int main(int argc, char **argv) d++; state = (ssocr_foreground == SSOCR_BLACK) ? FIND_DARK : FIND_LIGHT; } - if(d != number_of_digits) { + if((number_of_digits > -1) && (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) { @@ -833,6 +847,11 @@ int main(int argc, char **argv) imlib_free_image_and_decache(); } exit(1); + } else if(number_of_digits == -1) { + number_of_digits = d; + if(flags & DEBUG_OUTPUT) { + fprintf(stderr, "auto detecting number of digits: %d\n", d); + } } dig_w = digits[number_of_digits-1].x2 - digits[0].x1;