File: common.cc

package info (click to toggle)
ocrad 0.16-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 588 kB
  • ctags: 686
  • sloc: cpp: 7,741; makefile: 151; sh: 88
file content (161 lines) | stat: -rw-r--r-- 4,720 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*  GNU Ocrad - Optical Character Recognition program
    Copyright (C) 2003, 2004, 2005, 2006 Antonio Diaz Diaz.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include "common.h"


namespace {

const int charsets = 3;

const Charset::Value charset_value[charsets] =
  { Charset::ascii, Charset::iso_8859_9, Charset::iso_8859_15 };

const char * const charset_name[charsets] =
  { "ascii", "iso-8859-9", "iso-8859-15" };

struct F_entry
  {
  const char * name;
  Filter::Type type;
  };

const F_entry F_table[] =
  {
  { "none",         Filter::none },
  { "letters",      Filter::letters },
  { "letters_only", Filter::letters_only },
  { "numbers",      Filter::numbers },
  { "numbers_only", Filter::numbers_only },
  { 0, Filter::none }
  };

struct T_entry
  {
  const char * name;
  Transformation::Type type;
  };

const T_entry T_table[] =
  {
  { "none",      Transformation::none },
  { "rotate90",  Transformation::rotate90 },
  { "rotate180", Transformation::rotate180 },
  { "rotate270", Transformation::rotate270 },
  { "mirror_lr", Transformation::mirror_lr },
  { "mirror_tb", Transformation::mirror_tb },
  { "mirror_d1", Transformation::mirror_d1 },
  { "mirror_d2", Transformation::mirror_d2 },
  { 0, Transformation::none }
  };

} // end namespace


bool Ocrad::similar( int a, int b, int percent_dif, int abs_dif ) throw()
  {
  int difference = std::abs( a - b );
  if( percent_dif > 0 && difference <= abs_dif ) return true;
  int max_abs = std::max( std::abs(a), std::abs(b) );
  return ( difference * 100 <= max_abs * percent_dif );
  }


bool Charset::enable( const char * name ) throw()
  {
  for( int i = 0; i < charsets; ++i )
    if( std::strcmp( name, charset_name[i] ) == 0 )
      { _charset |= charset_value[i]; return true; }
  return false;
  }


bool Charset::enabled( Value cset ) const throw()
  {
  if( !_charset ) return cset == iso_8859_15;		// default charset
  return _charset & cset;
  }


bool Charset::only( Value cset ) const throw()
  {
  if( !_charset ) return cset == iso_8859_15;		// default charset
  return _charset == cset;
  }


void Charset::show_error( const char * program_name, const char * arg ) const throw()
  {
  if( arg && std::strcmp( arg, "help" ) )
    std::fprintf( stderr,"%s: bad charset `%s'\n", program_name, arg );
  std::fputs( "Valid charset names are:", stderr );
  for( int i = 0; i < charsets; ++i )
    std::fprintf( stderr, "  %s", charset_name[i] );
  std::fputs( "\n", stderr );
  }


bool Filter::set( const char * name ) throw()
  {
  for( int i = 0; F_table[i].name != 0; ++i )
    if( std::strcmp( name, F_table[i].name ) == 0 )
      { _type = F_table[i].type; return true; }
  return false;
  }


void Filter::show_error( const char * program_name, const char * arg ) const throw()
  {
  if( arg && std::strcmp( arg, "help" ) )
    std::fprintf( stderr,"%s: bad filter `%s'\n", program_name, arg );
  std::fputs( "Valid filter names are:", stderr );
  for( int i = 0; F_table[i].name != 0; ++i )
    std::fprintf( stderr, "  %s", F_table[i].name );
  std::fputs( "\n", stderr );
  }


bool Transformation::set( const char * name ) throw()
  {
  for( int i = 0; T_table[i].name != 0; ++i )
    if( std::strcmp( name, T_table[i].name ) == 0 )
      { _type = T_table[i].type; return true; }
  return false;
  }


void Transformation::show_error( const char * program_name, const char * arg ) const throw()
  {
  if( arg && std::strcmp( arg, "help" ) )
    std::fprintf( stderr,"%s: bad bitmap trasformation `%s'\n", program_name, arg );
  std::fputs( "Valid transformation names are:", stderr );
  for( int i = 0; T_table[i].name != 0; ++i )
    std::fprintf( stderr, "  %s", T_table[i].name );
  std::fputs( "\nRotations are made counter-clockwise.\n", stderr );
  }


bool Control::set_format( const char * name ) throw()
  {
  if( std::strcmp( name, "byte" ) == 0 ) { format = byte; return true; }
  if( std::strcmp( name, "utf8" ) == 0 ) { format = utf8; return true; }
  return false;
  }