mirror of
https://github.com/zxing/zxing.git
synced 2025-01-12 11:47:26 -08:00
Simplified some code (#596)
* Fixed bug #593 Bug #593 was misleadingly reported as pertaining to another project, but testing revealed it affected this project as well. The Code128 encoding algorithm failed when an odd number of digits were followed by FNC1 and other digits. The code selection algorithm was rewritten. A test case showing the bug was added, and an old test case was updated to expect the result of the new algorithm - another valid encoding of the same length as the old one. * Fixed bug #593 Bug #593 was misleadingly reported as pertaining to another project, but testing revealed it affected this project as well. The Code128 encoding algorithm failed when an odd number of digits were followed by FNC1 and other digits. The code selection algorithm was rewritten. A test case showing the bug was added, and an old test case was updated to expect the result of the new algorithm - another valid encoding of the same length as the old one. * Cleaning up Added a private enum and changed some names to improve readability. Also some trivial cosmetic changes. * Further cleanup of Code128 writer test Removed a private one-liner method used exactly once, alphabetized imports. * Simplify conversion RGB -> luminance Removed testing every pixel for greyness in RGBLuminanceSource, as it did not appreciably change the speed of converting a greyscale image and slowed down the conversion of a colour image by a few percent. Besides, there was no need to treat every row separately; the total number of pixels was enough. * Minor improvements to BitArray Simplified some code, added more accurate error checking, and corrected small inaccuracies in comments.
This commit is contained in:
parent
f0dfcdfed5
commit
cc16a72c85
|
@ -41,22 +41,17 @@ public final class RGBLuminanceSource extends LuminanceSource {
|
|||
|
||||
// In order to measure pure decoding speed, we convert the entire image to a greyscale array
|
||||
// up front, which is the same as the Y channel of the YUVLuminanceSource in the real app.
|
||||
luminances = new byte[width * height];
|
||||
for (int y = 0; y < height; y++) {
|
||||
int offset = y * width;
|
||||
for (int x = 0; x < width; x++) {
|
||||
int pixel = pixels[offset + x];
|
||||
int r = (pixel >> 16) & 0xff;
|
||||
int g = (pixel >> 8) & 0xff;
|
||||
int b = pixel & 0xff;
|
||||
if (r == g && g == b) {
|
||||
// Image is already greyscale, so pick any channel.
|
||||
luminances[offset + x] = (byte) r;
|
||||
} else {
|
||||
// Calculate luminance cheaply, favoring green.
|
||||
luminances[offset + x] = (byte) ((r + 2 * g + b) / 4);
|
||||
}
|
||||
}
|
||||
//
|
||||
// Total number of pixels suffices, can ignore shape
|
||||
int size = width * height;
|
||||
luminances = new byte[size];
|
||||
for (int offset = 0; offset < size; offset++) {
|
||||
int pixel = pixels[offset];
|
||||
int r = (pixel >> 16) & 0xff; // red
|
||||
int g2 = (pixel >> 7) & 0x1fe; // 2 * green
|
||||
int b = pixel & 0xff; // blue
|
||||
// Calculate green-favouring average cheaply
|
||||
luminances[offset] = (byte) ((r + g2 + b) / 4);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -151,7 +151,7 @@ public final class BitArray implements Cloneable {
|
|||
* @param end end of range, exclusive
|
||||
*/
|
||||
public void setRange(int start, int end) {
|
||||
if (end < start) {
|
||||
if (end < start || start < 0 || end > size) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if (end == start) {
|
||||
|
@ -163,15 +163,8 @@ public final class BitArray implements Cloneable {
|
|||
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;
|
||||
}
|
||||
}
|
||||
// Ones from firstBit to lastBit, inclusive
|
||||
int mask = (2 << lastBit) - (1 << firstBit);
|
||||
bits[i] |= mask;
|
||||
}
|
||||
}
|
||||
|
@ -193,10 +186,10 @@ public final class BitArray implements Cloneable {
|
|||
* @param end end of range, exclusive
|
||||
* @param value if true, checks that bits in range are set, otherwise checks that they are not set
|
||||
* @return true iff all bits are set or not set in range, according to value argument
|
||||
* @throws IllegalArgumentException if end is less than or equal to start
|
||||
* @throws IllegalArgumentException if end is less than start or the range is not contained in the array
|
||||
*/
|
||||
public boolean isRange(int start, int end, boolean value) {
|
||||
if (end < start) {
|
||||
if (end < start || start < 0 || end > size) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if (end == start) {
|
||||
|
@ -208,15 +201,8 @@ public final class BitArray implements Cloneable {
|
|||
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;
|
||||
}
|
||||
}
|
||||
// Ones from firstBit to lastBit, inclusive
|
||||
int mask = (2 << lastBit) - (1 << firstBit);
|
||||
|
||||
// Return false if we're looking for 1s and the masked bits[i] isn't all 1s (that is,
|
||||
// equals the mask, or we're looking for 0s and the masked portion is not all 0s
|
||||
|
@ -262,11 +248,11 @@ public final class BitArray implements Cloneable {
|
|||
}
|
||||
|
||||
public void xor(BitArray other) {
|
||||
if (bits.length != other.bits.length) {
|
||||
if (size != other.size) {
|
||||
throw new IllegalArgumentException("Sizes don't match");
|
||||
}
|
||||
for (int i = 0; i < bits.length; i++) {
|
||||
// The last byte could be incomplete (i.e. not have 8 bits in
|
||||
// The last int could be incomplete (i.e. not have 32 bits in
|
||||
// it) but there is no problem since 0 XOR 0 == 0.
|
||||
bits[i] ^= other.bits[i];
|
||||
}
|
||||
|
@ -321,16 +307,12 @@ public final class BitArray implements Cloneable {
|
|||
// now correct the int's if the bit size isn't a multiple of 32
|
||||
if (size != oldBitsLen * 32) {
|
||||
int leftOffset = oldBitsLen * 32 - size;
|
||||
int mask = 1;
|
||||
for (int i = 0; i < 31 - leftOffset; i++) {
|
||||
mask = (mask << 1) | 1;
|
||||
}
|
||||
int currentInt = (newBits[0] >> leftOffset) & mask;
|
||||
int currentInt = (newBits[0] >>> leftOffset);
|
||||
for (int i = 1; i < oldBitsLen; i++) {
|
||||
int nextInt = newBits[i];
|
||||
currentInt |= nextInt << (32 - leftOffset);
|
||||
newBits[i - 1] = currentInt;
|
||||
currentInt = (nextInt >> leftOffset) & mask;
|
||||
currentInt = (nextInt >>> leftOffset);
|
||||
}
|
||||
newBits[oldBitsLen - 1] = currentInt;
|
||||
}
|
||||
|
@ -372,4 +354,4 @@ public final class BitArray implements Cloneable {
|
|||
return new BitArray(bits.clone(), size);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue