+++ /dev/null
-/*\r
-* Licensed under the Apache License, Version 2.0 (the "License");\r
-* you may not use this file except in compliance with the License.\r
-* You may obtain a copy of the License at\r
-*\r
-* http://www.apache.org/licenses/LICENSE-2.0\r
-*\r
-* Unless required by applicable law or agreed to in writing, software\r
-* distributed under the License is distributed on an "AS IS" BASIS,\r
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-* See the License for the specific language governing permissions and\r
-* limitations under the License.\r
-*/\r
-\r
-using System;\r
-using BitArray = com.google.zxing.common.BitArray;\r
-using com.google.zxing.common;\r
-namespace com.google.zxing\r
-{\r
- /**\r
- * @author dswitkin@google.com (Daniel Switkin)\r
- */\r
- public abstract class BaseMonochromeBitmapSource: MonochromeBitmapSource \r
- {\r
- private static int LUMINANCE_BITS = 5;\r
- private static int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;\r
- private static int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;\r
-\r
- private int blackPoint;\r
- private BlackPointEstimationMethod lastMethod;\r
- private int lastArgument;\r
- private int[] luminances;\r
-\r
- protected BaseMonochromeBitmapSource() {\r
- blackPoint = 0x7F;\r
- lastMethod = null;\r
- lastArgument = 0;\r
- }\r
-\r
- private void initLuminances() {\r
- if (luminances == null) {\r
- int width = getWidth();\r
- int height = getHeight();\r
- int max = width > height ? width : height;\r
- luminances = new int[max];\r
- }\r
- }\r
-\r
- public bool isBlack(int x, int y) {\r
- return getLuminance(x, y) < blackPoint;\r
- }\r
-\r
- public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {\r
- if (row == null || row.getSize() < getWidth) {\r
- row = new BitArray(getWidth);\r
- } else {\r
- row.clear();\r
- }\r
-\r
- // Reuse the same int array each time\r
- initLuminances();\r
- luminances = getLuminanceRow(y, luminances);\r
-\r
- // If the current decoder calculated the blackPoint based on one row, assume we're trying to\r
- // decode a 1D barcode, and apply some sharpening.\r
- if (lastMethod.Equals(BlackPointEstimationMethod.ROW_SAMPLING)) {\r
- int left = luminances[startX];\r
- int center = luminances[startX + 1];\r
- for (int x = 1; x < getWidth - 1; x++) {\r
- int right = luminances[startX + x + 1];\r
- // Simple -1 4 -1 box filter with a weight of 2\r
- int luminance = ((center << 2) - left - right) >> 1;\r
- if (luminance < blackPoint) {\r
- row.set(x);\r
- }\r
- left = center;\r
- center = right;\r
- }\r
- } else {\r
- for (int x = 0; x < getWidth; x++) {\r
- if (luminances[startX + x] < blackPoint) {\r
- row.set(x);\r
- }\r
- }\r
- }\r
- return row;\r
- }\r
-\r
- public BitArray getBlackColumn(int x, BitArray column, int startY, int getHeight) {\r
- if (column == null || column.getSize() < getHeight) {\r
- column = new BitArray(getHeight);\r
- } else {\r
- column.clear();\r
- }\r
-\r
- // Reuse the same int array each time\r
- initLuminances();\r
- luminances = getLuminanceColumn(x, luminances);\r
-\r
- // We don't handle "row sampling" specially here\r
- for (int y = 0; y < getHeight; y++) {\r
- if (luminances[startY + y] < blackPoint) {\r
- column.set(y);\r
- }\r
- }\r
- return column;\r
- }\r
-\r
- public void estimateBlackPoint(BlackPointEstimationMethod method, int argument){\r
- if (!method.Equals(lastMethod) || argument != lastArgument) {\r
- int width = getWidth();\r
- int height = getHeight();\r
- int[] histogram = new int[LUMINANCE_BUCKETS];\r
- if (method.Equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {\r
- int minDimension = width < height ? width : height;\r
- int startX = (width - minDimension) >> 1;\r
- int startY = (height - minDimension) >> 1;\r
- for (int n = 0; n < minDimension; n++) {\r
- int luminance = getLuminance(startX + n, startY + n);\r
- histogram[luminance >> LUMINANCE_SHIFT]++;\r
- }\r
- } else if (method.Equals(BlackPointEstimationMethod.ROW_SAMPLING)) {\r
- if (argument < 0 || argument >= height) {\r
- throw new Exception("Row is not within the image: " + argument);\r
- }\r
- initLuminances();\r
- luminances = getLuminanceRow(argument, luminances);\r
- for (int x = 0; x < width; x++) {\r
- histogram[luminances[x] >> LUMINANCE_SHIFT]++;\r
- }\r
- } else {\r
- throw new Exception("Unknown method: " + method);\r
- }\r
- blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;\r
- lastMethod = method;\r
- lastArgument = argument;\r
- }\r
- }\r
-\r
- public BlackPointEstimationMethod getLastEstimationMethod() {\r
- return lastMethod;\r
- }\r
-\r
- public MonochromeBitmapSource rotateCounterClockwise() {\r
- throw new Exception("Rotate not supported");\r
- }\r
-\r
- public bool isRotateSupported() {\r
- return false;\r
- }\r
-\r
- // These two methods should not need to exist because they are defined in the interface that\r
- // this abstract class implements. However this seems to cause problems on some Nokias. \r
- // So we write these redundant declarations.\r
-\r
- public abstract int getHeight();\r
-\r
- public abstract int getWidth();\r
-\r
- /**\r
- * Retrieves the luminance at the pixel x,y in the bitmap. This method is only used for estimating\r
- * the black point and implementing getBlackRow() - it is not meant for decoding, hence it is not\r
- * part of MonochromeBitmapSource itself, and is protected.\r
- *\r
- * @param x The x coordinate in the image.\r
- * @param y The y coordinate in the image.\r
- * @return The luminance value between 0 and 255.\r
- */\r
- protected abstract int getLuminance(int x, int y);\r
-\r
- /**\r
- * This is the main mechanism for retrieving luminance data. It is dramatically more efficient\r
- * than repeatedly calling getLuminance(). As above, this is not meant for decoders.\r
- *\r
- * @param y The row to fetch\r
- * @param row The array to write luminance values into. It is <b>strongly</b> suggested that you\r
- * allocate this yourself, making sure row.length >= getWidth(), and reuse the same\r
- * array on subsequent calls for performance. If you pass null, you will be flogged,\r
- * but then I will take pity on you and allocate a sufficient array internally.\r
- * @return The array containing the luminance data. This is the same as row if it was usable.\r
- */\r
- protected abstract int[] getLuminanceRow(int y, int[] row);\r
-\r
- /**\r
- * The same as getLuminanceRow(), but for columns.\r
- *\r
- * @param x The column to fetch\r
- * @param column The array to write luminance values into. See above.\r
- * @return The array containing the luminance data.\r
- */\r
- protected abstract int[] getLuminanceColumn(int x, int[] column);\r
-\r
- }\r
-}\r
-\r
-\r