mirror of
https://github.com/zxing/zxing.git
synced 2025-01-27 11:01:00 -08:00
Hunting an AIOOBE here -- clarified some code a little bit but still not seeing the issue
git-svn-id: https://zxing.googlecode.com/svn/trunk@2277 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
224568c732
commit
60288ac9e4
|
@ -42,8 +42,8 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
|||
// This class uses 5x5 blocks to compute local luminance, where each block is 8x8 pixels.
|
||||
// So this is the smallest dimension in each axis we can accept.
|
||||
private static final int BLOCK_SIZE_POWER = 3;
|
||||
private static final int BLOCK_SIZE = 1 << BLOCK_SIZE_POWER;
|
||||
private static final int BLOCK_SIZE_MASK = BLOCK_SIZE - 1;
|
||||
private static final int BLOCK_SIZE = 1 << BLOCK_SIZE_POWER; // ...0100...00
|
||||
private static final int BLOCK_SIZE_MASK = BLOCK_SIZE - 1; // ...0011...11
|
||||
private static final int MINIMUM_DIMENSION = BLOCK_SIZE * 5;
|
||||
private static final int MIN_DYNAMIC_RANGE = 24;
|
||||
|
||||
|
@ -53,19 +53,21 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
|||
super(source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the final BitMatrix once for all requests. This could be called once from the
|
||||
* constructor instead, but there are some advantages to doing it lazily, such as making
|
||||
* profiling easier, and not doing heavy lifting when callers don't expect it.
|
||||
*/
|
||||
@Override
|
||||
public BitMatrix getBlackMatrix() throws NotFoundException {
|
||||
// Calculates the final BitMatrix once for all requests. This could be called once from the
|
||||
// constructor instead, but there are some advantages to doing it lazily, such as making
|
||||
// profiling easier, and not doing heavy lifting when callers don't expect it.
|
||||
if (matrix != null) {
|
||||
return matrix;
|
||||
}
|
||||
LuminanceSource source = getLuminanceSource();
|
||||
if (source.getWidth() >= MINIMUM_DIMENSION && source.getHeight() >= MINIMUM_DIMENSION) {
|
||||
int width = source.getWidth();
|
||||
int height = source.getHeight();
|
||||
if (width >= MINIMUM_DIMENSION && height >= MINIMUM_DIMENSION) {
|
||||
byte[] luminances = source.getMatrix();
|
||||
int width = source.getWidth();
|
||||
int height = source.getHeight();
|
||||
int subWidth = width >> BLOCK_SIZE_POWER;
|
||||
if ((width & BLOCK_SIZE_MASK) != 0) {
|
||||
subWidth++;
|
||||
|
@ -77,8 +79,7 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
|||
int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height);
|
||||
|
||||
BitMatrix newMatrix = new BitMatrix(width, height);
|
||||
calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints,
|
||||
newMatrix);
|
||||
calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix);
|
||||
matrix = newMatrix;
|
||||
} else {
|
||||
// If the image is too small, fall back to the global histogram approach.
|
||||
|
@ -92,40 +93,56 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
|||
return new HybridBinarizer(source);
|
||||
}
|
||||
|
||||
// For each 8x8 block in the image, calculate the average black point using a 5x5 grid
|
||||
// of the blocks around it. Also handles the corner cases (fractional blocks are computed based
|
||||
// on the last 8 pixels in the row/column which are also used in the previous block).
|
||||
private static void calculateThresholdForBlock(byte[] luminances, int subWidth, int subHeight,
|
||||
int width, int height, int[][] blackPoints, BitMatrix matrix) {
|
||||
/**
|
||||
* For each block in the image, calculate the average black point using a 5x5 grid
|
||||
* of the blocks around it. Also handles the corner cases (fractional blocks are computed based
|
||||
* on the last pixels in the row/column which are also used in the previous block).
|
||||
*/
|
||||
private static void calculateThresholdForBlock(byte[] luminances,
|
||||
int subWidth,
|
||||
int subHeight,
|
||||
int width,
|
||||
int height,
|
||||
int[][] blackPoints,
|
||||
BitMatrix matrix) {
|
||||
for (int y = 0; y < subHeight; y++) {
|
||||
int yoffset = y << BLOCK_SIZE_POWER;
|
||||
if ((yoffset + BLOCK_SIZE) >= height) {
|
||||
yoffset = height - BLOCK_SIZE;
|
||||
int maxYOffset = height - BLOCK_SIZE;
|
||||
if (yoffset > maxYOffset) {
|
||||
yoffset = maxYOffset;
|
||||
}
|
||||
for (int x = 0; x < subWidth; x++) {
|
||||
int xoffset = x << BLOCK_SIZE_POWER;
|
||||
if ((xoffset + BLOCK_SIZE) >= width) {
|
||||
xoffset = width - BLOCK_SIZE;
|
||||
int maxXOffset = width - BLOCK_SIZE;
|
||||
if (xoffset > maxXOffset) {
|
||||
xoffset = maxXOffset;
|
||||
}
|
||||
int left = x > 1 ? x : 2;
|
||||
left = left < subWidth - 2 ? left : subWidth - 3;
|
||||
int top = y > 1 ? y : 2;
|
||||
top = top < subHeight - 2 ? top : subHeight - 3;
|
||||
int left = cap(x, 2, subWidth - 3);
|
||||
int top = cap(y, 2, subHeight - 3);
|
||||
int sum = 0;
|
||||
for (int z = -2; z <= 2; z++) {
|
||||
int[] blackRow = blackPoints[top + z];
|
||||
sum += blackRow[left - 2] + blackRow[left - 1] + blackRow[left] + blackRow[left + 1] +
|
||||
blackRow[left + 2];
|
||||
sum += blackRow[left - 2] + blackRow[left - 1] + blackRow[left] + blackRow[left + 1] + blackRow[left + 2];
|
||||
}
|
||||
int average = sum / 25;
|
||||
threshold8x8Block(luminances, xoffset, yoffset, average, width, matrix);
|
||||
thresholdBlock(luminances, xoffset, yoffset, average, width, matrix);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Applies a single threshold to an 8x8 block of pixels.
|
||||
private static void threshold8x8Block(byte[] luminances, int xoffset, int yoffset, int threshold,
|
||||
int stride, BitMatrix matrix) {
|
||||
private static int cap(int value, int min, int max) {
|
||||
return value < min ? min : value > max ? max : value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a single threshold to a block of pixels.
|
||||
*/
|
||||
private static void thresholdBlock(byte[] luminances,
|
||||
int xoffset,
|
||||
int yoffset,
|
||||
int threshold,
|
||||
int stride,
|
||||
BitMatrix matrix) {
|
||||
for (int y = 0, offset = yoffset * stride + xoffset; y < BLOCK_SIZE; y++, offset += stride) {
|
||||
for (int x = 0; x < BLOCK_SIZE; x++) {
|
||||
// Comparison needs to be <= so that black == 0 pixels are black even if the threshold is 0.
|
||||
|
@ -136,21 +153,28 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
|||
}
|
||||
}
|
||||
|
||||
// Calculates a single black point for each 8x8 block of pixels and saves it away.
|
||||
// See the following thread for a discussion of this algorithm:
|
||||
// http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
|
||||
private static int[][] calculateBlackPoints(byte[] luminances, int subWidth, int subHeight,
|
||||
int width, int height) {
|
||||
/**
|
||||
* Calculates a single black point for each block of pixels and saves it away.
|
||||
* See the following thread for a discussion of this algorithm:
|
||||
* http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
|
||||
*/
|
||||
private static int[][] calculateBlackPoints(byte[] luminances,
|
||||
int subWidth,
|
||||
int subHeight,
|
||||
int width,
|
||||
int height) {
|
||||
int[][] blackPoints = new int[subHeight][subWidth];
|
||||
for (int y = 0; y < subHeight; y++) {
|
||||
int yoffset = y << BLOCK_SIZE_POWER;
|
||||
if ((yoffset + BLOCK_SIZE) >= height) {
|
||||
yoffset = height - BLOCK_SIZE;
|
||||
int maxYOffset = height - BLOCK_SIZE;
|
||||
if (yoffset > maxYOffset) {
|
||||
yoffset = maxYOffset;
|
||||
}
|
||||
for (int x = 0; x < subWidth; x++) {
|
||||
int xoffset = x << BLOCK_SIZE_POWER;
|
||||
if ((xoffset + BLOCK_SIZE) >= width) {
|
||||
xoffset = width - BLOCK_SIZE;
|
||||
int maxXOffset = width - BLOCK_SIZE;
|
||||
if (xoffset > maxXOffset) {
|
||||
xoffset = maxXOffset;
|
||||
}
|
||||
int sum = 0;
|
||||
int min = 0xFF;
|
||||
|
|
Loading…
Reference in a new issue