mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -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.
|
// 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.
|
// 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_POWER = 3;
|
||||||
private static final int BLOCK_SIZE = 1 << BLOCK_SIZE_POWER;
|
private static final int BLOCK_SIZE = 1 << BLOCK_SIZE_POWER; // ...0100...00
|
||||||
private static final int BLOCK_SIZE_MASK = BLOCK_SIZE - 1;
|
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 MINIMUM_DIMENSION = BLOCK_SIZE * 5;
|
||||||
private static final int MIN_DYNAMIC_RANGE = 24;
|
private static final int MIN_DYNAMIC_RANGE = 24;
|
||||||
|
|
||||||
|
@ -53,19 +53,21 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
||||||
super(source);
|
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
|
@Override
|
||||||
public BitMatrix getBlackMatrix() throws NotFoundException {
|
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) {
|
if (matrix != null) {
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
LuminanceSource source = getLuminanceSource();
|
LuminanceSource source = getLuminanceSource();
|
||||||
if (source.getWidth() >= MINIMUM_DIMENSION && source.getHeight() >= MINIMUM_DIMENSION) {
|
|
||||||
byte[] luminances = source.getMatrix();
|
|
||||||
int width = source.getWidth();
|
int width = source.getWidth();
|
||||||
int height = source.getHeight();
|
int height = source.getHeight();
|
||||||
|
if (width >= MINIMUM_DIMENSION && height >= MINIMUM_DIMENSION) {
|
||||||
|
byte[] luminances = source.getMatrix();
|
||||||
int subWidth = width >> BLOCK_SIZE_POWER;
|
int subWidth = width >> BLOCK_SIZE_POWER;
|
||||||
if ((width & BLOCK_SIZE_MASK) != 0) {
|
if ((width & BLOCK_SIZE_MASK) != 0) {
|
||||||
subWidth++;
|
subWidth++;
|
||||||
|
@ -77,8 +79,7 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
|
||||||
int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height);
|
int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height);
|
||||||
|
|
||||||
BitMatrix newMatrix = new BitMatrix(width, height);
|
BitMatrix newMatrix = new BitMatrix(width, height);
|
||||||
calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints,
|
calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix);
|
||||||
newMatrix);
|
|
||||||
matrix = newMatrix;
|
matrix = newMatrix;
|
||||||
} else {
|
} else {
|
||||||
// If the image is too small, fall back to the global histogram approach.
|
// 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);
|
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
|
* For each block in the image, calculate the average black point using a 5x5 grid
|
||||||
// on the last 8 pixels in the row/column which are also used in the previous block).
|
* of the blocks around it. Also handles the corner cases (fractional blocks are computed based
|
||||||
private static void calculateThresholdForBlock(byte[] luminances, int subWidth, int subHeight,
|
* on the last pixels in the row/column which are also used in the previous block).
|
||||||
int width, int height, int[][] blackPoints, BitMatrix matrix) {
|
*/
|
||||||
|
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++) {
|
for (int y = 0; y < subHeight; y++) {
|
||||||
int yoffset = y << BLOCK_SIZE_POWER;
|
int yoffset = y << BLOCK_SIZE_POWER;
|
||||||
if ((yoffset + BLOCK_SIZE) >= height) {
|
int maxYOffset = height - BLOCK_SIZE;
|
||||||
yoffset = height - BLOCK_SIZE;
|
if (yoffset > maxYOffset) {
|
||||||
|
yoffset = maxYOffset;
|
||||||
}
|
}
|
||||||
for (int x = 0; x < subWidth; x++) {
|
for (int x = 0; x < subWidth; x++) {
|
||||||
int xoffset = x << BLOCK_SIZE_POWER;
|
int xoffset = x << BLOCK_SIZE_POWER;
|
||||||
if ((xoffset + BLOCK_SIZE) >= width) {
|
int maxXOffset = width - BLOCK_SIZE;
|
||||||
xoffset = width - BLOCK_SIZE;
|
if (xoffset > maxXOffset) {
|
||||||
|
xoffset = maxXOffset;
|
||||||
}
|
}
|
||||||
int left = x > 1 ? x : 2;
|
int left = cap(x, 2, subWidth - 3);
|
||||||
left = left < subWidth - 2 ? left : subWidth - 3;
|
int top = cap(y, 2, subHeight - 3);
|
||||||
int top = y > 1 ? y : 2;
|
|
||||||
top = top < subHeight - 2 ? top : subHeight - 3;
|
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for (int z = -2; z <= 2; z++) {
|
for (int z = -2; z <= 2; z++) {
|
||||||
int[] blackRow = blackPoints[top + z];
|
int[] blackRow = blackPoints[top + z];
|
||||||
sum += blackRow[left - 2] + blackRow[left - 1] + blackRow[left] + blackRow[left + 1] +
|
sum += blackRow[left - 2] + blackRow[left - 1] + blackRow[left] + blackRow[left + 1] + blackRow[left + 2];
|
||||||
blackRow[left + 2];
|
|
||||||
}
|
}
|
||||||
int average = sum / 25;
|
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 int cap(int value, int min, int max) {
|
||||||
private static void threshold8x8Block(byte[] luminances, int xoffset, int yoffset, int threshold,
|
return value < min ? min : value > max ? max : value;
|
||||||
int stride, BitMatrix matrix) {
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 y = 0, offset = yoffset * stride + xoffset; y < BLOCK_SIZE; y++, offset += stride) {
|
||||||
for (int x = 0; x < BLOCK_SIZE; x++) {
|
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.
|
// 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:
|
* Calculates a single black point for each block of pixels and saves it away.
|
||||||
// http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
|
* See the following thread for a discussion of this algorithm:
|
||||||
private static int[][] calculateBlackPoints(byte[] luminances, int subWidth, int subHeight,
|
* http://groups.google.com/group/zxing/browse_thread/thread/d06efa2c35a7ddc0
|
||||||
int width, int height) {
|
*/
|
||||||
|
private static int[][] calculateBlackPoints(byte[] luminances,
|
||||||
|
int subWidth,
|
||||||
|
int subHeight,
|
||||||
|
int width,
|
||||||
|
int height) {
|
||||||
int[][] blackPoints = new int[subHeight][subWidth];
|
int[][] blackPoints = new int[subHeight][subWidth];
|
||||||
for (int y = 0; y < subHeight; y++) {
|
for (int y = 0; y < subHeight; y++) {
|
||||||
int yoffset = y << BLOCK_SIZE_POWER;
|
int yoffset = y << BLOCK_SIZE_POWER;
|
||||||
if ((yoffset + BLOCK_SIZE) >= height) {
|
int maxYOffset = height - BLOCK_SIZE;
|
||||||
yoffset = height - BLOCK_SIZE;
|
if (yoffset > maxYOffset) {
|
||||||
|
yoffset = maxYOffset;
|
||||||
}
|
}
|
||||||
for (int x = 0; x < subWidth; x++) {
|
for (int x = 0; x < subWidth; x++) {
|
||||||
int xoffset = x << BLOCK_SIZE_POWER;
|
int xoffset = x << BLOCK_SIZE_POWER;
|
||||||
if ((xoffset + BLOCK_SIZE) >= width) {
|
int maxXOffset = width - BLOCK_SIZE;
|
||||||
xoffset = width - BLOCK_SIZE;
|
if (xoffset > maxXOffset) {
|
||||||
|
xoffset = maxXOffset;
|
||||||
}
|
}
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
int min = 0xFF;
|
int min = 0xFF;
|
||||||
|
|
Loading…
Reference in a new issue