Issue 521, avoid an NPE
[zxing.git] / csharp / BinaryBitmap.cs
1 /*\r
2 * Copyright 2009 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 using BitArray = com.google.zxing.common.BitArray;\r
18 using BitMatrix = com.google.zxing.common.BitMatrix;\r
19 namespace com.google.zxing\r
20 {\r
21         \r
22         /// <summary> This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects\r
23         /// accept a BinaryBitmap and attempt to decode it.\r
24         /// \r
25         /// </summary>\r
26         /// <author>  dswitkin@google.com (Daniel Switkin)\r
27         /// </author>\r
28         /// <author>www.Redivivus.in (suraj.supekar@redivivus.in) - Ported from ZXING Java Source \r
29         /// </author>\r
30         \r
31         public sealed class BinaryBitmap\r
32         {\r
33                 /// <returns> The width of the bitmap.\r
34                 /// </returns>\r
35                 public int Width\r
36                 {\r
37                         get\r
38                         {\r
39                                 return binarizer.LuminanceSource.Width;\r
40                         }\r
41                         \r
42                 }\r
43                 /// <returns> The height of the bitmap.\r
44                 /// </returns>\r
45                 public int Height\r
46                 {\r
47                         get\r
48                         {\r
49                                 return binarizer.LuminanceSource.Height;\r
50                         }\r
51                         \r
52                 }\r
53                 /// <summary> Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive\r
54                 /// and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or\r
55                 /// may not apply sharpening. Therefore, a row from this matrix may not be identical to one\r
56                 /// fetched using getBlackRow(), so don't mix and match between them.\r
57                 /// \r
58                 /// </summary>\r
59                 /// <returns> The 2D array of bits for the image (true means black).\r
60                 /// </returns>\r
61                 public BitMatrix BlackMatrix\r
62                 {\r
63                         get\r
64                         {\r
65                                 // The matrix is created on demand the first time it is requested, then cached. There are two\r
66                                 // reasons for this:\r
67                                 // 1. This work will never be done if the caller only installs 1D Reader objects, or if a\r
68                                 //    1D Reader finds a barcode before the 2D Readers run.\r
69                                 // 2. This work will only be done once even if the caller installs multiple 2D Readers.\r
70                                 if (matrix == null)\r
71                                 {\r
72                                         matrix = binarizer.BlackMatrix;\r
73                                 }\r
74                                 return matrix;\r
75                         }\r
76                         \r
77                 }\r
78                 /// <returns> Whether this bitmap can be cropped.\r
79                 /// </returns>\r
80                 public bool CropSupported\r
81                 {\r
82                         get\r
83                         {\r
84                                 return binarizer.LuminanceSource.CropSupported;\r
85                         }\r
86                         \r
87                 }\r
88                 /// <returns> Whether this bitmap supports counter-clockwise rotation.\r
89                 /// </returns>\r
90                 public bool RotateSupported\r
91                 {\r
92                         get\r
93                         {\r
94                                 return binarizer.LuminanceSource.RotateSupported;\r
95                         }\r
96                         \r
97                 }\r
98                 \r
99                 //UPGRADE_NOTE: Final was removed from the declaration of 'binarizer '. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1003'"\r
100                 private Binarizer binarizer;\r
101                 private BitMatrix matrix;\r
102                 \r
103                 public BinaryBitmap(Binarizer binarizer)\r
104                 {\r
105                         if (binarizer == null)\r
106                         {\r
107                                 throw new System.ArgumentException("Binarizer must be non-null.");\r
108                         }\r
109                         this.binarizer = binarizer;\r
110                         matrix = null;\r
111                 }\r
112                 \r
113                 /// <summary> Converts one row of luminance data to 1 bit data. May actually do the conversion, or return\r
114                 /// cached data. Callers should assume this method is expensive and call it as seldom as possible.\r
115                 /// This method is intended for decoding 1D barcodes and may choose to apply sharpening.\r
116                 /// \r
117                 /// </summary>\r
118                 /// <param name="y">The row to fetch, 0 <= y < bitmap height.\r
119                 /// </param>\r
120                 /// <param name="row">An optional preallocated array. If null or too small, it will be ignored.\r
121                 /// If used, the Binarizer will call BitArray.clear(). Always use the returned object.\r
122                 /// </param>\r
123                 /// <returns> The array of bits for this row (true means black).\r
124                 /// </returns>\r
125                 public BitArray getBlackRow(int y, BitArray row)\r
126                 {\r
127                         return binarizer.getBlackRow(y, row);\r
128                 }\r
129                 \r
130                 /// <summary> Returns a new object with cropped image data. Implementations may keep a reference to the\r
131                 /// original data rather than a copy. Only callable if isCropSupported() is true.\r
132                 /// \r
133                 /// </summary>\r
134                 /// <param name="left">The left coordinate, 0 <= left < getWidth().\r
135                 /// </param>\r
136                 /// <param name="top">The top coordinate, 0 <= top <= getHeight().\r
137                 /// </param>\r
138                 /// <param name="width">The width of the rectangle to crop.\r
139                 /// </param>\r
140                 /// <param name="height">The height of the rectangle to crop.\r
141                 /// </param>\r
142                 /// <returns> A cropped version of this object.\r
143                 /// </returns>\r
144                 public BinaryBitmap crop(int left, int top, int width, int height)\r
145                 {\r
146                         LuminanceSource newSource = binarizer.LuminanceSource.crop(left, top, width, height);\r
147                         return new BinaryBitmap(binarizer.createBinarizer(newSource));\r
148                 }\r
149                 \r
150                 /// <summary> Returns a new object with rotated image data. Only callable if isRotateSupported() is true.\r
151                 /// \r
152                 /// </summary>\r
153                 /// <returns> A rotated version of this object.\r
154                 /// </returns>\r
155                 public BinaryBitmap rotateCounterClockwise()\r
156                 {\r
157                         LuminanceSource newSource = binarizer.LuminanceSource.rotateCounterClockwise();\r
158                         return new BinaryBitmap(binarizer.createBinarizer(newSource));\r
159                 }\r
160         }\r
161 }