- 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;