]> Pileus Git - ~andy/freeotp/blob - src/com/google/zxing/InvertedLuminanceSource.java
Add native camera support
[~andy/freeotp] / src / com / google / zxing / InvertedLuminanceSource.java
1 /*
2  * Copyright 2013 ZXing authors
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing;
18
19 /**
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).
22  * 
23  * @author Sean Owen
24  */
25 public final class InvertedLuminanceSource extends LuminanceSource {
26
27   private final LuminanceSource delegate;
28
29   public InvertedLuminanceSource(LuminanceSource delegate) {
30     super(delegate.getWidth(), delegate.getHeight());
31     this.delegate = delegate;
32   }
33
34   @Override
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));
40     }
41     return row;
42   }
43
44   @Override
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));
51     }
52     return invertedMatrix;
53   }
54   
55   @Override
56   public boolean isCropSupported() {
57     return delegate.isCropSupported();
58   }
59
60   @Override
61   public LuminanceSource crop(int left, int top, int width, int height) {
62     return new InvertedLuminanceSource(delegate.crop(left, top, width, height));
63   }
64
65   @Override
66   public boolean isRotateSupported() {
67     return delegate.isRotateSupported();
68   }
69
70   /**
71    * @return original delegate {@link LuminanceSource} since invert undoes itself
72    */
73   @Override
74   public LuminanceSource invert() {
75     return delegate;
76   }
77
78   @Override
79   public LuminanceSource rotateCounterClockwise() {
80     return new InvertedLuminanceSource(delegate.rotateCounterClockwise());
81   }
82
83   @Override
84   public LuminanceSource rotateCounterClockwise45() {
85     return new InvertedLuminanceSource(delegate.rotateCounterClockwise45());
86   }
87
88 }