mirror of
https://github.com/zxing/zxing.git
synced 2025-02-02 05:41:08 -08:00
Issue 677, faster grayscale conversion from Wolfgang
git-svn-id: https://zxing.googlecode.com/svn/trunk@1697 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
4b20e8a00e
commit
2cd75045fd
1
AUTHORS
1
AUTHORS
|
@ -59,4 +59,5 @@ Suraj Supekar
|
||||||
Sven Klinkhamer
|
Sven Klinkhamer
|
||||||
Thomas Gerbet
|
Thomas Gerbet
|
||||||
Vince Francis (LifeMarks)
|
Vince Francis (LifeMarks)
|
||||||
|
Wolfgang Jung
|
||||||
Yakov Okshtein (Google)
|
Yakov Okshtein (Google)
|
||||||
|
|
|
@ -19,28 +19,27 @@ package com.google.zxing.client.j2se;
|
||||||
import com.google.zxing.LuminanceSource;
|
import com.google.zxing.LuminanceSource;
|
||||||
|
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.awt.geom.AffineTransform;
|
import java.awt.geom.AffineTransform;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This LuminanceSource implementation is meant for J2SE clients and our blackbox unit tests.
|
* This LuminanceSource implementation is meant for J2SE clients and our blackbox unit tests.
|
||||||
*
|
*
|
||||||
* @author dswitkin@google.com (Daniel Switkin)
|
* @author dswitkin@google.com (Daniel Switkin)
|
||||||
* @author Sean Owen
|
* @author Sean Owen
|
||||||
|
* @author code@elektrowolle.de (Wolfgang Jung)
|
||||||
*/
|
*/
|
||||||
public final class BufferedImageLuminanceSource extends LuminanceSource {
|
public final class BufferedImageLuminanceSource extends LuminanceSource {
|
||||||
|
|
||||||
private final BufferedImage image;
|
private final BufferedImage image;
|
||||||
private final int left;
|
private final int left;
|
||||||
private final int top;
|
private final int top;
|
||||||
private int[] rgbData;
|
|
||||||
|
|
||||||
public BufferedImageLuminanceSource(BufferedImage image) {
|
public BufferedImageLuminanceSource(BufferedImage image) {
|
||||||
this(image, 0, 0, image.getWidth(), image.getHeight());
|
this(image, 0, 0, image.getWidth(), image.getHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width,
|
public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {
|
||||||
int height) {
|
|
||||||
super(width, height);
|
super(width, height);
|
||||||
|
|
||||||
int sourceWidth = image.getWidth();
|
int sourceWidth = image.getWidth();
|
||||||
|
@ -48,14 +47,13 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
|
||||||
if (left + width > sourceWidth || top + height > sourceHeight) {
|
if (left + width > sourceWidth || top + height > sourceHeight) {
|
||||||
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
|
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
|
||||||
}
|
}
|
||||||
|
// Create a grayscale copy, no need to calculate the luminance manually
|
||||||
this.image = image;
|
this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
|
||||||
|
this.image.getGraphics().drawImage(image, 0, 0, null);
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.top = top;
|
this.top = top;
|
||||||
}
|
}
|
||||||
|
|
||||||
// These methods use an integer calculation for luminance derived from:
|
|
||||||
// <code>Y = 0.299R + 0.587G + 0.114B</code>
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] getRow(int y, byte[] row) {
|
public byte[] getRow(int y, byte[] row) {
|
||||||
if (y < 0 || y >= getHeight()) {
|
if (y < 0 || y >= getHeight()) {
|
||||||
|
@ -65,19 +63,8 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
|
||||||
if (row == null || row.length < width) {
|
if (row == null || row.length < width) {
|
||||||
row = new byte[width];
|
row = new byte[width];
|
||||||
}
|
}
|
||||||
|
// The underlying raster of image consists of bytes with the luminance values
|
||||||
if (rgbData == null || rgbData.length < width) {
|
image.getRaster().getDataElements(left, top + y, width, 1, row);
|
||||||
rgbData = new int[width];
|
|
||||||
}
|
|
||||||
image.getRGB(left, top + y, width, 1, rgbData, 0, width);
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
int pixel = rgbData[x];
|
|
||||||
int luminance = (306 * ((pixel >> 16) & 0xFF) +
|
|
||||||
601 * ((pixel >> 8) & 0xFF) +
|
|
||||||
117 * (pixel & 0xFF) +
|
|
||||||
(0x200)) >> 10; // 0x200 = 1<<9, half an lsb of the result to force rounding
|
|
||||||
row[x] = (byte) luminance;
|
|
||||||
}
|
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,20 +74,8 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
|
||||||
int height = getHeight();
|
int height = getHeight();
|
||||||
int area = width * height;
|
int area = width * height;
|
||||||
byte[] matrix = new byte[area];
|
byte[] matrix = new byte[area];
|
||||||
|
// The underlying raster of image consists of area bytes with the luminance values
|
||||||
int[] rgb = new int[area];
|
image.getRaster().getDataElements(left, top, width, height, matrix);
|
||||||
image.getRGB(left, top, width, height, rgb, 0, width);
|
|
||||||
for (int y = 0; y < height; y++) {
|
|
||||||
int offset = y * width;
|
|
||||||
for (int x = 0; x < width; x++) {
|
|
||||||
int pixel = rgb[offset + x];
|
|
||||||
int luminance = (306 * ((pixel >> 16) & 0xFF) +
|
|
||||||
601 * ((pixel >> 8) & 0xFF) +
|
|
||||||
117 * (pixel & 0xFF) +
|
|
||||||
(0x200)) >> 10; // 0x200 = 1<<9, half an lsb of the result to force rounding
|
|
||||||
matrix[offset + x] = (byte) luminance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,17 +89,21 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
|
||||||
return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
|
return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Can't run AffineTransforms on images of unknown format.
|
/**
|
||||||
|
* This is always true, since the image is a gray-scale image.
|
||||||
|
*
|
||||||
|
* @return true
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean isRotateSupported() {
|
public boolean isRotateSupported() {
|
||||||
return image.getType() != BufferedImage.TYPE_CUSTOM;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LuminanceSource rotateCounterClockwise() {
|
public LuminanceSource rotateCounterClockwise() {
|
||||||
if (!isRotateSupported()) {
|
//if (!isRotateSupported()) {
|
||||||
throw new IllegalStateException("Rotate not supported");
|
// throw new IllegalStateException("Rotate not supported");
|
||||||
}
|
//}
|
||||||
int sourceWidth = image.getWidth();
|
int sourceWidth = image.getWidth();
|
||||||
int sourceHeight = image.getHeight();
|
int sourceHeight = image.getHeight();
|
||||||
|
|
||||||
|
@ -132,7 +111,7 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
|
||||||
AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
|
AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
|
||||||
|
|
||||||
// Note width/height are flipped since we are rotating 90 degrees.
|
// Note width/height are flipped since we are rotating 90 degrees.
|
||||||
BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, image.getType());
|
BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
|
||||||
|
|
||||||
// Draw the original image into rotated, via transformation
|
// Draw the original image into rotated, via transformation
|
||||||
Graphics2D g = rotatedImage.createGraphics();
|
Graphics2D g = rotatedImage.createGraphics();
|
||||||
|
@ -141,8 +120,7 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
|
||||||
|
|
||||||
// Maintain the cropped region, but rotate it too.
|
// Maintain the cropped region, but rotate it too.
|
||||||
int width = getWidth();
|
int width = getWidth();
|
||||||
return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width),
|
return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
|
||||||
getHeight(), width);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue