Try adding current javadoc to SVN
[zxing.git] / core / src / com / google / zxing / common / 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.common;
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 height() {
41     return height;
42   }
43
44   public int width() {
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 clear(byte value) {
65     for (int y = 0; y < height; ++y) {
66       for (int x = 0; x < width; ++x) {
67         bytes[y][x] = value;
68       }
69     }
70   }
71
72   public String toString() {
73     StringBuffer result = new StringBuffer();
74     for (int y = 0; y < height; ++y) {
75       for (int x = 0; x < width; ++x) {
76         switch (bytes[y][x]) {
77           case 0:
78             result.append(" 0");
79             break;
80           case 1:
81             result.append(" 1");
82             break;
83           default:
84             result.append("  ");
85             break;
86         }
87       }
88       result.append('\n');
89     }
90     return result.toString();
91   }
92
93 }