Small style stuff
[zxing.git] / core / src / com / google / zxing / qrcode / encoder / ByteMatrix.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 /**
20  * A class which wraps a 2D array of bytes. The default usage is signed. If you want to use it as a
21  * unsigned container, it's up to you to do byteValue & 0xff at each location.
22  *
23  * JAVAPORT: The original code was a 2D array of ints, but since it only ever gets assigned
24  * -1, 0, and 1, I'm going to use less memory and go with bytes.
25  *
26  * @author dswitkin@google.com (Daniel Switkin)
27  */
28 public final class ByteMatrix {
29
30   private final byte[][] bytes;
31   private final int width;
32   private final int height;
33
34   public ByteMatrix(int width, int height) {
35     bytes = new byte[height][width];
36     this.width = width;
37     this.height = height;
38   }
39
40   public int getHeight() {
41     return height;
42   }
43
44   public int getWidth() {
45     return width;
46   }
47
48   public byte get(int x, int y) {
49     return bytes[y][x];
50   }
51
52   public byte[][] getArray() {
53     return bytes;
54   }
55
56   public void set(int x, int y, byte value) {
57     bytes[y][x] = value;
58   }
59
60   public void set(int x, int y, int value) {
61     bytes[y][x] = (byte) value;
62   }
63
64   public void set(int x, int y, boolean value) {
65     bytes[y][x] = (byte) (value ? 1 : 0);
66   }
67
68   public void clear(byte value) {
69     for (int y = 0; y < height; ++y) {
70       for (int x = 0; x < width; ++x) {
71         bytes[y][x] = value;
72       }
73     }
74   }
75
76   public String toString() {
77     StringBuffer result = new StringBuffer(2 * width * height + 2);
78     for (int y = 0; y < height; ++y) {
79       for (int x = 0; x < width; ++x) {
80         switch (bytes[y][x]) {
81           case 0:
82             result.append(" 0");
83             break;
84           case 1:
85             result.append(" 1");
86             break;
87           default:
88             result.append("  ");
89             break;
90         }
91       }
92       result.append('\n');
93     }
94     return result.toString();
95   }
96
97 }