X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=javame%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fj2me%2FLCDUIImageMonochromeBitmapSource.java;h=fbbf0da6636e4764285f979dffe35c5d24cf6d37;hb=cca18e08cfdac97b03ed00143a24cc88cd2fe865;hp=164562875ca571465560f2e5165be926e788e405;hpb=19da9f871ea823b4010cfbb215d74d8dbdd49f9e;p=zxing.git diff --git a/javame/src/com/google/zxing/client/j2me/LCDUIImageMonochromeBitmapSource.java b/javame/src/com/google/zxing/client/j2me/LCDUIImageMonochromeBitmapSource.java index 16456287..fbbf0da6 100644 --- a/javame/src/com/google/zxing/client/j2me/LCDUIImageMonochromeBitmapSource.java +++ b/javame/src/com/google/zxing/client/j2me/LCDUIImageMonochromeBitmapSource.java @@ -16,8 +16,8 @@ package com.google.zxing.client.j2me; -import com.google.zxing.MonochromeBitmapSource; import com.google.zxing.BlackPointEstimationMethod; +import com.google.zxing.MonochromeBitmapSource; import com.google.zxing.common.BitArray; import com.google.zxing.common.BlackPointEstimator; @@ -25,23 +25,30 @@ import javax.microedition.lcdui.Image; /** *

An implementation based on Java ME's {@link Image} representation.

- * + * * @author Sean Owen (srowen@google.com), Daniel Switkin (dswitkin@google.com) */ -final class LCDUIImageMonochromeBitmapSource implements MonochromeBitmapSource { +public final class LCDUIImageMonochromeBitmapSource implements MonochromeBitmapSource { private final int[] rgbPixels; private final int width; private final int height; private int blackPoint; private BlackPointEstimationMethod lastMethod; + private int lastArgument; - LCDUIImageMonochromeBitmapSource(final Image image) { + private static final int LUMINANCE_BITS = 5; + private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS; + private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS; + + public LCDUIImageMonochromeBitmapSource(Image image) { width = image.getWidth(); height = image.getHeight(); rgbPixels = new int[width * height]; image.getRGB(rgbPixels, 0, width, 0, 0, width, height); blackPoint = 0x7F; + lastMethod = null; + lastArgument = 0; } public boolean isBlack(int x, int y) { @@ -71,27 +78,44 @@ final class LCDUIImageMonochromeBitmapSource implements MonochromeBitmapSource { } public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) { - if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) { - if (!BlackPointEstimationMethod.TWO_D_SAMPLING.equals(lastMethod)) { - int[] luminanceBuckets = new int[32]; + if (!method.equals(lastMethod) || argument != lastArgument) { + int[] histogram = new int[LUMINANCE_BUCKETS]; + float biasTowardsWhite = 1.0f; + if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) { int minDimension = width < height ? width : height; for (int n = 0, offset = 0; n < minDimension; n++, offset += width + 1) { - luminanceBuckets[computeRGBLuminance(rgbPixels[offset]) >> 3]++; + histogram[computeRGBLuminance(rgbPixels[offset]) >> LUMINANCE_SHIFT]++; + } + } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) { + if (argument < 0 || argument >= height) { + throw new IllegalArgumentException("Row is not within the image: " + argument); + } + biasTowardsWhite = 2.0f; + int offset = argument * width; + for (int x = 0; x < width; x++) { + histogram[computeRGBLuminance(rgbPixels[offset + x]) >> LUMINANCE_SHIFT]++; } - blackPoint = BlackPointEstimator.estimate(luminanceBuckets) << 3; + } else { + throw new IllegalArgumentException("Unknown method: " + method); } - } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) { - // TODO - } else { - throw new IllegalArgumentException("Unknown method: " + method); + blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT; + lastMethod = method; + lastArgument = argument; } - lastMethod = method; } public BlackPointEstimationMethod getLastEstimationMethod() { return lastMethod; } + public MonochromeBitmapSource rotateCounterClockwise() { + throw new IllegalStateException("Rotate not supported"); + } + + public boolean isRotateSupported() { + return false; + } + /** * Extracts luminance from a pixel from this source. By default, the source is assumed to use RGB, * so this implementation computes luminance is a function of a red, green and blue components as