2 * Copyright 2013 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing;
20 * A wrapper implementation of {@link LuminanceSource} which inverts the luminances it returns -- black becomes
21 * white and vice versa, and each value becomes (255-value).
25 public final class InvertedLuminanceSource extends LuminanceSource {
27 private final LuminanceSource delegate;
29 public InvertedLuminanceSource(LuminanceSource delegate) {
30 super(delegate.getWidth(), delegate.getHeight());
31 this.delegate = delegate;
35 public byte[] getRow(int y, byte[] row) {
36 row = delegate.getRow(y, row);
37 int width = getWidth();
38 for (int i = 0; i < width; i++) {
39 row[i] = (byte) (255 - (row[i] & 0xFF));
45 public byte[] getMatrix() {
46 byte[] matrix = delegate.getMatrix();
47 int length = getWidth() * getHeight();
48 byte[] invertedMatrix = new byte[length];
49 for (int i = 0; i < length; i++) {
50 invertedMatrix[i] = (byte) (255 - (matrix[i] & 0xFF));
52 return invertedMatrix;
56 public boolean isCropSupported() {
57 return delegate.isCropSupported();
61 public LuminanceSource crop(int left, int top, int width, int height) {
62 return new InvertedLuminanceSource(delegate.crop(left, top, width, height));
66 public boolean isRotateSupported() {
67 return delegate.isRotateSupported();
71 * @return original delegate {@link LuminanceSource} since invert undoes itself
74 public LuminanceSource invert() {
79 public LuminanceSource rotateCounterClockwise() {
80 return new InvertedLuminanceSource(delegate.rotateCounterClockwise());
84 public LuminanceSource rotateCounterClockwise45() {
85 return new InvertedLuminanceSource(delegate.rotateCounterClockwise45());