Committed C# port from Mohamad
[zxing.git] / csharp / common / ByteMatrix.cs
1 /*\r
2 * Copyright 2008 ZXing authors\r
3 *\r
4 * Licensed under the Apache License, Version 2.0 (the "License");\r
5 * you may not use this file except in compliance with the License.\r
6 * You may obtain a copy of the License at\r
7 *\r
8 *      http://www.apache.org/licenses/LICENSE-2.0\r
9 *\r
10 * Unless required by applicable law or agreed to in writing, software\r
11 * distributed under the License is distributed on an "AS IS" BASIS,\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13 * See the License for the specific language governing permissions and\r
14 * limitations under the License.\r
15 */\r
16 namespace com.google.zxing.common\r
17 {\r
18     using System;\r
19     using System.Text;\r
20 \r
21     /// <summary> A class which wraps a 2D array of bytes. The default usage is signed. If you want to use it as a\r
22     /// unsigned container, it's up to you to do byteValue & 0xff at each location.\r
23     /// *\r
24     /// JAVAPORT: I'm not happy about the argument ordering throughout the file, as I always like to have\r
25     /// the horizontal component first, but this is for compatibility with the C++ code. The original\r
26     /// code was a 2D array of ints, but since it only ever gets assigned -1, 0, and 1, I'm going to use\r
27     /// less memory and go with bytes.\r
28     /// *\r
29     /// </summary>\r
30     /// <author>  dswitkin@google.com (Daniel Switkin)\r
31     /// \r
32     /// </author>\r
33     public sealed class ByteMatrix\r
34     {\r
35         private sbyte[][] bytes;\r
36         private int Height;\r
37         private int Width;\r
38 \r
39 \r
40     public ByteMatrix(int height, int width) {\r
41         bytes = new sbyte[height][];\r
42         for (int i = 0; i < height; i++) {\r
43             bytes[i] = new sbyte[width];\r
44         }\r
45         this.Height = height;\r
46         this.Width = width;\r
47     }\r
48 \r
49         public int height()\r
50         {\r
51             return Height;\r
52         }\r
53 \r
54         public int width()\r
55         {\r
56             return Width;\r
57         }\r
58 \r
59         public sbyte get(int y, int x)\r
60         {\r
61             return bytes[y][x];\r
62         }\r
63 \r
64         public sbyte[][] getArray()\r
65         {\r
66             return bytes;\r
67         }\r
68 \r
69         public void set(int y, int x, sbyte value)\r
70         {\r
71             bytes[y][x] = value;\r
72         }\r
73 \r
74         public void set(int y, int x, int value)\r
75         {\r
76             bytes[y][x] = (sbyte)value;\r
77         }\r
78 \r
79         public void clear(sbyte value)\r
80         {\r
81             for (int y = 0; y < Height; ++y)\r
82             {\r
83                 for (int x = 0; x < Width; ++x)\r
84                 {\r
85                     bytes[y][x] = value;\r
86                 }\r
87             }\r
88         }\r
89 \r
90         public String toString()\r
91         {\r
92             StringBuilder result = new StringBuilder();\r
93             for (int y = 0; y < Height; ++y)\r
94             {\r
95                 for (int x = 0; x < Width; ++x)\r
96                 {\r
97                     switch (bytes[y][x])\r
98                     {\r
99                         case 0:\r
100                             result.Append(" 0");\r
101                             break;\r
102                         case 1:\r
103                             result.Append(" 1");\r
104                             break;\r
105                         default:\r
106                             result.Append("  ");\r
107                             break;\r
108                     }\r
109                 }\r
110                 result.Append('\n');\r
111             }\r
112             return result.ToString();\r
113         }        \r
114     }\r
115 }