2009-07-11 10:03:09 +08:00
|
|
|
/**********************************************************************
|
2010-11-24 02:34:14 +08:00
|
|
|
* File: tessedit.cpp (Formerly tessedit.c)
|
|
|
|
* Description: Main program for merge of tess and editor.
|
|
|
|
* Author: Ray Smith
|
|
|
|
* Created: Tue Jan 07 15:21:46 GMT 1992
|
|
|
|
*
|
|
|
|
* (C) Copyright 1992, Hewlett-Packard Ltd.
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2009-07-11 10:03:09 +08:00
|
|
|
|
2010-12-09 09:32:20 +08:00
|
|
|
// #define USE_VLD //Uncomment for Visual Leak Detector.
|
2010-07-22 05:19:53 +08:00
|
|
|
#if (defined _MSC_VER && defined USE_VLD)
|
2012-03-03 21:22:51 +08:00
|
|
|
#include "mfcpch.h"
|
2010-07-22 05:19:53 +08:00
|
|
|
#include <vld.h>
|
|
|
|
#endif
|
2009-07-11 10:03:09 +08:00
|
|
|
|
|
|
|
// Include automatically generated configuration file if running autoconf
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config_auto.h"
|
|
|
|
#endif
|
2010-09-30 23:53:40 +08:00
|
|
|
#ifdef USING_GETTEXT
|
2010-07-19 09:59:13 +08:00
|
|
|
#include <libintl.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#define _(x) gettext(x)
|
|
|
|
#else
|
|
|
|
#define _(x) (x)
|
|
|
|
#endif
|
2011-08-30 14:34:41 +08:00
|
|
|
|
2009-07-11 10:03:09 +08:00
|
|
|
#include "allheaders.h"
|
2010-11-30 08:58:30 +08:00
|
|
|
#include "baseapi.h"
|
2012-10-09 08:41:08 +08:00
|
|
|
#include "basedir.h"
|
2010-11-30 08:58:30 +08:00
|
|
|
#include "strngs.h"
|
2010-11-30 20:46:41 +08:00
|
|
|
#include "tesseractmain.h"
|
2012-03-05 06:27:16 +08:00
|
|
|
#include "tprintf.h"
|
2009-07-11 10:03:09 +08:00
|
|
|
|
|
|
|
/**********************************************************************
|
2010-11-30 08:58:30 +08:00
|
|
|
* main()
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2009-07-11 10:03:09 +08:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2012-03-03 01:31:24 +08:00
|
|
|
#ifdef USING_GETTEXT
|
2010-11-30 08:58:30 +08:00
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
bindtextdomain (PACKAGE, LOCALEDIR);
|
|
|
|
textdomain (PACKAGE);
|
2010-07-19 09:59:13 +08:00
|
|
|
#endif
|
2011-08-19 00:41:51 +08:00
|
|
|
if ((argc == 2 && strcmp(argv[1], "-v") == 0) ||
|
|
|
|
(argc == 2 && strcmp(argv[1], "--version") == 0)) {
|
2012-03-05 06:27:16 +08:00
|
|
|
char *versionStrP;
|
|
|
|
|
2011-08-09 04:33:18 +08:00
|
|
|
fprintf(stderr, "tesseract %s\n", tesseract::TessBaseAPI::Version());
|
2012-09-27 16:24:46 +08:00
|
|
|
|
2012-03-05 06:27:16 +08:00
|
|
|
versionStrP = getLeptonicaVersion();
|
|
|
|
fprintf(stderr, " %s\n", versionStrP);
|
|
|
|
lept_free(versionStrP);
|
2012-09-27 16:24:46 +08:00
|
|
|
|
2012-03-05 06:27:16 +08:00
|
|
|
versionStrP = getImagelibVersions();
|
|
|
|
fprintf(stderr, " %s\n", versionStrP);
|
|
|
|
lept_free(versionStrP);
|
|
|
|
|
2011-07-30 05:55:49 +08:00
|
|
|
exit(0);
|
2010-11-30 08:58:30 +08:00
|
|
|
}
|
2012-09-27 16:24:46 +08:00
|
|
|
|
2010-11-30 08:58:30 +08:00
|
|
|
// Make the order of args a bit more forgiving than it used to be.
|
|
|
|
const char* lang = "eng";
|
|
|
|
const char* image = NULL;
|
|
|
|
const char* output = NULL;
|
2012-12-24 01:47:06 +08:00
|
|
|
bool noocr = false;
|
|
|
|
bool list_langs = false;
|
2012-12-24 01:54:14 +08:00
|
|
|
bool print_parameters = false;
|
2012-12-24 01:47:06 +08:00
|
|
|
|
2010-11-30 08:58:30 +08:00
|
|
|
tesseract::PageSegMode pagesegmode = tesseract::PSM_AUTO;
|
|
|
|
int arg = 1;
|
|
|
|
while (arg < argc && (output == NULL || argv[arg][0] == '-')) {
|
|
|
|
if (strcmp(argv[arg], "-l") == 0 && arg + 1 < argc) {
|
|
|
|
lang = argv[arg + 1];
|
|
|
|
++arg;
|
|
|
|
} else if (strcmp(argv[arg], "-psm") == 0 && arg + 1 < argc) {
|
|
|
|
pagesegmode = static_cast<tesseract::PageSegMode>(atoi(argv[arg + 1]));
|
|
|
|
++arg;
|
2012-12-24 01:54:14 +08:00
|
|
|
} else if (strcmp(argv[arg], "--print-parameters") == 0) {
|
|
|
|
noocr = true;
|
|
|
|
print_parameters = true;
|
2013-05-03 01:06:14 +08:00
|
|
|
} else if (strcmp(argv[arg], "-c") == 0 && arg + 1 < argc) {
|
2013-04-30 04:43:14 +08:00
|
|
|
// handled properly after api init
|
|
|
|
++arg;
|
2010-11-30 08:58:30 +08:00
|
|
|
} else if (image == NULL) {
|
|
|
|
image = argv[arg];
|
|
|
|
} else if (output == NULL) {
|
|
|
|
output = argv[arg];
|
|
|
|
}
|
|
|
|
++arg;
|
|
|
|
}
|
2012-12-24 01:47:06 +08:00
|
|
|
|
|
|
|
if (argc == 2 && strcmp(argv[1], "--list-langs") == 0) {
|
|
|
|
list_langs = true;
|
|
|
|
noocr = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output == NULL && noocr == false) {
|
2012-12-24 01:52:42 +08:00
|
|
|
fprintf(stderr, _("Usage:%s imagename outputbase|stdout [-l lang] "
|
2013-05-03 01:06:14 +08:00
|
|
|
"[-psm pagesegmode] [-c configvar=value] "
|
2013-04-30 04:43:14 +08:00
|
|
|
"[configfile...]\n\n"), argv[0]);
|
2010-11-30 08:58:30 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
_("pagesegmode values are:\n"
|
|
|
|
"0 = Orientation and script detection (OSD) only.\n"
|
|
|
|
"1 = Automatic page segmentation with OSD.\n"
|
|
|
|
"2 = Automatic page segmentation, but no OSD, or OCR\n"
|
|
|
|
"3 = Fully automatic page segmentation, but no OSD. (Default)\n"
|
|
|
|
"4 = Assume a single column of text of variable sizes.\n"
|
|
|
|
"5 = Assume a single uniform block of vertically aligned text.\n"
|
|
|
|
"6 = Assume a single uniform block of text.\n"
|
|
|
|
"7 = Treat the image as a single text line.\n"
|
|
|
|
"8 = Treat the image as a single word.\n"
|
|
|
|
"9 = Treat the image as a single word in a circle.\n"
|
|
|
|
"10 = Treat the image as a single character.\n"));
|
2013-05-03 01:06:14 +08:00
|
|
|
fprintf(stderr, _("multiple -c arguments are allowed.\n"));
|
|
|
|
fprintf(stderr, _("-l lang, -psm pagesegmode and any -c options must occur"
|
2013-04-30 04:43:14 +08:00
|
|
|
"before any configfile.\n\n"));
|
2012-09-27 16:24:46 +08:00
|
|
|
fprintf(stderr, _("Single options:\n"));
|
|
|
|
fprintf(stderr, _(" -v --version: version info\n"));
|
2012-10-23 07:40:19 +08:00
|
|
|
fprintf(stderr, _(" --list-langs: list available languages for tesseract "
|
|
|
|
"engine\n"));
|
2012-12-24 01:54:14 +08:00
|
|
|
fprintf(stderr, _(" --print-parameters: print tesseract parameters to the "
|
|
|
|
"stdout\n"));
|
2010-11-30 08:58:30 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-12-24 01:47:06 +08:00
|
|
|
tesseract::TessBaseAPI api;
|
2010-11-30 08:58:30 +08:00
|
|
|
|
2012-12-24 01:47:06 +08:00
|
|
|
STRING tessdata_dir;
|
|
|
|
truncate_path(argv[0], &tessdata_dir);
|
2010-11-30 08:58:30 +08:00
|
|
|
api.SetOutputName(output);
|
2012-12-24 01:47:06 +08:00
|
|
|
int rc = api.Init(tessdata_dir.string(), lang, tesseract::OEM_DEFAULT,
|
2012-10-09 08:41:08 +08:00
|
|
|
&(argv[arg]), argc - arg, NULL, NULL, false);
|
2012-12-24 01:47:06 +08:00
|
|
|
|
2012-02-26 22:48:35 +08:00
|
|
|
if (rc) {
|
2012-10-23 07:40:19 +08:00
|
|
|
fprintf(stderr, _("Could not initialize tesseract.\n"));
|
2012-02-26 22:48:35 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-09-27 16:24:46 +08:00
|
|
|
|
2013-04-30 04:43:14 +08:00
|
|
|
char opt1[255], opt2[255];
|
|
|
|
for (arg = 0; arg < argc; arg++) {
|
2013-05-03 01:06:14 +08:00
|
|
|
if (strcmp(argv[arg], "-c") == 0 && arg + 1 < argc) {
|
2013-04-30 04:43:14 +08:00
|
|
|
strncpy(opt1, argv[arg + 1], 255);
|
|
|
|
*(strchr(opt1, '=')) = 0;
|
|
|
|
strncpy(opt2, strchr(argv[arg + 1], '=') + 1, 255);
|
|
|
|
opt2[254] = 0;
|
|
|
|
++arg;
|
|
|
|
|
|
|
|
if(!api.SetVariable(opt1, opt2)) {
|
|
|
|
fprintf(stderr, _("Could not set option: %s=%s\n"), opt1, opt2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-24 01:47:06 +08:00
|
|
|
if (list_langs) {
|
|
|
|
GenericVector<STRING> languages;
|
|
|
|
api.GetAvailableLanguagesAsVector(&languages);
|
|
|
|
fprintf(stderr, _("List of available languages (%d):\n"),
|
|
|
|
languages.size());
|
|
|
|
for (int index = 0; index < languages.size(); ++index) {
|
|
|
|
STRING& string = languages[index];
|
|
|
|
fprintf(stderr, "%s\n", string.string());
|
|
|
|
}
|
|
|
|
api.End();
|
|
|
|
exit(0);
|
|
|
|
}
|
2012-12-24 01:54:14 +08:00
|
|
|
|
|
|
|
if (print_parameters) {
|
|
|
|
FILE* fout = stdout;
|
|
|
|
fprintf(stdout, _("Tesseract parameters:\n"));
|
|
|
|
api.PrintVariables(fout);
|
|
|
|
api.End();
|
|
|
|
exit(0);
|
|
|
|
}
|
2012-12-24 01:47:06 +08:00
|
|
|
|
2011-08-19 00:41:51 +08:00
|
|
|
// We have 2 possible sources of pagesegmode: a config file and
|
|
|
|
// the command line. For backwards compatability reasons, the
|
|
|
|
// default in tesseract is tesseract::PSM_SINGLE_BLOCK, but the
|
|
|
|
// default for this program is tesseract::PSM_AUTO. We will let
|
|
|
|
// the config file take priority, so the command-line default
|
|
|
|
// can take priority over the tesseract default, so we use the
|
|
|
|
// value from the command line only if the retrieved mode
|
|
|
|
// is still tesseract::PSM_SINGLE_BLOCK, indicating no change
|
|
|
|
// in any config file. Therefore the only way to force
|
|
|
|
// tesseract::PSM_SINGLE_BLOCK is from the command line.
|
|
|
|
// It would be simpler if we could set the value before Init,
|
|
|
|
// but that doesn't work.
|
|
|
|
if (api.GetPageSegMode() == tesseract::PSM_SINGLE_BLOCK)
|
|
|
|
api.SetPageSegMode(pagesegmode);
|
2012-03-03 19:48:59 +08:00
|
|
|
tprintf("Tesseract Open Source OCR Engine v%s with Leptonica\n",
|
2010-11-30 08:58:30 +08:00
|
|
|
tesseract::TessBaseAPI::Version());
|
|
|
|
|
2011-08-19 00:41:51 +08:00
|
|
|
|
2011-08-12 05:42:13 +08:00
|
|
|
FILE* fin = fopen(image, "rb");
|
2011-07-30 05:55:49 +08:00
|
|
|
if (fin == NULL) {
|
2012-10-23 07:40:19 +08:00
|
|
|
fprintf(stderr, _("Cannot open input file: %s\n"), image);
|
2011-07-30 05:55:49 +08:00
|
|
|
exit(2);
|
2011-08-19 00:41:51 +08:00
|
|
|
}
|
2011-07-30 05:55:49 +08:00
|
|
|
fclose(fin);
|
2011-08-19 00:41:51 +08:00
|
|
|
|
2010-11-30 08:58:30 +08:00
|
|
|
bool output_hocr = false;
|
|
|
|
api.GetBoolVariable("tessedit_create_hocr", &output_hocr);
|
|
|
|
bool output_box = false;
|
|
|
|
api.GetBoolVariable("tessedit_create_boxfile", &output_box);
|
2012-12-24 01:52:42 +08:00
|
|
|
|
|
|
|
FILE* fout = stdout;
|
|
|
|
if (strcmp(output, "-") && strcmp(output, "stdout")) {
|
|
|
|
STRING outfile = output;
|
|
|
|
outfile += output_hocr ? ".html" : output_box ? ".box" : ".txt";
|
|
|
|
fout = fopen(outfile.string(), "wb");
|
|
|
|
if (fout == NULL) {
|
|
|
|
fprintf(stderr, _("Cannot create output file %s\n"), outfile.string());
|
|
|
|
exit(1);
|
|
|
|
}
|
2010-11-30 08:58:30 +08:00
|
|
|
}
|
2012-12-24 01:49:15 +08:00
|
|
|
|
|
|
|
STRING text_out;
|
|
|
|
if (!api.ProcessPages(image, NULL, 0, &text_out)) {
|
|
|
|
fprintf(stderr, _("Error during processing.\n"));
|
2013-04-12 16:14:13 +08:00
|
|
|
if (fout != stdout)
|
|
|
|
fclose(fout);
|
|
|
|
exit(1);
|
2012-12-24 01:49:15 +08:00
|
|
|
}
|
|
|
|
|
2010-11-30 08:58:30 +08:00
|
|
|
fwrite(text_out.string(), 1, text_out.length(), fout);
|
2012-12-24 01:52:42 +08:00
|
|
|
if (fout != stdout)
|
|
|
|
fclose(fout);
|
|
|
|
else
|
|
|
|
clearerr(fout);
|
2010-11-30 08:58:30 +08:00
|
|
|
|
|
|
|
return 0; // Normal exit
|
2009-07-11 10:03:09 +08:00
|
|
|
}
|
|
|
|
|
2012-02-20 05:48:45 +08:00
|
|
|
#ifdef _WIN32
|
2010-11-30 08:58:30 +08:00
|
|
|
|
2011-07-30 05:55:49 +08:00
|
|
|
char szAppName[] = "Tesseract"; //app name
|
2009-07-11 10:03:09 +08:00
|
|
|
int initialized = 0;
|
|
|
|
|
|
|
|
/**********************************************************************
|
2010-11-24 02:34:14 +08:00
|
|
|
* WinMain
|
|
|
|
*
|
|
|
|
* Main function for a windows program.
|
|
|
|
**********************************************************************/
|
2009-07-11 10:03:09 +08:00
|
|
|
|
|
|
|
int WINAPI WinMain( //main for windows //command line
|
2010-11-24 02:34:14 +08:00
|
|
|
HINSTANCE hInstance,
|
|
|
|
HINSTANCE hPrevInstance,
|
|
|
|
LPSTR lpszCmdLine,
|
|
|
|
int nCmdShow) {
|
2010-11-30 08:58:30 +08:00
|
|
|
WNDCLASS wc;
|
|
|
|
HWND hwnd;
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
char **argv;
|
|
|
|
char *argsin[2];
|
|
|
|
int argc;
|
|
|
|
int exit_code;
|
|
|
|
|
|
|
|
wc.style = CS_NOCLOSE | CS_OWNDC;
|
|
|
|
wc.lpfnWndProc = (WNDPROC) WndProc;
|
|
|
|
wc.cbClsExtra = 0;
|
|
|
|
wc.cbWndExtra = 0;
|
|
|
|
wc.hInstance = hInstance;
|
|
|
|
wc.hIcon = NULL; //LoadIcon (NULL, IDI_APPLICATION);
|
|
|
|
wc.hCursor = NULL; //LoadCursor (NULL, IDC_ARROW);
|
|
|
|
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
|
|
|
|
wc.lpszMenuName = NULL;
|
|
|
|
wc.lpszClassName = szAppName;
|
|
|
|
|
|
|
|
RegisterClass(&wc);
|
|
|
|
|
|
|
|
hwnd = CreateWindow (szAppName, szAppName,
|
|
|
|
WS_OVERLAPPEDWINDOW | WS_DISABLED,
|
|
|
|
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
|
|
|
|
CW_USEDEFAULT, HWND_DESKTOP, NULL, hInstance, NULL);
|
|
|
|
|
|
|
|
argsin[0] = strdup (szAppName);
|
|
|
|
argsin[1] = strdup (lpszCmdLine);
|
|
|
|
/*allocate memory for the args. There can never be more than half*/
|
|
|
|
/*the total number of characters in the arguments.*/
|
2010-12-09 09:32:20 +08:00
|
|
|
argv = (char **)malloc(((strlen(argsin[0]) + strlen(argsin[1])) / 2 + 1) *
|
|
|
|
sizeof(char *));
|
2010-11-30 08:58:30 +08:00
|
|
|
|
|
|
|
/*now construct argv as it should be for C.*/
|
|
|
|
argc = parse_args (2, argsin, argv);
|
|
|
|
|
|
|
|
// ShowWindow (hwnd, nCmdShow);
|
|
|
|
// UpdateWindow (hwnd);
|
|
|
|
|
|
|
|
if (initialized) {
|
|
|
|
exit_code = main (argc, argv);
|
|
|
|
free (argsin[0]);
|
|
|
|
free (argsin[1]);
|
|
|
|
free(argv);
|
|
|
|
return exit_code;
|
|
|
|
}
|
|
|
|
while (GetMessage (&msg, NULL, 0, 0)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
if (initialized) {
|
|
|
|
exit_code = main (argc, argv);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
exit_code = msg.wParam;
|
|
|
|
}
|
|
|
|
free (argsin[0]);
|
|
|
|
free (argsin[1]);
|
|
|
|
free(argv);
|
|
|
|
return exit_code;
|
2009-07-11 10:03:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
2010-11-24 02:34:14 +08:00
|
|
|
* WndProc
|
|
|
|
*
|
|
|
|
* Function to respond to messages.
|
|
|
|
**********************************************************************/
|
2009-07-11 10:03:09 +08:00
|
|
|
|
|
|
|
LONG WINAPI WndProc( //message handler
|
2010-11-24 02:34:14 +08:00
|
|
|
HWND hwnd, //window with message
|
|
|
|
UINT msg, //message typ
|
|
|
|
WPARAM wParam,
|
|
|
|
LPARAM lParam) {
|
2010-11-30 08:58:30 +08:00
|
|
|
HDC hdc;
|
|
|
|
|
|
|
|
if (msg == WM_CREATE) {
|
|
|
|
//
|
|
|
|
// Create a rendering context.
|
|
|
|
//
|
|
|
|
hdc = GetDC (hwnd);
|
|
|
|
ReleaseDC(hwnd, hdc);
|
|
|
|
initialized = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return DefWindowProc (hwnd, msg, wParam, lParam);
|
2009-07-11 10:03:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************************
|
2010-11-24 02:34:14 +08:00
|
|
|
* parse_args
|
|
|
|
*
|
|
|
|
* Turn a list of args into a new list of args with each separate
|
|
|
|
* whitespace spaced string being an arg.
|
|
|
|
**********************************************************************/
|
2009-07-11 10:03:09 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
parse_args ( /*refine arg list */
|
2010-11-24 02:34:14 +08:00
|
|
|
int argc, /*no of input args */
|
|
|
|
char *argv[], /*input args */
|
|
|
|
char *arglist[] /*output args */
|
|
|
|
) {
|
2010-11-30 08:58:30 +08:00
|
|
|
int argcount; /*converted argc */
|
|
|
|
char *testchar; /*char in option string */
|
|
|
|
int arg; /*current argument */
|
|
|
|
|
|
|
|
argcount = 0; /*no of options */
|
|
|
|
for (arg = 0; arg < argc; arg++) {
|
|
|
|
testchar = argv[arg]; /*start of arg */
|
|
|
|
do {
|
|
|
|
while (*testchar
|
|
|
|
&& (*testchar == ' ' || *testchar == '\n'
|
|
|
|
|| *testchar == '\t'))
|
|
|
|
testchar++; /*skip white space */
|
|
|
|
if (*testchar) {
|
|
|
|
/*new arg */
|
|
|
|
arglist[argcount++] = testchar;
|
|
|
|
/*skip to white space */
|
|
|
|
for (testchar++; *testchar && *testchar != ' ' && *testchar != '\n' && *testchar != '\t'; testchar++) ;
|
|
|
|
if (*testchar)
|
|
|
|
*testchar++ = '\0'; /*turn to separate args */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*testchar);
|
|
|
|
}
|
|
|
|
return argcount; /*new number of args */
|
2009-07-11 10:03:09 +08:00
|
|
|
}
|
|
|
|
#endif
|