056e2612f80410e3303509d025fa3047d69e7257
[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: I'm not happy about the argument ordering throughout the file, as I always like to have
24  * the horizontal component first, but this is for compatibility with the C++ code. The original
25  * code was a 2D array of ints, but since it only ever gets assigned -1, 0, and 1, I'm going to use
26  * less memory and go with bytes.
27  *
28  * @author dswitkin@google.com (Daniel Switkin)
29  */
30 public final class ByteMatrix {
31
32   private final byte[][] bytes;
33   private final int height;
34   private final int width;
35
36   public ByteMatrix(int height, int width) {
37     bytes = new byte[height][width];
38     this.height = height;
39     this.width = width;
40   }
41
42   public int height() {
43     return height;
44   }
45
46   public int width() {
47     return width;
48   }
49
50   public byte get(int y, int x) {
51     return bytes[y][x];
52   }
53
54   public byte[][] getArray() {
55     return bytes;
56   }
57
58   public void set(int y, int x, byte value) {
59     bytes[y][x] = value;
60   }
61
62   public void set(int y, int x, int value) {
63     bytes[y][x] = (byte) value;
64   }
65
66   public void clear(byte value) {
67     for (int y = 0; y < height; ++y) {
68       for (int x = 0; x < width; ++x) {
69         bytes[y][x] = value;
70       }
71     }
72   }
73
74   public String toString() {
75     StringBuffer result = new StringBuffer();
76     for (int y = 0; y < height; ++y) {
77       for (int x = 0; x < width; ++x) {
78         switch (bytes[y][x]) {
79           case 0:
80             result.append(" 0");
81             break;
82           case 1:
83             result.append(" 1");
84             break;
85           default:
86             result.append("  ");
87             break;
88         }
89       }
90       result.append("\n");
91     }
92     return result.toString();
93   }
94
95 }