Adjust formatting on last change. Simplify GridSampler
[zxing.git] / core / src / com / google / zxing / qrcode / encoder / QRCode.java
1 /*
2  * Copyright 2008 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.qrcode.encoder;
18
19 import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
20 import com.google.zxing.qrcode.decoder.Mode;
21
22 /**
23  * @author satorux@google.com (Satoru Takabayashi) - creator
24  * @author dswitkin@google.com (Daniel Switkin) - ported from C++
25  */
26 public final class QRCode {
27
28   public static final int NUM_MASK_PATTERNS = 8;
29
30   private Mode mode;
31   private ErrorCorrectionLevel ecLevel;
32   private int version;
33   private int matrixWidth;
34   private int maskPattern;
35   private int numTotalBytes;
36   private int numDataBytes;
37   private int numECBytes;
38   private int numRSBlocks;
39   private ByteMatrix matrix;
40
41   public QRCode() {
42     mode = null;
43     ecLevel = null;
44     version = -1;
45     matrixWidth = -1;
46     maskPattern = -1;
47     numTotalBytes = -1;
48     numDataBytes = -1;
49     numECBytes = -1;
50     numRSBlocks = -1;
51     matrix = null;
52   }
53
54   // Mode of the QR Code.
55   public Mode getMode() {
56     return mode;
57   }
58
59   // Error correction level of the QR Code.
60   public ErrorCorrectionLevel getECLevel() {
61     return ecLevel;
62   }
63
64   // Version of the QR Code.  The bigger size, the bigger version.
65   public int getVersion() {
66     return version;
67   }
68
69   // ByteMatrix width of the QR Code.
70   public int getMatrixWidth() {
71     return matrixWidth;
72   }
73
74   // Mask pattern of the QR Code.
75   public int getMaskPattern() {
76     return maskPattern;
77   }
78
79   // Number of total bytes in the QR Code.
80   public int getNumTotalBytes() {
81     return numTotalBytes;
82   }
83
84   // Number of data bytes in the QR Code.
85   public int getNumDataBytes() {
86     return numDataBytes;
87   }
88
89   // Number of error correction bytes in the QR Code.
90   public int getNumECBytes() {
91     return numECBytes;
92   }
93
94   // Number of Reedsolomon blocks in the QR Code.
95   public int getNumRSBlocks() {
96     return numRSBlocks;
97   }
98
99   // ByteMatrix data of the QR Code.
100   public ByteMatrix getMatrix() {
101     return matrix;
102   }
103   
104
105   // Return the value of the module (cell) pointed by "x" and "y" in the matrix of the QR Code. They
106   // call cells in the matrix "modules". 1 represents a black cell, and 0 represents a white cell.
107   public int at(int x, int y) {
108     // The value must be zero or one.
109     int value = matrix.get(x, y);
110     if (!(value == 0 || value == 1)) {
111       // this is really like an assert... not sure what better exception to use?
112       throw new RuntimeException("Bad value");
113     }
114     return value;
115   }
116
117   // Checks all the member variables are set properly. Returns true on success. Otherwise, returns
118   // false.
119   public boolean isValid() {
120     return
121         // First check if all version are not uninitialized.
122         mode != null &&
123         ecLevel != null &&
124         version != -1 &&
125         matrixWidth != -1 &&
126         maskPattern != -1 &&
127         numTotalBytes != -1 &&
128         numDataBytes != -1 &&
129         numECBytes != -1 &&
130         numRSBlocks != -1 &&
131         // Then check them in other ways..
132         isValidMaskPattern(maskPattern) &&
133         numTotalBytes == numDataBytes + numECBytes &&
134         // ByteMatrix stuff.
135         matrix != null &&
136         matrixWidth == matrix.getWidth() &&
137         // See 7.3.1 of JISX0510:2004 (p.5).
138         matrix.getWidth() == matrix.getHeight(); // Must be square.
139   }
140
141   // Return debug String.
142   public String toString() {
143     StringBuffer result = new StringBuffer(200);
144     result.append("<<\n");
145     result.append(" mode: ");
146     result.append(mode);
147     result.append("\n ecLevel: ");
148     result.append(ecLevel);
149     result.append("\n version: ");
150     result.append(version);
151     result.append("\n matrixWidth: ");
152     result.append(matrixWidth);
153     result.append("\n maskPattern: ");
154     result.append(maskPattern);
155     result.append("\n numTotalBytes: ");
156     result.append(numTotalBytes);
157     result.append("\n numDataBytes: ");
158     result.append(numDataBytes);
159     result.append("\n numECBytes: ");
160     result.append(numECBytes);
161     result.append("\n numRSBlocks: ");
162     result.append(numRSBlocks);
163     if (matrix == null) {
164       result.append("\n matrix: null\n");
165     } else {
166       result.append("\n matrix:\n");
167       result.append(matrix.toString());
168     }
169     result.append(">>\n");
170     return result.toString();
171   }
172
173   public void setMode(Mode value) {
174     mode = value;
175   }
176
177   public void setECLevel(ErrorCorrectionLevel value) {
178     ecLevel = value;
179   }
180
181   public void setVersion(int value) {
182     version = value;
183   }
184
185   public void setMatrixWidth(int value) {
186     matrixWidth = value;
187   }
188
189   public void setMaskPattern(int value) {
190     maskPattern = value;
191   }
192
193   public void setNumTotalBytes(int value) {
194     numTotalBytes = value;
195   }
196
197   public void setNumDataBytes(int value) {
198     numDataBytes = value;
199   }
200
201   public void setNumECBytes(int value) {
202     numECBytes = value;
203   }
204
205   public void setNumRSBlocks(int value) {
206     numRSBlocks = value;
207   }
208
209   // This takes ownership of the 2D array.
210   public void setMatrix(ByteMatrix value) {
211     matrix = value;
212   }
213
214   // Check if "mask_pattern" is valid.
215   public static boolean isValidMaskPattern(int maskPattern) {
216     return maskPattern >= 0 && maskPattern < NUM_MASK_PATTERNS;
217   }
218
219   // Return true if the all values in the matrix are binary numbers.
220   //
221   // JAVAPORT: This is going to be super expensive and unnecessary, we should not call this in
222   // production. I'm leaving it because it may be useful for testing. It should be removed entirely
223   // if ByteMatrix is changed never to contain a -1.
224   /*
225   private static boolean EverythingIsBinary(final ByteMatrix matrix) {
226     for (int y = 0; y < matrix.height(); ++y) {
227       for (int x = 0; x < matrix.width(); ++x) {
228         int value = matrix.get(y, x);
229         if (!(value == 0 || value == 1)) {
230           // Found non zero/one value.
231           return false;
232         }
233       }
234     }
235     return true;
236   }
237    */
238
239 }