Morgan's cosmetic improvement to a translation, and equivalent for other translations...
[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 using System;\r
17 namespace com.google.zxing.common\r
18 {\r
19         \r
20         /// <summary> A class which wraps a 2D array of bytes. The default usage is signed. If you want to use it as a\r
21         /// unsigned container, it's up to you to do byteValue & 0xff at each location.\r
22         /// \r
23         /// JAVAPORT: The original code was a 2D array of ints, but since it only ever gets assigned\r
24         /// -1, 0, and 1, I'm going to use less memory and go with bytes.\r
25         /// \r
26         /// </summary>\r
27         /// <author>  dswitkin@google.com (Daniel Switkin)\r
28         /// </author>\r
29         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
30         /// </author>\r
31         public sealed class ByteMatrix\r
32         {\r
33                 public int Height\r
34                 {\r
35                         get\r
36                         {\r
37                                 return height;\r
38                         }\r
39                         \r
40                 }\r
41                 public int Width\r
42                 {\r
43                         get\r
44                         {\r
45                                 return width;\r
46                         }\r
47                         \r
48                 }\r
49                 public sbyte[][] Array\r
50                 {\r
51                         get\r
52                         {\r
53                                 return bytes;\r
54                         }\r
55                         \r
56                 }\r
57                 \r
58                 //UPGRADE_NOTE: Final was removed from the declaration of 'bytes '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
59                 private sbyte[][] bytes;\r
60                 //UPGRADE_NOTE: Final was removed from the declaration of 'width '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
61                 private int width;\r
62                 //UPGRADE_NOTE: Final was removed from the declaration of 'height '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
63                 private int height;\r
64                 \r
65                 public ByteMatrix(int width, int height)\r
66                 {\r
67                         bytes = new sbyte[height][];\r
68                         for (int i = 0; i < height; i++)\r
69                         {\r
70                                 bytes[i] = new sbyte[width];\r
71                         }\r
72                         this.width = width;\r
73                         this.height = height;\r
74                 }\r
75                 \r
76                 public sbyte get_Renamed(int x, int y)\r
77                 {\r
78                         return bytes[y][x];\r
79                 }\r
80                 \r
81                 public void  set_Renamed(int x, int y, sbyte value_Renamed)\r
82                 {\r
83                         bytes[y][x] = value_Renamed;\r
84                 }\r
85                 \r
86                 public void  set_Renamed(int x, int y, int value_Renamed)\r
87                 {\r
88                         bytes[y][x] = (sbyte) value_Renamed;\r
89                 }\r
90                 \r
91                 public void  clear(sbyte value_Renamed)\r
92                 {\r
93                         for (int y = 0; y < height; ++y)\r
94                         {\r
95                                 for (int x = 0; x < width; ++x)\r
96                                 {\r
97                                         bytes[y][x] = value_Renamed;\r
98                                 }\r
99                         }\r
100                 }\r
101                 \r
102                 public override System.String ToString()\r
103                 {\r
104                         System.Text.StringBuilder result = new System.Text.StringBuilder(2 * width * height + 2);\r
105                         for (int y = 0; y < height; ++y)\r
106                         {\r
107                                 for (int x = 0; x < width; ++x)\r
108                                 {\r
109                                         switch (bytes[y][x])\r
110                                         {\r
111                                                 \r
112                                                 case 0: \r
113                                                         result.Append(" 0");\r
114                                                         break;\r
115                                                 \r
116                                                 case 1: \r
117                                                         result.Append(" 1");\r
118                                                         break;\r
119                                                 \r
120                                                 default: \r
121                                                         result.Append("  ");\r
122                                                         break;\r
123                                                 \r
124                                         }\r
125                                 }\r
126                                 result.Append('\n');\r
127                         }\r
128                         return result.ToString();\r
129                 }\r
130         }\r
131 }