Tesseract  3.02
tesseract-ocr/ccutil/ocrclass.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        ocrclass.h
00003  * Description: Class definitions and constants for the OCR API.
00004  * Author:                                      Hewlett-Packard Co
00005  *
00006  * (C) Copyright 1996, Hewlett-Packard Co.
00007  ** Licensed under the Apache License, Version 2.0 (the "License");
00008  ** you may not use this file except in compliance with the License.
00009  ** You may obtain a copy of the License at
00010  ** http://www.apache.org/licenses/LICENSE-2.0
00011  ** Unless required by applicable law or agreed to in writing, software
00012  ** distributed under the License is distributed on an "AS IS" BASIS,
00013  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  ** See the License for the specific language governing permissions and
00015  ** limitations under the License.
00016  *
00017  **********************************************************************/
00018 
00019 /**********************************************************************
00020  * This file contains typedefs for all the structures used by
00021  * the HP OCR interface.
00022  * The code is designed to be used with either a C or C++ compiler.
00023  * The structures are designed to allow them to be used with any
00024  * structure alignment upto 8.
00025  **********************************************************************/
00026 
00027 #ifndef            CCUTIL_OCRCLASS_H_
00028 #define            CCUTIL_OCRCLASS_H_
00029 
00030 #ifndef __GNUC__
00031 #ifdef _WIN32
00032 #include          <windows.h>
00033 #include          "gettimeofday.h"
00034 #endif
00035 #else
00036 #include          <sys/time.h>
00037 #endif
00038 #include          <time.h>
00039 #include          "host.h"
00040 
00041 /*Maximum lengths of various strings*/
00042 #define MAX_FONT_NAME   34       /*name of font */
00043 #define MAX_OCR_NAME    32       /*name of engine */
00044 #define MAX_OCR_VERSION   17     /*version code of engine */
00045 
00046 /*pitch set definitions are identical to RTF*/
00047 #define PITCH_DEF     0          /*default */
00048 #define PITCH_FIXED     1        /*fixed pitch */
00049 #define PITCH_VAR     2          /*variable pitch */
00050 
00051 /**********************************************************************
00052  * EANYCODE_CHAR
00053  * Description of a single character. The character code is defined by
00054  * the character set of the current font.
00055  * Output text is sent as an array of these structures.
00056  * Spaces and line endings in the output are represented in the
00057  * structures of the surrounding characters. They are not directly
00058  * represented as characters.
00059  * The first character in a word has a positive value of blanks.
00060  * Missing information should be set to the defaults in the comments.
00061  * If word bounds are known, but not character bounds, then the top and
00062  * bottom of each character should be those of the word. The left of the
00063  * first and right of the last char in each word should be set. All other
00064  * lefts and rights should be set to -1.
00065  * If set, the values of right and bottom are left+width and top+height.
00066  * Most of the members come directly from the parameters to ocr_append_char.
00067  * The formatting member uses the enhancement parameter and combines the
00068  * line direction stuff into the top 3 bits.
00069  * The coding is 0=RL char, 1=LR char, 2=DR NL, 3=UL NL, 4=DR Para,
00070  * 5=UL Para, 6=TB char, 7=BT char. API users do not need to know what
00071  * the coding is, only that it is backwards compatible with the previous
00072  * version.
00073  **********************************************************************/
00074 
00075 typedef struct {                  /*single character */
00076 // It should be noted that the format for char_code for version 2.0 and beyond
00077 // is UTF8 which means that ASCII characters will come out as one structure but
00078 // other characters will be returned in two or more instances of this structure
00079 // with a single byte of the  UTF8 code in each, but each will have the same
00080 // bounding box. Programs which want to handle languagues with different
00081 // characters sets will need to handle extended characters appropriately, but
00082 // *all* code needs to be prepared to receive UTF8 coded characters for
00083 // characters such as bullet and fancy quotes.
00084   uinT16 char_code;              /*character itself */
00085   inT16 left;                    /*of char (-1) */
00086   inT16 right;                   /*of char (-1) */
00087   inT16 top;                     /*of char (-1) */
00088   inT16 bottom;                  /*of char (-1) */
00089   inT16 font_index;              /*what font (0) */
00090   uinT8 confidence;              /*0=perfect, 100=reject (0/100) */
00091   uinT8 point_size;              /*of char, 72=i inch, (10) */
00092   inT8 blanks;                   /*no of spaces before this char (1) */
00093   uinT8 formatting;              /*char formatting (0) */
00094 } EANYCODE_CHAR;                 /*single character */
00095 
00096 /**********************************************************************
00097  * ETEXT_DESC
00098  * Description of the output of the OCR engine.
00099  * This structure is used as both a progress monitor and the final
00100  * output header, since it needs to be a valid progress monitor while
00101  * the OCR engine is storing its output to shared memory.
00102  * During progress, all the buffer info is -1.
00103  * Progress starts at 0 and increases to 100 during OCR. No other constraint.
00104  * Every progress callback, the OCR engine must set ocr_alive to 1.
00105  * The HP side will set ocr_alive to 0. Repeated failure to reset
00106  * to 1 indicates that the OCR engine is dead.
00107  * If the cancel function is not null then it is called with the number of
00108  * user words found. If it returns true then operation is cancelled.
00109  **********************************************************************/
00110 typedef bool (*CANCEL_FUNC)(void* cancel_this, int words);
00111 
00112 class ETEXT_DESC {             // output header
00113  public:
00114   inT16 count;                 // chars in this buffer(0)
00115   inT16 progress;              // percent complete increasing (0-100)
00116   inT8 more_to_come;           // true if not last
00117   volatile inT8 ocr_alive;     // ocr sets to 1, HP 0
00118   inT8 err_code;               // for errcode use
00119   CANCEL_FUNC cancel;          // returns true to cancel
00120   void* cancel_this;           // this or other data for cancel
00121   struct timeval end_time;     // time to stop. expected to be set only by call
00122                                // to set_deadline_msecs()
00123   EANYCODE_CHAR text[1];       // character data
00124 
00125   ETEXT_DESC() : count(0), progress(0), more_to_come(0), ocr_alive(0),
00126                    err_code(0), cancel(NULL), cancel_this(NULL) {
00127     end_time.tv_sec = 0;
00128     end_time.tv_usec = 0;
00129   }
00130 
00131   // Sets the end time to be deadline_msecs milliseconds from now.
00132   void set_deadline_msecs(inT32 deadline_msecs) {
00133     gettimeofday(&end_time, NULL);
00134     inT32 deadline_secs = deadline_msecs / 1000;
00135     end_time.tv_sec += deadline_secs;
00136     end_time.tv_usec += (deadline_msecs -  deadline_secs * 1000) * 1000;
00137     if (end_time.tv_usec > 1000000) {
00138       end_time.tv_usec -= 1000000;
00139       ++end_time.tv_sec;
00140     }
00141   }
00142 
00143   // Returns false if we've not passed the end_time, or have not set a deadline.
00144   bool deadline_exceeded() const {
00145     if (end_time.tv_sec == 0 && end_time.tv_usec == 0) return false;
00146     struct timeval now;
00147     gettimeofday(&now, NULL);
00148     return (now.tv_sec > end_time.tv_sec || (now.tv_sec == end_time.tv_sec &&
00149                                              now.tv_usec > end_time.tv_usec));
00150   }
00151 };
00152 
00153 #endif  // CCUTIL_OCRCLASS_H_