Replace bit shifts that are really multiplication and division with simple operators now

This commit is contained in:
Sean Owen 2014-04-25 13:30:58 +01:00
parent 408c3848b1
commit 8af98a573f
32 changed files with 62 additions and 62 deletions

View file

@ -54,7 +54,7 @@ public final class RGBLuminanceSource extends LuminanceSource {
luminances[offset + x] = (byte) r;
} else {
// Calculate luminance cheaply, favoring green.
luminances[offset + x] = (byte) ((r + g + g + b) >> 2);
luminances[offset + x] = (byte) ((r + 2 * g + b) / 4);
}
}
}

View file

@ -323,7 +323,7 @@ public final class Decoder {
for (int i = startIndex; i < startIndex + length; i++) {
res <<= 1;
if (rawbits[i]) {
res++;
res |= 0x01;
}
}
return res;

View file

@ -51,7 +51,7 @@ public final class BitMatrix implements Cloneable {
}
this.width = width;
this.height = height;
this.rowSize = (width + 31) >> 5;
this.rowSize = (width + 31) / 32;
bits = new int[rowSize * height];
}
@ -70,7 +70,7 @@ public final class BitMatrix implements Cloneable {
* @return value of given bit in matrix
*/
public boolean get(int x, int y) {
int offset = y * rowSize + (x >> 5);
int offset = y * rowSize + (x / 32);
return ((bits[offset] >>> (x & 0x1f)) & 1) != 0;
}
@ -81,7 +81,7 @@ public final class BitMatrix implements Cloneable {
* @param y The vertical component (i.e. which row)
*/
public void set(int x, int y) {
int offset = y * rowSize + (x >> 5);
int offset = y * rowSize + (x / 32);
bits[offset] |= 1 << (x & 0x1f);
}
@ -92,7 +92,7 @@ public final class BitMatrix implements Cloneable {
* @param y The vertical component (i.e. which row)
*/
public void flip(int x, int y) {
int offset = y * rowSize + (x >> 5);
int offset = y * rowSize + (x / 32);
bits[offset] ^= 1 << (x & 0x1f);
}
@ -129,7 +129,7 @@ public final class BitMatrix implements Cloneable {
for (int y = top; y < bottom; y++) {
int offset = y * rowSize;
for (int x = left; x < right; x++) {
bits[offset + (x >> 5)] |= 1 << (x & 0x1f);
bits[offset + (x / 32)] |= 1 << (x & 0x1f);
}
}
}
@ -150,7 +150,7 @@ public final class BitMatrix implements Cloneable {
}
int offset = y * rowSize;
for (int x = 0; x < rowSize; x++) {
row.setBulk(x << 5, bits[offset + x]);
row.setBulk(x * 32, bits[offset + x]);
}
return row;
}
@ -248,7 +248,7 @@ public final class BitMatrix implements Cloneable {
return null;
}
int y = bitsOffset / rowSize;
int x = (bitsOffset % rowSize) << 5;
int x = (bitsOffset % rowSize) * 32;
int theBits = bits[bitsOffset];
int bit = 0;
@ -269,7 +269,7 @@ public final class BitMatrix implements Cloneable {
}
int y = bitsOffset / rowSize;
int x = (bitsOffset % rowSize) << 5;
int x = (bitsOffset % rowSize) * 32;
int theBits = bits[bitsOffset];
int bit = 31;

View file

@ -52,12 +52,12 @@ public final class DefaultGridSampler extends GridSampler {
throw NotFoundException.getNotFoundInstance();
}
BitMatrix bits = new BitMatrix(dimensionX, dimensionY);
float[] points = new float[dimensionX << 1];
float[] points = new float[2 * dimensionX];
for (int y = 0; y < dimensionY; y++) {
int max = points.length;
float iValue = (float) y + 0.5f;
for (int x = 0; x < max; x += 2) {
points[x] = (float) (x >> 1) + 0.5f;
points[x] = (float) (x / 2) + 0.5f;
points[x + 1] = iValue;
}
transform.transformPoints(points);
@ -68,7 +68,7 @@ public final class DefaultGridSampler extends GridSampler {
for (int x = 0; x < max; x += 2) {
if (image.get((int) points[x], (int) points[x + 1])) {
// Black(-ish) pixel
bits.set(x >> 1, y);
bits.set(x / 2, y);
}
}
} catch (ArrayIndexOutOfBoundsException aioobe) {

View file

@ -72,7 +72,7 @@ public class GlobalHistogramBinarizer extends Binarizer {
for (int x = 1; x < width - 1; x++) {
int right = localLuminances[x + 1] & 0xff;
// A simple -1 4 -1 box filter with a weight of 2.
int luminance = ((center << 2) - left - right) >> 1;
int luminance = ((center * 4) - left - right) / 2;
if (luminance < blackPoint) {
row.set(x);
}
@ -97,7 +97,7 @@ public class GlobalHistogramBinarizer extends Binarizer {
for (int y = 1; y < 5; y++) {
int row = height * y / 5;
byte[] localLuminances = source.getRow(row, luminances);
int right = (width << 2) / 5;
int right = (width * 4) / 5;
for (int x = width / 5; x < right; x++) {
int pixel = localLuminances[x] & 0xff;
localBuckets[pixel >> LUMINANCE_SHIFT]++;
@ -174,7 +174,7 @@ public class GlobalHistogramBinarizer extends Binarizer {
// If there is too little contrast in the image to pick a meaningful black point, throw rather
// than waste time trying to decode the image, and risk false positives.
if (secondPeak - firstPeak <= numBuckets >> 4) {
if (secondPeak - firstPeak <= numBuckets / 16) {
throw NotFoundException.getNotFoundInstance();
}

View file

@ -211,7 +211,7 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
//
// The default assumption is that the block is light/background. Since no estimate for
// the level of dark pixels exists locally, use half the min for the block.
average = min >> 1;
average = min / 2;
if (y > 0 && x > 0) {
// Correct the "white background" assumption for blocks that have neighbors by comparing
@ -221,8 +221,8 @@ public final class HybridBinarizer extends GlobalHistogramBinarizer {
// the boundaries is used for the interior.
// The (min < bp) is arbitrary but works better than other heuristics that were tried.
int averageNeighborBlackPoint = (blackPoints[y - 1][x] + (2 * blackPoints[y][x - 1]) +
blackPoints[y - 1][x - 1]) >> 2;
int averageNeighborBlackPoint =
(blackPoints[y - 1][x] + (2 * blackPoints[y][x - 1]) + blackPoints[y - 1][x - 1]) / 4;
if (min < averageNeighborBlackPoint) {
average = averageNeighborBlackPoint;
}

View file

@ -50,31 +50,31 @@ public final class MonochromeRectangleDetector {
public ResultPoint[] detect() throws NotFoundException {
int height = image.getHeight();
int width = image.getWidth();
int halfHeight = height >> 1;
int halfWidth = width >> 1;
int deltaY = Math.max(1, height / (MAX_MODULES << 3));
int deltaX = Math.max(1, width / (MAX_MODULES << 3));
int halfHeight = height / 2;
int halfWidth = width / 2;
int deltaY = Math.max(1, height / (MAX_MODULES * 8));
int deltaX = Math.max(1, width / (MAX_MODULES * 8));
int top = 0;
int bottom = height;
int left = 0;
int right = width;
ResultPoint pointA = findCornerFromCenter(halfWidth, 0, left, right,
halfHeight, -deltaY, top, bottom, halfWidth >> 1);
halfHeight, -deltaY, top, bottom, halfWidth / 2);
top = (int) pointA.getY() - 1;
ResultPoint pointB = findCornerFromCenter(halfWidth, -deltaX, left, right,
halfHeight, 0, top, bottom, halfHeight >> 1);
halfHeight, 0, top, bottom, halfHeight / 2);
left = (int) pointB.getX() - 1;
ResultPoint pointC = findCornerFromCenter(halfWidth, deltaX, left, right,
halfHeight, 0, top, bottom, halfHeight >> 1);
halfHeight, 0, top, bottom, halfHeight / 2);
right = (int) pointC.getX() + 1;
ResultPoint pointD = findCornerFromCenter(halfWidth, 0, left, right,
halfHeight, deltaY, top, bottom, halfWidth >> 1);
halfHeight, deltaY, top, bottom, halfWidth / 2);
bottom = (int) pointD.getY() + 1;
// Go try to find point A again with better information -- might have been off at first.
pointA = findCornerFromCenter(halfWidth, 0, left, right,
halfHeight, -deltaY, top, bottom, halfWidth >> 2);
halfHeight, -deltaY, top, bottom, halfWidth / 4);
return new ResultPoint[] { pointA, pointB, pointC, pointD };
}
@ -167,7 +167,7 @@ public final class MonochromeRectangleDetector {
*/
private int[] blackWhiteRange(int fixedDimension, int maxWhiteRun, int minDim, int maxDim, boolean horizontal) {
int center = (minDim + maxDim) >> 1;
int center = (minDim + maxDim) / 2;
// Scan left/up first
int start = center;

View file

@ -67,7 +67,7 @@ public final class GenericGF {
int x = 1;
for (int i = 0; i < size; i++) {
expTable[i] = x;
x <<= 1; // x = x * 2; we're assuming the generator alpha is 2
x *= 2; // we're assuming the generator alpha is 2
if (x >= size) {
x ^= primitive;
x &= size-1;

View file

@ -123,7 +123,7 @@ public final class DataMatrixReader implements Reader {
// Push in the "border" by half the module width so that we start
// sampling in the middle of the module. Just in case the image is a
// little off, this will help recover.
int nudge = moduleSize >> 1;
int nudge = moduleSize / 2;
top += nudge;
left += nudge;

View file

@ -370,7 +370,7 @@ public final class Detector {
int dx = Math.abs(toX - fromX);
int dy = Math.abs(toY - fromY);
int error = -dx >> 1;
int error = -dx / 2;
int ystep = fromY < toY ? 1 : -1;
int xstep = fromX < toX ? 1 : -1;
int transitions = 0;

View file

@ -81,7 +81,7 @@ public final class ErrorCorrection {
for (int i = 0; i < 255; i++) {
ALOG[i] = p;
LOG[p] = i;
p <<= 1;
p *= 2;
if (p >= 256) {
p ^= MODULO_VALUE;
}

View file

@ -136,7 +136,7 @@ public final class Code39Reader extends OneDReader {
int whiteSpaceAfterEnd = nextStart - lastStart - lastPatternSize;
// If 50% of last pattern size, following last pattern, is not whitespace, fail
// (but if it's whitespace to the very end of the image, that's OK)
if (nextStart != end && (whiteSpaceAfterEnd << 1) < lastPatternSize) {
if (nextStart != end && (whiteSpaceAfterEnd * 2) < lastPatternSize) {
throw NotFoundException.getNotFoundInstance();
}
@ -192,7 +192,7 @@ public final class Code39Reader extends OneDReader {
if (counterPosition == patternLength - 1) {
// Look for whitespace before start pattern, >= 50% of width of start pattern
if (toNarrowWidePattern(counters) == ASTERISK_ENCODING &&
row.isRange(Math.max(0, patternStart - ((i - patternStart) >> 1)), patternStart, false)) {
row.isRange(Math.max(0, patternStart - ((i - patternStart) / 2)), patternStart, false)) {
return new int[]{patternStart, i};
}
patternStart += counters[0] + counters[1];
@ -244,7 +244,7 @@ public final class Code39Reader extends OneDReader {
if (counter > maxNarrowCounter) {
wideCounters--;
// totalWideCountersWidth = 3 * average, so this checks if counter >= 3/2 * average
if ((counter << 1) >= totalWideCountersWidth) {
if ((counter * 2) >= totalWideCountersWidth) {
return -1;
}
}

View file

@ -157,7 +157,7 @@ public final class ITFReader extends OneDReader {
recordPattern(row, payloadStart, counterDigitPair);
// Split them into each array
for (int k = 0; k < 5; k++) {
int twoK = k << 1;
int twoK = 2 * k;
counterBlack[k] = counterDigitPair[twoK];
counterWhite[k] = counterDigitPair[twoK + 1];
}
@ -188,7 +188,7 @@ public final class ITFReader extends OneDReader {
// Determine the width of a narrow line in pixels. We can do this by
// getting the width of the start pattern and dividing by 4 because its
// made up of 4 narrow lines.
this.narrowLineWidth = (startPattern[1] - startPattern[0]) >> 2;
this.narrowLineWidth = (startPattern[1] - startPattern[0]) / 4;
validateQuietZone(row, startPattern[0]);

View file

@ -63,8 +63,8 @@ public final class ITFWriter extends OneDimensionalCodeWriter {
int two = Character.digit(contents.charAt(i+1), 10);
int[] encoding = new int[18];
for (int j = 0; j < 5; j++) {
encoding[j << 1] = ITFReader.PATTERNS[one][j];
encoding[(j << 1) + 1] = ITFReader.PATTERNS[two][j];
encoding[2 * j] = ITFReader.PATTERNS[one][j];
encoding[2 * j + 1] = ITFReader.PATTERNS[two][j];
}
pos += appendPattern(result, pos, encoding, true);
}

View file

@ -119,7 +119,7 @@ public abstract class OneDReader implements Reader {
for (int x = 0; x < maxLines; x++) {
// Scanning from the middle out. Determine which row we're looking at next:
int rowStepsAboveOrBelow = (x + 1) >> 1;
int rowStepsAboveOrBelow = (x + 1) / 2;
boolean isAbove = (x & 0x01) == 0; // i.e. is x even?
int rowNumber = middle + rowStep * (isAbove ? rowStepsAboveOrBelow : -rowStepsAboveOrBelow);
if (rowNumber < 0 || rowNumber >= height) {

View file

@ -228,7 +228,7 @@ public final class RSS14Reader extends AbstractRSSReader {
} else if (count > 8) {
count = 8;
}
int offset = i >> 1;
int offset = i / 2;
if ((i & 0x01) == 0) {
oddCounts[offset] = count;
oddRoundingErrors[offset] = value - count;

View file

@ -129,7 +129,7 @@ public final class RSSUtils {
/*
static int[] elements(int[] eDist, int N, int K) {
int[] widths = new int[eDist.length + 2];
int twoK = K << 1;
int twoK = 2 * K;
widths[0] = 1;
int i;
int minEven = 10;

View file

@ -40,7 +40,7 @@ final class BitArrayBuilder {
}
static BitArray buildBitArray(List<ExpandedPair> pairs) {
int charNumber = (pairs.size() << 1) - 1;
int charNumber = (pairs.size() * 2) - 1;
if (pairs.get(pairs.size() - 1).getRightChar() == null) {
charNumber -= 1;
}

View file

@ -639,7 +639,7 @@ public final class RSSExpandedReader extends AbstractRSSReader {
}
count = 8;
}
int offset = i >> 1;
int offset = i / 2;
if ((i & 0x01) == 0) {
oddCounts[offset] = count;
oddRoundingErrors[offset] = value - count;

View file

@ -83,7 +83,7 @@ public final class PDF417Common {
int first = 0;
int upto = SYMBOL_TABLE.length;
while (first < upto) {
int mid = (first + upto) >>> 1; // Compute mid point.
int mid = (first + upto) / 2; // Compute mid point.
if (symbol < SYMBOL_TABLE[mid]) {
upto = mid; // continue search in bottom half.
} else if (symbol > SYMBOL_TABLE[mid]) {

View file

@ -197,9 +197,9 @@ final class DecodedBitStreamParser {
*/
private static int textCompaction(int[] codewords, int codeIndex, StringBuilder result) {
// 2 character per codeword
int[] textCompactionData = new int[(codewords[0] - codeIndex) << 1];
int[] textCompactionData = new int[(codewords[0] - codeIndex) * 2];
// Used to hold the byte compaction value if there is a mode shift
int[] byteCompactionData = new int[(codewords[0] - codeIndex) << 1];
int[] byteCompactionData = new int[(codewords[0] - codeIndex) * 2];
int index = 0;
boolean end = false;

View file

@ -416,7 +416,7 @@ public final class PDF417ScanningDecoder {
if (leftToRight) {
endColumn = startColumn + codewordBitCount;
} else {
for (int i = 0; i < moduleBitCount.length >> 1; i++) {
for (int i = 0; i < moduleBitCount.length / 2; i++) {
int tmpCount = moduleBitCount[i];
moduleBitCount[i] = moduleBitCount[moduleBitCount.length - 1 - i];
moduleBitCount[moduleBitCount.length - 1 - i] = tmpCount;

View file

@ -90,8 +90,8 @@ public final class QRCodeWriter implements Writer {
}
int inputWidth = input.getWidth();
int inputHeight = input.getHeight();
int qrWidth = inputWidth + (quietZone << 1);
int qrHeight = inputHeight + (quietZone << 1);
int qrWidth = inputWidth + (quietZone * 2);
int qrHeight = inputHeight + (quietZone * 2);
int outputWidth = Math.max(width, qrWidth);
int outputHeight = Math.max(height, qrHeight);

View file

@ -101,7 +101,7 @@ final class BitMatrixParser {
int dimension = bitMatrix.getHeight();
int provisionalVersion = (dimension - 17) >> 2;
int provisionalVersion = (dimension - 17) / 4;
if (provisionalVersion <= 6) {
return Version.getVersionForNumber(provisionalVersion);
}

View file

@ -125,7 +125,7 @@ abstract class DataMask {
private static final class DataMask100 extends DataMask {
@Override
boolean isMasked(int i, int j) {
return (((i >>> 1) + (j /3)) & 0x01) == 0;
return (((i / 2) + (j /3)) & 0x01) == 0;
}
}

View file

@ -217,7 +217,7 @@ final class DecodedBitStreamParser {
Collection<byte[]> byteSegments,
Map<DecodeHintType,?> hints) throws FormatException {
// Don't crash trying to read more bits than we have available.
if (count << 3 > bits.available()) {
if (8 * count > bits.available()) {
throw FormatException.getFormatInstance();
}

View file

@ -94,7 +94,7 @@ public final class Version {
throw FormatException.getFormatInstance();
}
try {
return getVersionForNumber((dimension - 17) >> 2);
return getVersionForNumber((dimension - 17) / 4);
} catch (IllegalArgumentException ignored) {
throw FormatException.getFormatInstance();
}

View file

@ -88,13 +88,13 @@ final class AlignmentPatternFinder {
int startX = this.startX;
int height = this.height;
int maxJ = startX + width;
int middleI = startY + (height >> 1);
int middleI = startY + (height / 2);
// We are looking for black/white/black modules in 1:1:1 ratio;
// this tracks the number of black/white/black modules seen so far
int[] stateCount = new int[3];
for (int iGen = 0; iGen < height; iGen++) {
// Search from middle outwards
int i = middleI + ((iGen & 0x01) == 0 ? (iGen + 1) >> 1 : -((iGen + 1) >> 1));
int i = middleI + ((iGen & 0x01) == 0 ? (iGen + 1) / 2 : -((iGen + 1) / 2));
stateCount[0] = 0;
stateCount[1] = 0;
stateCount[2] = 0;

View file

@ -201,7 +201,7 @@ public class Detector {
float moduleSize) throws NotFoundException {
int tltrCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, topRight) / moduleSize);
int tlblCentersDimension = MathUtils.round(ResultPoint.distance(topLeft, bottomLeft) / moduleSize);
int dimension = ((tltrCentersDimension + tlblCentersDimension) >> 1) + 7;
int dimension = ((tltrCentersDimension + tlblCentersDimension) / 2) + 7;
switch (dimension & 0x03) { // mod 4
case 0:
dimension++;
@ -318,7 +318,7 @@ public class Detector {
int dx = Math.abs(toX - fromX);
int dy = Math.abs(toY - fromY);
int error = -dx >> 1;
int error = -dx / 2;
int xstep = fromX < toX ? 1 : -1;
int ystep = fromY < toY ? 1 : -1;

View file

@ -268,7 +268,7 @@ public final class Encoder {
* Terminate bits as described in 8.4.8 and 8.4.9 of JISX0510:2004 (p.24).
*/
static void terminateBits(int numDataBytes, BitArray bits) throws WriterException {
int capacity = numDataBytes << 3;
int capacity = numDataBytes * 8;
if (bits.getSize() > capacity) {
throw new WriterException("data bits cannot fit in the QR Code" + bits.getSize() + " > " +
capacity);

View file

@ -163,7 +163,7 @@ final class MaskUtil {
intermediate = (y + x) % 3;
break;
case 4:
intermediate = ((y >>> 1) + (x / 3)) & 0x1;
intermediate = ((y / 2) + (x / 3)) & 0x1;
break;
case 5:
temp = y * x;

View file

@ -28,7 +28,7 @@ public final class BitVectorTestCase extends Assert {
private static long getUnsignedInt(BitArray v, int index) {
long result = 0L;
for (int i = 0, offset = index << 3; i < 32; i++) {
for (int i = 0, offset = index * 8; i < 32; i++) {
if (v.get(offset + i)) {
result |= 1L << (31 - i);
}