- 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) {
- return computeRGBLuminance(rgbPixels[x + y * width]) < blackPoint;
- }
-
- public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
- if (row == null) {
- row = new BitArray(getWidth);
- } else {
- row.clear();
- }
- for (int i = 0, offset = y * width + startX; i < getWidth; i++, offset++) {
- if (computeRGBLuminance(rgbPixels[offset]) < blackPoint) {
- row.set(i);
- }
- }
- return row;
- }
-
- public int getHeight() {
- return height;
- }
-
- public int getWidth() {
- return width;
- }
-
- public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
- 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) {
- 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]++;
- }
- } else {
- throw new IllegalArgumentException("Unknown method: " + method);
- }
- blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
- lastMethod = method;
- lastArgument = argument;
- }