/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Text;
namespace ZXing.QrCode.Internal
{
///
/// JAVAPORT: The original code was a 2D array of ints, but since it only ever gets assigned
/// 0, 1 and 2 I'm going to use less memory and go with bytes.
///
/// dswitkin@google.com (Daniel Switkin)
public sealed class ByteMatrix
{
private readonly byte[][] bytes;
private readonly int width;
private readonly int height;
///
/// Initializes a new instance of the class.
///
/// The width.
/// The height.
public ByteMatrix(int width, int height)
{
bytes = new byte[height][];
for (var i = 0; i < height; i++)
bytes[i] = new byte[width];
this.width = width;
this.height = height;
}
///
/// Gets the height.
///
public int Height
{
get { return height; }
}
///
/// Gets the width.
///
public int Width
{
get { return width; }
}
///
/// Gets or sets the with the specified x.
///
public int this[int x, int y]
{
get { return bytes[y][x]; }
set { bytes[y][x] = (byte)value; }
}
///
/// an internal representation as bytes, in row-major order. array[y][x] represents point (x,y)
///
public byte[][] Array
{
get { return bytes; }
}
///
/// Sets the specified x.
///
/// The x.
/// The y.
/// The value.
public void set(int x, int y, byte value)
{
bytes[y][x] = value;
}
///
/// Sets the specified x.
///
/// The x.
/// The y.
/// if set to true [value].
public void set(int x, int y, bool value)
{
bytes[y][x] = (byte)(value ? 1 : 0);
}
///
/// Clears the specified value.
///
/// The value.
public void clear(byte value)
{
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
bytes[y][x] = value;
}
}
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
override public String ToString()
{
var result = new StringBuilder(2 * width * height + 2);
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
switch (bytes[y][x])
{
case 0:
result.Append(" 0");
break;
case 1:
result.Append(" 1");
break;
default:
result.Append(" ");
break;
}
}
result.Append('\n');
}
return result.ToString();
}
}
}