From 71a4338d4725485127a91b14a33d7070af15aa17 Mon Sep 17 00:00:00 2001 From: Geoffrey Date: Sun, 21 Jun 2020 00:27:35 +1200 Subject: [PATCH 1/3] Add support for Tesseract 4: invert images and compile as C++11 This contains the following PRs: - #72 by @bit: Fix compile with gcc7 and tesseract 4 - #75 by @oltodosel: inverting images for tesseract 4 --- CMakeLists.txt | 2 +- src/vobsub2srt.c++ | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d1954cb..cb0a17f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,7 +54,7 @@ include(CheckCXXSourceCompiles) include(CheckCXXSourceRuns) set(CMAKE_C_FLAGS "-std=gnu99") -set(CMAKE_CXX_FLAGS "-ansi -pedantic -Wall -Wextra -Wno-long-long") +set(CMAKE_CXX_FLAGS "-ansi -pedantic -Wall -Wextra -Wno-long-long -std=gnu++11") set(CMAKE_CXX_FLAGS_RELEASE "-O3 -mtune=native -march=native -DNDEBUG -fomit-frame-pointer -ffast-math") # TODO -Ofast GCC 4.6 set(CMAKE_C_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) diff --git a/src/vobsub2srt.c++ b/src/vobsub2srt.c++ index 2f9e1a0..b9c131c 100644 --- a/src/vobsub2srt.c++ +++ b/src/vobsub2srt.c++ @@ -265,6 +265,25 @@ int main(int argc, char **argv) { << start_pts << ")\n"; } + + // While tesseract version 3.05 (and older) handle inverted image (dark background and light text) without problem, for 4.x version use dark text on light background. + // https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality#inverting-images + + bool inverting_images = true; + + if (inverting_images) { + int size_r = width * height; + unsigned char* image_rev = new unsigned char[size_r]; + for (int i = 0; i < size_r; i++) + { + int val = static_cast(image[i]); + unsigned char cz = (255 - val); + image_rev[i] = cz; + } + + image = image_rev; + } + if(dump_images) { dump_pgm(subname, sub_counter, width, height, stride, image, image_size); } From 6d450d93eacebc82dc314bf8869e3aa31a7085d2 Mon Sep 17 00:00:00 2001 From: Geoffrey Date: Sun, 21 Jun 2020 00:41:07 +1200 Subject: [PATCH 2/3] Address feedback by @ruediger on PR #75 --- CMakeLists.txt | 2 +- src/vobsub2srt.c++ | 34 ++++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cb0a17f..e0f3117 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,7 +54,7 @@ include(CheckCXXSourceCompiles) include(CheckCXXSourceRuns) set(CMAKE_C_FLAGS "-std=gnu99") -set(CMAKE_CXX_FLAGS "-ansi -pedantic -Wall -Wextra -Wno-long-long -std=gnu++11") +set(CMAKE_CXX_FLAGS "-ansi -pedantic -Wall -Wextra -Wno-long-long -std=gnu++11 -DINVERT_IMAGES") set(CMAKE_CXX_FLAGS_RELEASE "-O3 -mtune=native -march=native -DNDEBUG -fomit-frame-pointer -ffast-math") # TODO -Ofast GCC 4.6 set(CMAKE_C_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) diff --git a/src/vobsub2srt.c++ b/src/vobsub2srt.c++ index b9c131c..5cba42f 100644 --- a/src/vobsub2srt.c++ +++ b/src/vobsub2srt.c++ @@ -92,6 +92,19 @@ using namespace tesseract; #define TESSERACT_DATA_PATH TESSERACT_DEFAULT_PATH #endif +struct ImageInverter { + ImageInverter(const unsigned char* image, size_t image_size) + : inverted_image(new unsigned char[image_size]) + { + for (size_t i = 0; i < image_size; ++i) { + inverted_image[i] = 255 - image[i]; + } + } + ~ImageInverter() { delete[] inverted_image; } + + unsigned char* inverted_image; +}; + int main(int argc, char **argv) { bool dump_images = false; bool verb = false; @@ -265,24 +278,17 @@ int main(int argc, char **argv) { << start_pts << ")\n"; } - - // While tesseract version 3.05 (and older) handle inverted image (dark background and light text) without problem, for 4.x version use dark text on light background. + // While tesseract version 3.05 (and older) handle inverted image (dark + // background and light text) without problem, for 4.x version use dark + // text on light background. // https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality#inverting-images - bool inverting_images = true; + #ifdef INVERT_IMAGES - if (inverting_images) { - int size_r = width * height; - unsigned char* image_rev = new unsigned char[size_r]; - for (int i = 0; i < size_r; i++) - { - int val = static_cast(image[i]); - unsigned char cz = (255 - val); - image_rev[i] = cz; - } + ImageInverter inverter(image, width*height); + image = inverter.inverted_image; - image = image_rev; - } + #endif // INVERT_IMAGES if(dump_images) { dump_pgm(subname, sub_counter, width, height, stride, image, image_size); From 3aee5bb60d673faa36a719f13cafe90bf3acf589 Mon Sep 17 00:00:00 2001 From: Geoffrey Date: Sun, 21 Jun 2020 18:11:39 +1200 Subject: [PATCH 3/3] Remove unused CMAKE_C_FLAGS --- CMakeLists.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e0f3117..278aad8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,13 +53,10 @@ include(CheckCXXCompilerFlag) include(CheckCXXSourceCompiles) include(CheckCXXSourceRuns) -set(CMAKE_C_FLAGS "-std=gnu99") set(CMAKE_CXX_FLAGS "-ansi -pedantic -Wall -Wextra -Wno-long-long -std=gnu++11 -DINVERT_IMAGES") set(CMAKE_CXX_FLAGS_RELEASE "-O3 -mtune=native -march=native -DNDEBUG -fomit-frame-pointer -ffast-math") # TODO -Ofast GCC 4.6 -set(CMAKE_C_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE}) set(CMAKE_CXX_FLAGS_DEBUG "-g3 -DDEBUG") -set(CMAKE_C_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) # TODO RelWithDebInfo MinSizeRel? maybe move -march=native -ffast-math etc. to a different build type find_package(Threads)