All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::CubeUtils Class Reference

#include <cube_utils.h>

Public Member Functions

 CubeUtils ()
 
 ~CubeUtils ()
 

Static Public Member Functions

static int Prob2Cost (double prob_val)
 
static double Cost2Prob (int cost)
 
static int StrLen (const char_32 *str)
 
static int StrCmp (const char_32 *str1, const char_32 *str2)
 
static char_32StrDup (const char_32 *str)
 
static CharSampCharSampleFromPix (Pix *pix, int left, int top, int wid, int hgt)
 
static Pix * PixFromCharSample (CharSamp *char_samp)
 
static bool ReadFileToString (const string &file_name, string *str)
 
static void SplitStringUsing (const string &str, const string &delims, vector< string > *str_vec)
 
static void UTF8ToUTF32 (const char *utf8_str, string_32 *str32)
 
static void UTF32ToUTF8 (const char_32 *utf32_str, string *str)
 
static bool IsCaseInvariant (const char_32 *str32, CharSet *char_set)
 
static char_32ToLower (const char_32 *str32, CharSet *char_set)
 
static char_32ToUpper (const char_32 *str32, CharSet *char_set)
 

Detailed Description

Definition at line 35 of file cube_utils.h.

Constructor & Destructor Documentation

tesseract::CubeUtils::CubeUtils ( )

Definition at line 28 of file cube_utils.cpp.

28  {
29 }
tesseract::CubeUtils::~CubeUtils ( )

Definition at line 31 of file cube_utils.cpp.

31  {
32 }

Member Function Documentation

CharSamp * tesseract::CubeUtils::CharSampleFromPix ( Pix *  pix,
int  left,
int  top,
int  wid,
int  hgt 
)
static

creates a char samp from a specified portion of the image

Definition at line 104 of file cube_utils.cpp.

105  {
106  // get the raw img data from the image
107  unsigned char *temp_buff = GetImageData(pix, left, top, wid, hgt);
108  if (temp_buff == NULL) {
109  return NULL;
110  }
111 
112  // create a char samp from temp buffer
113  CharSamp *char_samp = CharSamp::FromRawData(left, top, wid, hgt, temp_buff);
114 
115  // clean up temp buffer
116  delete []temp_buff;
117  return char_samp;
118 }
static CharSamp * FromRawData(int left, int top, int wid, int hgt, unsigned char *data)
Definition: char_samp.cpp:273
#define NULL
Definition: host.h:144
double tesseract::CubeUtils::Cost2Prob ( int  cost)
static

converts a cost to probability

Definition at line 47 of file cube_utils.cpp.

47  {
48  return exp(-cost / PROB2COST_SCALE);
49 }
#define PROB2COST_SCALE
Definition: cube_const.h:24
bool tesseract::CubeUtils::IsCaseInvariant ( const char_32 str32,
CharSet char_set 
)
static

Definition at line 294 of file cube_utils.cpp.

