X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=javame%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fj2me%2FLCDUIImageMonochromeBitmapSource.java;h=bd6b1220822216b9d81a606bbe7fe3f37c3aae94;hb=f3de25e70f5a5cd2ba193d3df09ef1de0d47b01b;hp=aebe9a29668fb5a1ba64a0d23a8be20cc2dea01f;hpb=4aac2eac6df15f40b8e4342e0f7c7b5ab726fa53;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 aebe9a29..bd6b1220 100644 --- a/javame/src/com/google/zxing/client/j2me/LCDUIImageMonochromeBitmapSource.java +++ b/javame/src/com/google/zxing/client/j2me/LCDUIImageMonochromeBitmapSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2007 Google Inc. + * Copyright 2007 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,42 +23,24 @@ 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) + * @author Sean Owen + * @author Daniel Switkin (dswitkin@google.com) */ public final class LCDUIImageMonochromeBitmapSource extends BaseMonochromeBitmapSource { private final Image image; - private final int height; - private final int width; - private final int[] rgbRow; private final int[] pixelHolder; - private int cachedRow; public LCDUIImageMonochromeBitmapSource(Image image) { + super(image.getWidth(), image.getHeight()); this.image = image; - height = image.getHeight(); - width = image.getWidth(); - rgbRow = new int[width]; pixelHolder = new int[1]; - cachedRow = -1; } - public int getHeight() { - return height; - } - - public int getWidth() { - return width; - } - - public int getLuminance(int x, int y) { - int pixel; - if (cachedRow == y) { - pixel = rgbRow[x]; - } else { - image.getRGB(pixelHolder, 0, width, x, y, 1, 1); - pixel = pixelHolder[0]; - } + // This is expensive and should be used very sparingly. + protected int getLuminance(int x, int y) { + image.getRGB(pixelHolder, 0, getWidth(), x, y, 1, 1); + int pixel = pixelHolder[0]; // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that // the multiplies can be implemented as shifts. @@ -77,11 +59,35 @@ public final class LCDUIImageMonochromeBitmapSource extends BaseMonochromeBitmap (pixel & 0x000000FF )) >> 2; } - public void cacheRowForLuminance(int y) { - if (y != cachedRow) { - image.getRGB(rgbRow, 0, width, 0, y, width, 1); - cachedRow = y; + // For efficiency, the RGB data and the luminance data share the same array. + protected int[] getLuminanceRow(int y, int[] row) { + int width = getWidth(); + if (row == null || row.length < width) { + row = new int[width]; + } + image.getRGB(row, 0, width, 0, y, width, 1); + for (int x = 0; x < width; x++) { + int pixel = row[x]; + row[x] = (((pixel & 0x00FF0000) >> 16) + + ((pixel & 0x0000FF00) >> 7) + + (pixel & 0x000000FF )) >> 2; + } + return row; + } + + protected int[] getLuminanceColumn(int x, int[] column) { + int height = getHeight(); + if (column == null || column.length < height) { + column = new int[height]; + } + image.getRGB(column, 0, 1, x, 0, 1, height); + for (int y = 0; y < height; y++) { + int pixel = column[y]; + column[y] = (((pixel & 0x00FF0000) >> 16) + + ((pixel & 0x0000FF00) >> 7) + + (pixel & 0x000000FF )) >> 2; } + return column; } -} \ No newline at end of file +}