Tesseract  3.02
tesseract-ocr/ccstruct/mod128.cpp
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        mod128.c  (Formerly dir128.c)
00003  * Description: Code to convert a DIR128 to an ICOORD.
00004  * Author:                                      Ray Smith
00005  * Created:                                     Tue Oct 22 11:56:09 BST 1991
00006  *
00007  * (C) Copyright 1991, Hewlett-Packard Ltd.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #include          "mfcpch.h"     //precompiled headers
00021 #include          "mod128.h"
00022 
00023 const inT16 idirtab[] = {
00024   1000, 0, 998, 49, 995, 98, 989, 146,
00025   980, 195, 970, 242, 956, 290, 941, 336,
00026   923, 382, 903, 427, 881, 471, 857, 514,
00027   831, 555, 803, 595, 773, 634, 740, 671,
00028   707, 707, 671, 740, 634, 773, 595, 803,
00029   555, 831, 514, 857, 471, 881, 427, 903,
00030   382, 923, 336, 941, 290, 956, 242, 970,
00031   195, 980, 146, 989, 98, 995, 49, 998,
00032   0, 1000, -49, 998, -98, 995, -146, 989,
00033   -195, 980, -242, 970, -290, 956, -336, 941,
00034   -382, 923, -427, 903, -471, 881, -514, 857,
00035   -555, 831, -595, 803, -634, 773, -671, 740,
00036   -707, 707, -740, 671, -773, 634, -803, 595,
00037   -831, 555, -857, 514, -881, 471, -903, 427,
00038   -923, 382, -941, 336, -956, 290, -970, 242,
00039   -980, 195, -989, 146, -995, 98, -998, 49,
00040   -1000, 0, -998, -49, -995, -98, -989, -146,
00041   -980, -195, -970, -242, -956, -290, -941, -336,
00042   -923, -382, -903, -427, -881, -471, -857, -514,
00043   -831, -555, -803, -595, -773, -634, -740, -671,
00044   -707, -707, -671, -740, -634, -773, -595, -803,
00045   -555, -831, -514, -857, -471, -881, -427, -903,
00046   -382, -923, -336, -941, -290, -956, -242, -970,
00047   -195, -980, -146, -989, -98, -995, -49, -998,
00048   0, -1000, 49, -998, 98, -995, 146, -989,
00049   195, -980, 242, -970, 290, -956, 336, -941,
00050   382, -923, 427, -903, 471, -881, 514, -857,
00051   555, -831, 595, -803, 634, -773, 671, -740,
00052   707, -707, 740, -671, 773, -634, 803, -595,
00053   831, -555, 857, -514, 881, -471, 903, -427,
00054   923, -382, 941, -336, 956, -290, 970, -242,
00055   980, -195, 989, -146, 995, -98, 998, -49
00056 };
00057 
00058 const ICOORD *dirtab = (ICOORD *) idirtab;
00059 
00060 /**********************************************************************
00061  * DIR128::DIR128
00062  *
00063  * Quantize the direction of an FCOORD to make a DIR128.
00064  **********************************************************************/
00065 
00066 DIR128::DIR128(                 //from fcoord
00067                const FCOORD fc  //vector to quantize
00068               ) {
00069   int high, low, current;        //binary search
00070 
00071   low = 0;
00072   if (fc.y () == 0) {
00073     if (fc.x () >= 0)
00074       dir = 0;
00075     else
00076       dir = MODULUS / 2;
00077     return;
00078   }
00079   high = MODULUS;
00080   do {
00081     current = (high + low) / 2;
00082     if (dirtab[current] * fc >= 0)
00083       low = current;
00084     else
00085       high = current;
00086   }
00087   while (high - low > 1);
00088   dir = low;
00089 }
00090 
00091 
00092 /**********************************************************************
00093  * dir_to_gradient
00094  *
00095  * Convert a direction to a vector.
00096  **********************************************************************/
00097 
00098 ICOORD DIR128::vector() const {  //convert to vector
00099   return dirtab[dir];            //easy really
00100 }