294  {
295  bool all_one_case = true;
296  bool capitalized;
297  bool prev_upper;
298  bool prev_lower;
299  bool first_upper;
300  bool first_lower;
301  bool cur_upper;
302  bool cur_lower;
303 
304  string str8;
305  if (!char_set) {
306  // If cube char_set is missing, use C-locale-dependent functions
307  // on UTF8 characters to determine case properties.
308  first_upper = isupper(str32[0]);
309  first_lower = islower(str32[0]);
310  if (first_upper)
311  capitalized = true;
312  prev_upper = first_upper;
313  prev_lower = islower(str32[0]);
314  for (int c = 1; str32[c] != 0; ++c) {
315  cur_upper = isupper(str32[c]);
316  cur_lower = islower(str32[c]);
317  if ((prev_upper && cur_lower) || (prev_lower && cur_upper))
318  all_one_case = false;
319  if (cur_upper)
320  capitalized = false;
321  prev_upper = cur_upper;
322  prev_lower = cur_lower;
323  }
324  } else {
325  UNICHARSET *unicharset = char_set->InternalUnicharset();
326  // Use UNICHARSET functions to determine case properties
327  first_upper = unicharset->get_isupper(char_set->ClassID(str32[0]));
328  first_lower = unicharset->get_islower(char_set->ClassID(str32[0]));
329  if (first_upper)
330  capitalized = true;
331  prev_upper = first_upper;
332  prev_lower = unicharset->get_islower(char_set->ClassID(str32[0]));
333 
334  for (int c = 1; c < StrLen(str32); ++c) {
335  cur_upper = unicharset->get_isupper(char_set->ClassID(str32[c]));
336  cur_lower = unicharset->get_islower(char_set->ClassID(str32[c]));
337  if ((prev_upper && cur_lower) || (prev_lower && cur_upper))
338  all_one_case = false;
339  if (cur_upper)
340  capitalized = false;
341  prev_upper = cur_upper;
342  prev_lower = cur_lower;
343  }
344  }
345  return all_one_case || capitalized;
346 }
bool get_isupper(UNICHAR_ID unichar_id) const
Definition: unicharset.h:463
bool get_islower(UNICHAR_ID unichar_id) const
Definition: unicharset.h:456
static int StrLen(const char_32 *str)
Definition: cube_utils.cpp:54
Pix * tesseract::CubeUtils::PixFromCharSample ( CharSamp char_samp)
static

create a B/W image from a char_sample

Definition at line 123 of file cube_utils.cpp.

123  {
124  // parameter check
125  if (char_samp == NULL) {
126  return NULL;
127  }
128 
129  // get the raw data
130  int stride = char_samp->Stride();
131  int wid = char_samp->Width();
132  int hgt = char_samp->Height();
133 
134  Pix *pix = pixCreate(wid, hgt, 1);
135  if (pix == NULL) {
136  return NULL;
137  }
138 
139  // copy the contents
140  unsigned char *line = char_samp->RawData();
141  for (int y = 0; y < hgt ; y++, line += stride) {
142  for (int x = 0; x < wid; x++) {
143  if (line[x] != 0) {
144  pixSetPixel(pix, x, y, 0);
145  } else {
146  pixSetPixel(pix, x, y, 255);
147  }
148  }
149  }
150 
151  return pix;
152 }
#define NULL
Definition: host.h:144
int tesseract::CubeUtils::Prob2Cost ( double  prob_val)
static

convert a prob to a cost (-ve log prob)

Definition at line 37 of file cube_utils.cpp.

37  {
38  if (prob_val < MIN_PROB) {
39  return MIN_PROB_COST;
40  }
41  return static_cast<int>(-log(prob_val) * PROB2COST_SCALE);
42 }
#define MIN_PROB
Definition: cube_const.h:28
#define PROB2COST_SCALE
Definition: cube_const.h:24
#define MIN_PROB_COST
Definition: cube_const.h:26
bool tesseract::CubeUtils::ReadFileToString ( const string &  file_name,
string *  str 
)
static

read file contents to a string

Definition at line 195 of file cube_utils.cpp.

195  {
196  str->clear();
197  FILE *fp = fopen(file_name.c_str(), "rb");
198  if (fp == NULL) {
199  return false;
200  }
201 
202  // get the size of the size
203  fseek(fp, 0, SEEK_END);
204  int file_size = ftell(fp);
205  if (file_size < 1) {
206  fclose(fp);
207  return false;
208  }
209  // adjust string size
210  str->reserve(file_size);
211  // read the contents
212  rewind(fp);
213  char *buff = new char[file_size];
214  if (buff == NULL) {
215  fclose(fp);
216  return false;
217  }
218  int read_bytes = fread(buff, 1, static_cast<int>(file_size), fp);
219  if (read_bytes == file_size) {
220  str->append(buff, file_size);
221  }
222  delete []buff;
223  fclose(fp);
224  return (read_bytes == file_size);
225 }
#define NULL
Definition: host.h:144
void tesseract::CubeUtils::SplitStringUsing ( const string &  str,
const string &  delims,
vector< string > *  str_vec 
)
static

