Add a couple helpful methods

git-svn-id: https://zxing.googlecode.com/svn/trunk@1899 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2011-09-01 08:51:04 +00:00
parent 0519393846
commit 177e8728c9
3 changed files with 58 additions and 7 deletions

View file

@ -55,13 +55,8 @@ public final class RGBLuminanceSource extends LuminanceSource {
int r = (pixel >> 16) & 0xff; int r = (pixel >> 16) & 0xff;
int g = (pixel >> 8) & 0xff; int g = (pixel >> 8) & 0xff;
int b = pixel & 0xff; int b = pixel & 0xff;
if (r == g && g == b) { // Calculate luminance cheaply, favoring green.
// Image is already greyscale, so pick any channel. luminances[offset + x] = (byte) ((r + g + g + b) >> 2);
luminances[offset + x] = (byte) r;
} else {
// Calculate luminance cheaply, favoring green.
luminances[offset + x] = (byte) ((r + g + g + b) >> 2);
}
} }
} }
} }

View file

@ -110,4 +110,28 @@ public abstract class LuminanceSource {
throw new RuntimeException("This luminance source does not support rotation."); throw new RuntimeException("This luminance source does not support rotation.");
} }
public String toString() {
byte[] row = new byte[width];
StringBuffer result = new StringBuffer(height * (width + 1));
for (int y = 0; y < height; y++) {
row = getRow(y, row);
for (int x = 0; x < width; x++) {
int luminance = row[x] & 0xFF;
char c;
if (luminance < 0x40) {
c = '#';
} else if (luminance < 0x80) {
c = '+';
} else if (luminance < 0xC0) {
c = '.';
} else {
c = ' ';
}
result.append(c);
}
result.append('\n');
}
return result.toString();
}
} }

View file

@ -92,6 +92,38 @@ public final class BitArray {
bits[i >> 5] = newBits; bits[i >> 5] = newBits;
} }
/**
* Sets a range of bits.
*
* @param start start of range, inclusive.
* @param end end of range, exclusive
*/
public void setRange(int start, int end) {
if (end < start) {
throw new IllegalArgumentException();
}
if (end == start) {
return;
}
end--; // will be easier to treat this as the last actually set bit -- inclusive
int firstInt = start >> 5;
int lastInt = end >> 5;
for (int i = firstInt; i <= lastInt; i++) {
int firstBit = i > firstInt ? 0 : start & 0x1F;
int lastBit = i < lastInt ? 31 : end & 0x1F;
int mask;
if (firstBit == 0 && lastBit == 31) {
mask = -1;
} else {
mask = 0;
for (int j = firstBit; j <= lastBit; j++) {
mask |= 1 << j;
}
}
bits[i] |= mask;
}
}
/** /**
* Clears all bits (sets to false). * Clears all bits (sets to false).
*/ */