Tesseract  3.02
tesseract-ocr/cube/char_altlist.cpp
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        char_altlist.cpp
00003  * Description: Implementation of a Character Alternate List Class
00004  * Author:    Ahmad Abdulkader
00005  * Created:   2007
00006  *
00007  * (C) Copyright 2008, Google Inc.
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 "char_altlist.h"
00021 
00022 namespace tesseract {
00023 
00024 // The CharSet is not class owned and must exist for
00025 // the life time of this class
00026 CharAltList::CharAltList(const CharSet *char_set, int max_alt)
00027     : AltList(max_alt) {
00028   char_set_ = char_set;
00029   max_alt_ = max_alt;
00030   class_id_alt_ = NULL;
00031   class_id_cost_ = NULL;
00032 }
00033 
00034 CharAltList::~CharAltList() {
00035   if (class_id_alt_ != NULL) {
00036     delete []class_id_alt_;
00037     class_id_alt_ = NULL;
00038   }
00039 
00040   if (class_id_cost_ != NULL) {
00041     delete []class_id_cost_;
00042     class_id_cost_ = NULL;
00043   }
00044 }
00045 
00046 // Insert a new char alternate
00047 bool CharAltList::Insert(int class_id, int cost, void *tag) {
00048   // validate class ID
00049   if (class_id < 0 || class_id >= char_set_->ClassCount()) {
00050     return false;
00051   }
00052 
00053   // allocate buffers if nedded
00054   if (class_id_alt_ == NULL || alt_cost_ == NULL) {
00055     class_id_alt_ = new int[max_alt_];
00056     alt_cost_ = new int[max_alt_];
00057     alt_tag_ = new void *[max_alt_];
00058 
00059     if (class_id_alt_ == NULL || alt_cost_ == NULL || alt_tag_ == NULL) {
00060       return false;
00061     }
00062 
00063     memset(alt_tag_, 0, max_alt_ * sizeof(*alt_tag_));
00064   }
00065 
00066   if (class_id_cost_ == NULL) {
00067     int class_cnt = char_set_->ClassCount();
00068 
00069     class_id_cost_ = new int[class_cnt];
00070     if (class_id_cost_ == NULL) {
00071       return false;
00072     }
00073 
00074     for (int ich = 0; ich < class_cnt; ich++) {
00075       class_id_cost_[ich] = WORST_COST;
00076     }
00077   }
00078 
00079   if (class_id < 0 || class_id >= char_set_->ClassCount()) {
00080     return false;
00081   }
00082 
00083   // insert the alternate
00084   class_id_alt_[alt_cnt_] = class_id;
00085   alt_cost_[alt_cnt_] = cost;
00086   alt_tag_[alt_cnt_] = tag;
00087 
00088   alt_cnt_++;
00089 
00090   class_id_cost_[class_id] = cost;
00091 
00092   return true;
00093 }
00094 
00095 // sort the alternate Desc. based on prob
00096 void CharAltList::Sort() {
00097   for (int alt_idx = 0; alt_idx < alt_cnt_; alt_idx++) {
00098     for (int alt = alt_idx + 1; alt < alt_cnt_; alt++) {
00099       if (alt_cost_[alt_idx] > alt_cost_[alt]) {
00100         int temp = class_id_alt_[alt_idx];
00101         class_id_alt_[alt_idx] = class_id_alt_[alt];
00102         class_id_alt_[alt] = temp;
00103 
00104         temp = alt_cost_[alt_idx];
00105         alt_cost_[alt_idx] = alt_cost_[alt];
00106         alt_cost_[alt] = temp;
00107 
00108         void *tag = alt_tag_[alt_idx];
00109         alt_tag_[alt_idx] = alt_tag_[alt];
00110         alt_tag_[alt] = tag;
00111       }
00112     }
00113   }
00114 }
00115 }