splits a string into vectors based on specified delimiters

Definition at line 230 of file cube_utils.cpp.

232  {
233  // Optimize the common case where delims is a single character.
234  if (delims[0] != '\0' && delims[1] == '\0') {
235  char c = delims[0];
236  const char* p = str.data();
237  const char* end = p + str.size();
238  while (p != end) {
239  if (*p == c) {
240  ++p;
241  } else {
242  const char* start = p;
243  while (++p != end && *p != c);
244  str_vec->push_back(string(start, p - start));
245  }
246  }
247  return;
248  }
249 
250  string::size_type begin_index, end_index;
251  begin_index = str.find_first_not_of(delims);
252  while (begin_index != string::npos) {
253  end_index = str.find_first_of(delims, begin_index);
254  if (end_index == string::npos) {
255  str_vec->push_back(str.substr(begin_index));
256  return;
257  }
258  str_vec->push_back(str.substr(begin_index, (end_index - begin_index)));
259  begin_index = str.find_first_not_of(delims, end_index);
260  }
261 }
int tesseract::CubeUtils::StrCmp ( const char_32 str1,
const char_32 str2 
)
static

compares two char_32 strings

Definition at line 66 of file cube_utils.cpp.

66  {
67  const char_32 *pch1 = str1;
68  const char_32 *pch2 = str2;
69 
70  for (; (*pch1) != 0 && (*pch2) != 0; pch1++, pch2++) {
71  if ((*pch1) != (*pch2)) {
72  return (*pch1) - (*pch2);
73  }
74  }
75 
76  if ((*pch1) == 0) {
77  if ((*pch2) == 0) {
78  return 0;
79  } else {
80  return -1;
81  }
82  } else {
83  return 1;
84  }
85 }
signed int char_32
Definition: string_32.h:40
char_32 * tesseract::CubeUtils::StrDup ( const char_32 str32)
static

Duplicates a 32-bit char buffer

Definition at line 90 of file cube_utils.cpp.

90  {
91  int len = StrLen(str32);
92  char_32 *new_str = new char_32[len + 1];
93  if (new_str == NULL) {
94  return NULL;
95  }
96  memcpy(new_str, str32, len * sizeof(*str32));
97  new_str[len] = 0;
98  return new_str;
99 }
static int StrLen(const char_32 *str)
Definition: cube_utils.cpp:54
signed int char_32
Definition: string_32.h:40
#define NULL
Definition: host.h:144
int tesseract::CubeUtils::StrLen ( const char_32 char_32_ptr)
static

computes the length of a NULL terminated char_32 string

Definition at line 54 of file cube_utils.cpp.

54  {
55  if (char_32_ptr == NULL) {
56  return 0;
57  }
58  int len = -1;
59  while (char_32_ptr[++len]);
60  return len;
61 }
#define NULL
Definition: host.h:144
char_32 * tesseract::CubeUtils::ToLower ( const char_32 str32,
CharSet char_set 
)
static

Definition at line 348 of file cube_utils.cpp.

