mirror of
https://github.com/zxing/zxing.git
synced 2025-03-05 20:48:51 -08:00
Brought this back into sync with BufferedImageMonochromeBitmapSource
git-svn-id: https://zxing.googlecode.com/svn/trunk@205 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
febdcf8d15
commit
4c4adc953e
|
@ -34,9 +34,6 @@ import com.google.zxing.common.BlackPointEstimator;
|
||||||
final class YUVMonochromeBitmapSource implements MonochromeBitmapSource {
|
final class YUVMonochromeBitmapSource implements MonochromeBitmapSource {
|
||||||
|
|
||||||
private final Bitmap image;
|
private final Bitmap image;
|
||||||
private final BitArray[] blackWhitePixels;
|
|
||||||
private final int width;
|
|
||||||
private final int height;
|
|
||||||
private int blackPoint;
|
private int blackPoint;
|
||||||
private BlackPointEstimationMethod lastMethod;
|
private BlackPointEstimationMethod lastMethod;
|
||||||
private int lastArgument;
|
private int lastArgument;
|
||||||
|
@ -46,108 +43,68 @@ final class YUVMonochromeBitmapSource implements MonochromeBitmapSource {
|
||||||
private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
|
private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
|
||||||
|
|
||||||
YUVMonochromeBitmapSource(Bitmap image) {
|
YUVMonochromeBitmapSource(Bitmap image) {
|
||||||
width = image.width();
|
|
||||||
height = image.height();
|
|
||||||
this.image = image;
|
this.image = image;
|
||||||
blackWhitePixels = new BitArray[height];
|
|
||||||
blackPoint = 0x7F;
|
blackPoint = 0x7F;
|
||||||
lastMethod = null;
|
lastMethod = null;
|
||||||
lastArgument = 0;
|
lastArgument = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isBlack(int x, int y) {
|
public boolean isBlack(int x, int y) {
|
||||||
BitArray blackWhite = blackWhitePixels[y];
|
return ((image.getPixel(x, y) >> 16) & 0xFF) < blackPoint;
|
||||||
if (blackWhite == null) {
|
|
||||||
blackWhite = parseBlackWhite(y);
|
|
||||||
}
|
|
||||||
return blackWhite.get(x);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
|
public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
|
||||||
BitArray blackWhite = blackWhitePixels[y];
|
|
||||||
if (blackWhite == null) {
|
|
||||||
blackWhite = parseBlackWhite(y);
|
|
||||||
}
|
|
||||||
if (row == null) {
|
if (row == null) {
|
||||||
if (startX == 0 && getWidth == width) {
|
|
||||||
return blackWhite;
|
|
||||||
}
|
|
||||||
row = new BitArray(getWidth);
|
row = new BitArray(getWidth);
|
||||||
} else {
|
} else {
|
||||||
row.clear();
|
row.clear();
|
||||||
}
|
}
|
||||||
|
int[] pixelRow = new int[getWidth];
|
||||||
|
image.getPixels(pixelRow, 0, getWidth, startX, y, getWidth, 1);
|
||||||
for (int i = 0; i < getWidth; i++) {
|
for (int i = 0; i < getWidth; i++) {
|
||||||
if (blackWhite.get(startX + i)) {
|
if (((pixelRow[i] >> 16) & 0xFF) < blackPoint) {
|
||||||
row.set(i);
|
row.set(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
|
|
||||||
private BitArray parseBlackWhite(int y) {
|
|
||||||
int width = this.width;
|
|
||||||
int[] pixelRow = new int[width];
|
|
||||||
image.getPixels(pixelRow, 0, width, 0, y, width, 1);
|
|
||||||
BitArray luminanceRow = new BitArray(width);
|
|
||||||
int blackPoint = this.blackPoint;
|
|
||||||
// Calculate 32 bits at a time to more efficiently set the bit array
|
|
||||||
int bits = 0;
|
|
||||||
int bitCount = 0;
|
|
||||||
for (int j = 0; j < width; j++) {
|
|
||||||
bits >>>= 1;
|
|
||||||
// Computation of luminance is inlined here for speed:
|
|
||||||
if (((pixelRow[j] >> 16) & 0xFF) <= blackPoint) {
|
|
||||||
bits |= 0x80000000;
|
|
||||||
}
|
|
||||||
if (++bitCount == 32) {
|
|
||||||
luminanceRow.setBulk(j, bits);
|
|
||||||
bits = 0;
|
|
||||||
bitCount = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (bitCount > 0) {
|
|
||||||
luminanceRow.setBulk(width, bits >>> (32 - bitCount));
|
|
||||||
}
|
|
||||||
blackWhitePixels[y] = luminanceRow;
|
|
||||||
return luminanceRow;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getHeight() {
|
public int getHeight() {
|
||||||
return height;
|
return image.height();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
return width;
|
return image.width();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
|
public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
|
||||||
if (!method.equals(lastMethod) || argument != lastArgument) {
|
if (!method.equals(lastMethod) || argument != lastArgument) {
|
||||||
for (int i = 0; i < blackWhitePixels.length; i++) {
|
int width = image.width();
|
||||||
blackWhitePixels[i] = null;
|
int height = image.height();
|
||||||
}
|
|
||||||
int[] histogram = new int[LUMINANCE_BUCKETS];
|
int[] histogram = new int[LUMINANCE_BUCKETS];
|
||||||
|
float biasTowardsWhite = 1.0f;
|
||||||
if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
|
if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
|
||||||
int minDimension = width < height ? width : height;
|
int minDimension = width < height ? width : height;
|
||||||
int startI = height == minDimension ? 0 : (height - width) >> 1;
|
int startI = height == minDimension ? 0 : (height - width) >> 1;
|
||||||
int startJ = width == minDimension ? 0 : (width - height) >> 1;
|
int startJ = width == minDimension ? 0 : (width - height) >> 1;
|
||||||
for (int n = 0; n < minDimension; n++) {
|
for (int n = 0; n < minDimension; n++) {
|
||||||
int pixel = image.getPixel(startJ + n, startI + n);
|
int pixel = image.getPixel(startJ + n, startI + n);
|
||||||
// Computation of luminance is inlined here for speed:
|
|
||||||
histogram[((pixel >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
|
histogram[((pixel >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
|
||||||
}
|
}
|
||||||
} else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
|
} else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
|
||||||
if (argument < 0 || argument >= height) {
|
if (argument < 0 || argument >= height) {
|
||||||
throw new IllegalArgumentException("Row is not within the image: " + argument);
|
throw new IllegalArgumentException("Row is not within the image: " + argument);
|
||||||
}
|
}
|
||||||
int[] yuvArray = new int[width];
|
biasTowardsWhite = 2.0f;
|
||||||
image.getPixels(yuvArray, 0, width, 0, argument, width, 1);
|
int[] pixelRow = new int[width];
|
||||||
|
image.getPixels(pixelRow, 0, width, 0, argument, width, 1);
|
||||||
for (int x = 0; x < width; x++) {
|
for (int x = 0; x < width; x++) {
|
||||||
histogram[((yuvArray[x] >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
|
histogram[((pixelRow[x] >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Unknown method: " + method);
|
throw new IllegalArgumentException("Unknown method: " + method);
|
||||||
}
|
}
|
||||||
blackPoint = BlackPointEstimator.estimate(histogram, 1.0f) << LUMINANCE_SHIFT;
|
blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
|
||||||
lastMethod = method;
|
lastMethod = method;
|
||||||
lastArgument = argument;
|
lastArgument = argument;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue