001/*
002 * Copyright 2008 ZXing authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.itextpdf.text.pdf.qrcode;
018
019/**
020 * A class which wraps a 2D array of bytes. The default usage is signed. If you want to use it as a
021 * unsigned container, it's up to you to do byteValue & 0xff at each location.
022 *
023 * JAVAPORT: The original code was a 2D array of ints, but since it only ever gets assigned
024 * -1, 0, and 1, I'm going to use less memory and go with bytes.
025 *
026 * @author dswitkin@google.com (Daniel Switkin)
027 * @since 5.0.2
028 */
029public final class ByteMatrix {
030
031  private final byte[][] bytes;
032  private final int width;
033  private final int height;
034
035  public ByteMatrix(int width, int height) {
036    bytes = new byte[height][width];
037    this.width = width;
038    this.height = height;
039  }
040
041  public int getHeight() {
042    return height;
043  }
044
045  public int getWidth() {
046    return width;
047  }
048
049  public byte get(int x, int y) {
050    return bytes[y][x];
051  }
052
053  public byte[][] getArray() {
054    return bytes;
055  }
056
057  public void set(int x, int y, byte value) {
058    bytes[y][x] = value;
059  }
060
061  public void set(int x, int y, int value) {
062    bytes[y][x] = (byte) value;
063  }
064
065  public void clear(byte value) {
066    for (int y = 0; y < height; ++y) {
067      for (int x = 0; x < width; ++x) {
068        bytes[y][x] = value;
069      }
070    }
071  }
072
073  public String toString() {
074    StringBuffer result = new StringBuffer(2 * width * height + 2);
075    for (int y = 0; y < height; ++y) {
076      for (int x = 0; x < width; ++x) {
077        switch (bytes[y][x]) {
078          case 0:
079            result.append(" 0");
080            break;
081          case 1:
082            result.append(" 1");
083            break;
084          default:
085            result.append("  ");
086            break;
087        }
088      }
089      result.append('\n');
090    }
091    return result.toString();
092  }
093
094}