348  {
349  if (!char_set) {
350  return NULL;
351  }
352  UNICHARSET *unicharset = char_set->InternalUnicharset();
353  int len = StrLen(str32);
354  char_32 *lower = new char_32[len + 1];
355  if (!lower)
356  return NULL;
357  for (int i = 0; i < len; ++i) {
358  char_32 ch = str32[i];
359  if (ch == INVALID_UNICHAR_ID) {
360  delete [] lower;
361  return NULL;
362  }
363  // convert upper-case characters to lower-case
364  if (unicharset->get_isupper(char_set->ClassID(ch))) {
365  UNICHAR_ID uid_lower = unicharset->get_other_case(char_set->ClassID(ch));
366  const char_32 *str32_lower = char_set->ClassString(uid_lower);
367  // expect lower-case version of character to be a single character
368  if (!str32_lower || StrLen(str32_lower) != 1) {
369  delete [] lower;
370  return NULL;
371  }
372  lower[i] = str32_lower[0];
373  } else {
374  lower[i] = ch;
375  }
376  }
377  lower[len] = 0;
378  return lower;
379 }
bool get_isupper(UNICHAR_ID unichar_id) const
Definition: unicharset.h:463
int UNICHAR_ID
Definition: unichar.h:33
UNICHAR_ID get_other_case(UNICHAR_ID unichar_id) const
Definition: unicharset.h:631
static int StrLen(const char_32 *str)
Definition: cube_utils.cpp:54
signed int char_32
Definition: string_32.h:40
#define NULL
Definition: host.h:144
char_32 * tesseract::CubeUtils::ToUpper ( const char_32 str32,
CharSet char_set 
)
static

Definition at line 381 of file cube_utils.cpp.

381  {
382  if (!char_set) {
383  return NULL;
384  }
385  UNICHARSET *unicharset = char_set->InternalUnicharset();
386  int len = StrLen(str32);
387  char_32 *upper = new char_32[len + 1];
388  if (!upper)
389  return NULL;
390  for (int i = 0; i < len; ++i) {
391  char_32 ch = str32[i];
392  if (ch == INVALID_UNICHAR_ID) {
393  delete [] upper;
394  return NULL;
395  }
396  // convert lower-case characters to upper-case
397  if (unicharset->get_islower(char_set->ClassID(ch))) {
398  UNICHAR_ID uid_upper = unicharset->get_other_case(char_set->ClassID(ch));
399  const char_32 *str32_upper = char_set->ClassString(uid_upper);
400  // expect upper-case version of character to be a single character
401  if (!str32_upper || StrLen(str32_upper) != 1) {
402  delete [] upper;
403  return NULL;
404  }
405  upper[i] = str32_upper[0];
406  } else {
407  upper[i] = ch;
408  }
409  }
410  upper[len] = 0;
411  return upper;
412 }
int UNICHAR_ID
Definition: unichar.h:33
bool get_islower(UNICHAR_ID unichar_id) const
Definition: unicharset.h:456
UNICHAR_ID get_other_case(UNICHAR_ID unichar_id) const
Definition: unicharset.h:631
static int StrLen(const char_32 *str)
Definition: cube_utils.cpp:54
signed int char_32
Definition: string_32.h:40
#define NULL
Definition: host.h:144
void tesseract::CubeUtils::UTF32ToUTF8 ( const char_32 utf32_str,
string *  str 
)
static

UTF-8 to UTF-32 conversion functions

Definition at line 282 of file cube_utils.cpp.

282  {
283  str->clear();
284  for (const char_32 *ch_32 = utf32_str; (*ch_32) != 0; ch_32++) {
285  UNICHAR uni_ch((*ch_32));
286  char *utf8 = uni_ch.utf8_str();
287  if (utf8 != NULL) {
288  (*str) += utf8;
289  delete []utf8;
290  }
291  }
292 }
signed int char_32
Definition: string_32.h:40
#define NULL
Definition: host.h:144
void tesseract::CubeUtils::UTF8ToUTF32 ( const char *  utf8_str,
string_32 str32 
)
static

UTF-8 to UTF-32 conversion functions

Definition at line 266 of file cube_utils.cpp.

266  {
267  str32->clear();
268  int len = strlen(utf8_str);
269  int step = 0;
270  for (int ch = 0; ch < len; ch += step) {
271  step = UNICHAR::utf8_step(utf8_str + ch);
272  if (step > 0) {
273  UNICHAR uni_ch(utf8_str + ch, step);
274  (*str32) += uni_ch.first_uni();
275  }
276  }
277 }
static int utf8_step(const char *utf8_str)
Definition: unichar.cpp:134

The documentation for this class was generated from the following files: