/usr/include/tesseract/matrix.h is in libtesseract-dev 3.02.01-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | /* -*-C-*-
********************************************************************************
*
* File: matrix.h (Formerly matrix.h)
* Description: Ratings matrix code. (Used by associator)
* Author: Mark Seaman, OCR Technology
* Created: Wed May 16 13:22:06 1990
* Modified: Tue Mar 19 16:00:20 1991 (Mark Seaman) marks@hpgrlt
* Language: C
* Package: N/A
* Status: Experimental (Do Not Distribute)
*
* (c) Copyright 1990, Hewlett-Packard Company.
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
** http://www.apache.org/licenses/LICENSE-2.0
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*
*********************************************************************************/
#ifndef TESSERACT_CCSTRUCT_MATRIX_H__
#define TESSERACT_CCSTRUCT_MATRIX_H__
#include "ratngs.h"
#include "unicharset.h"
#define NOT_CLASSIFIED reinterpret_cast<BLOB_CHOICE_LIST*>(NULL)
// A generic class to store a matrix with entries of type T.
template <class T>
class GENERIC_2D_ARRAY {
public:
// Allocate a piece of memory to hold a 2d-array of the given dimension.
// Initialize all the elements of the array to empty instead of assuming
// that a default constructor can be used.
GENERIC_2D_ARRAY(int dim1, int dim2, const T& empty)
: empty_(empty), dim1_(dim1), dim2_(dim2) {
array_ = new T[dim1_ * dim2_];
for (int x = 0; x < dim1_; x++)
for (int y = 0; y < dim2_; y++)
this->put(x, y, empty_);
}
~GENERIC_2D_ARRAY() { delete[] array_; }
// Writes to the given file. Returns false in case of error.
// Only works with bitwise-serializeable types!
bool Serialize(FILE* fp) const {
if (!SerializeSize(fp)) return false;
if (fwrite(&empty_, sizeof(empty_), 1, fp) != 1) return false;
int size = dim1_ * dim2_;
if (fwrite(array_, sizeof(*array_), size, fp) != size) return false;
return true;
}
// Reads from the given file. Returns false in case of error.
// Only works with bitwise-serializeable types!
// If swap is true, assumes a big/little-endian swap is needed.
bool DeSerialize(bool swap, FILE* fp) {
if (!DeSerializeSize(swap, fp)) return false;
if (fread(&empty_, sizeof(empty_), 1, fp) != 1) return false;
if (swap) ReverseN(&empty_, sizeof(empty_));
int size = dim1_ * dim2_;
if (fread(array_, sizeof(*array_), size, fp) != size) return false;
if (swap) {
for (int i = 0; i < size; ++i)
ReverseN(&array_[i], sizeof(array_[i]));
}
return true;
}
// Writes to the given file. Returns false in case of error.
// Assumes a T::Serialize(FILE*) const function.
bool SerializeClasses(FILE* fp) const {
if (!SerializeSize(fp)) return false;
if (!empty_.Serialize(fp)) return false;
int size = dim1_ * dim2_;
for (int i = 0; i < size; ++i) {
if (!array_[i].Serialize(fp)) return false;
}
return true;
}
// Reads from the given file. Returns false in case of error.
// Assumes a T::DeSerialize(bool swap, FILE*) function.
// If swap is true, assumes a big/little-endian swap is needed.
bool DeSerializeClasses(bool swap, FILE* fp) {
if (!DeSerializeSize(swap, fp)) return false;
if (!empty_.DeSerialize(swap, fp)) return false;
int size = dim1_ * dim2_;
for (int i = 0; i < size; ++i) {
if (!array_[i].DeSerialize(swap, fp)) return false;
}
return true;
}
// Provide the dimensions of this rectangular matrix.
int dim1() const { return dim1_; }
int dim2() const { return dim2_; }
// Expression to select a specific location in the matrix. The matrix is
// stored COLUMN-major, so the left-most index is the most significant.
// This allows [][] access to use indices in the same order as (,).
int index(int column, int row) const {
return (column * dim2_ + row);
}
// Put a list element into the matrix at a specific location.
void put(int column, int row, const T& thing) {
array_[this->index(column, row)] = thing;
}
// Get the item at a specified location from the matrix.
T get(int column, int row) const {
return array_[this->index(column, row)];
}
// Return a reference to the element at the specified location.
const T& operator()(int column, int row) const {
return array_[this->index(column, row)];
}
T& operator()(int column, int row) {
return array_[this->index(column, row)];
}
// Allow access using array[column][row]. NOTE that the indices are
// in the same left-to-right order as the () indexing.
T* operator[](int column) {
return &array_[this->index(column, 0)];
}
// Delete objects pointed to by array_[i].
void delete_matrix_pointers() {
for (int x = 0; x < dim1_; x++) {
for (int y = 0; y < dim2_; y++) {
T matrix_cell = this->get(x, y);
if (matrix_cell != empty_)
delete matrix_cell;
}
}
}
private:
// Factored helper to serialize the size.
bool SerializeSize(FILE* fp) const {
inT32 size = dim1_;
if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
size = dim2_;
if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
return true;
}
// Factored helper to deserialize the size.
// If swap is true, assumes a big/little-endian swap is needed.
bool DeSerializeSize(bool swap, FILE* fp) {
inT32 size1, size2;
if (fread(&size1, sizeof(size1), 1, fp) != 1) return false;
if (fread(&size2, sizeof(size2), 1, fp) != 1) return false;
if (swap) {
ReverseN(&size1, sizeof(size1));
ReverseN(&size2, sizeof(size2));
}
if (size1 != dim1_ || size2 != dim2_) {
dim1_ = size1;
dim2_ = size2;
delete [] array_;
array_ = new T[dim1_ * dim2_];
}
return true;
}
T* array_;
T empty_; // The unused cell.
int dim1_; // Size of the 1st dimension in indexing functions.
int dim2_; // Size of the 2nd dimension in indexing functions.
};
// A generic class to store a square matrix with entries of type T.
template <class T>
class GENERIC_MATRIX : public GENERIC_2D_ARRAY<T> {
public:
// Allocate a piece of memory to hold a matrix of the given dimension.
// Initialize all the elements of the matrix to empty instead of assuming
// that a default constructor can be used.
GENERIC_MATRIX(int dimension, const T& empty)
: GENERIC_2D_ARRAY<T>(dimension, dimension, empty) {
}
// Provide the dimension of this square matrix.
int dimension() const { return this->dim1(); }
};
class MATRIX : public GENERIC_MATRIX<BLOB_CHOICE_LIST *> {
public:
MATRIX(int dimension) : GENERIC_MATRIX<BLOB_CHOICE_LIST *>(dimension,
NOT_CLASSIFIED) {}
// Print a shortened version of the contents of the matrix.
void print(const UNICHARSET &unicharset) const;
};
struct MATRIX_COORD {
static void Delete(void *arg) {
MATRIX_COORD *c = static_cast<MATRIX_COORD *>(arg);
delete c;
}
MATRIX_COORD(int c, int r): col(c), row(r) {}
~MATRIX_COORD() {}
bool Valid(const MATRIX &m) const {
return (col >= 0 && row >= 0 &&
col < m.dimension() && row < m.dimension());
}
int col;
int row;
};
#endif // TESSERACT_CCSTRUCT_MATRIX_H__
|