OLD | NEW |
| (Empty) |
1 // Copyright 2014 PDFium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 // Original code is licensed as follows: | |
7 /* | |
8 * Copyright 2009 ZXing authors | |
9 * | |
10 * Licensed under the Apache License, Version 2.0 (the "License"); | |
11 * you may not use this file except in compliance with the License. | |
12 * You may obtain a copy of the License at | |
13 * | |
14 * http://www.apache.org/licenses/LICENSE-2.0 | |
15 * | |
16 * Unless required by applicable law or agreed to in writing, software | |
17 * distributed under the License is distributed on an "AS IS" BASIS, | |
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
19 * See the License for the specific language governing permissions and | |
20 * limitations under the License. | |
21 */ | |
22 | |
23 #include "xfa/src/fxbarcode/BC_UtilRSS.h" | |
24 | |
25 #include <memory> | |
26 | |
27 #include "core/include/fxcrt/fx_basic.h" | |
28 #include "xfa/src/fxbarcode/utils.h" | |
29 | |
30 CBC_UtilRSS::CBC_UtilRSS() {} | |
31 CBC_UtilRSS::~CBC_UtilRSS() {} | |
32 CFX_Int32Array* CBC_UtilRSS::GetRssWidths(int32_t val, | |
33 int32_t n, | |
34 int32_t elements, | |
35 int32_t maxWidth, | |
36 FX_BOOL noNarrow) { | |
37 std::unique_ptr<CFX_Int32Array> widths(new CFX_Int32Array); | |
38 widths->SetSize(elements); | |
39 int32_t bar; | |
40 int32_t narrowMask = 0; | |
41 for (bar = 0; bar < elements - 1; bar++) { | |
42 narrowMask |= (1 << bar); | |
43 int32_t elmWidth = 1; | |
44 int32_t subVal; | |
45 while (TRUE) { | |
46 subVal = Combins(n - elmWidth - 1, elements - bar - 2); | |
47 if (noNarrow && (narrowMask == 0) && | |
48 (n - elmWidth - (elements - bar - 1) >= elements - bar - 1)) { | |
49 subVal -= Combins(n - elmWidth - (elements - bar), elements - bar - 2); | |
50 } | |
51 if (elements - bar - 1 > 1) { | |
52 int32_t lessVal = 0; | |
53 for (int32_t mxwElement = n - elmWidth - (elements - bar - 2); | |
54 mxwElement > maxWidth; mxwElement--) { | |
55 lessVal += Combins(n - elmWidth - mxwElement - 1, elements - bar - 3); | |
56 } | |
57 subVal -= lessVal * (elements - 1 - bar); | |
58 } else if (n - elmWidth > maxWidth) { | |
59 subVal--; | |
60 } | |
61 val -= subVal; | |
62 if (val < 0) { | |
63 break; | |
64 } | |
65 elmWidth++; | |
66 narrowMask &= ~(1 << bar); | |
67 } | |
68 val += subVal; | |
69 n -= elmWidth; | |
70 (*widths)[bar] = elmWidth; | |
71 } | |
72 (*widths)[bar] = n; | |
73 return widths.release(); | |
74 } | |
75 int32_t CBC_UtilRSS::GetRSSvalue(CFX_Int32Array& widths, | |
76 int32_t maxWidth, | |
77 FX_BOOL noNarrow) { | |
78 int32_t elements = widths.GetSize(); | |
79 int32_t n = 0; | |
80 for (int32_t i = 0; i < elements; i++) { | |
81 n += widths[i]; | |
82 } | |
83 int32_t val = 0; | |
84 int32_t narrowMask = 0; | |
85 for (int32_t bar = 0; bar < elements - 1; bar++) { | |
86 int32_t elmWidth; | |
87 for (elmWidth = 1, narrowMask |= (1 << bar); elmWidth < widths[bar]; | |
88 elmWidth++, narrowMask &= ~(1 << bar)) { | |
89 int32_t subVal = Combins(n - elmWidth - 1, elements - bar - 2); | |
90 if (noNarrow && (narrowMask == 0) && | |
91 (n - elmWidth - (elements - bar - 1) >= elements - bar - 1)) { | |
92 subVal -= Combins(n - elmWidth - (elements - bar), elements - bar - 2); | |
93 } | |
94 if (elements - bar - 1 > 1) { | |
95 int32_t lessVal = 0; | |
96 for (int32_t mxwElement = n - elmWidth - (elements - bar - 2); | |
97 mxwElement > maxWidth; mxwElement--) { | |
98 lessVal += Combins(n - elmWidth - mxwElement - 1, elements - bar - 3); | |
99 } | |
100 subVal -= lessVal * (elements - 1 - bar); | |
101 } else if (n - elmWidth > maxWidth) { | |
102 subVal--; | |
103 } | |
104 val += subVal; | |
105 } | |
106 n -= elmWidth; | |
107 } | |
108 return val; | |
109 } | |
110 int32_t CBC_UtilRSS::Combins(int32_t n, int32_t r) { | |
111 int32_t maxDenom; | |
112 int32_t minDenom; | |
113 if (n - r > r) { | |
114 minDenom = r; | |
115 maxDenom = n - r; | |
116 } else { | |
117 minDenom = n - r; | |
118 maxDenom = r; | |
119 } | |
120 int32_t val = 1; | |
121 int32_t j = 1; | |
122 for (int32_t i = n; i > maxDenom; i--) { | |
123 val *= i; | |
124 if (j <= minDenom) { | |
125 val /= j; | |
126 j++; | |
127 } | |
128 } | |
129 while (j <= minDenom) { | |
130 val /= j; | |
131 j++; | |
132 } | |
133 return val; | |
134 } | |
135 CFX_Int32Array* CBC_UtilRSS::Elements(CFX_Int32Array& eDist, | |
136 int32_t N, | |
137 int32_t K) { | |
138 CFX_Int32Array* widths = new CFX_Int32Array; | |
139 widths->SetSize(eDist.GetSize() + 2); | |
140 int32_t twoK = K << 1; | |
141 (*widths)[0] = 1; | |
142 int32_t i; | |
143 int32_t minEven = 10; | |
144 int32_t barSum = 1; | |
145 for (i = 1; i < twoK - 2; i += 2) { | |
146 (*widths)[i] = eDist[i - 1] - (*widths)[i - 1]; | |
147 (*widths)[i + 1] = eDist[i] - (*widths)[i]; | |
148 barSum += (*widths)[i] + (*widths)[i + 1]; | |
149 if ((*widths)[i] < minEven) { | |
150 minEven = (*widths)[i]; | |
151 } | |
152 } | |
153 (*widths)[twoK - 1] = N - barSum; | |
154 if ((*widths)[twoK - 1] < minEven) { | |
155 minEven = (*widths)[twoK - 1]; | |
156 } | |
157 if (minEven > 1) { | |
158 for (i = 0; i < twoK; i += 2) { | |
159 (*widths)[i] += minEven - 1; | |
160 (*widths)[i + 1] -= minEven - 1; | |
161 } | |
162 } | |
163 return widths; | |
164 } | |
OLD | NEW |