comparison mupdf-source/thirdparty/tesseract/src/ccstruct/matrix.cpp @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /******************************************************************************
2 *
3 * File: matrix.cpp (Formerly matrix.c)
4 * Description: Ratings matrix code. (Used by associator)
5 * Author: Mark Seaman, OCR Technology
6 *
7 * (c) Copyright 1990, Hewlett-Packard Company.
8 ** Licensed under the Apache License, Version 2.0 (the "License");
9 ** you may not use this file except in compliance with the License.
10 ** You may obtain a copy of the License at
11 ** http://www.apache.org/licenses/LICENSE-2.0
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 *
18 *****************************************************************************/
19 /*----------------------------------------------------------------------
20 I n c l u d e s
21 ----------------------------------------------------------------------*/
22 #include "matrix.h"
23
24 #include "ratngs.h"
25 #include "tprintf.h"
26 #include "unicharset.h"
27
28 namespace tesseract {
29
30 // Destructor.
31 // It is defined here, so the compiler can create a single vtable
32 // instead of weak vtables in every compilation unit.
33 MATRIX::~MATRIX() = default;
34
35 // Returns true if there are any real classification results.
36 bool MATRIX::Classified(int col, int row, int wildcard_id) const {
37 if (get(col, row) == NOT_CLASSIFIED) {
38 return false;
39 }
40 BLOB_CHOICE_IT b_it(get(col, row));
41 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
42 BLOB_CHOICE *choice = b_it.data();
43 if (choice->IsClassified()) {
44 return true;
45 }
46 }
47 return false;
48 }
49
50 // Expands the existing matrix in-place to make the band wider, without
51 // losing any existing data.
52 void MATRIX::IncreaseBandSize(int bandwidth) {
53 ResizeWithCopy(dimension(), bandwidth);
54 }
55
56 // Returns a bigger MATRIX with a new column and row in the matrix in order
57 // to split the blob at the given (ind,ind) diagonal location.
58 // Entries are relocated to the new MATRIX using the transformation defined
59 // by MATRIX_COORD::MapForSplit.
60 // Transfers the pointer data to the new MATRIX and deletes *this.
61 MATRIX *MATRIX::ConsumeAndMakeBigger(int ind) {
62 int dim = dimension();
63 int band_width = bandwidth();
64 // Check to see if bandwidth needs expanding.
65 for (int col = ind; col >= 0 && col > ind - band_width; --col) {
66 if (array_[col * band_width + band_width - 1] != empty_) {
67 ++band_width;
68 break;
69 }
70 }
71 auto *result = new MATRIX(dim + 1, band_width);
72
73 for (int col = 0; col < dim; ++col) {
74 for (int row = col; row < dim && row < col + bandwidth(); ++row) {
75 MATRIX_COORD coord(col, row);
76 coord.MapForSplit(ind);
77 BLOB_CHOICE_LIST *choices = get(col, row);
78 if (choices != nullptr) {
79 // Correct matrix location on each choice.
80 BLOB_CHOICE_IT bc_it(choices);
81 for (bc_it.mark_cycle_pt(); !bc_it.cycled_list(); bc_it.forward()) {
82 BLOB_CHOICE *choice = bc_it.data();
83 choice->set_matrix_cell(coord.col, coord.row);
84 }
85 ASSERT_HOST(coord.Valid(*result));
86 result->put(coord.col, coord.row, choices);
87 }
88 }
89 }
90 delete this;
91 return result;
92 }
93
94 // Makes and returns a deep copy of *this, including all the BLOB_CHOICEs
95 // on the lists, but not any LanguageModelState that may be attached to the
96 // BLOB_CHOICEs.
97 MATRIX *MATRIX::DeepCopy() const {
98 int dim = dimension();
99 int band_width = bandwidth();
100 auto *result = new MATRIX(dim, band_width);
101 for (int col = 0; col < dim; ++col) {
102 for (int row = col; row < dim && row < col + band_width; ++row) {
103 BLOB_CHOICE_LIST *choices = get(col, row);
104 if (choices != nullptr) {
105 auto *copy_choices = new BLOB_CHOICE_LIST;
106 copy_choices->deep_copy(choices, &BLOB_CHOICE::deep_copy);
107 result->put(col, row, copy_choices);
108 }
109 }
110 }
111 return result;
112 }
113
114 // Print the best guesses out of the match rating matrix.
115 void MATRIX::print(const UNICHARSET &unicharset) const {
116 tprintf("Ratings Matrix (top 3 choices)\n");
117 int dim = dimension();
118 int band_width = bandwidth();
119 int row, col;
120 for (col = 0; col < dim; ++col) {
121 for (row = col; row < dim && row < col + band_width; ++row) {
122 BLOB_CHOICE_LIST *rating = this->get(col, row);
123 if (rating == NOT_CLASSIFIED) {
124 continue;
125 }
126 BLOB_CHOICE_IT b_it(rating);
127 tprintf("col=%d row=%d ", col, row);
128 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
129 tprintf("%s rat=%g cert=%g ", unicharset.id_to_unichar(b_it.data()->unichar_id()),
130 b_it.data()->rating(), b_it.data()->certainty());
131 }
132 tprintf("\n");
133 }
134 tprintf("\n");
135 }
136 tprintf("\n");
137 for (col = 0; col < dim; ++col) {
138 tprintf("\t%d", col);
139 }
140 tprintf("\n");
141 for (row = 0; row < dim; ++row) {
142 for (col = 0; col <= row; ++col) {
143 if (col == 0) {
144 tprintf("%d\t", row);
145 }
146 if (row >= col + band_width) {
147 tprintf(" \t");
148 continue;
149 }
150 BLOB_CHOICE_LIST *rating = this->get(col, row);
151 if (rating != NOT_CLASSIFIED) {
152 BLOB_CHOICE_IT b_it(rating);
153 int counter = 0;
154 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
155 tprintf("%s ", unicharset.id_to_unichar(b_it.data()->unichar_id()));
156 ++counter;
157 if (counter == 3) {
158 break;
159 }
160 }
161 tprintf("\t");
162 } else {
163 tprintf(" \t");
164 }
165 }
166 tprintf("\n");
167 }
168 }
169
170 } // namespace tesseract