2 * Copyright 2009 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.oned.rss;
19 /** Adapted from listings in ISO/IEC 24724 Appendix B and Appendix G. */
20 public final class RSSUtils {
24 static int[] getRSSwidths(int val, int n, int elements, int maxWidth, boolean noNarrow) {
25 int[] widths = new int[elements];
28 for (bar = 0; bar < elements - 1; bar++) {
29 narrowMask |= (1 << bar);
33 subVal = combins(n - elmWidth - 1, elements - bar - 2);
34 if (noNarrow && (narrowMask == 0) &&
35 (n - elmWidth - (elements - bar - 1) >= elements - bar - 1)) {
36 subVal -= combins(n - elmWidth - (elements - bar), elements - bar - 2);
38 if (elements - bar - 1 > 1) {
40 for (int mxwElement = n - elmWidth - (elements - bar - 2);
41 mxwElement > maxWidth;
43 lessVal += combins(n - elmWidth - mxwElement - 1, elements - bar - 3);
45 subVal -= lessVal * (elements - 1 - bar);
46 } else if (n - elmWidth > maxWidth) {
54 narrowMask &= ~(1 << bar);
58 widths[bar] = elmWidth;
64 public static int getRSSvalue(int[] widths, int maxWidth, boolean noNarrow) {
65 int elements = widths.length;
67 for (int i = 0; i < elements; i++) {
72 for (int bar = 0; bar < elements - 1; bar++) {
74 for (elmWidth = 1, narrowMask |= (1 << bar);
75 elmWidth < widths[bar];
76 elmWidth++, narrowMask &= ~(1 << bar)) {
77 int subVal = combins(n - elmWidth - 1, elements - bar - 2);
78 if (noNarrow && (narrowMask == 0) &&
79 (n - elmWidth - (elements - bar - 1) >= elements - bar - 1)) {
80 subVal -= combins(n - elmWidth - (elements - bar),
83 if (elements - bar - 1 > 1) {
85 for (int mxwElement = n - elmWidth - (elements - bar - 2);
86 mxwElement > maxWidth; mxwElement--) {
87 lessVal += combins(n - elmWidth - mxwElement - 1,
90 subVal -= lessVal * (elements - 1 - bar);
91 } else if (n - elmWidth > maxWidth) {
101 static int combins(int n, int r) {
102 int maxDenom, minDenom;
112 for (int i = n; i > maxDenom; i--) {
119 while (j <= minDenom) {
126 static int[] elements(int[] eDist, int N, int K) {
127 int[] widths = new int[eDist.length + 2];
133 for (i = 1; i < twoK - 2; i += 2) {
134 widths[i] = eDist[i - 1] - widths[i - 1];
135 widths[i + 1] = eDist[i] - widths[i];
136 barSum += widths[i] + widths[i + 1];
137 if (widths[i] < minEven) {
141 widths[twoK - 1] = N - barSum;
142 if (widths[twoK - 1] < minEven) {
143 minEven = widths[twoK - 1];
146 for (i = 0; i < twoK; i += 2) {
147 widths[i] += minEven - 1;
148 widths[i + 1] -= minEven - 1;